You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pegasus.apache.org by GitBox <gi...@apache.org> on 2022/11/15 10:27:07 UTC

[GitHub] [incubator-pegasus] empiredan commented on a diff in pull request #1241: refactor: use command_deregister to auto deregister from command_manager

empiredan commented on code in PR #1241:
URL: https://github.com/apache/incubator-pegasus/pull/1241#discussion_r1022568316


##########
src/utils/command_manager.cpp:
##########
@@ -24,48 +24,50 @@
  * THE SOFTWARE.
  */
 
+#include "utils/command_manager.h"
+
 #include <iostream>
 #include <sstream>
 #include <thread>
 
-#include "utils/command_manager.h"
 #include "utils/fmt_logging.h"
+#include "utils/smart_pointers.h"
 #include "utils/utils.h"
 
 namespace dsn {
 
-dsn_handle_t command_manager::register_command(const std::vector<std::string> &commands,
-                                               const std::string &help_one_line,
-                                               const std::string &help_long,
-                                               command_handler handler)
+std::unique_ptr<command_deregister>
+command_manager::register_command(const std::vector<std::string> &commands,
+                                  const std::string &help_one_line,
+                                  const std::string &help_long,
+                                  command_handler handler)
 {
     utils::auto_write_lock l(_lock);
     bool is_valid_cmd = false;
-
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             is_valid_cmd = true;
-            auto it = _handlers.find(cmd);
-            CHECK(it == _handlers.end(), "command '{}' already regisered", cmd);
+            CHECK(_handlers.find(cmd) == _handlers.end(), "command '{}' already regisered", cmd);
         }
     }
     CHECK(is_valid_cmd, "should not register empty command");
 
     command_instance *c = new command_instance();
     c->commands = commands;
-    c->help_long = help_long;
     c->help_short = help_one_line;
+    c->help_long = help_long;
     c->handler = handler;
 
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             _handlers[cmd] = c;
         }
     }
-    return c;
+
+    return dsn::make_unique<command_deregister>((uintptr_t)c);

Review Comment:
   ```suggestion
       return dsn::make_unique<command_deregister>(static_cast<uintptr_t>(c));
   ```



##########
src/utils/command_manager.h:
##########
@@ -64,20 +66,30 @@ class command_manager : public ::dsn::utils::singleton<command_manager>
         command_handler handler;
     };
 
+    void deregister_command(uintptr_t handle);
+
     typedef ref_ptr<command_instance> command_instance_ptr;
     utils::rw_lock_nr _lock;
     std::map<std::string, command_instance_ptr> _handlers;
 };
 
-} // namespace dsn
+class command_deregister
+{
+public:
+    command_deregister(uintptr_t id) : cmd_id_(id) {}
+    ~command_deregister()
+    {
+        if (cmd_id_ != 0) {
+            dsn::command_manager::instance().deregister_command(cmd_id_);
+            cmd_id_ = 0;
+        }
+    }
 
-#define UNREGISTER_VALID_HANDLER(ptr)                                                              \
-    do {                                                                                           \
-        if (ptr != nullptr) {                                                                      \
-            dsn::command_manager::instance().deregister_command(ptr);                              \
-            ptr = nullptr;                                                                         \
-        }                                                                                          \
-    } while (0)
+private:
+    uintptr_t cmd_id_ = 0;

Review Comment:
   Can `cmd_id_` be type of `command_instance *`, or just `std::shared_ptr<command_instance>` ?



##########
src/utils/command_manager.cpp:
##########
@@ -24,48 +24,50 @@
  * THE SOFTWARE.
  */
 
+#include "utils/command_manager.h"
+
 #include <iostream>
 #include <sstream>
 #include <thread>
 
-#include "utils/command_manager.h"
 #include "utils/fmt_logging.h"
+#include "utils/smart_pointers.h"
 #include "utils/utils.h"
 
 namespace dsn {
 
-dsn_handle_t command_manager::register_command(const std::vector<std::string> &commands,
-                                               const std::string &help_one_line,
-                                               const std::string &help_long,
-                                               command_handler handler)
+std::unique_ptr<command_deregister>
+command_manager::register_command(const std::vector<std::string> &commands,
+                                  const std::string &help_one_line,
+                                  const std::string &help_long,
+                                  command_handler handler)
 {
     utils::auto_write_lock l(_lock);
     bool is_valid_cmd = false;
-
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             is_valid_cmd = true;
-            auto it = _handlers.find(cmd);
-            CHECK(it == _handlers.end(), "command '{}' already regisered", cmd);
+            CHECK(_handlers.find(cmd) == _handlers.end(), "command '{}' already regisered", cmd);
         }
     }
     CHECK(is_valid_cmd, "should not register empty command");
 
     command_instance *c = new command_instance();
     c->commands = commands;
-    c->help_long = help_long;
     c->help_short = help_one_line;
+    c->help_long = help_long;
     c->handler = handler;
 
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             _handlers[cmd] = c;
         }
     }
-    return c;
+
+    return dsn::make_unique<command_deregister>((uintptr_t)c);
 }
 
-void command_manager::deregister_command(dsn_handle_t handle)
+void command_manager::deregister_command(uintptr_t handle)

Review Comment:
   Should `c` be released by `delete c;` at last ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org