TypeΒΆ
-
class
Type
The Type class is one of the central pieces of the em-core library. It encapsulates information about the underlying C++ datatypes (e.g. float, int8_t, double, string, etc) that are internally managed via templates. This class is implemented following some sort of Singleton pattern and this means that each instance of the same type will share the implementation and avoid data duplication. Moreover, the Type class provides several functions to manipulate chunks of memory in a generic way. This is the base for the implementation of a generic value container (Object class) or multi-elements containers (Array and Image classes).
Public Types
-
enum
Operation
Enumerate with common operations applied to most type. Basic arithmetic operations can only be applied to arithmetic types.
Values:
-
NO_OP
= 0
-
CAST
= 'c'
-
ADD
= '+'
-
SUB
= '-'
-
MUL
= '*'
-
DIV
= '/'
-
LOG
= 'l'
-
SQRT
= 's'
-
Public Functions
-
Type
() Empty constructor, Null type
-
size_t
getId
() const Return an unique identifier for the Type within a given run.
-
std::string
getName
() const Return the name of the type
-
std::size_t
getSize
() const Return the size in bytes of this type
-
bool
isPod
() const Return True if this type is a plain old type (POD)
-
bool
isTriviallyCopyable
() const Return True if this type is trivially copyable.
Objects of trivially-copyable types are the only C++ objects that may be safely copied with std::memcpy or serialized to/from binary files with std::ofstream::write()/std::ifstream::read(). In general, a trivially copyable type is any type for which the underlying bytes can be copied to an array of char or unsigned char and into a new object of the same type, and the resulting object would have the same value as the original.
-
bool
isNull
() const Return True if this type is the NullType
-
std::string
toString
() const String representation of a Type
-
void
copy
(const void *inputMem, void *outputMem, size_t count) const Copy N elements from inputMem to outputMem assuming both memory locations point to data of this Type.
This function is useful for memory manipulation from generic memory container such as Array or Image where they store the pointer to the data and know the underlying data type.
- Parameters
inputMem
: Memory location of the input dataoutputMem
: Memory location of the output datacount
: Number of elements that are in inputMem
-
void
operate
(Operation op, const void *inputMem, const Type &inputType, void *outputMem, size_t count, bool singleInput = false) const Operate on N elements from inputMem (of type inputType) and store the results into outputMem (of type of the caller type object).
- Parameters
op
: Operation to be appliedinputMem
: Memory location of the input dataoutputMem
: Memory where cast elements will be putcount
: Number of elements in both input and outputinputType
: The Type of the elements in inputMemoutputMem
: Memory where resulting elements will be storedcount
: Number of elements in the outputsingleInput
: If true, the inputMem points to a single value, otherwise the outputMem has the same size (count elements) of input
-
void *
allocate
(size_t count) const Allocate memory for N elements of this Type.
- Return
The pointer to the allocated memory
- Parameters
count
: Number of elements to be allocated
-
void
deallocate
(void *inputMem, size_t count) const Release the memory allocated for N elements of this Type.
- Parameters
inputMem
: Pointer to allocated memory location.count
: Number of elements that were allocated.
-
void
toStream
(const void *inputMem, std::ostream &stream, size_t count) const Push N elements of this Type to an output stream.
- Parameters
inputMem
: Pointer to the memory location of the elements.stream
: Output stream.count
: Number of elements.
-
void
fromStream
(std::istream &stream, void *outputMem, size_t count) const Read N elements of this Type from an input stream.
- Parameters
stream
: Input stream.outputMem
: Memory location where to put read elements.count
: Number of elements to read from the stream.
Public Static Functions
-
template<class
T
>
static const Type &get
() Get a reference to the requested Type instance.
-
static Type
inferFromString
(const std::string &str) Infer a Type from a string literal. It will check first if it has non-numeric characters in which case the type will be TypeString. If it is numeric, it will be differentiated between Integer and Double.
- Return
The inferred Type from the string literal
-
static Type
inferFromString
(const char *str, size_t n) Infer a Type from a char * taking into account n first characters. This function is useful when parsing multiple values from a string.
- Return
The inferred Type from the string literal
- Parameters
str
: Input string as char *n
: Number of characters to take into account
-
static void
swapBytes
(void *mem, size_t count, size_t typeSize) Swap the bytes order
- Parameters
mem
: Pointer to datacount
: Number of data elementstypeSize
: Number of bytes for each element
-
static bool
isLittleEndian
() Returns true if machine is little endian else false
-
class
Impl
Base class for internal Type implementation. By default, this class will correspond to the Null type instance and will raise an error on most operations. These methods should be overriden in subclasses of real types.
Subclassed by emcore::TypeImplBaseT< T >, emcore::TypeImplBaseT< std::complex< double > >, emcore::TypeImplBaseT< std::complex< float > >
-
enum