2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/event/resources/archive.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/math/vector2.h>
29 #include <dali/public-api/math/vector3.h>
30 #include <dali/public-api/math/vector4.h>
31 #include <dali/public-api/math/matrix.h>
32 #include <dali/public-api/math/quaternion.h>
40 /*--------- Archive ---------*/
42 Archive::Archive(std::streambuf& buf)
51 if(!mChunkStack.empty())
53 DALI_LOG_ERROR("mChunkStack should be empty!\n");
58 /*--------- OutputArchive ---------*/
60 OutputArchive::OutputArchive(std::streambuf& buf, const unsigned int version)
66 OutputArchive::~OutputArchive()
70 bool OutputArchive::Write(const char* data, const unsigned int length)
74 mStream.write(data, length);
83 bool OutputArchive::OpenChunk(const unsigned int fourCC)
85 const unsigned int zero = 0;
87 // Get current position in stream
88 std::streampos currentPos = mStream.tellp();
90 // Ensure chunk will start at an even byte
91 // (necessary for nested chunks)
94 const char padding = 0;
95 Write(&padding, sizeof(char));
98 // Write chunk header to stream
99 Write(reinterpret_cast<const char*>(&fourCC), sizeof(unsigned int));
101 // Push chunkHeader information to chunk LIFO stack
102 mChunkStack.push(std::make_pair(fourCC, mStream.tellp()));
104 // Write zero for chunk length temporarily. This will be overwritten in CloseChunk
105 Write(reinterpret_cast<const char*>(&zero), sizeof(unsigned int));
110 void OutputArchive::CloseChunk()
112 // Get current position in stream
113 std::streampos currentPos = mStream.tellp();
115 // retrieve chunk header
116 ChunkHeader chunkHeader = mChunkStack.top();
119 // seek to chunk header in stream
120 mStream.seekp(chunkHeader.second);
122 // calculate and write chunk size
123 unsigned int chunkLength = static_cast<unsigned int>(currentPos - chunkHeader.second) - sizeof(unsigned int);
124 Write(reinterpret_cast<const char*>(&chunkLength), sizeof(unsigned int));
126 // return to current position in stream
127 mStream.seekp(currentPos);
129 // Ensure next chunk will start at on even byte
132 const char padding = 0;
138 /*--------- InputArchive ---------*/
140 InputArchive::InputArchive(std::streambuf& buf, const unsigned int version)
147 InputArchive::~InputArchive()
151 bool InputArchive::Read(char* data, const unsigned int length)
155 mStream.read(data, length);
164 bool InputArchive::OpenChunk(const unsigned int fourCC)
166 if( PeekChunk() != fourCC )
168 // trying to open incorrect chunk, mark archive error
175 unsigned int fourCC_int;
180 if( mStream.tellg() & 1 )
182 mStream.seekg(1, std::ios_base::cur);
185 Read(fourCC_char, sizeof(unsigned int));
187 // Push chunkHeader information to chunk LIFO stack
188 mChunkStack.push(std::make_pair(fourCC_int, mStream.tellg()));
190 # if defined(DEBUG_ENABLED)
192 unsigned int chunkLength;
193 Read(reinterpret_cast<char*>(&chunkLength), sizeof(unsigned int));
195 DALI_LOG_INFO(Debug::Filter::gModel, Debug::Verbose, "Enter: %c%c%c%c(%d)\n",
196 fourCC_char[0], fourCC_char[1], fourCC_char[2], fourCC_char[3], chunkLength);
198 # else // defined(DEBUG_ENABLED)
201 mStream.seekg(sizeof(unsigned int), std::ios_base::cur);
203 # endif // defined(DEBUG_ENABLED)
208 void InputArchive::SkipChunk(const unsigned int fourCC)
210 // Ensure the next chunk is the correct one
211 if( PeekChunk() != fourCC )
218 unsigned int fourCC_int;
221 unsigned int chunkLength;
223 if( mStream.tellg() & 1 )
225 mStream.seekg(1, std::ios_base::cur);
228 Read(fourCC_char, sizeof(unsigned int));
229 Read(reinterpret_cast<char*>(&chunkLength), sizeof(unsigned int));
231 DALI_LOG_INFO(Debug::Filter::gModel, Debug::Verbose, "Enter: %c%c%c%c(%d)\n",
232 fourCC_char[0], fourCC_char[1], fourCC_char[2], fourCC_char[3], chunkLength);
234 if( chunkLength & 1 )
239 mStream.seekg(chunkLength, std::ios_base::cur);
242 void InputArchive::CloseChunk()
244 ChunkHeader chunkHeader = mChunkStack.top();
247 mStream.seekg(chunkHeader.second);
249 unsigned int chunkLength;
250 Read(reinterpret_cast<char*>(&chunkLength), sizeof(unsigned int));
256 mStream.seekg(chunkLength, std::ios_base::cur);
259 unsigned int InputArchive::PeekChunk()
263 unsigned int fourCC_int;
268 // Get current position in stream
269 std::streampos currentPos = mStream.tellg();
271 // Ensure next read will be from an even byte
274 mStream.seekg(1, std::ios_base::cur);
278 Read(fourCC_char, sizeof(unsigned int));
280 # if defined(DEBUG_ENABLED)
281 unsigned int chunkLength;
282 Read(reinterpret_cast<char*>(&chunkLength), sizeof(unsigned int));
284 DALI_LOG_INFO(Debug::Filter::gModel, Debug::Verbose, "Peek: %c%c%c%c(%d)\n",
285 fourCC_char[0], fourCC_char[1], fourCC_char[2], fourCC_char[3], chunkLength);
286 # endif // defined(DEBUG_ENABLED)
288 // return to original position in stream
289 mStream.seekg(currentPos);
296 /*------ serialization overrides of operator<< () and operator>> () ------*/
298 Archive& operator<< (Archive& ar, const char& t)
300 ar.Write((char*)&t, sizeof(t));
305 Archive& operator>> (Archive& ar, char& t)
307 ar.Read((char*)&t, sizeof(t));
312 Archive& operator<< (Archive& ar, const unsigned char& t)
314 ar.Write((char*)&t, sizeof(t));
318 Archive& operator>> (Archive& ar, unsigned char& t)
320 ar.Read((char*)&t, sizeof(t));
325 Archive& operator<< (Archive& ar, const bool& t)
327 char value = t ? 1 : 0;
328 ar.Write(&value, sizeof(value));
332 Archive& operator>> (Archive& ar, bool& t)
335 ar.Read(&value, sizeof(value));
341 Archive& operator<< (Archive& ar, const short& t)
343 ar.Write((char*)&t, sizeof(t));
348 Archive& operator>> (Archive& ar, short& t)
350 ar.Read((char*)&t, sizeof(t));
355 Archive& operator<< (Archive& ar, const unsigned short& t)
357 ar.Write((char*)&t, sizeof(t));
362 Archive& operator>> (Archive& ar, unsigned short& t)
364 ar.Read((char*)&t, sizeof(t));
369 Archive& operator<< (Archive& ar, const int& t)
371 ar.Write((char*)&t, sizeof(t));
376 Archive& operator>> (Archive& ar, int& t)
378 ar.Read((char*)&t, sizeof(t));
383 Archive& operator<< (Archive& ar, const unsigned int& t)
385 ar.Write((char*)&t, sizeof(t));
390 Archive& operator>> (Archive& ar, unsigned int& t)
392 ar.Read((char*)&t, sizeof(t));
398 Archive& operator<< (Archive& ar, const float& t)
400 ar.Write((char*)&t, sizeof(t));
405 Archive& operator>> (Archive& ar, float& t)
407 ar.Read((char*)&t, sizeof(t));
413 Archive& operator<< (Archive& ar, const Vector2& t)
419 Archive& operator>> (Archive& ar, Vector2& t)
426 Archive& operator<< (Archive& ar, const Vector3& t)
428 ar << t.x << t.y << t.z;
432 Archive& operator>> (Archive& ar, Vector3& t)
434 ar >> t.x >> t.y >> t.z;
439 Archive& operator<< (Archive& ar, const Vector4& t)
441 ar << t.x << t.y << t.z << t.w;
445 Archive& operator>> (Archive& ar, Vector4& t)
447 ar >> t.x >> t.y >> t.z >> t.w;
452 Archive& operator<< (Archive& ar, const Quaternion& t)
459 Archive& operator>> (Archive& ar, Quaternion& t)
467 Archive& operator<< (Archive& ar, const Matrix& t)
469 const float* data = t.AsFloat();
471 for( unsigned i = 0; i < sizeof(Matrix) / sizeof(float); ++i)
480 Archive& operator>> (Archive& ar, Matrix& t)
482 float* data = t.AsFloat();
484 for( unsigned i = 0; i < sizeof(Matrix) / sizeof(float); ++i)
493 /*---- std::string ----*/
494 Archive& operator<< (Archive& ar, const std::string& t)
496 unsigned int length = t.size();
498 ar.Write(t.data(), length);
503 Archive& operator>> (Archive& ar, std::string& t)
505 unsigned int length = 0;
509 data = new char[ length ];
510 ar.Read(data, length);
511 t.assign(data, length);
517 } // namespace Serialize