/**
* @file
*
- * This file contains the declaration of classes and its members related
+ * This file contains the declaration of classes and its members related
* to OCRepresentation.
*/
-#ifndef __OCREPRESENTATION_H
-#define __OCREPRESENTATION_H
-
+#ifndef OC_REPRESENTATION_H_
+#define OC_REPRESENTATION_H_
#include <string>
#include <sstream>
#include <AttributeValue.h>
#include <StringConstants.h>
-#include <OCException.h>
-
#ifdef __ANDROID__
-#include "android_cpp11_compat.h"
+#include "OCAndroid.h"
#endif
-namespace cereal
-{
- class access;
-}
+#include <OCException.h>
namespace OC
{
DefaultChild
};
- // The consumer requires resource info to be printed in 2 different ways, both with the "oc":[]
- // and without. This enum is used to differentiate between the two situations. When the
- // serialize is called with Include OC, we encode OC, otherwise we skip it and return just the
- // contents of the array.
- enum class OCInfoFormat
- {
- IncludeOC,
- ExcludeOC
- };
-
class MessageContainer
{
public:
- void setJSONRepresentation(const std::string& payload);
+ void setPayload(const OCPayload* rep);
- void setJSONRepresentation(const unsigned char* payload);
+ void setPayload(const OCRepPayload* rep);
- std::string getJSONRepresentation(OCInfoFormat f) const;
+ OCRepPayload* getPayload() const;
const std::vector<OCRepresentation>& representations() const;
class OCRepresentation
{
public:
+ friend bool operator==(const OC::OCRepresentation&, const OC::OCRepresentation&);
+ // Note: Implementation of all constructors and destructors
+ // are all placed in the same location due to a crash that
+ // was observed in Android, where merely constructing/destructing
+ // an OCRepresentation object was enough to cause an invalid 'free'.
+ // It is believed that this is a result of incompatible compiler
+ // options between the gradle JNI and armeabi scons build, however
+ // this fix will work in the meantime.
OCRepresentation(): m_interfaceType(InterfaceType::None){}
+#if defined(_MSC_VER) && (_MSC_VER < 1900)
+ OCRepresentation(OCRepresentation&& o)
+ {
+ std::memmove(this, &o, sizeof(o));
+ }
+#else
OCRepresentation(OCRepresentation&&) = default;
+#endif
OCRepresentation(const OCRepresentation&) = default;
OCRepresentation& operator=(const OCRepresentation&) = default;
+#if defined(_MSC_VER) && (_MSC_VER < 1900)
+ OCRepresentation& operator=(OCRepresentation&& o)
+ {
+ std::memmove(this, &o, sizeof(o));
+ return *this;
+ }
+#else
OCRepresentation& operator=(OCRepresentation&&) = default;
+#endif
virtual ~OCRepresentation(){}
- std::string getJSONRepresentation() const;
+ void setDevAddr(const OCDevAddr&);
+
+ const std::string getHost() const;
+
+ OCRepPayload* getPayload() const;
void addChild(const OCRepresentation&);
void setChildren(const std::vector<OCRepresentation>& children);
+ void setUri(const char* uri);
+
void setUri(const std::string& uri);
std::string getUri() const;
const std::vector<std::string>& getResourceTypes() const;
+ const std::vector<std::string>& getDataModelVersions() const;
+
void setResourceTypes(const std::vector<std::string>& resourceTypes);
+ void addResourceType(const std::string& str);
+
const std::vector<std::string>& getResourceInterfaces() const;
void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
+ void addResourceInterface(const std::string& str);
+
+ void addDataModelVersion(const std::string& str);
+
bool emptyData() const;
int numberOfAttributes() const;
m_values[str] = val;
}
+ // using R-value(or universal ref depending) to move string and vector<uint8_t>
+ template <typename T>
+ void setValue(const std::string& str, T&& val)
+ {
+ m_values[str] = std::forward<T>(val);
+ }
+
+ const std::map<std::string, AttributeValue>& getValues() const {
+ return m_values;
+ }
+
+ /**
+ * Retrieve the attribute value associated with the supplied name
+ *
+ * @param str Name of the attribute
+ * @param val Value of the attribute
+ * @return The getValue method returns true if the attribute was
+ * found in the representation. Otherwise it returns false.
+ */
template <typename T>
bool getValue(const std::string& str, T& val) const
{
if(x!= m_values.end())
{
- val = boost::get<T>(x->second);
- return true;
+ try
+ {
+ if (x->second.type() == typeid(T))
+ {
+ val = boost::get<T>(x->second);
+ return true;
+ }
+ else
+ {
+ val = T();
+ return false;
+ }
+ }
+ catch (boost::bad_get& e)
+ {
+ val = T();
+ return false;
+ }
}
else
{
}
}
+ /**
+ * Return the attribute value associated with the supplied name
+ *
+ * @param str Name of the attribute
+ * @return When the representation contains the attribute, the
+ * the associated value is returned. Otherwise, getValue
+ * returns the default contructed value for the type.
+ */
template <typename T>
T getValue(const std::string& str) const
{
auto x = m_values.find(str);
if(x != m_values.end())
{
- val = boost::get<T>(x->second);
+ try
+ {
+ if (x->second.type() == typeid(T))
+ {
+ val = boost::get<T>(x->second);
+ }
+ else
+ {
+ return val;
+ }
+ }
+ catch (boost::bad_get& e)
+ {
+ return val;
+ }
}
return val;
}
+ /**
+ * Retrieve the attributevalue structure associated with the supplied name
+ *
+ * @param str Name of the attribute
+ * @param attrValue Attribute Value structure
+ * @return The getAttributeValue method returns true if the attribute was
+ * found in the representation. Otherwise it returns false.
+ */
+ bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
+ {
+ auto x = m_values.find(str);
+
+ if (x != m_values.end())
+ {
+ attrValue = x->second;
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
std::string getValueToString(const std::string& key) const;
bool hasAttribute(const std::string& str) const;
bool isNULL(const std::string& str) const;
+ private:
+ std::string m_host;
+
// STL Container stuff
public:
class iterator;
template<typename T>
T getValue() const
{
- return boost::get<T>(m_values[m_attrName]);
+ T val = T();
+ try
+ {
+ if (m_values[m_attrName].type() == typeid(T))
+ {
+ val = boost::get<T>(m_values[m_attrName]);
+ }
+ else
+ {
+ return val;
+ }
+ }
+ catch (boost::bad_get& e)
+ {
+ return val;
+ }
+ return val;
}
std::string getValueToString() const;
return *this;
}
- AttributeItem& operator=(std::nullptr_t rhs)
+ AttributeItem& operator=(std::nullptr_t /*rhs*/)
{
NullType t;
m_values[m_attrName] = t;
// Enable-if required to prevent conversions to alternate types. This prevents
// ambigious conversions in the case where conversions can include a number of
// types, such as the string constructor.
- template<typename T, typename= typename std::enable_if<
+#if (defined(_MSC_VER) ) || (defined(__GNUC__) && (__GNUC__ <= 5))
+ template<typename T, typename std::enable_if<
std::is_same<T, int>::value ||
std::is_same<T, double>::value ||
std::is_same<T, bool>::value ||
std::is_same<T, std::string>::value ||
std::is_same<T, OCRepresentation>::value ||
+ std::is_same<T, OCByteString>::value ||
std::is_same<T, std::vector<int>>::value ||
std::is_same<T, std::vector<std::vector<int>>>::value ||
std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
std::is_same<T, std::vector<OCRepresentation>>::value ||
std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
- std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
- >::type // enable_if
+ std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value ||
+ std::is_same<T, std::vector<OCByteString>>::value ||
+ std::is_same<T, std::vector<std::vector<OCByteString>>>::value ||
+ std::is_same<T, std::vector<std::vector<std::vector<OCByteString>>>>::value
+ , int>::type = 0// enable_if
+ >
+#else
+ template<typename T, typename std::enable_if<
+ is_component<T,
+ remove_first<AttributeValue>::type
+ >::value
+ , int>::type = 0
>
+#endif
operator T() const
{
return this->getValue<T>();
}
- operator std::nullptr_t() const
+ template<typename T, typename std::enable_if<
+ std::is_same<T, std::nullptr_t>::value
+ , int>::type = 0
+ >
+ operator T() const
{
this->getValue<NullType>();
return nullptr;
const AttributeItem operator[](const std::string& key) const;
private:
friend class OCResourceResponse;
- friend class cereal::access;
-
+ friend class MessageContainer;
+
+ template<typename T>
+ void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
+ template<typename T>
+ T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
+ void setPayload(const OCRepPayload* payload);
+ void setPayloadArray(const OCRepPayloadValue* pl);
+ void getPayloadArray(OCRepPayload* payload,
+ const OCRepresentation::AttributeItem& item) const;
// the root node has a slightly different JSON version
// based on the interface type configured in ResourceResponse.
// This allows ResourceResponse to set it, so that the save function
m_interfaces(interfaces)
{}*/
private:
- friend class cereal::access;
- template <class Archive>
- void save(Archive& ar) const;
-
- template<class Archive>
- void load(Archive& ar);
-
std::vector<std::string>& m_types;
std::vector<std::string>& m_interfaces;
};
- template<class Archive, class Val>
- static void optional_load(Archive& ar, Val&& v);
-
- template<class Archive>
- void save(Archive& ar) const;
-
- template<class Archive>
- void load(Archive& ar);
-
private:
std::string m_uri;
std::vector<OCRepresentation> m_children;
mutable std::map<std::string, AttributeValue> m_values;
std::vector<std::string> m_resourceTypes;
std::vector<std::string> m_interfaces;
+ std::vector<std::string> m_dataModelVersions;
InterfaceType m_interfaceType;
};
} // namespace OC
-#endif //__OCREPRESENTATION_H
+#endif // OC_REPRESENTATION_H_