[openrtm-commit:00451] r2231 - trunk/OpenRTM-aist/src/lib/rtm

openrtm @ openrtm.org openrtm @ openrtm.org
2011年 10月 10日 (月) 10:09:53 JST


Author: n-ando
Date: 2011-10-10 10:09:53 +0900 (Mon, 10 Oct 2011)
New Revision: 2231

Modified:
   trunk/OpenRTM-aist/src/lib/rtm/ConfigAdmin.cpp
   trunk/OpenRTM-aist/src/lib/rtm/ConfigAdmin.h
   trunk/OpenRTM-aist/src/lib/rtm/ConfigurationListener.h
Log:
Now onUpdateParam() callback is called when parameter is updated

Modified: trunk/OpenRTM-aist/src/lib/rtm/ConfigAdmin.cpp
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/ConfigAdmin.cpp	2011-09-05 13:33:03 UTC (rev 2230)
+++ trunk/OpenRTM-aist/src/lib/rtm/ConfigAdmin.cpp	2011-10-10 01:09:53 UTC (rev 2231)
@@ -25,6 +25,31 @@
 {
   /*!
    * @if jp
+   * @brief コールバックのセット
+   * @else
+   * @brief Setting callback
+   * @endif
+   */
+  void ConfigBase::setCallback(ConfigAdmin* cadmin, CallbackFunc cbf)
+    {
+      m_admin = cadmin;
+      m_callback = cbf;
+    }
+
+  /*!
+   * @if jp
+   * @brief 変数変更を知らせるオブザーバ関数
+   * @else
+   * @brief Observer function to notify variable changed
+   * @endif
+   */
+  void ConfigBase::notifyUpdate(const char* key, const char* val)
+  {
+    (m_admin->*m_callback)(key, val);
+  }
+
+  /*!
+   * @if jp
    * @brief コンストラクタ
    * @else
    * @endif
@@ -51,7 +76,40 @@
     m_params.clear();
   }
   
+  /*!
+   * @if jp
+   * @brief コンフィギュレーションパラメータの解除
+   * @else
+   * @brief Unbinding configuration parameters
+   * @endif
+   */
+  bool ConfigAdmin::unbindParameter(const char* param_name)
+  {
+    std::vector<ConfigBase*>::iterator it;
+    it = std::find_if(m_params.begin(), m_params.end(),
+		      find_conf(param_name));
+    if (it == m_params.end())
+      {
+        return false;
+      }
+    m_params.erase(it);
 
+    // configsets
+    const std::vector<coil::Properties*>& leaf(m_configsets.getLeaf());
+
+    for (size_t i(0); i < leaf.size(); ++i)
+      {
+        if (leaf[i]->hasKey(param_name))
+          {
+            coil::Properties* p(leaf[i]->removeNode(param_name));
+            delete p;
+          }
+      }
+
+    return true;
+  }
+
+
   /*!
    * @if jp
    * @brief コンフィギュレーションパラメータの更新
@@ -88,11 +146,11 @@
     for (int i(0), len(m_params.size()); i < len; ++i)
       {
 	if (prop.hasKey(m_params[i]->name) != NULL)
-	  {
-	    m_params[i]->update(prop[m_params[i]->name].c_str());
-            onUpdate(config_set);
-	  }
+          {
+            m_params[i]->update(prop[m_params[i]->name].c_str());
+          }
       }
+    onUpdate(config_set);
   }
 
 
@@ -182,7 +240,7 @@
     std::string node(config_set.getName());
     if (node.empty()) { return false; }
     
-    coil::Properties& p(m_configsets.getNode(config_set.getName()));
+    coil::Properties& p(m_configsets.getNode(node));
 
     p << config_set;
     m_changed = true;
@@ -434,10 +492,11 @@
    * @endif
    */
   void
-  ConfigAdmin::onUpdateParam(const char* config_set, const char* config_param)
+  ConfigAdmin::onUpdateParam(const char* config_param, const char* config_value)
   {
-    m_listeners.configparam_[ON_UPDATE_CONFIG_PARAM].notify(config_set,
-                                                            config_param);
+    std::cout << "Update: key = " << config_param << " value = " << config_value << std::endl;
+    m_listeners.configparam_[ON_UPDATE_CONFIG_PARAM].notify(config_param,
+                                                            config_value);
   }
 
   /*!

Modified: trunk/OpenRTM-aist/src/lib/rtm/ConfigAdmin.h
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/ConfigAdmin.h	2011-09-05 13:33:03 UTC (rev 2230)
+++ trunk/OpenRTM-aist/src/lib/rtm/ConfigAdmin.h	2011-10-10 01:09:53 UTC (rev 2231)
@@ -120,6 +120,8 @@
    */
   typedef ConfigurationSetNameListener OnActivateSetCallback;
 
+  // forward decl
+  class ConfigAdmin;
 
   //============================================================
   // ConfigBase class
@@ -178,7 +180,9 @@
      * @endif
      */
     ConfigBase(const char* name_, const char* def_val)
-      : name(name_), default_value(def_val) {}
+      : name(name_), default_value(def_val),
+        string_value(""), m_admin(NULL), m_callback(NULL)
+    {}
     
     /*!
      * @if jp
@@ -196,6 +200,44 @@
      * @endif
      */
     virtual ~ConfigBase(void){};
+
+    // typedef of ConfigAdmin's member function
+    typedef void (ConfigAdmin::*CallbackFunc)(const char*, const char*);
+
+    /*!
+     * @if jp
+     *
+     * @brief コールバックのセット
+     * 
+     * 変数変更時にコールされるコールバック関数をセットする.
+     *
+     * @else
+     *
+     * @brief Setting callback
+     *
+     * This member function sets callback function which is called
+     * when variable is changed.
+     *
+     * @endif
+     */
+    void setCallback(ConfigAdmin* cadmin, CallbackFunc cbf);
+
+    /*!
+     * @if jp
+     *
+     * @brief 変数変更を知らせるオブザーバ関数
+     * 
+     * 変数変更を知らせるオブザーバ関数.
+     *
+     * @else
+     *
+     * @brief Observer function to notify variable changed
+     *
+     * This function notifies variable has been changed.
+     *
+     * @endif
+     */
+    void notifyUpdate(const char* key, const char* val);
     
     /*!
      * @if jp
@@ -223,7 +265,7 @@
      * @endif
      */
     virtual bool update(const char* val) = 0;
-    
+
     /*!
      * @if jp
      * @brief  コンフィギュレーション名
@@ -241,6 +283,32 @@
      * @endif
      */
     const char* default_value;
+
+  protected:
+    /*!
+     * @if jp
+     * @brief  文字列形式の現在値
+     * @else
+     * @brief  Current value in string format
+     * @endif
+     */
+    std::string string_value;
+    /*!
+     * @if jp
+     * @brief  ConfigAdminオブジェクトへのポインタ
+     * @else
+     * @brief  A pointer to the ConfigAdmin object
+     * @endif
+     */
+    ConfigAdmin* m_admin;
+    /*!
+     * @if jp
+     * @brief  コールバックのメンバ関数ポインタ
+     * @else
+     * @brief  A member function pointer to the callback function.
+     * @endif
+     */
+    CallbackFunc m_callback;
   };
   
   //============================================================
@@ -357,8 +425,16 @@
      */
     virtual bool update(const char* val)
     {
-      if ((*m_trans)(m_var, val)) { return true; }
+      if (string_value == val) { return true; }
+      string_value = val;
+      // value changed
+      if ((*m_trans)(m_var, val))
+        {
+          notifyUpdate(name, val);
+          return true;
+        }
       (*m_trans)(m_var, default_value);
+      notifyUpdate(name, val);
       return false;
     }
     
@@ -617,9 +693,38 @@
       if (def_val == 0) { return false; }
       if (isExist(param_name)) { return false; }
       if (!trans(var, def_val)) { return false; }
-      m_params.push_back(new Config<VarType>(param_name, var, def_val, trans));
+      Config<VarType>* c = new Config<VarType>(param_name, var, def_val, trans);
+      m_params.push_back(c);
+      c->setCallback(this, &RTC::ConfigAdmin::onUpdateParam);
       return true;
     }
+
+    /*!
+     * @if jp
+     *
+     * @brief コンフィギュレーションパラメータの解除
+     * 
+     * コンフィギュレーションパラメータと変数のバインドを解除する。
+     * 指定した名称のコンフィギュレーションパラメータが存在しない場合は
+     * falseを返す。
+     *
+     * @param param_name コンフィギュレーションパラメータ名
+     * @return 設定結果(設定成功:true,設定失敗:false)
+     * 
+     * @else
+     *
+     * @brief Unbinding configuration parameters
+     * 
+     * Unbind configuration parameter from its variable. It returns
+     * false, if configuration parameter of specified name has already
+     * existed.
+     *
+     * @param param_name Configuration parameter name
+     * @return Setup result (Successful:true, Failed:false)
+     *
+     * @endif
+     */
+    bool unbindParameter(const char* param_name);
         
     /*!
      * @if jp

Modified: trunk/OpenRTM-aist/src/lib/rtm/ConfigurationListener.h
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/ConfigurationListener.h	2011-09-05 13:33:03 UTC (rev 2230)
+++ trunk/OpenRTM-aist/src/lib/rtm/ConfigurationListener.h	2011-10-10 01:09:53 UTC (rev 2231)
@@ -31,7 +31,7 @@
    * @if jp
    * @brief ConfigurationParamListener のタイプ
    *
-   * - ON_UPDATE_CONFIG_PARAM,
+   * - ON_UPDATE_CONFIG_PARAM: パラメータが変更された
    *
    * @else
    * @brief The types of ConnectorDataListener



openrtm-commit メーリングリストの案内