www.cemf.ir
stdVectorHelper.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #ifndef __stdVectorHelper_hpp__
23 #define __stdVectorHelper_hpp__
24 
25 #include <vector>
26 #include <algorithm>
27 
28 #include "span.hpp"
29 #include "iOstream.hpp"
30 #include "iIstream.hpp"
31 #include "dataIO.hpp"
32 #include "pFlowProcessors.hpp"
33 
34 namespace pFlow
35 {
36 
37 
38 template <class T>
40  : public std::allocator<T>
41 {
42 public:
43  using std::allocator<T>::allocator;
44 
45  template <class U, class... Args> void construct(U*, Args&&...) {}
46 };
47 
48 template<typename T>
49 using vecAllocator = std::allocator<T>;
50 
51 template<typename T>
52 inline
53 span<T> makeSpan(std::vector<T>& container)
54 {
55  return span<T>(container.data(), container.size());
56 }
57 
58 template<typename T>
59 inline
60 span<T> makeSpan(const std::vector<T>& container)
61 {
62  return span<T>(
63  const_cast<T*>(container.data()),
64  container.size());
65 }
66 
67 
68 template<typename T>
69 inline
70 bool writeSpan(
71  iOstream& os,
72  span<T> sp)
73 {
74  return writeDataAsciiBinary(os, sp);
75 }
76 
77 template<typename T>
78 inline
79 bool writeSpan(
80  iOstream& os,
81  span<T> sp,
82  const IOPattern& iop)
83 {
84 
85  auto ioPtr = dataIO<T>::create(iop);
86 
87  if(!ioPtr)
88  {
90  return false;
91  }
92 
93  if(!ioPtr().writeData(os, sp))
94  {
96  return false;
97  }
98  return true;
99 }
100 
101 template<typename T, typename Allocator>
102 bool writeStdVector
103 (
104  iOstream& os,
105  const std::vector<T,Allocator>& vec
106 )
107 {
108  auto sp = makeSpan(vec);
109  return writeSpan(os, sp);
110 }
111 
112 template<typename T, typename Allocator>
113 bool writeStdVector
114 (
115  iOstream& os,
116  const std::vector<T,Allocator>& vec,
117  const IOPattern& iop
118 )
119 {
120  auto sp = makeSpan(vec);
121  return writeSpan(os, sp, iop);
122 }
123 
124 template<typename T, typename Allocator>
125 bool readStdVector
126 (
127  iIstream& is,
128  std::vector<T,Allocator>& vec
129 )
130 {
131 
132  return readDataAsciiBinary(is, vec);
133 
134 }
135 
136 template<typename T, typename Allocator>
137 bool readStdVector
138 (
139  iIstream& is,
140  std::vector<T,Allocator>& vec,
141  const IOPattern& iop
142 )
143 {
144  auto ioPtr = dataIO<T>::create(iop);
145 
146  if(!ioPtr)
147  {
149  return false;
150  }
151 
152  if(!ioPtr().readData(is, vec))
153  {
155  return false;
156  }
157  return true;
158 
159 }
160 
161 template<typename T, typename Allocator>
162 iOstream& operator<<( iOstream& os, const std::vector<T,Allocator>& vec)
163 {
164  if(!writeStdVector(os, vec))
165  {
167  fatalExit;
168  }
169  return os;
170 }
171 
173 template<typename T, typename Allocator>
174 iIstream& operator>>(iIstream& is, std::vector<T,Allocator>& vec)
175 {
176  if( !readStdVector(is,vec))
177  {
179  fatalExit;
180  }
181  return is;
182 }
183 
184 } // pFlow
185 
186 
187 #endif
pFlow::span
Definition: span.hpp:31
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::writeDataAsciiBinary
bool writeDataAsciiBinary(iOstream &os, span< T > data)
Definition: dataIO.cpp:22
pFlow::readStdVector
bool readStdVector(iIstream &is, std::vector< T, Allocator > &vec)
Definition: stdVectorHelper.hpp:126
iIstream.hpp
pFlow::noConstructAllocator::construct
void construct(U *, Args &&...)
Definition: stdVectorHelper.hpp:45
pFlow::dataIO::create
static uniquePtr< dataIO > create(const IOPattern &iop)
Definition: dataIO.cpp:263
pFlow::vecAllocator
std::allocator< T > vecAllocator
Definition: stdVectorHelper.hpp:49
pFlow
Definition: demGeometry.hpp:27
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::readDataAsciiBinary
bool readDataAsciiBinary(iIstream &is, std::vector< T > &data)
Definition: dataIO.cpp:80
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::noConstructAllocator
Definition: stdVectorHelper.hpp:39
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::IOPattern
Definition: IOPattern.hpp:32
pFlow::writeSpan
bool writeSpan(iOstream &os, span< T > sp)
Definition: stdVectorHelper.hpp:70
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
dataIO.hpp
pFlowProcessors.hpp
iOstream.hpp
pFlow::makeSpan
span< T > makeSpan(std::vector< T > &container)
Definition: stdVectorHelper.hpp:53
span.hpp
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::writeStdVector
bool writeStdVector(iOstream &os, const std::vector< T, Allocator > &vec)
Definition: stdVectorHelper.hpp:103