Merge branch 'devel/master (1.2.0)' into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / resources / archive.h
1 #if !defined(__DALI_INTERNAL_ARCHIVE_H__)
2 #define __DALI_INTERNAL_ARCHIVE_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <string>
23 #include <iostream>
24 #include <stack>
25
26 namespace Dali
27 {
28 struct Vector2;
29 struct Vector3;
30 struct Vector4;
31 class Matrix;
32 class Quaternion;
33
34 namespace Internal
35 {
36
37 /**
38  * Archive class. Serializes data to a Tag-Length-Value archive
39  */
40 class Archive
41 {
42 public:
43   typedef std::pair<unsigned int, std::streampos> ChunkHeader;
44
45 public:
46   /**
47    * Constructor
48    * @param[in] buf An open streambuf
49    */
50   Archive(std::streambuf& buf);
51
52   /**
53    * Destructor
54    */
55   virtual ~Archive();
56
57   /**
58    * Get archive version
59    */
60   unsigned int GetVersion() const
61   {
62     return mVersion;
63   }
64
65   /**
66    * Set archive version
67    * @param[in] version the version number
68    */
69   void SetVersion(const unsigned int version)
70   {
71     mVersion = version;
72   }
73
74   /**
75    * Returns the result of the archiving operation
76    * The result status is initialised to true on construction
77    * Any failure will set it to false
78    * @return the archiving result, true signals success.
79    */
80   bool GetResult() const
81   {
82     return mResult;
83   }
84
85   /**
86    * Set the archive status to failed
87    */
88   void SetResultFailed()
89   {
90     mResult = false;
91   }
92
93   /**
94    * Write a bytestream to the archive
95    * @param[in] data    A pointer to the data
96    * @param[in] length  The length of the data in bytes
97    * @return            true if the data was successfully written
98    */
99   virtual bool Write(const char* data, const unsigned int length)
100   {
101     return false;
102   }
103
104   /**
105    * Read a bytestream from the archive
106    * @param[in] data    A pointer to a buffer to store the data
107    * @param[in] length  The length of the data in bytes
108    * @return            true if the data was successfully read
109    */
110   virtual bool Read(char* data, const unsigned int length)
111   {
112     return false;
113   }
114
115   /**
116    * Open a new chunk
117    * @param[in] tag The FourCC tag for the chunk
118    * @return true if success.
119    */
120   virtual bool OpenChunk(const unsigned int tag) = 0;
121
122   /**
123    * Skip an entire chunk
124    * @param[in] tag The FourCC tag for the chunk
125    */
126   virtual void SkipChunk(const unsigned int tag)
127   {
128   }
129
130   /**
131    * Close the current chunk
132    * The chunk length is written to the archive
133    */
134   virtual void CloseChunk() = 0;
135
136   /**
137    * Peek at the tag of the next chunk.
138    * This will move the file pointer to the next even byte,
139    * then read the next four bytes
140    * @result The FourCC tag for the next chunk.
141    */
142   virtual unsigned int PeekChunk()
143   {
144     return 0;
145   }
146
147 protected:
148   unsigned int            mVersion;
149   std::iostream           mStream;
150   std::stack<ChunkHeader> mChunkStack;
151   bool                    mResult;
152 }; // class Archive
153
154 /**
155  * Archive specialization. Performs serialization to an Archive
156  */
157 class OutputArchive : public Archive
158 {
159 public:
160   OutputArchive(std::streambuf& buf, const unsigned int version);
161   virtual ~OutputArchive();
162
163 public: // from Archive
164   /**
165    * @copydoc Archive::Write()
166    */
167   virtual bool Write(const char* data, const unsigned int length);
168
169   /**
170    * @copydoc Archive::OpenChunk()
171    * @param[in] tag The chunk tag
172    */
173   virtual bool OpenChunk(const unsigned int tag);
174
175   /**
176    * @copydoc Archive::CloseChunk()
177    */
178   virtual void CloseChunk();
179
180 }; // class OutputArchive
181
182 /**
183  * Archive specialization. Performs serialization from an Archive
184  */
185 class InputArchive : public Archive
186 {
187 public:
188   InputArchive(std::streambuf& buf, const unsigned int version);
189   virtual ~InputArchive();
190
191 public: // from Archive
192   /**
193    * @copydoc Archive::Read()
194    */
195   virtual bool Read(char* data, const unsigned int length);
196
197   /**
198    * @copydoc Archive::OpenChunk()
199    * @param[in] tag The chunk tag
200    */
201   virtual bool OpenChunk(const unsigned int tag);
202
203   /**
204    * @copydoc Archive::SkipChunk()
205    * @param[in] tag The chunk tag
206    */
207   virtual void SkipChunk(const unsigned int tag);
208
209   /**
210    * @copydoc Archive::CloseChunk()
211    */
212   virtual void CloseChunk();
213
214   /**
215    * @copydoc Archive::PeekChunk()
216    */
217   virtual unsigned int PeekChunk();
218
219 public:
220   /**
221    * Set the archive version as read from the archive
222    * @param[in] version The archives version number
223    */
224   void SetFileVersion(unsigned int version)
225   {
226     mFileVersion = version;
227   }
228
229   /**
230    * Get the archive version number read from the archive
231    */
232   unsigned int GetFileVersion() const
233   {
234     return mFileVersion;
235   }
236 private:
237   unsigned int  mFileVersion;
238
239 }; // class InputArchive
240
241 namespace Serialize
242 {
243
244 Archive& operator<< (Archive&, const char&);
245 Archive& operator>> (Archive&, char&);
246 Archive& operator<< (Archive&, const unsigned char&);
247 Archive& operator>> (Archive&, unsigned char&);
248 Archive& operator<< (Archive&, const bool&);
249 Archive& operator>> (Archive&, bool&);
250 Archive& operator<< (Archive&, const short&);
251 Archive& operator>> (Archive&, short&);
252 Archive& operator<< (Archive&, const unsigned short&);
253 Archive& operator>> (Archive&, unsigned short&);
254 Archive& operator<< (Archive&, const int&);
255 Archive& operator>> (Archive&, int&);
256 Archive& operator<< (Archive&, const unsigned int&);
257 Archive& operator>> (Archive&, unsigned int&);
258 Archive& operator<< (Archive&, const float&);
259 Archive& operator>> (Archive&, float&);
260
261 Archive& operator<< (Archive&, const std::string&);
262 Archive& operator>> (Archive&, std::string&);
263
264 // math
265 Archive& operator<< (Archive&, const Vector2&);
266 Archive& operator>> (Archive&, Vector2&);
267 Archive& operator<< (Archive&, const Vector3&);
268 Archive& operator>> (Archive&, Vector3&);
269 Archive& operator<< (Archive&, const Vector4&);
270 Archive& operator>> (Archive&, Vector4&);
271 Archive& operator<< (Archive&, const Quaternion&);
272 Archive& operator>> (Archive&, Quaternion&);
273 Archive& operator<< (Archive&, const Matrix&);
274 Archive& operator>> (Archive&, Matrix&);
275
276 } // namespace Serialize
277 } // Internal
278
279 } // Dali
280
281 #endif // !defined(__DALI_INTERNAL_ARCHIVE_H__)
282
283