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_INTERNAL_RESOURCEATTRIBUTESCONVERTER_H
22 #define COMMON_INTERNAL_RESOURCEATTRIBUTESCONVERTER_H
24 #include <RCSResourceAttributes.h>
26 #include <OCRepresentation.h>
33 class ResourceAttributesConverter
36 ResourceAttributesConverter() = delete;
38 class ResourceAttributesBuilder
41 ResourceAttributesBuilder() = default;
43 void insertItemTo(const OC::OCRepresentation::AttributeItem& item)
47 case OC::AttributeType::Null:
48 return putValue(item.attrname(), nullptr);
50 case OC::AttributeType::Integer:
51 return putValue(item.attrname(), item.getValue< int >());
53 case OC::AttributeType::Double:
54 return putValue(item.attrname(), item.getValue< double >());
56 case OC::AttributeType::Boolean:
57 return putValue(item.attrname(), item.getValue< bool >());
59 case OC::AttributeType::String:
60 return putValue(item.attrname(), item.getValue< std::string >());
62 case OC::AttributeType::OCRepresentation:
63 return putValue(item.attrname(),
64 ResourceAttributesConverter::fromOCRepresentation(
65 item.getValue< OC::OCRepresentation >()));
67 case OC::AttributeType::Vector:
68 // RCSResourceAttributes doesn't support vector yet!
73 RCSResourceAttributes&& extract()
75 return std::move(m_target);
79 template< typename T >
80 typename std::enable_if<RCSResourceAttributes::is_supported_type< T >::value >::type
81 putValue(const std::string& key, T && value)
83 m_target[key] = std::forward< T >(value);
86 template< typename T >
87 typename std::enable_if<!RCSResourceAttributes::is_supported_type< T >::value >::type
88 putValue(const std::string& key, T && value)
93 RCSResourceAttributes m_target;
96 class OCRepresentationBuilder
99 OCRepresentationBuilder() = default;
101 template< typename T >
102 void operator()(const std::string& key, const T& value)
104 m_target[key] = value;
107 void operator()(const std::string& key, const std::nullptr_t&)
109 m_target.setNULL(key);
112 void operator()(const std::string& key, const RCSResourceAttributes& value)
114 m_target[key] = ResourceAttributesConverter::toOCRepresentation(value);
117 OC::OCRepresentation&& extract()
119 return std::move(m_target);
123 OC::OCRepresentation m_target;
127 static RCSResourceAttributes fromOCRepresentation(
128 const OC::OCRepresentation& ocRepresentation)
130 ResourceAttributesBuilder builder;
132 for (const auto& item : ocRepresentation)
134 builder.insertItemTo(item);
137 return builder.extract();
140 static OC::OCRepresentation toOCRepresentation(
141 const RCSResourceAttributes& resourceAttributes)
143 OCRepresentationBuilder builder;
145 resourceAttributes.visit(builder);
147 return builder.extract();
154 #endif // COMMON_INTERNAL_RESOURCEATTRIBUTESCONVERTER_H