summaryrefslogtreecommitdiff
path: root/cpp/pom.hpp
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2025-09-16 14:03:51 -0400
committerpommicket <pommicket@gmail.com>2025-09-16 14:03:51 -0400
commit9f817b370e55c24db5dee3f6948d2d95df7c3207 (patch)
tree5a315d38ab6e607ab80953f266bc319958e149dd /cpp/pom.hpp
parent9c4f73d970d549388661bbf274393f613cff511d (diff)
Start C++ library
Diffstat (limited to 'cpp/pom.hpp')
-rw-r--r--cpp/pom.hpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/cpp/pom.hpp b/cpp/pom.hpp
new file mode 100644
index 0000000..e3c66d4
--- /dev/null
+++ b/cpp/pom.hpp
@@ -0,0 +1,86 @@
+#ifndef POM_HPP_
+#define POM_HPP_
+
+#include <exception>
+#include <cstdint>
+#include <string_view>
+#include <memory>
+#include <optional>
+
+namespace pom {
+
+class Error: public std::exception {
+public:
+ ~Error();
+ std::string_view file() const noexcept;
+ uint64_t line() const noexcept;
+ std::string_view message() const noexcept;
+ const Error *next() const noexcept;
+ std::string_view to_string() noexcept;
+ /// You should only call this on the first error in an error list.
+ /// (This can't be enforced with constness because it needs to
+ /// override `std::exception::what`.)
+ virtual const char *what() const noexcept override;
+private:
+ friend class Configuration;
+ friend class Parser;
+ Error() = delete;
+ Error(void *C_error);
+ Error(const void *C_error);
+ void *C;
+ bool m_is_original;
+ std::unique_ptr<const Error> m_next;
+};
+
+class Allocator {
+public:
+ virtual ~Allocator() = 0;
+ virtual void *calloc(size_t, size_t) = 0;
+ virtual void *realloc(void *, size_t) = 0;
+ virtual void free(void *) = 0;
+};
+
+class Settings {
+public:
+ Settings();
+ /// Set allocator.
+ void set_allocator(std::shared_ptr<Allocator> allocator) {
+ m_allocator = allocator;
+ }
+ void set_error_language(std::string_view lang);
+private:
+ void to_C(void *C) const;
+ friend class Configuration;
+ char m_error_lang[16] = {};
+ std::shared_ptr<Allocator> m_allocator;
+ uint32_t version = 1; // future-proofing
+};
+
+class Reader {
+public:
+ virtual size_t read(char *buf, size_t count) = 0;
+};
+
+class Configuration {
+public:
+ Configuration();
+ ~Configuration();
+ std::optional<std::string_view> get(std::string_view key) const;
+ std::string_view get_or_default(std::string_view key, std::string_view dflt) const;
+ Configuration section(std::string_view name) const;
+ void merge(const Configuration &other);
+ static Configuration load(std::string_view filename, Reader &source, const Settings *settings = nullptr);
+ static Configuration load(std::string_view filename, std::istream &stream, const Settings *settings = nullptr);
+ static Configuration load(std::string_view path, const Settings *settings = nullptr);
+ static Configuration load(std::string_view filename, std::string_view string, const Settings *settings = nullptr);
+private:
+ explicit Configuration(void *c);
+ void *C;
+};
+
+
+
+
+} // namespace pom
+
+#endif // POM_HPP_