1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 /// @file OCRepresentation.h
23 /// @brief This file contains the declaration of classes and its members
24 /// related to OCRepresentation
26 #ifndef __OCREPRESENTATION_H
27 #define __OCREPRESENTATION_H
35 #include <AttributeValue.h>
36 #include <StringConstants.h>
38 #include <OCException.h>
41 #include "android_cpp11_compat.h"
52 enum class InterfaceType
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
73 class MessageContainer
76 void setJSONRepresentation(const std::string& payload);
78 void setJSONRepresentation(const unsigned char* payload);
80 std::string getJSONRepresentation(OCInfoFormat f) const;
82 const std::vector<OCRepresentation>& representations() const;
84 void addRepresentation(const OCRepresentation& rep);
86 const OCRepresentation& operator[](int index) const
91 const OCRepresentation& back() const
96 std::vector<OCRepresentation> m_reps;
98 class OCRepresentation
102 std::string getJSONRepresentation() const;
104 void addChild(const OCRepresentation&);
106 void clearChildren();
108 const std::vector<OCRepresentation>& getChildren() const;
110 void setChildren(const std::vector<OCRepresentation>& children);
112 void setUri(const std::string& uri);
114 std::string getUri() const;
116 const std::vector<std::string>& getResourceTypes() const;
118 void setResourceTypes(const std::vector<std::string>& resourceTypes);
120 const std::vector<std::string>& getResourceInterfaces() const;
122 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
126 int numberOfAttributes() const;
128 bool erase(const std::string& str);
130 template <typename T>
131 void setValue(const std::string& str, const T& val)
136 template <typename T>
137 bool getValue(const std::string& str, T& val) const
139 auto x = m_values.find(str);
141 if(x!= m_values.end())
143 val = boost::get<T>(x->second);
153 template <typename T>
154 T getValue(const std::string& str) const
157 auto x = m_values.find(str);
158 if(x != m_values.end())
160 val = boost::get<T>(x->second);
165 bool hasAttribute(const std::string& str) const;
167 void setNULL(const std::string& str);
169 bool isNULL(const std::string& str) const;
171 friend class OCResourceResponse;
172 friend class cereal::access;
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)
180 m_interfaceType = ift;
183 // class used to wrap the 'prop' feature of the save/load
187 Prop(std::vector<std::string>& resourceTypes,
188 std::vector<std::string>& interfaces)
189 : m_types(resourceTypes), m_interfaces(interfaces)
192 /* Prop(const std::vector<std::string>& resourceTypes,
193 const std::vector<std::string>& interfaces)
194 :m_types(resourceTypes),
195 m_interfaces(interfaces)
198 friend class cereal::access;
199 template <class Archive>
200 void save(Archive& ar) const;
202 template<class Archive>
203 void load(Archive& ar);
205 std::vector<std::string>& m_types;
206 std::vector<std::string>& m_interfaces;
208 template<class Archive, class Val>
209 static void optional_load(Archive& ar, Val&& v);
211 template<class Archive>
212 void save(Archive& ar) const;
214 template<class Archive>
215 void load(Archive& ar);
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;
224 InterfaceType m_interfaceType;
229 #endif //__OCREPRESENTATION_H