Add an ArrayList<String> to the IPC paramtraits
[platform/framework/native/appfw.git] / src / io / inc / FIo_IpcCommonParamTraits.h
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FIo_IpcCommonParamTraits.h
19  * @brief               This is the header file for common param traits.
20  */
21
22 #ifndef _FIO_INTERNAL_IPC_COMMON_PARAM_TRAITS_H_
23 #define _FIO_INTERNAL_IPC_COMMON_PARAM_TRAITS_H_
24 #pragma once
25
26 #include <base/tuple.h>
27 #include <ipc/ipc_param_traits.h>
28
29 #include <FBaseString.h>
30 #include <FBaseByteBuffer.h>
31 #include <FBaseColArrayList.h>
32 #include <FBaseColArrayListT.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::ArrayListT<Tizen::Base::String> >
201 {
202         typedef Tizen::Base::Collection::ArrayListT<Tizen::Base::String> param_type;
203
204         static void Write(Message* m, const param_type& p)
205         {
206                 int count = p.GetCount();
207                 Tizen::Base::String str;
208
209                 WriteParam(m, count);
210
211                 for (int i = 0; i < count; i++)
212                 {
213                         p.GetAt(i, str);
214                         WriteParam(m, str);
215                 }
216         }
217
218         static bool Read(const Message* m, void** iter, param_type* r)
219         {
220                 Tizen::Base::String str;
221                 int count = 0;
222
223                 if (!m->ReadLength(iter, &count))
224                 {
225                         return false;
226                 }
227
228                 r->Construct(count);
229
230                 for (int i = 0; i < count; i++)
231                 {
232                         if (!ReadParam(m, iter, &str))
233                         {
234                                 return false;
235                         }
236                         r->Add(str);
237                 }
238
239                 return true;
240         }
241
242         static void Log(const param_type& p, std::string* l)
243         {
244         }
245 };
246
247 template<>
248 struct ParamTraits <Tizen::Base::Collection::HashMap>
249 {
250         typedef Tizen::Base::Collection::HashMap param_type;
251
252         static void Write(Message* m, const param_type& p)
253         {
254                 int count = p.GetCount();
255                 const Tizen::Base::String* pType = NULL;
256
257                 Tizen::Base::Collection::IList* pKeys = p.GetKeysN();
258                 Tizen::Base::Collection::IList* pValues = p.GetValuesN();
259
260                 WriteParam(m, count);
261
262                 for (int i = 0; i < count; i++)
263                 {
264                         WriteParam(m, *(Tizen::Base::String*) pKeys->GetAt(i));
265
266                         pType = dynamic_cast < const Tizen::Base::String* > (pValues->GetAt(i));
267                         if (pType)
268                         {
269                                 WriteParam(m, IPC_OBJECT_TYPE_STRING);
270                                 WriteParam(m, *pType);
271                         }
272                         else
273                         {
274                                 WriteParam(m, IPC_OBJECT_TYPE_BYTE_BUFFER);
275                                 WriteParam(m, *(Tizen::Base::ByteBuffer*) pValues->GetAt(i));
276                         }
277                 }
278
279                 delete pKeys;
280                 delete pValues;
281         }
282
283         static bool Read(const Message* m, void** iter, param_type* r)
284         {
285                 Tizen::Base::String* pKey = null;
286                 Tizen::Base::String* pStr = null;
287                 Tizen::Base::ByteBuffer* pBuffer = null;
288
289                 int count = 0;
290                 int type = 0;
291
292                 if (!m->ReadLength(iter, &count))
293                 {
294                         return false;
295                 }
296
297                 r->Construct(count);
298
299                 for (int i = 0; i < count; i++)
300                 {
301                         pKey = new Tizen::Base::String();
302
303                         if (!ReadParam(m, iter, pKey))
304                         {
305                                 delete pKey;
306                                 return false;
307                         }
308
309                         m->ReadLength(iter, &type);
310
311                         if (type == IPC_OBJECT_TYPE_STRING)
312                         {
313                                 pStr = new Tizen::Base::String();
314                                 if (!ReadParam(m, iter, pStr))
315                                 {
316                                         delete pKey;
317                                         delete pStr;
318                                         return false;
319                                 }
320
321                                 r->Add(*pKey, *pStr);
322                         }
323                         else
324                         {
325                                 pBuffer = new Tizen::Base::ByteBuffer();
326                                 if (!ReadParam(m, iter, pBuffer))
327                                 {
328                                         delete pKey;
329                                         delete pBuffer;
330                                         return false;
331                                 }
332
333                                 r->Add(*pKey, *pBuffer);
334                         }
335
336                 }
337
338                 return true;
339         }
340
341         static void Log(const param_type& p, std::string* l)
342         {
343         }
344 };
345
346 template<>
347 struct ParamTraits <Tizen::Io::_IpcBuffer>
348 {
349         typedef Tizen::Io::_IpcBuffer param_type;
350
351         static void Write(Message* m, const param_type& p)
352         {
353                 m->WriteInt(p.size);
354                 m->WriteBytes((void*) p.pBuffer, p.size);
355         }
356
357         static bool Read(const Message* m, void** iter, param_type* r)
358         {
359                 const char* pBuffer = null;
360                 int len = 0;
361                 if (!m->ReadLength(iter, &len))
362                 {
363                         return false;
364                 }
365
366                 m->ReadBytes(iter, &pBuffer, len);
367
368                 r->size = len;
369                 r->pBuffer = malloc(len);
370                 memcpy(r->pBuffer, pBuffer, len);
371
372                 return true;
373         }
374
375         static void Log(const param_type& p, std::string* l)
376         {
377         }
378 };
379
380 }
381
382 #endif // _FIO_INTERNAL_CLIENT_CHANNEL_H_