-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathRefCounted.h
More file actions
47 lines (37 loc) · 1.14 KB
/
RefCounted.h
File metadata and controls
47 lines (37 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (C) Microsoft Corporation.
// Copyright (C) 2025 IAMAI CONSULTING CORP
//
// MIT License. All rights reserved.
#pragma once
#include <atomic>
#include "IRefCounted.h"
namespace microsoft {
namespace projectairsim {
namespace client {
namespace internal {
// Reference counted class. When the reference count drops
// to zero, the object deletes itelf.
template <typename I>
class TRefCounted : public I {
public:
TRefCounted(void) : I(), ref_count_(1) {}
// Add a reference to this object
void AddRef(void) { ref_count_++; }
// Release a reference to this object. Callers must stop using
// this object immediately after calling this method. If the
// reference count drops below zero, the object deletes itself.
void Release(void) {
assert(ref_count_ > 0);
if (ref_count_-- == 1) delete this;
}
protected:
// Prohibit explicit delete
~TRefCounted() {}
protected:
std::atomic<unsigned int>
ref_count_; // Number of outstanding references to this object
}; // class TRefCounted
} // namespace internal
} // namespace client
} // namespace projectairsim
} // namespace microsoft