This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ump_object.h
59 lines (48 loc) · 1.68 KB
/
ump_object.h
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
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include "ump_shared.h"
class IExtDummy {};
using UmpCustomDtor = std::function<void(IUmpObject* obj)>;
template<typename TBase, typename TExt = IExtDummy>
class UmpObject : public TBase, public TExt
{
protected:
virtual ~UmpObject() override {}
public:
UmpObject() : _ref_count(1) {}
UmpObject(UmpCustomDtor& dtor) : _ref_count(1), _dtor(dtor) {}
// non copyable
UmpObject(const UmpObject&) = delete;
UmpObject(UmpObject&&) = delete;
UmpObject& operator=(const UmpObject&) = delete;
UmpObject& operator=(const UmpObject&&) = delete;
virtual void Release() override { ReleaseImpl(); }
virtual void AddRef() override { AddRefImpl(); }
inline void log_e(const char* msg) const { log(EUmpVerbosity::Error, msg); }
inline void log_w(const char* msg) const { log(EUmpVerbosity::Warning, msg); }
inline void log_i(const char* msg) const { log(EUmpVerbosity::Info, msg); }
inline void log_d(const char* msg) const { log(EUmpVerbosity::Debug, msg); }
inline void log_e(const std::string& msg) const { log(EUmpVerbosity::Error, *msg); }
inline void log_w(const std::string& msg) const { log(EUmpVerbosity::Warning, *msg); }
inline void log_i(const std::string& msg) const { log(EUmpVerbosity::Info, *msg); }
inline void log_d(const std::string& msg) const { log(EUmpVerbosity::Debug, *msg); }
protected:
inline int ReleaseImpl()
{
const int n = _ref_count.fetch_sub(1); // TODO: better memory_order?
if (n == 1)
{
if (_dtor)
_dtor(static_cast<IUmpObject*>(this));
else
delete this;
}
return n;
}
inline int AddRefImpl()
{
return _ref_count.fetch_add(1); // TODO: better memory_order?
}
protected:
std::atomic<int> _ref_count;
UmpCustomDtor _dtor;
};