OPTPiX SpriteStudio SDK
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
ssarchiver.h
1 #ifndef __SSARCHIVER__
2 #define __SSARCHIVER__
3 
4 #include "./tinyxml2/tinyxml2.h"
5 #include "./babel/babel.h"
6 #include "sstypes.h"
7 #include <string>
8 #include <vector>
9 
10 using namespace tinyxml2;
11 
12 
13 
16 {
17  enum Type
18  {
19  unkown = 0, //不明
20  in, //Input
21  out, //Output
22  };
23 };
24 
25 class SsXmlIArchiver;
26 
29 {
30 private:
31  XMLElement* m_xml;
32 
33 public:
34 
35  virtual EnumSsArchiver::Type getType(){ return EnumSsArchiver::unkown;}
36 
37 
38  ISsXmlArchiver(){}
39  virtual ~ISsXmlArchiver(){}
40 
41  XMLElement* getxml(){ return m_xml;}
42 
43  void firstChild(ISsXmlArchiver* ar , const char* element_name)
44  {
45  m_xml = ar->getxml()->FirstChildElement( element_name );
46  }
47 
48  void setDocumet( tinyxml2::XMLDocument* doc , const char* element_name)
49  {
50  m_xml = doc->FirstChildElement(element_name);
51  }
52 
53  void setElement(XMLElement* e )
54  {
55  m_xml = e;
56  }
57 
58  virtual bool dc( const char* name , int& member ) = 0;
59  virtual bool dc( const char* name , float& member ) = 0;
60  virtual bool dc( const char* name , bool& member ) = 0;
61  virtual bool dc( const char* name , std::vector<SsString>& list ) = 0;
62 
63  virtual bool dc( const char* name , SsString& member ) = 0;
64  virtual bool dc( const char* name , SsPoint2& member ) = 0;
65  virtual bool dc( const char* name , SsCurve& member ) = 0;
66 
67 
68  virtual bool dc_attr( const char* name , SsString& member ) = 0;
69  virtual bool dc_attr( const char* name , int& member ) = 0;
70 
71 };
72 
73 
74 inline static int GetTextToInt(XMLElement* e , int default_value )
75 {
76  int ret =default_value;
77  const char* v = e->GetText();
78  if ( v )
79  {
80  ret = atoi(v);
81  }
82  return ret;
83 }
84 
85 
88 {
89 public:
90  virtual EnumSsArchiver::Type getType(){ return EnumSsArchiver::in;}
91 
92 
93  SsXmlIArchiver(ISsXmlArchiver* ar , const char* element_name)
94  {
95  firstChild( ar , element_name );
96  }
97  SsXmlIArchiver( tinyxml2::XMLDocument* doc , const char* element_name)
98  {
99  setDocumet( doc , element_name );
100  }
101 
103  {
104  setElement( e );
105  }
106 
107 
108  virtual ~SsXmlIArchiver(){}
109 
110 
111  virtual bool dc( const char* name , int& member );
112  virtual bool dc( const char* name , float& member );
113  virtual bool dc( const char* name , SsString& member );
114  virtual bool dc( const char* name , bool& member );
115  virtual bool dc( const char* name , std::vector<SsString>& list );
116  virtual bool dc( const char* name , SsPoint2& member );
117  virtual bool dc( const char* name , SsCurve& member );
118 
119  virtual bool dc_attr( const char* name , SsString& member );
120  virtual bool dc_attr( const char* name , int& member );
121 
122 
123  template<class myclass> bool dc( const char* name , std::vector<myclass*>& list , const std::string key = "value" )
124  {
125  list.clear();
126  XMLElement* e = getxml()->FirstChildElement( name );
127 
128  if ( key != "" )
129  e = e->FirstChildElement( key.c_str() );
130 
131  while( e )
132  {
133  myclass* _temp = new myclass();
134  SsXmlIArchiver _ar(e);
135  _temp->__Serialize( &_ar );
136  list.push_back( _temp );
137  e = e->NextSiblingElement();
138  }
139 
140  return true;
141  }
142 
143  template<class myclass> bool dc( const char* name , myclass& type )
144  {
145  SsString str;
146  dc( name , str );
147  __StringToEnum_( str , type);
148 
149  return false;
150  }
151 
152  template<class myclass> bool dc_attr( const char* name , myclass& type )
153  {
154  SsString str;
155  dc_attr( name , str );
156  __StringToEnum_( str , type);
157 
158  return false;
159  }
160 
161 };
162 
163 
164 #define SSSERIALIZE_BLOCK void __Serialize(ISsXmlArchiver* ar)
165 
166 #define SSAR_DECLARE(t) ar->dc(#t,t)
167 #define SSAR_DECLARE_ATTRIBUTE(t) ar->dc_attr(#t,t)
168 
169 #define SSAR_STRUCT_DECLARE(t) {SsXmlIArchiver _ar( ar , #t );\
170 t.__Serialize( &_ar );}\
171 
172 template<class myclass>
173 inline bool __SSAR_DECLARE_LIST__( ISsXmlArchiver* ar , std::vector<myclass*>& list, const char* name ,const std::string key = "value" )
174 {
175  if ( ar->getType() == EnumSsArchiver::in )
176  {
177  return (static_cast<SsXmlIArchiver*>(ar))->dc( name , list , key );
178  }
179 
180  if ( ar->getType() == EnumSsArchiver::out )
181  {
182  }
183 
184  return false;
185 }
186 
187 #define SSAR_DECLARE_LIST(t) __SSAR_DECLARE_LIST__( ar , t , #t)
188 #define SSAR_DECLARE_LISTEX(t,key) __SSAR_DECLARE_LIST__( ar , t , #t , key )
189 
190 template<class myclass>
191 inline bool __SSAR_DECLARE_ENUM__( ISsXmlArchiver* ar ,myclass& type, const char* name )
192 {
193  if ( ar->getType() == EnumSsArchiver::in )
194  {
195  return (static_cast<SsXmlIArchiver*>(ar))->dc( name , type );
196  }
197 
198  if ( ar->getType() == EnumSsArchiver::out )
199  {
200  }
201  return false;
202 }
203 
204 template<class myclass>
205 inline bool __SSAR_DECLARE_ATTRIBUTE_ENUM__( ISsXmlArchiver* ar ,myclass& type, const char* name )
206 {
207  if ( ar->getType() == EnumSsArchiver::in )
208  {
209  return (static_cast<SsXmlIArchiver*>(ar))->dc_attr( name , type );
210  }
211 
212  if ( ar->getType() == EnumSsArchiver::out )
213  {
214  }
215  return false;
216 }
217 
218 
219 
220 #define SSAR_DECLARE_ENUM(t) __SSAR_DECLARE_ENUM__( ar , t , #t)
221 #define SSAR_DECLARE_ATTRIBUTE_ENUM(t) __SSAR_DECLARE_ATTRIBUTE_ENUM__( ar , t , #t)
222 
223 
224 bool StringToPoint2( const std::string& str , SsPoint2& point );
225 bool StringToIRect( const std::string& str , SsIRect& rect );
226 
227 
229 void SsArchiverInit();
230 
231 
232 #define AR_SELF_CHECK() if ( this->getxml() == 0 ) return false;
233 
234 
235 #endif