LRAUV  revA
BlobValue.h
Go to the documentation of this file.
1 
10 #ifndef BLOBVALUE_H_
11 #define BLOBVALUE_H_
12 
13 #include "data/BlobType.h"
14 #include "data/StrValue.h"
15 
16 class DataWriter;
17 class ElementURI;
18 class RingBufferVoid;
19 
26 class BlobValue : public StrValue
27 {
28 public:
29 
30  static size_t ElementSize( const BlobType& blobType )
31  {
32  return ELEMENT_SIZE[ blobType >= NOT_BLOB && blobType < BLOB_TYPE_COUNT ? blobType : NOT_BLOB ];
33  }
34 
35  static const size_t ELEMENT_SIZE[BLOB_TYPE_COUNT];
36 
38  BlobValue( const ElementURI& elementUri );
39 
41  BlobValue( const void* bytes, size_t elements, const ElementURI& elementUri );
42 
44  BlobValue( const Unit& unit, const double* array, size_t elements, const ElementURI& elementUri );
45 
47  BlobValue( const Unit& unit, const float* array, size_t elements, const ElementURI& elementUri );
48 
50  BlobValue( const Unit& unit, const int* array, size_t elements, const ElementURI& elementUri );
51 
52  // set from raw data
53  void setFromRaw( const void* bytes, size_t elements );
54 
55  // set from a 1-d array (of double, float or int)
56  template<typename T>
57  void setFrom( const Unit& unit, const T* array, size_t elements );
58 
59  // set from a 2-d array (of double, float or int)
60  template<typename T, size_t m, size_t n>
61  void setFrom( const Unit& unit, const T( &array )[m][n] )
62  {
63  size_t length = m * n * ElementSize( blobType_ );
64  value_.set( NULL, length );
65  if( conversionNeeded<T>( unit ) )
66  {
67  for( size_t i = 0; i < m; ++i )
68  {
69  for( size_t j = 0; j < n; ++j )
70  {
71  set( i * n + j, unit, array[i][j] );
72  }
73  }
74  }
75  else
76  {
77  length = n * ElementSize( blobType_ );
78  for( size_t i = 0; i < m; ++i )
79  {
80  size_t offset = i * length;
81  memcpy( ( char* )value_.cStr() + offset, ( const char* )( array[i] ), length );
82  }
83  }
84  }
85 
86  // set from a 2-d array pointer (of double, float or int)
87  template<typename T>
88  void setFrom( const Unit& unit, const T** array, int m, int n );
89 
90  // set from a 3-d array (of double, float or int)
91  template<typename T, size_t m, size_t n, size_t o>
92  void setFrom( const Unit& unit, const T( &array )[m][n][o] )
93  {
94  size_t length = m * n * o * ElementSize( blobType_ );
95  value_.set( NULL, length );
96  if( conversionNeeded<T>( unit ) )
97  {
98  for( size_t i = 0; i < m; ++i )
99  {
100  for( size_t j = 0; j < n; ++j )
101  {
102  for( size_t k = 0; k < o; ++k )
103  {
104  set( i * n * o + j * o + k, unit, array[i][j][k] );
105  }
106  }
107  }
108  }
109  else
110  {
111  length = o * ElementSize( blobType_ );
112  for( size_t i = 0; i < m; ++i )
113  {
114  for( size_t j = 0; j < n; ++j )
115  {
116  size_t offset = ( i * n + j ) * length;
117  memcpy( ( char* )value_.cStr() + offset, ( const char* )( array[i][j] ), length );
118  }
119  }
120  }
121  }
122 
123  // set from a 3-d array pointer (of double, float or int)
124  template<typename T>
125  void setFrom( const Unit& unit, const T*** array, int m, int n, int o );
126 
128  bool set( size_t index, const Unit& unit, double value );
129 
130  // copy to a 1-d array (of double, float or int), returns # of elements copied
131  template<typename T>
132  int copyTo( const Unit& unit, T* array, size_t elements );
133 
134  // copy to a 2-d array (of doubles, float, or int)
135  template<typename T, size_t m, size_t n>
136  int copyTo( const Unit& unit, T( &array )[m][n] )
137  {
138  size_t elements = m * n;
139  if( elements > getElements() )
140  {
141  elements = getElements() / n * n;
142  }
143  if( conversionNeeded<T>( unit ) )
144  {
145  double value;
146  for( unsigned int i = 0; i < elements / n; ++i )
147  {
148  for( unsigned int j = 0; j < elements / m; ++j )
149  {
150  get( i * n + j, unit, value );
151  array[i][j] = value;
152  }
153  }
154  }
155  else
156  {
157  size_t length = n * sizeof( T );
158  for( unsigned int i = 0; i < elements / n; ++i )
159  {
160  size_t offset = i * length;
161  memcpy( ( char* )( array[i] ), value_.cStr() + offset, length );
162  }
163  }
164  return elements;
165  }
166 
167  // copy to a 2-d array pointer (of double, float or int), returns # of elements copied
168  template<typename T>
169  int copyTo( const Unit& unit, T** array, int m, int n );
170 
171  // copy to a 3-d array (of doubles, float, or int)
172  template<typename T, size_t m, size_t n, size_t o>
173  int copyTo( const Unit& unit, T( &array )[m][n][o] )
174  {
175  size_t elements = m * n * o;
176  if( elements > getElements() )
177  {
178  elements = getElements() / n / o * n * o;
179  }
180  if( conversionNeeded<T>( unit ) )
181  {
182  double value;
183  for( unsigned int i = 0; i < elements / n / o ; ++i )
184  {
185  for( unsigned int j = 0; j < n; ++j )
186  {
187  for( unsigned int k = 0; k < o; ++k )
188  {
189  get( i * n * o + j * o + k, unit, value );
190  array[i][j][k] = value;
191  }
192  }
193  }
194  }
195  else
196  {
197  size_t length = o * sizeof( T );
198  for( unsigned int i = 0; i < elements / n / o; ++i )
199  {
200  for( unsigned int j = 0; j < n; ++j )
201  {
202  size_t offset = ( i * n + j ) * length;
203  memcpy( ( char* )( array[i][j] ), value_.cStr() + offset, length );
204  }
205  }
206  }
207  return elements;
208  }
209 
210  // copy to a 3-d array pointer (of double, float or int), returns # of elements copied
211  template<typename T>
212  int copyTo( const Unit& unit, T*** array, int m, int n, int o );
213 
215  bool get( size_t index, const Unit& unit, double& value );
216 
217  size_t getElements()
218  {
219  return ElementSize( blobType_ ) == 0 ? 0 : value_.length() / ElementSize( blobType_ );
220  }
221 
222  size_t getByteLength()
223  {
224  return value_.length();
225  }
226 
227  void* getBytes()
228  {
229  return ( void* )value_.cStr();
230  }
231 
232  // as a Str
233  virtual Str toString( const Unit& unit ) const
234  {
235  return Str( value_.length(), 10 ) + " bytes of " + unit.getName();
236  }
237 
239  {
240  return blobType_;
241  }
242 
243 protected:
244 
245  friend class Blob_Test;
246 
247  // returns true if conversion needed
248  template<typename T>
249  bool conversionNeeded( const Unit& unit );
250 
252  template<typename T>
253  void setElement( size_t index, T value, bool flip );
254 
256  template<typename T>
257  void getElement( size_t index, T& value, bool flip );
258 
260 
261 
262 };
263 
264 #endif /*BLOBVALUE_H_*/
size_t getByteLength()
Definition: BlobValue.h:222
void * getBytes()
Definition: BlobValue.h:227
BlobType blobType_
Definition: BlobValue.h:259
Contains the BlobType enum definition.
int copyTo(const Unit &unit, T(&array)[m][n][o])
Definition: BlobValue.h:173
static const size_t ELEMENT_SIZE[BLOB_TYPE_COUNT]
Definition: BlobValue.h:35
BlobValue(const ElementURI &elementUri)
ElementURI constructior.
Definition: BlobValue.cpp:46
void setFromRaw(const void *bytes, size_t elements)
Definition: BlobValue.cpp:85
size_t length() const
Definition: Str.h:196
A DataAccessor that writes values to the Slate via its associated DataElement.
Definition: DataWriter.h:27
bool conversionNeeded(const Unit &unit)
BlobType getBlobType()
Definition: BlobValue.h:238
size_t getElements()
Definition: BlobValue.h:217
Contains the StrValue class definition.
void setFrom(const Unit &unit, const T(&array)[m][n][o])
Definition: BlobValue.h:92
BlobType
Definition: BlobType.h:13
int copyTo(const Unit &unit, T *array, size_t elements)
Definition: BlobValue.cpp:272
Replacement for standard template class string.
Definition: Str.h:12
Definition: BlobType.h:15
void getElement(size_t index, T &value, bool flip)
Get an individual value, without bounds checking or scaling.
Definition: BlobValue.cpp:541
Wraps a StrValue as an arbitrary collection of binary values, with units.
Definition: BlobValue.h:26
Definition: BlobType.h:38
virtual Str toString(const Unit &unit) const
Definition: BlobValue.h:233
void setElement(size_t index, T value, bool flip)
Set an individual value, without bounds checking or scaling.
Definition: BlobValue.cpp:518
const char * cStr() const
Definition: Str.h:188
void setFrom(const Unit &unit, const T(&array)[m][n])
Definition: BlobValue.h:61
bool set(size_t index, const Unit &unit, double value)
Set an individual value.
Definition: BlobValue.cpp:186
static size_t ElementSize(const BlobType &blobType)
Definition: BlobValue.h:30
Unit tests for the BlobValue, BlobReader, and BlobWriter classes.
Definition: Blob_Test.h:25
Code unit that represents a unique name for a DataElement.
Definition: ElementURI.h:27
const char * getName(void) const
Definition: Unit.h:108
void setFrom(const Unit &unit, const T *array, size_t elements)
Definition: BlobValue.cpp:93
int copyTo(const Unit &unit, T(&array)[m][n])
Definition: BlobValue.h:136
Thread-safe ring buffer for storing void pointers.
Definition: RingBuffer.h:36
Wraps a string (see Str) as a DataValue, so strings can be written to and read from the slate...
Definition: StrValue.h:34
Code that represents an engineering unit.
Definition: Unit.h:24
Str value_
Definition: StrValue.h:212
Str & set(const char *str=NULL, size_t length=NO_POS)
Definition: Str.cpp:165