resource.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28
29
30 #pragma once
31
32 #include <memory>
33 #include <map>
34
35 namespace clan
36 {
39
40 class Resource_BaseImpl
41 {
42 public:
43 virtual ~Resource_BaseImpl() { }
44 };
45
46 template<typename Type>
47 class Resource_Impl : public Resource_BaseImpl
48 {
49 public:
50 Resource_Impl() : value(), generation(0) { }
51 Resource_Impl(const Type &initial_value) : value(initial_value), generation(0) { }
52 Type value;
53 int generation;
54 };
55
57 template<typename Type>
58 class Resource
59 {
60 public:
61 Resource()
62 : object(new Resource_Impl<Type>()), generation(-1)
63 {
64 }
65
66 Resource(std::shared_ptr<Resource_Impl<Type> > object)
67 : object(object), generation(-1)
68 {
69 }
70
71 Resource(const Type &initial_value)
72 : object(new Resource_Impl<Type>(initial_value)), generation(-1)
73 {
74 }
75
76 Type *operator->()
77 {
78 return &object->value;
79 }
80
81 const Type *operator->() const
82 {
83 return &object->value;
84 }
85
86 bool updated()
87 {
88 bool updated = (generation != object->generation);
89 generation = object->generation;
90 return updated;
91 }
92
93 void set(const Type &value)
94 {
95 object->value = value;
96 generation = ++object->generation;
97 }
98
99 Type &get()
100 {
101 return object->value;
102 }
103
104 const Type &get() const
105 {
106 return object->value;
107 }
108
109 operator Type&() {return object->value;}
110 operator const Type&() const {return object->value;}
111
112 const std::shared_ptr<Resource_Impl<Type> > &handle() const { return object; }
113
114 bool operator<(const Resource &other) const { return object < other.object; }
115 bool operator<=(const Resource &other) const { return object <= other.object; }
116 bool operator>(const Resource &other) const { return object > other.object; }
117 bool operator>=(const Resource &other) const { return object >= other.object; }
118 bool operator==(const Resource &other) const { return object == other.object; }
119 bool operator!=(const Resource &other) const { return object != other.object; }
120
121 private:
122 std::shared_ptr<Resource_Impl<Type> > object;
123 int generation;
124 };
125
126 }
127
clan
Definition: clanapp.h:35
clan::Resource_Impl::Resource_Impl
Resource_Impl()
Definition: resource.h:50
clan::Resource::operator!=
bool operator!=(const Resource &other) const
Definition: resource.h:119
clan::Resource
Resource proxy of a specific type.
Definition: resource.h:58
clan::Resource::Resource
Resource(const Type &initial_value)
Definition: resource.h:71
clan::Resource::Resource
Resource(std::shared_ptr< Resource_Impl< Type > > object)
Definition: resource.h:66
clan::Resource::operator<=
bool operator<=(const Resource &other) const
Definition: resource.h:115
clan::Resource::Resource
Resource()
Definition: resource.h:61
clan::Resource::operator==
bool operator==(const Resource &other) const
Definition: resource.h:118
clan::Resource::operator>
bool operator>(const Resource &other) const
Definition: resource.h:116
clan::Resource_Impl::Resource_Impl
Resource_Impl(const Type &initial_value)
Definition: resource.h:51
clan::Resource_Impl::value
Type value
Definition: resource.h:52
clan::Resource_BaseImpl
Definition: resource.h:40
clan::Resource::operator->
const Type * operator->() const
Definition: resource.h:81
clan::Resource_BaseImpl::~Resource_BaseImpl
virtual ~Resource_BaseImpl()
Definition: resource.h:43
clan::Resource::operator>=
bool operator>=(const Resource &other) const
Definition: resource.h:117
clan::Resource::handle
const std::shared_ptr< Resource_Impl< Type > > & handle() const
Definition: resource.h:112
clan::Resource_Impl::generation
int generation
Definition: resource.h:53
clan::Resource::operator->
Type * operator->()
Definition: resource.h:76
clan::Resource::set
void set(const Type &value)
Definition: resource.h:93
clan::Resource::operator<
bool operator<(const Resource &other) const
Definition: resource.h:114
clan::Resource_Impl
Definition: resource.h:47
clan::Resource::updated
bool updated()
Definition: resource.h:86