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::OCRepresentation >
60 : TypeDef< OC::OCRepresentation >
63 template< int DEPTH, typename BASE_TYPE >
66 typedef std::vector< typename SeqType< DEPTH - 1, BASE_TYPE >::type > type;
69 template< typename BASE_TYPE >
70 struct SeqType< 0, BASE_TYPE >
72 typedef BASE_TYPE type;
75 template< int DEPTH, OC::AttributeType BASE_TYPE >
78 typedef typename SeqType< DEPTH,
79 typename OCBaseType< BASE_TYPE >::type >::type type;
82 template< typename T >
87 constexpr static size_t depth = 0;
90 template< typename T >
91 struct TypeInfo< std::vector< T > >
94 typedef typename TypeInfo< T >::base_type base_type;
95 constexpr static size_t depth = 1 + TypeInfo< T >::depth;
99 class ResourceAttributesConverter
102 ResourceAttributesConverter() = delete;
104 class ResourceAttributesBuilder
107 template < int DEPTH >
108 void insertItem(Detail::Int2Type< DEPTH >,
109 const OC::OCRepresentation::AttributeItem& item)
111 switch (item.base_type()) {
112 case OC::AttributeType::Null:
113 return putValue(item.attrname(), nullptr);
115 case OC::AttributeType::Integer:
116 return insertItem< DEPTH, OC::AttributeType::Integer >(item);
118 case OC::AttributeType::Double:
119 return insertItem< DEPTH, OC::AttributeType::Double >(item);
121 case OC::AttributeType::Boolean:
122 return insertItem< DEPTH, OC::AttributeType::Boolean >(item);
124 case OC::AttributeType::String:
125 return insertItem< DEPTH, OC::AttributeType::String >(item);
127 case OC::AttributeType::OCRepresentation:
128 return insertOcRep(Detail::Int2Type< DEPTH >{ }, item);
131 assert("There must be no another base type!");
135 template< int DEPTH, OC::AttributeType BASE_TYPE >
136 void insertItem(const OC::OCRepresentation::AttributeItem& item)
138 typedef typename Detail::OCItemType< DEPTH, BASE_TYPE >::type ItemType;
139 putValue(item.attrname(), item.getValue< ItemType >());
142 RCSResourceAttributes insertOcRep(Detail::Int2Type< 0 >,
143 const OC::OCRepresentation& ocRep)
145 return ResourceAttributesConverter::fromOCRepresentation(ocRep);
148 template< int DEPTH, typename OCREPS,
149 typename ATTRS = typename Detail::SeqType< DEPTH, RCSResourceAttributes >::type >
150 ATTRS insertOcRep(Detail::Int2Type< DEPTH >, const OCREPS& ocRepVec)
154 for (const auto& nested : ocRepVec)
156 result.push_back(insertOcRep(Detail::Int2Type< DEPTH - 1 >{ }, nested));
162 template< int DEPTH >
163 void insertOcRep(Detail::Int2Type< DEPTH >,
164 const OC::OCRepresentation::AttributeItem& item)
166 typedef typename Detail::OCItemType< DEPTH,
167 OC::AttributeType::OCRepresentation >::type ItemType;
169 putValue(item.attrname(),
170 insertOcRep(Detail::Int2Type< DEPTH >{ }, item.getValue< ItemType >()));
174 ResourceAttributesBuilder() = default;
176 void insertItem(const OC::OCRepresentation::AttributeItem& item)
178 switch (item.depth())
181 return insertItem(Detail::Int2Type< 0 >{ }, item);
183 return insertItem(Detail::Int2Type< 1 >{ }, item);
185 return insertItem(Detail::Int2Type< 2 >{ }, item);
187 return insertItem(Detail::Int2Type< 3 >{ }, item);
190 assert("There must be no another depth!");
194 RCSResourceAttributes&& extract()
196 return std::move(m_target);
200 template< typename T >
201 void putValue(const std::string& key, T&& value)
203 m_target[key] = std::forward< T >(value);
207 RCSResourceAttributes m_target;
210 class OCRepresentationBuilder
213 OCRepresentationBuilder() = default;
215 template< typename T, typename B = typename Detail::TypeInfo< T >::base_type >
216 typename std::enable_if< !std::is_same< B, RCSResourceAttributes >::value >::type
217 operator()(const std::string& key, const T& value)
219 m_target[key] = value;
222 template< typename T, typename I = Detail::TypeInfo< T > >
223 typename std::enable_if< std::is_same< typename I::base_type,
224 RCSResourceAttributes >::value >::type
225 operator()(const std::string& key, const T& value)
227 m_target[key] = convertAttributes(Detail::Int2Type< I::depth >{ }, value);
230 void operator()(const std::string& key, const std::nullptr_t&)
232 m_target.setNULL(key);
235 OC::OCRepresentation convertAttributes(Detail::Int2Type< 0 >,
236 const RCSResourceAttributes& attrs)
238 return ResourceAttributesConverter::toOCRepresentation(attrs);
241 template< int DEPTH, typename ATTRS, typename OCREPS = typename Detail::SeqType<
242 DEPTH, OC::OCRepresentation >::type >
243 OCREPS convertAttributes(Detail::Int2Type< DEPTH >, const ATTRS& attrs)
247 for (const auto& nested : attrs)
250 convertAttributes(Detail::Int2Type< DEPTH - 1 >{ }, nested));
256 OC::OCRepresentation&& extract()
258 return std::move(m_target);
262 OC::OCRepresentation m_target;
266 static RCSResourceAttributes fromOCRepresentation(
267 const OC::OCRepresentation& ocRepresentation)
269 ResourceAttributesBuilder builder;
271 for (const auto& item : ocRepresentation)
273 builder.insertItem(item);
276 return builder.extract();
279 static OC::OCRepresentation toOCRepresentation(
280 const RCSResourceAttributes& resourceAttributes)
282 OCRepresentationBuilder builder;
284 resourceAttributes.visit(builder);
286 return builder.extract();
293 #endif // COMMON_RESOURCEATTRIBUTESCONVERTER_H