delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
SelfReferenceCountingObject.hpp
1
7#pragma once
8
9#include <cstddef>
10
11#ifdef DELPI_THREAD_SAFE
12#include <atomic>
13#endif
14
15namespace delpi {
16
35 public:
37 void AddRef() {
38#ifdef DELPI_THREAD_SAFE
39 ref_count_.fetch_add(1);
40#else
41 ref_count_++;
42#endif
43 }
45 void Release() {
46#ifdef DELPI_THREAD_SAFE
47 if (ref_count_.fetch_sub(1) == 1) {
48#else
49 if (--ref_count_ == 0) {
50#endif
51 delete this;
52 }
53 }
54
56 [[nodiscard]] std::size_t use_count() const noexcept {
57#ifdef DELPI_THREAD_SAFE
58 return ref_count_.load();
59#else
60 return ref_count_;
61#endif
62 }
63
64 protected:
69 virtual ~SelfReferenceCountingObject() = default;
70
71 private:
72#ifdef DELPI_THREAD_SAFE
73 std::atomic<std::size_t> ref_count_;
74#else
75 std::size_t ref_count_{0};
76#endif
77}; // NOLINT(readability/braces) per C++ standard concept definition
78
79} // namespace delpi
Utility class to be inherited from to obtain compatibility with the intrusive_ptr.
virtual ~SelfReferenceCountingObject()=default
Virtual destructor for the SelfReferenceCountingObject.
Global namespace for the delpi library.