Fix the boiler plate codes
[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 <FBaseColHashMap.h>
33 #include <FBaseSysLog.h>
34 #include "FIo_IpcCommonDataTypes.h"
35
36 namespace IPC
37 {
38
39 const static int IPC_OBJECT_TYPE_STRING = 0;
40 const static int IPC_OBJECT_TYPE_BYTE_BUFFER = 1;
41
42 template<>
43 struct ParamTraits <Tizen::Base::String>
44 {
45         typedef Tizen::Base::String param_type;
46
47         static void Write(Message* m, const param_type& p)
48         {
49                 int len = (p.GetLength() + 1) * sizeof(wchar_t);
50                 m->WriteInt(len);
51                 m->WriteBytes((void*) p.GetPointer(), (p.GetLength() + 1) * sizeof(wchar_t));
52         }
53
54         static bool Read(const Message* m, void** iter, param_type* r)
55         {
56                 const char* pStr = null;
57                 int len = 0;
58
59                 if (!m->ReadLength(iter, &len))
60                 {
61                         return false;
62                 }
63
64                 m->ReadBytes(iter, &pStr, len);
65                 r->Clear();
66                 r->Append((wchar_t*) pStr);
67
68                 return true;
69         }
70
71         static void Log(const param_type& p, std::string* l)
72         {
73         }
74 };
75
76 template<>
77 struct ParamTraits <Tizen::Base::ByteBuffer>
78 {
79         typedef Tizen::Base::ByteBuffer param_type;
80
81         static void Write(Message* m, const param_type& p)
82         {
83                 int capacity = p.GetCapacity();
84
85                 m->WriteInt(capacity);
86                 m->WriteBytes((void*) p.GetPointer(), capacity);
87                 m->WriteInt(p.GetLimit());
88                 m->WriteInt(p.GetPosition());
89         }
90
91         static bool Read(const Message* m, void** iter, param_type* r)
92         {
93                 const byte* pByte = null;
94                 int capacity = 0;
95                 int value = 0;
96
97                 if (!m->ReadLength(iter, &capacity))
98                 {
99                         return false;
100                 }
101                 r->Construct(capacity);
102
103                 m->ReadBytes(iter, (const char**)&pByte, capacity);
104                 r->SetArray(pByte, 0, capacity);
105
106                 m->ReadLength(iter, &value);
107                 if (r->SetLimit(value) != E_SUCCESS)
108                 {
109                         return false;
110                 }
111
112                 m->ReadLength(iter, &value);
113                 r->SetPosition(value);
114
115                 return true;
116         }
117
118         static void Log(const param_type& p, std::string* l)
119         {
120         }
121 };
122
123 template<>
124 struct ParamTraits <Tizen::Base::Collection::ArrayList>
125 {
126         typedef Tizen::Base::Collection::ArrayList param_type;
127
128         static void Write(Message* m, const param_type& p)
129         {
130                 int count = p.GetCount();
131                 const Tizen::Base::String* pType = NULL;
132
133                 WriteParam(m, count);
134
135                 for (int i = 0; i < count; i++)
136                 {
137                         pType = dynamic_cast < const Tizen::Base::String* > (p.GetAt(i));
138                         if (pType)
139                         {
140                                 WriteParam(m, IPC_OBJECT_TYPE_STRING);
141                                 WriteParam(m, *pType);
142                         }
143                         else
144                         {
145                                 WriteParam(m, IPC_OBJECT_TYPE_BYTE_BUFFER);
146                                 WriteParam(m, *(Tizen::Base::ByteBuffer*) p.GetAt(i));
147                         }
148                 }
149         }
150
151         static bool Read(const Message* m, void** iter, param_type* r)
152         {
153                 Tizen::Base::String* pStr = null;
154                 Tizen::Base::ByteBuffer* pBuffer = null;
155                 int count = 0;
156                 int type = 0;
157
158                 if (!m->ReadLength(iter, &count))
159                 {
160                         return false;
161                 }
162
163                 r->Construct(count);
164
165                 for (int i = 0; i < count; i++)
166                 {
167                         m->ReadLength(iter, &type);
168                         if (type == IPC_OBJECT_TYPE_STRING)
169                         {
170                                 pStr = new Tizen::Base::String;
171                                 if (!ReadParam(m, iter, pStr))
172                                 {
173                                         delete pStr;
174                                         return false;
175                                 }
176                                 r->Add(*pStr);
177                         }
178                         else
179                         {
180                                 pBuffer = new Tizen::Base::ByteBuffer;
181                                 if (!ReadParam(m, iter, pBuffer))
182                                 {
183                                         delete pBuffer;
184                                         return false;
185                                 }
186                                 r->Add(*pBuffer);
187                         }
188                 }
189
190                 return true;
191         }
192
193         static void Log(const param_type& p, std::string* l)
194         {
195         }
196 };
197
198 template<>
199 struct ParamTraits <Tizen::Base::Collection::HashMap>
200 {
201         typedef Tizen::Base::Collection::HashMap param_type;
202
203         static void Write(Message* m, const param_type& p)
204         {
205                 int count = p.GetCount();
206                 const Tizen::Base::String* pType = NULL;
207
208                 Tizen::Base::Collection::IList* pKeys = p.GetKeysN();
209                 Tizen::Base::Collection::IList* pValues = p.GetValuesN();
210
211                 WriteParam(m, count);
212
213                 for (int i = 0; i < count; i++)
214                 {
215                         WriteParam(m, *(Tizen::Base::String*) pKeys->GetAt(i));
216
217                         pType = dynamic_cast < const Tizen::Base::String* > (pValues->GetAt(i));
218                         if (pType)
219                         {
220                                 WriteParam(m, IPC_OBJECT_TYPE_STRING);
221                                 WriteParam(m, *pType);
222                         }
223                         else
224                         {
225                                 WriteParam(m, IPC_OBJECT_TYPE_BYTE_BUFFER);
226                                 WriteParam(m, *(Tizen::Base::ByteBuffer*) pValues->GetAt(i));
227                         }
228                 }
229
230                 delete pKeys;
231                 delete pValues;
232         }
233
234         static bool Read(const Message* m, void** iter, param_type* r)
235         {
236                 Tizen::Base::String* pKey = null;
237                 Tizen::Base::String* pStr = null;
238                 Tizen::Base::ByteBuffer* pBuffer = null;
239
240                 int count = 0;
241                 int type = 0;
242
243                 if (!m->ReadLength(iter, &count))
244                 {
245                         return false;
246                 }
247
248                 r->Construct(count);
249
250                 for (int i = 0; i < count; i++)
251                 {
252                         pKey = new Tizen::Base::String();
253
254                         if (!ReadParam(m, iter, pKey))
255                         {
256                                 delete pKey;
257                                 return false;
258                         }
259
260                         m->ReadLength(iter, &type);
261
262                         if (type == IPC_OBJECT_TYPE_STRING)
263                         {
264                                 pStr = new Tizen::Base::String();
265                                 if (!ReadParam(m, iter, pStr))
266                                 {
267                                         delete pKey;
268                                         delete pStr;
269                                         return false;
270                                 }
271
272                                 r->Add(*pKey, *pStr);
273                         }
274                         else
275                         {
276                                 pBuffer = new Tizen::Base::ByteBuffer();
277                                 if (!ReadParam(m, iter, pBuffer))
278                                 {
279                                         delete pKey;
280                                         delete pBuffer;
281                                         return false;
282                                 }
283
284                                 r->Add(*pKey, *pBuffer);
285                         }
286
287                 }
288
289                 return true;
290         }
291
292         static void Log(const param_type& p, std::string* l)
293         {
294         }
295 };
296
297 template<>
298 struct ParamTraits <Tizen::Io::_IpcBuffer>
299 {
300         typedef Tizen::Io::_IpcBuffer param_type;
301
302         static void Write(Message* m, const param_type& p)
303         {
304                 m->WriteInt(p.size);
305                 m->WriteBytes((void*) p.pBuffer, p.size);
306         }
307
308         static bool Read(const Message* m, void** iter, param_type* r)
309         {
310                 const char* pBuffer = null;
311                 int len = 0;
312                 if (!m->ReadLength(iter, &len))
313                 {
314                         return false;
315                 }
316
317                 m->ReadBytes(iter, &pBuffer, len);
318
319                 r->size = len;
320                 r->pBuffer = malloc(len);
321                 memcpy(r->pBuffer, pBuffer, len);
322
323                 return true;
324         }
325
326         static void Log(const param_type& p, std::string* l)
327         {
328         }
329 };
330
331 }
332
333 #endif // _FIO_INTERNAL_CLIENT_CHANNEL_H_