Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / common / primitiveResource / include / ResourceAttributesConverter.h
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef COMMON_INTERNAL_RESOURCEATTRIBUTESCONVERTER_H
22 #define COMMON_INTERNAL_RESOURCEATTRIBUTESCONVERTER_H
23
24 #include <RCSResourceAttributes.h>
25
26 #include <OCRepresentation.h>
27
28 namespace OIC
29 {
30     namespace Service
31     {
32
33         class ResourceAttributesConverter
34         {
35         private:
36             ResourceAttributesConverter() = delete;
37
38             class ResourceAttributesBuilder
39             {
40             public:
41                 ResourceAttributesBuilder() = default;
42
43                 void insertItemTo(const OC::OCRepresentation::AttributeItem& item)
44                 {
45                     switch (item.type())
46                     {
47                         case OC::AttributeType::Null:
48                             return putValue(item.attrname(), nullptr);
49
50                         case OC::AttributeType::Integer:
51                             return putValue(item.attrname(), item.getValue< int >());
52
53                         case OC::AttributeType::Double:
54                             return putValue(item.attrname(), item.getValue< double >());
55
56                         case OC::AttributeType::Boolean:
57                             return putValue(item.attrname(), item.getValue< bool >());
58
59                         case OC::AttributeType::String:
60                             return putValue(item.attrname(), item.getValue< std::string >());
61
62                         case OC::AttributeType::OCRepresentation:
63                             return putValue(item.attrname(),
64                                     ResourceAttributesConverter::fromOCRepresentation(
65                                             item.getValue< OC::OCRepresentation >()));
66
67                         case OC::AttributeType::Vector:
68                             // RCSResourceAttributes doesn't support vector yet!
69                             return;
70                     }
71                 }
72
73                 RCSResourceAttributes&& extract()
74                 {
75                     return std::move(m_target);
76                 }
77
78             private:
79                 template< typename T >
80                 typename std::enable_if<RCSResourceAttributes::is_supported_type< T >::value >::type
81                 putValue(const std::string& key, T && value)
82                 {
83                     m_target[key] = std::forward< T >(value);
84                 }
85
86                 template< typename T >
87                 typename std::enable_if<!RCSResourceAttributes::is_supported_type< T >::value >::type
88                 putValue(const std::string& key, T && value)
89                 {
90                 }
91
92             private:
93                 RCSResourceAttributes m_target;
94             };
95
96             class OCRepresentationBuilder
97             {
98             public:
99                 OCRepresentationBuilder() = default;
100
101                 template< typename T >
102                 void operator()(const std::string& key, const T& value)
103                 {
104                     m_target[key] = value;
105                 }
106
107                 void operator()(const std::string& key, const std::nullptr_t&)
108                 {
109                     m_target.setNULL(key);
110                 }
111
112                 void operator()(const std::string& key, const RCSResourceAttributes& value)
113                 {
114                     m_target[key] = ResourceAttributesConverter::toOCRepresentation(value);
115                 }
116
117                 OC::OCRepresentation&& extract()
118                 {
119                     return std::move(m_target);
120                 }
121
122             private:
123                 OC::OCRepresentation m_target;
124             };
125
126         public:
127             static RCSResourceAttributes fromOCRepresentation(
128                     const OC::OCRepresentation& ocRepresentation)
129             {
130                 ResourceAttributesBuilder builder;
131
132                 for (const auto& item : ocRepresentation)
133                 {
134                     builder.insertItemTo(item);
135                 }
136
137                 return builder.extract();
138             }
139
140             static OC::OCRepresentation toOCRepresentation(
141                     const RCSResourceAttributes& resourceAttributes)
142             {
143                 OCRepresentationBuilder builder;
144
145                 resourceAttributes.visit(builder);
146
147                 return builder.extract();
148             }
149         };
150
151     }
152 }
153
154 #endif // COMMON_INTERNAL_RESOURCEATTRIBUTESCONVERTER_H