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