Discontinuous Galerkin Library
#include "dg/algorithm.h"
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1#pragma once
2
3#include <any>
4#include <memory>
5#include <typeindex>
6
7namespace dg
8{
9
10//there is probably a better class in boost...
19template<class Cloneable>
21{
23 ClonePtr() : m_ptr() {}
25 ClonePtr( std::nullptr_t):m_ptr(nullptr){}
30 ClonePtr( Cloneable* ptr): m_ptr(ptr){}
31
39 ClonePtr( const Cloneable& src) : m_ptr( src.clone() ) { }
40
45 ClonePtr( const ClonePtr& src) : m_ptr( src.m_ptr.get() == nullptr ? nullptr : src.m_ptr->clone() ) { }
51 {
52 //copy and swap
53 ClonePtr tmp(src);
54 swap( *this, tmp );
55 return *this;
56 }
61 ClonePtr( ClonePtr&& src) noexcept : m_ptr( nullptr)
62 {
63 swap( *this, src); //steal resource
64 }
69 ClonePtr& operator=( ClonePtr&& src) noexcept
70 {
71 swap( *this, src );
72 return *this;
73 }
91 friend void swap( ClonePtr& first, ClonePtr& second)
92 {
93 std::swap(first.m_ptr,second.m_ptr);
94 }
95
103 void reset( Cloneable* ptr){
104 m_ptr.reset( ptr);
105 }
107 Cloneable* release() noexcept { m_ptr.release();}
112 void reset( const Cloneable& src){
113 ClonePtr tmp(src);
114 swap(*this, tmp);
115 }
116
121 Cloneable * get() {return m_ptr.get();}
126 const Cloneable* get() const {return m_ptr.get();}
127
129 Cloneable& operator*() { return *m_ptr;}
131 const Cloneable& operator*() const { return *m_ptr;}
133 Cloneable* operator->() { return m_ptr.operator->();}
135 const Cloneable* operator->()const { return m_ptr.operator->();}
137 explicit operator bool() const{ return (bool)m_ptr;}
138
139
140 private:
141 std::unique_ptr<Cloneable> m_ptr;
142};
143
145namespace detail
146{
147
149//should not be public (because of the const behaviour, which is a dirty trick...)
150template<template<typename> typename Vector>
151struct AnyVector
152{
153 AnyVector( ) : m_type( typeid( void)){}
154
155 // If not allocated or wrong type; change size and type
156 // May need to be called using any_vec.template set<value_type>(size)
157 template<class value_type>
158 void set(unsigned size)
159 {
160 auto type_idx = std::type_index( typeid( value_type));
161 if( type_idx != m_type)
162 {
163 m_vec.emplace<Vector<value_type>>(size);
164 m_type = type_idx;
165 }
166 else
167 {
168 auto ptr = std::any_cast<Vector<value_type>>(
169 &m_vec);
170 ptr->resize( size);
171 }
172 }
173 // If you think you need this fct. think again, std::vector e.g. will not release
174 // memory on resize unless the size is bigger
175 //template<class value_type>
176 //void set_at_least( unsigned size);
177 template<class value_type>
178 void swap ( Vector<value_type>& src)
179 {
180 auto type_idx = std::type_index( typeid( value_type));
181 if( type_idx != m_type)
182 {
183 m_vec.emplace< Vector<value_type>>(std::move(src));
184 m_type = type_idx;
185 }
186 else
187 {
188 auto& vec = std::any_cast<Vector<value_type>&>( m_vec);
189 src.swap(vec);
190 }
191 }
192 // Get read access to underlying buffer
193 // May need to be called using any_vec.template get<value_type>()
194 template<class value_type>
195 const Vector<value_type>& get( ) const
196 {
197 // throws if not previously set
198 return std::any_cast<const Vector<value_type>&>(
199 m_vec);
200 }
201 template<class value_type>
202 Vector<value_type>& get( )
203 {
204 // throws if not previously set
205 return std::any_cast<Vector<value_type>&>(
206 m_vec);
207 }
208 private:
209 //std::unordered_map< std::type_index, Buffer<std::any>> m_vec;
210 std::any m_vec;
211 std::type_index m_type;
212};
213
214}//namespace detail
216
217}//namespace dg
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Manager class that invokes the clone() method on the managed ptr when copied.
Definition memory.h:21
Cloneable * operator->()
Dereference pointer to owned object, i.e. get()
Definition memory.h:133
void reset(const Cloneable &src)
Clone the given object and replace the currently held one.
Definition memory.h:112
Cloneable * get()
Get a pointer to the object on the heap.
Definition memory.h:121
friend void swap(ClonePtr &first, ClonePtr &second)
swap the managed pointers
Definition memory.h:91
ClonePtr(const Cloneable &src)
clone the given value and manage
Definition memory.h:39
ClonePtr()
init an empty ClonePtr
Definition memory.h:23
void reset(Cloneable *ptr)
Replace the managed object.
Definition memory.h:103
ClonePtr(const ClonePtr &src)
deep copy the given handle using the clone() method of Cloneable
Definition memory.h:45
const Cloneable * get() const
Get a constant pointer to the object on the heap.
Definition memory.h:126
ClonePtr(ClonePtr &&src) noexcept
Steal resources (move construct)
Definition memory.h:61
ClonePtr(Cloneable *ptr)
take ownership of the pointer
Definition memory.h:30
Cloneable & operator*()
Dereference pointer to owned object, i.e. *get()
Definition memory.h:129
const Cloneable * operator->() const
Dereference pointer to owned object, i.e. get()
Definition memory.h:135
ClonePtr(std::nullptr_t)
init an empty ClonePtr
Definition memory.h:25
ClonePtr & operator=(const ClonePtr &src)
deep copy the given handle using the clone() method of Cloneable
Definition memory.h:50
const Cloneable & operator*() const
Dereference pointer to owned object, i.e. *get()
Definition memory.h:131
ClonePtr & operator=(ClonePtr &&src) noexcept
Steal resources (move assignment)
Definition memory.h:69
Cloneable * release() noexcept
Releases ownership of managed object, get() returns nullptr after call.
Definition memory.h:107
double value_type