Spicy
library.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <string_view>
7 
8 #include <hilti/rt/filesystem.h>
9 #include <hilti/rt/result.h>
10 #include <hilti/rt/util.h>
11 
12 namespace hilti::rt::library {
13 
18 struct Version {
19  std::string magic;
20  uint64_t hilti_version;
21  bool debug;
23  hilti::rt::filesystem::path path;
27  std::string toJSON() const;
28 
34  static hilti::rt::Result<Version> fromJSON(const std::string& json);
35 
40  void checkCompatibility() const;
41 
42  friend bool operator==(const Version& a, const Version& b) {
43  return a.magic == b.magic && a.hilti_version == b.hilti_version && a.debug == b.debug;
44  }
45 
46  friend bool operator!=(const Version& a, const Version& b) { return ! (a == b); }
47 };
48 
49 } // namespace hilti::rt::library
50 
51 namespace hilti::rt {
52 
59 class Library {
60 public:
67  Library(const hilti::rt::filesystem::path& path);
68  ~Library();
69 
70  // Since this library has exclusive ownership of some path it cannot be copied.
71  Library(const Library&) = delete;
72  Library& operator=(const Library&) = delete;
73 
74  Library(Library&&) = default;
75  Library& operator=(Library&&) = default;
76 
83 
90  hilti::rt::Result<Nothing> save(const hilti::rt::filesystem::path& path) const;
91 
92  // Gets a symbol from the library.
93  //
94  // @param name name of the symbol
95  // @return a valid pointer to the symbol or an error
96  hilti::rt::Result<void*> symbol(std::string_view name) const;
97 
98  /*
99  * Remove the file corresponding to this library without unloading it.
100  *
101  * @return nothing or an error
102  */
103  hilti::rt::Result<Nothing> remove() const;
104 
105 private:
106  hilti::rt::filesystem::path _path; // Absolute path to the physical file wrapped by this instance.
107  mutable void* _handle = nullptr; // Handle to the library.
108 };
109 
110 } // namespace hilti::rt
std::string toJSON() const
Definition: library.cc:17
Definition: library.h:18
Definition: any.h:7
Definition: library.h:12
void checkCompatibility() const
Definition: library.cc:40
uint64_t hilti_version
Definition: library.h:20
hilti::rt::filesystem::path path
Definition: library.h:23
Definition: library.h:59
bool debug
Definition: library.h:21
static hilti::rt::Result< Version > fromJSON(const std::string &json)
Definition: library.cc:25
std::string magic
Definition: library.h:19
Definition: result.h:67