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