RigsofRods
Soft-body Physics Simulation
source
main
utils
memory
RefCountingObject.h
Go to the documentation of this file.
1
2
// RefCountingObject system for AngelScript
3
// Copyright (c) 2022 Petr Ohlidal
4
// https://github.com/only-a-ptr/RefCountingObject-AngelScript
5
// See license (MIT) at the bottom of this file.
6
7
#pragma once
8
9
#include <angelscript.h>
10
11
#include <mutex>
// Against accidental threaded access
12
#include "
Application.h
"
// Provides access to AppContext
13
#include "
AppContext.h
"
// Stores main thread ID for debug checking
14
15
16
#if !defined(RefCoutingObject_DEBUGTRACE)
17
# define RefCoutingObject_DEBUGTRACE()
18
#endif
19
20
#if !defined(RefCountingObject_ASSERT)
21
# include <cassert>
22
# define RefCountingObject_ASSERT(_Expr_) assert(_Expr_)
23
#endif
24
26
template
<
class
T>
class
RefCountingObject
27
{
28
public
:
29
RefCountingObject
()
30
{
31
RefCoutingObject_DEBUGTRACE
();
32
}
33
34
virtual
~RefCountingObject
()
35
{
36
RefCoutingObject_DEBUGTRACE
();
37
}
38
39
void
AddRef
()
40
{
41
// Detect and prevent accidental threaded access.
42
RefCountingObject_ASSERT
(
RoR::App::GetAppContext
()->GetMainThreadID() == std::this_thread::get_id());
43
std::unique_lock<std::mutex> lock(
m_refcount_mtx
);
44
m_refcount
++;
45
RefCoutingObject_DEBUGTRACE
();
46
}
47
48
void
Release
()
49
{
50
// Detect and prevent accidental threaded access.
51
RefCountingObject_ASSERT
(
RoR::App::GetAppContext
()->GetMainThreadID() == std::this_thread::get_id());
52
int
nw_refcount = -1;
53
{
54
std::unique_lock<std::mutex> lock(
m_refcount_mtx
);
55
m_refcount
--;
56
nw_refcount =
m_refcount
;
57
RefCoutingObject_DEBUGTRACE
();
58
}
59
if
(nw_refcount == 0)
60
{
61
delete
this
;
// commit suicide! This is legit in C++, but you must completely 100% positively read https://isocpp.org/wiki/faq/freestore-mgmt#delete-this
62
}
63
}
64
65
static
void
RegisterRefCountingObject
(AS_NAMESPACE_QUALIFIER asIScriptEngine* engine,
const
char
* name)
66
{
67
int
r;
68
69
#if defined(AS_USE_NAMESPACE)
70
using namespace
AngelScript;
71
#endif
72
73
// Registering the reference type
74
r = engine->RegisterObjectType(name, 0, asOBJ_REF);
RefCountingObject_ASSERT
( r >= 0 );
75
76
// Registering the addref/release behaviours
77
r = engine->RegisterObjectBehaviour(name, asBEHAVE_ADDREF,
"void f()"
, asMETHOD(T,
AddRef
), asCALL_THISCALL);
RefCountingObject_ASSERT
( r >= 0 );
78
r = engine->RegisterObjectBehaviour(name, asBEHAVE_RELEASE,
"void f()"
, asMETHOD(T,
Release
), asCALL_THISCALL);
RefCountingObject_ASSERT
( r >= 0 );
79
}
80
81
int
m_refcount
= 0;
82
std::mutex
m_refcount_mtx
;
// Against accidental threaded access
83
};
84
85
/*
86
MIT License
87
88
Copyright (c) 2022 Petr OhlĂdal
89
90
Permission is hereby granted, free of charge, to any person obtaining a copy
91
of this software and associated documentation files (the "Software"), to deal
92
in the Software without restriction, including without limitation the rights
93
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
94
copies of the Software, and to permit persons to whom the Software is
95
furnished to do so, subject to the following conditions:
96
97
The above copyright notice and this permission notice shall be included in all
98
copies or substantial portions of the Software.
99
100
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
101
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
102
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
103
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
104
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
105
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
106
SOFTWARE.
107
*/
RefCountingObject::m_refcount
int m_refcount
Definition:
RefCountingObject.h:81
RefCountingObject::RefCountingObject
RefCountingObject()
Definition:
RefCountingObject.h:29
RoR::App::GetAppContext
AppContext * GetAppContext()
Definition:
Application.cpp:266
RefCountingObject::RegisterRefCountingObject
static void RegisterRefCountingObject(AS_NAMESPACE_QUALIFIER asIScriptEngine *engine, const char *name)
Definition:
RefCountingObject.h:65
AppContext.h
System integration layer; inspired by OgreBites::ApplicationContext.
RefCountingObject::AddRef
void AddRef()
Definition:
RefCountingObject.h:39
RefCountingObject::m_refcount_mtx
std::mutex m_refcount_mtx
Definition:
RefCountingObject.h:82
RefCountingObject::~RefCountingObject
virtual ~RefCountingObject()
Definition:
RefCountingObject.h:34
Application.h
Central state/object manager and communications hub.
RefCountingObject_ASSERT
#define RefCountingObject_ASSERT(_Expr_)
Definition:
RefCountingObject.h:22
RefCountingObject::Release
void Release()
Definition:
RefCountingObject.h:48
RefCountingObject
Self reference-counting objects, as requred by AngelScript garbage collector.
Definition:
RefCountingObject.h:26
RefCoutingObject_DEBUGTRACE
#define RefCoutingObject_DEBUGTRACE()
Definition:
RefCountingObject.h:17
Generated by
1.8.17