V4 Design: epicsTypes

From EPICSWIKI
Revision as of 20:47, 14 June 2005 by AndrewJohnson (talk | contribs) (Fixes from Marty, modified and documented EpicsEnum)

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 capacity = 0,
                   epicsInt32 elementSize = 0);
        EpicsArray(EpicsBufferCreator *creator, epicsInt32 capacity = 0,
                   epicsInt32 elementSize = 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