Shiokaze Framework
A research-oriented fluid solver for computer graphics
shared_array2.h
Go to the documentation of this file.
1 /*
2 ** shared_array2.h
3 **
4 ** This is part of Shiokaze, a research-oriented fluid solver for computer graphics.
5 ** Created by Ryoichi Ando <rand@nii.ac.jp> on March 15, 2018.
6 **
7 ** Permission is hereby granted, free of charge, to any person obtaining a copy of
8 ** this software and associated documentation files (the "Software"), to deal in
9 ** the Software without restriction, including without limitation the rights to use,
10 ** copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
11 ** Software, and to permit persons to whom the Software is furnished to do so,
12 ** subject to the following conditions:
13 **
14 ** The above copyright notice and this permission notice shall be included in all copies
15 ** or substantial portions of the Software.
16 **
17 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18 ** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19 ** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 ** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 ** CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22 ** OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 //
25 #ifndef SHKZ_SHARED_ARRAY2_H
26 #define SHKZ_SHARED_ARRAY2_H
27 //
28 #include <typeinfo>
29 #include <cassert>
30 //
31 #include "shared_array_core2.h"
32 #include "array2.h"
33 #include "macarray2.h"
34 //
36 //
38 template<class T> class shared_array2 {
41 public:
52  shared_array2( const shape2 &shape, T initial_value=T(), std::string core_name="" ) {
53  m_array = reinterpret_cast<array2<T> *>(shared_array_core2::borrow_shared(shape,typeid(array2<T>).hash_code(),core_name,
54  []( const shape2 &shape, std::string core_name ) {
56  credit("Shared Array 2D","SharedArray"));
57  return (void *)(new array2<T>(shape,T(),core_name));
58  },
59  []( void *ptr ) {
60  delete reinterpret_cast<array2<T> *>(ptr);
61  }));
62  assert( m_array->shape() == shape );
63  m_array->clear(initial_value);
64  }
73  shared_array2( const typename array2<T>::type2 &type, T initial_value=T() ) : shared_array2(type.shape,initial_value,type.core_name) {
74  m_array->set_type(type);
75  }
82  shared_array2( const array2<T> &array ) : shared_array2(array.type()) {
83  m_array->copy(array);
84  }
90  m_array->clear();
92  }
97  const array2<T>& operator()() const { return *m_array; }
102  array2<T>& operator()() { return *m_array; }
107  array2<T>* operator->() { return get(); }
112  const array2<T>* operator->() const { return get(); }
117  array2<T>* get() { return m_array; }
122  const array2<T>* get() const { return m_array; }
123  //
124 private:
125  array2<T> *m_array;
126 };
127 //
130 template<class T> class shared_macarray2 {
131 public:
142  shared_macarray2( const shape2 &shape, vec2<T> initial_value=vec2<T>(), std::string core_name="" ) {
143  m_array = reinterpret_cast<macarray2<T> *>(shared_array_core2::borrow_shared(shape,typeid(macarray2<T>).hash_code(),core_name,
144  []( const shape2 &shape, std::string core_name ) {
146  credit("Shared MAC Array 2D","SharedMACArray"));
147  return (void *)(new macarray2<T>(shape,T(),core_name));
148  },
149  []( void *ptr ) {
150  delete reinterpret_cast<macarray2<T> *>(ptr);
151  }));
152  assert( m_array->shape() == shape );
153  m_array->clear(initial_value);
154  }
163  shared_macarray2( const typename macarray2<T>::type2 &type, vec2<T> initial_value=vec2<T>() ) : shared_macarray2(type.shape,initial_value,type.core_name) {
164  m_array->set_type(type);
165  }
172  shared_macarray2( const macarray2<T> &array ) : shared_macarray2(array.type()) {
173  m_array->copy(array);
174  }
180  m_array->clear();
182  }
187  const macarray2<T>& operator()() const { return *m_array; }
192  macarray2<T>& operator()() { return *m_array; }
197  macarray2<T>* operator->() { return get(); }
202  const macarray2<T>* operator->() const { return get(); }
207  macarray2<T>* get() { return m_array; }
212  const macarray2<T>* get() const { return m_array; }
213  //
214 private:
215  macarray2<T> *m_array;
216 };
217 //
219 //
220 #endif
221 //
shared_macarray2::shared_macarray2
shared_macarray2(const macarray2< T > &array)
Borrow a shared array and copy the input.
Definition: shared_array2.h:172
shared_array2::operator->
array2< T > * operator->()
Get the pointer to the internal borrowed array.
Definition: shared_array2.h:107
shared_macarray2::get
const macarray2< T > * get() const
Get the const pointer to the internal borrowed array.
Definition: shared_array2.h:212
macarray2.h
array2.h
shared_array2::shared_array2
shared_array2(const shape2 &shape, T initial_value=T(), std::string core_name="")
Borrow a shared array.
Definition: shared_array2.h:52
shared_macarray2::get
macarray2< T > * get()
Get the pointer to the internal borrowed array.
Definition: shared_array2.h:207
shared_macarray2::shared_macarray2
shared_macarray2(const shape2 &shape, vec2< T > initial_value=vec2< T >(), std::string core_name="")
Borrow a shared MAC array.
Definition: shared_array2.h:142
shared_array2::get
array2< T > * get()
Get the pointer to the internal borrowed array.
Definition: shared_array2.h:117
shared_array2::operator()
const array2< T > & operator()() const
Get the const reference to the internal borrowed array.
Definition: shared_array2.h:97
macarray2::type2
Collection of properties of this grid.
Definition: macarray2.h:1037
shared_array2::shared_array2
shared_array2(const typename array2< T >::type2 &type, T initial_value=T())
Borrow a shared array.
Definition: shared_array2.h:73
array2::type2
Collection of properties of this grid.
Definition: array2.h:1797
shared_macarray2::shared_macarray2
shared_macarray2(const typename macarray2< T >::type2 &type, vec2< T > initial_value=vec2< T >())
Borrow a shared array.
Definition: shared_array2.h:163
shared_array_core2::borrow_shared
static void * borrow_shared(const shape2 &shape, size_t class_hash, std::string core_name, std::function< void *(const shape2 &shape, std::string core_name)> alloc_func, std::function< void(void *ptr)> dealloc_func)
Borrow a shared array.
shared_array2::get
const array2< T > * get() const
Get the const pointer to the internal borrowed array.
Definition: shared_array2.h:122
shared_array2::operator()
array2< T > & operator()()
Get the reference to the internal borrowed array.
Definition: shared_array2.h:102
SHKZ_BEGIN_NAMESPACE
#define SHKZ_BEGIN_NAMESPACE
Name space beggining definition for shiokaze.
Definition: common.h:39
shared_macarray2::operator->
macarray2< T > * operator->()
Get the pointer to the internal borrowed array.
Definition: shared_array2.h:197
shared_array2::operator->
const array2< T > * operator->() const
Get the const pointer to the internal borrowed array.
Definition: shared_array2.h:112
shared_array2::~shared_array2
~shared_array2()
Destructor for shared_array2.
Definition: shared_array2.h:89
shared_array_core2.h
shared_macarray2::~shared_macarray2
~shared_macarray2()
Destructor for shared_macarray2.
Definition: shared_array2.h:179
configurable::get_global_configuration
static configuration & get_global_configuration()
Get the global settings of the program.
shared_array2::shared_array2
shared_array2(const array2< T > &array)
Borrow a shared array and copy the input.
Definition: shared_array2.h:82
shared_macarray2::operator()
const macarray2< T > & operator()() const
Get the const reference to the internal borrowed array.
Definition: shared_array2.h:187
shared_macarray2::operator->
const macarray2< T > * operator->() const
Get the const pointer to the internal borrowed array.
Definition: shared_array2.h:202
configuration::auto_group
Class that automates the push and pop groups.
Definition: configuration.h:107
shared_macarray2::operator()
macarray2< T > & operator()()
Get the reference to the internal borrowed array.
Definition: shared_array2.h:192
vec
Fixed sized vector structure.
Definition: vec.h:38
shared_array2
Storage class that enables sharing pre-allocated arrays.
Definition: shared_array2.h:40
array2
Two dimensional array class designed to be defined as instance member in recursive_configurable class...
Definition: array2.h:42
credit
Class that defines the name, argument name, author's name, email address, date and the version of the...
Definition: credit.h:47
shared_array_core2::return_shared
static void return_shared(void *array)
Return a borrowed array.
shape2
Structure that defines shape such as width, height.
Definition: shape.h:42
SHKZ_END_NAMESPACE
#define SHKZ_END_NAMESPACE
Name space end definition for shiokaze.
Definition: common.h:44
macarray2
Two dimensional staggered grid class designed to be defined as instance member in recursive_configura...
Definition: macarray2.h:37
shared_macarray2
Storage class that enables sharing pre-allocated arrays for MAC grid.
Definition: shared_array2.h:130