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