reSIProcate/stack  9694
QValue.hxx
Go to the documentation of this file.
00001 #if !defined(RESIP_QVALUE_HXX)
00002 #define RESIP_QVALUE_HXX 
00003 
00004 #include "rutil/Data.hxx"
00005 #include "rutil/ParseBuffer.hxx"
00006 
00007 #ifndef RESIP_FIXED_POINT
00008 #include <math.h>
00009 // Required due to float point inaccuracies and platform dependent issues (ie. rounding)
00010 static int doubleToInt(const double d) { double f = floor(d); double c = ceil(d); return (((c-d) >= (d-f)) ? (int)f :(int)c); }
00011 #endif
00012 
00013 namespace resip
00014 {
00015    class QValue 
00016    {
00017    public:
00018       explicit QValue() : mValue(0) { }
00019       explicit QValue(int val) : mValue(val) { }
00020       explicit QValue(const Data& data) { setValue(data); }
00021 
00022       operator int() const { return mValue; }
00023 
00024       bool operator<(const QValue& rhs) const { return mValue < rhs.mValue; }
00025       bool operator>(const QValue& rhs) const { return mValue > rhs.mValue; }
00026       bool operator<=(const QValue& rhs) const { return mValue <= rhs.mValue; }
00027       bool operator>=(const QValue& rhs) const { return mValue >= rhs.mValue; }
00028       bool operator==(const QValue& rhs) const { return mValue == rhs.mValue; }
00029       bool operator!=(const QValue& rhs) const { return mValue != rhs.mValue; }                 
00030 
00031       bool operator<(const int rhs) const { return mValue < rhs; }
00032       bool operator>(const int rhs) const { return mValue > rhs; }
00033       bool operator<=(const int rhs) const { return mValue <= rhs; }
00034       bool operator>=(const int rhs) const { return mValue >= rhs; }
00035       bool operator==(const int rhs) const { return mValue == rhs; }
00036       bool operator!=(const int rhs) const { return mValue != rhs; }                    
00037 
00038       bool operator<(const long rhs) const { return mValue < rhs; }
00039       bool operator>(const long rhs) const { return mValue > rhs; }
00040       bool operator<=(const long rhs) const { return mValue <= rhs; }
00041       bool operator>=(const long rhs) const { return mValue >= rhs; }
00042       bool operator==(const long rhs) const { return mValue == rhs; }
00043       bool operator!=(const long rhs) const { return mValue != rhs; }                   
00044 
00045       QValue& operator=(const QValue& rhs) { mValue = rhs.mValue;  return (*this); }
00046       QValue& operator=(const int rhs) { setValue(rhs); return (*this); }                       
00047       QValue& operator=(const long rhs) { setValue(rhs); return (*this); }                      
00048 
00049 #ifndef RESIP_FIXED_POINT
00050 
00051       float floatVal() const { return mValue/float(1000.0); }
00052 
00053       operator float() const { return floatVal(); }
00054       operator double() const { return (double) floatVal(); }           
00055 
00056       bool operator<(const float rhs) const { return mValue < doubleToInt(rhs*1000.0); }
00057       bool operator>(const float rhs) const { return mValue > doubleToInt(rhs*1000.0); }
00058       bool operator<=(const float rhs) const { return mValue <= doubleToInt(rhs*1000.0); }
00059       bool operator>=(const float rhs) const { return mValue >= doubleToInt(rhs*1000.0); }
00060       bool operator==(const float rhs) const { return mValue == doubleToInt(rhs*1000.0); }
00061       bool operator!=(const float rhs) const { return mValue != doubleToInt(rhs*1000.0); }
00062 
00063       bool operator<(const double rhs) const { return mValue < doubleToInt(rhs*1000.0); }
00064       bool operator>(const double rhs) const { return mValue > doubleToInt(rhs*1000.0); }
00065       bool operator<=(const double rhs) const { return mValue <= doubleToInt(rhs*1000.0); }
00066       bool operator>=(const double rhs) const { return mValue >= doubleToInt(rhs*1000.0); }
00067       bool operator==(const double rhs) const { return mValue == doubleToInt(rhs*1000.0); }
00068       bool operator!=(const double rhs) const { return mValue != doubleToInt(rhs*1000.0); }
00069 
00070       QValue& operator=(const float rhs) { setValue(doubleToInt(rhs*1000.0)); return (*this); }                 
00071       QValue& operator=(const double rhs) { setValue(doubleToInt(rhs*1000.0)); return (*this); }                        
00072 #endif          
00073       void setValue(int val) { mValue = (val<0) || (val>1000) ? 1000 : val; }
00074       void setValue(const Data& data) { ParseBuffer pb(data); mValue = pb.qVal(); }
00075       int getValue() const { return mValue; }
00076       const Data& getData() const;
00077       EncodeStream& encode(EncodeStream& stream) const;
00078 
00079    private:
00080       int mValue;
00081       mutable Data mDataValue;
00082    };
00083 }
00084 
00085 #endif
00086 
00087 /* ====================================================================
00088  * The Vovida Software License, Version 1.0 
00089  * 
00090  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00091  * 
00092  * Redistribution and use in source and binary forms, with or without
00093  * modification, are permitted provided that the following conditions
00094  * are met:
00095  * 
00096  * 1. Redistributions of source code must retain the above copyright
00097  *    notice, this list of conditions and the following disclaimer.
00098  * 
00099  * 2. Redistributions in binary form must reproduce the above copyright
00100  *    notice, this list of conditions and the following disclaimer in
00101  *    the documentation and/or other materials provided with the
00102  *    distribution.
00103  * 
00104  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00105  *    and "Vovida Open Communication Application Library (VOCAL)" must
00106  *    not be used to endorse or promote products derived from this
00107  *    software without prior written permission. For written
00108  *    permission, please contact vocal@vovida.org.
00109  *
00110  * 4. Products derived from this software may not be called "VOCAL", nor
00111  *    may "VOCAL" appear in their name, without prior written
00112  *    permission of Vovida Networks, Inc.
00113  * 
00114  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00115  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00116  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00117  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00118  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00119  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00120  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00121  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00122  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00123  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00124  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00125  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00126  * DAMAGE.
00127  * 
00128  * ====================================================================
00129  * 
00130  * This software consists of voluntary contributions made by Vovida
00131  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00132  * Inc.  For more information on Vovida Networks, Inc., please see
00133  * <http://www.vovida.org/>.
00134  *
00135  */