#ifndef BABELTRACE_CPP_COMMON_BT2_SHARED_OBJECT_HPP
#define BABELTRACE_CPP_COMMON_BT2_SHARED_OBJECT_HPP
-#include "common/assert.h"
-#include "cpp-common/bt2s/optional.hpp"
+#include <utility>
+
+#include "optional-borrowed-object.hpp"
namespace bt2 {
* manages the reference counting of the underlying libbabeltrace2
* object.
*
- * When you move a shared object, it becomes invalid, in that
- * operator*() and operator->() will either fail to assert in debug mode
- * or trigger a segmentation fault.
+ * When you move a shared object, it becomes empty, in that operator*()
+ * and operator->() will either fail to assert in debug mode or trigger
+ * a segmentation fault.
+ *
+ * The default constructor builds an empty shared object. You may also
+ * call the reset() method to make a shared object empty. Check whether
+ * or not a shared object is empty with the `bool` operator.
*
* `LibObjT` is the direct libbabeltrace2 object type, for example
* `bt_stream_class` or `const bt_value`.
template <typename, typename, typename>
friend class SharedObject;
+public:
+ /*
+ * Builds an empty shared object.
+ */
+ explicit SharedObject() noexcept
+ {
+ }
+
private:
/*
* Builds a shared object from `obj` without getting a reference.
this->_putRef();
}
- ObjT& operator*() noexcept
+ ObjT operator*() const noexcept
{
- BT_ASSERT_DBG(_mObj);
return *_mObj;
}
- const ObjT& operator*() const noexcept
+ BorrowedObjectProxy<ObjT> operator->() const noexcept
{
- BT_ASSERT_DBG(_mObj);
- return *_mObj;
+ return _mObj.operator->();
}
- ObjT *operator->() noexcept
+ operator bool() const noexcept
{
- BT_ASSERT_DBG(_mObj);
- return &*_mObj;
+ return _mObj.hasObject();
}
- const ObjT *operator->() const noexcept
+ /*
+ * Makes this shared object empty.
+ */
+ void reset() noexcept
{
- BT_ASSERT_DBG(_mObj);
- return &*_mObj;
+ if (_mObj) {
+ this->_putRef();
+ this->_reset();
+ }
}
/*
* wrapper manages and returns it, making the caller become an
* active owner.
*
- * This method makes this object invalid.
+ * This method makes this object empty.
*/
ObjT release() noexcept
{
- BT_ASSERT_DBG(_mObj);
-
const auto obj = *_mObj;
this->_reset();
*/
void _getRef() const noexcept
{
- if (_mObj) {
- RefFuncsT::get(_mObj->libObjPtr());
- }
+ RefFuncsT::get(_mObj.libObjPtr());
}
/*
*/
void _putRef() const noexcept
{
- if (_mObj) {
- RefFuncsT::put(_mObj->libObjPtr());
- }
+ RefFuncsT::put(_mObj.libObjPtr());
}
- bt2s::optional<ObjT> _mObj;
+ OptionalBorrowedObject<ObjT> _mObj;
};
} /* namespace bt2 */