APIs to setNULL and check for NULL in OCRepresenation
authorSashi Penta <sashi.kumar.penta@intel.com>
Tue, 30 Sep 2014 08:44:54 +0000 (01:44 -0700)
committerSashi Penta <sashi.kumar.penta@intel.com>
Tue, 30 Sep 2014 21:14:00 +0000 (14:14 -0700)
Change-Id: I3db71123c1362efaf7e641a845fa737057d56da5

examples/garageclient.cpp
examples/garageserver.cpp
include/OCApi.h

index 813c06b..17cf0cf 100644 (file)
@@ -80,6 +80,15 @@ void printRepresentation(const OCRepresentation& rep)
                   << rep2.numberOfAttributes() << std::endl;
 
 
+        if(rep.isNULL("nullAttribute"))
+        {
+            std::cout << "\tnullAttribute is null." << std::endl;
+        }
+        else
+        {
+            std::cout << "\tnullAttribute is not null." << std::endl;
+        }
+
         rep.getValue("light", myGarage.m_lightRep);
 
         myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
index 4f15780..5212db1 100644 (file)
@@ -63,6 +63,16 @@ public:
         m_garageRep.setValue("state", m_state);
         m_garageRep.setValue("name", m_name);
 
+        // For demonstration purpose we are setting x to nullptr here.
+        // In reality it may happen else where.
+        int* x = nullptr;
+
+        // Check for nullptr and set null for that attribute
+        if(x == nullptr)
+        {
+            m_garageRep.setNULL("nullAttribute");
+        }
+
         for(int i = 0; i <= 9; i++)
         {
             m_lightStates.push_back(i % 2 == 0);
index 1c8c2a2..39b1345 100644 (file)
@@ -33,6 +33,7 @@
 #include <boost/variant.hpp>
 
 #include "ocstack.h"
+#include <OCException.h>
 
 namespace OC
 {
@@ -166,6 +167,29 @@ namespace OC
             return m_attributeMap.find(str) != m_attributeMap.end();
         }
 
+        void setNULL(const std::string& str)
+        {
+            m_attributeMap[str] = "null";
+        }
+
+        bool isNULL(const std::string& str) const
+        {
+            auto x = m_attributeMap.find(str);
+
+            if(m_attributeMap.end() != x)
+            {
+                return x->second.compare("null") == 0;
+            }
+            else
+            {
+                std::ostringstream message;
+                message << "attribute: " << str << " doesn't exist\n";
+                throw OCException(message.str());
+            }
+
+            return false;
+        }
+
         int numberOfAttributes() const
         {
             return m_attributeMap.size();
@@ -244,7 +268,6 @@ namespace OC
     };
 
     typedef boost::variant<
-                // TODO NULL value to be implmented.
                 int,
                 double,
                 bool,
@@ -418,8 +441,7 @@ namespace OC
                 }
                 else
                 {
-                    // TODO Logging
-                    std::cout << "Array type should have atleast []\n";
+                    throw OCException("Array type should have at least []");
                 }
 
             }
@@ -441,8 +463,7 @@ namespace OC
                 }
                 else
                 {
-                    // TODO Logging
-                    std::cout << "Array type should have atleast []\n";
+                    throw OCException("Array type should have at least []");
                 }
             }
 
@@ -464,8 +485,7 @@ namespace OC
                 }
                 else
                 {
-                    // TODO Logging
-                    std::cout << "Array type should have atleast []\n";
+                    throw OCException("Array type should have at least []");
                 }
 
             }
@@ -488,8 +508,7 @@ namespace OC
                 }
                 else
                 {
-                    // TODO Logging
-                    std::cout << "Array type should have atleast []\n";
+                    throw OCException("Array type should have at least []");
                 }
             }
 
@@ -507,9 +526,7 @@ namespace OC
                 }
                 catch(boost::property_tree::json_parser::json_parser_error &e)
                 {
-                    //TODO: log this
-                    std::cout << "Parse error\n";
-                    return;
+                    throw OCException("JSON parse error");
                 }
 
                 for(auto& item: payload)