Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IntegerValue.cpp Source File

IntegerValue.cpp

00001 /*
00002  * IntegerValue.cpp
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 #include "IntegerValue.h"
00030 
00031 
00032 /*-------------------------------------------------------------------------*/
00033 IntegerValue::IntegerValue(long number)
00034 {
00035     _negative = (number < 0L);
00036     if (_negative)
00037         _number = -number;
00038     else
00039         _number = number;
00040 }
00041 /*-------------------------------------------------------------------------*/
00042 uint8_t IntegerValue::valueType() const
00043 {
00044     return VALUE_INTEGER;
00045 }
00046 /*-------------------------------------------------------------------------*/
00047 long IntegerValue::integerValue() const
00048 {
00049     return (_negative) ? -_number : _number;
00050 }
00051 /*-------------------------------------------------------------------------*/
00052 double IntegerValue::floatValue() const
00053 {
00054     return 0.0;
00055 }
00056 /*-------------------------------------------------------------------------*/
00057 const char * IntegerValue::characterValue() const
00058 {
00059     return 0;
00060 }
00061 /*-------------------------------------------------------------------------*/
00062 size_t IntegerValue::write(AbstractDataSink& sink) const
00063 {
00064     size_t n = 0;
00065     if (_negative)
00066         n += sink.write('-');
00067     n += sink.write(_number);
00068     return n;
00069 }
00070 /*-------------------------------------------------------------------------*/
00071 size_t IntegerValue::length() const
00072 {
00073     size_t l = 0;
00074 
00075     if (_negative)
00076         l++;
00077 
00078     unsigned long n = _number;
00079 
00080     do
00081     {
00082         n /= 10;
00083         l++;
00084     } while(n);
00085     return l;
00086 }
00087 /*-------------------------------------------------------------------------*/
00088 Value* IntegerValue::copy() const
00089 {
00090     long number;
00091 
00092     number = (_negative) ? -_number : _number;
00093     return new IntegerValue(number);
00094 }
00095 /*-------------------------------------------------------------------------*/