Lolly
1.4.28
Loading...
Searching...
No Matches
Kernel
Abstractions
resource.hpp
Go to the documentation of this file.
1
2
/** \file resource.hpp
3
* \copyright GPLv3
4
* \details Utils to define a class that holding its instances globally.
5
* Instances is called "Resources", and can be looked up by a string.
6
* Weak referrence is provided to access member of resources.
7
* \author Joris van der Hoeven
8
* \date 1999
9
* \author jingkaimori
10
* \date 2024
11
*/
12
13
#ifndef RESOURCE_H
14
#define RESOURCE_H
15
16
#include "
hashmap.hpp
"
17
#include "
string.hpp
"
18
19
/**
20
* \brief base class of resources
21
* \note no referrence counting is applied on this structure
22
*/
23
template
<
class
T>
struct
rep
{
24
string
res_name
;
25
inline
rep<T>
(
string
res_name2
) :
res_name
(
res_name2
) {
26
T::instances (
res_name
)=
static_cast<
pointer
>
(
this
);
27
}
28
inline
virtual
~rep<T>
() { T::instances->reset (
res_name
); }
29
};
30
31
template
<
class
R>
class
resource_ptr
{
32
protected
:
33
~resource_ptr
(){};
34
35
public
:
36
R
*
rep
;
37
inline
static
hashmap<string, pointer>
instances
=
38
hashmap<string, pointer>
(
NULL
);
39
/* C++17 feature, use inline keyword here to pack definition along with
40
declaration, instead of defining static member inside expansion
41
of RESOURCE macro.*/
42
inline
R
*
operator->
() {
return
rep
; }
43
};
44
45
#define RESOURCE(PTR) \
46
struct PTR##_rep; \
47
struct PTR : public resource_ptr<PTR##_rep> { \
48
inline PTR (PTR##_rep* rep2= NULL) { rep= rep2; } \
49
inline PTR (string s) { rep= (PTR##_rep*) instances[s]; } \
50
inline ~PTR () {} \
51
}
52
53
template
<
class
R>
54
inline
bool
55
is_nil
(
const
resource_ptr<R>
&
res
) {
56
return
res
.rep ==
NULL
;
57
}
58
59
template
<
class
R>
60
tm_ostream
&
operator<<
(
tm_ostream
& out,
const
resource_ptr<R>
&
t
);
61
62
#define make(T, s, im) ((T::instances->contains (s)) ? T (s) : T (im))
63
64
template
<
class
T>
65
tm_ostream
&
66
operator<<
(
tm_ostream
& out,
const
resource_ptr<T>
&
t
) {
67
return
out <<
t
->res_name;
68
}
69
70
#endif
// RESOURCE_H
t
blackbox t[13]
Definition
blackbox_test.cpp:40
list
The list class represents a linked list.
Definition
list.hpp:48
resource_ptr
Definition
resource.hpp:31
resource_ptr::~resource_ptr
~resource_ptr()
Definition
resource.hpp:33
resource_ptr::instances
static hashmap< string, pointer > instances
Definition
resource.hpp:37
resource_ptr::rep
R * rep
Definition
resource.hpp:36
resource_ptr::operator->
R * operator->()
Definition
resource.hpp:42
tm_ostream
Definition
tm_ostream.hpp:36
hashmap.hpp
is_nil
bool is_nil(const resource_ptr< R > &res)
Definition
resource.hpp:55
operator<<
tm_ostream & operator<<(tm_ostream &out, const resource_ptr< R > &t)
string.hpp
rep
base class of resources
Definition
resource.hpp:23
rep::res_name
string res_name
Definition
resource.hpp:24
Generated by
1.10.0