WARPXM v1.10.0
Loading...
Searching...
No Matches
wxiobase.h
Go to the documentation of this file.
1#ifndef wxiobase_h
2#define wxiobase_h
3
4// WarpX includes
5#include "wxiotmpl.h"
6#include "wxexcept.h"
7
8#include "lib/wmopaqueptr.h" // TODO: not required here if I move implementation to CC file
9
10// std includes
11#include <vector>
12#include <typeinfo>
13#include <list>
14#include <string>
15#include <exception>
16
25{
26
27public:
31 virtual ~WxIoBase();
32
38 void setBaseName(const std::string& bn)
39 {
40 baseName = bn;
41 }
42
48 std::string getBaseName()
49 {
50 return baseName;
51 }
52
58 void setDumpNo(int d)
59 {
60 dumpNo = d;
61 }
62
69 {
70 return dumpNo;
71 }
72
80 virtual WxIoNodeType createFile(const std::string& fileName) = 0;
81
88 virtual WxIoNodeType createDumpFile(const std::string& dataName);
89
97 virtual WxIoNodeType openFile(const std::string& fileName,
98 const std::string& perms) = 0;
99
108 virtual WxIoNodeType openDumpFile(const std::string& dataName,
109 const std::string& perms);
110
114 virtual void closeFile(WxIoNodeType fileNode) = 0;
115
119 virtual std::string getDumpFileName(const std::string& dataName);
120
130 const std::string& dataName) const = 0;
131
141 const std::string& dataName) const = 0;
142
150 virtual std::list<std::string> getGroupNames(WxIoNodeType node) const = 0;
151
162 const std::string& name,
163 const WxRange& range,
164 const std::type_info& typeinfo) const = 0;
165
175 const std::string& dataName) const = 0;
176
186 const std::string& dataName) const = 0;
187
203 template<typename T>
206 const std::string& dataName,
207 const std::vector<std::pair<int, int>>& fileSpaceCorners,
208 const std::vector<std::pair<int, int>>& memorySpaceCorners,
209 const std::vector<std::pair<int, int>>& writeSelectionCorners,
210 const T* data) const
211 {
212 WmConstOpaquePtr dptr(data);
213 return getIoPtr()->writeDataSet(node,
214 dataName,
215 fileSpaceCorners,
216 memorySpaceCorners,
217 writeSelectionCorners,
218 dptr);
219 }
220
233 template<typename T>
235 const WxRange& memoryRange,
236 const WxRange& memorySubRange,
237 const WxRange& fileRange,
238 const WxRange& fileSubRange,
239 const T* data) const
240 {
241 WmConstOpaquePtr dptr(data);
242 getIoPtr()->writeToDataSet(
243 node, memoryRange, memorySubRange, fileRange, fileSubRange, dptr);
244 }
245
260 template<typename T>
262 const std::string& dataName,
263 const std::vector<std::pair<int, int>>& memorySpaceCorners,
264 const std::vector<std::pair<int, int>>& readSelectionCorners,
265 T* data) const
266 {
267 WmOpaquePtr dptr(data);
268 return getIoPtr()->readDataSet(
269 node, dataName, memorySpaceCorners, readSelectionCorners, dptr);
270 }
271
284 template<typename T>
286 const WxRange& memoryRange,
287 const WxRange& memorySubRange,
288 const WxRange& fileRange,
289 const WxRange& fileSubRange,
290 T* data) const
291 {
292 WmOpaquePtr dptr(data);
293 getIoPtr()->readFromDataSet(
294 node, memoryRange, memorySubRange, fileRange, fileSubRange, dptr);
295 }
296
307 const std::string& dataSetName,
308 WxRange& dataShape) const
309 {
310 return getIoPtr()->getDataShape(node, dataSetName, dataShape);
311 }
312
318 virtual void closeNode(WxIoNodeType node) const = 0;
319
327 template<typename T>
328 void writeAttribute(WxIoNodeType node, const std::string& attribName, const T* attrib)
329 {
330 WmConstOpaquePtr aptr(attrib);
331 getIoPtr()->writeAttribute(node, attribName, aptr);
332 }
333
341 template<typename T>
343 const std::string& attribName,
344 const std::vector<T>& attrib)
345 {
346 writeVecAttribute<T>(node, attribName, &(attrib[0]), attrib.size());
347 }
348
357 template<typename T>
359 const std::string& attribName,
360 const T* attribStorage,
361 int count)
362 {
363 WmConstOpaquePtr aptr(attribStorage);
364 getIoPtr()->writeVecAttribute(node, attribName, aptr, count);
365 }
366
367 // Characters when meant to represent readable strings are stored a bit differently.
368 // TODO: rather than special Str functions, make a specific template implementation at
369 // the hdf5 level.
378 const std::string& attribName,
379 const std::string& attrib) const = 0;
380
388 virtual void
390 const std::string& attribName,
391 const std::vector<std::string>& stringsvector) const = 0;
392
401 const std::string& attribName,
402 std::string& attrib) const = 0;
403
407 struct AtttributeNotFoundException : std::exception
408 {
409 const char* what() const noexcept
410 {
411 return "Attribute Not Found.\n";
412 }
413 };
414
424 virtual std::vector<std::string>
425 readStrVecAttribute(WxIoNodeType node, const std::string& attribName) const = 0;
426
434 template<typename T>
435 void readAttribute(WxIoNodeType node, const std::string& attribName, T* attrib)
436 {
437 WmOpaquePtr aptr(attrib);
438 getIoPtr()->readAttribute(node, attribName, aptr);
439 }
440
451 template<typename T>
453 const std::string& attribName,
454 T* attribStorage,
455 int count)
456 {
457 WmOpaquePtr aptr(attribStorage);
458 return getIoPtr()->readVecAttribute(node, attribName, aptr, count);
459 }
460
461protected:
467
474 WxIoBase(const std::string& sfx);
475
483 WxIoBase(const std::string& bn, int d, const std::string& sfx);
484
490 virtual void addOpenFile(WxIoNodeType node);
491
497 virtual void removeOpenFile(WxIoNodeType node);
498
502 virtual void closeOpenFiles();
503
509 void setIo(const WxIoTmpl* b)
510 {
511 implementationPtr = b;
512 }
513
514 std::string suffix; // suffix for output
515
516private:
518 WxIoBase(const WxIoBase&);
519
521 WxIoBase& operator=(const WxIoBase&);
522
526 const WxIoTmpl* getIoPtr() const
527 {
528 if (implementationPtr)
529 return implementationPtr;
530 WxExcept wxe;
531 wxe << "I/O implementation not set properly";
532 throw wxe;
533 }
534
535 std::vector<WxIoNodeType> openFiles; // List of all open files
536 std::string baseName; // base name for output
537 int dumpNo; // dump number
538
539 const WxIoTmpl* implementationPtr;
540};
541
543#endif // wxiobase_h
Opaque Pointer object that carries the referenced data type, but does not expose this payload type as...
Definition: wmopaqueptr.h:81
Opaque Pointer object that carries the referenced data type, but does not expose this payload type as...
Definition: wmopaqueptr.h:24
Provides an abstract interface for access to hierachical datasets.
Definition: wxiobase.h:25
void writeVecAttribute(WxIoNodeType node, const std::string &attribName, const std::vector< T > &attrib)
Write an attribute (vector).
Definition: wxiobase.h:342
virtual void closeNode(WxIoNodeType node) const =0
Close a data set.
virtual void addOpenFile(WxIoNodeType node)
Add a file to the list of open files.
virtual WxIoNodeType openFile(const std::string &fileName, const std::string &perms)=0
Open a file.
virtual ~WxIoBase()
Virtual destructor.
void setBaseName(const std::string &bn)
Set the base name.
Definition: wxiobase.h:38
virtual std::list< std::string > getGroupNames(WxIoNodeType node) const =0
Get list of group names under the supplied node.
virtual WxIoNodeType openDumpFile(const std::string &dataName, const std::string &perms)
Open a file with the base name prepended and the dump and suffix appended.
virtual std::string getDumpFileName(const std::string &dataName)
Get the dump name for a given data name.
WxIoNodeType getDataShape(WxIoNodeType node, const std::string &dataSetName, WxRange &dataShape) const
Obtain simple data shape for a data set under a node.
Definition: wxiobase.h:306
WxIoBase(const std::string &sfx)
Constructor is protected, as this class cannot be made standalone.
void writeToDataSet(WxIoNodeType node, const WxRange &memoryRange, const WxRange &memorySubRange, const WxRange &fileRange, const WxRange &fileSubRange, const T *data) const
Write to an open dataset (independent)
Definition: wxiobase.h:234
virtual void removeOpenFile(WxIoNodeType node)
Remove a file from the list of open files.
virtual WxIoNodeType createDataSet(WxIoNodeType node, const std::string &dataName) const =0
Create an empty node.
WxIoNodeType writeDataSet(WxIoNodeType node, const std::string &dataName, const std::vector< std::pair< int, int > > &fileSpaceCorners, const std::vector< std::pair< int, int > > &memorySpaceCorners, const std::vector< std::pair< int, int > > &writeSelectionCorners, const T *data) const
Write a new data set under a node.
Definition: wxiobase.h:205
WxIoBase(const std::string &bn, int d, const std::string &sfx)
Constructor is protected, as this class cannot be made standalone.
virtual WxIoNodeType createFile(const std::string &fileName)=0
Create a file.
virtual std::vector< std::string > readStrVecAttribute(WxIoNodeType node, const std::string &attribName) const =0
Read a vector of strings attribute.
virtual WxIoNodeType createDataSet(WxIoNodeType node, const std::string &name, const WxRange &range, const std::type_info &typeinfo) const =0
Collective dataset allocation.
virtual void closeFile(WxIoNodeType fileNode)=0
Close a file node.
virtual void writeStrVecAttribute(WxIoNodeType node, const std::string &attribName, const std::vector< std::string > &stringsvector) const =0
Write a vector of strings as attribute.
virtual WxIoNodeType openDataSet(WxIoNodeType node, const std::string &dataName) const =0
Open a node.
void readFromDataSet(WxIoNodeType node, const WxRange &memoryRange, const WxRange &memorySubRange, const WxRange &fileRange, const WxRange &fileSubRange, T *data) const
Read from an open dataset (independent)
Definition: wxiobase.h:285
virtual void readStrAttribute(WxIoNodeType node, const std::string &attribName, std::string &attrib) const =0
Read a string attribute.
std::string getBaseName()
Get the base name.
Definition: wxiobase.h:48
int getDumpNo()
Get the dump number.
Definition: wxiobase.h:68
void writeAttribute(WxIoNodeType node, const std::string &attribName, const T *attrib)
Write an attribute.
Definition: wxiobase.h:328
int readVecAttribute(WxIoNodeType node, const std::string &attribName, T *attribStorage, int count)
Read a vector attribute.
Definition: wxiobase.h:452
virtual void closeOpenFiles()
Close any files currently open.
virtual WxIoNodeType openGroup(WxIoNodeType node, const std::string &dataName) const =0
Open a group.
void readAttribute(WxIoNodeType node, const std::string &attribName, T *attrib)
Read an attribute.
Definition: wxiobase.h:435
void setDumpNo(int d)
Set the dump number.
Definition: wxiobase.h:58
WxIoNodeType readDataSet(WxIoNodeType node, const std::string &dataName, const std::vector< std::pair< int, int > > &memorySpaceCorners, const std::vector< std::pair< int, int > > &readSelectionCorners, T *data) const
Read a new data set under a node.
Definition: wxiobase.h:261
std::string suffix
Definition: wxiobase.h:514
WxIoBase()
Default constructor - no dump, basename or suffix.
void setIo(const WxIoTmpl *b)
Sets the new io object implementation.
Definition: wxiobase.h:509
virtual WxIoNodeType createGroup(WxIoNodeType node, const std::string &dataName) const =0
Create an empty group.
virtual void writeStrAttribute(WxIoNodeType node, const std::string &attribName, const std::string &attrib) const =0
Write a string attribute.
virtual WxIoNodeType createDumpFile(const std::string &dataName)
Create a file with base name prepended and dump and suffix appended.
void writeVecAttribute(WxIoNodeType node, const std::string &attribName, const T *attribStorage, int count)
Write a vector of attributes.
Definition: wxiobase.h:358
WxIoTmpl is the base class for access to a hierarchical file system with groups, data sets,...
Definition: wxiotmpl.h:57
virtual WxIoNodeType writeDataSet(WxIoNodeType node, const std::string &dataName, const std::vector< std::pair< int, int > > &fileSpaceCorners, const std::vector< std::pair< int, int > > &memorySpaceCorners, const std::vector< std::pair< int, int > > &writeSelectionCorners, const WmConstOpaquePtr &data) const =0
Write a new data set under a node.
virtual WxIoNodeType getDataShape(WxIoNodeType node, const std::string &dataSetName, WxRange &dataShape) const =0
Obtain simple data shape for a data set under a node.
virtual void writeToDataSet(WxIoNodeType node, const WxRange &memoryRange, const WxRange &memorySubRange, const WxRange &fileRange, const WxRange &fileSubRange, const WmConstOpaquePtr &data) const =0
Write to an open dataset (independent)
virtual void readFromDataSet(WxIoNodeType node, const WxRange &memoryRange, const WxRange &memorySubRange, const WxRange &fileRange, const WxRange &fileSubRange, WmOpaquePtr &data) const =0
Read from an open dataset (independent)
virtual WxIoNodeType readDataSet(WxIoNodeType node, const std::string &dataName, const std::vector< std::pair< int, int > > &memorySpaceCorners, const std::vector< std::pair< int, int > > &readSelectionCorners, WmOpaquePtr &data) const =0
Read a new data set under a node.
virtual void writeAttribute(WxIoNodeType node, const std::string &attribName, const WmConstOpaquePtr &attrib) const =0
Write an attribute.
virtual void readAttribute(WxIoNodeType node, const std::string &attribName, WmOpaquePtr &attrib) const =0
Read an attribute.
virtual int readVecAttribute(WxIoNodeType node, const std::string &attribName, WmOpaquePtr &attribStorage, int count) const =0
Read a vector attribute.
virtual void writeVecAttribute(WxIoNodeType node, const std::string &attribName, const WmConstOpaquePtr &attribStorage, int count) const =0
Write a vector attribute.
WxRange represents a hyper-rectangular domain of an n-dimensional space of integers.
Definition: wxrange.h:23
wxm::lib::Except is the class to use for creating and throwing exceptions.
Definition: wxexcept.h:31
Exception class for reporting when an attribute does not exist.
Definition: wxiobase.h:408
const char * what() const noexcept
Definition: wxiobase.h:409
Provides a means for derived messengers to return implimentation specific message status flags and da...
Definition: wxiotmpl.h:23