refactor: session: provide an iterator over consumer data channel keys
[lttng-tools.git] / src / common / reference.hpp
index ccf5f4e86f3bcc709b6894a64eda1d3339fabac9..d98b2181d374a5bfccf0766ff7ca566c1f5d97e2 100644 (file)
@@ -16,18 +16,28 @@ namespace lttng {
 template <typename ReferencedType, typename CustomDeleter>
 class non_copyable_reference {
 public:
-       explicit non_copyable_reference(ReferencedType& instance) noexcept : _value(&instance)
-       {
-       }
+       using referenced_type = ReferencedType;
+       using deleter = CustomDeleter;
 
        non_copyable_reference(non_copyable_reference&& other) noexcept : _value(other._value)
        {
-               other._value = nullptr;
+               _value = other._value;
+               other.release();
        }
 
        non_copyable_reference() = delete;
        non_copyable_reference(const non_copyable_reference&) = delete;
-       non_copyable_reference& operator=(non_copyable_reference&&) = delete;
+       non_copyable_reference& operator=(non_copyable_reference&& other) noexcept
+       {
+               if (this != &other) {
+                       _clean_up();
+                       _value = other._value;
+                       other.release();
+               }
+
+               return *this;
+       }
+
        non_copyable_reference& operator=(const non_copyable_reference&) = delete;
 
        ReferencedType& get() const noexcept
@@ -45,7 +55,22 @@ public:
                return *_value;
        }
 
+       void release() noexcept
+       {
+               _value = nullptr;
+       }
+
        ~non_copyable_reference()
+       {
+               _clean_up();
+       }
+
+private:
+       explicit non_copyable_reference(ReferencedType& instance) noexcept : _value(&instance)
+       {
+       }
+
+       void _clean_up()
        {
                if (!_value) {
                        return;
@@ -53,12 +78,23 @@ public:
 
                typename CustomDeleter::deleter del;
                del(_value);
+               release();
        }
 
-private:
+       template <typename FactoryReferencedType, typename FactoryCustomDeleter>
+       friend non_copyable_reference<FactoryReferencedType, FactoryCustomDeleter>
+       make_non_copyable_reference(FactoryReferencedType&);
+
        ReferencedType *_value = nullptr;
 };
 
+template <typename ReferencedType, typename CustomDeleter>
+non_copyable_reference<ReferencedType, CustomDeleter>
+make_non_copyable_reference(ReferencedType& instance)
+{
+       return non_copyable_reference<ReferencedType, CustomDeleter>(instance);
+}
+
 } /* namespace lttng */
 
 #endif /* LTTNG_REFERENCE_H */
This page took 0.022945 seconds and 4 git commands to generate.