tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
refcount.h
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef TENSOR_REFCOUNT_H
21 #define TENSOR_REFCOUNT_H
22 
23 #include <cstring>
24 #include <algorithm>
25 
26 namespace tensor {
27 
48 template<class value_type>
49 class RefPointer {
50 public:
51  typedef value_type elt_t;
52 
54  RefPointer();
56  RefPointer(size_t new_size);
58  RefPointer(elt_t *data, size_t size, bool owned = true);
60  RefPointer(const RefPointer<elt_t> &p);
61 
62 
64  ~RefPointer();
65 
68 
70  elt_t *begin() { appropriate(); return ref_->begin(); }
72  const elt_t *begin() const { return ref_->begin(); }
74  const elt_t *begin_const() const { return ref_->begin(); }
76  const elt_t *end_const() const { return ref_->end(); }
78  const elt_t *end() const { return ref_->end(); }
80  elt_t *end() { appropriate(); return ref_->end(); }
81 
83  size_t size() const { return ref_->size(); }
84 
86  size_t ref_count() const { return ref_->references(); }
87 
89  void reallocate(size_t new_size);
90 
91 private:
92  class pointer;
93  mutable pointer *ref_; // Pointer to data we reference or NULL
94 
98  void appropriate();
99  pointer *reference() const;
100  void dereference();
101 };
102 
103  template<typename T>
104  class shared_ptr {
105  public:
106  shared_ptr(T *t) : _value(t), _ref(new int(1)) {}
107  ~shared_ptr() { --(*_ref); if (!(*_ref)) { delete _value; delete _ref; }}
108  T* operator->() const { return _value; }
109  T& operator*() const { return *_value; }
110  private:
111  int *_ref;
112  T *_value;
113  };
114 
115 }; // namespace
116 
117 #include <tensor/detail/refcount.hpp>
118 
119 #endif /* !REFCOUNT_REFCOUNT_H */
const elt_t * end_const() const
Retreive the pointer without caring for references (unsafe).
Definition: refcount.h:76
value_type elt_t
Type of data pointed to.
Definition: refcount.h:51
A reference counting pointer with copy-on-write.
Definition: refcount.h:49
RefPointer< elt_t > & operator=(const RefPointer< elt_t > &p)
Copy a pointer increasing the reference count.
elt_t * begin()
Retreive the pointer without caring for references (unsafe).
Definition: refcount.h:70
size_t size() const
Size of pointed-to data.
Definition: refcount.h:83
const elt_t * end() const
Retreive the pointer without caring for references (unsafe).
Definition: refcount.h:78
~RefPointer()
Destructor that deletes no longer reference data.
const elt_t * begin_const() const
Retreive the pointer without caring for references (unsafe).
Definition: refcount.h:74
elt_t * end()
Retreive the pointer without caring for references (unsafe).
Definition: refcount.h:80
void reallocate(size_t new_size)
Replace the pointer with newly allocated data.
size_t ref_count() const
Number of references to the internal data.
Definition: refcount.h:86
RefPointer()
Create an empty reference.
const elt_t * begin() const
Retreive the pointer without caring for references (unsafe).
Definition: refcount.h:72