Realistic 3D camera system
3D camera system components
object_pool.hpp
Go to the documentation of this file.
1 //
2 // detail/object_pool.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_DETAIL_OBJECT_POOL_HPP
12 #define ASIO_DETAIL_OBJECT_POOL_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
19 
21 
22 namespace asio {
23 namespace detail {
24 
25 template <typename Object>
27 
29 {
30 public:
31  template <typename Object>
32  static Object* create()
33  {
34  return new Object;
35  }
36 
37  template <typename Object>
38  static void destroy(Object* o)
39  {
40  delete o;
41  }
42 
43  template <typename Object>
44  static Object*& next(Object* o)
45  {
46  return o->next_;
47  }
48 
49  template <typename Object>
50  static Object*& prev(Object* o)
51  {
52  return o->prev_;
53  }
54 };
55 
56 template <typename Object>
57 class object_pool
58  : private noncopyable
59 {
60 public:
61  // Constructor.
63  : live_list_(0),
64  free_list_(0)
65  {
66  }
67 
68  // Destructor destroys all objects.
70  {
71  destroy_list(live_list_);
72  destroy_list(free_list_);
73  }
74 
75  // Get the object at the start of the live list.
76  Object* first()
77  {
78  return live_list_;
79  }
80 
81  // Allocate a new object.
82  Object* alloc()
83  {
84  Object* o = free_list_;
85  if (o)
86  free_list_ = object_pool_access::next(free_list_);
87  else
88  o = object_pool_access::create<Object>();
89 
90  object_pool_access::next(o) = live_list_;
92  if (live_list_)
93  object_pool_access::prev(live_list_) = o;
94  live_list_ = o;
95 
96  return o;
97  }
98 
99  // Free an object. Moves it to the free list. No destructors are run.
100  void free(Object* o)
101  {
102  if (live_list_ == o)
103  live_list_ = object_pool_access::next(o);
104 
106  {
109  }
110 
112  {
115  }
116 
117  object_pool_access::next(o) = free_list_;
119  free_list_ = o;
120  }
121 
122 private:
123  // Helper function to destroy all elements in a list.
124  void destroy_list(Object* list)
125  {
126  while (list)
127  {
128  Object* o = list;
129  list = object_pool_access::next(o);
131  }
132  }
133 
134  // The list of live objects.
135  Object* live_list_;
136 
137  // The free list.
138  Object* free_list_;
139 };
140 
141 } // namespace detail
142 } // namespace asio
143 
145 
146 #endif // ASIO_DETAIL_OBJECT_POOL_HPP
static void destroy(Object *o)
Definition: object_pool.hpp:38
static Object *& next(Object *o)
Definition: object_pool.hpp:44
static Object *& prev(Object *o)
Definition: object_pool.hpp:50