]> git.stg.codes - stg.git/blob - libs/json/include/stg/json_generator.h
Replace boost::scoped_ptr with std::unique_ptr.
[stg.git] / libs / json / 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 struct yajl_gen_t;
30
31 namespace STG
32 {
33 namespace JSON
34 {
35
36 struct Gen
37 {
38     virtual ~Gen() {}
39     virtual void run(yajl_gen_t* handle) const = 0;
40 };
41
42 struct NullGen : public Gen
43 {
44     virtual void run(yajl_gen_t* handle) const;
45 };
46
47 class BoolGen : public Gen
48 {
49     public:
50         explicit BoolGen(bool value) : m_value(value) {}
51         virtual void run(yajl_gen_t* handle) const;
52     private:
53         bool m_value;
54 };
55
56 class StringGen : public Gen
57 {
58     public:
59         explicit StringGen(const std::string& value) : m_value(value) {}
60         virtual void run(yajl_gen_t* handle) const;
61     private:
62         std::string m_value;
63 };
64
65 class NumberGen : public Gen
66 {
67     public:
68         explicit NumberGen(const std::string& value) : m_value(value) {}
69         template <typename T>
70         explicit NumberGen(const T& value) : m_value(std::to_string(value)) {}
71         virtual void run(yajl_gen_t* handle) const;
72     private:
73         std::string m_value;
74 };
75
76 class MapGen : public Gen
77 {
78     public:
79         MapGen() {}
80         virtual ~MapGen()
81         {
82             for (Value::iterator it = m_value.begin(); it != m_value.end(); ++it)
83                 if (it->second.second)
84                     delete it->second.first;
85         }
86         MapGen& add(const std::string& key, Gen* value) { m_value[key] = std::make_pair(value, true); return *this; }
87         MapGen& add(const std::string& key, Gen& value) { m_value[key] = std::make_pair(&value, false); return *this; }
88         virtual void run(yajl_gen_t* handle) const;
89     private:
90         typedef std::pair<Gen*, bool> SmartGen;
91         typedef std::map<std::string, SmartGen> Value;
92         Value m_value;
93 };
94
95 class ArrayGen : public Gen
96 {
97     public:
98         ArrayGen() {}
99         virtual ~ArrayGen()
100         {
101             for (Value::iterator it = m_value.begin(); it != m_value.end(); ++it)
102                 if (it->second)
103                     delete it->first;
104         }
105         void add(Gen* value) { m_value.push_back(std::make_pair(value, true)); }
106         void add(Gen& value) { m_value.push_back(std::make_pair(&value, false)); }
107         virtual void run(yajl_gen_t* handle) const;
108     private:
109         typedef std::pair<Gen*, bool> SmartGen;
110         typedef std::vector<SmartGen> Value;
111         Value m_value;
112 };
113
114 typedef bool (*Callback)(void* /*data*/, const char* /*buf*/, size_t /*size*/);
115 bool generate(Gen& gen, Callback callback, void* data);
116
117 }
118 }
119
120 #endif