]> git.stg.codes - stg.git/blob - stglibs/json.lib/include/stg/json_generator.h
Declare unary ctors explicit.
[stg.git] / stglibs / json.lib / include / stg / json_generator.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #ifndef __STG_STGLIBS_JSON_GENERATOR_H__
22 #define __STG_STGLIBS_JSON_GENERATOR_H__
23
24 #include <string>
25 #include <map>
26 #include <vector>
27 #include <utility>
28
29 #include <boost/scoped_ptr.hpp>
30
31 struct yajl_gen_t;
32
33 namespace STG
34 {
35 namespace JSON
36 {
37
38 struct Gen
39 {
40     virtual ~Gen() {}
41     virtual void run(yajl_gen_t* handle) const = 0;
42 };
43
44 struct NullGen : public Gen
45 {
46     virtual void run(yajl_gen_t* handle) const;
47 };
48
49 class BoolGen : public Gen
50 {
51     public:
52         explicit BoolGen(bool value) : m_value(value) {}
53         virtual void run(yajl_gen_t* handle) const;
54     private:
55         bool m_value;
56 };
57
58 class StringGen : public Gen
59 {
60     public:
61         explicit StringGen(const std::string& value) : m_value(value) {}
62         virtual void run(yajl_gen_t* handle) const;
63     private:
64         std::string m_value;
65 };
66
67 class NumberGen : public Gen
68 {
69     public:
70         explicit NumberGen(const std::string& value) : m_value(value) {}
71         template <typename T>
72         explicit NumberGen(const T& value) : m_value(x2str(value)) {}
73         virtual void run(yajl_gen_t* handle) const;
74     private:
75         std::string m_value;
76 };
77
78 class MapGen : public Gen
79 {
80     public:
81         MapGen() {}
82         virtual ~MapGen()
83         {
84             for (Value::iterator it = m_value.begin(); it != m_value.end(); ++it)
85                 if (it->second.second)
86                     delete it->second.first;
87         }
88         MapGen& add(const std::string& key, Gen* value) { m_value[key] = std::make_pair(value, true); return *this; }
89         MapGen& add(const std::string& key, Gen& value) { m_value[key] = std::make_pair(&value, false); return *this; }
90         virtual void run(yajl_gen_t* handle) const;
91     private:
92         typedef std::pair<Gen*, bool> SmartGen;
93         typedef std::map<std::string, SmartGen> Value;
94         Value m_value;
95 };
96
97 class ArrayGen : public Gen
98 {
99     public:
100         ArrayGen() {}
101         virtual ~ArrayGen()
102         {
103             for (Value::iterator it = m_value.begin(); it != m_value.end(); ++it)
104                 if (it->second)
105                     delete it->first;
106         }
107         void add(Gen* value) { m_value.push_back(std::make_pair(value, true)); }
108         void add(Gen& value) { m_value.push_back(std::make_pair(&value, false)); }
109         virtual void run(yajl_gen_t* handle) const;
110     private:
111         typedef std::pair<Gen*, bool> SmartGen;
112         typedef std::vector<SmartGen> Value;
113         Value m_value;
114 };
115
116 typedef bool (*Callback)(void* /*data*/, const char* /*buf*/, size_t /*size*/);
117 bool generate(Gen& gen, Callback callback, void* data);
118
119 }
120 }
121
122 #endif