Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.0 kB
3
Indexable
template<typename Derived,typename JSONType>
    class AbstractJsonDictionary
    {
    protected:
        JSONType json;
    public:
        AbstractJsonDictionary(const JSONType &inner_json):json(inner_json)
        {
        }
        template<typename T>
        auto get(const std::string &str) const
        {
            static_cast<Derived*>(this)->get(str);
        }
        void set(const std::string &key,const auto &value)
        {
            static_cast<Derived*>(this)->set(key,value);
        }
        JSONType get_underlying_json() const;

    };
    template<typename Derived,typename Base,typename Callable,typename ...Args>
    constexpr void crtp_helper(const Base *given_item,const Callable &given_method,Args... given_arguments)
    {
        boost::hana::eval_if(std::is_base_of<Base, Derived>{},
                             [&](auto _)
                             {
                                 static_cast<Derived*>(_(given_item))->given_method(_(given_arguments...));
                             },
                             [&](auto _)
                             {
                                 throw _(NotDerivedError());
                             });
    }
    template<typename Derived,typename DerivedDictionary,typename JSONType>
    class AbstractJsonParser
    {
    public:
        AbstractJsonDictionary<DerivedDictionary,JSONType> parse_string(const std::string &json_string)
        {
            crtp_helper<Derived>(this, &Derived::parse_string,json_string);
        }
        void dump_string(const AbstractJsonDictionary<DerivedDictionary,JSONType> &given_dictionary_to_dump)
        {
            crtp_helper<Derived>(this, &Derived::dump_string, given_dictionary_to_dump);
        }
        void dump(const std::string &file_path)
        {
            crtp_helper<Derived>(this, &Derived::dump, file_path);
        }
        void parse(const std::string &file_path)
        {
            crtp_helper<Derived>(this, &Derived::parse, file_path);
        }

    };