hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
HandlerProperties.hpp
Go to the documentation of this file.
1 
14 #ifndef HANDLER_PROPERTIES_HPP
15 #define HANDLER_PROPERTIES_HPP
16 
17 #include <Poco/Dynamic/Var.h>
18 #include <Poco/Types.h>
19 
20 #include "JsonSerializable.hpp"
21 #include "Dictionary.hpp"
22 
23 namespace HCE
24 {
25 namespace handlers
26 {
27 //-----------------------------------------------------------------------------
28 class HandlerProperties : public HCE::handlers::Dictionary<std::string, Poco::Dynamic::Var>, public IJsonSerializable
29 {
30 public:
32  {
37  };
38 public:
39  HandlerProperties(void);
41 
42  bool serialize(std::string& json);
43  bool unserialize(const std::string& json);
44 
45  bool load(const std::string& fileName);
46  bool save(const std::string& fileName);
47 
48 template <typename _Property> void set(const std::string& name, _Property value);
49 template <typename _Property> _Property get(const std::string& name, _Property defaultValue=_Property());
50 
51 private:
53 
54  friend std::istream& operator>>(std::istream& is, HandlerProperties& handlerProperties);
55  friend std::ostream& operator<<(std::ostream& os, const HandlerProperties& handlerProperties);
56 };
57 //-----------------------------------------------------------------------------
58 template <typename _Property> void HandlerProperties::set(const std::string& name, _Property value)
59 {
60  setItem(name, value);
61 }
62 //-----------------------------------------------------------------------------
63 template <typename _Property> _Property HandlerProperties::get(const std::string& name, _Property defaultValue)
64 {
65  Poco::Dynamic::Var var = getItem(name);
66  _Property value = defaultValue;
67  try
68  {
69  value = var.convert<_Property>();
70  }
71  catch(...)
72  {
73  value = defaultValue;
74  }
75  return value;
76 }
77 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
79 } // end namespace handlers
80 } // end namespace HCE
81 
82 #endif // HANDLER_PROPERTIES_HPP