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>
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 \
--- /dev/null
+/*
+ * 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 */