1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #ifndef COMMON_RESOURCEATTRIBUTESCONVERTER_H
22 #define COMMON_RESOURCEATTRIBUTESCONVERTER_H
24 #include <RCSResourceAttributes.h>
26 #include <OCRepresentation.h>
37 template < typename T >
43 template< OC::AttributeType TYPE >
47 struct OCBaseType< OC::AttributeType::Integer > : TypeDef< int >{ };
50 struct OCBaseType< OC::AttributeType::Double > : TypeDef< double > { };
53 struct OCBaseType< OC::AttributeType::Boolean > : TypeDef< bool > { };
56 struct OCBaseType< OC::AttributeType::String > : TypeDef< std::string > { };
59 struct OCBaseType< OC::AttributeType::Binary > : TypeDef< RCSByteString::DataType > { };
62 struct OCBaseType< OC::AttributeType::OCByteString > : TypeDef< OCByteString > { };
65 struct OCBaseType< OC::AttributeType::OCRepresentation >
66 : TypeDef< OC::OCRepresentation >
69 template< int DEPTH, typename BASE_TYPE >
72 typedef std::vector< typename SeqType< DEPTH - 1, BASE_TYPE >::type > type;
75 template< typename BASE_TYPE >
76 struct SeqType< 0, BASE_TYPE >
78 typedef BASE_TYPE type;
81 template< int DEPTH, OC::AttributeType BASE_TYPE >
84 typedef typename SeqType< DEPTH,
85 typename OCBaseType< BASE_TYPE >::type >::type type;
88 template< typename T >
93 constexpr static size_t depth = 0;
96 template< typename T >
97 struct TypeInfo< std::vector< T > >
100 typedef typename TypeInfo< T >::base_type base_type;
101 constexpr static size_t depth = 1 + TypeInfo< T >::depth;
105 class ResourceAttributesConverter
108 ResourceAttributesConverter() = delete;
110 class ResourceAttributesBuilder
113 template < int DEPTH >
114 void insertItem(Detail::Int2Type< DEPTH >,
115 const OC::OCRepresentation::AttributeItem& item)
117 switch (item.base_type()) {
118 case OC::AttributeType::Null:
119 return putValue(item.attrname(), nullptr);
121 case OC::AttributeType::Integer:
122 return insertItem< DEPTH, OC::AttributeType::Integer >(item);
124 case OC::AttributeType::Double:
125 return insertItem< DEPTH, OC::AttributeType::Double >(item);
127 case OC::AttributeType::Boolean:
128 return insertItem< DEPTH, OC::AttributeType::Boolean >(item);
130 case OC::AttributeType::String:
131 return insertItem< DEPTH, OC::AttributeType::String >(item);
133 case OC::AttributeType::Binary:
134 // OCRep support only 0-depth for binary type.
135 // If RI changed, this line should be changed to DEPTH.
136 return insertOcBinary< OC::AttributeType::Binary >
137 (Detail::Int2Type< 0 >{ }, item);
139 case OC::AttributeType::OCByteString:
140 return insertOcBinary< OC::AttributeType::OCByteString >
141 (Detail::Int2Type< DEPTH >{ }, item);
143 case OC::AttributeType::OCRepresentation:
144 return insertOcRep(Detail::Int2Type< DEPTH >{ }, item);
147 assert("There must be no another base type!");
151 template< int DEPTH, OC::AttributeType BASE_TYPE >
152 void insertItem(const OC::OCRepresentation::AttributeItem& item)
154 typedef typename Detail::OCItemType< DEPTH, BASE_TYPE >::type ItemType;
155 putValue(item.attrname(), item.getValue< ItemType >());
158 RCSResourceAttributes insertOcRep(Detail::Int2Type< 0 >,
159 const OC::OCRepresentation& ocRep)
161 return ResourceAttributesConverter::fromOCRepresentation(ocRep);
164 template< int DEPTH, typename OCREPS,
165 typename ATTRS = typename Detail::SeqType< DEPTH, RCSResourceAttributes >::type >
166 ATTRS insertOcRep(Detail::Int2Type< DEPTH >, const OCREPS& ocRepVec)
170 for (const auto& nested : ocRepVec)
172 result.push_back(insertOcRep(Detail::Int2Type< DEPTH - 1 >{ }, nested));
178 template< int DEPTH >
179 void insertOcRep(Detail::Int2Type< DEPTH >,
180 const OC::OCRepresentation::AttributeItem& item)
182 typedef typename Detail::OCItemType< DEPTH,
183 OC::AttributeType::OCRepresentation >::type ItemType;
185 putValue(item.attrname(),
186 insertOcRep(Detail::Int2Type< DEPTH >{ }, item.getValue< ItemType >()));
189 template< typename OCREP >
190 RCSByteString insertOcBinary(Detail::Int2Type< 0 >, const OCREP& ocBinary)
192 return RCSByteString(ocBinary);
195 template< int DEPTH, typename OCREPS,
196 typename ATTRS = typename Detail::SeqType< DEPTH, RCSByteString >::type >
197 ATTRS insertOcBinary(Detail::Int2Type< DEPTH >, const OCREPS& ocBinaryVec)
201 for (const auto& nested : ocBinaryVec)
203 result.push_back(insertOcBinary(Detail::Int2Type< DEPTH - 1 >{ }, nested));
209 template< OC::AttributeType BASE_TYPE, int DEPTH >
210 void insertOcBinary(Detail::Int2Type< DEPTH >,
211 const OC::OCRepresentation::AttributeItem& item)
213 typedef typename Detail::OCItemType< DEPTH, BASE_TYPE >::type ItemType;
215 putValue(item.attrname(),
216 insertOcBinary(Detail::Int2Type< DEPTH >{ },
217 item.getValue< ItemType >()));
221 ResourceAttributesBuilder() = default;
223 void insertItem(const OC::OCRepresentation::AttributeItem& item)
225 switch (item.depth())
228 return insertItem(Detail::Int2Type< 0 >{ }, item);
230 return insertItem(Detail::Int2Type< 1 >{ }, item);
232 return insertItem(Detail::Int2Type< 2 >{ }, item);
234 return insertItem(Detail::Int2Type< 3 >{ }, item);
237 assert("There must be no another depth!");
241 RCSResourceAttributes&& extract()
243 return std::move(m_target);
247 template< typename T >
248 void putValue(const std::string& key, T&& value)
250 m_target[key] = std::forward< T >(value);
254 RCSResourceAttributes m_target;
257 class OCRepresentationBuilder
260 OCRepresentationBuilder() = default;
262 template< typename T, typename B = typename Detail::TypeInfo< T >::base_type >
263 typename std::enable_if< (
264 !std::is_same< B, RCSResourceAttributes >::value &&
265 !std::is_same< B, RCSByteString >::value
267 operator()(const std::string& key, const T& value)
269 m_target[key] = value;
272 template< typename T, typename I = Detail::TypeInfo< T > >
273 typename std::enable_if< std::is_same< typename I::base_type,
274 RCSResourceAttributes >::value >::type
275 operator()(const std::string& key, const T& value)
277 m_target[key] = convertAttributes(Detail::Int2Type< I::depth >{ }, value);
280 template< typename T, typename I = Detail::TypeInfo< T > >
281 typename std::enable_if< std::is_same< typename I::base_type,
282 RCSByteString >::value >::type
283 operator()(const std::string& key, const T& value)
285 m_target[key] = convertByteString(Detail::Int2Type< I::depth >{ }, value);
288 void operator()(const std::string& key, const std::nullptr_t&)
290 m_target.setNULL(key);
293 OC::OCRepresentation convertAttributes(Detail::Int2Type< 0 >,
294 const RCSResourceAttributes& attrs)
296 return ResourceAttributesConverter::toOCRepresentation(attrs);
299 template< int DEPTH, typename ATTRS, typename OCREPS = typename Detail::SeqType<
300 DEPTH, OC::OCRepresentation >::type >
301 OCREPS convertAttributes(Detail::Int2Type< DEPTH >, const ATTRS& attrs)
305 for (const auto& nested : attrs)
308 convertAttributes(Detail::Int2Type< DEPTH - 1 >{ }, nested));
314 OCByteString convertByteString(Detail::Int2Type< 0 >,
315 const RCSByteString& byteString)
318 blob.len = byteString.size();
319 blob.bytes = new uint8_t[blob.len];
320 for (size_t i = 0; i < blob.len; ++i)
322 blob.bytes[i] = byteString[i];
328 template< int DEPTH, typename ATTRS, typename OCREPS = typename Detail::SeqType<
329 DEPTH, OCByteString >::type >
330 OCREPS convertByteString(Detail::Int2Type< DEPTH >, const ATTRS& byteStringVec)
334 for (const auto& nested : byteStringVec)
337 convertByteString(Detail::Int2Type< DEPTH - 1 >{ }, nested));
343 OC::OCRepresentation&& extract()
345 return std::move(m_target);
349 OC::OCRepresentation m_target;
353 static RCSResourceAttributes fromOCRepresentation(
354 const OC::OCRepresentation& ocRepresentation)
356 ResourceAttributesBuilder builder;
358 for (const auto& item : ocRepresentation)
360 builder.insertItem(item);
363 return builder.extract();
366 static OC::OCRepresentation toOCRepresentation(
367 const RCSResourceAttributes& resourceAttributes)
369 OCRepresentationBuilder builder;
371 resourceAttributes.visit(builder);
373 return builder.extract();
380 #endif // COMMON_RESOURCEATTRIBUTESCONVERTER_H