secureswitools/swisistools/source/interpretsislib/deserialiser.h
changeset 0 ba25891c3a9e
child 24 5cc91383ab1e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef	DESERIALISER_H
       
    20 #define	DESERIALISER_H
       
    21 
       
    22 #include <iostream>
       
    23 #include <stdexcept>
       
    24 #include <istream>
       
    25 #include <vector>
       
    26 #include "serialisable.h"
       
    27 #include "cardinality.h"
       
    28 #include "ucmp.h"
       
    29 
       
    30 #pragma warning (disable: 4800)
       
    31 
       
    32 
       
    33 /**
       
    34 * @file DESERIALISER.H
       
    35 *
       
    36 * @internalComponent
       
    37 * @released
       
    38 */
       
    39 class Deserialiser
       
    40 	{
       
    41 public:
       
    42 	Deserialiser(std::istream& stream) : iStream(stream)
       
    43 	{}
       
    44 
       
    45 	Deserialiser& operator>>(Serialisable& val)
       
    46 	{
       
    47 	val.Internalize(*this);
       
    48 	return *this;
       
    49 	}
       
    50 
       
    51 	// integer types
       
    52 	Deserialiser& operator>>(TInt64& val)
       
    53 	{ iStream.read(reinterpret_cast<char*>(&val),8); return *this; }
       
    54 
       
    55 	Deserialiser& operator>>(TInt& val)
       
    56 	{ iStream.read((char*)&val,4); return *this; }
       
    57 
       
    58 	Deserialiser& operator>>(TInt16& val)
       
    59 	{ iStream.read((char*)&val,2); return *this; }
       
    60 
       
    61 	Deserialiser& operator>>(TInt8& val)
       
    62 	{ iStream.read((char*)&val,1) ; return *this; }
       
    63 
       
    64 	Deserialiser& operator>>(TUint64& val)
       
    65 	{ iStream.read(reinterpret_cast<char*>(&val),8);  return *this; }
       
    66 
       
    67 	Deserialiser& operator>>(TUint32& val)
       
    68 	{ iStream.read((char*)&val,4);  return *this; }
       
    69 
       
    70 	Deserialiser& operator>>(TUint16& val)
       
    71 	{ iStream.read((char*)&val,2); return *this; }
       
    72 
       
    73 	Deserialiser& operator>>(TUint8& val)
       
    74 	{iStream.read((char*)&val,1); return *this; }
       
    75 
       
    76 	Deserialiser& operator>>(bool& val)
       
    77 	{
       
    78 	TUint32 x = 0;
       
    79 	iStream.read((char*)&x,4);
       
    80 	val = (bool)x;
       
    81 	return *this;
       
    82 	}
       
    83 
       
    84 
       
    85 	template <class T>
       
    86 	Deserialiser& operator>>(std::vector<T>& val)
       
    87 	{
       
    88 	TUint32 size = 0;
       
    89 	*this >> size;
       
    90 	val.resize(size);
       
    91 	for (TUint32 i = 0; i < size ; ++i)
       
    92 		{
       
    93 		*this >> val[i];
       
    94 		}
       
    95 	return *this;
       
    96 	}
       
    97 
       
    98 #ifndef _MSC_VER
       
    99 
       
   100 	template <class T>
       
   101 	Deserialiser& operator>>(std::vector<T*>& val)
       
   102 	{
       
   103 	TUint32 size = 0;
       
   104 	*this >> size;
       
   105 	val.resize(size);
       
   106 	for (TUint32 i = 0; i < size ; ++i)
       
   107 		{
       
   108 		val[i] = new T;
       
   109 		*this >> *val[i];
       
   110 		}
       
   111 	return *this;
       
   112 	}
       
   113 
       
   114 #endif
       
   115 
       
   116 	Deserialiser& operator>>(std::string& val)
       
   117 	{
       
   118 
       
   119 	Cardinality card;
       
   120 	*this >> card;
       
   121 	TUint32 size = card;
       
   122 
       
   123 	char* buff = 0;
       
   124 	if (size & 0x01)
       
   125 		{
       
   126 		// 8-bit
       
   127 		size = size >> 1;
       
   128 		buff = new char[(int)size];
       
   129 		iStream.read(buff, size);
       
   130 		}
       
   131 	else
       
   132 		{
       
   133 		throw std::runtime_error("Decoding unicode into std::string");
       
   134 		}
       
   135 	val.assign(buff, size);
       
   136 	delete buff;
       
   137 	return *this;
       
   138 	}
       
   139 
       
   140 	Deserialiser& operator>>(std::wstring& val)
       
   141 	{
       
   142 
       
   143 	Cardinality card;
       
   144 	*this >> card;
       
   145 	TUint32 size = card;
       
   146 
       
   147 	wchar_t* buff = 0;
       
   148 	if (size & 0x01)
       
   149 		{
       
   150 		throw std::runtime_error("Decoding 8bit text into std::wstring");
       
   151 		}
       
   152 	else
       
   153 		{
       
   154 		size = size >> 1;
       
   155 		buff = new wchar_t[(int)size];
       
   156 		TUnicodeExpander exp;
       
   157 		TMemoryUnicodeSink sink((TUint16*)buff);
       
   158 		exp.ExpandL(sink, *this ,size);
       
   159 		}
       
   160 	val.assign(buff, size);
       
   161 	delete buff;
       
   162 	return *this;
       
   163 	}
       
   164 
       
   165 	void read(TUint8* aDst, TUint32 aCount)
       
   166 	{
       
   167 	iStream.read((char*)aDst, aCount);
       
   168 	}
       
   169 
       
   170 
       
   171 private:
       
   172 	std::istream& iStream;
       
   173 	};
       
   174 
       
   175 #endif	/* DESERIALISER_H */