Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FloatValue.h Source File

FloatValue.h

00001 /*
00002  * FloatValue.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 #ifndef FLOATVALUE_H
00030 #define FLOATVALUE_H
00031 
00032 #include "config.h"
00033 #include <stdint.h>
00034 #include "Value.h"
00035 
00036 #define SMARTREST_FLOATVALUE_DETECTPREC 255
00037 
00038 /**
00039  * A floating-point value.
00040  * 
00041  * Example:
00042  * @code
00043  * // decleration                       // serialized form
00044  * FloatValue val1(3.1415, 2);          // 3.14
00045  * FloatValue val2(3.1234, 0);          // 3
00046  * FloatValue val3(3.95, 0);            // 4
00047  * FloatValue val4(0.55, 1, false);     // .5
00048  * FloatValue val4(0.55, 2, true);      // 0.55
00049  * @encode
00050  */
00051 class FloatValue : public Value
00052 {
00053     public:
00054         /**
00055          * Creates a new instance of a floating-point value.
00056          * @param number the floating-point number
00057          * @param digits precision in number of digits
00058          * @param zflag whether to write a zero before the floating point
00059          */
00060         FloatValue(double, uint8_t = SMARTREST_FLOATVALUE_DETECTPREC, bool = true);
00061 
00062         virtual uint8_t valueType() const;
00063         virtual long integerValue() const;
00064         virtual double floatValue() const;
00065         virtual const char * characterValue() const;
00066 
00067         virtual size_t write(AbstractDataSink&) const;
00068         virtual size_t length() const;
00069         virtual Value* copy() const;
00070 
00071     protected:
00072         uint8_t detectPrecision(double);
00073 
00074     private:
00075         double _number;
00076         bool _negative;
00077         uint8_t _digits;
00078         bool _zflag;
00079 };
00080 
00081 #endif