Spicy
library.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <cstdint>
6 #include <optional>
7 #include <string>
8 
9 #include <hilti/rt/filesystem.h>
10 #include <hilti/rt/result.h>
11 #include <hilti/rt/util.h>
12 
13 namespace hilti::rt::library {
14 
19 struct Version {
20  std::string magic;
21  uint64_t hilti_version;
22  bool debug;
24  hilti::rt::filesystem::path path;
28  std::string toJSON() const;
29 
35  static hilti::rt::Result<Version> fromJSON(const std::string& json);
36 
41  void checkCompatibility() const;
42 
43  friend bool operator==(const Version& a, const Version& b) {
44  return a.magic == b.magic && a.hilti_version == b.hilti_version && a.debug == b.debug;
45  }
46 
47  friend bool operator!=(const Version& a, const Version& b) { return ! (a == b); }
48 };
49 
50 } // namespace hilti::rt::library
51 
52 namespace hilti::rt {
53 
60 class Library {
61 public:
68  Library(const hilti::rt::filesystem::path& path);
69  ~Library();
70 
71  // Since this library has exclusive ownership of some path it cannot be copied.
72  Library(const Library&) = delete;
73  Library& operator=(const Library&) = delete;
74 
75  Library(Library&&) = default;
76  Library& operator=(Library&&) = default;
77 
84 
91  hilti::rt::Result<Nothing> save(const hilti::rt::filesystem::path& path) const;
92 
93  // Gets a symbol from the library.
94  //
95  // @param name name of the symbol
96  // @return a valid pointer to the symbol or an error
97  hilti::rt::Result<void*> symbol(const char* name) const;
98 
99  /*
100  * Remove the file corresponding to this library without unloading it.
101  *
102  * @return nothing or an error
103  */
104  hilti::rt::Result<Nothing> remove() const;
105 
120  static void setScope(uint64_t* scope);
121 
122 private:
123  hilti::rt::filesystem::path _path; // Absolute path to the physical file wrapped by this instance.
124  mutable void* _handle = nullptr; // Handle to the library.
125 
126  static std::optional<hilti::rt::filesystem::path>
127  _current_path; // absolute path to library currently being loaded, if any
128 };
129 
130 } // namespace hilti::rt
Definition: library.h:60
Library(const hilti::rt::filesystem::path &path)
Definition: library.cc:57
hilti::rt::Result< hilti::rt::library::Version > open() const
Definition: library.cc:72
static void setScope(uint64_t *scope)
Definition: library.cc:201
hilti::rt::Result< Nothing > save(const hilti::rt::filesystem::path &path) const
Definition: library.cc:165
Definition: result.h:73
Definition: any.h:7
Definition: library.h:19
std::string magic
Definition: library.h:20
static hilti::rt::Result< Version > fromJSON(const std::string &json)
Definition: library.cc:34
hilti::rt::filesystem::path path
Definition: library.h:24
uint64_t hilti_version
Definition: library.h:21
void checkCompatibility() const
Definition: library.cc:49
bool debug
Definition: library.h:22
std::string toJSON() const
Definition: library.cc:27