sync with tizen_2.0
[platform/framework/native/appfw.git] / src / io / inc / FIo_IpcCommonParamTraits.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FIo_IpcCommonParamTraits.h
20  * @brief               This is the header file for common param traits.
21  */
22
23 #ifndef _FIO_INTERNAL_IPC_COMMON_PARAM_TRAITS_H_
24 #define _FIO_INTERNAL_IPC_COMMON_PARAM_TRAITS_H_
25 #pragma once
26
27 #include <base/tuple.h>
28 #include <ipc/ipc_param_traits.h>
29
30 #include <FBaseString.h>
31 #include <FBaseByteBuffer.h>
32 #include <FBaseColArrayList.h>
33 #include <FBaseColHashMap.h>
34 #include <FBaseSysLog.h>
35 #include "FIo_IpcCommonDataTypes.h"
36
37 namespace IPC
38 {
39
40 const static int IPC_OBJECT_TYPE_STRING = 0;
41 const static int IPC_OBJECT_TYPE_BYTE_BUFFER = 1;
42
43 template<>
44 struct ParamTraits <Tizen::Base::String>
45 {
46         typedef Tizen::Base::String param_type;
47
48         static void Write(Message* m, const param_type& p)
49         {
50                 int len = (p.GetLength() + 1) * sizeof(wchar_t);
51                 m->WriteInt(len);
52                 m->WriteBytes((void*) p.GetPointer(), (p.GetLength() + 1) * sizeof(wchar_t));
53         }
54
55         static bool Read(const Message* m, void** iter, param_type* r)
56         {
57                 const char* pStr = null;
58                 int len = 0;
59
60                 if (!m->ReadLength(iter, &len))
61                 {
62                         return false;
63                 }
64
65                 m->ReadBytes(iter, &pStr, len);
66                 r->Clear();
67                 r->Append((wchar_t*) pStr);
68
69                 return true;
70         }
71
72         static void Log(const param_type& p, std::string* l)
73         {
74         }
75 };
76
77 template<>
78 struct ParamTraits <Tizen::Base::ByteBuffer>
79 {
80         typedef Tizen::Base::ByteBuffer param_type;
81
82         static void Write(Message* m, const param_type& p)
83         {
84                 int capacity = p.GetCapacity();
85
86                 m->WriteInt(capacity);
87                 m->WriteBytes((void*) p.GetPointer(), capacity);
88                 m->WriteInt(p.GetLimit());
89                 m->WriteInt(p.GetPosition());
90         }
91
92         static bool Read(const Message* m, void** iter, param_type* r)
93         {
94                 const byte* pByte = null;
95                 int capacity = 0;
96                 int value = 0;
97
98                 if (!m->ReadLength(iter, &capacity))
99                 {
100                         return false;
101                 }
102                 r->Construct(capacity);
103
104                 m->ReadBytes(iter, (const char**)&pByte, capacity);
105                 r->SetArray(pByte, 0, capacity);
106
107                 m->ReadLength(iter, &value);
108                 if (r->SetLimit(value) != E_SUCCESS)
109                 {
110                         return false;
111                 }
112
113                 m->ReadLength(iter, &value);
114                 r->SetPosition(value);
115
116                 return true;
117         }
118
119         static void Log(const param_type& p, std::string* l)
120         {
121         }
122 };
123
124 template<>
125 struct ParamTraits <Tizen::Base::Collection::ArrayList>
126 {
127         typedef Tizen::Base::Collection::ArrayList param_type;
128
129         static void Write(Message* m, const param_type& p)
130         {
131                 int count = p.GetCount();
132                 const Tizen::Base::String* pType = NULL;
133
134                 WriteParam(m, count);
135
136                 for (int i = 0; i < count; i++)
137                 {
138                         pType = dynamic_cast < const Tizen::Base::String* > (p.GetAt(i));
139                         if (pType)
140                         {
141                                 WriteParam(m, IPC_OBJECT_TYPE_STRING);
142                                 WriteParam(m, *pType);
143                         }
144                         else
145                         {
146                                 WriteParam(m, IPC_OBJECT_TYPE_BYTE_BUFFER);
147                                 WriteParam(m, *(Tizen::Base::ByteBuffer*) p.GetAt(i));
148                         }
149                 }
150         }
151
152         static bool Read(const Message* m, void** iter, param_type* r)
153         {
154                 Tizen::Base::String* pStr = null;
155                 Tizen::Base::ByteBuffer* pBuffer = null;
156                 int count = 0;
157                 int type = 0;
158
159                 if (!m->ReadLength(iter, &count))
160                 {
161                         return false;
162                 }
163
164                 r->Construct(count);
165
166                 for (int i = 0; i < count; i++)
167                 {
168                         m->ReadLength(iter, &type);
169                         if (type == IPC_OBJECT_TYPE_STRING)
170                         {
171                                 pStr = new Tizen::Base::String;
172                                 if (!ReadParam(m, iter, pStr))
173                                 {
174                                         delete pStr;
175                                         return false;
176                                 }
177                                 r->Add(*pStr);
178                         }
179                         else
180                         {
181                                 pBuffer = new Tizen::Base::ByteBuffer;
182                                 if (!ReadParam(m, iter, pBuffer))
183                                 {
184                                         delete pBuffer;
185                                         return false;
186                                 }
187                                 r->Add(*pBuffer);
188                         }
189                 }
190
191                 return true;
192         }
193
194         static void Log(const param_type& p, std::string* l)
195         {
196         }
197 };
198
199 template<>
200 struct ParamTraits <Tizen::Base::Collection::HashMap>
201 {
202         typedef Tizen::Base::Collection::HashMap param_type;
203
204         static void Write(Message* m, const param_type& p)
205         {
206                 int count = p.GetCount();
207                 const Tizen::Base::String* pType = NULL;
208
209                 Tizen::Base::Collection::IList* pKeys = p.GetKeysN();
210                 Tizen::Base::Collection::IList* pValues = p.GetValuesN();
211
212                 WriteParam(m, count);
213
214                 for (int i = 0; i < count; i++)
215                 {
216                         WriteParam(m, *(Tizen::Base::String*) pKeys->GetAt(i));
217
218                         pType = dynamic_cast < const Tizen::Base::String* > (pValues->GetAt(i));
219                         if (pType)
220                         {
221                                 WriteParam(m, IPC_OBJECT_TYPE_STRING);
222                                 WriteParam(m, *pType);
223                         }
224                         else
225                         {
226                                 WriteParam(m, IPC_OBJECT_TYPE_BYTE_BUFFER);
227                                 WriteParam(m, *(Tizen::Base::ByteBuffer*) pValues->GetAt(i));
228                         }
229                 }
230
231                 delete pKeys;
232                 delete pValues;
233         }
234
235         static bool Read(const Message* m, void** iter, param_type* r)
236         {
237                 Tizen::Base::String* pKey = null;
238                 Tizen::Base::String* pStr = null;
239                 Tizen::Base::ByteBuffer* pBuffer = null;
240
241                 int count = 0;
242                 int type = 0;
243
244                 if (!m->ReadLength(iter, &count))
245                 {
246                         return false;
247                 }
248
249                 r->Construct(count);
250
251                 for (int i = 0; i < count; i++)
252                 {
253                         pKey = new Tizen::Base::String();
254
255                         if (!ReadParam(m, iter, pKey))
256                         {
257                                 delete pKey;
258                                 return false;
259                         }
260
261                         m->ReadLength(iter, &type);
262
263                         if (type == IPC_OBJECT_TYPE_STRING)
264                         {
265                                 pStr = new Tizen::Base::String();
266                                 if (!ReadParam(m, iter, pStr))
267                                 {
268                                         delete pKey;
269                                         delete pStr;
270                                         return false;
271                                 }
272
273                                 r->Add(*pKey, *pStr);
274                         }
275                         else
276                         {
277                                 pBuffer = new Tizen::Base::ByteBuffer();
278                                 if (!ReadParam(m, iter, pBuffer))
279                                 {
280                                         delete pKey;
281                                         delete pBuffer;
282                                         return false;
283                                 }
284
285                                 r->Add(*pKey, *pBuffer);
286                         }
287
288                 }
289
290                 return true;
291         }
292
293         static void Log(const param_type& p, std::string* l)
294         {
295         }
296 };
297
298 template<>
299 struct ParamTraits <Tizen::Io::_IpcBuffer>
300 {
301         typedef Tizen::Io::_IpcBuffer param_type;
302
303         static void Write(Message* m, const param_type& p)
304         {
305                 m->WriteInt(p.size);
306                 m->WriteBytes((void*) p.pBuffer, p.size);
307         }
308
309         static bool Read(const Message* m, void** iter, param_type* r)
310         {
311                 const char* pBuffer = null;
312                 int len = 0;
313                 if (!m->ReadLength(iter, &len))
314                 {
315                         return false;
316                 }
317
318                 m->ReadBytes(iter, &pBuffer, len);
319
320                 r->size = len;
321                 r->pBuffer = (void*) pBuffer;
322
323
324                 return true;
325         }
326
327         static void Log(const param_type& p, std::string* l)
328         {
329         }
330 };
331
332 }
333
334 #endif // _FIO_INTERNAL_CLIENT_CHANNEL_H_