OPTPiX SpriteStudio SDK
Loading...
Searching...
No Matches
ssvalue.h
Go to the documentation of this file.
1#ifndef __SSVALUE__
2#define __SSVALUE__
3
4#include "ssarchiver.h"
5#include "ssstring_uty.h"
6#include <map>
7#include <vector>
8#include <cassert>
9
10namespace spritestudio6
11{
12
13class SsValue;
14
15typedef wchar_t SsChar;
16typedef std::vector<SsValue> SsArray;
17typedef std::map<SsString,SsValue> SsHash;
18
19//SsValue用のシリアライザ
20void SsValueSeriarizer( ISsXmlArchiver* ar , SsValue& v , const std::string key = "value" );
21
22
23class SsValue{
24public:
25 enum{
33 };
34
35public:
36 int type;
39
40 union{
42 int _int;
43 float _float;
44 bool _bool;
47 };
48
52
53
54 SsValue() : type(unkown) , _str(0){}
55
56 explicit SsValue(bool b ) : type(boolean_type) { _bool = b; }
57 explicit SsValue(int n, char* org = 0) : type(int_type) {
58 _int = n;
59 if (org)
60 org_txt = SsString(org);
61 }
62 explicit SsValue(float n, char* org = 0) : type(float_type)
63 {
64 _float = n;
65 if (org)
66 org_txt = SsString(org);
67
68 }
69 explicit SsValue(SsString& str) { type = string_type; _str = new SsString(str); }
70 explicit SsValue(const char* str) { type = string_type; _str = new SsString(str); }
71 explicit SsValue(SsArray& n)
72 {
74 _array = new SsArray(n);
75 }
76
77
78 explicit SsValue(SsHash& n) { type = hash_type; _hash = new SsHash(n); }
79
80
82 {
83
84 switch( x.type )
85 {
86 case string_type:
87 _str = new SsString( *x._str );
88 _float_temp = (float)atof( _str->c_str() );
89 _int_temp = atoi( _str->c_str() );
90 break;
91 case int_type:
92 _int = x._int;
93 _float_temp = (float)_int;
94 _bool_temp = _int == 1 ? true : false;
95 break;
96 case float_type:
97 _float = x._float;
98 _int_temp = (int)_float;
99 _bool_temp = _int > 0 ? true : false;
100 break;
101 case boolean_type:
102 _bool = x._bool;
103 break;
104 case array_type:
105 _array = new SsArray( *x._array);
106 break;
107 case hash_type:
108 _hash = new SsHash( *x._hash);
109 break;
110 }
111 type = x.type;
112 org_txt = x.org_txt;
113
114 }
116 {
117 if (this != &x) {
118 this->release();
119 new (this) SsValue(x);
120 }
121 return *this;
122 }
123
124 void release()
125 {
126 name.~SsString();
127 org_txt.~SsString();
128
129 if(type == string_type && _str) {
130 delete _str;
131 return;
132 }
133
134 if(type == array_type && _array) {
135 delete _array;
136 return;
137 }
138 if(type == hash_type && _hash)
139 {
140 delete _hash;
141 return;
142 }
143 }
144
145 virtual ~SsValue() {
146 release();
147 }
148
149 template <typename T> bool is() const;
150 template <typename T> const T& get() const;
151 template <typename T> T& get();
152
153 const SsValue& operator[](const std::string& key) const
154 {
155 static SsValue r_value;
156
157 if ( type == hash_type )
158 {
159 SsHash::const_iterator i = _hash->find(key);
160 return i != _hash->end() ? i->second : r_value;
161 }
162
163 assert(1);
164 return *this;
165 }
166
167 bool IsExistHashkey( const std::string& key ) const
168 {
169 if ( type == hash_type )
170 {
171 SsHash::const_iterator i = _hash->find(key);
172 return i != _hash->end();
173 }
174
175 return false;
176 }
177
179 {
180 SsValueSeriarizer( ar , *this ,"" );
181 }
182
183};
184
185template <> inline const SsString& SsValue::get<SsString>() const {
186 static SsString ret = "";
187 if ( this->type != string_type )
188 {
189 if (this->type == int_type )
190 {
191 //char tmp[64+1];
192 //sprintf_s( tmp , 64 , "%d" , _int );
193 //ret = tmp;
194 ret = org_txt;
195 }
196
197 return ret;
198 }
199 return *_str;
200}
201template <> inline SsString& SsValue::get<SsString>() {
202 static SsString ret = "";
203 if ( this->type != string_type )
204 {
205 if ( this->type == float_type )
206 {
207 ret = std::to_string( (long double)_float);
208 }else if ( this->type == int_type )
209 {
210#ifdef _WIN32
211 ret = std::to_string( (int)_int);
212#else
213 ret = std::to_string( (int)(_int) );
214#endif
215 }
216 return ret;
217 }
218 return *_str;
219}
220
221template <> inline const int& SsValue::get<int>() const {
222 if ( this->type == float_type )
223 {
224 return _int_temp;
225 }else{
226 return _int;
227 }
228}
229template <> inline int& SsValue::get<int>() {
230 if ( this->type == float_type )
231 {
232 return _int_temp;
233 }else{
234 return _int;
235 }
236}
237
238template <> inline const float& SsValue::get<float>() const {
239 if ( this->type == float_type )
240 {
241 return _float;
242 }else{
243 return _float_temp;
244 }
245}
246template <> inline float& SsValue::get<float>() {
247 if ( this->type == float_type )
248 {
249 return _float;
250 }else{
251 return _float_temp;
252 }
253}
254
255template <> inline const bool& SsValue::get<bool>() const {
256 if ( this->type == boolean_type )
257 {
258 return _bool;
259 }else{
260 return _bool_temp;
261 }
262}
263template <> inline bool& SsValue::get<bool>() {
264 if ( this->type == boolean_type )
265 {
266 return _bool;
267 }else{
268 return _bool_temp;
269 }
270}
271
272template <> inline const SsArray& SsValue::get<SsArray>() const {
273 return *_array;
274}
275template <> inline SsArray& SsValue::get<SsArray>() {
276 return *_array;
277}
278
279
280template <> inline const SsHash& SsValue::get<SsHash>() const {
281 return *_hash;
282}
283template <> inline SsHash& SsValue::get<SsHash>() {
284 return *_hash;
285}
286
287
288
289template <> inline bool SsValue::is<bool>() const {
290 return type == boolean_type;
291}
292
293template <> inline bool SsValue::is<int>() const {
294 return type == int_type;
295}
296
297template <> inline bool SsValue::is<float>() const {
298 return type == float_type;
299}
300template <> inline bool SsValue::is<SsString>() const {
301 return type == string_type;
302}
303
304template <> inline bool SsValue::is<SsArray>() const {
305 return type == array_type;
306}
307
308template <> inline bool SsValue::is<SsHash>() const {
309 return type == hash_type;
310}
311
312
313
314
315inline static SsValue SsValueSeriarizer__MakeValue( const char* v )
316{
317 std::string temp = v;
318 bool is_priod;
319
320 if ( is_digit_string( temp , &is_priod) )
321 {
322 if ( is_priod )
323 {
324 return SsValue( (float)atof( v ) , (char*)v );
325 }
326
327 return SsValue( (int)atoi( v ), (char*)v);
328
329 }else{
330 return SsValue( v );
331 }
332
333}
334
335
336
337} // namespace spritestudio6
338
339#endif
アーカイバクラスのインターフェース
Definition ssarchiver.h:47
Definition ssvalue.h:23
const T & get() const
@ int_type
Definition ssvalue.h:28
@ float_type
Definition ssvalue.h:29
@ string_type
Definition ssvalue.h:27
@ hash_type
Definition ssvalue.h:31
@ boolean_type
Definition ssvalue.h:30
@ unkown
Definition ssvalue.h:26
@ array_type
Definition ssvalue.h:32
SsString * _str
Definition ssvalue.h:41
SsString org_txt
Definition ssvalue.h:38
SsString name
Definition ssvalue.h:37
float _float
Definition ssvalue.h:43
SsValue(const char *str)
Definition ssvalue.h:70
SsValue(int n, char *org=0)
Definition ssvalue.h:57
SsValue(float n, char *org=0)
Definition ssvalue.h:62
SsValue(const SsValue &x)
Definition ssvalue.h:81
float _float_temp
Definition ssvalue.h:50
bool IsExistHashkey(const std::string &key) const
Definition ssvalue.h:167
int _int_temp
Definition ssvalue.h:49
SPRITESTUDIO6SDK_SERIALIZE_BLOCK
Definition ssvalue.h:179
virtual ~SsValue()
Definition ssvalue.h:145
int _int
Definition ssvalue.h:42
SsValue(SsHash &n)
Definition ssvalue.h:78
bool _bool_temp
Definition ssvalue.h:51
SsValue(SsArray &n)
Definition ssvalue.h:71
bool _bool
Definition ssvalue.h:44
SsHash * _hash
Definition ssvalue.h:46
SsValue(bool b)
Definition ssvalue.h:56
const SsValue & operator[](const std::string &key) const
Definition ssvalue.h:153
SsValue()
Definition ssvalue.h:54
SsValue(SsString &str)
Definition ssvalue.h:69
void release()
Definition ssvalue.h:124
SsValue & operator=(const SsValue &x)
Definition ssvalue.h:115
int type
Definition ssvalue.h:36
SsArray * _array
Definition ssvalue.h:45
GLdouble n
Definition glad.h:4564
GLdouble v
Definition glad.h:2712
GLboolean GLboolean GLboolean b
Definition glad.h:3632
GLdouble x
Definition glad.h:2847
Definition ISsEffectRender.h:5
bool is_digit_string(std::string &in_str, bool *is_priod)
Definition ssstring_uty.cpp:74
std::vector< SsValue > SsArray
Definition ssvalue.h:16
void SsValueSeriarizer(ISsXmlArchiver *ar, SsValue &v, const std::string key)
Definition ssvalue.cpp:11
std::map< SsString, SsValue > SsHash
Definition ssvalue.h:17
std::string SsString
Definition sstypes.h:30
wchar_t SsChar
Definition ssvalue.h:15