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>
39 #include "OCAndroid.h"
42 #include <OCException.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
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){}
110 OCRepresentation(OCRepresentation&&) = default;
112 OCRepresentation(const OCRepresentation&) = default;
114 OCRepresentation& operator=(const OCRepresentation&) = default;
116 OCRepresentation& operator=(OCRepresentation&&) = default;
118 virtual ~OCRepresentation(){}
120 std::string getJSONRepresentation() const;
122 void addChild(const OCRepresentation&);
124 void clearChildren();
126 const std::vector<OCRepresentation>& getChildren() const;
128 void setChildren(const std::vector<OCRepresentation>& children);
130 void setUri(const std::string& uri);
132 std::string getUri() const;
134 const std::vector<std::string>& getResourceTypes() const;
136 void setResourceTypes(const std::vector<std::string>& resourceTypes);
138 const std::vector<std::string>& getResourceInterfaces() const;
140 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
144 int numberOfAttributes() const;
146 bool erase(const std::string& str);
148 template <typename T>
149 void setValue(const std::string& str, const T& val)
154 template <typename T>
155 bool getValue(const std::string& str, T& val) const
157 auto x = m_values.find(str);
159 if(x!= m_values.end())
161 val = boost::get<T>(x->second);
171 template <typename T>
172 T getValue(const std::string& str) const
175 auto x = m_values.find(str);
176 if(x != m_values.end())
178 val = boost::get<T>(x->second);
183 bool hasAttribute(const std::string& str) const;
185 void setNULL(const std::string& str);
187 bool isNULL(const std::string& str) const;
189 friend class OCResourceResponse;
190 friend class cereal::access;
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)
198 m_interfaceType = ift;
201 // class used to wrap the 'prop' feature of the save/load
205 Prop(std::vector<std::string>& resourceTypes,
206 std::vector<std::string>& interfaces)
207 : m_types(resourceTypes), m_interfaces(interfaces)
210 /* Prop(const std::vector<std::string>& resourceTypes,
211 const std::vector<std::string>& interfaces)
212 :m_types(resourceTypes),
213 m_interfaces(interfaces)
216 friend class cereal::access;
217 template <class Archive>
218 void save(Archive& ar) const;
220 template<class Archive>
221 void load(Archive& ar);
223 std::vector<std::string>& m_types;
224 std::vector<std::string>& m_interfaces;
226 template<class Archive, class Val>
227 static void optional_load(Archive& ar, Val&& v);
229 template<class Archive>
230 void save(Archive& ar) const;
232 template<class Archive>
233 void load(Archive& ar);
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;
242 InterfaceType m_interfaceType;
247 #endif //__OCREPRESENTATION_H