]> git.stg.codes - stg.git/blobdiff - include/stg/user_property.h
Ticket 37. The ChangePolicyToString() and StringToChangePolicy()
[stg.git] / include / stg / user_property.h
index 1716296acd7300482c53f3014c41d7f13869574d..20ca6fd8052fe33336a3c66b89292a6da0462239 100644 (file)
@@ -12,12 +12,14 @@ $Author: faust $
 #include <ctime>
 #include <string>
 #include <set>
+#include <map>
 #include <sstream>
 #include <iostream>
 
 #include "stg/logger.h"
 #include "stg/locker.h"
 #include "stg/scriptexecuter.h"
+#include "stg/common.h"
 
 #include "store.h"
 #include "admin.h"
@@ -25,10 +27,16 @@ $Author: faust $
 #include "noncopyable.h"
 
 extern volatile time_t stgTime;
-
+//-----------------------------------------------------------------------------
+class USER_PROPERTY_BASE {
+public:
+    virtual std::string ToString() const = 0;
+};
+//-----------------------------------------------------------------------------
+typedef std::map<std::string, USER_PROPERTY_BASE *> REGISTRY;
 //-----------------------------------------------------------------------------
 template<typename varT>
-class USER_PROPERTY {
+class USER_PROPERTY : public USER_PROPERTY_BASE {
 public:
     USER_PROPERTY(varT & val);
     virtual ~USER_PROPERTY();
@@ -68,10 +76,12 @@ public:
                          bool isPassword,
                          bool isStat,
                          STG_LOGGER & logger,
-                         const std::string & sd);
+                         const std::string & sd,
+                         REGISTRY & properties);
     virtual ~USER_PROPERTY_LOGGED() {}
 
     USER_PROPERTY_LOGGED<varT> * GetPointer() throw() { return this; }
+    const USER_PROPERTY_LOGGED<varT> * GetPointer() const throw() { return this; }
     const varT & Get() const { return USER_PROPERTY<varT>::ConstData(); }
     const std::string & GetName() const { return name; }
     bool Set(const varT & val,
@@ -118,6 +128,7 @@ private:
     USER_STAT stat;
     USER_CONF conf;
 
+    REGISTRY properties;
 public:
     USER_PROPERTIES(const std::string & sd);
 
@@ -130,6 +141,9 @@ public:
 
     void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
 
+    std::string GetPropertyValue(const std::string & name) const;
+    bool Exists(const std::string & name) const;
+
     USER_PROPERTY_LOGGED<double>            cash;
     USER_PROPERTY_LOGGED<DIR_TRAFF>         up;
     USER_PROPERTY_LOGGED<DIR_TRAFF>         down;
@@ -201,7 +215,7 @@ template <typename varT>
 inline
 void USER_PROPERTY<varT>::Set(const varT & rvalue)
 {
-STG_LOCKER locker(&mutex, __FILE__, __LINE__);
+STG_LOCKER locker(&mutex);
 
 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
 
@@ -231,7 +245,7 @@ template <typename varT>
 inline
 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
 {
-STG_LOCKER locker(&mutex, __FILE__, __LINE__);
+STG_LOCKER locker(&mutex);
 beforeNotifiers.insert(n);
 }
 //-----------------------------------------------------------------------------
@@ -239,7 +253,7 @@ template <typename varT>
 inline
 void USER_PROPERTY<varT>::DelBeforeNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n)
 {
-STG_LOCKER locker(&mutex, __FILE__, __LINE__);
+STG_LOCKER locker(&mutex);
 beforeNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(n));
 }
 //-----------------------------------------------------------------------------
@@ -247,7 +261,7 @@ template <typename varT>
 inline
 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
 {
-STG_LOCKER locker(&mutex, __FILE__, __LINE__);
+STG_LOCKER locker(&mutex);
 afterNotifiers.insert(n);
 }
 //-----------------------------------------------------------------------------
@@ -255,7 +269,7 @@ template <typename varT>
 inline
 void USER_PROPERTY<varT>::DelAfterNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n)
 {
-STG_LOCKER locker(&mutex, __FILE__, __LINE__);
+STG_LOCKER locker(&mutex);
 afterNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(n));
 }
 //-----------------------------------------------------------------------------
@@ -268,7 +282,8 @@ USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
                                                  bool isPass,
                                                  bool isSt,
                                                  STG_LOGGER & logger,
-                                                 const std::string & sd)
+                                                 const std::string & sd,
+                                                 REGISTRY & properties)
 
     : USER_PROPERTY<varT>(val),
       stgLogger(logger),
@@ -277,6 +292,7 @@ USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
       name(n),
       scriptsDir(sd)
 {
+properties.insert(std::make_pair(ToLower(name), this));
 }
 //-------------------------------------------------------------------------
 template <typename varT>
@@ -376,6 +392,23 @@ else
 //-------------------------------------------------------------------------
 //-------------------------------------------------------------------------
 //-------------------------------------------------------------------------
+inline
+std::string USER_PROPERTIES::GetPropertyValue(const std::string & name) const
+{
+REGISTRY::const_iterator it = properties.find(ToLower(name));
+if (it == properties.end())
+    return "";
+return it->second->ToString();
+}
+//-----------------------------------------------------------------------------
+inline
+bool USER_PROPERTIES::Exists(const std::string & name) const
+{
+return properties.find(ToLower(name)) != properties.end();
+}
+//-------------------------------------------------------------------------
+//-------------------------------------------------------------------------
+//-------------------------------------------------------------------------
 template<typename varT>
 inline
 std::ostream & operator<< (std::ostream & stream, const USER_PROPERTY<varT> & value)
@@ -384,9 +417,10 @@ return stream << value.ConstData();
 }
 //-----------------------------------------------------------------------------
 template<typename varT>
+inline
 std::string USER_PROPERTY<varT>::ToString() const
 {
-std::stringstream stream;
+std::ostringstream stream;
 stream << value;
 return stream.str();
 }