#include <mcs.hh>
This class let you create a high-level object that has its own base type, but is able to perform conversion to a different data type. Available base types are those of the mcs::Types enumeration, namely: integers, floating point, string, char*, struct tm*, time_t, memory buffers and pointers. Some base types (STRING, TINY_BLOB and BLOB) are variable length objects, that is their length varies between zero and a maximum specified in the constructor. All other base types are fixed length objects. All integer types can be either signed or unsigned. Conversion between different types follow standard rules (described below). If the conversion cannot be performed an exception is thrown.
This class is widely used inside MCS code due to its ease of use. Some examples follows:
Data di(INT); //Data object with base type INT Data ds(STRING, 5); //Data object with base type STRING (max length 5) //Store some values di = 1; //store 1 di = 2.7; //store 2, note that the value is truncated di = "3"; //store 3 di = "mcs"; //throws an exception because base type is an integer ds = "mcs"; //now its ok because base type is STRING ds = "giorgio"; //throws an exception because the string is too long //Retrieve values into different data type variables struct tm tm; di.tval(&tm); time_t tt = di.tval(); int ii = di.ival(); unsigned int ui = di.uival(); long long int il = di.lval(); unsigned long long int ul = di.ulval(); float ff = di.fval(); string ss = di.sval(); char cc[10]; di.cval(cc, 10);
The Data class features several constructors to allow writing "easy to read" code as well as several methods to perform conversion to/from any allowed data types (see all *val, and set*val methods). Also several assignment operators wraps the set*val methods. Furthermore a Data object can contain either a single value (scalar) or a multi-dimensional array of the same base data type values (see resize(), array(), dim()).
Each Data object has auxiliary informations:
The Data object can be used to access value of a database table, in this case the isAutoIncrement() and isNull() methods provide further information about the underlying database field.
Finally, the Data class extends the Serialize class so it can be serialized to be sent through the network or saved on disk.
Definition at line 3107 of file mcs.hh.
Public Member Functions | |
unsigned int | array (unsigned short int i1=0, unsigned short int i2=0, unsigned short int i3=0, unsigned short int i4=0, unsigned short int i5=0, unsigned short int i6=0, unsigned short int i7=0, unsigned short int i8=0, unsigned short int i9=0, unsigned short int i10=0, unsigned short int i11=0, unsigned short int i12=0, unsigned short int i13=0, unsigned short int i14=0, unsigned short int i15=0) |
Select a cell from the array. | |
unsigned int | arraySize () |
Return size of array. | |
void * | buffer () const |
Return a pointer to the internal buffer, use this at your own risk. | |
int | cval (char *c, int maxlength) const |
Convert internal data to a NULL terminated string and store it in a buffer. | |
Data (void *lbuf) | |
Build a Data object from a buffer prepared with Data.prepareBuffer(). | |
Data (void *lbuf, unsigned int size, unsigned char tag=0) | |
Build an object with base type BLOB. | |
Data (time_t v, unsigned char tag=0) | |
Build an object with base type TIME. | |
Data (struct tm v, unsigned char tag=0) | |
Build an object with base type TIME. | |
Data (string v, unsigned char tag=0) | |
Build an object with base type STRING. | |
Data (double v, unsigned char tag=0) | |
Build an object with base type DOUBLE. | |
Data (long long int v, unsigned char tag=0) | |
Build an object with base type BIGINT. | |
Data (int v, unsigned char tag=0) | |
Build an object with base type INT. | |
Data (MYSQL_BIND *bind=NULL, Types type=STRING, const char *name="", unsigned short int maxLength=0, bool isunsigned=false, unsigned int flags=0, unsigned char tag=0) | |
Constructor used to bind the object with database data. | |
Data (Types type, unsigned short int maxLength=0, bool isunsigned=false, string dimSpec="") | |
Constructor with specification of base type. | |
Data (const Data &from) | |
Copy constructor. Note that this will not copy any bind information. | |
Data () | |
Build Data object of base type STRING and length zero. | |
unsigned short int | dim (int d) |
Return length of a dimension. | |
double | dval () const |
Convert internal data to a double (8 bytes) floating point value. | |
void | emptyName () |
Set the object name to an empty string. | |
float | fval () const |
Convert internal data to a floating point value. | |
int | getDestID () |
int | getSourceID () |
unsigned char | getTag () |
Retrieve the value of the tag. | |
unsigned int | howManyDim () |
Return how many dimensions are in the array. | |
bool | isAutoIncrement () |
Tells if the database field is an auto increment field. | |
bool | isNull () |
Tells if no value is stored. | |
bool | isUnsigned () |
Tell if the object contains unsigned integers. | |
int | ival () const |
Convert internal data to a integer value. | |
unsigned short int | length () |
Return the actual length of the data in the internal buffer. | |
long long int | lval () const |
Convert internal data to a long long integer value. | |
unsigned short int | maxLength () |
Return the size of the internal buffer. | |
string | name () |
Return the name of the object. | |
unsigned int | objSize () |
Return how many bytes require the object to be serialized. | |
operator const char * () const | |
Wrapper cast operator to pval(). | |
operator const double () const | |
Wrapper cast operator to dval(). | |
operator const float () const | |
Wrapper cast operator to dval(). | |
operator const int () const | |
Wrapper cast operator to ival(). | |
operator const long long int () const | |
Wrapper cast operator to lval(). | |
operator const string () const | |
Wrapper cast operator to sval(). | |
operator const struct tm () const | |
Wrapper cast operator to tval(struct tm). | |
operator const time_t () const | |
Wrapper cast operator to tval(). | |
operator const unsigned int () const | |
Wrapper cast operator to uival(). | |
operator const unsigned long long int () const | |
Wrapper cast operator to ulval(). | |
Data & | operator() (const int i1=0, const int i2=0, const int i3=0, const int i4=0, const int i5=0, const int i6=0, const int i7=0, const int i8=0, const int i9=0, const int i10=0, const int i11=0, const int i12=0, const int i13=0, const int i14=0, const int i15=0) |
Same as array(). | |
Data & | operator= (Data &d) |
To assign the value of a Data object to another Data object. | |
Data & | operator= (const time_t v) |
Wrapper assignment operator to settimeval(time_t). | |
Data & | operator= (const struct tm v) |
Wrapper assignment operator to settimeval(struct tm). | |
Data & | operator= (const string v) |
Wrapper assignment operator to setsval(string). | |
Data & | operator= (const char *v) |
Wrapper assignment operator to setsval(string). | |
Data & | operator= (const double v) |
Wrapper assignment operator to setdval(double). | |
Data & | operator= (const unsigned long long int v) |
Wrapper assignment operator to setulval(unsigned long long int). | |
Data & | operator= (const long long int v) |
Wrapper assignment operator to setlval(long long int). | |
Data & | operator= (const unsigned int v) |
Wrapper assignment operator to setuival(unsigned int). | |
Data & | operator= (const int v) |
Wrapper assignment operator to setival(int). | |
string | print () |
Returns a string representation of the object, for debug purpose. | |
void * | pval () const |
void | resize (string dimSpec) |
Create or resize a multi-dimensional array. | |
void | resizeVaryingDim (unsigned short int newsize) |
Resize variable length dimension. | |
void | setblob (void *lbuf, unsigned int size) |
Copy "size" bytes from the address given in "lbuf" parameter. | |
void | setcval (const char *v) |
Convert string value to base type and store in internal buffer. | |
void | setDestID (int id) |
void | setdval (double v) |
Convert double value to base type and store in internal buffer. | |
void | setival (int v) |
Convert int value to base type and store in internal buffer. | |
void | setlval (long long int v) |
Convert long int value to base type and store in internal buffer. | |
void | setName (string name) |
void | setNull (bool null=true) |
Set null flag. Following call to isNull() returns the value used here as parameter. | |
void | setpval (void *p) |
void | setSourceID (int id) |
void | setsval (string v) |
Convert string value to base type and store in internal buffer. | |
void | setTag (unsigned char tag) |
Set a new value to internal tag. | |
void | setTimeMode (enum TimeMode tm) |
void | settimenow () |
Convert current time value to base type and store in internal buffer. | |
void | settimeval (time_t v) |
Convert time_t value to base type and store in internal buffer. | |
void | settimeval (struct tm v) |
Convert "struct tm" value to base type and store in internal buffer. | |
void | setuival (unsigned int v) |
Convert unsigned int value to base type and store in internal buffer. | |
void | setulval (unsigned long long int v) |
Convert unsigned long int value to base type and store in internal buffer. | |
string | sval (bool addWhiteSpaces=false) const |
Convert internal data to a string object. | |
void | tval (struct tm *t) const |
Convert internal data to a struct tm and store it in a buffer. | |
time_t | tval () const |
Convert internal data to a time_t value. | |
Types | type () |
Return the base type of the object. | |
unsigned int | uival () const |
Convert internal data to an unsigned integer value. | |
unsigned long long int | ulval () const |
Convert internal data to an unsigned long long integer value. | |
unsigned int | varyingDim () |
Return the index of the dimension that may vary. | |
~Data () | |
Destructor Frees internal buffer. | |
Static Public Member Functions | |
static long long int | MaxValue (Types ltype, bool flunsigned) |
Return the maximum integer value for the base type specified. | |
static long long int | MinValue (Types ltype, bool flunsigned) |
Return the minimum integer value for the base type specified. | |
static void | parseTime (string s, struct tm *ts) |
Fills the "struct tm*" passed as argument with the current local datetime. | |
Private Member Functions | |
void | init (MYSQL_BIND *bind, Types type, const char *name="", unsigned short int maxLength=0, bool isunsigned=false, unsigned int flags=0) |
Initialize internal structures. | |
void | reallocBuffer () |
bool | serialize_buffer (char *&from, unsigned int &size) |
Prepare a buffer with all informations contained in the object. | |
Private Attributes | |
unsigned int | arrpos |
Last selected array cell. | |
unsigned int | arrsize |
Array size, i.e. how many cells are in the array. | |
char * | buf |
Data buffer. | |
unsigned int | bufsize |
Size of data buffer. | |
DateTime | dt |
int | id_dest |
The userid of the thread that will receive this object. | |
int | id_source |
The userid of the thread who send this object. | |
bool | lautoincr |
If the AUTO_INCREMENT flag is true. | |
MYSQL_BIND * | lbind |
Internal reference to MYSQL_BIND structure. | |
unsigned short int | ldim [15] |
Size of each dimension. | |
unsigned char | ldimspec |
Multi-dimensional array specification. | |
unsigned int | lflags |
Flags (EXPERIMENTAL). | |
my_bool | lisnull |
The object has a null value. | |
bool | lisunsigned |
Base type is unsigned. | |
unsigned long | llength |
Actual length of real data in the buffer, useful only when the base type is a variable length type. | |
unsigned short int | lmaxlength |
Max number of bytes that can be stored in the buffer (size of the buffer). | |
string | lname |
Object name. | |
Types | ltype |
Object base type. | |
MCS_DEBUG_ALLOC | |
unsigned short int | mult [15] |
Multiplicative coefficients to calculate arrpos. | |
unsigned char | tag |
Tag for user convenience. | |
Static Private Attributes | |
static const char * | dafmt = "%04d-%02d-%02d" |
Format to handle date in sprintf/sscanf calls. | |
static const char * | dfmt = "%lf" |
Format to handle double float in sprintf/sscanf calls. | |
static const char * | dtfmt = "%04d-%02d-%02d %02d:%02d:%02g" |
Format to handle datetime in sprintf/sscanf calls. | |
static const char * | ffmt = "%f" |
Format to handle float in sprintf/sscanf calls. | |
static const char * | ifmt = "%d" |
Format to handle integers in sprintf/sscanf calls. | |
static const char * | lfmt = "%ld" |
Format to handle long integers in sprintf/sscanf calls. | |
static const char * | tmfmt = "%02d:%02d:%02g" |
Format to handle time in sprintf/sscanf calls. |
mcs::Data::Data | ( | ) |
mcs::Data::Data | ( | const Data & | from | ) |
mcs::Data::Data | ( | Types | type, | |
unsigned short int | maxLength = 0 , |
|||
bool | isunsigned = false , |
|||
string | dimSpec = "" | |||
) |
Constructor with specification of base type.
With this constructor the object will have the base type specified in the parameters but won't have any value stored in the internal buffer, and no bind informations.
type | Base type for the object; | |
maxLength | Max length of the object if "type" is a variable length type; | |
isunsigned | Unsigned flag of the object, default is false (so that the data is signed); |
mcs::Data::Data | ( | MYSQL_BIND * | bind = NULL , |
|
Types | type = STRING , |
|||
const char * | name = "" , |
|||
unsigned short int | maxLength = 0 , |
|||
bool | isunsigned = false , |
|||
unsigned int | flags = 0 , |
|||
unsigned char | tag = 0 | |||
) |
Constructor used to bind the object with database data.
bind | A bind structure address, retrieved from a Query object. It can be NULL, if the object is not intended for db use; | |
type | Base type for the object; | |
name | Name of the object, this can be NULL for all practical use, it becomes important when the object is created inside a Query object because you can specifiy the name of a parameter or a field instead of its positional index; | |
maxLength | Size of internal buffer when base type is STRING; | |
isunsigned | Unsigned flag of the object, default is false (so that the data is signed); | |
flags | Database flags for the field; | |
tag | Set a new value to internal tag. |
mcs::Data::Data | ( | int | v, | |
unsigned char | tag = 0 | |||
) |
mcs::Data::Data | ( | long long int | v, | |
unsigned char | tag = 0 | |||
) |
mcs::Data::Data | ( | double | v, | |
unsigned char | tag = 0 | |||
) |
mcs::Data::Data | ( | string | v, | |
unsigned char | tag = 0 | |||
) |
mcs::Data::Data | ( | struct tm | v, | |
unsigned char | tag = 0 | |||
) |
mcs::Data::Data | ( | time_t | v, | |
unsigned char | tag = 0 | |||
) |
mcs::Data::Data | ( | void * | lbuf, | |
unsigned int | size, | |||
unsigned char | tag = 0 | |||
) |
Build an object with base type BLOB.
The max length is given by the parameter "size". This constructor also copies "size" bytes from the source "lbuf" to the internal buffer.
lbuf | Pointer to the buffer in memory. | |
size | Size of the BLOB. | |
tag | Optional tag for the object. |
mcs::Data::Data | ( | void * | lbuf | ) |
unsigned int mcs::Data::array | ( | unsigned short int | i1 = 0 , |
|
unsigned short int | i2 = 0 , |
|||
unsigned short int | i3 = 0 , |
|||
unsigned short int | i4 = 0 , |
|||
unsigned short int | i5 = 0 , |
|||
unsigned short int | i6 = 0 , |
|||
unsigned short int | i7 = 0 , |
|||
unsigned short int | i8 = 0 , |
|||
unsigned short int | i9 = 0 , |
|||
unsigned short int | i10 = 0 , |
|||
unsigned short int | i11 = 0 , |
|||
unsigned short int | i12 = 0 , |
|||
unsigned short int | i13 = 0 , |
|||
unsigned short int | i14 = 0 , |
|||
unsigned short int | i15 = 0 | |||
) |
unsigned int mcs::Data::arraySize | ( | ) |
void * mcs::Data::buffer | ( | ) | const |
int mcs::Data::cval | ( | char * | c, | |
int | maxlength | |||
) | const |
Convert internal data to a NULL terminated string and store it in a buffer.
c | Address of the buffer. | |
maxlength | Max number of bytes that can be copied in the buffer. |
unsigned short int mcs::Data::dim | ( | int | d | ) |
double mcs::Data::dval | ( | ) | const |
void mcs::Data::emptyName | ( | ) |
float mcs::Data::fval | ( | ) | const |
unsigned char mcs::Data::getTag | ( | ) |
unsigned int mcs::Data::howManyDim | ( | ) |
void mcs::Data::init | ( | MYSQL_BIND * | bind, | |
Types | type, | |||
const char * | name = "" , |
|||
unsigned short int | maxLength = 0 , |
|||
bool | isunsigned = false , |
|||
unsigned int | flags = 0 | |||
) | [private] |
bool mcs::Data::isAutoIncrement | ( | ) |
bool mcs::Data::isUnsigned | ( | ) |
int mcs::Data::ival | ( | ) | const |
unsigned short int mcs::Data::length | ( | ) |
long long int mcs::Data::lval | ( | ) | const |
unsigned short int mcs::Data::maxLength | ( | ) |
long long int mcs::Data::MaxValue | ( | Types | ltype, | |
bool | flunsigned | |||
) | [static] |
long long int mcs::Data::MinValue | ( | Types | ltype, | |
bool | flunsigned | |||
) | [static] |
Return the minimum integer value for the base type specified.
ltype | Base type for which compute the min value. | |
flunsigned | If the value should be considered unsigned. |
unsigned int mcs::Data::objSize | ( | ) |
mcs::Data::operator const char * | ( | ) | const [inline] |
mcs::Data::operator const double | ( | ) | const [inline] |
mcs::Data::operator const float | ( | ) | const [inline] |
mcs::Data::operator const int | ( | ) | const [inline] |
mcs::Data::operator const long long int | ( | ) | const [inline] |
mcs::Data::operator const string | ( | ) | const [inline] |
mcs::Data::operator const struct tm | ( | ) | const [inline] |
mcs::Data::operator const time_t | ( | ) | const [inline] |
mcs::Data::operator const unsigned int | ( | ) | const [inline] |
mcs::Data::operator const unsigned long long int | ( | ) | const [inline] |
Data & mcs::Data::operator() | ( | const int | i1 = 0 , |
|
const int | i2 = 0 , |
|||
const int | i3 = 0 , |
|||
const int | i4 = 0 , |
|||
const int | i5 = 0 , |
|||
const int | i6 = 0 , |
|||
const int | i7 = 0 , |
|||
const int | i8 = 0 , |
|||
const int | i9 = 0 , |
|||
const int | i10 = 0 , |
|||
const int | i11 = 0 , |
|||
const int | i12 = 0 , |
|||
const int | i13 = 0 , |
|||
const int | i14 = 0 , |
|||
const int | i15 = 0 | |||
) |
To assign the value of a Data object to another Data object.
This operator is used to copy the value of a Data object to another Data object. Note that, as in any other use of the assignment operator, only the value is copied, not all the other settings of a Data object. This operator will be called each time you assign a Data object to another Data object. In the particular cas in which you are creating a Data object with an assignment the copy constructor will be called instead of this.
Data& mcs::Data::operator= | ( | const time_t | v | ) | [inline] |
Data& mcs::Data::operator= | ( | const struct tm | v | ) | [inline] |
Data& mcs::Data::operator= | ( | const string | v | ) | [inline] |
Data& mcs::Data::operator= | ( | const char * | v | ) | [inline] |
Data& mcs::Data::operator= | ( | const double | v | ) | [inline] |
Data& mcs::Data::operator= | ( | const unsigned long long int | v | ) | [inline] |
Data& mcs::Data::operator= | ( | const long long int | v | ) | [inline] |
Data& mcs::Data::operator= | ( | const unsigned int | v | ) | [inline] |
Data& mcs::Data::operator= | ( | const int | v | ) | [inline] |
void mcs::Data::parseTime | ( | string | s, | |
struct tm * | ts | |||
) | [static] |
Fills the "struct tm*" passed as argument with the current local datetime.
Parse string to extract date time information.
The date time format must be one of: YYYY-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS otherwise an exception will be thrown.
s | String to be parsed. | |
ts | Address of a struct tm that will be filled with parsed information. |
ERROR | MSG_CONVERSION_DATETIME. |
string mcs::Data::print | ( | ) |
void mcs::Data::resize | ( | string | dimSpec | ) |
Create or resize a multi-dimensional array.
The array can have as much as MCS_DATA_NDIM dimensions (actually 15), and each dimension can have as much as 65535 positions.
When you call this method you must specify:
All this informations are given in a string formatted using the VOTable specifications (http://www.ivoa.net/Documents/latest/VOT.html) for the "arraysize" attribute of a "FIELD" node.
Some examples are:
void mcs::Data::resizeVaryingDim | ( | unsigned short int | newsize | ) |
Resize variable length dimension.
If a dimension is allowed to vary in length (see resize()) you can change its length with this method. If you increase the size of the dimension then all values already present in the array will be preserved and new values will be set to zeroes.
If no dimension is allowed to vary then an exception will be thrown.
bool mcs::Data::serialize_buffer | ( | char *& | from, | |
unsigned int & | size | |||
) | [private, virtual] |
Prepare a buffer with all informations contained in the object.
Once the buffer is prepared you can rebuild an identical object (except for the bind information) using the Data(void*) cosntructor.
If "*lbuf" is NULL then the buffer will be allocated, otherwise "size" must be set to the length of the buffer pointed by "*lbuf";
lbuf | Address of a pointer to a buffer; | |
size | Size of the buffer if it should not be allocated. |
ERROR | MSG_NOT_ENOUGH_SPACE. |
Reimplemented from mcs::Serializable.
void mcs::Data::setblob | ( | void * | lbuf, | |
unsigned int | size | |||
) |
void mcs::Data::setcval | ( | const char * | v | ) |
void mcs::Data::setdval | ( | double | v | ) |
void mcs::Data::setival | ( | int | v | ) |
void mcs::Data::setlval | ( | long long int | v | ) |
void mcs::Data::setNull | ( | bool | null = true |
) |
void mcs::Data::setsval | ( | string | v | ) |
void mcs::Data::setTag | ( | unsigned char | tag | ) |
void mcs::Data::settimenow | ( | ) |
void mcs::Data::settimeval | ( | time_t | v | ) |
void mcs::Data::settimeval | ( | struct tm | v | ) |
Convert "struct tm" value to base type and store in internal buffer.
void mcs::Data::setuival | ( | unsigned int | v | ) |
void mcs::Data::setulval | ( | unsigned long long int | v | ) |
string mcs::Data::sval | ( | bool | addWhiteSpaces = false |
) | const |
void mcs::Data::tval | ( | struct tm * | t | ) | const |
time_t mcs::Data::tval | ( | ) | const |
Types mcs::Data::type | ( | ) |
unsigned int mcs::Data::uival | ( | ) | const |
unsigned long long int mcs::Data::ulval | ( | ) | const |
unsigned int mcs::Data::varyingDim | ( | ) |
Return the index of the dimension that may vary.
The last dimension of the multi-dimensional array may vary in length (see resizeVaryingDim()) if it is allowed to do so. This method returns the index of the dimension which is allowed to vary in the 1..MCS_DATA_NDIM range, or 0 if no dimension can change.
unsigned int mcs::Data::arrpos [private] |
unsigned int mcs::Data::arrsize [private] |
char* mcs::Data::buf [private] |
unsigned int mcs::Data::bufsize [private] |
const char * mcs::Data::dafmt = "%04d-%02d-%02d" [static, private] |
const char * mcs::Data::dfmt = "%lf" [static, private] |
const char * mcs::Data::dtfmt = "%04d-%02d-%02d %02d:%02d:%02g" [static, private] |
const char * mcs::Data::ffmt = "%f" [static, private] |
int mcs::Data::id_dest [private] |
int mcs::Data::id_source [private] |
const char * mcs::Data::ifmt = "%d" [static, private] |
bool mcs::Data::lautoincr [private] |
MYSQL_BIND* mcs::Data::lbind [private] |
unsigned short int mcs::Data::ldim[15] [private] |
unsigned char mcs::Data::ldimspec [private] |
unsigned int mcs::Data::lflags [private] |
const char * mcs::Data::lfmt = "%ld" [static, private] |
my_bool mcs::Data::lisnull [private] |
bool mcs::Data::lisunsigned [private] |
unsigned long mcs::Data::llength [private] |
unsigned short int mcs::Data::lmaxlength [private] |
string mcs::Data::lname [private] |
Types mcs::Data::ltype [private] |
unsigned short int mcs::Data::mult[15] [private] |
unsigned char mcs::Data::tag [private] |
const char * mcs::Data::tmfmt = "%02d:%02d:%02g" [static, private] |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha3
|