summaryrefslogtreecommitdiff
path: root/cpp/pom.hpp
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2025-09-16 15:02:39 -0400
committerpommicket <pommicket@gmail.com>2025-09-16 15:02:39 -0400
commit7ededc16f36099acbfac457f8e4e268879b0ce62 (patch)
tree35543f851383a5002566b3b0a8ea5f38d97e94c7 /cpp/pom.hpp
parent76f68d6c93b55f9cd96a369bf2e5785ed16fa184 (diff)
Finish up implementation of C++ library
Diffstat (limited to 'cpp/pom.hpp')
-rw-r--r--cpp/pom.hpp43
1 files changed, 37 insertions, 6 deletions
diff --git a/cpp/pom.hpp b/cpp/pom.hpp
index bf16a29..f9d7475 100644
--- a/cpp/pom.hpp
+++ b/cpp/pom.hpp
@@ -4,6 +4,8 @@
#include <exception>
#include <cstdint>
#include <string_view>
+#include <string>
+#include <vector>
#include <memory>
#include <optional>
@@ -12,10 +14,11 @@ namespace pom {
class Error: public std::exception {
public:
~Error();
+ Error(Error &other) = delete;
std::string_view file() const noexcept;
uint64_t line() const noexcept;
std::string_view message() const noexcept;
- const Error *next() const noexcept;
+ inline const Error *next() const noexcept { return m_next.get(); }
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
@@ -31,10 +34,11 @@ private:
bool m_is_original;
std::unique_ptr<const Error> m_next;
};
+std::ostream &operator<<(std::ostream &, Error &);
class Allocator {
public:
- virtual ~Allocator() = 0;
+ inline virtual ~Allocator() {};
virtual void *calloc(size_t, size_t) = 0;
virtual void *realloc(void *, size_t) = 0;
virtual void free(void *) = 0;
@@ -42,9 +46,9 @@ public:
class Settings {
public:
- Settings();
+ inline Settings() {};
/// Set allocator.
- void set_allocator(std::shared_ptr<Allocator> allocator) {
+ inline void set_allocator(std::shared_ptr<Allocator> allocator) {
m_allocator = allocator;
}
void set_error_language(std::string_view lang);
@@ -61,22 +65,49 @@ public:
virtual size_t read(char *buf, size_t count) = 0;
};
+/// An item in a configuration
+///
+/// This is an abstract class so that items can be given
+/// more members in the future.
+class Item {
+public:
+ inline virtual ~Item() {};
+ virtual std::string_view key() const noexcept = 0;
+ virtual std::string_view value() const noexcept = 0;
+ virtual std::string_view file() const noexcept = 0;
+ virtual uint64_t line() const noexcept = 0;
+};
+
class Configuration {
public:
Configuration();
+ Configuration &operator=(const Configuration &other);
+ inline Configuration(const Configuration &other) { *this = other; };
Configuration(std::string_view filename, Reader &source, const Settings *settings = nullptr);
Configuration(std::string_view filename, std::istream &stream, const Settings *settings = nullptr);
Configuration(std::string_view path, const Settings *settings = nullptr);
Configuration(std::string_view filename, std::string_view string, const Settings *settings = nullptr);
~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;
+ std::optional<std::string> get(std::string_view key) const;
+ std::string get_or_default(std::string_view key, std::string_view dflt) const;
+ std::optional<int64_t> get_int(std::string_view key) const;
+ int64_t get_int_or_default(std::string_view key, int64_t dflt) const;
+ std::optional<uint64_t> get_uint(std::string_view key) const;
+ uint64_t get_uint_or_default(std::string_view key, uint64_t dflt) const;
+ std::optional<double> get_float(std::string_view key) const;
+ double get_float_or_default(std::string_view key, double dflt) const;
+ std::optional<bool> get_bool(std::string_view key) const;
+ bool get_bool_or_default(std::string_view key, bool dflt) const;
Configuration section(std::string_view name) const;
+ std::vector<std::string> unread_keys() const;
+ std::vector<std::string> keys() const;
+ std::vector<std::shared_ptr<Item>> items() const;
void merge(const Configuration &other);
private:
explicit Configuration(void *c): C(c) {}
void *C;
};
+std::ostream &operator<<(std::ostream &, const Configuration &);