GCC Code Coverage Report


Directory: ./
File: pdserv-1.1.0/src/DataType.h
Date: 2024-11-17 04:08:36
Exec Total Coverage
Lines: 2 15 13.3%
Branches: 0 0 -%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * $Id$
4 *
5 * copyright (C) 2012 Richard Hacker (lerichi at gmx dot net)
6 *
7 * This file is part of the pdserv library.
8 *
9 * The pdserv library is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation, either version 3 of the License, or (at
12 * your option) any later version.
13 *
14 * The pdserv library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the pdserv library. If not, see <http://www.gnu.org/licenses/>.
21 *
22 *****************************************************************************/
23
24 #ifndef DATATYPE_H
25 #define DATATYPE_H
26
27 #include "Debug.h"
28
29 #include <list>
30 #include <stdint.h>
31 #include <vector>
32 #include <string>
33 #include <ostream>
34
35 namespace PdServ {
36
37 class DataType {
38 public:
39 60 struct DimType: public std::vector<size_t> {
40 DimType(size_t ndims, const size_t *dim);
41 const size_t nelem;
42
43 bool isScalar() const {
44 return nelem == 1;
45 }
46
47 bool isVector() const {
48 return size() == 1;
49 }
50
51 bool isMultiDim() const {
52 return size() > 1;
53 }
54
55 };
56
57 struct Field {
58 Field(const std::string& name, const DataType& type,
59 size_t offset, size_t ndims, const size_t *dims);
60 const std::string name;
61 const DataType& type;
62 const size_t offset;
63 const DimType dim;
64 };
65
66 enum Primary {
67 compound_T = 0,
68 boolean_T,
69 uint8_T, int8_T,
70 uint16_T, int16_T,
71 uint32_T, int32_T,
72 uint64_T, int64_T,
73 double_T, single_T
74 };
75
76 DataType( const std::string& name, size_t size);
77 virtual ~DataType();
78
79 const std::string name;
80 const size_t size;
81 virtual Primary primary() const;
82 bool isPrimary() const;
83 static const size_t maxWidth = 8; /**< Maximum supported data type
84 size in bytes */
85
86 virtual size_t align() const;
87
88 void addField(const std::string& name,
89 const DataType& type,
90 size_t offset,
91 size_t ndims = 1,
92 const size_t* dims = 0);
93
94 22 struct FieldList: public std::list<const Field*> {
95 friend std::ostream& operator<<(std::ostream& os,
96 const FieldList& fieldList);
97 };
98 const FieldList& getFieldList() const;
99
100 bool operator==(const DataType& other) const;
101 void (* const setValue)(char *&, double);
102
103 static const DataType& boolean;
104 static const DataType& uint8;
105 static const DataType& int8;
106 static const DataType& uint16;
107 static const DataType& int16;
108 static const DataType& uint32;
109 static const DataType& int32;
110 static const DataType& uint64;
111 static const DataType& int64;
112 static const DataType& float64;
113 static const DataType& float32;
114
115 virtual void print(std::ostream& os, const char *data,
116 const char* start, const char* end) const;
117
118 struct Printer {
119 Printer(const DataType* dt, const char *addr, size_t nelem):
120 type(dt), addr(addr), size(nelem * dt->size) {
121 }
122
123 friend std::ostream& operator<<(std::ostream& os,
124 const Printer& obj) {
125 obj.type->print(os, obj.addr, obj.addr,
126 obj.addr + obj.size);
127 return os;
128 }
129
130 const DataType* const type;
131 const char * const addr;
132 const size_t size;
133 };
134
135 Printer operator()(const void *addr, size_t n = 1) const {
136 return Printer(this, reinterpret_cast<const char*>(addr), n);
137 }
138
139 protected:
140 DataType(const DataType& other);
141 explicit DataType(size_t size, void (*)(char *&dst, double src));
142
143 private:
144
145 FieldList fieldList;
146 };
147
148 }
149 #endif //DATATYPE_H
150