This is Unofficial EPICS BASE Doxygen Site
epics::pvData::PVUnion Class Reference

PVUnion has a single subfield. More...

#include "pvData.h"

+ Inheritance diagram for epics::pvData::PVUnion:
+ Collaboration diagram for epics::pvData::PVUnion:

Public Types

typedef PVUnionreference
 
typedef const PVUnionconst_reference
 
- Public Types inherited from epics::pvData::PVField
enum  { isPVField =1 }
 

Public Member Functions

 POINTER_DEFINITIONS (PVUnion)
 
virtual ~PVUnion ()
 
const UnionConstPtrgetUnion () const
 
const PVFieldPtrget ()
 
PVField::const_shared_pointer get () const
 
template<typename PVT >
std::tr1::shared_ptr< PVT > get ()
 
template<typename PVT >
std::tr1::shared_ptr< const PVT > get () const
 
PVFieldPtr select (int32 index)
 
template<typename PVT >
std::tr1::shared_ptr< PVT > select (int32 index)
 
PVFieldPtr select (std::string const &fieldName)
 
template<typename PVT >
std::tr1::shared_ptr< PVT > select (std::string const &fieldName)
 
int32 getSelectedIndex () const
 
std::string getSelectedFieldName () const
 
void set (PVFieldPtr const &value)
 
void set (int32 index, PVFieldPtr const &value)
 
void set (std::string const &fieldName, PVFieldPtr const &value)
 
virtual void serialize (ByteBuffer *pbuffer, SerializableControl *pflusher) const OVERRIDE FINAL
 
virtual void deserialize (ByteBuffer *pbuffer, DeserializableControl *pflusher) OVERRIDE FINAL
 
 PVUnion (UnionConstPtr const &punion)
 
virtual std::ostream & dumpValue (std::ostream &o) const OVERRIDE FINAL
 
void copy (const PVUnion &from)
 
void copyUnchecked (const PVUnion &from)
 
- Public Member Functions inherited from epics::pvData::PVField
 POINTER_DEFINITIONS (PVField)
 
virtual ~PVField ()
 
const std::string & getFieldName () const
 
std::string getFullName () const
 
std::size_t getFieldOffset () const
 
std::size_t getNextFieldOffset () const
 
std::size_t getNumberFields () const
 
bool isImmutable () const
 
virtual void setImmutable ()
 
const FieldConstPtrgetField () const
 
PVStructuregetParent ()
 
const PVStructuregetParent () const
 
void postPut ()
 
void setPostHandler (PostHandlerPtr const &postHandler)
 
virtual bool equals (PVField &pv)
 
void copy (const PVField &from)
 
void copyUnchecked (const PVField &from)
 
- Public Member Functions inherited from epics::pvData::Serializable
virtual ~Serializable ()
 

Static Public Attributes

static const int32 UNDEFINED_INDEX = PVUNION_UNDEFINED_INDEX
 
- Static Public Attributes inherited from epics::pvData::PVField
static size_t num_instances
 

Friends

class PVDataCreate
 

Additional Inherited Members

- Protected Member Functions inherited from epics::pvData::PVField
PVField::shared_pointer getPtrSelf ()
 
 PVField (FieldConstPtr field)
 
void setParentAndName (PVStructure *parent, std::string const &fieldName)
 

Detailed Description

PVUnion has a single subfield.

The type for the subfield is specified by a union introspection interface.

Definition at line 940 of file pvData.h.

Member Typedef Documentation

Definition at line 949 of file pvData.h.

Definition at line 948 of file pvData.h.

Constructor & Destructor Documentation

epics::pvData::PVUnion::~PVUnion ( )
virtual

Destructor

Definition at line 43 of file PVUnion.cpp.

43 {}
epics::pvData::PVUnion::PVUnion ( UnionConstPtr const &  punion)
explicit

Constructor

Parameters
punionThe introspection interface.

Definition at line 32 of file PVUnion.cpp.

33 : PVField(unionPtr),
34  unionPtr(unionPtr),
35  selector(PVUNION_UNDEFINED_INDEX), // to allow out-of-order static initialization
36  value(),
37  variant(unionPtr->isVariant())
38 {
39 }
Definition: link.h:174
#define PVUNION_UNDEFINED_INDEX
Definition: PVUnion.cpp:27
PVField(FieldConstPtr field)
Definition: PVField.cpp:30

Member Function Documentation

void epics::pvData::PVUnion::copy ( const PVUnion from)

Definition at line 199 of file PVUnion.cpp.

200 {
201  if(isImmutable())
202  throw std::invalid_argument("destination is immutable");
203 
204  if(*getUnion() != *from.getUnion())
205  throw std::invalid_argument("union definitions do not match");
206 
207  copyUnchecked(from);
208 }
const UnionConstPtr & getUnion() const
Definition: pvData.h:962
bool isImmutable() const
Definition: pvData.h:198
void copyUnchecked(const PVUnion &from)
Definition: PVUnion.cpp:210
void epics::pvData::PVUnion::copyUnchecked ( const PVUnion from)

Definition at line 210 of file PVUnion.cpp.

211 {
212 
213  const PVField::const_shared_pointer& fromValue = from.get();
214  if (from.getUnion()->isVariant())
215  {
216  if (fromValue.get() == 0)
217  {
218  set(PVField::shared_pointer());
219  }
220  else
221  {
222  PVFieldPtr toValue = get();
223  if (toValue.get() == 0 || *toValue->getField() != *fromValue->getField())
224  {
225  toValue = pvDataCreate->createPVField(fromValue->getField());
226  toValue->copyUnchecked(*fromValue);
227  set(toValue);
228  }
229  else
230  {
231  toValue->copyUnchecked(*fromValue);
232  postPut();
233  }
234  }
235  }
236  else
237  {
238  if (fromValue.get() == 0)
239  {
241  }
242  else
243  {
244  select(from.getSelectedIndex())->copyUnchecked(*fromValue);
245  }
246  postPut();
247  }
248 
249 }
PVFieldPtr select(int32 index)
Definition: PVUnion.cpp:54
std::tr1::shared_ptr< PVField > PVFieldPtr
Definition: pvData.h:66
void copyUnchecked(const PVUnion &from)
Definition: PVUnion.cpp:210
static const int32 UNDEFINED_INDEX
Definition: pvData.h:956
void epics::pvData::PVUnion::deserialize ( ByteBuffer pbuffer,
DeserializableControl pflusher 
)
virtual

Deserialize

Parameters
pbufferThe byte buffer.
pflusherInterface to call when buffer is empty.

Implements epics::pvData::Serializable.

Definition at line 144 of file PVUnion.cpp.

145 {
146  if (variant)
147  {
148  FieldConstPtr field = pcontrol->cachedDeserialize(pbuffer);
149  if (field.get())
150  {
151  // try to reuse existing field instance
152  if (!value.get() || *value->getField() != *field)
153  value = pvDataCreate->createPVField(field);
154  value->deserialize(pbuffer, pcontrol);
155  }
156  else
157  value.reset();
158  }
159  else
160  {
161  int32 previousSelector = selector;
162  selector = static_cast<int32>(SerializeHelper::readSize(pbuffer, pcontrol));
163  if (selector != UNDEFINED_INDEX)
164  {
165  if (selector != previousSelector)
166  {
167  FieldConstPtr field = unionPtr->getField(selector);
168  // try to reuse existing field instance
169  if (!value.get() || *value->getField() != *field)
170  value = pvDataCreate->createPVField(field);
171  }
172  value->deserialize(pbuffer, pcontrol);
173  }
174  else
175  value.reset();
176  }
177 }
Definition: link.h:174
static std::size_t readSize(ByteBuffer *buffer, DeserializableControl *control)
std::tr1::shared_ptr< const Field > FieldConstPtr
Definition: pvIntrospect.h:137
char * pbuffer
Definition: errlog.c:85
int32_t int32
Definition: pvType.h:83
static const int32 UNDEFINED_INDEX
Definition: pvData.h:956
std::ostream & epics::pvData::PVUnion::dumpValue ( std::ostream &  o) const
virtual

Puts the PVField raw value to the stream.

Parameters
ooutput stream.
Returns
The output stream.

Implements epics::pvData::PVField.

Definition at line 179 of file PVUnion.cpp.

180 {
181  o << format::indent() << getUnion()->getID() << ' ' << getFieldName() << std::endl;
182  {
183  format::indent_scope s(o);
184  const PVField::const_shared_pointer& fieldField = get();
185  if (fieldField.get() == NULL)
186  o << format::indent() << "(none)" << std::endl;
187  else
188  {
189  Type type = fieldField->getField()->getType();
190  if (type == scalar || type == scalarArray)
191  o << format::indent() << fieldField->getField()->getID() << ' ' << fieldField->getFieldName() << ' ' << *(fieldField.get()) << std::endl;
192  else
193  o << *(fieldField.get());
194  }
195  }
196  return o;
197 }
const std::string & getFieldName() const
Definition: pvData.h:166
const UnionConstPtr & getUnion() const
Definition: pvData.h:962
pvd::StructureConstPtr type
#define NULL
Definition: catime.c:38
const PVFieldPtr& epics::pvData::PVUnion::get ( )
inline

Get the PVField value stored in the field.

Returns
PVField value of field, null if .

Definition at line 968 of file pvData.h.

968 { return value; }
Definition: link.h:174
PVField::const_shared_pointer epics::pvData::PVUnion::get ( ) const
inline

Definition at line 969 of file pvData.h.

969 { return value; }
Definition: link.h:174
template<typename PVT >
std::tr1::shared_ptr<PVT> epics::pvData::PVUnion::get ( )
inline

Definition at line 972 of file pvData.h.

972  {
973  STATIC_ASSERT(PVT::isPVField); // only allow cast from PVField sub-class
974  return std::tr1::dynamic_pointer_cast<PVT>(get());
975  }
#define STATIC_ASSERT(expr)
Declare a condition that should be true at compile-time.
Definition: epicsAssert.h:86
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r) BOOST_NOEXCEPT
Definition: shared_ptr.hpp:808
template<typename PVT >
std::tr1::shared_ptr<const PVT> epics::pvData::PVUnion::get ( ) const
inline

Definition at line 978 of file pvData.h.

978  {
979  STATIC_ASSERT(PVT::isPVField); // only allow cast from PVField sub-class
980  return std::tr1::dynamic_pointer_cast<const PVT>(get());
981  }
#define STATIC_ASSERT(expr)
Declare a condition that should be true at compile-time.
Definition: epicsAssert.h:86
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r) BOOST_NOEXCEPT
Definition: shared_ptr.hpp:808
string epics::pvData::PVUnion::getSelectedFieldName ( ) const

Get selected field name.

Returns
selected field name, empty string if field does not exist.

Definition at line 45 of file PVUnion.cpp.

46 {
47  // no name for undefined and for variant unions
48  if (selector == UNDEFINED_INDEX)
49  return string();
50  else
51  return unionPtr->getFieldName(selector);
52 }
static const int32 UNDEFINED_INDEX
Definition: pvData.h:956
int32 epics::pvData::PVUnion::getSelectedIndex ( ) const
inline

Get selected field index.

Returns
selected field index.

Definition at line 1014 of file pvData.h.

1014 { return selector; }
const UnionConstPtr& epics::pvData::PVUnion::getUnion ( ) const
inline

Get the introspection interface

Returns
The interface.

Definition at line 962 of file pvData.h.

962 { return unionPtr; }
epics::pvData::PVUnion::POINTER_DEFINITIONS ( PVUnion  )
PVFieldPtr epics::pvData::PVUnion::select ( int32  index)

Select field (set index) and get the field at the index.

Parameters
indexindex of the field to select.
Returns
corresponding PVField (of undetermined value), null if
index == UNDEFINED_INDEX
.
Exceptions
std::invalid_argumentif index is invalid (out of range).

Definition at line 54 of file PVUnion.cpp.

55 {
56  if (variant && index != UNDEFINED_INDEX)
57  throw std::invalid_argument("index out of bounds");
58 
59  // no change
60  if (selector == index && !variant)
61  return value;
62 
63  if (index == UNDEFINED_INDEX)
64  {
65  selector = UNDEFINED_INDEX;
66  value.reset();
67  return value;
68  }
69  else if (index < 0 || size_t(index) >= unionPtr->getFields().size())
70  throw std::invalid_argument("index out of bounds");
71 
72  FieldConstPtr field = unionPtr->getField(index);
73  selector = index;
74  value = pvDataCreate->createPVField(field);
75 
76  return value;
77 }
Definition: link.h:174
std::tr1::shared_ptr< const Field > FieldConstPtr
Definition: pvIntrospect.h:137
static const int32 UNDEFINED_INDEX
Definition: pvData.h:956
template<typename PVT >
std::tr1::shared_ptr<PVT> epics::pvData::PVUnion::select ( int32  index)
inline

Definition at line 992 of file pvData.h.

992  {
993  STATIC_ASSERT(PVT::isPVField); // only allow cast from PVField sub-class
994  return std::tr1::dynamic_pointer_cast<PVT>(select(index));
995  }
#define STATIC_ASSERT(expr)
Declare a condition that should be true at compile-time.
Definition: epicsAssert.h:86
PVFieldPtr select(int32 index)
Definition: PVUnion.cpp:54
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r) BOOST_NOEXCEPT
Definition: shared_ptr.hpp:808
PVFieldPtr epics::pvData::PVUnion::select ( std::string const &  fieldName)

Select field (set index) and get the field by given name.

Parameters
fieldNamethe name of the field to select.
Returns
corresponding PVField (of undetermined value).
Exceptions
std::invalid_argumentif field does not exist.
template<typename PVT >
std::tr1::shared_ptr<PVT> epics::pvData::PVUnion::select ( std::string const &  fieldName)
inline

Definition at line 1006 of file pvData.h.

1006  {
1007  return std::tr1::dynamic_pointer_cast<PVT>(select(fieldName));
1008  }
PVFieldPtr select(int32 index)
Definition: PVUnion.cpp:54
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r) BOOST_NOEXCEPT
Definition: shared_ptr.hpp:808
void epics::pvData::PVUnion::serialize ( ByteBuffer pbuffer,
SerializableControl pflusher 
) const
virtual

Serialize.

Parameters
pbufferThe byte buffer.
pflusherInterface to call when buffer is full.

Implements epics::pvData::Serializable.

Definition at line 121 of file PVUnion.cpp.

122 {
123  if (variant)
124  {
125  // write introspection data
126  if (value.get() == 0) {
127  pflusher->ensureBuffer(1);
128  pbuffer->put((int8)-1);
129  }else {
130  pflusher->cachedSerialize(value->getField(), pbuffer);
131  value->serialize(pbuffer, pflusher);
132  }
133  }
134  else
135  {
136  // write selector value
137  SerializeHelper::writeSize(selector, pbuffer, pflusher);
138  // write value, no value for UNDEFINED_INDEX
139  if (selector != UNDEFINED_INDEX)
140  value->serialize(pbuffer, pflusher);
141  }
142 }
int8_t int8
Definition: pvType.h:75
Definition: link.h:174
static void writeSize(std::size_t s, ByteBuffer *buffer, SerializableControl *flusher)
char * pbuffer
Definition: errlog.c:85
static const int32 UNDEFINED_INDEX
Definition: pvData.h:956
void epics::pvData::PVUnion::set ( PVFieldPtr const &  value)
inline

Set the PVField (by reference!) as selected field. If a value is not a valid union field an std::invalid_argument exception is thrown.

Parameters
valuethe field to set.

Definition at line 1028 of file pvData.h.

1028  {
1029  set(selector, value);
1030  }
Definition: link.h:174
void epics::pvData::PVUnion::set ( int32  index,
PVFieldPtr const &  value 
)

Set the PVField (by reference!) as field at given index. If a value is not a valid union field an std::invalid_argument exception is thrown. Use select(int32) to put by value.

Parameters
indexindex of a field to put.
valuethe field to set.
See also
select(int32)

Definition at line 87 of file PVUnion.cpp.

88 {
89  if (variant && index != UNDEFINED_INDEX)
90  throw std::invalid_argument("index out of bounds");
91  else if (!variant)
92  {
93  if (index == UNDEFINED_INDEX)
94  {
95  // for undefined index we accept only null values
96  if (value)
97  throw std::invalid_argument("non-null value for index == UNDEFINED_INDEX");
98  }
99  else if (index < 0 || size_t(index) >= unionPtr->getFields().size())
100  throw std::invalid_argument("index out of bounds");
101  else if (!value)
102  throw std::invalid_argument("Can't set defined index w/ NULL");
103  else if (value->getField() != unionPtr->getField(index))
104  throw std::invalid_argument("selected field and its introspection data do not match");
105  }
106 
107  selector = index;
108  this->value = value;
109  postPut();
110 }
Definition: link.h:174
static const int32 UNDEFINED_INDEX
Definition: pvData.h:956
void epics::pvData::PVUnion::set ( std::string const &  fieldName,
PVFieldPtr const &  value 
)

Set the PVField (by reference!) as field by given name. If a value is not a valid union field an std::invalid_argument exception is thrown. Use select(std::string const &) to put by value.

Parameters
fieldNameName of the field to put.
valuethe field to set.
See also
select(std::string const &)

Friends And Related Function Documentation

friend class PVDataCreate
friend

Definition at line 1081 of file pvData.h.

Member Data Documentation

const int32 epics::pvData::PVUnion::UNDEFINED_INDEX = PVUNION_UNDEFINED_INDEX
static

Undefined index. Default value upon PVUnion construction. Can be set by the user. Corresponds to null value.

Definition at line 956 of file pvData.h.


The documentation for this class was generated from the following files: