cpp-common/bt2: add `bt2::BorrowedObjectProxy`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 5 Dec 2023 14:46:25 +0000 (09:46 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 14 Dec 2023 15:57:04 +0000 (10:57 -0500)
When bt2::Something::operator->() would need to return the address of
some `ObjT` instance, but none is available (because it only keeps the
libbabeltrace2 object pointer, not a built wrapper), then it can use
this new `bt2::BorrowedObjectProxy`.

This new class template accepts a library pointer at construction time,
constructs an internal `ObjT` instance, and its own operator->() method
returns the address of the latter.

Example:

    class Something
    {
        // ...

    public:
        BorrowedObjectProxy<SomeObject> operator->() const noexcept
        {
            return BorrowedObjectProxy<SomeObject> {_mLibObjPtr};
        }

    private:
        const bt_some_object *_mLibObjPtr;
    };

Then, with a `Something` instance `something`:

    something->methodOfSomeObject(...);

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I67d63591fb6593080a15e2ab6804a53bd2d2aba8
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11487
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/Makefile.am
src/cpp-common/bt2/borrowed-object-proxy.hpp [new file with mode: 0644]

index 5e2ac10a7badf4bb34d6188bd130ba2418eacf4e..1429883f654cb5bea2f769d17a3f9378a6c8785f 100644 (file)
@@ -12,6 +12,7 @@ SUBDIRS += bindings/python/bt2
 endif
 
 noinst_HEADERS = \
+       cpp-common/bt2/borrowed-object-proxy.hpp \
        cpp-common/bt2/borrowed-object.hpp \
        cpp-common/bt2/clock-class.hpp \
        cpp-common/bt2/clock-snapshot.hpp \
diff --git a/src/cpp-common/bt2/borrowed-object-proxy.hpp b/src/cpp-common/bt2/borrowed-object-proxy.hpp
new file mode 100644 (file)
index 0000000..93ef405
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2_BORROWED_OBJECT_PROXY_HPP
+#define BABELTRACE_CPP_COMMON_BT2_BORROWED_OBJECT_PROXY_HPP
+
+namespace bt2 {
+
+/*
+ * A proxy containing a valid borrowed object instance of `ObjT` to make
+ * Something::operator->() work when only a libbabeltrace2 object
+ * pointer is available.
+ */
+template <typename ObjT>
+class BorrowedObjectProxy final
+{
+public:
+    explicit BorrowedObjectProxy(typename ObjT::_LibObjPtr libObjPtr) noexcept : _mObj {libObjPtr}
+    {
+    }
+
+    const ObjT *operator->() const noexcept
+    {
+        return &_mObj;
+    }
+
+private:
+    ObjT _mObj;
+};
+
+} /* namespace bt2 */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2_BORROWED_OBJECT_PROXY_HPP */
This page took 0.025387 seconds and 4 git commands to generate.