openPMD-api
Memory.hpp
1 /* Copyright 2017-2020 Fabian Koller
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openPMD-api 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 General Public License and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 #pragma once
22 
23 #include "openPMD/Dataset.hpp"
24 #include "openPMD/Datatype.hpp"
25 
26 #include <functional>
27 #include <memory>
28 #include <utility>
29 
30 
31 namespace openPMD
32 {
33 namespace auxiliary
34 {
35 std::unique_ptr< void, std::function< void(void*) > >
36 allocatePtr(Datatype dtype, Extent const& e);
37 
38 std::unique_ptr< void, std::function< void(void*) > >
39 allocatePtr(Datatype dtype, uint64_t numPoints);
40 
41 inline std::unique_ptr< void, std::function< void(void*) > >
42 allocatePtr(Datatype dtype, Extent const& e)
43 {
44  uint64_t numPoints = 1u;
45  for( auto const& dimensionSize : e )
46  numPoints *= dimensionSize;
47  return allocatePtr(dtype, numPoints);
48 }
49 
50 inline std::unique_ptr< void, std::function< void(void*) > >
51 allocatePtr(Datatype dtype, uint64_t numPoints)
52 {
53  void* data = nullptr;
54  std::function< void(void*) > del = [](void*){};
55  switch( dtype )
56  {
57  using DT = Datatype;
58  case DT::VEC_STRING:
59  data = new char*[numPoints];
60  del = [](void* p){ delete[] static_cast< char** >(p); };
61  break;
62  case DT::VEC_LONG_DOUBLE:
63  case DT::LONG_DOUBLE:
64  data = new long double[numPoints];
65  del = [](void* p){ delete[] static_cast< long double* >(p); };
66  break;
67  case DT::ARR_DBL_7:
68  case DT::VEC_DOUBLE:
69  case DT::DOUBLE:
70  data = new double[numPoints];
71  del = [](void* p){ delete[] static_cast< double* >(p); };
72  break;
73  case DT::VEC_FLOAT:
74  case DT::FLOAT:
75  data = new float[numPoints];
76  del = [](void* p){ delete[] static_cast< float* >(p); };
77  break;
78  case DT::VEC_SHORT:
79  case DT::SHORT:
80  data = new short[numPoints];
81  del = [](void* p){ delete[] static_cast< short* >(p); };
82  break;
83  case DT::VEC_INT:
84  case DT::INT:
85  data = new int[numPoints];
86  del = [](void* p){ delete[] static_cast< int* >(p); };
87  break;
88  case DT::VEC_LONG:
89  case DT::LONG:
90  data = new long[numPoints];
91  del = [](void* p){ delete[] static_cast< long* >(p); };
92  break;
93  case DT::VEC_LONGLONG:
94  case DT::LONGLONG:
95  data = new long long[numPoints];
96  del = [](void* p){ delete[] static_cast< long long* >(p); };
97  break;
98  case DT::VEC_USHORT:
99  case DT::USHORT:
100  data = new unsigned short[numPoints];
101  del = [](void* p){ delete[] static_cast< unsigned short* >(p); };
102  break;
103  case DT::VEC_UINT:
104  case DT::UINT:
105  data = new unsigned int[numPoints];
106  del = [](void* p){ delete[] static_cast< unsigned int* >(p); };
107  break;
108  case DT::VEC_ULONG:
109  case DT::ULONG:
110  data = new unsigned long[numPoints];
111  del = [](void* p){ delete[] static_cast< unsigned long* >(p); };
112  break;
113  case DT::VEC_ULONGLONG:
114  case DT::ULONGLONG:
115  data = new unsigned long long[numPoints];
116  del = [](void* p){ delete[] static_cast< unsigned long long* >(p); };
117  break;
118  case DT::VEC_CHAR:
119  case DT::CHAR:
120  data = new char[numPoints];
121  del = [](void* p){ delete[] static_cast< char* >(p); };
122  break;
123  case DT::VEC_UCHAR:
124  case DT::UCHAR:
125  data = new unsigned char[numPoints];
126  del = [](void* p){ delete[] static_cast< unsigned char* >(p); };
127  break;
128  case DT::BOOL:
129  data = new bool[numPoints];
130  del = [](void* p){ delete[] static_cast< bool* >(p); };
131  break;
132  case DT::STRING:
133  /* user assigns c_str pointer */
134  break;
135  case DT::UNDEFINED:
136  default:
137  throw std::runtime_error("Unknown Attribute datatype (Pointer allocation)");
138  }
139 
140  return std::unique_ptr< void, std::function< void(void*) > >(data, del);
141 }
142 } // auxiliary
143 } // openPMD
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:38
Public definitions of openPMD-api.
Definition: Date.cpp:28