Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / resource / include / OCRepresentation.h
index 534bb5a..e7bbccb 100644 (file)
 
 #include <OCException.h>
 
-namespace cereal
-{
-    class access;
-}
-
 namespace OC
 {
 
@@ -62,24 +57,18 @@ 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 setPayload(const OCDevicePayload* rep);
 
-            void setJSONRepresentation(const char* payload);
+            void setPayload(const OCPlatformPayload* rep);
 
-            std::string getJSONRepresentation(OCInfoFormat f) const;
+            void setPayload(const OCRepPayload* rep);
+
+            OCRepPayload* getPayload() const;
 
             const std::vector<OCRepresentation>& representations() const;
 
@@ -100,6 +89,7 @@ namespace OC
     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
@@ -119,7 +109,11 @@ namespace OC
 
             virtual ~OCRepresentation(){}
 
-            std::string getJSONRepresentation() const;
+            void setDevAddr(const OCDevAddr addr);
+
+            const std::string getHost() const;
+
+            OCRepPayload* getPayload() const;
 
             void addChild(const OCRepresentation&);
 
@@ -129,6 +123,8 @@ namespace OC
 
             void setChildren(const std::vector<OCRepresentation>& children);
 
+            void setUri(const char* uri);
+
             void setUri(const std::string& uri);
 
             std::string getUri() const;
@@ -137,10 +133,14 @@ namespace OC
 
             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);
+
             bool emptyData() const;
 
             int numberOfAttributes() const;
@@ -168,8 +168,16 @@ namespace OC
 
                 if(x!= m_values.end())
                 {
-                    val = boost::get<T>(x->second);
-                    return true;
+                    try
+                    {
+                        val = boost::get<T>(x->second);
+                        return true;
+                    }
+                    catch (boost::bad_get& e)
+                    {
+                        val = T();
+                        return false;
+                    }
                 }
                 else
                 {
@@ -193,7 +201,14 @@ namespace OC
                 auto x = m_values.find(str);
                 if(x != m_values.end())
                 {
-                    val = boost::get<T>(x->second);
+                    try
+                    {
+                        val = boost::get<T>(x->second);
+                    }
+                    catch (boost::bad_get& e)
+                    {
+                        return val;
+                    }
                 }
                 return val;
             }
@@ -228,6 +243,9 @@ namespace OC
 
             bool isNULL(const std::string& str) const;
 
+        private:
+            std::string m_host;
+
             // STL Container stuff
         public:
             class iterator;
@@ -247,7 +265,15 @@ namespace OC
                     template<typename T>
                     T getValue() const
                     {
-                        return boost::get<T>(m_values[m_attrName]);
+                        try
+                        {
+                            return boost::get<T>(m_values[m_attrName]);
+                        }
+                        catch (boost::bad_get& e)
+                        {
+                            T val = T();
+                            return val;
+                        }
                     }
 
                     std::string getValueToString() const;
@@ -259,7 +285,7 @@ namespace OC
                         return *this;
                     }
 
-                    AttributeItem& operator=(std::nullptr_t rhs)
+                    AttributeItem& operator=(std::nullptr_t /*rhs*/)
                     {
                         NullType t;
                         m_values[m_attrName] = t;
@@ -269,7 +295,7 @@ namespace OC
                     // 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<
+                    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 ||
@@ -290,14 +316,18 @@ namespace OC
                      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
+                     , int>::type = 0// enable_if
                     >
                     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;
@@ -389,8 +419,16 @@ namespace OC
             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
@@ -415,25 +453,9 @@ namespace OC
                     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;