From f6b7a9ef4aa9b20d8e61e21cb12f68c2cb57feff Mon Sep 17 00:00:00 2001 From: Sashi Penta Date: Tue, 30 Sep 2014 01:44:54 -0700 Subject: [PATCH] APIs to setNULL and check for NULL in OCRepresenation Change-Id: I3db71123c1362efaf7e641a845fa737057d56da5 --- examples/garageclient.cpp | 9 +++++++++ examples/garageserver.cpp | 10 ++++++++++ include/OCApi.h | 41 +++++++++++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/examples/garageclient.cpp b/examples/garageclient.cpp index 813c06b..17cf0cf 100644 --- a/examples/garageclient.cpp +++ b/examples/garageclient.cpp @@ -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); diff --git a/examples/garageserver.cpp b/examples/garageserver.cpp index 4f15780..5212db1 100644 --- a/examples/garageserver.cpp +++ b/examples/garageserver.cpp @@ -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); diff --git a/include/OCApi.h b/include/OCApi.h index 1c8c2a2..39b1345 100644 --- a/include/OCApi.h +++ b/include/OCApi.h @@ -33,6 +33,7 @@ #include #include "ocstack.h" +#include 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) -- 2.7.4