0bc505b24f70f101a4f5945c57d6db81616a70da
[jansson.git] / janssonxx.tcc
1 // get size of array or object
2 template <typename _Base>
3 unsigned int jansson::_private::ValueBase<_Base>::size() const {
4         if (is_object())
5                 return json_object_size(_Base::as_json());
6         else
7                 return json_array_size(_Base::as_json());
8 }
9
10 // get value at array index (const version)
11 template <typename _Base>
12 const jansson::Value jansson::_private::ValueBase<_Base>::at(unsigned int index) const {
13         return jansson::Value(json_array_get(_Base::as_json(), index));
14 }
15
16 template <typename _Base>
17 const jansson::Value jansson::_private::ValueBase<_Base>::operator[](signed int index) const { return at(index); }
18 template <typename _Base>
19 const jansson::Value jansson::_private::ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
20 template <typename _Base>
21 const jansson::Value jansson::_private::ValueBase<_Base>::operator[](signed short index) const { return at(index); }
22 template <typename _Base>
23 const jansson::Value jansson::_private::ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
24 template <typename _Base>
25 const jansson::Value jansson::_private::ValueBase<_Base>::operator[](signed long index) const { return at(index); }
26 template <typename _Base>
27 const jansson::Value jansson::_private::ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }
28
29 // get value at array index (non-const version)
30 template <typename _Base>
31 jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::at(unsigned int index) {
32         return ArrayProxy(_Base::as_json(), index);
33 }
34
35 template <typename _Base>
36 jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](signed int index) { return at(index); }
37 template <typename _Base>
38 jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](unsigned int index) { return at(index); }
39 template <typename _Base>
40 jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](signed short index) { return at(index); }
41 template <typename _Base>
42 jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](unsigned short index) { return at(index); }
43 template <typename _Base>
44 jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](signed long index) { return at(index); }
45 template <typename _Base>
46 jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](unsigned long index) { return at(index); }
47
48 // get object property (const version)
49 template <typename _Base>
50 const jansson::Value jansson::_private::ValueBase<_Base>::get(const char* key) const {
51         return jansson::Value(json_object_get(_Base::as_json(), key));
52 }
53
54 template <typename _Base>
55 const jansson::Value jansson::_private::ValueBase<_Base>::get(const std::string& key) const { return get(key.c_str()); }
56 template <typename _Base>
57 const jansson::Value jansson::_private::ValueBase<_Base>::operator[](const char* key) const { return get(key); }
58 template <typename _Base>
59 const jansson::Value jansson::_private::ValueBase<_Base>::operator[](const std::string& key) const { return get(key.c_str()); }
60
61 // get object property (non-const version)
62 template <typename _Base>
63 jansson::_private::ValueBase<jansson::_private::ObjectProxy> jansson::_private::ValueBase<_Base>::get(const char* key) {
64         return ObjectProxy(_Base::as_json(), key);
65 }
66
67 template <typename _Base>
68 jansson::_private::ValueBase<jansson::_private::ObjectProxy> jansson::_private::ValueBase<_Base>::get(const std::string& key) { return get(key.c_str()); }
69 template <typename _Base>
70 jansson::_private::ValueBase<jansson::_private::ObjectProxy> jansson::_private::ValueBase<_Base>::operator[](const char* key) { return get(key); }
71 template <typename _Base>
72 jansson::_private::ValueBase<jansson::_private::ObjectProxy> jansson::_private::ValueBase<_Base>::operator[](const std::string& key) { return get(key.c_str()); }
73
74 // clear all array/object values
75 template <typename _Base>
76 void jansson::_private::ValueBase<_Base>::clear() {
77         if (is_object())
78                 json_object_clear(_Base::as_json());
79         else
80                 json_array_clear(_Base::as_json());
81 }
82
83 // get value cast to specified type
84 template <typename _Base>
85 const char* jansson::_private::ValueBase<_Base>::as_cstring() const { return json_string_value(_Base::as_json()); }
86 template <typename _Base>
87 std::string jansson::_private::ValueBase<_Base>::as_string() const {
88         const char* tmp = as_cstring();
89         return tmp == 0 ? "" : tmp;
90 }
91 template <typename _Base>
92 int jansson::_private::ValueBase<_Base>::as_integer() const { return json_integer_value(_Base::as_json()); }
93 template <typename _Base>
94 double jansson::_private::ValueBase<_Base>::as_real() const { return json_real_value(_Base::as_json()); }
95 template <typename _Base>
96 double jansson::_private::ValueBase<_Base>::as_number() const { return json_number_value(_Base::as_json()); }
97 template <typename _Base>
98 bool jansson::_private::ValueBase<_Base>::as_boolean() const { return is_true(); }
99
100 // set an object property (converts value to object is not one already)
101 template <typename _Base>
102  _Base& jansson::_private::ValueBase<_Base>::set_key(const char* key, const jansson::Value& value) {
103         json_object_set(_Base::as_json(), key, value._Base::as_json());
104         return *this;
105 }
106
107 template <typename _Base>
108  _Base& jansson::_private::ValueBase<_Base>::set_key(const std::string& key, const jansson::Value& value) {
109         return set_key(key.c_str(), value);
110 }
111
112 // set an array index (converts value to object is not one already)
113 template <typename _Base>
114  _Base& jansson::_private::ValueBase<_Base>::set_at(unsigned int index, const jansson::Value& value) {
115         if (index == size())
116                 json_array_append(_Base::as_json(), value._Base::as_json());
117         else
118                 json_array_set(_Base::as_json(), index, value._Base::as_json());
119         return *this;
120 }
121
122 // delete an object key
123 template <typename _Base>
124  _Base& jansson::_private::ValueBase<_Base>::del_key(const char* key) {
125         json_object_del(_Base::as_json(), key);
126         return *this;
127 }
128
129 template <typename _Base>
130  _Base& jansson::_private::ValueBase<_Base>::del_key(const std::string& key) {
131         return del_key(key.c_str());
132 }
133
134 // delete an item from an array by index
135 template <typename _Base>
136  _Base& jansson::_private::ValueBase<_Base>::del_at(unsigned int index) {
137         json_array_remove(_Base::as_json(), index);
138         return *this;
139 }
140
141 // insert an item into an array at a given index
142 template <typename _Base>
143  _Base& jansson::_private::ValueBase<_Base>::insert_at(unsigned int index, const jansson::Value& value) {
144         json_array_insert(_Base::as_json(), index, value._Base::as_json());
145         return *this;
146 }
147
148 // assign value to proxied array element
149 jansson::_private::ArrayProxy& jansson::_private::ArrayProxy::operator=(const Value& value) {
150         json_array_set(_array, _index, value.as_json());
151         return *this;
152 }
153
154 // assign value to proxied object property
155 jansson::_private::ObjectProxy& jansson::_private::ObjectProxy::operator=(const Value& value) {
156         json_object_set(_object, _key, value.as_json());
157         return *this;
158 }