-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththing.cpp
More file actions
98 lines (81 loc) · 2.28 KB
/
thing.cpp
File metadata and controls
98 lines (81 loc) · 2.28 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "./thing.hpp"
#include "things/thing_data.hpp"
#include "./thing_factory.hpp"
#define LOCK(MUTEX) LockGuard<RMutex> lock{MUTEX}
using namespace TheatreFile;
Thing::Thing() noexcept: mUID{ID::Invalid} {}\
Thing::Thing(Sarg inName) noexcept: mUID{_generate()}, mName{inName} {}
Thing::~Thing() noexcept
{
this->Shutdown();
LOCK(mMutex);
m_sActiveUIDs.erase(mUID());
}
void Thing::SetVariables(Farg<ThingData> data)
{
LOCK(mMutex);
if(mName.empty())
{ mName = data.name; }
if(not m_pStartingData)
{ m_pStartingData = MakeShared<ThingData>(data); }
}
Shared<ThingData> Thing::GetVariables() const
{
LOCK(mMutex);
auto data{MakeShared<ThingData>()};
data->name = mName;
data->type = Type();
return data;
}
bool Thing::DerivedFrom(FPID inType) const
{ LOCK(mMutex); return ThingFactory::IsDerivedFrom(Type(), inType); }
bool Thing::IsThinker() const
{ LOCK(mMutex); return ThingFactory::IsThinker(Type()); }
bool Thing::IsResource() const
{ LOCK(mMutex); return ThingFactory::IsResource(Type()); }
ThingData Thing::GetStartingVariables() const
{
LOCK(mMutex);
return (m_pStartingData)
? *m_pStartingData
: ThingData{};
}
ID Thing::uid() const
{ LOCK(mMutex); return mUID; }
Farg<std::string> Thing::name() const
{ LOCK(mMutex); return mName; }
const char* const Thing::c_name() const
{ LOCK(mMutex); return mName.data(); }
Error Thing::rename(Sarg inNewName)
{
LOCK(mMutex);
Error status{OK};
if(m_pNameChangeCallback)
{ status = m_pNameChangeCallback(mName, inNewName); }
if(status == OK)
{ mName = inNewName; }
return status;
}
void Thing::SetNameChangeCallback(pNameChangeCallback_f inCallback)
{
LOCK(mMutex);
m_pNameChangeCallback = inCallback;
}
ID Thing::_generate()
{
LOCK(m_sUIDMutex);
if(m_sActiveUIDs.size() == UID::max_size)
{
print_warning("Somehow, you have hit the maximum number of UIDs for this set ({}). Please consider removing a few. Please.", UID::max_size);
return ID::Invalid;
}
uint new_id{_get_random()};
while(not m_sActiveUIDs.insert(new_id).second)
{ new_id = _get_random(); }
return new_id;
}
uint Thing::_get_random()
{
m_sIdEngine.seed(m_sRandomSeed());
return m_sDistribution(m_sIdEngine);
}