Difference between revisions of "V4 Design: epicsTypes"

From EPICSWIKI
m (switch capacity and elementSize arguments for EpicsArray constructors)
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
= EPICS: epicsTypes =
__TOC__


June 7, 2005
Marty Kraimer and Andrew Johnson


<center>
June 14, 2005
== Overview ==
</center>


This document describes the C++ definitions for storing
= Overview =
data that can be accessed without pre-complied code, i.e.
it has self describing features.


This document describes the fundamental C++ data types which will be used internally by iocCore and hence must be supported in EPICS Version 4.0.  We do not cover composite (<tt>struct</tt>) types here, as a generic interface to such types requires introspection information which is only provided at a higher level.


epicsTypes can be used by any code that stores data accessable via V4 Channel Access.
All data that is sent to or received from any EPICS record will be composed out of the fundamental data types described here.
Some examples are:
# IOC records - Most data accessable from outside record support is stored as an epicsType.
# Channel Access Gateway - Can be used to store and transfer data.
# Channel Access Clients - Can be used to get/receive data from CA server.


Standard support can be provided to access, via dataAccess, epicsType data.
The data types are:
For example standard support can be provided to move IOC record data between
record instances and a Channel Access server.


<tt>epicsType</tt> is an <tt>enum</tt> that defines the following:
* Primitive Types
* epicsTypeUnknown - Type is unknown
** epicsBoolean
* epicsTypeBoolean - true or false
** epicsOctet
* epicsTypeOctet - An eight bit byte. It is NOT an integer type
** epicsInt16
* epicsTypeInt16 - 16 bit signed integer
** epicsInt32
* epicsTypeInt32 - 32 bit signed integer
** epicsInt64
* epicsTypeInt64 - 64 bit signed integer
** epicsFloat32
* epicsTypeFloat32 - 32 bit IEEE float
** epicsFloat64
* epicsTypeFloat64 - 64 bit IEEE float
* Aggregate Types
* epicsTypeString - A UTF-8 Encoded Character String
** EpicsString
* epicsTypeArray - One dimensional array of any epicsType
** EpicsArray
* epicsTypeMDArray - Multidimensional array of type epicsTypeBoolean,..epicsTypeFloat64, or epicsTypeString.
** EpicsMDArray
* epicsTypeEnum - An index and associated set of choice strings.
** EpicsEnum
* epicsTypeStruct - A structure with fields each of which is any epicsType


== Comments on Type Safety ==


The actual types associated with the <tt>epicsType</tt>s are:
<tt>EpicsArray</tt> and <tt>EpicsMDArray</tt> as presented below are not type-safe, in that they take and return pointers to an <tt>epicsOctet</tt> which the user must cast to the appropriate element type. They also will not be able to default-construct or destruct their array elements, so cannot safely be used for complex C++ data types.  This will not be an issue when creating an array of strings as long as we ensure that a buffer is zero-filled at allocation.
   
    typedef bool              epicsBoolean;
    typedef char              epicsOctet;
    typedef short              epicsInt16;
    typedef int                epicsInt32;
    typedef long long         epicsInt64;
    typedef float              epicsFloat32;
    typedef double            epicsFloat64;


    //EpicsString holds UTF-8 characters
We could make versions of these classes that are templated on the element type, which default-construct and destruct their elements and even use the element type's assignment operator for copying operations.  However since Version 4.0 record types can contain array fields whose data types are not known until the database is loaded at runtime, we will still need to provide a non type-safe version of the array types.  For the moment we're going to work with these definitions.
    class EpicsString; // see below for details;


    class EpicsArray; // see below for details;
== Locking Issues ==


    class EpicsMDArray; // see below for details;
These fundamental EPICS datatypes do not provide facilities for preventing
simultaneous access by multiple threads, which is especially important for the
aggregate types. For example the class definitions for <tt>EpicsString</tt>,
<tt>EpicsArray</tt> and <tt>EpicsMDArray</tt> all have methods which return
pointers into their internal data buffers, for efficiency reasons. In order to
make the use of these types thread-safe, suitable access rules and mutual
exclusion protection must be established at some higher level. The particular
locking scheme used must be appropriate to the application, so cannot be defined
in this document.


    class EpicsEnum; // see below for details;


    class EpicsStruct; // see below for details;
----


NOTE: In this document primitive type means one of epicsBoolean,
epicsOctet, epicsInt16, epicsInt32, epicsInt64, epicsFloat32, or epicsFloat64.


<center>
= Primitive Types =
== epicsTypes ==
</center>


<tt>epicsTypes.h</tt> contains the following:
<tt>epicsTypes.h</tt> contains the following typedefs, which are called the primitive types:


    /* The following may require OSD definitions*/
     typedef bool              epicsBoolean;
     typedef bool              epicsBoolean;
     typedef char              epicsOctet;
     typedef char              epicsOctet;
Line 76: Line 60:
     typedef float              epicsFloat32;
     typedef float              epicsFloat32;
     typedef double            epicsFloat64;
     typedef double            epicsFloat64;
   
    enum epicsType {
        epicsTypeUnknown,
        epicsTypeBoolean,
        epicsTypeOctet,
        epicsTypeInt16,
        epicsTypeInt32,
        epicsTypeInt64,
        epicsTypeFloat32,
        epicsTypeFloat64,
        epicsTypeString,
        epicsTypeArray,
        epicsTypeMDArray,
        epicsTypeEnum,
        epicsTypeBitmask,
        epicsTypeStruct
    };


epicsTypes provides classes for describing data that can be introspected
and can be passed between different platforms.
All data that is sent to or received from EPICS records will be composed
of epicsTypes.
The types <tt>epicsBoolean</tt>, ..., <tt>epicsFloat64</tt>
all map to a C++ standard type.
It may be necessary to provide operating system dependent definitions for some of the types. For example on some architectures an <tt>epicsInt64</tt> may have to be defined as a <tt>long</tt> rather than a <tt>long long</tt>.
It may be necessary to provide operating system dependent definitions for some of the types. For example on some architectures an <tt>epicsInt64</tt> may have to be defined as a <tt>long</tt> rather than a <tt>long long</tt>.


<tt>epicsTypeUnknown</tt> is for anything that is not one of the other epicsTypes.


The non-primitive type, except, <tt>epicsTypeUnknown</tt>
----
are designed so that the data they contain
can be described, introspected, and passed over a network.
Each is described in the following sections.
 
<center>
== Locking Issues ==
</center>
 
epicsTypes does not provide any facilities for preventing simultaneous
access by multiple threads.
For example the class definitions for non-primitive types,
i.e. <tt>epicsString</tt>, <tt>epicsArray</tt>, <tt>epicsStruct</tt>,
and <tt>epicsMDArray</tt>, all provide
a method <tt>expose</tt> which returns the address of data.
In order to make expose safe, some rules must be established.


As an example of rules, a lock can be associated with each object that supports expose. Code can only call expose and access the exposed data while it holds the lock.


The rules are <b>not</b> specified by epicsTypes since
= Aggregate Types =
they are application dependent.


----
The aggregate types must have a fixed size but still be able to manage memory in various ways which will be different for different purposes. Therefor they are all based on a standard interface to a memory buffer which can have different implementations for different memory management requirements.


<center>
<center>
Line 134: Line 75:
</center>
</center>


<tt>epicsBuffer.h</tt> defines a generic interface <tt>EpicsBuffer</tt> to a buffer of octet data, and
<tt>epicsBuffer.h</tt> defines a generic interface <tt>EpicsBuffer</tt> to a buffer of octet data, for which there may be several different implementations.


A buffer factory <tt>EpicsBufferFactory</tt> provides a central registry of <tt>EpicsBufferCreator</tt> objects and uses these to create instances of a particular buffer type on demand.
A buffer factory <tt>EpicsBufferFactory</tt> provides a central registry of <tt>EpicsBufferCreator</tt> objects and uses these to create instances of a particular buffer type on demand.
Line 218: Line 159:
         virtual const char *bufferType() const = 0;
         virtual const char *bufferType() const = 0;
         virtual EpicsBuffer *create(epicsInt32 elementSize = 1) = 0;
         virtual EpicsBuffer *create(epicsInt32 elementSize = 1) = 0;
         virtual void destroy(EpicsBuffer *pbuffer) = 0;
         virtual void destroy(EpicsBuffer *&pbuffer) = 0;
     };
     };


Line 227: Line 168:
;<tt>EpicsBuffer *create(epicsInt32 elementSize = 1)</tt>
;<tt>EpicsBuffer *create(epicsInt32 elementSize = 1)</tt>
: Instanciates an <tt>EpicsBuffer</tt> which is appropriate for storing arrays of size <tt>elementSize</tt>. Array elements can never cross a segment boundary, thus a buffer segment can never be smaller than an individual element. This method may throw an exception if it is unable to create the buffer.
: Instanciates an <tt>EpicsBuffer</tt> which is appropriate for storing arrays of size <tt>elementSize</tt>. Array elements can never cross a segment boundary, thus a buffer segment can never be smaller than an individual element. This method may throw an exception if it is unable to create the buffer.
;<tt>void destroy(EpicsBuffer *pbuffer)</tt>
;<tt>void destroy(EpicsBuffer *&pbuffer)</tt>
: Executes <tt>pbuffer</tt>'s destructor to reclaim the buffer storage, and also reclaims the <tt>pbuffer</tt> object itself.
: Executes <tt>pbuffer</tt>'s destructor to reclaim the buffer storage, reclaims the <tt>pbuffer</tt> object itself, and sets pbuffer to NULL.


The way to deallocate an <tt>EpicsBuffer *pbuffer</tt> is like this:
The way to deallocate an <tt>EpicsBuffer *pbuffer</tt> is like this:
     pbuffer->creator()->destroy(pbuffer);
     if (pbuffer) pbuffer->creator()->destroy(pbuffer);
     pbuffer = NULL;
or use the convenience function provided by <tt>EpicsBufferFactory</tt> which does exactly the same thing:
     EpicsBufferFactory::destroy(pbuffer);


Implementations of this class must be thread-safe; buffers could be created or destroyed at any time from any thread.
Implementations of this class must be thread-safe; buffers could be created or destroyed at any time from any thread.
Line 243: Line 185:
         static EpicsBufferCreator *creator(const char *type);
         static EpicsBufferCreator *creator(const char *type);
         static EpicsBuffer *create(const char *type,
         static EpicsBuffer *create(const char *type,
                                     epicsInt32 elementSize = 1);
                                     epicsInt32 elementSize = 1) {
            return EpicsBufferFactory::creator(type)->create(elementSize);
        }
        static void destroy(EpicsBuffer *&pbuffer) {
            if (pbuffer) pbuffer->creator()->destroy(pbuffer);
        }
     };
     };


The <tt>EpicsBufferFactory</tt> provides a registry of <tt>EpicsBuffer</tt> implementations, each defined by an <tt>EpicsBufferCreator</tt>.  While each <tt>EpicsBufferCreator</tt> is ultimately responsible for creating all <tt>EpicsBuffer</tt>s of its particular type, the <tt>EpicsBufferFactory</tt> provides a convenience function that looks up an <tt>EpicsBufferCreator</tt> by name and calls its <tt>create()</tt> function directly. Use of the factory to create buffers is optional, since the registry can also be queried to find an <tt>EpicsBufferCreator</tt> by name.
The <tt>EpicsBufferFactory</tt> provides a registry of <tt>EpicsBuffer</tt> implementations, each defined by an <tt>EpicsBufferCreator</tt>.  While each <tt>EpicsBufferCreator</tt> is ultimately responsible for creating all <tt>EpicsBuffer</tt>s of its particular type, the <tt>EpicsBufferFactory</tt> provides a convenience function that looks up an <tt>EpicsBufferCreator</tt> by name and calls its <tt>create()</tt> function directly. Use of the factory to create buffers is optional, since the registry can also be queried to find an <tt>EpicsBufferCreator</tt> by name.
;<tt>void register(EpicsBufferCreator *creator)</tt>
: Adds <tt>creator</tt> to the list of known buffer types. It will throw an exception if the same name is already registered at a different address.
;<tt>EpicsBufferCreator *creator(const char *type)</tt>
: Looks up the <tt>EpicsBufferCreator</tt> associated with <tt>type</tt> and returns a pointer to it. '''Return NULL, or throw if not found?'''
;<tt>EpicsBuffer *create(const char *type, epicsInt32 elementSize = 1)</tt>
: Looks up the <tt>EpicsBufferCreator</tt> associated with <tt>type</tt>, passes <tt>elementSize</tt> to its <tt>create()</tt> method and returns the resulting <tt>EpicsBuffer</tt> pointer. '''Return NULL, or throw if not found?'''
;<tt>void destroy(EpicsBuffer *&pbuffer)</tt>
: Asks <tt>pbuffer</tt>'s creator destroy the buffer, reclaiming its associated memory and set pbuffer to NULL.


It should be clear that creating a buffer is not the same as allocating storage for the data held in that buffer. The former is an <tt>EpicsBufferCreator</tt>'s job, while the latter is the responsibility of the <tt>EpicsBuffer</tt> itself. However an <tt>EpicsBufferCreator</tt> may provide additional services to its <tt>EpicsBuffer</tt> instances such as free lists.
It should be clear that creating a buffer is not the same as allocating storage for the data held in that buffer. The former is an <tt>EpicsBufferCreator</tt>'s job, while the latter is the responsibility of the <tt>EpicsBuffer</tt> itself. However an <tt>EpicsBufferCreator</tt> may provide additional services to its <tt>EpicsBuffer</tt> instances such as free lists.
----




Line 255: Line 214:
</center>
</center>


<tt>epicsString.h</tt> defines a string class that uses an <tt>EpicsBuffer</tt> to hold its data.
<tt>epicsString.h</tt> defines a string class that uses an <tt>EpicsBuffer</tt> to hold its data.  Within the core software all strings should be encoded using Unicode/UTF-8, but there is nothing specific to that encoding in the <tt>EpicsString</tt> interface, so other encodings may be stored if desired. EPICS will not provide software to translate between encodings.


     class EpicsString {
     class EpicsString {
Line 265: Line 224:
         virtual ~EpicsString();
         virtual ~EpicsString();
         EpicsString& operator=(const EpicsString &rhs);
         EpicsString& operator=(const EpicsString &rhs);
         void useBuffer(const char *bufferType, epicsInt32 capacity = 0)
         void createBuffer(const char *bufferType, epicsInt32 capacity = 0)
         void useBuffer(EpicsBufferCreator *creator, epicsInt32 capacity = 0);
         void createBuffer(EpicsBufferCreator *creator, epicsInt32 capacity = 0);
         void destroy();
        EpicsBufferCreator *bufferCreator() const;
         void destroyBuffer();
         epicsInt32 get(epicsInt32 offset, epicsInt32 len,
         epicsInt32 get(epicsInt32 offset, epicsInt32 len,
                         epicsOctet *pto) const;
                         epicsOctet *pto) const;
Line 275: Line 235:
          
          
         // These routines are as described for EpicsBuffer
         // These routines are as described for EpicsBuffer
        EpicsBufferCreator *creator() const;
         void reserve(epicsInt32 capacity);
         void reserve(epicsInt32 capacity);
         epicsInt32 capacity() const;
         epicsInt32 capacity() const;
Line 289: Line 248:
         bool expose(epicsInt32 offset, epicsInt32 &len,
         bool expose(epicsInt32 offset, epicsInt32 &len,
                     const epicsOctet *&pdata) const;
                     const epicsOctet *&pdata) const;
     protected:
     private:
         EpicsBuffer *pbuffer;
         EpicsBuffer *pbuffer;
     private:
     private:   // Prevent compiler-implemented methods
         EpicsString(const EpicsString &str);  // No copy constructor
         EpicsString(const EpicsString &str);  // No copy constructor
     };
     };
Line 298: Line 257:
     epicsBoolean operator!=(const EpicsString &lhs, const EpicsString &rhs);
     epicsBoolean operator!=(const EpicsString &lhs, const EpicsString &rhs);


An <tt>EpicsString</tt> is a datatype represented by the epicsType enumeration value <tt>epicsTypeString</tt>.
In Unicode/UTF-8 encoded strings, multiple octets may be needed to store a
It holds a UTF-8 encoded string, i.e. multiple octets may be needed to store
single character. However most code should be able to ignore the character
a single character.
encoding as long as it does not assume that any character can be stored in a
As long as it does not assume that each character is a single byte, most code can just ignore the fact that the string is UTF-8 encoded.
single octet. As long as the final output device (be it a terminal window, a
If the local computer has been internationalized for the particular UTF-8
printer or some other software package) is expecting Unicode/UTF-8 characters, a
encoding then a UTF-8 string can be printed via the printf family of methods.
string can be output using the printf family of methods.


The <tt>EpicsString</tt> data is ''not'' guaranteed to be null-terminated and in most cases will not be, so applications using the string must take precautions when interfacing with routines that expect the string to end with a zero byte. We may provide a type of contiguous <tt>EpicsBuffer</tt> that does guarantee a null terminator.
The <tt>EpicsString</tt> data is ''not'' guaranteed to be null-terminated and in most cases will not be, so applications using the string must take precautions when interfacing with routines that expect the string to end with a zero byte. We may provide a type of contiguous <tt>EpicsBuffer</tt> that does guarantee a null terminator.
Line 309: Line 268:
<tt>EpicsString</tt> provides the following methods:
<tt>EpicsString</tt> provides the following methods:
;<tt>EpicsString()</tt>
;<tt>EpicsString()</tt>
: If a string is default constructed, one of the <tt>useBuffer()</tt> methods must be called to set the underlying buffer type before any data can be stored.
: If a string is default constructed, one of the <tt>createBuffer()</tt> methods must be called to set the underlying buffer type before any data can be stored.
;<tt>EpicsString(const char *literal)</tt>
;<tt>EpicsString(const char *literal)</tt>
: Use of this constructor causes the string to have a readonly buffer that holds just the literal string given, without copying. This is intended to be efficient as it is likely to be commonly used.
: Use of this constructor causes the string to have a readonly buffer that holds just the literal string given, without copying. This is intended to be efficient as it is likely to be commonly used.
Line 316: Line 275:
: Here a buffer type is specified, either by name or by identifier, so the selected buffer type will be created using the <tt>EpicsBufferFactory</tt>, and if <tt>capacity</tt> is non-zero the space will be reserved for at least that number of octets of data.
: Here a buffer type is specified, either by name or by identifier, so the selected buffer type will be created using the <tt>EpicsBufferFactory</tt>, and if <tt>capacity</tt> is non-zero the space will be reserved for at least that number of octets of data.
;<tt>~EpicsString()</tt>
;<tt>~EpicsString()</tt>
: The destructor will <tt>destroy()</tt> the <tt>EpicsBuffer</tt> if one has been created.
: The destructor will destroy the <tt>EpicsBuffer</tt> if one has been created.
;<tt>void useBuffer(const char *bufferType, epicsInt32 capacity)</tt>
;<tt>void createBuffer(const char *bufferType, epicsInt32 capacity)</tt>
;<tt>void useBuffer(EpicsBufferCreator *creator, epicsInt32 capacity)</tt>
;<tt>void createBuffer(EpicsBufferCreator *creator, epicsInt32 capacity)</tt>
: These methods create an <tt>EpicsBuffer</tt> to hold the string data, and if <tt>capacity</tt> is non-zero the space will be reserved for at least that number of octets of data.
: These methods create an <tt>EpicsBuffer</tt> to hold the string data, and if <tt>capacity</tt> is non-zero the space will be reserved for at least that number of octets of data.
:: '''What happens if a buffer type has already been selected for the string? Throw an exception, or create the new one, copy the data and destroy the old buffer? Start with the exception, switch if we need to.'''
: If a buffer type has already been selected for the string, this method will throw an exception.  (However this definition may change if it is found to be too onerous; the alternative would be to create a new buffer, copy the data to it and then destroy the old buffer)
;<tt>void destroy()</tt>
;<tt>EpicsBufferCreator *bufferCreator() const</tt>
: Causes the underlying buffer storage to be completely released, after which one of the <tt>useBuffer()</tt> methods must be used before data can be stored in the string again.
: Returns the <tt>EpicsBufferCreator</tt> used for the underlying buffer storage, or NULL if none has been set yet.
;<tt>void destroyBuffer()</tt>
: Causes the underlying buffer storage to be destroyed, after which one of the <tt>createBuffer()</tt> methods must be used before data can be stored in the string again.
;<tt>epicsInt32 get(epicsInt32 offset, epicsInt32 len, epicsOctet *pto)</tt>
;<tt>epicsInt32 get(epicsInt32 offset, epicsInt32 len, epicsOctet *pto)</tt>
: Copies up to <tt>len</tt> octets starting at <tt>offset</tt> from the string buffer to <tt>pto</tt>, and returns the number of octets transfered. The return value will be less than <tt>len</tt> if <tt>offset+len > size()</tt>.
: Copies up to <tt>len</tt> octets starting at <tt>offset</tt> from the string buffer to <tt>pto</tt>, and returns the number of octets transfered. The return value will be less than <tt>len</tt> if <tt>offset+len > size()</tt>.
Line 330: Line 291:
: Calculates an n-bit hash of the octets stored in the string buffer.
: Calculates an n-bit hash of the octets stored in the string buffer.


<tt>EpicsString</tt> also implements all of the routines in the <tt>EpicsBuffer</tt> interface. If an <tt>EpicsBuffer</tt> has been created these calls are forwarded to the underlying  <tt>EpicsBuffer</tt> method. If an <tt>EpicsBuffer</tt> has not been created however, all methods except for <tt>useBuffer()</tt> will throw an exception.
<tt>EpicsString</tt> also implements all of the routines in the <tt>EpicsBuffer</tt> interface. If an <tt>EpicsBuffer</tt> has been created these calls are forwarded to the underlying  <tt>EpicsBuffer</tt> method. If an <tt>EpicsBuffer</tt> has not been created however, all methods except for <tt>createBuffer()</tt> will throw an exception.
 


----
----


<center>
<center>
Line 338: Line 301:
</center>
</center>


<tt>epicsArray.h</tt> defines an array class that uses an <tt>EpicsBuffer</tt> to hold its data.
<tt>epicsArray.h</tt> defines an array class that uses an <tt>EpicsBuffer</tt> to hold its data, which can be of any datatype that has a fixed size.


     class EpicsArray {
     class EpicsArray {
    public:
         EpicsArray(epicsInt32 elementSize = 0);
         EpicsArray(epicsInt32 elementSize = 0);
         EpicsArray(const char *bufferType, epicsInt32 capacity,
         EpicsArray(const char *bufferType, epicsInt32 elementSize = 0,
                     epicsInt32 elementSize = 0);
                     epicsInt32 capacity = 0);
         EpicsArray(EpicsBufferCreator *creator, epicsInt32 capacity,
         EpicsArray(EpicsBufferCreator *creator, epicsInt32 elementSize = 0,
                     epicsInt32 elementSize = 0);
                     epicsInt32 capacity = 0);
         virtual ~EpicsArray();
         virtual ~EpicsArray();
         void setElementSize(epicsInt32 elementSize);
         EpicsArray& operator=(const EpicsArray &);
        epicsInt32 getElementSize() const;
         void createBuffer(const char *bufferType, epicsInt32 capacity = 0);
         void useBuffer(const char *bufferType, epicsInt32 capacity = 0);
         void createBuffer(EpicsBufferCreator *creator, epicsInt32 capacity = 0);
         void useBuffer(EpicsBufferCreator *creator, epicsInt32 capacity = 0);
        EpicsBufferCreator *bufferCreator() const;
         void destroy();
         void destroyBuffer();
         epicsOctet * element(epicsInt32 index);
         epicsOctet * element(epicsInt32 index);
         const epicsOctet * element(epicsInt32 index) const;
         const epicsOctet * element(epicsInt32 index) const;
Line 359: Line 323:
                         const epicsOctet *pfrom);
                         const epicsOctet *pfrom);
          
          
         // These are similar to EpicsBuffer, measured in elements not octets
         // Like EpicsBuffer, but units are elements not octets
        EpicsBufferCreator *creator() const;
         void reserve(epicsInt32 capacity);
         void reserve(epicsInt32 capacity);
         epicsInt32 capacity() const;
         epicsInt32 capacity() const;
Line 372: Line 335:
                     const epicsOctet *&pdata) const;
                     const epicsOctet *&pdata) const;
     protected:
     protected:
        void setElementSize(epicsInt32 elementSize);
        epicsInt32 getElementSize() const;
    private:
         epicsInt32 elementSize;
         epicsInt32 elementSize;
         EpicsBuffer *pbuffer;
         EpicsBuffer *pbuffer;
     private:
     private:   // Prevent compiler-implemented methods
         EpicsArray(const EpicsArray &);            // No copy constructor
         EpicsArray(const EpicsArray &);            // No copy constructor
        EpicsArray& operator=(const EpicsArray &); // No assignment operator
     };
     };


An <tt>EpicsArray</tt> is a datatype represented by the epicsType enumeration value <tt>epicsTypeArray</tt>. It holds an array of elements of constant size.
An <tt>EpicsArray</tt> holds an array of elements of constant size.


EpicsArray has the following methods:
<tt>EpicsArray</tt> has the following methods:
;<tt>EpicsArray(epicsInt32 elementSize = 0)</tt>
;<tt>EpicsArray(epicsInt32 elementSize = 0)</tt>
: If an array is default constructed, one of the <tt>useBuffer()</tt> methods must be called to set the underlying buffer type before any storage can be reserved for the array elements. The array must also be told its element size before buffer space can be reserved.
: If an array is default constructed, one of the <tt>createBuffer()</tt> methods must be called to set the underlying buffer type before any storage can be reserved for the array elements. The array must also be told its element size before buffer space can be reserved.
;<tt>EpicsArray(const char *bufferType, epicsInt32 capacity, epicsInt32 elementSize = 0)</tt>
;<tt>EpicsArray(const char *bufferType, epicsInt32 capacity = 0, epicsInt32 elementSize = 0)</tt>
;<tt>EpicsArray(EpicsBufferCreator *creator, epicsInt32 capacity, epicsInt32 elementSize = 0)</tt>
;<tt>EpicsArray(EpicsBufferCreator *creator, epicsInt32 capacity = 0, epicsInt32 elementSize = 0)</tt>
: Here a buffer type is specified, either by name or by identifier, so the selected butter type will be created using the <tt>EpicsBufferFactory</tt>, and if both <tt>capacity</tt> and <tt>elementSize</tt> are non-zero the space will be reserved for at least that number of array elements.
: Here a buffer type is specified, either by name or by identifier, so the selected butter type will be created using the <tt>EpicsBufferFactory</tt>, and if both <tt>capacity</tt> and <tt>elementSize</tt> are non-zero the space will be reserved for at least that number of array elements.
;<tt>~EpicsArray</tt>
;<tt>~EpicsArray</tt>
: The destructor will <tt>destroy()</tt> the <tt>EpicsBuffer</tt> if one has been created.
: The destructor will destroy the <tt>EpicsBuffer</tt> if one has been created.
;<tt>void setElementSize(epicsInt32 elementSize)</tt>
;<tt>EpicsArray& operator=(const EpicsArray &)</tt>
: This sets the size (in octets) of the element type to be stored in the array. If a buffer has already been used for previous array data, this will be released and the buffer capacity set to zero.
: Assignment is possible between arrays (implemented by bit copying), providing the element sizes match exactly and the target array has enough storage to hold all of the data in the source array - call <tt>reserve()</tt> on the target array first if necessary. Think very carefully before assigning arrays of strings, structs or other aggregate types that store pointers, as this will result in duplicate pointers to the same objects. This method will throw an exception if the element sizes differ, and will truncate the array data if the target capacity is insufficient. Calls <tt>resize()</tt> as appropriate.
;<tt>epicsInt32 getElementSize()</tt>
;<tt>void createBuffer</tt>
: This returns the element size currently stored.
;<tt>void useBuffer</tt>
: These methods create an <tt>EpicsBuffer</tt> to hold the array data, and if <tt>capacity</tt> and <tt>elementSize</tt> are non-zero the space will be reserved for at least that number of array elements.
: These methods create an <tt>EpicsBuffer</tt> to hold the array data, and if <tt>capacity</tt> and <tt>elementSize</tt> are non-zero the space will be reserved for at least that number of array elements.
:: '''What happens if a buffer type has already been selected for the array? Throw an exception, or create the new one, copy the data and destroy the old buffer? Start with the exception, switch if we need to.'''
: If a buffer type has already been selected for the array, this method will throw an exception.  (However this definition may change if it is found to be too onerous; the alternative would be to create a new buffer, copy the data to it and then destroy the old buffer)
;<tt>destroy()</tt>
;<tt>EpicsBufferCreator *bufferCreator() const</tt>
: Causes the underlying buffer storage to be completely released, after which one of the <tt>useBuffer()</tt> methods must be used before data can be stored in the array again.
: Returns the <tt>EpicsBufferCreator</tt> used for the underlying buffer storage, or NULL if none has been set yet.
;<tt>destroyBuffer()</tt>
: Causes the underlying buffer storage to be destroyed, after which one of the <tt>createBuffer()</tt> methods must be used before data can be stored in the array again.
;<tt>epicsOctet * element(epicsInt32 index)</tt>
;<tt>epicsOctet * element(epicsInt32 index)</tt>
: Returns a pointer giving direct access to the <tt>index</tt>'th element of the array, allowing this element to be modified. The pointer returned will have to be cast to an appropriate element type before use. Access to either adjacent array element ''must not'' be made by adjusting the pointer since the underlying buffer storage may be segmented - instead call <tt>element()</tt> again with the appropriately modified index.
: Returns a pointer giving direct access to the <tt>index</tt>'th element of the array, allowing this element to be modified. The pointer returned will have to be cast to an appropriate element type before use. Access to either adjacent array element ''must not'' be made by adjusting the pointer since the underlying buffer storage may be segmented - instead call <tt>element()</tt> again with the appropriately modified index.
Line 406: Line 371:
;<tt>epicsInt32 put(epicsInt32 offset, epicsInt32 len, const epicsOctet *pfrom)</tt>
;<tt>epicsInt32 put(epicsInt32 offset, epicsInt32 len, const epicsOctet *pfrom)</tt>
: Copies up to <tt>len</tt> elements from <tt>pfrom</tt> into the buffer starting <tt>offset</tt> elements from the beginning, and returns the number of elements copied. The return value will be less than <tt>len</tt> if <tt>offset+len > capacity()</tt>. The array size will be updated if this call extends the array beyond its original size. However on entry <tt>offset</tt> must not be greater than <tt>size()</tt> or an exception will be thrown.
: Copies up to <tt>len</tt> elements from <tt>pfrom</tt> into the buffer starting <tt>offset</tt> elements from the beginning, and returns the number of elements copied. The return value will be less than <tt>len</tt> if <tt>offset+len > capacity()</tt>. The array size will be updated if this call extends the array beyond its original size. However on entry <tt>offset</tt> must not be greater than <tt>size()</tt> or an exception will be thrown.
;<tt></tt>
;<tt>void setElementSize(epicsInt32 elementSize)</tt>
: This sets the size (in octets) of the element type to be stored in the array. If a buffer has already been used for previous array data, this will be released and the buffer capacity set to zero.
;<tt>epicsInt32 getElementSize()</tt>
: This returns the element size currently stored.
 
<tt>EpicsArray</tt> also implements the access routines from the <tt>EpicsBuffer</tt> interface, translating capacity, size and offset values from element counts into octets and passing them to the underlying <tt>EpicsBuffer</tt>. If an <tt>EpicsBuffer</tt> has not been created however, all these methods will throw an exception. The ability to change the element size is restricted to derived classes only.


<tt>EpicsArray</tt> also implements all of the routines in the EpicsBuffer interface, translating all capacity, size and offset values into element counts before passing them to the underlying <tt>EpicsBuffer</tt>. If an <tt>EpicsBuffer</tt> has not been created however, all these methods will throw an exception.


----
----


'''Stuff below here still needs work - ANJ'''


<center>
<center>
Line 418: Line 386:
</center>
</center>


An <tt>EpicsMDArray</tt> is a datatype represented by the epicsType enumeration value <tt>epicsTypeMDArray</tt>. It holds a multi-dimensional array of elements of constant size, using an <tt>EpicsBuffer</tt> to hold its data.
<tt>epicsMDArray.h</tt> defines a multi-dimensional array class that uses two <tt>EpicsArray</tt>s, one for the sizes of each dimension and another for the array data, which can be of any datatype that has a fixed size.


<tt>epicsMDArray.h</tt> contains the following:
     class EpicsMDArray {
 
     public:
     class EpicsMDArraySlice;
        EpicsMDArray(epicsInt32 elementSize = 0);
      
        EpicsMDArray(const char *bufferType, epicsInt32 capacity = 0,
    class EpicsMDArray : public EpicsArrayBuffer {
                      epicsInt32 elementSize = 0);
         EpicsMDArray(epicsType type,epicsInt16 ndim);
         EpicsMDArray(EpicsBufferCreator *creator, epicsInt32 capacity = 0,
                      epicsInt32 elementSize = 0);
         virtual ~EpicsMDArray();
         virtual ~EpicsMDArray();
         EpicsMDArray(epicsType type,epicsInt16 ndim,const char *bufferType);
         EpicsMDArray& operator=(const EpicsMDArray &);
        EpicsMDArray(epicsType type,epicsInt16 ndim,epicsInt16 bufferTypeId);
         void createBuffer(const char *bufferType, epicsInt32 capacity = 0);
         EpicsMDArray(epicsType type,epicsInt16 ndim,
         void createBuffer(EpicsBufferCreator *creator, epicsInt32 capacity = 0);
                      const char *bufferType,,epicsInt32 capacity);
        EpicsBufferCreator *bufferCreator() const;
         EpicsMDArray(epicsType type,epicsInt16 ndim,
         void destroyBuffer();
                      epicsInt16 bufferTypeId,,epicsInt32 capacity);
         void setDimensions(const EpicsArray &newDims);
         void allocateBuffer(const char *bufferType);
         const EpicsArray& getDimensions() const;
         void allocateBuffer(epicsInt16 bufferTypeId);
        epicsInt32 get(epicsInt32 offset, epicsInt32 len,
         void allocateBuffer(const char *bufferType,
                        epicsOctet *pto) const;
                    epicsInt16 elementSize, epicsInt32 capacity)
        epicsInt32 put(epicsInt32 offset, epicsInt32 len,
         void allocateBuffer(epicsInt16 bufferTypeId,
                        const epicsOctet *pfrom);
                    epicsInt16 elementSize, epicsInt32 capacity)
       
         epicsType getType();
        // Like EpicsBuffer, but units are elements not octets
         epicsInt16 getElementSize();
        void reserve(epicsInt32 capacity);
        epicsInt32 capacity() const;
         epicsInt32 size() const;
        epicsInt32 maxSize() const;
         void expose(epicsInt32 offset, epicsInt32 &len,
                    epicsOctet *&pdata);
         void expose(epicsInt32 offset, epicsInt32 &len,
                    const epicsOctet *&pdata) const;
     protected:
     protected:
         epicsType type;
         void setElementSize(epicsInt32 elementSize);
        epicsInt16 elementSize;
         epicsInt32 getElementSize() const;
         EpicsMDArraySlice *pslice;
         void resize(epicsInt32 newsize);
         EpicsMDArrayBuffer *pbuffer;
     private:
     private:
         EpicsMDArray(); // no default constructor
         EpicsArray dimensions;     // EpicsArray<epicsInt32>
         EpicsMDArray(const EpicsMDArray &); // copy not allowed
         EpicsArray elements;
         EpicsMDArray(const EpicsMDArray *); // copy not allowed
    private:  // Prevent compiler-implemented methods
         EpicsMDArray(const EpicsMDArray &);           // No copy constructor
     };
     };


    class EpicsMDArrayIndex {
An <tt>EpicsMDArray</tt> is a multi-dimensional array of elements of constant size, using two <tt>EpicsArray</tt>s, one to hold the element data and another for the size in each dimension.
    public
        epicsInt32 low;
        epicsInt32 high;
    };
    class EpicsMDArraySlice {
    public:
        epicsType  type;
        epicsInt16  ndim;      // number of dimensions
        EpicsMDArrayIndex *paindex; // addr of array of EpicsMDArrayIndex
    };


=== EpicsMDArray ===
The <tt>get()</tt>, <tt>put()</tt> and <tt>expose()</tt> methods all take an <tt>epicsInt32 offset</tt> parameter that specifies an element count into the underlying one dimensional array element buffer.  Currently there are no methods provided for accessing elements of the array at some particular coordinate; such methods would be more appropriate for a derived class with a fixed number of dimensions.


An <tt>EpicsMDArray</tt> is an instance of an <tt>epicsTypeMDArray</tt>.
<tt>EpicsMDArray</tt> has the following methods:
It can contain an array of any epicsType.
;<tt>EpicsMDArray(epicsInt32 elementSize = 0)</tt>
: If an array is default constructed, one of the <tt>createBuffer()</tt> methods must be called to set the underlying buffer type before any storage can be reserved for the array elements. The array must also be told its element size before buffer space can be reserved.
;<tt>EpicsMDArray(const char *bufferType, epicsInt32 capacity = 0, epicsInt32 elementSize = 0)</tt>
;<tt>EpicsMDArray(EpicsBufferCreator *creator, epicsInt32 capacity = 0, epicsInt32 elementSize = 0)</tt>
: Here a buffer type is specified, either by name or by identifier, so the selected butter type will be created using the <tt>EpicsBufferFactory</tt>, and if both <tt>capacity</tt> and <tt>elementSize</tt> are non-zero the space will be reserved for at least that number of array elements.
;<tt>~EpicsMDArray</tt>
: The destructor will destroy the <tt>EpicsBuffer</tt>s if any have been created.
;<tt>void createBuffer</tt>
: These methods create an <tt>EpicsBuffer</tt> to hold the array data, and if <tt>capacity</tt> and <tt>elementSize</tt> are non-zero the space will be reserved for at least that number of array elements.
: If a buffer type has already been selected for the array, this method will throw an exception.  (However this definition may change if it is found to be too onerous; the alternative would be to create a new buffer, copy the data to it and then destroy the old buffer)
;<tt>EpicsBufferCreator *bufferCreator() const</tt>
: Returns the <tt>EpicsBufferCreator</tt> used for the underlying buffer storage, or NULL if none has been set yet.
;<tt>destroyBuffer()</tt>
: Causes the underlying buffer storage to be destroyed, after which one of the <tt>createBuffer()</tt> methods must be used before data can be stored in the array again.
;<tt>void setDimensions(const EpicsArray &newDims)</tt>
: Changes the dimensionality of the array, setting the number of dimensions and the extent in each dimension to match <tt>newDims</tt>. This call may cause the capacity of the underlying buffers to be increased if necessary to hold an increased number of dimensions and/or element data. Call <tt>reserve()</tt> to free up any unneeded data storage if desired.
;<tt>const EpicsArray& getDimensions() const</tt>
: Returns a reference to the internal <tt>EpicsArray</tt> that holds the dimension information fot the element array.
;<tt>epicsInt32 get(epicsInt32 offset, epicsInt32 len, epicsOctet *pto)</tt>
: Copies up to <tt>len</tt> elements starting at <tt>offset</tt> from the element array buffer to <tt>pto</tt>, and returns the number of elements transfered. The return value will be less than <tt>len</tt> if <tt>offset+len > size()</tt>.
;<tt>epicsInt32 put(epicsInt32 offset, epicsInt32 len, const epicsOctet *pfrom)</tt>
: Copies up to <tt>len</tt> elements from <tt>pfrom</tt> into the buffer starting <tt>offset</tt> elements from the beginning, and returns the number of elements copied. The return value will be less than <tt>len</tt> if <tt>offset+len > capacity()</tt>. The array size will be updated if this call extends the array beyond its original size. However on entry <tt>offset</tt> must not be greater than <tt>size()</tt> or an exception will be thrown.
;<tt>void setElementSize(epicsInt32 elementSize)</tt>
: This sets the size (in octets) of the element type to be stored in the array. If a buffer has already been used for previous array data, this will be released and the buffer capacity set to zero.
;<tt>epicsInt32 getElementSize()</tt>
: This returns the element size currently stored.


EpicsMDArray has the following methods:
<tt>EpicsMDArray</tt> also implements the access routines from the <tt>EpicsBuffer</tt> interface, translating capacity, size and offset values from element counts into octets and passing them to the underlying <tt>EpicsBuffer</tt>. If an <tt>EpicsBuffer</tt> has not been created however, all these methods will throw an exception. The ability to change the element or array sizes is restricted to derived classes only.
; <tt>EpicsMDArray</tt>
: Several constructors are available. All require an epicsType and the number ofdimensions. If no buffer type is specified then, one of the allocate methods must be called. If a buffer type is specified then EpicsArrayBufferFactory:allocate is called to initialize pbuffer. If capacity is also specified EpicsArrayBuffer:allocate is called to allocate storage for data.
; <tt>~EpicsMDArray</tt>
: The descructor will release the EpicsArrayBuffer if it has been allocated.
; <tt>void allocateBuffer</tt>
: These methods allocate an EpicsArrayBuffer and if capacity is specified call EpicsArrayBuffer:allocate.


EpicsMDArray implements interface EpicsArrayBuffer.
If an EpicsArrayBuffer has not been allocated all methods raise an exceptioon.
If an EpicsArrayBuffer has been allocated it
calls the associated EpicsArrayBuffer method.


----


<b>EpicsArrayBuffer Notes</b>
EpicsMDArray uses the same buffer interface as EpicsArray.
Code that accesses a multidimensional array should be prepared to
access both contiguous and segmented buffer implementations.
Circular buffer implementations may not make sense.
=== EpicsMDArraySlice ===
<tt>EpicsMDArraySlice</tt> has the following fields:
; <tt>type</tt>
: The element type which must be one of bool,...,string
; <tt>ndim</tt>
: The number of dimensions
; <tt>paindex</tt>
: addr of array(ndim) of EpicsMDArrayIndex
<tt>EpicsMDArrayIndex</tt> has the fields:
; <tt>low</tt>
:
; <tt>high</tt>
:
The definitions describe a "slice" of a multi-dimensional array.
----


<center>
<center>
Line 515: Line 470:
</center>
</center>


An <tt>EpicsEnum</tt> is a 16-bit index value,
An <tt>EpicsEnum</tt> is a 16-bit index value, which uses a standard interface to convert between choice strings and their index values. There may be more than one implementation of the interface.
with an interface to convert between choice strings and their index values.


class EpicsEnumChoices {
<tt>epicsEnum.h</tt> contains these declarations:
public:
     const int EpicsEnumInvalid = -1;
     virtual EpicsEnumChoices *duplicate() const = 0;
    virtual void release() = 0;
    virtual epicsInt16 nChoices() const = 0;
    virtual epicsInt16 index(const EpicsString &choice) const = 0;
    virtual void choice(epicsInt16 index, EpicsString &choice) const = 0;
};
class EpicsEnum {
public:
    enum {invalid = -1};
      
      
     EpicsEnum();
     class EpicsEnumChoices {
    EpicsEnum(const EpicsEnum &e);
    EpicsEnum(EpicsEnumChoices *choices);
    EpicsEnum(EpicsEnumChoices *choices, epicsInt16 index);
    virtual ~EpicsEnum();
    EpicsEnum& operator=(const EpicsEnum &rhs);
   
    virtual void choices(EpicsEnumChoices *pchoices);
    EpicsEnumChoices *choices() const { return pchoices; }
    epicsInt16 nChoices() const { return pchoices ? pchoices->nChoices() : 0; }
    epicsInt16 get() const { return index; }
    virtual void get(EpicsString &choice) const;
    virtual void put(epicsInt16 index);
    virtual void put(const EpicsString &choice);
protected:
    EpicsEnumChoices *pchoices;
    epicsInt16 index;
};
 
----
 
<center>
== epicsStruct ==
</center>
 
<tt>epicsStruct.h</tt> contains the following:
 
    class EpicsStructDescription;
    class EpicsStructManager;
 
    class EpicsStruct{
        EpicsStruct(EpicsStructDescription &structDef);
        ~EpicsStruct();
    protected:
        EpicsStructDescription *pdescription;
        void    *pstorage;
    private:
        EpicsStruct(); // no default cobstructor
    };
 
    struct EpicsStringDefaults { // defaults for string
        const char * bufferType;
        epicsInt32 capacity;
    };
 
    struct EpicsArrayDefaults { // defaults for array
        epicsType  type;
        const char * bufferType;
        epicsInt32 capacity;
        epicsInt32 nelements;
    };
 
    struct EpicsMDArrayDefaults { // defaults for mdarray
        epicsType  type;
        epicsInt16 ndim
        const char *bufferType;
        epicsInt32 capacity;
    };
 
    struct EpicsStructDefaults { // defaults for struct
        EpicsStructDescription *pdescription;
    };
 
    class EpicsStructField {
     public:
     public:
         EpicsString name;
         virtual EpicsEnumChoices * duplicate() = 0;
         epicsType  type;
         virtual void release() = 0;
         union {
         virtual epicsInt16 nChoices() const = 0;
            EpicsStringDefaults  *pstring;
        virtual epicsInt16 index(const EpicsString &choice) const = 0;
            EpicsArrayDefaults  *parray;
         virtual const EpicsString & choice(epicsInt16 index) const = 0;
            EpicsMDArrayDefaults *pmdarray;
            EpicsStructDefaults  *pstruct;
         } defaults;
     };
     };
   
   
     class EpicsStructDescription{
     class EpicsEnum {
     public:
     public:
         EpicsString        name;
         EpicsEnum();
         EpicsStructManager *plifetime;
        EpicsEnum(const EpicsEnum &rhs);
         epicsInt16         nfields;
         EpicsEnum(EpicsEnumChoices *choices);
         EpicsStructField    *pafield;//ptr to array of EpicsStructField
         virtual ~EpicsEnum();
    };
         EpicsEnum& operator=(const EpicsEnum &rhs);
 
          
    class EpicsStructManager {
        void choices(EpicsEnumChoices *pchoices);
    public:
        EpicsEnumChoices *choices() const { return pchoices; }
         virtual void allocate(EpicsStruct &struct) = 0;
        epicsInt16 nChoices() const { return pchoices ? pchoices->nChoices() : 0; }
         virtual void destroy(EpicsStruct &struct) = 0;
         operator epicsInt16() const { return index; }
         virtual void *exposeField(EpicsStruct &struct, epicsInt16 index) = 0;
        operator const EpicsString &() const;
        EpicsEnum& operator=(epicsInt16 rhs);
         EpicsEnum& operator=(const EpicsString &rhs);
    protected:
         EpicsEnumChoices *pchoices;
        epicsInt16 index;
     };
     };


An epicsStruct is a container with fields each
The <tt>EpicsEnumChoices</tt> interface provides information about the values and choices available that can be use in an <tt>EpicsEnum</tt>. The <tt>EpicsEnum</tt> class itself provides storage for an enum index and a pointer to the describing interface implementation, and provides a number of different access methods.
of which is any epicsType including unknown.
 
The following classes are involved with an epicsStruct:
* <tt>EpicsStruct</tt> - An instance of an epicsStruct
* <tt>EpicsStructDescription</tt> - Describes an epicsStruct
* <tt>EpicsStructField</tt> - Describes a field of an epicsStruct
* <tt>EpicsStructDefaults</tt> - Provides default values for non-primitive fields. This is intended for code that initializes fields.
* <tt>EpicsStructManager</tt> - An interface that manages storage for an epicsStruct.
 
It is expected that each of these classes are extended. In fact if any
field of an epicsStruct has type <tt>epicsTypeUnknown</tt> then it is likely
that most will be extended.
 
epicsTypes does not specify how to locate an EpicsStructManager, i.e.
the mechanism is application dependent.
 
An <tt>EpicsStruct</tt> contains two fields:
; <tt>pdescription</tt>
: Address of a <tt>EpicsStructDescription</tt> that describes the structure.
; <tt>pstorage</tt>
: Address of storage for the data contained in the structure.
 
=== EpicsStructDescription ===
 
<tt>EpicsStructDescription</tt> has the fields:
; <tt>name</tt>
: The structure name.
; <tt>plifetime</tt>
: Address of a <tt>EpicsStructManager</tt> interface. See below.
; <tt>nfields</tt>
: The number of fields in the structure.
; <tt>pafield</tt>
: addresss of an array of <tt>EpicsStructField</tt>, one for each field.
 
=== EpicsStructField ===
 
<tt>EpicsStructField</tt> has the fields:
; <tt>name</tt>
: The field name.
; <tt>type</tt>
: The field type, which can be any epicsType.
; <tt>defaults</tt>
: For each of the non-primitive epicsTypes, default values can be specified. This is for use by code that must initialize field instances.


=== EpicsStructManager ===
The <tt>EpicsEnumChoices</tt> interface has five methods which must be implemented:
;<tt>EpicsEnumChoices * clone()</tt>
: Asks the interface for a pointer that another enum can use, permitting a copy to be made and returned or a reference count to be incremented.
;<tt>void release()</tt>
: Tells the interface that this enum is no longer referencing it, permitting a reference-counted implementation.
;<tt>epicsInt16 nChoices() const</tt>
: Returns the number of possible choices for this enumeration, which is also one more than the index number of the highest choice.
;<tt>epicsInt16 index(const EpicsString &choice) const</tt>
: Looks up and returns the index value associated with a particular choice string.  If no match is found, it returns <tt>EpicsEnumInvalid</tt>.
;<tt>const EpicsString & choice(epicsInt16 index) const</tt>
: Looks up and returns the choice string associated with a particular index value.  If the value is outside the legal range of choices, it throws an exception.


<tt>EpicsStructManager</tt> is an interface that has three methods:
The <tt>EpicsEnum</tt> class defines the following methods:
; <tt>allocate</tt>
;<tt>EpicsEnum()</tt>
: This sets <tt>pstructDef</tt> to the definition for the associated structure and sets <tt>pstorage</tt> to the address of storage for the fields in the structure.
: The default constructor initializes the enum to invalid. This cannot be changed until the choices to use has been set.
; <tt>destroy</tt>
;<tt>EpicsEnum(const EpicsEnum &rhs)</tt>
: releases the storage for the structure and sets <tt>pstructDef</tt> null.
: The copy constructor clone's the choices from its input parameter and copies the index value.
; <tt>exposeField</tt>
;<tt>EpicsEnum(EpicsEnumChoices *choices)</tt>
: This returns the address of the storage for the data associated with the field.
: This constructor sets the enum's choices interface (does not call clone).
;<tt>~EpicsEnum()</tt>
: The destructor releases the choice interface.
;<tt>EpicsEnum& operator=(const EpicsEnum &rhs)</tt>
: Assignment from another enum checks if the same choices interface is used and if not it releases the old interface and clones the new one. Finally it copies the value.
;<tt>void choices(EpicsEnumChoices *pchoices)</tt>
: Releases the existing choices interface if one is already set, and initializes itself with the new interface (does not call clone). This also resets the index to <tt>EpicsEnumInvalid</tt>.
;<tt>EpicsEnumChoices *choices() const</tt>
: Returns the choices interface used by this enum.
;<tt>epicsInt16 nChoices() const</tt>
: Returns the number of possible choices for this enumeration, which is also one more than the index number of the highest choice.
;<tt>operator epicsInt16() const</tt>
: A conversion operator, returns the current choice as an integer.
;<tt>operator const EpicsString &() const</tt>
: A conversion operator, returns the current choice as a string. Throws an exception if the current choice is <tt>EpicsEnumInvalid</tt>.
;<tt>EpicsEnum& operator=(epicsInt16 rhs)</tt>
: Assignment with an integer sets the index value. Throws an exception if the index is outside the allwed range.
;<tt>EpicsEnum& operator=(const EpicsString &rhs)</tt>
: Assignment with a string looks up and sets the index value. Throws an exception if the string cannot be

Latest revision as of 19:19, 6 July 2005

Marty Kraimer and Andrew Johnson

June 14, 2005

Overview

This document describes the fundamental C++ data types which will be used internally by iocCore and hence must be supported in EPICS Version 4.0. We do not cover composite (struct) types here, as a generic interface to such types requires introspection information which is only provided at a higher level.

All data that is sent to or received from any EPICS record will be composed out of the fundamental data types described here.

The data types are:

  • Primitive Types
    • epicsBoolean
    • epicsOctet
    • epicsInt16
    • epicsInt32
    • epicsInt64
    • epicsFloat32
    • epicsFloat64
  • Aggregate Types
    • EpicsString
    • EpicsArray
    • EpicsMDArray
    • EpicsEnum

Comments on Type Safety

EpicsArray and EpicsMDArray as presented below are not type-safe, in that they take and return pointers to an epicsOctet which the user must cast to the appropriate element type. They also will not be able to default-construct or destruct their array elements, so cannot safely be used for complex C++ data types. This will not be an issue when creating an array of strings as long as we ensure that a buffer is zero-filled at allocation.

We could make versions of these classes that are templated on the element type, which default-construct and destruct their elements and even use the element type's assignment operator for copying operations. However since Version 4.0 record types can contain array fields whose data types are not known until the database is loaded at runtime, we will still need to provide a non type-safe version of the array types. For the moment we're going to work with these definitions.

Locking Issues

These fundamental EPICS datatypes do not provide facilities for preventing simultaneous access by multiple threads, which is especially important for the aggregate types. For example the class definitions for EpicsString, EpicsArray and EpicsMDArray all have methods which return pointers into their internal data buffers, for efficiency reasons. In order to make the use of these types thread-safe, suitable access rules and mutual exclusion protection must be established at some higher level. The particular locking scheme used must be appropriate to the application, so cannot be defined in this document.




Primitive Types

epicsTypes.h contains the following typedefs, which are called the primitive types:

    typedef bool               epicsBoolean;
    typedef char               epicsOctet;
    typedef short              epicsInt16;
    typedef int                epicsInt32;
    typedef long long          epicsInt64;
    typedef float              epicsFloat32;
    typedef double             epicsFloat64;

It may be necessary to provide operating system dependent definitions for some of the types. For example on some architectures an epicsInt64 may have to be defined as a long rather than a long long.




Aggregate Types

The aggregate types must have a fixed size but still be able to manage memory in various ways which will be different for different purposes. Therefor they are all based on a standard interface to a memory buffer which can have different implementations for different memory management requirements.

EpicsBuffer

epicsBuffer.h defines a generic interface EpicsBuffer to a buffer of octet data, for which there may be several different implementations.

A buffer factory EpicsBufferFactory provides a central registry of EpicsBufferCreator objects and uses these to create instances of a particular buffer type on demand.

    class EpicsBuffer { // interface
    protected:
        virtual ~EpicsBuffer() = 0;
    public:
        virtual EpicsBufferCreator *creator() const = 0;
        virtual void reserve(epicsInt32 capacity) = 0;
        virtual epicsInt32 capacity() const = 0;
        virtual void resize(epicsInt32 newsize) = 0;
        virtual epicsInt32 size() const = 0;
        virtual epicsInt32 maxSize() const = 0;
        virtual bool mutable() const = 0;
        virtual bool isEqual(const EpicsBuffer &cmp) const = 0;
        virtual bool isEqual(epicsInt32 offset, epicsInt32 len,
                             const epicsOctet *pdata) const = 0;
        virtual void expose(epicsInt32 offset, epicsInt32 &len,
                            epicsOctet *&pdata) = 0;
        virtual void expose(epicsInt32 offset, epicsInt32 &len,
                            const epicsOctet *&pdata) const = 0;
    
    friend class EpicsBufferCreator;
    };

An EpicsBuffer is a container for epicsOctet data values, and may be used to store things such as character strings containing UTF-8 characters or arrays of some other data type.

Multiple implementations of EpicsBuffer are needed with different characteristics, but all buffers will be accessed using the same EpicsBuffer interface. At least the following buffer types will be provided:

  • readonly - A contiguous array that already contains the desired data is provided at buffer initialization time, and cannot be modified. This buffer is for use with literal character strings, and probably won't be registered with the EpicsBufferFactory.
  • contiguous - The data is stored in a contiguous array of octets.
  • segmented - The data is stored in fixed-size chunks. This form should be used for buffers that are frequently created and released, or which often need to change in size.

Additional implementations may be provided for other purposes such as managing network buffers. There might be a need for a contiguous zero-terminated buffer type for code that regularly talks to C string handling routines.

In addition to storing the data, the buffer keeps the following information:

capacity
The number of octets of storage allocated for the buffer.
size
The number of octets of data currently held in the buffer.

Implementations of EpicsBuffer must provide the following methods, the names of which have been designed (where possible) to match the names of the equivalent methods in the C++ standard template library's container classes:

~EpicsBuffer()
All data storage allocated for the buffer is reclaimed by the destructor.
EpicsBufferCreator *creator() const
An EpicsBuffer must know its own creator.
void reserve(epicsInt32 capacity)
Allocate space to store capacity octets. Some implementations may impose a max_size() smaller than the limits of memory. If either limit is exceeded by the request, an exception will be thrown. This method is used to request an increase or decrease in the amount of storage alloted for the buffer and may cause the data to move in memory, but does not change the data stored or its size. In particular it cannot reduce the capacity below the amount of data currently stored in the buffer, so to release all the data storage an application would call resize(0) then reserve(0).
epicsInt32 capacity() const
Returns the allocated capacity of the buffer.
void resize(epicsInt32 newsize)
Sets the number of octets currently stored, up to the current capacity.
epicsInt32 size() const
Returns the number of octets currently stored in the buffer.
epicsInt32 maxSize() const
Returns the size of the largest buffer that may be allocated. An attempt to allocate more than this amount of space will fail for certain, but there is no guarantee that this amount of memory is available. This method provides a way for application code to discover any limits that may be imposed by the particular buffer type.
bool mutable() const
Returns true if the buffer data can be modified, i.e. are puts allowed.
isEqual(const EpicsBuffer &cmp)
Compares the contents of this buffer with the contents of the buffer cmp, and returns true if the data is identical.
isEqual(epicsInt32 offset, epicsInt32 len, const epicsOctet *pdata)
Compares len octets of data starting offset octets into this buffer with the octet array pdata supplied by the caller. This method is mainly provided to simplify the implementation of the other isEqual method.
void expose(epicsInt32 offset, epicsInt32 &len, epicsOctet *&pdata)
A request for the address of len octets of buffer data starting offset octets into the buffer. If the buffer implementation uses segmented memory the maximum number of contiguous octets exposed is the segment size, which may be less than the length requested. In this case the value of len will be reduced before the method returns. The caller must process the data provided and call expose again with an increased offset to retrieve or modify subsequent segments.

Two versions of the expose method are provided, one for use when modifying the buffer and one where the buffer (and the returned data pointer) is const.

  • The const expose() method will only present data up to the current limit as reported by the size() method.
  • The non-const expose() method will present data up to the current capacity of the buffer, so application code that is reading as well as modifying the buffer's contents must keep track of the current value of size() and stop reading when this is reached (or use the both methods, the const one for reading and the other for writing).
  • The non-const version will throw an exception if called for a buffer that is immutable.

The design of the expose method is intended to increase the efficiency of data access. The caller must follow these conventions:

  • Must call resize(n) if the data length is to be changed.
  • Must not access storage outside the length returned by expose. The caller may have to make multiple expose calls to read or write a complete string.

Class methods that allocate or deallocate memory must be thread-safe; buffers could be resized, created or destroyed at any time from any thread. However synchronizing access to the data in the buffer is not the responsibility of this class, thus a buffer should probably not contain a mutex/lock, but its creator might.

EpicsBufferCreator

    class EpicsBufferCreator { // interface
    public:
        virtual const char *bufferType() const = 0;
        virtual EpicsBuffer *create(epicsInt32 elementSize = 1) = 0;
        virtual void destroy(EpicsBuffer *&pbuffer) = 0;
    };

An EpicsBufferCreator is an interface implemented by something that knows how to create a particular type of EpicsBuffer. How the creator manages the memory involved is up to the implementation - buffers and their data segments may be stored on a freelist by some implementations, and obtained from system memory by others. Three methods must be implemented:

const char *bufferType() const
Returns some well-known name of the buffer type created.
EpicsBuffer *create(epicsInt32 elementSize = 1)
Instanciates an EpicsBuffer which is appropriate for storing arrays of size elementSize. Array elements can never cross a segment boundary, thus a buffer segment can never be smaller than an individual element. This method may throw an exception if it is unable to create the buffer.
void destroy(EpicsBuffer *&pbuffer)
Executes pbuffer's destructor to reclaim the buffer storage, reclaims the pbuffer object itself, and sets pbuffer to NULL.

The way to deallocate an EpicsBuffer *pbuffer is like this:

    if (pbuffer) pbuffer->creator()->destroy(pbuffer);

or use the convenience function provided by EpicsBufferFactory which does exactly the same thing:

    EpicsBufferFactory::destroy(pbuffer);

Implementations of this class must be thread-safe; buffers could be created or destroyed at any time from any thread.

EpicsBufferFactory

    class EpicsBufferFactory {
    public:
        static void register(EpicsBufferCreator *creator);
        static EpicsBufferCreator *creator(const char *type);
        static EpicsBuffer *create(const char *type,
                                   epicsInt32 elementSize = 1) {
            return EpicsBufferFactory::creator(type)->create(elementSize);
        }
        static void destroy(EpicsBuffer *&pbuffer) {
            if (pbuffer) pbuffer->creator()->destroy(pbuffer);
        }
    };

The EpicsBufferFactory provides a registry of EpicsBuffer implementations, each defined by an EpicsBufferCreator. While each EpicsBufferCreator is ultimately responsible for creating all EpicsBuffers of its particular type, the EpicsBufferFactory provides a convenience function that looks up an EpicsBufferCreator by name and calls its create() function directly. Use of the factory to create buffers is optional, since the registry can also be queried to find an EpicsBufferCreator by name.

void register(EpicsBufferCreator *creator)
Adds creator to the list of known buffer types. It will throw an exception if the same name is already registered at a different address.
EpicsBufferCreator *creator(const char *type)
Looks up the EpicsBufferCreator associated with type and returns a pointer to it. Return NULL, or throw if not found?
EpicsBuffer *create(const char *type, epicsInt32 elementSize = 1)
Looks up the EpicsBufferCreator associated with type, passes elementSize to its create() method and returns the resulting EpicsBuffer pointer. Return NULL, or throw if not found?
void destroy(EpicsBuffer *&pbuffer)
Asks pbuffer's creator destroy the buffer, reclaiming its associated memory and set pbuffer to NULL.

It should be clear that creating a buffer is not the same as allocating storage for the data held in that buffer. The former is an EpicsBufferCreator's job, while the latter is the responsibility of the EpicsBuffer itself. However an EpicsBufferCreator may provide additional services to its EpicsBuffer instances such as free lists.




epicsString

epicsString.h defines a string class that uses an EpicsBuffer to hold its data. Within the core software all strings should be encoded using Unicode/UTF-8, but there is nothing specific to that encoding in the EpicsString interface, so other encodings may be stored if desired. EPICS will not provide software to translate between encodings.

    class EpicsString {
    public:
        EpicsString();
        EpicsString(const char *literal);
        EpicsString(const char *bufferType, epicsInt32 capacity);
        EpicsString(EpicsBufferCreator *creator, epicsInt32 capacity);
        virtual ~EpicsString();
        EpicsString& operator=(const EpicsString &rhs);
        void createBuffer(const char *bufferType, epicsInt32 capacity = 0)
        void createBuffer(EpicsBufferCreator *creator, epicsInt32 capacity = 0);
        EpicsBufferCreator *bufferCreator() const;
        void destroyBuffer();
        epicsInt32 get(epicsInt32 offset, epicsInt32 len,
                       epicsOctet *pto) const;
        epicsInt32 put(epicsInt32 offset, epicsInt32 len,
                       const epicsOctet *pfrom);
        epicsInt32 hash(epicsInt16 nBitsHashIndex) const;
        
        // These routines are as described for EpicsBuffer
        void reserve(epicsInt32 capacity);
        epicsInt32 capacity() const;
        void resize(epicsInt32 newsize);
        epicsInt32 size() const;
        epicsInt32 maxSize() const;
        bool mutable() const;
        bool isEqual(const EpicsBuffer &cmp) const;
        bool isEqual(epicsInt32 offset, epicsInt32 len,
                     const epicsOctet *pdata) const;
        bool expose(epicsInt32 offset, epicsInt32 &len,
                    epicsOctet *&pdata);
        bool expose(epicsInt32 offset, epicsInt32 &len,
                    const epicsOctet *&pdata) const;
    private:
        EpicsBuffer *pbuffer;
    private:   // Prevent compiler-implemented methods
        EpicsString(const EpicsString &str);   // No copy constructor
    };
    
    epicsBoolean operator==(const EpicsString &lhs, const EpicsString &rhs);
    epicsBoolean operator!=(const EpicsString &lhs, const EpicsString &rhs);

In Unicode/UTF-8 encoded strings, multiple octets may be needed to store a single character. However most code should be able to ignore the character encoding as long as it does not assume that any character can be stored in a single octet. As long as the final output device (be it a terminal window, a printer or some other software package) is expecting Unicode/UTF-8 characters, a string can be output using the printf family of methods.

The EpicsString data is not guaranteed to be null-terminated and in most cases will not be, so applications using the string must take precautions when interfacing with routines that expect the string to end with a zero byte. We may provide a type of contiguous EpicsBuffer that does guarantee a null terminator.

EpicsString provides the following methods:

EpicsString()
If a string is default constructed, one of the createBuffer() methods must be called to set the underlying buffer type before any data can be stored.
EpicsString(const char *literal)
Use of this constructor causes the string to have a readonly buffer that holds just the literal string given, without copying. This is intended to be efficient as it is likely to be commonly used.
EpicsString(const char *bufferType, epicsInt32 capacity)
EpicsString(picsInt16 bufferTypeId, epicsInt32 capacity)
Here a buffer type is specified, either by name or by identifier, so the selected buffer type will be created using the EpicsBufferFactory, and if capacity is non-zero the space will be reserved for at least that number of octets of data.
~EpicsString()
The destructor will destroy the EpicsBuffer if one has been created.
void createBuffer(const char *bufferType, epicsInt32 capacity)
void createBuffer(EpicsBufferCreator *creator, epicsInt32 capacity)
These methods create an EpicsBuffer to hold the string data, and if capacity is non-zero the space will be reserved for at least that number of octets of data.
If a buffer type has already been selected for the string, this method will throw an exception. (However this definition may change if it is found to be too onerous; the alternative would be to create a new buffer, copy the data to it and then destroy the old buffer)
EpicsBufferCreator *bufferCreator() const
Returns the EpicsBufferCreator used for the underlying buffer storage, or NULL if none has been set yet.
void destroyBuffer()
Causes the underlying buffer storage to be destroyed, after which one of the createBuffer() methods must be used before data can be stored in the string again.
epicsInt32 get(epicsInt32 offset, epicsInt32 len, epicsOctet *pto)
Copies up to len octets starting at offset from the string buffer to pto, and returns the number of octets transfered. The return value will be less than len if offset+len > size().
epicsInt32 put(epicsInt32 offset, epicsInt32 len, const epicsOctet *pfrom)
Copies up to len octets from pfrom into the buffer starting offset octets from the beginning, and returns the number of octets copied. The return value will be less than len if offset+len > capacity(). The string size will be updated if this call extends the string beyond its original size. However on entry offset must not be greater than size() or an exception will be thrown.
epicsInt32 hash(epicsInt16 nBitsHashIndex) const
Calculates an n-bit hash of the octets stored in the string buffer.

EpicsString also implements all of the routines in the EpicsBuffer interface. If an EpicsBuffer has been created these calls are forwarded to the underlying EpicsBuffer method. If an EpicsBuffer has not been created however, all methods except for createBuffer() will throw an exception.




epicsArray

epicsArray.h defines an array class that uses an EpicsBuffer to hold its data, which can be of any datatype that has a fixed size.

    class EpicsArray {
    public:
        EpicsArray(epicsInt32 elementSize = 0);
        EpicsArray(const char *bufferType, epicsInt32 elementSize = 0,
                   epicsInt32 capacity = 0);
        EpicsArray(EpicsBufferCreator *creator, epicsInt32 elementSize = 0,
                   epicsInt32 capacity = 0);
        virtual ~EpicsArray();
        EpicsArray& operator=(const EpicsArray &);
        void createBuffer(const char *bufferType, epicsInt32 capacity = 0);
        void createBuffer(EpicsBufferCreator *creator, epicsInt32 capacity = 0);
        EpicsBufferCreator *bufferCreator() const;
        void destroyBuffer();
        epicsOctet * element(epicsInt32 index);
        const epicsOctet * element(epicsInt32 index) const;
        epicsInt32 get(epicsInt32 offset, epicsInt32 len,
                       epicsOctet *pto) const;
        epicsInt32 put(epicsInt32 offset, epicsInt32 len,
                       const epicsOctet *pfrom);
        
        // Like EpicsBuffer, but units are elements not octets
        void reserve(epicsInt32 capacity);
        epicsInt32 capacity() const;
        void resize(epicsInt32 newsize);
        epicsInt32 size() const;
        epicsInt32 maxSize() const;
        bool mutable() const;
        void expose(epicsInt32 offset, epicsInt32 &len,
                    epicsOctet *&pdata);
        void expose(epicsInt32 offset, epicsInt32 &len,
                    const epicsOctet *&pdata) const;
    protected:
        void setElementSize(epicsInt32 elementSize);
        epicsInt32 getElementSize() const;
    private:
        epicsInt32 elementSize;
        EpicsBuffer *pbuffer;
    private:   // Prevent compiler-implemented methods
        EpicsArray(const EpicsArray &);            // No copy constructor
    };

An EpicsArray holds an array of elements of constant size.

EpicsArray has the following methods:

EpicsArray(epicsInt32 elementSize = 0)
If an array is default constructed, one of the createBuffer() methods must be called to set the underlying buffer type before any storage can be reserved for the array elements. The array must also be told its element size before buffer space can be reserved.
EpicsArray(const char *bufferType, epicsInt32 capacity = 0, epicsInt32 elementSize = 0)
EpicsArray(EpicsBufferCreator *creator, epicsInt32 capacity = 0, epicsInt32 elementSize = 0)
Here a buffer type is specified, either by name or by identifier, so the selected butter type will be created using the EpicsBufferFactory, and if both capacity and elementSize are non-zero the space will be reserved for at least that number of array elements.
~EpicsArray
The destructor will destroy the EpicsBuffer if one has been created.
EpicsArray& operator=(const EpicsArray &)
Assignment is possible between arrays (implemented by bit copying), providing the element sizes match exactly and the target array has enough storage to hold all of the data in the source array - call reserve() on the target array first if necessary. Think very carefully before assigning arrays of strings, structs or other aggregate types that store pointers, as this will result in duplicate pointers to the same objects. This method will throw an exception if the element sizes differ, and will truncate the array data if the target capacity is insufficient. Calls resize() as appropriate.
void createBuffer
These methods create an EpicsBuffer to hold the array data, and if capacity and elementSize are non-zero the space will be reserved for at least that number of array elements.
If a buffer type has already been selected for the array, this method will throw an exception. (However this definition may change if it is found to be too onerous; the alternative would be to create a new buffer, copy the data to it and then destroy the old buffer)
EpicsBufferCreator *bufferCreator() const
Returns the EpicsBufferCreator used for the underlying buffer storage, or NULL if none has been set yet.
destroyBuffer()
Causes the underlying buffer storage to be destroyed, after which one of the createBuffer() methods must be used before data can be stored in the array again.
epicsOctet * element(epicsInt32 index)
Returns a pointer giving direct access to the index'th element of the array, allowing this element to be modified. The pointer returned will have to be cast to an appropriate element type before use. Access to either adjacent array element must not be made by adjusting the pointer since the underlying buffer storage may be segmented - instead call element() again with the appropriately modified index.
const epicsOctet * element(epicsInt32 index) const
Returns a pointer giving const (read-only) access to the index'th element of the array, allowing this element to be read but not modified. If the underlying buffer type is not mutable, this method will throw an exception. The pointer returned will have to be cast to an appropriate element type before use. Access to either adjacent array element must not be made by adjusting the pointer since the underlying buffer storage may be segmented - instead call element() again with the appropriately modified index.
epicsInt32 get(epicsInt32 offset, epicsInt32 len, epicsOctet *pto)
Copies up to len elements starting at offset from the array buffer to pto, and returns the number of elements transfered. The return value will be less than len if offset+len > size().
epicsInt32 put(epicsInt32 offset, epicsInt32 len, const epicsOctet *pfrom)
Copies up to len elements from pfrom into the buffer starting offset elements from the beginning, and returns the number of elements copied. The return value will be less than len if offset+len > capacity(). The array size will be updated if this call extends the array beyond its original size. However on entry offset must not be greater than size() or an exception will be thrown.
void setElementSize(epicsInt32 elementSize)
This sets the size (in octets) of the element type to be stored in the array. If a buffer has already been used for previous array data, this will be released and the buffer capacity set to zero.
epicsInt32 getElementSize()
This returns the element size currently stored.

EpicsArray also implements the access routines from the EpicsBuffer interface, translating capacity, size and offset values from element counts into octets and passing them to the underlying EpicsBuffer. If an EpicsBuffer has not been created however, all these methods will throw an exception. The ability to change the element size is restricted to derived classes only.




epicsMDArray

epicsMDArray.h defines a multi-dimensional array class that uses two EpicsArrays, one for the sizes of each dimension and another for the array data, which can be of any datatype that has a fixed size.

    class EpicsMDArray {
    public:
        EpicsMDArray(epicsInt32 elementSize = 0);
        EpicsMDArray(const char *bufferType, epicsInt32 capacity = 0,
                     epicsInt32 elementSize = 0);
        EpicsMDArray(EpicsBufferCreator *creator, epicsInt32 capacity = 0,
                     epicsInt32 elementSize = 0);
        virtual ~EpicsMDArray();
        EpicsMDArray& operator=(const EpicsMDArray &);
        void createBuffer(const char *bufferType, epicsInt32 capacity = 0);
        void createBuffer(EpicsBufferCreator *creator, epicsInt32 capacity = 0);
        EpicsBufferCreator *bufferCreator() const;
        void destroyBuffer();
        void setDimensions(const EpicsArray &newDims);
        const EpicsArray& getDimensions() const;
        epicsInt32 get(epicsInt32 offset, epicsInt32 len,
                       epicsOctet *pto) const;
        epicsInt32 put(epicsInt32 offset, epicsInt32 len,
                       const epicsOctet *pfrom);
        
        // Like EpicsBuffer, but units are elements not octets
        void reserve(epicsInt32 capacity);
        epicsInt32 capacity() const;
        epicsInt32 size() const;
        epicsInt32 maxSize() const;
        void expose(epicsInt32 offset, epicsInt32 &len,
                    epicsOctet *&pdata);
        void expose(epicsInt32 offset, epicsInt32 &len,
                    const epicsOctet *&pdata) const;
    protected:
        void setElementSize(epicsInt32 elementSize);
        epicsInt32 getElementSize() const;
        void resize(epicsInt32 newsize);
    private:
        EpicsArray dimensions;     // EpicsArray<epicsInt32>
        EpicsArray elements;
    private:   // Prevent compiler-implemented methods
        EpicsMDArray(const EpicsMDArray &);            // No copy constructor
    };

An EpicsMDArray is a multi-dimensional array of elements of constant size, using two EpicsArrays, one to hold the element data and another for the size in each dimension.

The get(), put() and expose() methods all take an epicsInt32 offset parameter that specifies an element count into the underlying one dimensional array element buffer. Currently there are no methods provided for accessing elements of the array at some particular coordinate; such methods would be more appropriate for a derived class with a fixed number of dimensions.

EpicsMDArray has the following methods:

EpicsMDArray(epicsInt32 elementSize = 0)
If an array is default constructed, one of the createBuffer() methods must be called to set the underlying buffer type before any storage can be reserved for the array elements. The array must also be told its element size before buffer space can be reserved.
EpicsMDArray(const char *bufferType, epicsInt32 capacity = 0, epicsInt32 elementSize = 0)
EpicsMDArray(EpicsBufferCreator *creator, epicsInt32 capacity = 0, epicsInt32 elementSize = 0)
Here a buffer type is specified, either by name or by identifier, so the selected butter type will be created using the EpicsBufferFactory, and if both capacity and elementSize are non-zero the space will be reserved for at least that number of array elements.
~EpicsMDArray
The destructor will destroy the EpicsBuffers if any have been created.
void createBuffer
These methods create an EpicsBuffer to hold the array data, and if capacity and elementSize are non-zero the space will be reserved for at least that number of array elements.
If a buffer type has already been selected for the array, this method will throw an exception. (However this definition may change if it is found to be too onerous; the alternative would be to create a new buffer, copy the data to it and then destroy the old buffer)
EpicsBufferCreator *bufferCreator() const
Returns the EpicsBufferCreator used for the underlying buffer storage, or NULL if none has been set yet.
destroyBuffer()
Causes the underlying buffer storage to be destroyed, after which one of the createBuffer() methods must be used before data can be stored in the array again.
void setDimensions(const EpicsArray &newDims)
Changes the dimensionality of the array, setting the number of dimensions and the extent in each dimension to match newDims. This call may cause the capacity of the underlying buffers to be increased if necessary to hold an increased number of dimensions and/or element data. Call reserve() to free up any unneeded data storage if desired.
const EpicsArray& getDimensions() const
Returns a reference to the internal EpicsArray that holds the dimension information fot the element array.
epicsInt32 get(epicsInt32 offset, epicsInt32 len, epicsOctet *pto)
Copies up to len elements starting at offset from the element array buffer to pto, and returns the number of elements transfered. The return value will be less than len if offset+len > size().
epicsInt32 put(epicsInt32 offset, epicsInt32 len, const epicsOctet *pfrom)
Copies up to len elements from pfrom into the buffer starting offset elements from the beginning, and returns the number of elements copied. The return value will be less than len if offset+len > capacity(). The array size will be updated if this call extends the array beyond its original size. However on entry offset must not be greater than size() or an exception will be thrown.
void setElementSize(epicsInt32 elementSize)
This sets the size (in octets) of the element type to be stored in the array. If a buffer has already been used for previous array data, this will be released and the buffer capacity set to zero.
epicsInt32 getElementSize()
This returns the element size currently stored.

EpicsMDArray also implements the access routines from the EpicsBuffer interface, translating capacity, size and offset values from element counts into octets and passing them to the underlying EpicsBuffer. If an EpicsBuffer has not been created however, all these methods will throw an exception. The ability to change the element or array sizes is restricted to derived classes only.




EpicsEnum

An EpicsEnum is a 16-bit index value, which uses a standard interface to convert between choice strings and their index values. There may be more than one implementation of the interface.

epicsEnum.h contains these declarations:

    const int EpicsEnumInvalid = -1;
    
    class EpicsEnumChoices {
    public:
        virtual EpicsEnumChoices * duplicate() = 0;
        virtual void release() = 0;
        virtual epicsInt16 nChoices() const = 0;
        virtual epicsInt16 index(const EpicsString &choice) const = 0;
        virtual const EpicsString & choice(epicsInt16 index) const = 0;
    };
    
    class EpicsEnum {
    public:
        EpicsEnum();
        EpicsEnum(const EpicsEnum &rhs);
        EpicsEnum(EpicsEnumChoices *choices);
        virtual ~EpicsEnum();
        EpicsEnum& operator=(const EpicsEnum &rhs);
        
        void choices(EpicsEnumChoices *pchoices);
        EpicsEnumChoices *choices() const { return pchoices; }
        epicsInt16 nChoices() const { return pchoices ? pchoices->nChoices() : 0; }
        operator epicsInt16() const { return index; }
        operator const EpicsString &() const;
        EpicsEnum& operator=(epicsInt16 rhs);
        EpicsEnum& operator=(const EpicsString &rhs);
    protected:
        EpicsEnumChoices *pchoices;
        epicsInt16 index;
    };

The EpicsEnumChoices interface provides information about the values and choices available that can be use in an EpicsEnum. The EpicsEnum class itself provides storage for an enum index and a pointer to the describing interface implementation, and provides a number of different access methods.

The EpicsEnumChoices interface has five methods which must be implemented:

EpicsEnumChoices * clone()
Asks the interface for a pointer that another enum can use, permitting a copy to be made and returned or a reference count to be incremented.
void release()
Tells the interface that this enum is no longer referencing it, permitting a reference-counted implementation.
epicsInt16 nChoices() const
Returns the number of possible choices for this enumeration, which is also one more than the index number of the highest choice.
epicsInt16 index(const EpicsString &choice) const
Looks up and returns the index value associated with a particular choice string. If no match is found, it returns EpicsEnumInvalid.
const EpicsString & choice(epicsInt16 index) const
Looks up and returns the choice string associated with a particular index value. If the value is outside the legal range of choices, it throws an exception.

The EpicsEnum class defines the following methods:

EpicsEnum()
The default constructor initializes the enum to invalid. This cannot be changed until the choices to use has been set.
EpicsEnum(const EpicsEnum &rhs)
The copy constructor clone's the choices from its input parameter and copies the index value.
EpicsEnum(EpicsEnumChoices *choices)
This constructor sets the enum's choices interface (does not call clone).
~EpicsEnum()
The destructor releases the choice interface.
EpicsEnum& operator=(const EpicsEnum &rhs)
Assignment from another enum checks if the same choices interface is used and if not it releases the old interface and clones the new one. Finally it copies the value.
void choices(EpicsEnumChoices *pchoices)
Releases the existing choices interface if one is already set, and initializes itself with the new interface (does not call clone). This also resets the index to EpicsEnumInvalid.
EpicsEnumChoices *choices() const
Returns the choices interface used by this enum.
epicsInt16 nChoices() const
Returns the number of possible choices for this enumeration, which is also one more than the index number of the highest choice.
operator epicsInt16() const
A conversion operator, returns the current choice as an integer.
operator const EpicsString &() const
A conversion operator, returns the current choice as a string. Throws an exception if the current choice is EpicsEnumInvalid.
EpicsEnum& operator=(epicsInt16 rhs)
Assignment with an integer sets the index value. Throws an exception if the index is outside the allwed range.
EpicsEnum& operator=(const EpicsString &rhs)
Assignment with a string looks up and sets the index value. Throws an exception if the string cannot be