Merge "C++ SDK Adding OCConnectivity Type" into connectivity-abstraction
[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 #include <OCException.h>
39
40
41 namespace cereal
42 {
43     class access;
44 }
45
46 namespace OC
47 {
48
49     enum class InterfaceType
50     {
51         None,
52         LinkParent,
53         BatchParent,
54         DefaultParent,
55         LinkChild,
56         BatchChild,
57         DefaultChild
58     };
59
60     // The consumer requires resource info to be printed in 2 different ways, both with the "oc":[]
61     // and without.  This enum is used to differentiate between the two situations.  When the
62     // serialize is called with Include OC, we encode OC, otherwise we skip it and return just the
63     // contents of the array.
64     enum class OCInfoFormat
65     {
66         IncludeOC,
67         ExcludeOC
68     };
69
70     class MessageContainer
71     {
72         public:
73             void setJSONRepresentation(const std::string& payload);
74
75             void setJSONRepresentation(const unsigned char* payload);
76
77             std::string getJSONRepresentation(OCInfoFormat f) const;
78
79             const std::vector<OCRepresentation>& representations() const;
80
81             void addRepresentation(const OCRepresentation& rep);
82
83             const OCRepresentation& operator[](int index) const
84             {
85                 return m_reps[index];
86             }
87
88             const OCRepresentation& back() const
89             {
90                 return m_reps.back();
91             }
92         private:
93             std::vector<OCRepresentation> m_reps;
94     };
95     class OCRepresentation
96     {
97         public:
98             OCRepresentation();
99             std::string getJSONRepresentation() const;
100
101             void addChild(const OCRepresentation&);
102
103             void clearChildren();
104
105             const std::vector<OCRepresentation>& getChildren() const;
106
107             void setChildren(const std::vector<OCRepresentation>& children);
108
109             void setUri(const std::string& uri);
110
111             std::string getUri() const;
112
113             const std::vector<std::string>& getResourceTypes() const;
114
115             void setResourceTypes(const std::vector<std::string>& resourceTypes);
116
117             const std::vector<std::string>& getResourceInterfaces() const;
118
119             void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
120
121             bool empty() const;
122
123             int numberOfAttributes() const;
124
125             bool erase(const std::string& str);
126
127             template <typename T>
128             void setValue(const std::string& str, const T& val)
129             {
130                 m_values[str] = val;
131             }
132
133             template <typename T>
134             bool getValue(const std::string& str, T& val) const
135             {
136                 auto x = m_values.find(str);
137
138                 if(x!= m_values.end())
139                 {
140                     val = boost::get<T>(x->second);
141                     return true;
142                 }
143                 else
144                 {
145                     val = T();
146                     return false;
147                 }
148             }
149
150             template <typename T>
151             T getValue(const std::string& str) const
152             {
153                 T val = T();
154                 auto x = m_values.find(str);
155                 if(x != m_values.end())
156                 {
157                     val = boost::get<T>(x->second);
158                 }
159                 return val;
160             }
161
162             bool hasAttribute(const std::string& str) const;
163
164             void setNULL(const std::string& str);
165
166             bool isNULL(const std::string& str) const;
167         private:
168             friend class OCResourceResponse;
169             friend class cereal::access;
170
171             // the root node has a slightly different JSON version
172             // based on the interface type configured in ResourceResponse.
173             // This allows ResourceResponse to set it, so that the save function
174             // doesn't serialize things that it isn't supposed to serialize.
175             void setInterfaceType(InterfaceType ift)
176             {
177                 m_interfaceType = ift;
178             }
179
180             // class used to wrap the 'prop' feature of the save/load
181             class Prop
182             {
183                 public:
184                     Prop(std::vector<std::string>& resourceTypes,
185                             std::vector<std::string>& interfaces)
186                     : m_types(resourceTypes), m_interfaces(interfaces)
187                     {}
188
189                  /*   Prop(const std::vector<std::string>& resourceTypes,
190                             const std::vector<std::string>& interfaces)
191                     :m_types(resourceTypes),
192                     m_interfaces(interfaces)
193                     {}*/
194                 private:
195                     friend class cereal::access;
196                     template <class Archive>
197                     void save(Archive& ar) const;
198
199                     template<class Archive>
200                     void load(Archive& ar);
201
202                     std::vector<std::string>& m_types;
203                     std::vector<std::string>& m_interfaces;
204             };
205             template<class Archive, class Val>
206             static void optional_load(Archive& ar, Val&& v);
207
208             template<class Archive>
209             void save(Archive& ar) const;
210
211             template<class Archive>
212             void load(Archive& ar);
213
214         private:
215             std::string m_uri;
216             std::vector<OCRepresentation> m_children;
217             std::map<std::string, AttributeValue> m_values;
218             std::vector<std::string> m_resourceTypes;
219             std::vector<std::string> m_interfaces;
220
221             InterfaceType m_interfaceType;
222     };
223 } // namespace OC
224
225
226 #endif //__OCREPRESENTATION_H