LRAUV  revA
BlobReader.h
Go to the documentation of this file.
1 
10 #ifndef BLOBREADER_H_
11 #define BLOBREADER_H_
12 
13 #include "BlobValue.h"
14 #include "DataReader.h"
15 #include "I1D.h"
16 #include "I2D.h"
17 #include "I3D.h"
18 
19 #include <iostream> // for cerr, since there is no logger_ in this class, but readers should not fail silently (or close to silently)
20 
22 {
23 
33 public:
34 
36  {}
37 
38  // Parent method used in AbstractBlobReader: needs to be re-implemented in child classes
39  virtual bool read( DataValue& readTo ) = 0;
40 
41  // Parent method used in AbstractBlobReader: needs to be re-implemented in child classes
42  virtual const ElementURI& getUri( void ) const = 0;
43 
44 
45  BlobValue readBlob( bool& success )
46  {
47  success = read( blobValue_ );
49  }
50 
51  // Read into the 1-d array. Returns the number of elements written, 0 if an error.
52  template<typename T, size_t M>
53  int read1DArray( const Unit& unit, T( &x )[M] )
54  {
55  if( getUri().getDimensions() != 1 )
56  {
57  std::cerr << "URI " << Str( getUri() ).cStr() << " is not 1D." << std::endl;
58  return 0;
59  }
60  else if( getUri().getDimension1() != M )
61  {
62  std::cerr << "URI " << Str( getUri() ).cStr() << " expects " << getUri().getDimension1() << ", but received " << M << std::endl;
63  return 0;
64  }
65  return read1DPtr( unit, ( T* )&x, M );
66  }
67 
68  // Read into the 1-d class that inherits from the interface I1D.
69  template<typename T>
70  int read1DClass( const Unit& unit, I1D<T>& i1d )
71  {
72  return read1DPtr( unit, i1d.getPtr1d(), i1d.getM() );
73  }
74 
75  // Read into the 1-d pointer. Returns the number of elements written, 0 if an error.
76  template<typename T>
77  int read1DPtr( const Unit& unit, T* values, int m )
78  {
79  if( getUri().getDimensions() != 1 )
80  {
81  std::cerr << "URI " << Str( getUri() ).cStr() << " is not 1D." << std::endl;
82  return 0;
83  }
84  size_t elements = read( blobValue_ ) ? MIN( m, ( int )blobValue_.getElements() ) : 0;
85  if( elements > 0 )
86  {
87  blobValue_.copyTo( unit, values, m );
88  }
89  return elements;
90  }
91 
92  // Read into the 2-d array. Returns the number of elements written, 0 if an error.
93  template<typename T, size_t M, size_t N>
94  int read2DArray( const Unit& unit, T( &x )[M][N] )
95  {
96  if( getUri().getDimensions() != 2 )
97  {
98  std::cerr << "URI " << Str( getUri() ).cStr() << " is not 2D." << std::endl;
99  return 0;
100  }
101  else if( getUri().getDimension1() != M || getUri().getDimension2() != N )
102  {
103  std::cerr << "URI " << Str( getUri() ).cStr() << " expects " << getUri().getDimension1() << "-by-" << getUri().getDimension2() << ", but received " << M << "-by-" << N << std::endl;
104  return 0;
105  }
106  size_t elements = read( blobValue_ ) ? MIN( M * N, blobValue_.getElements() ) : 0;
107  if( elements > 0 )
108  {
109  blobValue_.copyTo( unit, x );
110  }
111  return elements;
112  }
113 
114  // Read into the 2-d class that inherits from the interface I2D.
115  template<typename T>
116  int read2DClass( const Unit& unit, I2D<T>& i2d )
117  {
118  return read2DPtr( unit, i2d.getPtr2d(), i2d.getM(), i2d.getN() );
119  }
120 
121  // Read into the 2-d pointer. Returns the number of elements written, 0 if an error.
122  template<typename T>
123  int read2DPtr( const Unit& unit, T** values, int m, int n )
124  {
125  if( getUri().getDimensions() != 2 )
126  {
127  std::cerr << "URI " << Str( getUri() ).cStr() << " is not 2D." << std::endl;
128  return 0;
129  }
130  else if( getUri().getDimension1() != m || getUri().getDimension2() != n )
131  {
132  std::cerr << "URI " << Str( getUri() ).cStr() << " expects " << getUri().getDimension1() << "-by-" << getUri().getDimension2() << ", but received " << m << "-by-" << n << std::endl;
133  return 0;
134  }
135  size_t elements = read( blobValue_ ) ? MIN( m * n, ( int )blobValue_.getElements() ) : 0;
136  if( elements > 0 )
137  {
138  blobValue_.copyTo( unit, values, elements / n, n );
139  }
140  return elements;
141  }
142 
143  // Read into the 3-d array. Returns the number of elements written, 0 if an error.
144  template<typename T, size_t M, size_t N, size_t O>
145  int read3DArray( const Unit& unit, T( &x )[M][N][O] )
146  {
147  if( getUri().getDimensions() != 3 )
148  {
149  std::cerr << "URI " << Str( getUri() ).cStr() << " is not 3D." << std::endl;
150  return 0;
151  }
152  else if( getUri().getDimension1() != M || getUri().getDimension2() != N || getUri().getDimension3() != O )
153  {
154  std::cerr << "URI " << Str( getUri() ).cStr() << " expects " << getUri().getDimension1() << "-by-" << getUri().getDimension2() << "-by-" << getUri().getDimension3() << ", but received " << M << "-by-" << N << "-by-" << O << std::endl;
155  return 0;
156  }
157  size_t elements = read( blobValue_ ) ? MIN( M * N * O, blobValue_.getElements() ) : 0;
158  if( elements > 0 )
159  {
160  blobValue_.copyTo( unit, x );
161  }
162  return elements;
163  }
164 
165  // Read into the 3-d class that inherits from the interface I3D.
166  template<typename T>
167  int read3DClass( const Unit& unit, I3D<T>& i3d )
168  {
169  return read3DPtr( unit, i3d.getPtr3d(), i3d.getM(), i3d.getN(), i3d.getO() );
170  }
171 
172  // Read into the 3-d pointer. Returns the number of elements written, 0 if an error.
173  template<typename T>
174  int read3DPtr( const Unit& unit, T*** values, int m, int n, int o )
175  {
176  if( getUri().getDimensions() != 3 )
177  {
178  std::cerr << "URI " << Str( getUri() ).cStr() << " is not 3D." << std::endl;
179  return 0;
180  }
181  else if( getUri().getDimension1() != m || getUri().getDimension2() != n || getUri().getDimension3() != o )
182  {
183  std::cerr << "URI " << Str( getUri() ).cStr() << " expects " << getUri().getDimension1() << "-by-" << getUri().getDimension2() << "-by-" << getUri().getDimension3() << ", but received " << m << "-by-" << n << "-by-" << o << std::endl;
184  return 0;
185  }
186  size_t elements = read( blobValue_ ) ? MIN( m * n * o, ( int )blobValue_.getElements() ) : 0;
187  if( elements > 0 )
188  {
189  blobValue_.copyTo( unit, values, elements / n / o, n, o );
190  }
191  return elements;
192  }
193 
194 protected:
195 
197 
199  : blobValue_( uri )
200  {}
201 
202 private:
203  // Disallow default constructor for abstract base class
205 
206  // Disallow copy constructor
207  AbstractBlobReader( const AbstractBlobReader& copyFrom );
208 
209 
210 };
211 
213 {
214 
222 public:
223 
224  // Re-implement abstract method from AbstractBlobReader
225  virtual bool read( DataValue& readTo )
226  {
227  return DataReader::read( readTo );
228  }
229 
230  // Re-implement abstract method from AbstractBlobReader
231  virtual const ElementURI& getUri( void ) const
232  {
233  return DataReader::getUri();
234  }
235 
236 protected:
237 
238  friend class Component;
239 
241  DataElement& element,
242  const Unit& defaultUnit )
243  : DataReader( owner, element, defaultUnit ),
245  {}
246 
247 };
248 
249 #endif /* BLOBREADER_H_ */
int read1DClass(const Unit &unit, I1D< T > &i1d)
Definition: BlobReader.h:70
const unsigned short getDimension2() const
Definition: ElementURI.h:136
virtual bool read(DataValue &readTo)=0
void * getBytes()
Definition: BlobValue.h:227
Definition: I3D.h:18
Contains the DataReader class definition.
BlobReader(Component *owner, DataElement &element, const Unit &defaultUnit)
Definition: BlobReader.h:240
const unsigned short getDimension1() const
Definition: ElementURI.h:131
A DataAccessor that reads values from the Slate Contains a default dataValue that must not be NULL...
Definition: DataReader.h:30
virtual int getM() const =0
Definition: I1D.h:17
int read1DPtr(const Unit &unit, T *values, int m)
Definition: BlobReader.h:77
#define MIN(a, b)
Minimum of two values – also available as AuvMath::Min() template function If either argument is a N...
Definition: AuvMath.h:49
BlobValue readBlob(bool &success)
Definition: BlobReader.h:45
Definition: BlobReader.h:212
virtual int getM() const =0
BlobValue blobValue_
Definition: BlobReader.h:196
virtual ~AbstractBlobReader()
Abstract parent class that adds the capability of reading BlobValues to a "normal" DataReader See bel...
Definition: BlobReader.h:35
virtual const ElementURI & getUri(void) const =0
virtual const ElementURI & getUri(void) const
Definition: DataAccessor.cpp:49
A DataValue is an abstract base class for a data value and associated engineering units of the value...
Definition: DataValue.h:35
int read2DClass(const Unit &unit, I2D< T > &i2d)
Definition: BlobReader.h:116
size_t getElements()
Definition: BlobValue.h:217
int read1DArray(const Unit &unit, T(&x)[M])
Definition: BlobReader.h:53
virtual const ElementURI & getUri(void) const
Definition: BlobReader.h:231
const unsigned short getDimension3() const
Definition: ElementURI.h:141
virtual bool read(DataValue &readTo)
A DataReader for reading BlobValues, normally created via Component::newBlobReader() ...
Definition: BlobReader.h:225
int copyTo(const Unit &unit, T *array, size_t elements)
Definition: BlobValue.cpp:272
virtual T * getPtr1d()=0
virtual int getN() const =0
virtual int getM() const =0
Replacement for standard template class string.
Definition: Str.h:12
Definition: I2D.h:17
int read2DPtr(const Unit &unit, T **values, int m, int n)
Definition: BlobReader.h:123
Abstract Base class for software components.
Definition: Component.h:53
Wraps a StrValue as an arbitrary collection of binary values, with units.
Definition: BlobValue.h:26
bool read(const Unit &unit, unsigned char &readTo)
Get functions.
Definition: DataReader.cpp:82
Definition: BlobReader.h:21
const char * cStr() const
Definition: Str.h:188
virtual int getO() const =0
virtual T *** getPtr3d()=0
AbstractBlobReader(const ElementURI &uri)
Definition: BlobReader.h:198
virtual T ** getPtr2d()=0
Code unit that represents a unique name for a DataElement.
Definition: ElementURI.h:27
int read2DArray(const Unit &unit, T(&x)[M][N])
Definition: BlobReader.h:94
int read3DPtr(const Unit &unit, T ***values, int m, int n, int o)
Definition: BlobReader.h:174
int read3DClass(const Unit &unit, I3D< T > &i3d)
Definition: BlobReader.h:167
Code that represents an engineering unit.
Definition: Unit.h:24
Contains the BlobValue class definition.
int read3DArray(const Unit &unit, T(&x)[M][N][O])
Definition: BlobReader.h:145
virtual int getN() const =0
Abstact base class for one "element" of the Slate.
Definition: DataElement.h:37