Lolly 1.4.28
Loading...
Searching...
No Matches
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 */
23template <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
31template <class R> class resource_ptr {
32protected:
34
35public:
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
53template <class R>
54inline bool
56 return res.rep == NULL;
57}
58
59template <class R>
61
62#define make(T, s, im) ((T::instances->contains (s)) ? T (s) : T (im))
63
64template <class T>
67 return out << t->res_name;
68}
69
70#endif // RESOURCE_H
blackbox t[13]
The list class represents a linked list.
Definition list.hpp:48
static hashmap< string, pointer > instances
Definition resource.hpp:37
R * operator->()
Definition resource.hpp:42
bool is_nil(const resource_ptr< R > &res)
Definition resource.hpp:55
tm_ostream & operator<<(tm_ostream &out, const resource_ptr< R > &t)
base class of resources
Definition resource.hpp:23
string res_name
Definition resource.hpp:24