X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/a6680ce3d763763a6010c81c8a5a8f7a1ce052db..8c6fa3fbaccc22127280bf77a48fab5a3ee0716e:/stglibs/json.lib/include/stg/json_generator.h diff --git a/stglibs/json.lib/include/stg/json_generator.h b/stglibs/json.lib/include/stg/json_generator.h new file mode 100644 index 00000000..4f1523fc --- /dev/null +++ b/stglibs/json.lib/include/stg/json_generator.h @@ -0,0 +1,122 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_STGLIBS_JSON_GENERATOR_H__ +#define __STG_STGLIBS_JSON_GENERATOR_H__ + +#include +#include +#include +#include + +#include + +struct yajl_gen_t; + +namespace STG +{ +namespace JSON +{ + +struct Gen +{ + virtual ~Gen() {} + virtual void run(yajl_gen_t* handle) const = 0; +}; + +struct NullGen : public Gen +{ + virtual void run(yajl_gen_t* handle) const; +}; + +class BoolGen : public Gen +{ + public: + explicit BoolGen(bool value) : m_value(value) {} + virtual void run(yajl_gen_t* handle) const; + private: + bool m_value; +}; + +class StringGen : public Gen +{ + public: + explicit StringGen(const std::string& value) : m_value(value) {} + virtual void run(yajl_gen_t* handle) const; + private: + std::string m_value; +}; + +class NumberGen : public Gen +{ + public: + explicit NumberGen(const std::string& value) : m_value(value) {} + template + explicit NumberGen(const T& value) : m_value(x2str(value)) {} + virtual void run(yajl_gen_t* handle) const; + private: + std::string m_value; +}; + +class MapGen : public Gen +{ + public: + MapGen() {} + virtual ~MapGen() + { + for (Value::iterator it = m_value.begin(); it != m_value.end(); ++it) + if (it->second.second) + delete it->second.first; + } + MapGen& add(const std::string& key, Gen* value) { m_value[key] = std::make_pair(value, true); return *this; } + MapGen& add(const std::string& key, Gen& value) { m_value[key] = std::make_pair(&value, false); return *this; } + virtual void run(yajl_gen_t* handle) const; + private: + typedef std::pair SmartGen; + typedef std::map Value; + Value m_value; +}; + +class ArrayGen : public Gen +{ + public: + ArrayGen() {} + virtual ~ArrayGen() + { + for (Value::iterator it = m_value.begin(); it != m_value.end(); ++it) + if (it->second) + delete it->first; + } + void add(Gen* value) { m_value.push_back(std::make_pair(value, true)); } + void add(Gen& value) { m_value.push_back(std::make_pair(&value, false)); } + virtual void run(yajl_gen_t* handle) const; + private: + typedef std::pair SmartGen; + typedef std::vector Value; + Value m_value; +}; + +typedef bool (*Callback)(void* /*data*/, const char* /*buf*/, size_t /*size*/); +bool generate(Gen& gen, Callback callback, void* data); + +} +} + +#endif