resource.h Source File - ClanLib SDK
Description: resource.h Source File

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
39 class Resource_BaseImpl
40 {
41 public:
42 virtual ~Resource_BaseImpl() { }
43 };
44
45 template<typename Type>
46 class Resource_Impl : public Resource_BaseImpl
47 {
48 public:
49 Resource_Impl() : value(), generation(0) { }
50 Resource_Impl(const Type &initial_value) : value(initial_value), generation(0) { }
51 Type value;
52 int generation;
53 };
54
56 template<typename Type>
57 class Resource
58 {
59 public:
60 Resource()
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}
clan::Resource_BaseImpl
Definition resource.h:40
clan::Resource_BaseImpl::~Resource_BaseImpl
virtual ~Resource_BaseImpl()
Definition resource.h:42
clan::Resource_Impl
Definition resource.h:47
clan::Resource_Impl::Resource_Impl
Resource_Impl(const Type &initial_value)
Definition resource.h:50
clan::Resource_Impl::value
Type value
Definition resource.h:51
clan::Resource_Impl::Resource_Impl
Resource_Impl()
Definition resource.h:49
clan::Resource_Impl::generation
int generation
Definition resource.h:52
clan::Resource::operator<=
bool operator<=(const Resource &other) const
Definition resource.h:114
clan::Resource::operator>=
bool operator>=(const Resource &other) const
Definition resource.h:116
clan::Resource::get
Type & get()
Definition resource.h:98
clan::Resource::get
const Type & get() const
Definition resource.h:103
clan::Resource::handle
const std::shared_ptr< Resource_Impl< Type > > & handle() const
Definition resource.h:111
clan::Resource::operator==
bool operator==(const Resource &other) const
Definition resource.h:117
clan::Resource::Resource
Resource()
Definition resource.h:60
clan::Resource::updated
bool updated()
Definition resource.h:85
clan::Resource::set
void set(const Type &value)
Definition resource.h:92
clan::Resource::operator<
bool operator<(const Resource &other) const
Definition resource.h:113
clan::Resource::operator->
Type * operator->()
Definition resource.h:75
clan::Resource::operator!=
bool operator!=(const Resource &other) const
Definition resource.h:118
clan::Resource::Resource
Resource(const Type &initial_value)
Definition resource.h:70
clan::Resource::operator->
const Type * operator->() const
Definition resource.h:80
clan::Resource::operator>
bool operator>(const Resource &other) const
Definition resource.h:115
clan::Resource::Resource
Resource(std::shared_ptr< Resource_Impl< Type > > object)
Definition resource.h:65
clan
Definition clanapp.h:36