144628cf89cead692ee68539ee54607d08ee1227
[platform/upstream/iotivity.git] / resource / include / OCRepresentation.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 /// @file OCRepresentation.h
22
23 /// @brief  This file contains the declaration of classes and its members
24 ///         related to OCRepresentation
25
26 #ifndef __OCREPRESENTATION_H
27 #define __OCREPRESENTATION_H
28
29
30 #include <string>
31 #include <sstream>
32 #include <vector>
33 #include <map>
34
35 #include <AttributeValue.h>
36 #include <StringConstants.h>
37
38 #ifdef __ANDROID__
39 #include "OCAndroid.h"
40 #endif
41
42 #include <OCException.h>
43
44 namespace cereal
45 {
46     class access;
47 }
48
49 namespace OC
50 {
51
52     enum class InterfaceType
53     {
54         None,
55         LinkParent,
56         BatchParent,
57         DefaultParent,
58         LinkChild,
59         BatchChild,
60         DefaultChild
61     };
62
63     // The consumer requires resource info to be printed in 2 different ways, both with the "oc":[]
64     // and without.  This enum is used to differentiate between the two situations.  When the
65     // serialize is called with Include OC, we encode OC, otherwise we skip it and return just the
66     // contents of the array.
67     enum class OCInfoFormat
68     {
69         IncludeOC,
70         ExcludeOC
71     };
72
73     class MessageContainer
74     {
75         public:
76             void setJSONRepresentation(const std::string& payload);
77
78             void setJSONRepresentation(const unsigned char* payload);
79
80             std::string getJSONRepresentation(OCInfoFormat f) const;
81
82             const std::vector<OCRepresentation>& representations() const;
83
84             void addRepresentation(const OCRepresentation& rep);
85
86             const OCRepresentation& operator[](int index) const
87             {
88                 return m_reps[index];
89             }
90
91             const OCRepresentation& back() const
92             {
93                 return m_reps.back();
94             }
95         private:
96             std::vector<OCRepresentation> m_reps;
97     };
98     class OCRepresentation
99     {
100         public:
101             // Note: Implementation of all constructors and destructors
102             // are all placed in the same location due to a crash that
103             // was observed in Android, where merely constructing/destructing
104             // an OCRepresentation object was enough to cause an invalid 'free'.
105             // It is believed that this is a result of incompatible compiler
106             // options between the gradle JNI and armeabi scons build, however
107             // this fix will work in the meantime.
108             OCRepresentation(): m_interfaceType(InterfaceType::None){}
109
110             OCRepresentation(OCRepresentation&&) = default;
111
112             OCRepresentation(const OCRepresentation&) = default;
113
114             OCRepresentation& operator=(const OCRepresentation&) = default;
115
116             OCRepresentation& operator=(OCRepresentation&&) = default;
117
118             virtual ~OCRepresentation(){}
119
120             std::string getJSONRepresentation() const;
121
122             void addChild(const OCRepresentation&);
123
124             void clearChildren();
125
126             const std::vector<OCRepresentation>& getChildren() const;
127
128             void setChildren(const std::vector<OCRepresentation>& children);
129
130             void setUri(const std::string& uri);
131
132             std::string getUri() const;
133
134             const std::vector<std::string>& getResourceTypes() const;
135
136             void setResourceTypes(const std::vector<std::string>& resourceTypes);
137
138             const std::vector<std::string>& getResourceInterfaces() const;
139
140             void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
141
142             bool empty() const;
143
144             int numberOfAttributes() const;
145
146             bool erase(const std::string& str);
147
148             template <typename T>
149             void setValue(const std::string& str, const T& val)
150             {
151                 m_values[str] = val;
152             }
153
154             template <typename T>
155             bool getValue(const std::string& str, T& val) const
156             {
157                 auto x = m_values.find(str);
158
159                 if(x!= m_values.end())
160                 {
161                     val = boost::get<T>(x->second);
162                     return true;
163                 }
164                 else
165                 {
166                     val = T();
167                     return false;
168                 }
169             }
170
171             template <typename T>
172             T getValue(const std::string& str) const
173             {
174                 T val = T();
175                 auto x = m_values.find(str);
176                 if(x != m_values.end())
177                 {
178                     val = boost::get<T>(x->second);
179                 }
180                 return val;
181             }
182
183             bool hasAttribute(const std::string& str) const;
184
185             void setNULL(const std::string& str);
186
187             bool isNULL(const std::string& str) const;
188         private:
189             friend class OCResourceResponse;
190             friend class cereal::access;
191
192             // the root node has a slightly different JSON version
193             // based on the interface type configured in ResourceResponse.
194             // This allows ResourceResponse to set it, so that the save function
195             // doesn't serialize things that it isn't supposed to serialize.
196             void setInterfaceType(InterfaceType ift)
197             {
198                 m_interfaceType = ift;
199             }
200
201             // class used to wrap the 'prop' feature of the save/load
202             class Prop
203             {
204                 public:
205                     Prop(std::vector<std::string>& resourceTypes,
206                             std::vector<std::string>& interfaces)
207                     : m_types(resourceTypes), m_interfaces(interfaces)
208                     {}
209
210                  /*   Prop(const std::vector<std::string>& resourceTypes,
211                             const std::vector<std::string>& interfaces)
212                     :m_types(resourceTypes),
213                     m_interfaces(interfaces)
214                     {}*/
215                 private:
216                     friend class cereal::access;
217                     template <class Archive>
218                     void save(Archive& ar) const;
219
220                     template<class Archive>
221                     void load(Archive& ar);
222
223                     std::vector<std::string>& m_types;
224                     std::vector<std::string>& m_interfaces;
225             };
226             template<class Archive, class Val>
227             static void optional_load(Archive& ar, Val&& v);
228
229             template<class Archive>
230             void save(Archive& ar) const;
231
232             template<class Archive>
233             void load(Archive& ar);
234
235         private:
236             std::string m_uri;
237             std::vector<OCRepresentation> m_children;
238             std::map<std::string, AttributeValue> m_values;
239             std::vector<std::string> m_resourceTypes;
240             std::vector<std::string> m_interfaces;
241
242             InterfaceType m_interfaceType;
243     };
244 } // namespace OC
245
246
247 #endif //__OCREPRESENTATION_H