cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnDataFunctionsArray.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Anton Deguet
7  Created on: 2011-06-27
8 
9  (C) Copyright 2011-2013 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 
20 */
21 
22 
23 #pragma once
24 #ifndef _cmnDataFunctionsArray_h
25 #define _cmnDataFunctionsArray_h
26 
29 
30 // always include last
31 #include <cisstCommon/cmnExport.h>
32 
33 template <class _elementType, size_t _size>
34 class cmnData<_elementType[_size]>
35 {
36 public:
37  enum {IS_SPECIALIZED = 1};
38  typedef _elementType * pointer;
39  typedef const _elementType * const_pointer;
40 
41  static void Copy(pointer data, const_pointer source)
42  {
43  pointer dataPtr = data;
44  const_pointer sourcePtr = source;
45  for (size_t index = 0; index < _size; ++index, ++dataPtr, ++sourcePtr) {
46  cmnData<_elementType>::Copy(*dataPtr, *sourcePtr);
47  }
48  }
49 
50  static std::string HumanReadable(const_pointer data)
51  {
52  const_pointer ptr = data;
53  std::stringstream stringStream;
54  stringStream << "[";
55  for (size_t index = 0; index < _size; ++index, ++ptr) {
56  if (index != 0) {
57  stringStream << ", ";
58  }
59  stringStream << cmnData<_elementType>::HumanReadable(*ptr);
60  }
61  stringStream << "]";
62  return stringStream.str();
63  }
64 
65  static void SerializeText(const_pointer data,
66  std::ostream & outputStream,
67  const char delimiter) throw (std::runtime_error)
68  {
69  const_pointer ptr = data;
70  for (size_t index = 0; index < _size; ++index, ++ptr) {
71  if (index != 0) {
72  outputStream << delimiter;
73  }
74  cmnData<_elementType>::SerializeText(*ptr, outputStream, delimiter);
75  }
76  }
77 
78  static void DeSerializeText(pointer data,
79  std::istream & inputStream,
80  const char delimiter) throw (std::runtime_error)
81  {
82  pointer ptr = data;
83  for (size_t index = 0; index < _size; ++index, ++ptr) {
84  if (index != 0) {
85  cmnDataDeSerializeTextDelimiter(inputStream, delimiter, "C Array");
86  }
87  cmnData<_elementType>::DeSerializeText(*ptr, inputStream, delimiter);
88  }
89  }
90 
91  static std::string SerializeDescription(const_pointer data,
92  const char delimiter,
93  const std::string & userDescription = "v")
94  {
95  std::string prefix = (userDescription == "") ? "v[" : (userDescription + "[");
96  std::stringstream indexSuffix;
97  std::stringstream description;
98  const_pointer ptr = data;
99  for (size_t index = 0; index < _size; ++index, ++ptr) {
100  if (index != 0) {
101  description << delimiter;
102  }
103  indexSuffix.clear();
104  indexSuffix.str(std::string());
105  indexSuffix << index << "]";
106  description << cmnData<_elementType>::SerializeDescription(*ptr, delimiter, prefix + indexSuffix.str());
107  }
108  return description.str();
109  }
110 
111  static void SerializeBinary(const_pointer data, std::ostream & outputStream)
112  throw (std::runtime_error)
113  {
114  const_pointer ptr = data;
115  for (size_t index = 0; index < _size; ++index, ++ptr) {
116  cmnData<_elementType>::SerializeBinary(*ptr, outputStream);
117  }
118  }
119 
120  static void DeSerializeBinary(pointer data,
121  std::istream & inputStream,
122  const cmnDataFormat & localFormat,
123  const cmnDataFormat & remoteFormat)
124  throw (std::runtime_error)
125  {
126  pointer ptr = data;
127  for (size_t index = 0; index < _size; ++index, ++ptr) {
128  cmnData<_elementType>::DeSerializeBinary(*ptr, inputStream, localFormat, remoteFormat);
129  }
130  }
131 
133  {
135  }
136 
137  static size_t ScalarNumber(const_pointer data)
138  {
140  return _size * cmnData<_elementType>::ScalarNumber(data[0]);
141  }
142  size_t result = 0;
143  const_pointer ptr = data;
144  for (size_t index = 0; index < _size; ++index, ++ptr) {
145  result += cmnData<_elementType>::ScalarNumber(*ptr);
146  }
147  return result;
148  }
149 
150  static std::string ScalarDescription(const_pointer data, const size_t index,
151  const std::string & userDescription = "v")
152  throw (std::out_of_range)
153  {
154  size_t elementIndex, inElementIndex;
155  std::stringstream suffix;
156  const size_t scalarNumber = cmnData<_elementType[_size]>::ScalarNumber(data);
157  if (cmnDataVectorScalarFindInVectorIndex<const _elementType *, _elementType>(data, _size, scalarNumber,
158  index, elementIndex, inElementIndex)) {
159  suffix << "[" << index << "]";
160  return cmnData<_elementType>::ScalarDescription(data[elementIndex], inElementIndex, userDescription + suffix.str());
161  }
162  cmnThrow(std::out_of_range("cmnDataScalarDescription: C array index out of range"));
163  return "";
164  }
165 
166  static double Scalar(const_pointer data, const size_t index)
167  throw (std::out_of_range)
168  {
169  size_t elementIndex, inElementIndex;
170  const size_t scalarNumber = cmnData<_elementType[_size]>::ScalarNumber(data);
171  if (cmnDataVectorScalarFindInVectorIndex<const _elementType *, _elementType>(data, _size, scalarNumber,
172  index, elementIndex, inElementIndex)) {
173  return cmnData<_elementType>::Scalar(data[elementIndex], inElementIndex);
174  } else {
175  cmnThrow(std::out_of_range("cmnDataScalar: C array index out of range"));
176  }
177  return 0.123456789; // unreachable, just to avoid compiler warnings
178  }
179 };
180 
181 #endif // _cmnDataFunctionsArray_h
static void SerializeBinary(const_pointer data, std::ostream &outputStream)
Definition: cmnDataFunctionsArray.h:111
static std::string ScalarDescription(const_pointer data, const size_t index, const std::string &userDescription="v")
Definition: cmnDataFunctionsArray.h:150
static void DeSerializeBinary(pointer data, std::istream &inputStream, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat)
Definition: cmnDataFunctionsArray.h:120
static void Copy(pointer data, const_pointer source)
Definition: cmnDataFunctionsArray.h:41
Definition: cmnDataFormat.h:32
static std::string HumanReadable(const_pointer data)
Definition: cmnDataFunctionsArray.h:50
_elementType * pointer
Definition: cmnDataFunctionsArray.h:38
static double Scalar(const_pointer data, const size_t index)
Definition: cmnDataFunctionsArray.h:166
static void SerializeBinary(const DataType &data, std::ostream &outputStream)
static void DeSerializeText(pointer data, std::istream &inputStream, const char delimiter)
Definition: cmnDataFunctionsArray.h:78
static void SerializeText(const DataType &data, std::ostream &outputStream, const char delimiter= ',')
Macros to export the symbols of cisstCommon (in a Dll).
static bool ScalarNumberIsFixed(const DataType &data)
static size_t ScalarNumber(const DataType &data)
static double Scalar(const DataType &data, const size_t index)
Definition: cmnDataFunctions.h:53
static std::string ScalarDescription(const DataType &data, const size_t index, const std::string &userDescription="")
Definition: cmnDataFunctions.h:56
#define cmnThrow(a)
Definition: MinimalCmn.h:4
static void SerializeText(const_pointer data, std::ostream &outputStream, const char delimiter)
Definition: cmnDataFunctionsArray.h:65
static std::string SerializeDescription(const_pointer data, const char delimiter, const std::string &userDescription="v")
Definition: cmnDataFunctionsArray.h:91
static void DeSerializeText(DataType &data, std::istream &inputStream, const char delimiter= ',')
static void DeSerializeBinary(DataType &data, std::istream &inputStream, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat)
static size_t ScalarNumber(const_pointer data)
Definition: cmnDataFunctionsArray.h:137
const _elementType * const_pointer
Definition: cmnDataFunctionsArray.h:39
static bool ScalarNumberIsFixed(const_pointer data)
Definition: cmnDataFunctionsArray.h:132
static void Copy(DataType &data, const DataType &source)
void CISST_EXPORT cmnDataDeSerializeTextDelimiter(std::istream &inputStream, const char delimiter, const char *className)