ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

reuse.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
27 #ifndef _UCOMMON_REUSE_H_
28 #define _UCOMMON_REUSE_H_
29 
30 #ifndef _UCOMMON_THREAD_H_
31 #include <ucommon/thread.h>
32 #endif
33 
34 namespace ucommon {
35 
36 typedef unsigned short vectorsize_t;
37 
46 {
47 private:
48  size_t objsize;
49  unsigned count, limit, used;
51 
53 
54 protected:
55  ArrayReuse(size_t objsize, unsigned c);
56  ArrayReuse(size_t objsize, unsigned c, void *memory);
57 
58 public:
62  ~ArrayReuse();
63 
64 protected:
65  bool avail(void) const;
66 
68  ReusableObject *get(void);
69  ReusableObject *request(void);
70 };
71 
80 {
81 private:
82  unsigned limit, count;
83  size_t osize;
84 
86 
87 protected:
88  PagerReuse(mempager *pager, size_t objsize, unsigned count);
89  ~PagerReuse();
90 
91  bool avail(void) const;
92  ReusableObject *get(void);
94  ReusableObject *request(void);
95 };
96 
103 template<class T>
104 class array_reuse : protected ArrayReuse
105 {
106 private:
108 
109 public:
114  inline array_reuse(unsigned count) :
115  ArrayReuse(sizeof(T), count) {}
116 
122  inline array_reuse(unsigned count, void *memory) :
123  ArrayReuse(sizeof(T), count, memory) {}
124 
129  inline operator bool() const {
130  return avail();
131  }
132 
137  inline bool operator!() const {
138  return !avail();
139  }
140 
145  inline T* request(void) {
146  return static_cast<T*>(ArrayReuse::request());
147  }
148 
154  inline T* get(void) {
155  return static_cast<T*>(ArrayReuse::get());
156  }
157 
163  inline T* create(void) {
164  return init<T>(static_cast<T*>(ArrayReuse::get()));
165  }
166 
173  inline T* get(timeout_t timeout) {
174  return static_cast<T*>(ArrayReuse::get(timeout));
175  }
176 
183  inline T* create(timeout_t timeout) {
184  return init<T>(static_cast<T*>(ArrayReuse::get(timeout)));
185  }
186 
191  inline void release(T *object) {
192  ArrayReuse::release(object);
193  }
194 
200  inline operator T*() {
201  return array_reuse::get();
202  }
203 
209  inline T *operator*() {
210  return array_reuse::get();
211  }
212 };
213 
220 template <class T>
221 class paged_reuse : protected PagerReuse
222 {
223 private:
225 
226 public:
234  inline paged_reuse(mempager *pager, unsigned count) :
235  PagerReuse(pager, sizeof(T), count) {}
236 
241  inline operator bool() const {
242  return PagerReuse::avail();
243  }
244 
249  inline bool operator!() const {
250  return !PagerReuse::avail();
251  }
252 
258  inline T *get(void) {
259  return static_cast<T*>(PagerReuse::get());
260  }
261 
268  inline T *create(void) {
269  return init<T>(static_cast<T*>(PagerReuse::get()));
270  }
271 
278  inline T *get(timeout_t timeout) {
279  return static_cast<T*>(PagerReuse::get(timeout));
280  }
281 
289  inline T *create(timeout_t timeout) {
290  return init<T>(static_cast<T*>(PagerReuse::get(timeout)));
291  }
292 
297  inline T *request(void) {
298  return static_cast<T*>(PagerReuse::request());
299  }
300 
305  inline void release(T *object) {
306  PagerReuse::release(object);
307  }
308 
314  inline T *operator*() {
315  return paged_reuse::get();
316  }
317 
323  inline operator T*() {
324  return paged_reuse::get();
325  }
326 };
327 
328 } // namespace ucommon
329 
330 #endif
ucommon::mempager
Definition: memory.h:184
ucommon::limit
T &() limit(T &value, T &low, T &high)
Definition: generics.h:468
ucommon::paged_reuse::paged_reuse
paged_reuse(mempager *pager, unsigned count)
Definition: reuse.h:234
ucommon::ArrayReuse::objsize
size_t objsize
Definition: reuse.h:48
ucommon
Definition: access.cpp:23
ucommon::paged_reuse::release
void release(T *object)
Definition: reuse.h:305
ucommon::array_reuse
Definition: reuse.h:104
ucommon::array_reuse::operator*
T * operator*()
Definition: reuse.h:209
ucommon::ReusableObject
Definition: linked.h:152
ucommon::MemoryRedirect
Definition: protocols.h:100
timeout_t
unsigned long timeout_t
Definition: platform.h:453
ucommon::array_reuse::__DELETE_DEFAULTS
__DELETE_DEFAULTS(array_reuse)
ucommon::PagerReuse::request
ReusableObject * request(void)
Definition: reuse.cpp:165
ucommon::vectorsize_t
unsigned short vectorsize_t
Definition: reuse.h:36
caddr_t
#define caddr_t
Definition: file.h:86
ucommon::array_reuse::array_reuse
array_reuse(unsigned count, void *memory)
Definition: reuse.h:122
ucommon::PagerReuse
Definition: reuse.h:79
ucommon::array_reuse::array_reuse
array_reuse(unsigned count)
Definition: reuse.h:114
ucommon::paged_reuse::operator*
T * operator*()
Definition: reuse.h:314
ucommon::paged_reuse::operator!
bool operator!() const
Definition: reuse.h:249
ucommon::paged_reuse::create
T * create(timeout_t timeout)
Definition: reuse.h:289
ucommon::array_reuse::get
T * get(timeout_t timeout)
Definition: reuse.h:173
__EXPORT
#define __EXPORT
Definition: config.h:49
thread.h
ucommon::array_reuse::create
T * create(void)
Definition: reuse.h:163
ucommon::PagerReuse::osize
size_t osize
Definition: reuse.h:83
ucommon::PagerReuse::avail
bool avail(void) const
Definition: reuse.cpp:151
ucommon::pager
Definition: memory.h:853
ucommon::ArrayReuse::request
ReusableObject * request(void)
Definition: reuse.cpp:118
ucommon::PagerReuse::limit
unsigned limit
Definition: reuse.h:82
ucommon::paged_reuse::create
T * create(void)
Definition: reuse.h:268
ucommon::paged_reuse::request
T * request(void)
Definition: reuse.h:297
ucommon::paged_reuse
Definition: reuse.h:221
ucommon::array_reuse::get
T * get(void)
Definition: reuse.h:154
ucommon::paged_reuse::get
T * get(void)
Definition: reuse.h:258
ucommon::array_reuse::release
void release(T *object)
Definition: reuse.h:191
ucommon::ArrayReuse::count
unsigned count
Definition: reuse.h:49
ucommon::paged_reuse::__DELETE_DEFAULTS
__DELETE_DEFAULTS(paged_reuse)
ucommon::ReusableAllocator::release
void release(ReusableObject *object)
Definition: thread.cpp:130
__DELETE_DEFAULTS
#define __DELETE_DEFAULTS(x)
Definition: platform.h:162
ucommon::PagerReuse::count
unsigned count
Definition: reuse.h:82
timeout
static shell::numericopt timeout('t', "--timeout", _TEXT("optional keyboard input timeout"), "seconds", 0)
__PROTOCOL
#define __PROTOCOL
Definition: platform.h:112
ucommon::array_reuse::request
T * request(void)
Definition: reuse.h:145
ucommon::ReusableAllocator
Definition: thread.h:416
ucommon::PagerReuse::get
ReusableObject * get(void)
Definition: reuse.cpp:185
ucommon::ArrayReuse::used
unsigned used
Definition: reuse.h:49
ucommon::ArrayReuse::avail
bool avail(void) const
Definition: reuse.cpp:64
ucommon::ArrayReuse::get
ReusableObject * get(void)
Definition: reuse.cpp:113
ucommon::array_reuse::operator!
bool operator!() const
Definition: reuse.h:137
ucommon::paged_reuse::get
T * get(timeout_t timeout)
Definition: reuse.h:278
ucommon::array_reuse::create
T * create(timeout_t timeout)
Definition: reuse.h:183
ucommon::ArrayReuse::mem
caddr_t mem
Definition: reuse.h:50
ucommon::ArrayReuse
Definition: reuse.h:45