FastLED 3.9.15
Loading...
Searching...
No Matches
referent.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/namespace.h"
4
5namespace fl {
6
7template <typename T> class Ptr; // Forward declaration
8template <typename T> class WeakPtr; // Forward declaration
9
10class Referent; // Forward declaration
11
12// Don't inherit from this, this is an internal object.
14 public:
15 WeakReferent() : mRefCount(0), mReferent(nullptr) {}
17
18 void ref() { mRefCount++; }
19 int ref_count() const { return mRefCount; }
20 void unref() {
21 if (--mRefCount == 0) {
22 destroy();
23 }
24 }
25 void destroy() { delete this; }
26 void setReferent(Referent *referent) { mReferent = referent; }
27 Referent *getReferent() const { return mReferent; }
28
29 protected:
30 WeakReferent(const WeakReferent &) = default;
31 WeakReferent &operator=(const WeakReferent &) = default;
32
33 private:
36};
37
38// Base class for reference counted objects.
39// NOTE: This is legacy - new code should use regular classes with fl::shared_ptr<T>
40class Referent {
41 public:
42 // NOTE: Start with ref count 0, TakeOwnership() will call ref() to make it 1
43 Referent() : mRefCount(0), mWeakReferent(nullptr) {}
44
45 // NOTE: Copy constructor also starts with ref count 0 for new object
46 Referent(const Referent &) : mRefCount(0), mWeakReferent(nullptr) {}
47
48 // Assignment does not change reference count
49 Referent &operator=(const Referent &) { return *this; }
50
51 virtual ~Referent() {
52 if (mWeakReferent) {
53 mWeakReferent->setReferent(nullptr);
54 mWeakReferent->unref();
55 }
56 }
57
58 void ref() { mRefCount++; }
59
60 int ref_count() const { return mRefCount; }
61
62 void unref() {
63 if (--mRefCount == 0) {
64 delete this;
65 }
66 }
67
69 if (!mWeakReferent) {
71 mWeakReferent->setReferent(this);
72 mWeakReferent->ref();
73 }
74 return mWeakReferent;
75 }
76
77 protected:
80};
81
82} // namespace fl
Definition ptr.h:114
int ref_count() const
Definition referent.h:60
Referent & operator=(const Referent &)
Definition referent.h:49
int mRefCount
Definition referent.h:78
Referent(const Referent &)
Definition referent.h:46
virtual ~Referent()
Definition referent.h:51
void ref()
Definition referent.h:58
WeakReferent * getWeakReferent()
Definition referent.h:68
WeakReferent * mWeakReferent
Definition referent.h:79
void unref()
Definition referent.h:62
void setReferent(Referent *referent)
Definition referent.h:26
int ref_count() const
Definition referent.h:19
WeakReferent(const WeakReferent &)=default
Referent * mReferent
Definition referent.h:35
WeakReferent & operator=(const WeakReferent &)=default
Referent * getReferent() const
Definition referent.h:27
Implements the FastLED namespace macros.
IMPORTANT!
Definition crgb.h:20