diff options
Diffstat (limited to 'src/include/tome')
-rw-r--r-- | src/include/tome/enum_string_map.hpp | 6 | ||||
-rw-r--r-- | src/include/tome/pp/global_constexpr.hpp | 21 | ||||
-rw-r--r-- | src/include/tome/squelch/automatizer.hpp | 16 | ||||
-rw-r--r-- | src/include/tome/squelch/condition.hpp | 90 | ||||
-rw-r--r-- | src/include/tome/squelch/rule.hpp | 14 | ||||
-rw-r--r-- | src/include/tome/squelch/tree_printer.hpp | 4 |
6 files changed, 86 insertions, 65 deletions
diff --git a/src/include/tome/enum_string_map.hpp b/src/include/tome/enum_string_map.hpp index 8ae1e115..814827fe 100644 --- a/src/include/tome/enum_string_map.hpp +++ b/src/include/tome/enum_string_map.hpp @@ -29,20 +29,20 @@ public: assert(bimap.size() == in.size()); } - const char *stringify(E e) { + const char *stringify(E e) const { auto i = bimap.left.find(e); assert(i != bimap.left.end() && "Missing mapping for enumerated value"); return i->second.c_str(); } - E parse(const char *s) { + E parse(const char *s) const { E e; bool result = parse(s, &e); assert(result && "Missing string->enum mapping"); return e; } - bool parse(const char *s, E *e) { + bool parse(const char *s, E *e) const { auto i = bimap.right.find(s); if (i == bimap.right.end()) { diff --git a/src/include/tome/pp/global_constexpr.hpp b/src/include/tome/pp/global_constexpr.hpp new file mode 100644 index 00000000..83168c59 --- /dev/null +++ b/src/include/tome/pp/global_constexpr.hpp @@ -0,0 +1,21 @@ +#pragma once + +/** + * Macro for declaring a global constexpr variable without + * violating the ODR and without running into the SIOF. + * + * Shamelessly cribbed from http://stackoverflow.com/a/20374473 + */ +#define PP_GLOBAL_CONSTEXPR_CONST(type, var, value) \ +namespace global_constexpr { namespace var { \ +template<class = void> \ +struct wrapper \ +{ \ + static constexpr type var = value; \ +}; \ +template<class T> \ +constexpr type wrapper<T>::var; \ +} } \ +namespace { \ +auto const& var = global_constexpr::var::wrapper<>::var; \ +} diff --git a/src/include/tome/squelch/automatizer.hpp b/src/include/tome/squelch/automatizer.hpp index 786ca1ae..833b5648 100644 --- a/src/include/tome/squelch/automatizer.hpp +++ b/src/include/tome/squelch/automatizer.hpp @@ -2,8 +2,8 @@ #include <boost/noncopyable.hpp> #include <memory> +#include <jsoncons/json.hpp> #include <vector> -#include <jansson.h> #include "tome/squelch/rule_fwd.hpp" #include "tome/squelch/cursor_fwd.hpp" @@ -44,15 +44,14 @@ public: bool apply_rules(object_type *o_ptr, int item_idx) const; /** - * Build a JSON data structure to represent - * all the rules. + * Build a JSON document to represent all the rules. */ - std::shared_ptr<json_t> to_json() const; + jsoncons::json to_json() const; /** - * Load rules from a JSON data structure. + * Load rules from a JSON document. */ - void load_json(json_t *json); + void load_json(jsoncons::json const &); /** * Remove currently selected condition or rule. @@ -115,10 +114,9 @@ public: void add_new_condition(std::function<std::shared_ptr<Condition> ()> factory); /** - * Get rule names. The names are not stable across multiple - * calls to methods on this class. + * Get rule names. */ - void get_rule_names(std::vector<const char *> *names) const; + std::vector<std::string> get_rule_names() const; /** * Get current number of rules. diff --git a/src/include/tome/squelch/condition.hpp b/src/include/tome/squelch/condition.hpp index 5d1240f5..584ecb0e 100644 --- a/src/include/tome/squelch/condition.hpp +++ b/src/include/tome/squelch/condition.hpp @@ -3,10 +3,10 @@ #include "tome/squelch/condition_fwd.hpp" #include <boost/noncopyable.hpp> +#include <cstdint> #include <functional> #include <memory> -#include <cstdint> -#include <jansson.h> +#include <jsoncons/json.hpp> #include "tome/squelch/cursor_fwd.hpp" #include "tome/squelch/tree_printer_fwd.hpp" @@ -60,7 +60,7 @@ public: } public: - json_t *to_json() const; + jsoncons::json to_json() const; virtual void add_child(ConditionFactory const &factory) { // Default is to not support children. @@ -88,16 +88,16 @@ public: /** * Parse condition from JSON */ - static std::shared_ptr<Condition> parse_condition(json_t *); + static std::shared_ptr<Condition> parse_condition(jsoncons::json const &); /** * Convert an (optional) condition to JSON. */ - static json_t *optional_to_json(std::shared_ptr<Condition> condition); + static jsoncons::json optional_to_json(std::shared_ptr<Condition> condition); protected: virtual void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const = 0; - virtual void to_json(json_t *) const = 0; + virtual void to_json(jsoncons::json &) const = 0; // What do we want to match? match_type match; @@ -116,12 +116,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: uint8_t m_tval; @@ -140,12 +140,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: std::string m_name; @@ -164,12 +164,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: std::string m_contain; @@ -189,12 +189,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: uint8_t m_min; @@ -228,10 +228,10 @@ public: virtual std::shared_ptr<Condition> next_child(Condition *current) override; // Parse a list of conditions from JSON property - static std::vector< std::shared_ptr<Condition> > parse_conditions(json_t *); + static std::vector< std::shared_ptr<Condition> > parse_conditions(jsoncons::json const &); protected: - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; protected: std::vector< std::shared_ptr<Condition> > m_conditions; @@ -248,7 +248,7 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; @@ -265,7 +265,7 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; @@ -285,12 +285,12 @@ public: public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: status_type m_status; @@ -309,12 +309,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: std::string m_race; @@ -333,12 +333,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: std::string m_subrace; @@ -357,12 +357,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: std::string m_class; @@ -381,12 +381,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: std::string m_inscription; @@ -406,12 +406,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: int m_min; @@ -432,12 +432,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: int m_min; @@ -459,12 +459,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: uint16_t m_skill_idx; @@ -485,12 +485,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: identification_state m_state; @@ -509,12 +509,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: char m_symbol; @@ -533,12 +533,12 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; private: uint16_t m_ability_idx; @@ -563,10 +563,10 @@ public: virtual std::shared_ptr<Condition> first_child() override; protected: - void to_json(json_t *) const override; + void to_json(jsoncons::json &) const override; static std::shared_ptr<Condition> parse_single_subcondition( - json_t *condition_json); + jsoncons::json const &condition_json); protected: std::shared_ptr<Condition> m_subcondition; @@ -584,7 +584,7 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; @@ -603,7 +603,7 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: @@ -623,7 +623,7 @@ public: bool is_match(object_type *) const override; - static std::shared_ptr<Condition> from_json(json_t *); + static std::shared_ptr<Condition> from_json(jsoncons::json const &); protected: void write_tree(TreePrinter *, Cursor *, uint8_t, uint8_t) const override; diff --git a/src/include/tome/squelch/rule.hpp b/src/include/tome/squelch/rule.hpp index 63f1b6c0..af86dfc8 100644 --- a/src/include/tome/squelch/rule.hpp +++ b/src/include/tome/squelch/rule.hpp @@ -1,7 +1,7 @@ #pragma once -#include <jansson.h> #include <memory> +#include <jsoncons/json.hpp> #include "tome/squelch/condition_fwd.hpp" #include "tome/squelch/cursor_fwd.hpp" @@ -40,12 +40,12 @@ public: /** * Set the name of the rule */ - void set_name(const char *new_name); + void set_name(const std::string &new_name); /** * Get the name of the rule */ - const char *get_name() const; + std::string get_name() const; /** * Get condition @@ -75,14 +75,14 @@ public: bool apply_rule(object_type *o_ptr, int item_idx) const; /** - * Convert rule to JSON + * Convert rule to JSON. */ - virtual json_t *to_json() const; + virtual jsoncons::json to_json() const; /** * Parse rule from JSON */ - static std::shared_ptr<Rule> parse_rule(json_t *); + static std::shared_ptr<Rule> parse_rule(jsoncons::json const &); protected: virtual bool do_apply_rule(object_type *, int) const = 0; @@ -148,7 +148,7 @@ public: , m_inscription(inscription) { } - json_t *to_json() const override; + jsoncons::json to_json() const override; protected: virtual void do_write_tree(TreePrinter *p) const override; diff --git a/src/include/tome/squelch/tree_printer.hpp b/src/include/tome/squelch/tree_printer.hpp index e8ee1e56..c9e79af2 100644 --- a/src/include/tome/squelch/tree_printer.hpp +++ b/src/include/tome/squelch/tree_printer.hpp @@ -3,6 +3,7 @@ #include <boost/noncopyable.hpp> #include <cstdint> +#include <string> namespace squelch { @@ -30,7 +31,8 @@ public: void scroll_right(); - void write(uint8_t color, const char *line); + void write(uint8_t color, const char *); + void write(uint8_t color, std::string const &); private: int m_indent; |