This is Unofficial EPICS BASE Doxygen Site
TypeFunc.cpp
Go to the documentation of this file.
1 /*TypeFunc.cpp*/
2 /*
3  * Copyright information and license terms for this software can be
4  * found in the file LICENSE that is included with the distribution
5  */
9 #include <cstddef>
10 #include <cstdlib>
11 #include <string>
12 #include <cstdio>
13 
14 #define epicsExportSharedSymbols
15 #include <pv/pvIntrospect.h>
16 #include <pv/epicsException.h>
17 #include <pv/sharedVector.h>
18 
19 #include "dbDefs.h" // for NELEMENTS
20 
21 using std::string;
22 
23 namespace epics { namespace pvData {
24 
25 namespace TypeFunc {
26  static const char* names[] = {
27  "scalar", "scalarArray", "structure", "structureArray", "union", "unionArray"
28  };
29  const char* name(Type t) {
30  if (t<int(scalar) || t>int(unionArray))
31  THROW_EXCEPTION2(std::invalid_argument, "logic error unknown Type");
32  return names[t];
33  }
34 } // namespace TypeFunc
35 
36 std::ostream& operator<<(std::ostream& o, const Type& type)
37 {
38  return o << TypeFunc::name(type);
39 }
40 
41 
42 namespace ScalarTypeFunc {
44  if(type>=pvByte && type<=pvULong) return true;
45  return false;
46  }
47 
49  if(type>=pvUByte && type<=pvULong) return true;
50  return false;
51  }
52 
54  if(type>=pvByte && type<=pvDouble) return true;
55  return false;
56  }
57 
59  if(type>=pvBoolean && type<=pvDouble) return true;
60  return false;
61  }
62 
63  static const char* names[] = {
64  "boolean",
65  "byte", "short", "int", "long",
66  "ubyte", "ushort", "uint", "ulong",
67  "float", "double", "string",
68  };
69  ScalarType getScalarType(const string& pvalue) {
70  for(size_t i=0; i<NELEMENTS(names); i++)
71  if(pvalue==names[i])
72  return ScalarType(i);
73  THROW_EXCEPTION2(std::invalid_argument, "error unknown ScalarType");
74  }
75 
76  const char* name(ScalarType t) {
77  if (t<pvBoolean || t>pvString)
78  THROW_EXCEPTION2(std::invalid_argument, "error unknown ScalarType");
79  return names[t];
80  }
81 
83  {
84  switch(id) {
85 #define OP(ENUM, TYPE) case ENUM: return sizeof(TYPE)
86  OP(pvBoolean, boolean);
87  OP(pvUByte, uint8);
88  OP(pvByte, int8);
89  OP(pvUShort, uint16);
90  OP(pvShort, int16);
91  OP(pvUInt, uint32);
92  OP(pvInt, int32);
93  OP(pvULong, uint64);
94  OP(pvLong, int64);
95  OP(pvFloat, float);
96  OP(pvDouble, double);
97  OP(pvString, string);
98 #undef OP
99  default:
100  THROW_EXCEPTION2(std::invalid_argument, "error unknown ScalarType");
101  }
102  }
103 
105  {
106  switch(id) {
107 #define OP(ENUM, TYPE) case ENUM: return static_shared_vector_cast<void>(shared_vector<TYPE>(len))
108  OP(pvBoolean, boolean);
109  OP(pvUByte, uint8);
110  OP(pvByte, int8);
111  OP(pvUShort, uint16);
112  OP(pvShort, int16);
113  OP(pvUInt, uint32);
114  OP(pvInt, int32);
115  OP(pvULong, uint64);
116  OP(pvLong, int64);
117  OP(pvFloat, float);
118  OP(pvDouble, double);
119  OP(pvString, string);
120 #undef OP
121  default:
122  throw std::bad_alloc();
123  }
124  }
125 
126 } // namespace ScalarTypeFunc
127 
128 std::ostream& operator<<(std::ostream& o, const ScalarType& scalarType)
129 {
130  return o << ScalarTypeFunc::name(scalarType);
131 }
132 
133 }}
int8_t int8
Definition: pvType.h:75
const char * name(ScalarType t)
Definition: TypeFunc.cpp:76
size_t elementSize(ScalarType id)
gives sizeof(T) where T depends on the scalar type id.
Definition: TypeFunc.cpp:82
A holder for a contiguous piece of memory.
Definition: sharedVector.h:27
#define THROW_EXCEPTION2(TYPE, MSG)
uint16_t uint16
Definition: pvType.h:95
int i
Definition: scan.c:967
ScalarType getScalarType(const string &pvalue)
Definition: TypeFunc.cpp:69
pvd::StructureConstPtr type
TODO only here because of the Lockable.
Definition: ntaggregate.cpp:16
std::ostream & operator<<(std::ostream &o, const Field &f)
Miscellaneous macro definitions.
shared_vector< void > allocArray(ScalarType id, size_t len)
Allocate an untyped array based on ScalarType.
Definition: TypeFunc.cpp:104
uint64_t uint64
Definition: pvType.h:103
bool isPrimitive(ScalarType type)
Definition: TypeFunc.cpp:58
#define OP(ENUM, TYPE)
bool isNumeric(ScalarType type)
Definition: TypeFunc.cpp:53
#define NELEMENTS(A)
Definition: aToIPAddr.c:21
bool isInteger(ScalarType type)
Definition: TypeFunc.cpp:43
int64_t int64
Definition: pvType.h:87
const char * name(Type t)
Definition: TypeFunc.cpp:29
int16_t int16
Definition: pvType.h:79
bool isUInteger(ScalarType type)
Definition: TypeFunc.cpp:48
int32_t int32
Definition: pvType.h:83
uint8_t uint8
Definition: pvType.h:91
uint32_t uint32
Definition: pvType.h:99