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;
21  double created;
22  bool debug;
23  bool optimize;
25  hilti::rt::filesystem::path path;
29  std::string toJSON() const;
30 
36  static hilti::rt::Result<Version> fromJSON(const std::string& json);
37 
42  void checkCompatibility() const;
43 
44  friend bool operator==(const Version& a, const Version& b) {
45  return a.magic == b.magic && a.hilti_version == b.hilti_version && a.created == b.created &&
46  a.debug == b.debug && a.optimize == b.optimize;
47  }
48 
49  friend bool operator!=(const Version& a, const Version& b) { return ! (a == b); }
50 };
51 
52 } // namespace hilti::rt::library
53 
54 namespace hilti::rt {
55 
62 class Library {
63 public:
70  Library(const hilti::rt::filesystem::path& path);
71  ~Library();
72 
73  // Since this library has exclusive ownership of some path it cannot be copied.
74  Library(const Library&) = delete;
75  Library& operator=(const Library&) = delete;
76 
77  Library(Library&&) = default;
78  Library& operator=(Library&&) = default;
79 
86 
93  hilti::rt::Result<Nothing> save(const hilti::rt::filesystem::path& path) const;
94 
95  // Gets a symbol from the library.
96  //
97  // @param name name of the symbol
98  // @return a valid pointer to the symbol or an error
99  hilti::rt::Result<void*> symbol(std::string_view name) const;
100 
101  /*
102  * Remove the file corresponding to this library without unloading it.
103  *
104  * @return nothing or an error
105  */
106  hilti::rt::Result<Nothing> remove() const;
107 
108 private:
109  hilti::rt::filesystem::path _path; // Absolute path to the physical file wrapped by this instance.
110  mutable void* _handle = nullptr; // Handle to the library.
111 };
112 
113 } // namespace hilti::rt
std::string toJSON() const
Definition: library.cc:16
Definition: library.h:18
Definition: any.h:7
Definition: library.h:12
void checkCompatibility() const
Definition: library.cc:44
int hilti_version
Definition: library.h:20
bool optimize
Definition: library.h:23
hilti::rt::filesystem::path path
Definition: library.h:25
Definition: library.h:62
bool debug
Definition: library.h:22
static hilti::rt::Result< Version > fromJSON(const std::string &json)
Definition: library.cc:27
double created
Definition: library.h:21
std::string magic
Definition: library.h:19
Definition: result.h:67