cisst-saw
Loading...
Searching...
No Matches
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 Author(s): Anton Deguet
6 Created on: 2011-06-27
7
8 (C) Copyright 2011-2018 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19
20#pragma once
21#ifndef _cmnDataFunctionsArray_h
22#define _cmnDataFunctionsArray_h
23
26
27// always include last
29
30template <class _elementType, size_t _size>
31class cmnData<_elementType[_size]>
32{
33public:
34 enum {IS_SPECIALIZED = 1};
35 typedef _elementType * pointer;
36 typedef const _elementType * const_pointer;
37
38 static void Copy(pointer data, const_pointer source)
39 {
40 pointer dataPtr = data;
41 const_pointer sourcePtr = source;
42 for (size_t index = 0; index < _size; ++index, ++dataPtr, ++sourcePtr) {
43 cmnData<_elementType>::Copy(*dataPtr, *sourcePtr);
44 }
45 }
46
47 static std::string HumanReadable(const_pointer data)
48 {
49 const_pointer ptr = data;
50 std::stringstream stringStream;
51 stringStream << "[";
52 for (size_t index = 0; index < _size; ++index, ++ptr) {
53 if (index != 0) {
54 stringStream << ", ";
55 }
56 stringStream << cmnData<_elementType>::HumanReadable(*ptr);
57 }
58 stringStream << "]";
59 return stringStream.str();
60 }
61
62 static void SerializeText(const_pointer data,
63 std::ostream & outputStream,
64 const char delimiter) CISST_THROW(std::runtime_error)
65 {
66 const_pointer ptr = data;
67 for (size_t index = 0; index < _size; ++index, ++ptr) {
68 if (index != 0) {
69 outputStream << delimiter;
70 }
71 cmnData<_elementType>::SerializeText(*ptr, outputStream, delimiter);
72 }
73 }
74
75 static void DeSerializeText(pointer data,
76 std::istream & inputStream,
77 const char delimiter) CISST_THROW(std::runtime_error)
78 {
79 pointer ptr = data;
80 for (size_t index = 0; index < _size; ++index, ++ptr) {
81 if (index != 0) {
82 cmnDataDeSerializeTextDelimiter(inputStream, delimiter, "C Array");
83 }
84 cmnData<_elementType>::DeSerializeText(*ptr, inputStream, delimiter);
85 }
86 }
87
88 static std::string SerializeDescription(const_pointer data,
89 const char delimiter,
90 const std::string & userDescription = "v")
91 {
92 std::string prefix = (userDescription == "") ? "v[" : (userDescription + "[");
93 std::stringstream indexSuffix;
94 std::stringstream description;
95 const_pointer ptr = data;
96 for (size_t index = 0; index < _size; ++index, ++ptr) {
97 if (index != 0) {
98 description << delimiter;
99 }
100 indexSuffix.clear();
101 indexSuffix.str(std::string());
102 indexSuffix << index << "]";
103 description << cmnData<_elementType>::SerializeDescription(*ptr, delimiter, prefix + indexSuffix.str());
104 }
105 return description.str();
106 }
107
108 static void SerializeBinary(const_pointer data, std::ostream & outputStream)
109 CISST_THROW(std::runtime_error)
110 {
111 const_pointer ptr = data;
112 for (size_t index = 0; index < _size; ++index, ++ptr) {
113 cmnData<_elementType>::SerializeBinary(*ptr, outputStream);
114 }
115 }
116
117 static void DeSerializeBinary(pointer data,
118 std::istream & inputStream,
119 const cmnDataFormat & localFormat,
120 const cmnDataFormat & remoteFormat)
121 CISST_THROW(std::runtime_error)
122 {
123 pointer ptr = data;
124 for (size_t index = 0; index < _size; ++index, ++ptr) {
125 cmnData<_elementType>::DeSerializeBinary(*ptr, inputStream, localFormat, remoteFormat);
126 }
127 }
128
130 {
132 }
133
134 static size_t ScalarNumber(const_pointer data)
135 {
137 return _size * cmnData<_elementType>::ScalarNumber(data[0]);
138 }
139 size_t result = 0;
140 const_pointer ptr = data;
141 for (size_t index = 0; index < _size; ++index, ++ptr) {
143 }
144 return result;
145 }
146
147 static std::string ScalarDescription(const_pointer data, const size_t index,
148 const std::string & userDescription = "v")
149 CISST_THROW(std::out_of_range)
150 {
151 size_t elementIndex, inElementIndex;
152 std::stringstream suffix;
153 const size_t scalarNumber = cmnData<_elementType[_size]>::ScalarNumber(data);
155 index, elementIndex, inElementIndex)) {
156 suffix << "[" << index << "]";
157 return cmnData<_elementType>::ScalarDescription(data[elementIndex], inElementIndex, userDescription + suffix.str());
158 }
159 cmnThrow(std::out_of_range("cmnDataScalarDescription: C array index out of range"));
160 return "";
161 }
162
163 static double Scalar(const_pointer data, const size_t index)
164 CISST_THROW(std::out_of_range)
165 {
166 size_t elementIndex, inElementIndex;
167 const size_t scalarNumber = cmnData<_elementType[_size]>::ScalarNumber(data);
169 index, elementIndex, inElementIndex)) {
170 return cmnData<_elementType>::Scalar(data[elementIndex], inElementIndex);
171 } else {
172 cmnThrow(std::out_of_range("cmnDataScalar: C array index out of range"));
173 }
174 return 0.123456789; // unreachable, just to avoid compiler warnings
175 }
176};
177
178#endif // _cmnDataFunctionsArray_h
@ IS_SPECIALIZED
Definition cmnDataFunctionsArray.h:34
static size_t ScalarNumber(const_pointer data)
Definition cmnDataFunctionsArray.h:134
static std::string ScalarDescription(const_pointer data, const size_t index, const std::string &userDescription="v") CISST_THROW(std
Definition cmnDataFunctionsArray.h:147
static bool ScalarNumberIsFixed(const_pointer data)
Definition cmnDataFunctionsArray.h:129
static void DeSerializeBinary(pointer data, std::istream &inputStream, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat) CISST_THROW(std
Definition cmnDataFunctionsArray.h:117
static double Scalar(const_pointer data, const size_t index) CISST_THROW(std
Definition cmnDataFunctionsArray.h:163
static void DeSerializeText(pointer data, std::istream &inputStream, const char delimiter) CISST_THROW(std
Definition cmnDataFunctionsArray.h:75
static std::string SerializeDescription(const_pointer data, const char delimiter, const std::string &userDescription="v")
Definition cmnDataFunctionsArray.h:88
static void SerializeBinary(const_pointer data, std::ostream &outputStream) CISST_THROW(std
Definition cmnDataFunctionsArray.h:108
const _elementType * const_pointer
Definition cmnDataFunctionsArray.h:36
static std::string HumanReadable(const_pointer data)
Definition cmnDataFunctionsArray.h:47
static void SerializeText(const_pointer data, std::ostream &outputStream, const char delimiter) CISST_THROW(std
Definition cmnDataFunctionsArray.h:62
_elementType * pointer
Definition cmnDataFunctionsArray.h:35
static void Copy(pointer data, const_pointer source)
Definition cmnDataFunctionsArray.h:38
Definition cmnDataFormat.h:30
Definition cmnDataFunctions.h:51
static void Copy(DataType &data, const DataType &source)
static size_t DeSerializeBinary(DataType &data, const char *buffer, size_t bufferSize, const cmnDataFormat &CMN_UNUSED(localFormat), const cmnDataFormat &CMN_UNUSED(remoteFormat))
static bool ScalarNumberIsFixed(const DataType &data)
static size_t SerializeBinary(const DataType &data, char *buffer, size_t bufferSize)
static std::string static ScalarDescription(const DataType &data, const size_t index, const std::string &userDescription="") CISST_THROW(std double static Scalar(const DataType &data, const size_t index) CISST_THROW(std size_t ScalarNumber(const DataType &data)
Definition cmnDataFunctions.h:162
bool cmnDataVectorScalarFindInVectorIndex(_vectorType data, const size_t size, const size_t scalarNumber, const size_t index, size_t &elementIndex, size_t &inElementIndex)
Definition cmnDataFunctionsVectorHelpers.h:238
Macros to export the symbols of cisstCommon (in a Dll).
#define CISST_THROW(exceptionParameter)
Somewhat portable compilation warning message. This works with very recent versions of gcc (4....
Definition cmnPortability.h:559
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76
STL namespace.