Modifying version number for building on tizen 3.0
[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             OCRepresentation();
102             std::string getJSONRepresentation() const;
103
104             void addChild(const OCRepresentation&);
105
106             void clearChildren();
107
108             const std::vector<OCRepresentation>& getChildren() const;
109
110             void setChildren(const std::vector<OCRepresentation>& children);
111
112             void setUri(const std::string& uri);
113
114             std::string getUri() const;
115
116             const std::vector<std::string>& getResourceTypes() const;
117
118             void setResourceTypes(const std::vector<std::string>& resourceTypes);
119
120             const std::vector<std::string>& getResourceInterfaces() const;
121
122             void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
123
124             bool empty() const;
125
126             int numberOfAttributes() const;
127
128             bool erase(const std::string& str);
129
130             template <typename T>
131             void setValue(const std::string& str, const T& val)
132             {
133                 m_values[str] = val;
134             }
135
136             template <typename T>
137             bool getValue(const std::string& str, T& val) const
138             {
139                 auto x = m_values.find(str);
140
141                 if(x!= m_values.end())
142                 {
143                     val = boost::get<T>(x->second);
144                     return true;
145                 }
146                 else
147                 {
148                     val = T();
149                     return false;
150                 }
151             }
152
153             template <typename T>
154             T getValue(const std::string& str) const
155             {
156                 T val = T();
157                 auto x = m_values.find(str);
158                 if(x != m_values.end())
159                 {
160                     val = boost::get<T>(x->second);
161                 }
162                 return val;
163             }
164
165             bool hasAttribute(const std::string& str) const;
166
167             void setNULL(const std::string& str);
168
169             bool isNULL(const std::string& str) const;
170         private:
171             friend class OCResourceResponse;
172             friend class cereal::access;
173
174             // the root node has a slightly different JSON version
175             // based on the interface type configured in ResourceResponse.
176             // This allows ResourceResponse to set it, so that the save function
177             // doesn't serialize things that it isn't supposed to serialize.
178             void setInterfaceType(InterfaceType ift)
179             {
180                 m_interfaceType = ift;
181             }
182
183             // class used to wrap the 'prop' feature of the save/load
184             class Prop
185             {
186                 public:
187                     Prop(std::vector<std::string>& resourceTypes,
188                             std::vector<std::string>& interfaces)
189                     : m_types(resourceTypes), m_interfaces(interfaces)
190                     {}
191
192                  /*   Prop(const std::vector<std::string>& resourceTypes,
193                             const std::vector<std::string>& interfaces)
194                     :m_types(resourceTypes),
195                     m_interfaces(interfaces)
196                     {}*/
197                 private:
198                     friend class cereal::access;
199                     template <class Archive>
200                     void save(Archive& ar) const;
201
202                     template<class Archive>
203                     void load(Archive& ar);
204
205                     std::vector<std::string>& m_types;
206                     std::vector<std::string>& m_interfaces;
207             };
208             template<class Archive, class Val>
209             static void optional_load(Archive& ar, Val&& v);
210
211             template<class Archive>
212             void save(Archive& ar) const;
213
214             template<class Archive>
215             void load(Archive& ar);
216
217         private:
218             std::string m_uri;
219             std::vector<OCRepresentation> m_children;
220             std::map<std::string, AttributeValue> m_values;
221             std::vector<std::string> m_resourceTypes;
222             std::vector<std::string> m_interfaces;
223
224             InterfaceType m_interfaceType;
225     };
226 } // namespace OC
227
228
229 #endif //__OCREPRESENTATION_H