This is Unofficial EPICS BASE Doxygen Site
anyscalar.cpp
Go to the documentation of this file.
1 
2 #include <epicsAssert.h>
3 
4 #define epicsExportSharedSymbols
5 #include <shareLib.h>
6 
7 #include "pv/anyscalar.h"
8 
9 namespace epics {namespace pvData {
10 
12 {
13  if(type==pvString) {
14  new (_wrap.blob) std::string(*static_cast<const std::string*>(buf));
15 
16  } else {
17  memcpy(_wrap.blob, buf, ScalarTypeFunc::elementSize(type));
18  }
19 
20  _stype = type;
21 }
22 
24  :_stype(o._stype)
25 {
26  if(o._stype==pvString) {
27  new (_wrap.blob) std::string(o._as<std::string>());
28  } else if(o._stype!=(ScalarType)-1) {
29  memcpy(_wrap.blob, o._wrap.blob, sizeof(_largest_blob));
30  }
31 }
32 
33 #if __cplusplus>=201103L
35  :_stype(o._stype)
36 {
37  typedef std::string string;
38  if(o._stype==pvString) {
39  new (_wrap.blob) std::string();
40  _as<std::string>() = std::move(o._as<std::string>());
41  o._as<string>().~string();
42  } else if(o._stype!=(ScalarType)-1) {
43  memcpy(_wrap.blob, o._wrap.blob, sizeof(_largest_blob));
44  }
45  o._stype = (ScalarType)-1;
46 }
47 #endif
48 
50  if(_stype==pvString) {
51  typedef std::string string;
52  _as<string>().~string();
53  }
54  // other types need no cleanup
55  _stype = (ScalarType)-1;
56 }
57 
59  typedef std::string string;
60  switch((int)_stype) {
61  case -1:
62  switch((int)o._stype) {
63  case -1:
64  // nil <-> nil
65  break;
66  case pvString:
67  // nil <-> string
68  new (_wrap.blob) std::string();
69  _as<std::string>().swap(o._as<std::string>());
70  o._as<std::string>().~string();
71  break;
72  default:
73  // nil <-> non-string
74  memcpy(_wrap.blob, o._wrap.blob, sizeof(_largest_blob));
75  break;
76  }
77  break;
78  case pvString:
79  switch((int)o._stype) {
80  case -1:
81  // string <-> nil
82  new (o._wrap.blob) std::string();
83  _as<std::string>().swap(o._as<std::string>());
84  _as<std::string>().~string();
85  break;
86  case pvString:
87  // string <-> string
88  _as<std::string>().swap(o._as<std::string>());
89  break;
90  default: {
91  // string <-> non-string
92  std::string temp;
93  temp.swap(_as<std::string>());
94 
95  _as<std::string>().~string();
96 
97  memcpy(_wrap.blob, o._wrap.blob, sizeof(_largest_blob));
98 
99  new (o._wrap.blob) std::string();
100  temp.swap(o._as<std::string>());
101  }
102  break;
103  }
104  break;
105  default:
106  switch((int)o._stype) {
107  case -1:
108  // non-string <-> nil
109  memcpy(o._wrap.blob, _wrap.blob, sizeof(_largest_blob));
110  break;
111  case pvString: {
112  // non-string <-> string
113  std::string temp;
114  temp.swap(o._as<std::string>());
115 
116  o._as<std::string>().~string();
117 
118  memcpy(o._wrap.blob, _wrap.blob, sizeof(_largest_blob));
119 
120  new (_wrap.blob) std::string();
121  temp.swap(_as<std::string>());
122  }
123  break;
124  default:
125  // non-string <-> non-string
126  _largest_blob temp;
127  memcpy(&temp, _wrap.blob, sizeof(_largest_blob));
128  memcpy(_wrap.blob, o._wrap.blob, sizeof(_largest_blob));
129  memcpy(o._wrap.blob, &temp, sizeof(_largest_blob));
130  // std::swap(o._wrap.blob, _wrap.blob); // gcc <=4.3 doesn't like this
131  break;
132  }
133  break;
134  }
135  std::swap(_stype, o._stype);
136 }
137 const void* AnyScalar::bufferUnsafe() const {
138  if(_stype==pvString) {
139  return as<std::string>().c_str();
140  } else {
141  return _wrap.blob;
142  }
143 }
144 
145 std::ostream& operator<<(std::ostream& strm, const AnyScalar& v)
146 {
147  switch(v.type()) {
148 #define CASE(BASETYPE, PVATYPE, DBFTYPE, PVACODE) case pv ## PVACODE: strm<<v._as<PVATYPE>(); break;
149 #define CASE_REAL_INT64
150 #define CASE_STRING
151 #include "pv/typemap.h"
152 #undef CASE
153 #undef CASE_REAL_INT64
154 #undef CASE_STRING
155  default:
156  strm<<"(nil)"; break;
157  }
158  return strm;
159 }
160 
161 }} // namespace epics::pvData
size_t elementSize(ScalarType id)
gives sizeof(T) where T depends on the scalar type id.
Definition: TypeFunc.cpp:82
An EPICS-specific replacement for ANSI C&#39;s assert.
pvd::StructureConstPtr type
TODO only here because of the Lockable.
Definition: ntaggregate.cpp:16
friend epicsShareFunc std::ostream & operator<<(std::ostream &strm, const AnyScalar &v)
Definition: anyscalar.cpp:145
Mark external symbols and entry points for shared libraries.
const void * bufferUnsafe() const
Definition: anyscalar.cpp:137
ScalarType type() const
Type code of contained value. Or (ScalarType)-1 is empty.
Definition: anyscalar.h:158
void swap(shared_ptr< T > &a, shared_ptr< T > &b) BOOST_NOEXCEPT
Definition: shared_ptr.hpp:783
void swap(AnyScalar &o)
Definition: anyscalar.cpp:58