Generic SmartRest library

Dependents:   SmartRestUnitTest MbedSmartRest MbedSmartRestStreaming

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Value.h Source File

Value.h

Go to the documentation of this file.
00001 /*
00002  * Value.h
00003  *
00004  * Created on: Nov 1, 2013
00005  * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
00006  *
00007  * Copyright (c) 2013 Cumulocity GmbH
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining
00010  * a copy of this software and associated documentation files (the
00011  * "Software"), to deal in the Software without restriction, including
00012  * without limitation the rights to use, copy, modify, merge, publish,
00013  * distribute, sublicense, and/or sell copies of the Software, and to
00014  * permit persons to whom the Software is furnished to do so, subject to
00015  * the following conditions:
00016  *
00017  * The above copyright notice and this permission notice shall be
00018  * included in all copies or substantial portions of the Software.
00019  *
00020  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00021  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00022  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00023  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00024  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00025  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00026  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00027  */
00028 
00029 /**
00030  * @file Value.h
00031  * Represents a generic data value.
00032  */
00033 
00034 #ifndef VALUE_H
00035 #define VALUE_H
00036 
00037 #ifdef HAVE_CONFIG_H
00038 #include <config.h>
00039 #endif
00040 
00041 #include <stddef.h>
00042 #include <stdint.h>
00043 #include "AbstractDataSink.h"
00044 
00045 /** Return value indicating a null value. */
00046 #define VALUE_NULL 0
00047 /** Return value indicating an integer value. */
00048 #define VALUE_INTEGER 1
00049 /** Return value indicating a floating-point number. */
00050 #define VALUE_FLOAT 2
00051 /** Return value indicating a character string. */
00052 #define VALUE_CHARACTER 3
00053 
00054 /**
00055  * A generic value offering means to obtain the value type and contents
00056  * as well as writing the value to a sink.
00057  */
00058 class Value
00059 {
00060 public:
00061     virtual ~Value() { };
00062 
00063     /**
00064      * Returns the value type which can be VALUE_NULL, VALUE_INTEGER,
00065      * VALUE_FLOAT or VALUE_CHARACTER.
00066      * @return the value type
00067      */
00068     virtual uint8_t valueType() const = 0;
00069 
00070     /**
00071      * Returns the integer value if an integer type, otherwise zero.
00072      * @return integer value
00073      */
00074     virtual long integerValue() const = 0;
00075 
00076     /**
00077      * Returns the float value if a float type, otherwise zero.
00078      * @return float value
00079      */
00080     virtual double floatValue() const = 0;
00081 
00082     /**
00083      * Returns the character value if a character type, otherwise zero.
00084      * @return character value
00085      */
00086     virtual const char * characterValue() const = 0;
00087 
00088     /**
00089      * Writes the value to a sink.
00090      * If necessary, quotes and escape characters may be added.
00091      * @param sink the sink to write the value to
00092      * @return the number of bytes written
00093      */
00094     virtual size_t write(AbstractDataSink&) const = 0;
00095 
00096     /**
00097      * Returns the number of bytes required to write this value to a sink.
00098      * @return number of bytes required to write this value
00099      */
00100     virtual size_t length() const = 0;
00101 
00102     /**
00103      * Copies this value to the heap. Referenced contents must also be
00104      * copied.
00105      * @return a new value instance allocated on the heap
00106      */
00107     virtual Value* copy() const = 0;
00108 };
00109 
00110 #endif