AAO DRAMA/DRAMA2 C++ Interface
DRAMA C++11 and later interface
sdsarray.hh
Go to the documentation of this file.
1#ifndef _DRAMA2_SDSARRAY_INC
2#define _DRAMA2_SDSARRAY_INC
3
16/*
17 * History:
18 07-Jan-2014 - TJF - Original version
19 */
20#include "sds.h"
21#include <string>
22
23namespace drama {
24
25 namespace sds {
26
42 template<typename SDS_T> int CodeForType() { return -1; }
43
48 template <> int CodeForType<double>();
53 template <> int CodeForType<float>();
58 template <> int CodeForType<short>();
63 template <> int CodeForType<INT32>();
68 template <> int CodeForType<INT64>();
73 template <> int CodeForType<unsigned short>();
78 template <> int CodeForType<UINT32>();
83 template <> int CodeForType<UINT64>();
88 template <> int CodeForType<signed char>();
93 template <> int CodeForType<unsigned char>();
94 //template <> int CodeForType<std::string>();
99 template <> int CodeForType<const std::string&>();
105 template <> int CodeForType<char>();
106
111 template <> int CodeForType<bool> ();
112
113
114 class Id;
116
117
153 template <typename T> class ArrayAccessHelper {
154 public:
155 friend class drama::sds::Id;
156 private:
157 SdsCodeType code; /* The SDS code of the item */
158 unsigned long elements; /* Number of element in the array */
159 T* data; /* The array pointer is written here */
160
161 /*
162 * Remaining private items used by sds::Id to set this item up.
163 */
164 /*
165 * Set the data pointer to be the specified address.
166 */
167 void SetDataToAddr(void *dataAddr) {
168 data = static_cast<T*>(dataAddr);
169 }
170
171 /*
172 * Set the number of elements in the array.
173 *
174 * This is used by SdsId.AccessArray() set the number of
175 * elements in the array.
176 */
177 void SetNumElements(unsigned long nelem) {
178 elements = nelem;
179 }
181 protected:
182
183 /*
184 * Helper function used to check an index is valid. Also checks
185 * that we have some data. Throws expections on problems.
186 */
187 void CheckIndexAndData(unsigned long index) const {
188 if (!data)
189 {
191 "drama::Sds::ArrayAccessHelper Index {0}, Code {1}, elements {2} - data is invalid",
192 index, code, elements);
193 }
194 if (index >= elements)
195 {
197 "drama::Sds::ArrayAccessHelper Index {0}, Code {1}, elements {2} - index is invalid",
198 index, code, elements);
199 }
200 }
201
202
203
208 T * GetDataAddress() {
209 return this->data;
210 }
215 const T * GetDataAddress() const {
216 return this->data;
217 }
218
219 public:
226 ArrayAccessHelper() : code(CodeForType<T>()), elements(0), data(0) {
227 if ((code < 0)||(code > SDS_C_MAXCODE))
229 "ArrayAccessHelper constructor with invalid code {0}, must be in range 0 to {1}",
230 code, SDS_C_MAXCODE);
231 }
232
237 SdsCodeType Code() const { return code; }
242 unsigned long Size() const { return elements; }
243
244
245 }; /* Sds::ArrayAccessHelper */
246
247
278 template <typename T> class ArrayWriteHelper : public ArrayAccessHelper<T> {
279 public:
286 ArrayWriteHelper() { }
287
295 T & operator[](const unsigned long index) {
296 /* Note, "this->" is required for both of these to avoid
297 * compiler not knowing the type applied to the code
298 */
299 this->CheckIndexAndData(index);
300 return this->GetDataAddress()[index];
301 }
309 T const & operator[](const unsigned long index) const {
310 /* Note, "this->" is required for both of these to avoid
311 * compiler not knowing the type applied to the code
312 */
313 this->CheckIndexAndData(index);
314 return this->GetDataAddress()[index];
315 }
316
325 T * DataAddress() {
326 return this->GetDataAddress();
327 }
328 }; /* Sds::ArrayWriteHelper */
329
360 template <typename T> class ArrayReadHelper : public ArrayAccessHelper<T> {
361 public:
368 ArrayReadHelper() { }
369
377 T const & operator[](const unsigned long index) const {
378 /* Note, "this->" is required for both of these to avoid
379 * compiler not knowing the type applied to the code
380 */
381 this->CheckIndexAndData(index);
382 return this->GetDataAddress()[index];
383 }
392 const T * DataAddress() const {
393 return this->GetDataAddress();
394 }
395 }; /* Sds::ArrayReadHelper */
396
397
398
399
400 } // namespace sds
401
402} // namespace drama
403
404#endif
SdsCodeType Code() const
Return the SDS type code of the item being accessed.
Definition sdsarray.hh:264
ArrayAccessHelper()
Create a drama::sds::ArrayAccessHelper object.
Definition sdsarray.hh:253
unsigned long Size() const
Return the number of elements in the array.
Definition sdsarray.hh:269
const T * GetDataAddress() const
Return the address of the data.
Definition sdsarray.hh:242
T * GetDataAddress()
Return the address of the data.
Definition sdsarray.hh:235
Helper class for access to an SDS Scalar Arrays.
Definition sdsarray.hh:180
ArrayReadHelper()
Create a drama::sds::ArrayReadHelper object.
Definition sdsarray.hh:395
T const & operator[](const unsigned long index) const
SDS array subscript operator.
Definition sdsarray.hh:404
const T * DataAddress() const
Access the data directly - read only.
Definition sdsarray.hh:419
Helper class for reading data from SDS Scalar Arrays.
Definition sdsarray.hh:387
ArrayWriteHelper()
Create a drama::sds::ArrayWriteHelper object.
Definition sdsarray.hh:313
T & operator[](const unsigned long index)
SDS array subscript operator - non-const version.
Definition sdsarray.hh:322
T const & operator[](const unsigned long index) const
SDS array subscript operator - const version.
Definition sdsarray.hh:336
T * DataAddress()
Access the data directly.
Definition sdsarray.hh:352
Helper class for writing data to SDS Scalar Arrays.
Definition sdsarray.hh:305
A C++ Interface to the handling SDS structures.
Definition sds.hh:428
#define DramaTHROW_F(status_, format_,...)
Throw a Drama exception with fmt::format string formatting.
Definition exception.hh:129
int CodeForType< short >()
Returns the SDS type for a short - SDS_SHORT.
int CodeForType< unsigned short >()
Returns the SDS type for an unsigned short - SDS_USHORT.
int CodeForType< double >()
Returns the SDS type for a double - SDS_DOUBLE.
int CodeForType< const std::string & >()
Returns the SDS type for a string - ARG_STRING.
int CodeForType< signed char >()
Returns the SDS type for a signed char - SDS_BYTE.
int CodeForType< char >()
Returns the SDS type for a char - SDS_CHAR.
int CodeForType< unsigned char >()
Returns the SDS type for an unsigned char - SDS_UBYTE.
int CodeForType< INT64 >()
Returns the SDS type for a 64 bit integer - SDS_I64.
int CodeForType()
Functions which returns the SDS type of a given C type.
Definition sdsarray.hh:69
int CodeForType< UINT32 >()
Returns the SDS type for an unsigned integer - SDS_UINT.
int CodeForType< float >()
Returns the SDS type for a flat - SDS_FLOAT.
int CodeForType< UINT64 >()
Returns the SDS type for an unsigned 64 bit integer - SDS_UI64.
int CodeForType< INT32 >()
Returns the SDS type for a 32 bit integer - SDS_INT.
int CodeForType< bool >()
Return the SDS type code for C - SDS_SHORT
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1339
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition drama.hh:99