The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
shared_object.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Chris Hopman <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef SHARED_OBJECT_HPP_INCLUDED
16 #define SHARED_OBJECT_HPP_INCLUDED
17 
18 #include <algorithm>
19 #include <cassert>
20 #include <climits>
21 #include <cstddef>
22 #include <ostream>
23 
24 template <typename T>
25 struct shared_node {
26  T val;
27  mutable unsigned long count;
28  shared_node() : val(), count(0) { }
29  shared_node(const T& o) : val(o), count(0) { }
30  static const unsigned long max_count = ULONG_MAX;
31 };
32 
33 template <typename T, typename node = shared_node<T> >
35 public:
36  typedef T type;
37 
38  static const node* insert_into_index(const node &);
39  static void erase_from_index(const node *);
40 
41  shared_object() : val_(nullptr) { set(T()); }
42 
43  explicit shared_object(const T &o) : val_(nullptr) { set(o); }
44 
46  assert(valid());
47  val_->count++;
48  }
49 
50  operator const T &() const {
51  assert(valid());
52  return val_->val;
53  }
54 
56  if (val_ == o.val_) return *this;
57  shared_object tmp(o);
58  swap(tmp);
59  return *this;
60  }
61 
62  /// Assignment from the template type is supported.
63  shared_object& operator=(const T& o) {
64  shared_object tmp(o);
65  swap(tmp);
66  return *this;
67  }
68 
70 
71  void set(const T& o) {
72  if (valid() && o == get()) return;
73  clear();
74 
75  val_ = insert_into_index(node(o)); //&*index().insert(node(o)).first;
76  val_->count++;
77 
78  assert((val_->count) < (node::max_count));
79  }
80 
81  const T& get() const {
82  assert(valid());
83  return val_->val;
84  }
85 
86  void swap(shared_object& o) {
87  std::swap(val_, o.val_);
88  }
89 
90  const node* ptr() const {
91  return val_;
92  }
93 
94 protected:
95 
96  const node* val_;
97 
98  bool valid() const {
99  return val_ != nullptr;
100  }
101 
102  void clear() {
103  if (!valid()) return;
104  val_->count--;
105 
106  if (val_->count == 0) erase_from_index(val_); //index().erase(index().find(val_->val));
107  val_ = nullptr;
108  }
109 
110 };
111 
112 template <typename T>
114  return a.ptr() == b.ptr() ? true : a.get() == b.get();
115 }
116 
117 template <typename T>
118 bool operator<(const shared_object<T>& a, const shared_object<T>& b) {
119  assert(a.valid());
120  assert(b.valid());
121  return a.get() < b.get();
122 }
123 
124 template <typename T>
125 std::ostream& operator<<(std::ostream& stream, const shared_object<T>& o) {
126  assert(o.valid());
127  return stream << o.get();
128 }
129 
130 template <typename T>
131 std::istream& operator>>(std::istream& stream, shared_object<T>& o) {
132  T t;
133  std::istream& ret = stream >> t;
134  o.set(t);
135  return ret;
136 }
137 
138 #endif
shared_object(const shared_object &o)
shared_object(const T &o)
const node * val_
static void erase_from_index(const node *)
shared_object & operator=(const T &o)
Assignment from the template type is supported.
bool operator==(const shared_object< T > &a, const shared_object< T > &b)
const T & get() const
std::istream & operator>>(std::istream &stream, shared_object< T > &o)
void swap(shared_object &o)
shared_object & operator=(const shared_object &o)
GLdouble GLdouble t
Definition: glew.h:1366
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
GLuint GLuint stream
Definition: glew.h:5239
static const unsigned long max_count
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
bool valid() const
void set(const T &o)
const node * ptr() const
unsigned long count
void swap(game_board &one, game_board &other)
Definition: game_board.cpp:56
shared_node(const T &o)
static const node * insert_into_index(const node &)