Discontinuous Galerkin Library
#include "dg/algorithm.h"
memory.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
5namespace dg
6{
7
8//there is probably a better class in boost...
17template<class Cloneable>
19{
21 ClonePtr( std::nullptr_t value = nullptr):m_ptr(nullptr){}
26 ClonePtr( Cloneable* ptr): m_ptr(ptr){}
27
35 ClonePtr( const Cloneable& src) : m_ptr( src.clone() ) { }
36
41 ClonePtr( const ClonePtr& src) : m_ptr( src.m_ptr.get() == nullptr ? nullptr : src.m_ptr->clone() ) { }
47 {
48 //copy and swap
49 ClonePtr tmp(src);
50 swap( *this, tmp );
51 return *this;
52 }
57 ClonePtr( ClonePtr&& src) noexcept : m_ptr( nullptr)
58 {
59 swap( *this, src); //steal resource
60 }
65 ClonePtr& operator=( ClonePtr&& src) noexcept
66 {
67 swap( *this, src );
68 return *this;
69 }
87 friend void swap( ClonePtr& first, ClonePtr& second)
88 {
89 std::swap(first.m_ptr,second.m_ptr);
90 }
91
99 void reset( Cloneable* ptr){
100 m_ptr.reset( ptr);
101 }
103 Cloneable* release() noexcept { m_ptr.release();}
108 void reset( const Cloneable& src){
109 ClonePtr tmp(src);
110 swap(*this, tmp);
111 }
112
117 Cloneable * get() {return m_ptr.get();}
122 const Cloneable* get() const {return m_ptr.get();}
123
125 Cloneable& operator*() { return *m_ptr;}
127 const Cloneable& operator*() const { return *m_ptr;}
129 Cloneable* operator->() { return m_ptr.operator->();}
131 const Cloneable* operator->()const { return m_ptr.operator->();}
133 explicit operator bool() const{ return (bool)m_ptr;}
134
135
136 private:
137 std::unique_ptr<Cloneable> m_ptr;
138};
139
140//Memory buffer class: data can be written even if the object is const
150template< class T>
151struct Buffer
152{
155 ptr = new T;
156 }
158 Buffer( const T& t){
159 ptr = new T(t);
160 }
161 Buffer( const Buffer& src){ //copy
162 ptr = new T(*src.ptr);
163 }
164 Buffer( Buffer&& t): ptr( t.ptr){ //move (memory steal) construct
165 t.ptr = nullptr;
166 }
167 Buffer& operator=( Buffer src){ //copy and swap idiom, also implements move assign
168 swap( *this, src);
169 return *this;
170 }
173 delete ptr; //if ptr is nullptr delete does nothing
174 }
175 friend void swap( Buffer& first, Buffer& second) //make std::swap work (ADL)
176 {
177 using std::swap;
178 swap( first.ptr, second.ptr);
179 }
180
181
187 T& data( )const { return *ptr;}
188
189 private:
190 T* ptr;
191};
192
193}//namespace dg
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
a manager class that invokes the copy constructor on the managed ptr when copied (deep copy)
Definition: memory.h:152
Buffer(const T &t)
new T(t)
Definition: memory.h:158
~Buffer()
delete managed object
Definition: memory.h:172
T & data() const
Get write access to the data on the heap.
Definition: memory.h:187
friend void swap(Buffer &first, Buffer &second)
Definition: memory.h:175
Buffer(const Buffer &src)
Definition: memory.h:161
Buffer()
new T
Definition: memory.h:154
Buffer & operator=(Buffer src)
Definition: memory.h:167
Buffer(Buffer &&t)
Definition: memory.h:164
Manager class that invokes the clone() method on the managed ptr when copied.
Definition: memory.h:19
Cloneable * operator->()
Dereference pointer to owned object, i.e. get()
Definition: memory.h:129
void reset(const Cloneable &src)
Clone the given object and replace the currently held one.
Definition: memory.h:108
Cloneable * get()
Get a pointer to the object on the heap.
Definition: memory.h:117
friend void swap(ClonePtr &first, ClonePtr &second)
swap the managed pointers
Definition: memory.h:87
ClonePtr(const Cloneable &src)
clone the given value and manage
Definition: memory.h:35
void reset(Cloneable *ptr)
Replace the managed object.
Definition: memory.h:99
ClonePtr(const ClonePtr &src)
deep copy the given handle using the clone() method of Cloneable
Definition: memory.h:41
const Cloneable * get() const
Get a constant pointer to the object on the heap.
Definition: memory.h:122
ClonePtr(ClonePtr &&src) noexcept
Steal resources (move construct)
Definition: memory.h:57
ClonePtr(Cloneable *ptr)
take ownership of the pointer
Definition: memory.h:26
Cloneable & operator*()
Dereference pointer to owned object, i.e. *get()
Definition: memory.h:125
const Cloneable * operator->() const
Dereference pointer to owned object, i.e. get()
Definition: memory.h:131
ClonePtr(std::nullptr_t value=nullptr)
init an empty ClonePtr
Definition: memory.h:21
ClonePtr & operator=(const ClonePtr &src)
deep copy the given handle using the clone() method of Cloneable
Definition: memory.h:46
const Cloneable & operator*() const
Dereference pointer to owned object, i.e. *get()
Definition: memory.h:127
ClonePtr & operator=(ClonePtr &&src) noexcept
Steal resources (move assignment)
Definition: memory.h:65
Cloneable * release() noexcept
Releases ownership of managed object, get() returns nullptr after call.
Definition: memory.h:103