Mana
Loading...
Searching...
No Matches
resource.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <ctime>
25#include <string>
26
31{
32 friend class ResourceManager;
33
34 public:
39
40 Resource() = default;
41
45 void incRef() { ++mRefCount; }
46
52 void decRef(OrphanPolicy orphanPolicy = DeleteLater);
53
57 const std::string &getIdPath() const
58 { return mIdPath; }
59
60 protected:
61 virtual ~Resource() = default;
62
63 private:
64 std::string mIdPath;
65 time_t mTimeStamp;
66 unsigned mRefCount = 0;
67};
68
72template<typename RESOURCE>
74{
75public:
76 // Allow implicit construction from RESOURCE *
77 ResourceRef(RESOURCE *resource = nullptr)
78 : mResource(resource)
79 {
80 if (mResource)
81 mResource->incRef();
82 }
83
84 // Copy constructor
86 : mResource(other.mResource)
87 {
88 if (mResource)
89 mResource->incRef();
90 }
91
92 // Move constructor
94 : mResource(other.mResource)
95 {
96 other.mResource = nullptr;
97 }
98
99 // Destructor
101 {
102 if (mResource)
103 mResource->decRef();
104 }
105
106 // Assignment operator
108 {
109 if (this != &other)
110 {
111 if (mResource)
112 mResource->decRef();
113
114 mResource = other.mResource;
115
116 if (mResource)
117 mResource->incRef();
118 }
119 return *this;
120 }
121
122 // Move assignment operator
124 {
125 if (this != &other)
126 {
127 if (mResource)
128 mResource->decRef();
129
130 mResource = other.mResource;
131 other.mResource = nullptr;
132 }
133 return *this;
134 }
135
136 // Allow dereferencing
137 RESOURCE *operator->() const
138 { return mResource; }
139
140 RESOURCE *get() const
141 { return mResource; }
142
143 // Allow implicit conversion to RESOURCE *
144 operator RESOURCE *() const
145 { return mResource; }
146
153 RESOURCE *release()
154 {
155 RESOURCE *resource = mResource;
156 mResource = nullptr;
157 return resource;
158 }
159
160private:
161 RESOURCE *mResource;
162};
A class for loading and managing resources.
Automatically counting Resource reference.
Definition resource.h:74
ResourceRef(RESOURCE *resource=nullptr)
Definition resource.h:77
RESOURCE * get() const
Definition resource.h:140
ResourceRef & operator=(ResourceRef &&other)
Definition resource.h:123
ResourceRef(const ResourceRef &other)
Definition resource.h:85
RESOURCE * operator->() const
Definition resource.h:137
RESOURCE * mResource
Definition resource.h:161
ResourceRef(ResourceRef &&other)
Definition resource.h:93
ResourceRef & operator=(const ResourceRef &other)
Definition resource.h:107
RESOURCE * release()
Releases the resource without decrementing the reference count!
Definition resource.h:153
A generic reference counted resource object.
Definition resource.h:31
std::string mIdPath
Path identifying this resource.
Definition resource.h:64
void incRef()
Increments the internal reference count.
Definition resource.h:45
virtual ~Resource()=default
const std::string & getIdPath() const
Return the path identifying this resource.
Definition resource.h:57
time_t mTimeStamp
Time at which the resource was orphaned.
Definition resource.h:65
Resource()=default
void decRef(OrphanPolicy orphanPolicy=DeleteLater)
Decrements the reference count.
Definition resource.cpp:30
unsigned mRefCount
Reference count.
Definition resource.h:66
@ DeleteImmediately
Definition resource.h:37
@ DeleteLater
Definition resource.h:36