resource.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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#pragma once
30
31#include <memory>
32#include <map>
33
34namespace clan
35{
38
40 {
41 public:
42 virtual ~Resource_BaseImpl() { }
43 };
44
45 template<typename Type>
47 {
48 public:
50 Resource_Impl(const Type &initial_value) : value(initial_value), generation(0) { }
51 Type value;
53 };
54
56 template<typename Type>
58 {
59 public:
61 : object(new Resource_Impl<Type>()), generation(-1)
62 {
63 }
64
65 Resource(std::shared_ptr<Resource_Impl<Type> > object)
66 : object(object), generation(-1)
67 {
68 }
69
70 Resource(const Type &initial_value)
71 : object(new Resource_Impl<Type>(initial_value)), generation(-1)
72 {
73 }
74
75 Type *operator->()
76 {
77 return &object->value;
78 }
79
80 const Type *operator->() const
81 {
82 return &object->value;
83 }
84
85 bool updated()
86 {
87 bool updated = (generation != object->generation);
88 generation = object->generation;
89 return updated;
90 }
91
92 void set(const Type &value)
93 {
94 object->value = value;
95 generation = ++object->generation;
96 }
97
98 Type &get()
99 {
100 return object->value;
101 }
102
103 const Type &get() const
104 {
105 return object->value;
106 }
107
108 operator Type&() { return object->value; }
109 operator const Type&() const { return object->value; }
110
111 const std::shared_ptr<Resource_Impl<Type> > &handle() const { return object; }
112
113 bool operator<(const Resource &other) const { return object < other.object; }
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
120 private:
121 std::shared_ptr<Resource_Impl<Type> > object;
122 int generation;
123 };
124
126}
Definition resource.h:40
virtual ~Resource_BaseImpl()
Definition resource.h:42
Definition resource.h:47
Resource_Impl(const Type &initial_value)
Definition resource.h:50
Type value
Definition resource.h:51
Resource_Impl()
Definition resource.h:49
int generation
Definition resource.h:52
Resource proxy of a specific type.
Definition resource.h:58
bool operator<=(const Resource &other) const
Definition resource.h:114
bool operator>=(const Resource &other) const
Definition resource.h:116
Type & get()
Definition resource.h:98
const Type & get() const
Definition resource.h:103
const std::shared_ptr< Resource_Impl< Type > > & handle() const
Definition resource.h:111
bool operator==(const Resource &other) const
Definition resource.h:117
Resource()
Definition resource.h:60
bool updated()
Definition resource.h:85
void set(const Type &value)
Definition resource.h:92
bool operator<(const Resource &other) const
Definition resource.h:113
Type * operator->()
Definition resource.h:75
bool operator!=(const Resource &other) const
Definition resource.h:118
Resource(const Type &initial_value)
Definition resource.h:70
const Type * operator->() const
Definition resource.h:80
bool operator>(const Resource &other) const
Definition resource.h:115
Resource(std::shared_ptr< Resource_Impl< Type > > object)
Definition resource.h:65
Definition clanapp.h:36