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 
41 {
42 public:
43  virtual ~Resource_BaseImpl() { }
44 };
45 
46 template<typename Type>
48 {
49 public:
51  Resource_Impl(const Type &initial_value) : value(initial_value), generation(0) { }
52  Type value;
54 };
55 
57 template<typename Type>
58 class Resource
59 {
60 public:
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 
Definition: resource.h:41
int generation
Definition: resource.h:53
bool operator>(const Resource &other) const
Definition: resource.h:116
Resource_Impl(const Type &initial_value)
Definition: resource.h:51
bool updated()
Definition: resource.h:86
bool operator>=(const Resource &other) const
Definition: resource.h:117
Resource()
Definition: resource.h:61
virtual ~Resource_BaseImpl()
Definition: resource.h:43
Type * operator->()
Definition: resource.h:76
Resource_Impl()
Definition: resource.h:50
bool operator!=(const Resource &other) const
Definition: resource.h:119
Resource(const Type &initial_value)
Definition: resource.h:71
const Type * operator->() const
Definition: resource.h:81
void set(const Type &value)
Definition: resource.h:93
bool operator<=(const Resource &other) const
Definition: resource.h:115
const Type & get() const
Definition: resource.h:104
Type value
Definition: resource.h:52
Definition: clanapp.h:36
bool operator<(const Resource &other) const
Definition: resource.h:114
Resource(std::shared_ptr< Resource_Impl< Type > > object)
Definition: resource.h:66
const std::shared_ptr< Resource_Impl< Type > > & handle() const
Definition: resource.h:112
Resource proxy of a specific type.
Definition: resource.h:59
bool operator==(const Resource &other) const
Definition: resource.h:118
Type & get()
Definition: resource.h:99
Definition: resource.h:48