]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: add a factory function returning unique_ptr to Formatter API
authorRonen Friedman <rfriedma@redhat.com>
Tue, 3 Jan 2023 09:42:36 +0000 (11:42 +0200)
committerRonen Friedman <rfriedma@redhat.com>
Wed, 4 Jan 2023 06:44:21 +0000 (08:44 +0200)
As the testing code (but not just) is full of the following sequence:
- dyn-allocating a Formatter;
- using it in its 'naked pointer' form;
- deleting it;

which is not a recommended practice.

Signed-off-by: Ronen Friedman <rfriedma@redhat.com>
src/common/Formatter.h

index 6751e4709ce0bf12455c05c97cd8bd5a8eee73b1..abdc172cba53b329b2be78d8dc687575b0db8bc1 100644 (file)
@@ -8,6 +8,7 @@
 
 #include <deque>
 #include <list>
+#include <memory>
 #include <vector>
 #include <stdarg.h>
 #include <sstream>
@@ -62,6 +63,12 @@ namespace ceph {
     static Formatter *create(std::string_view type) {
       return create(type, "json-pretty", "");
     }
+    template <typename... Params>
+    static std::unique_ptr<Formatter> create_unique(Params &&...params)
+    {
+      return std::unique_ptr<Formatter>(
+         Formatter::create(std::forward<Params>(params)...));
+    }
 
     Formatter();
     virtual ~Formatter();