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>
49 enum class InterfaceType
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
70 class MessageContainer
73 void setJSONRepresentation(const std::string& payload);
75 void setJSONRepresentation(const unsigned char* payload);
77 std::string getJSONRepresentation(OCInfoFormat f) const;
79 const std::vector<OCRepresentation>& representations() const;
81 void addRepresentation(const OCRepresentation& rep);
83 const OCRepresentation& operator[](int index) const
88 const OCRepresentation& back() const
93 std::vector<OCRepresentation> m_reps;
95 class OCRepresentation
99 std::string getJSONRepresentation() const;
101 void addChild(const OCRepresentation&);
103 void clearChildren();
105 const std::vector<OCRepresentation>& getChildren() const;
107 void setChildren(const std::vector<OCRepresentation>& children);
109 void setUri(const std::string& uri);
111 std::string getUri() const;
113 const std::vector<std::string>& getResourceTypes() const;
115 void setResourceTypes(const std::vector<std::string>& resourceTypes);
117 const std::vector<std::string>& getResourceInterfaces() const;
119 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
123 int numberOfAttributes() const;
125 bool erase(const std::string& str);
127 template <typename T>
128 void setValue(const std::string& str, const T& val)
133 template <typename T>
134 bool getValue(const std::string& str, T& val) const
136 auto x = m_values.find(str);
138 if(x!= m_values.end())
140 val = boost::get<T>(x->second);
150 template <typename T>
151 T getValue(const std::string& str) const
154 auto x = m_values.find(str);
155 if(x != m_values.end())
157 val = boost::get<T>(x->second);
162 bool hasAttribute(const std::string& str) const;
164 void setNULL(const std::string& str);
166 bool isNULL(const std::string& str) const;
168 friend class OCResourceResponse;
169 friend class cereal::access;
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)
177 m_interfaceType = ift;
180 // class used to wrap the 'prop' feature of the save/load
184 Prop(std::vector<std::string>& resourceTypes,
185 std::vector<std::string>& interfaces)
186 : m_types(resourceTypes), m_interfaces(interfaces)
189 /* Prop(const std::vector<std::string>& resourceTypes,
190 const std::vector<std::string>& interfaces)
191 :m_types(resourceTypes),
192 m_interfaces(interfaces)
195 friend class cereal::access;
196 template <class Archive>
197 void save(Archive& ar) const;
199 template<class Archive>
200 void load(Archive& ar);
202 std::vector<std::string>& m_types;
203 std::vector<std::string>& m_interfaces;
205 template<class Archive, class Val>
206 static void optional_load(Archive& ar, Val&& v);
208 template<class Archive>
209 void save(Archive& ar) const;
211 template<class Archive>
212 void load(Archive& ar);
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;
221 InterfaceType m_interfaceType;
226 #endif //__OCREPRESENTATION_H