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

This class implements introspection object for a union. More...

#include "pvIntrospect.h"

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

Public Types

typedef Unionreference
 
typedef const Unionconst_reference
 
- Public Types inherited from epics::pvData::Field
enum  { isField =1 }
 

Public Member Functions

 POINTER_DEFINITIONS (Union)
 
virtual ~Union ()
 
std::size_t getNumberFields () const
 
FieldConstPtr getField (std::string const &fieldName) const
 
template<typename FT >
std::tr1::shared_ptr< const FT > getField (std::string const &fieldName) const
 
FieldConstPtr getFieldT (std::string const &fieldName) const
 
template<typename FT >
std::tr1::shared_ptr< const FT > getFieldT (std::string const &fieldName) const
 
FieldConstPtr getField (std::size_t index) const
 
template<typename FT >
std::tr1::shared_ptr< const FT > getField (std::size_t index) const
 
FieldConstPtr getFieldT (std::size_t index) const
 
template<typename FT >
std::tr1::shared_ptr< const FT > getFieldT (std::size_t index) const
 
std::size_t getFieldIndex (std::string const &fieldName) const
 
FieldConstPtrArray const & getFields () const
 
StringArray const & getFieldNames () const
 
std::string getFieldName (std::size_t fieldIndex) const
 
bool isVariant () const
 
int32 guess (Type t, ScalarType s) const
 
virtual std::string getID () const OVERRIDE FINAL
 
virtual std::ostream & dump (std::ostream &o) const OVERRIDE FINAL
 
virtual void serialize (ByteBuffer *buffer, SerializableControl *control) const OVERRIDE FINAL
 
virtual void deserialize (ByteBuffer *buffer, DeserializableControl *control) OVERRIDE FINAL
 
std::tr1::shared_ptr< PVUnionbuild () const
 
- Public Member Functions inherited from epics::pvData::Field
 POINTER_DEFINITIONS (Field)
 
virtual ~Field ()
 
Type getType () const
 
std::tr1::shared_ptr< PVFieldbuild () const
 
- Public Member Functions inherited from epics::pvData::Serializable
virtual ~Serializable ()
 

Static Public Member Functions

static const std::string & defaultId ()
 
static const std::string & anyId ()
 

Static Public Attributes

static const std::string DEFAULT_ID = Union::defaultId()
 
static const std::string ANY_ID = Union::anyId()
 
- Static Public Attributes inherited from epics::pvData::Field
static size_t num_instances
 

Protected Member Functions

 Union ()
 
 Union (StringArray const &fieldNames, FieldConstPtrArray const &fields, std::string const &id=defaultId())
 
- Protected Member Functions inherited from epics::pvData::Field
 Field (Type type)
 
void cacheCleanup ()
 

Friends

class FieldCreate
 
class Structure
 

Detailed Description

This class implements introspection object for a union.

Definition at line 866 of file pvIntrospect.h.

Member Typedef Documentation

Definition at line 894 of file pvIntrospect.h.

Definition at line 893 of file pvIntrospect.h.

Constructor & Destructor Documentation

epics::pvData::Union::~Union ( )
virtual

Definition at line 731 of file FieldCreateFactory.cpp.

732 {
733  cacheCleanup();
734 }
epics::pvData::Union::Union ( )
protected

Definition at line 682 of file FieldCreateFactory.cpp.

683 : Field(union_),
684  fieldNames(),
685  fields(),
686  id(anyId())
687 {
688 }
static const std::string & anyId()
epics::pvData::Union::Union ( StringArray const &  fieldNames,
FieldConstPtrArray const &  fields,
std::string const &  id = defaultId() 
)
protected

Member Function Documentation

const string & epics::pvData::Union::anyId ( )
static

Get the default variant union ID.

Returns
The default variant union ID.

Definition at line 676 of file FieldCreateFactory.cpp.

677 {
678  static const string id = "any";
679  return id;
680 }
std::tr1::shared_ptr< PVUnion > epics::pvData::Union::build ( ) const

Allocate a new instance

Version
Added after 7.0.0

Definition at line 882 of file FieldCreateFactory.cpp.

883 {
884  return getPVDataCreate()->createPVUnion(std::tr1::static_pointer_cast<const Union>(shared_from_this()));
885 }
FORCE_INLINE const PVDataCreatePtr & getPVDataCreate()
Definition: pvData.h:1648
const string & epics::pvData::Union::defaultId ( )
static

Get the default union ID.

Returns
The default union ID.

Definition at line 668 of file FieldCreateFactory.cpp.

669 {
670  static const string id = "union";
671  return id;
672 }
void epics::pvData::Union::deserialize ( ByteBuffer buffer,
DeserializableControl flusher 
)
virtual

Deserialize buffer.

Parameters
bufferserialization buffer.
flusherdeserialization control.

Implements epics::pvData::Serializable.

Definition at line 878 of file FieldCreateFactory.cpp.

878  {
879  throw std::runtime_error("not valid operation, use FieldCreate::deserialize instead");
880 }
std::ostream & epics::pvData::Union::dump ( std::ostream &  o) const
virtual

Puts the string representation to the stream.

Parameters
ooutput stream.
Returns
The output stream.

Implements epics::pvData::Field.

Definition at line 812 of file FieldCreateFactory.cpp.

813 {
814  o << format::indent() << getID() << std::endl;
815  {
816  format::indent_scope s(o);
817  dumpFields(o);
818  }
819  return o;
820 }
virtual std::string getID() const OVERRIDE FINAL
FieldConstPtr epics::pvData::Union::getField ( std::string const &  fieldName) const

Lookup Field by name

Parameters
fieldNameMember field name. May not contain '.'
Returns
NULL if no member by this name.
template<typename FT >
std::tr1::shared_ptr<const FT> epics::pvData::Union::getField ( std::string const &  fieldName) const
inline

Lookup Field by name and cast to Field sub-class.

Parameters
fieldNameMember field name. May not contain '.'
Returns
NULL If no member by this name, or member exists, but has type other than FT.

Definition at line 914 of file pvIntrospect.h.

915  {
916  STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
917  return std::tr1::dynamic_pointer_cast<const FT>(getField(fieldName));
918  }
#define STATIC_ASSERT(expr)
Declare a condition that should be true at compile-time.
Definition: epicsAssert.h:86
FieldConstPtr getField(std::string const &fieldName) const
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r) BOOST_NOEXCEPT
Definition: shared_ptr.hpp:808
FieldConstPtr epics::pvData::Union::getField ( std::size_t  index) const
inline

Lookup Field by index, within this Union.

Parameters
indexIndex of member in this union.
index>=0 && index<getNumberFields()
Returns
Field pointer (never NULL)
Exceptions
std::out_of_rangeIf index >= getNumberFields()

Definition at line 952 of file pvIntrospect.h.

952 {return fields.at(index);}
template<typename FT >
std::tr1::shared_ptr<const FT> epics::pvData::Union::getField ( std::size_t  index) const
inline

Lookup Field by index, within this Union.

Parameters
indexIndex of member in this union.
index>=0 && index<getNumberFields()
Returns
NULL if member is not a sub-class of FT
Exceptions
std::out_of_rangeIf index >= getNumberFields()

Definition at line 960 of file pvIntrospect.h.

961  {
962  STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
963  return std::tr1::dynamic_pointer_cast<const FT>(getField(index));
964  }
#define STATIC_ASSERT(expr)
Declare a condition that should be true at compile-time.
Definition: epicsAssert.h:86
FieldConstPtr getField(std::string const &fieldName) const
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r) BOOST_NOEXCEPT
Definition: shared_ptr.hpp:808
size_t epics::pvData::Union::getFieldIndex ( std::string const &  fieldName) const

Get the field index for the specified fieldName.

Returns
The introspection interface. This will be -1 if the field is not in the union.

Definition at line 787 of file FieldCreateFactory.cpp.

787  {
788  size_t numberFields = fields.size();
789  for(size_t i=0; i<numberFields; i++) {
790  FieldConstPtr pfield = fields[i];
791  int result = fieldName.compare(fieldNames[i]);
792  if(result==0) return i;
793  }
794  return -1;
795 }
pvac::PutEvent result
Definition: clientSync.cpp:117
int i
Definition: scan.c:967
std::tr1::shared_ptr< const Field > FieldConstPtr
Definition: pvIntrospect.h:137
std::string epics::pvData::Union::getFieldName ( std::size_t  fieldIndex) const
inline

Get the name of the field with the specified index;

Parameters
fieldIndexThe index of the desired field.
Returns
The fieldName.

Definition at line 1014 of file pvIntrospect.h.

1014 {return fieldNames.at(fieldIndex);}
StringArray const& epics::pvData::Union::getFieldNames ( ) const
inline

Get the names of the fields in the union.

Returns
The array of fieldNames.

Definition at line 1008 of file pvIntrospect.h.

1008 {return fieldNames;}
FieldConstPtrArray const& epics::pvData::Union::getFields ( ) const
inline

Get the fields in the union.

Returns
The array of fields.

Definition at line 1003 of file pvIntrospect.h.

1003 {return fields;}
FieldConstPtr epics::pvData::Union::getFieldT ( std::string const &  fieldName) const
inline

Lookup Field by name

Parameters
fieldNameMember field name. May not contain '.'
Returns
Field pointer (never NULL)
Exceptions
std::runtime_errorIf no member by this name

Definition at line 926 of file pvIntrospect.h.

926 {return getFieldImpl(fieldName, true);};
template<typename FT >
std::tr1::shared_ptr<const FT> epics::pvData::Union::getFieldT ( std::string const &  fieldName) const
inline

Lookup Field by name and cast to Field sub-class.

Parameters
fieldNameMember field name. May not contain '.'
Returns
Field pointer (never NULL)
Exceptions
std::runtime_errorIf no member by this name, or member exists, but has type other than FT.

Definition at line 934 of file pvIntrospect.h.

935  {
936  STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
937  std::tr1::shared_ptr<const FT> result(
938  std::tr1::dynamic_pointer_cast<const FT>(getFieldT(fieldName))
939  );
940 
941  if (!result)
942  throw std::runtime_error("Wrong Field type");
943 
944  return result;
945  }
pvac::PutEvent result
Definition: clientSync.cpp:117
FieldConstPtr getFieldT(std::string const &fieldName) const
Definition: pvIntrospect.h:926
#define STATIC_ASSERT(expr)
Declare a condition that should be true at compile-time.
Definition: epicsAssert.h:86
FieldConstPtr epics::pvData::Union::getFieldT ( std::size_t  index) const
inline

Lookup Field by index, within this Union.

Parameters
indexIndex of member in this union.
index>=0 && index<getNumberFields()
Returns
Field pointer (never NULL)
Exceptions
std::out_of_rangeIf index >= getNumberFields()

Definition at line 971 of file pvIntrospect.h.

971 {return fields.at(index);}
template<typename FT >
std::tr1::shared_ptr<const FT> epics::pvData::Union::getFieldT ( std::size_t  index) const
inline

Lookup Field by index, within this Structure.

Parameters
indexIndex of member in this structure.
index>=0 && index<getNumberFields()
Returns
Field pointer (never NULL)
Exceptions
std::out_of_rangeIf index >= getNumberFields()
std::runtime_errorIf member is not a sub-class of FT

Definition at line 980 of file pvIntrospect.h.

981  {
982  STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class
983  std::tr1::shared_ptr<const FT> result(
984  std::tr1::dynamic_pointer_cast<const FT>(getFieldT(index))
985  );
986 
987  if (!result)
988  throw std::runtime_error("Wrong Field type");
989 
990  return result;
991  }
pvac::PutEvent result
Definition: clientSync.cpp:117
FieldConstPtr getFieldT(std::string const &fieldName) const
Definition: pvIntrospect.h:926
#define STATIC_ASSERT(expr)
Declare a condition that should be true at compile-time.
Definition: epicsAssert.h:86
string epics::pvData::Union::getID ( ) const
virtual

Get the identification string.

Returns
The identification string, can be empty.

Implements epics::pvData::Field.

Definition at line 772 of file FieldCreateFactory.cpp.

773 {
774  return id;
775 }
std::size_t epics::pvData::Union::getNumberFields ( ) const
inline

Get the number of immediate subfields in the union.

Returns
The number of fields.

Definition at line 900 of file pvIntrospect.h.

900 {return fieldNames.size();}
int32 epics::pvData::Union::guess ( Type  t,
ScalarType  s 
) const

Attempt to find an suitable member to stored the specified type.

Returned index is guerenteed to by of specified Type (either scalar or scalarArray). Provided ScalarType is taken as a hint.

Parameters
tMust be either scalar or scalarArray
sThe preferred ScalarType
Returns
A valid index or -1

Definition at line 736 of file FieldCreateFactory.cpp.

737 {
738  if(t!=scalar && t!=scalarArray)
739  THROW_EXCEPTION2(std::logic_error, "PVUnion::guess() only support scalar and scalarArray");
740 
741  int32 ret = -1;
742  for(size_t i=0, N=fields.size(); i<N; i++)
743  {
744  if(fields[i]->getType()!=t)
745  continue;
746 
748  switch(fields[i]->getType()) {
749  case scalar:
750  type = static_cast<const Scalar*>(fields[i].get())->getScalarType();
751  break;
752  case scalarArray:
753  type = static_cast<const ScalarArray*>(fields[i].get())->getElementType();
754  break;
755  default:
756  continue;
757  }
758 
759  if(type==s) {
760  // exact match
761  ret = i;
762  break; // we're done
763 
764  } else if(ret==-1) {
765  // first partial match
766  ret = i;
767  }
768  }
769  return ret;
770 }
#define THROW_EXCEPTION2(TYPE, MSG)
int i
Definition: scan.c:967
ScalarType getScalarType(const string &pvalue)
Definition: TypeFunc.cpp:69
pvd::StructureConstPtr type
Type getType() const
Definition: pvIntrospect.h:348
int32_t int32
Definition: pvType.h:83
bool epics::pvData::Union::isVariant ( ) const
inline

Check if this union is variant union (aka any type).

Returns
true if this union is variant union, otherwise false.

Definition at line 1019 of file pvIntrospect.h.

1019 {return (fieldNames.size() == 0);}
epics::pvData::Union::POINTER_DEFINITIONS ( Union  )
void epics::pvData::Union::serialize ( ByteBuffer buffer,
SerializableControl flusher 
) const
virtual

Serialize field into given buffer.

Parameters
bufferserialization buffer.
flusherflush interface.

Implements epics::pvData::Serializable.

Definition at line 864 of file FieldCreateFactory.cpp.

864  {
865  control->ensureBuffer(1);
866  if (fields.size() == 0)
867  {
868  // unrestricted/variant union
869  buffer->putByte((int8)0x82);
870  }
871  else
872  {
873  buffer->putByte((int8)0x81);
874  serializeUnionField(this, buffer, control);
875  }
876 }
int8_t int8
Definition: pvType.h:75

Friends And Related Function Documentation

friend class FieldCreate
friend

Definition at line 1054 of file pvIntrospect.h.

friend class Structure
friend

Definition at line 1055 of file pvIntrospect.h.

Member Data Documentation

const string epics::pvData::Union::ANY_ID = Union::anyId()
static

Default variant union ID.

Definition at line 884 of file pvIntrospect.h.

const string epics::pvData::Union::DEFAULT_ID = Union::defaultId()
static

Default union ID.

Definition at line 873 of file pvIntrospect.h.


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