ISIS Logo
NINetVar
An EPICS support module to export National Instruments Network Shared Variables as process variables
cnvconvert.h
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2013 Science and Technology Facilities Council (STFC), GB.
3 * All rights reverved.
4 * This file is distributed subject to a Software License Agreement found
5 * in the file LICENSE.txt that is included with this distribution.
6 \*************************************************************************/
7 
10 
11 #ifndef CNVCONVERT_H
12 #define CNVCONVERT_H
13 
14 #include <type_traits>
15 
16 // CNV to C
17 
19 template<CNVDataType type>
20 struct CNV2C
21 {
22  typedef int ctype;
23  static const char* desc;
24  static void free(ctype);
25 };
26 
27 template<>
28 struct CNV2C<CNVBool>
29 {
30  typedef char ctype; // char not bool as we need a 1 byte data type for C call and i'm not sure about "bool"
31  static const char* desc;
32  static void free(ctype val) { }
33 };
34 
35 template<>
36 struct CNV2C<CNVString>
37 {
38  typedef char* ctype;
39  static const char* desc;
40  static void free(ctype val)
41  {
42  if (val != 0)
43  {
44  CNVFreeMemory(val);
45  }
46  }
47 };
48 
49 template<>
50 struct CNV2C<CNVSingle>
51 {
52  typedef float ctype;
53  static const char* desc;
54  static void free(ctype val) { }
55 };
56 
57 template<>
58 struct CNV2C<CNVDouble>
59 {
60  typedef double ctype;
61  static const char* desc;
62  static void free(ctype val) { }
63 };
64 
65 template<>
66 struct CNV2C<CNVInt8>
67 {
68  typedef char ctype;
69  static const char* desc;
70  static void free(ctype val) { }
71 };
72 
73 template<>
74 struct CNV2C<CNVUInt8>
75 {
76  typedef unsigned char ctype;
77  static const char* desc;
78  static void free(ctype val) { }
79 };
80 
81 template<>
82 struct CNV2C<CNVInt16>
83 {
84  typedef short ctype;
85  static const char* desc;
86  static void free(ctype val) { }
87 };
88 
89 template<>
90 struct CNV2C<CNVUInt16>
91 {
92  typedef unsigned short ctype;
93  static const char* desc;
94  static void free(ctype val) { }
95 };
96 
97 template<>
98 struct CNV2C<CNVInt32>
99 {
100  typedef int ctype;
101  static const char* desc;
102  static void free(ctype val) { }
103 };
104 
105 template<>
106 struct CNV2C<CNVUInt32>
107 {
108  typedef unsigned int ctype;
109  static const char* desc;
110  static void free(ctype val) { }
111 };
112 
113 template<>
114 struct CNV2C<CNVInt64>
115 {
116  typedef __int64 ctype;
117  static const char* desc;
118  static void free(ctype val) { }
119 };
120 
121 template<>
122 struct CNV2C<CNVUInt64>
123 {
124  typedef unsigned __int64 ctype;
125  static const char* desc;
126  static void free(ctype val) { }
127 };
128 
129 // C type to CNV
130 
132 template<typename T>
133 struct C2CNV
134 {
135  enum { nvtype = -1 }; // never used
136  static const char* desc;
137  static asynStatus (asynPortDriver::*asyn_callback)(T* value, size_t nElements, int reason, int addr);
138 };
139 
140 template<>
141 struct C2CNV<bool>
142 {
143  enum { nvtype = CNVBool };
144  static const char* desc;
145  static asynStatus (asynPortDriver::*asyn_callback)(epicsInt8* value, size_t nElements, int reason, int addr);
146 };
147 
148 template<>
149 struct C2CNV<char*>
150 {
151  enum { nvtype = CNVString };
152  static const char* desc;
153  static asynStatus (asynPortDriver::*asyn_callback)(char** value, size_t nElements, int reason, int addr);
154 };
155 
156 template<>
157 struct C2CNV<float>
158 {
159  enum { nvtype = CNVSingle };
160  static const char* desc;
161  static asynStatus (asynPortDriver::*asyn_callback)(epicsFloat32* value, size_t nElements, int reason, int addr);
162 };
163 
164 template<>
165 struct C2CNV<double>
166 {
167  enum { nvtype = CNVDouble };
168  static const char* desc;
169  static asynStatus (asynPortDriver::*asyn_callback)(epicsFloat64* value, size_t nElements, int reason, int addr);
170 };
171 
172 template<>
173 struct C2CNV<char>
174 {
175  enum { nvtype = CNVInt8 };
176  static const char* desc;
177  static asynStatus (asynPortDriver::*asyn_callback)(epicsInt8* value, size_t nElements, int reason, int addr);
178 };
179 
180 template<>
181 struct C2CNV<unsigned char>
182 {
183  enum { nvtype = CNVUInt8 };
184  static const char* desc;
185  static asynStatus (asynPortDriver::*asyn_callback)(epicsInt8* value, size_t nElements, int reason, int addr);
186 };
187 
188 template<>
189 struct C2CNV<short>
190 {
191  enum { nvtype = CNVInt16 };
192  static const char* desc;
193  static asynStatus (asynPortDriver::*asyn_callback)(epicsInt16* value, size_t nElements, int reason, int addr);
194 };
195 
196 template<>
197 struct C2CNV<unsigned short>
198 {
199  enum { nvtype = CNVUInt16 };
200  static const char* desc;
201  static asynStatus(asynPortDriver::*asyn_callback)(epicsInt16* value, size_t nElements, int reason, int addr);
202 };
203 
204 template<>
205 struct C2CNV<int>
206 {
207  enum { nvtype = CNVInt32 };
208  static const char* desc;
209  static asynStatus (asynPortDriver::*asyn_callback)(epicsInt32* value, size_t nElements, int reason, int addr);
210 };
211 
212 template<>
213 struct C2CNV<unsigned int>
214 {
215  enum { nvtype = CNVUInt32 };
216  static const char* desc;
217  static asynStatus (asynPortDriver::*asyn_callback)(epicsInt32* value, size_t nElements, int reason, int addr);
218 };
219 
220 template<>
221 struct C2CNV<__int64>
222 {
223  enum { nvtype = CNVInt64 };
224  static const char* desc;
225  static asynStatus (asynPortDriver::*asyn_callback)(__int64* value, size_t nElements, int reason, int addr);
226 };
227 
228 template<>
229 struct C2CNV<unsigned __int64>
230 {
231  enum { nvtype = CNVUInt64 };
232  static const char* desc;
233  static asynStatus (asynPortDriver::*asyn_callback)(__int64* value, size_t nElements, int reason, int addr);
234 };
235 
236 // type convertions - used when we set asyn parameters. We want to cast basic type -> basic type and pointer -> pointer, but ignore
237 // everything else which shouldn't get called anyway.
238 // e.g. convertToScalar<double>(value)
239 
241 template<typename T, typename U>
242 static T convertToScalar(U val)
243 {
244  return static_cast<T>(val);
245 }
246 
247 template<typename T, typename U>
248 static T convertToScalar(U* val)
249 {
250  throw std::runtime_error("convertToScalar: illegal cast of pointer to simple type");
251 // return static_cast<T>(0);
252 }
253 
254 template<typename T, typename U>
255 static T* convertToPtr(U val)
256 {
257  throw std::runtime_error("convertToPtr: illegal cast of simple type to pointer");
258 // return static_cast<T*>(0);
259 }
260 
261 // Types that differ only in sign are considered castable as epics asyn doesn't do unsigned data types for arrays
262 template<typename T, typename U>
263 static T* convertToPtr(U* val)
264 {
265 // if ( typeid(std::make_signed<T>::type) == typeid(std::make_signed<U>::type) )
266  if ( std::is_same< std::make_signed< std::remove_cv<T>::type >::type, std::make_signed< std::remove_cv<U>::type >::type >::value )
267  {
268  return reinterpret_cast<T*>(val);
269  }
270  else
271  {
272  return 0;
273  }
274 }
275 
276 #endif /* CNVCONVERT_H */
static const char * desc
Definition: cnvconvert.h:208
static const char * desc
Definition: cnvconvert.h:53
static const char * desc
Definition: cnvconvert.h:176
static void free(ctype val)
Definition: cnvconvert.h:110
static const char * desc
Definition: cnvconvert.h:93
static void free(ctype)
function to free any memory associated with type
static const char * desc
Definition: cnvconvert.h:31
static const char * desc
Definition: cnvconvert.h:192
unsigned char ctype
Definition: cnvconvert.h:76
static const char * desc
Definition: cnvconvert.h:224
static void free(ctype val)
Definition: cnvconvert.h:102
static T convertToScalar(U val)
convert one type to another
Definition: cnvconvert.h:242
static const char * desc
Definition: cnvconvert.h:69
static const char * desc
Definition: cnvconvert.h:61
unsigned short ctype
Definition: cnvconvert.h:92
static const char * desc
Definition: cnvconvert.h:216
unsigned int ctype
Definition: cnvconvert.h:108
static asynStatus(asynPortDriver::* asyn_callback)(T *value, size_t nElements, int reason, int addr)
Definition: cnvconvert.h:137
int ctype
an instance of the underlying type
Definition: cnvconvert.h:22
static const char * desc
Definition: cnvconvert.h:77
static void free(ctype val)
Definition: cnvconvert.h:40
unsigned __int64 ctype
Definition: cnvconvert.h:124
static const char * desc
Definition: cnvconvert.h:117
Provide the underlying C data type ctype for a given network shared variable type.
Definition: cnvconvert.h:20
static const char * desc
Definition: cnvconvert.h:136
static void free(ctype val)
Definition: cnvconvert.h:118
static const char * desc
Definition: cnvconvert.h:144
static const char * desc
Definition: cnvconvert.h:184
static void free(ctype val)
Definition: cnvconvert.h:126
static void free(ctype val)
Definition: cnvconvert.h:78
static const char * desc
Definition: cnvconvert.h:232
static const char * desc
description of type
Definition: cnvconvert.h:23
static void free(ctype val)
Definition: cnvconvert.h:70
static T * convertToPtr(U val)
Definition: cnvconvert.h:255
static void free(ctype val)
Definition: cnvconvert.h:62
static void free(ctype val)
Definition: cnvconvert.h:86
static const char * desc
Definition: cnvconvert.h:168
static const char * desc
Definition: cnvconvert.h:39
static const char * desc
Definition: cnvconvert.h:101
static void free(ctype val)
Definition: cnvconvert.h:94
static const char * desc
Definition: cnvconvert.h:160
static const char * desc
Definition: cnvconvert.h:85
static const char * desc
Definition: cnvconvert.h:152
static void free(ctype val)
Definition: cnvconvert.h:54
static void free(ctype val)
Definition: cnvconvert.h:32
static const char * desc
Definition: cnvconvert.h:109
static const char * desc
Definition: cnvconvert.h:125
For a given C data type, provide the appropriate network shared variable type.
Definition: cnvconvert.h:133
static const char * desc
Definition: cnvconvert.h:200
Copyright © 2013 Science and Technology Facilities Council | Generated by   doxygen 1.8.8