From: g0dil Date: Tue, 31 Aug 2010 15:23:43 +0000 (+0000) Subject: Utils: Implement RestrictedInt template X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=8e19d3954af99dc92be506fdcffe14af5313cfa1;p=senf.git Utils: Implement RestrictedInt template git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1705 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Utils/RestrictedInt.hh b/senf/Utils/RestrictedInt.hh new file mode 100644 index 0000000..0beb4d3 --- /dev/null +++ b/senf/Utils/RestrictedInt.hh @@ -0,0 +1,128 @@ +// $Id$ +// +// Copyright (C) 2010 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief RestrictedInt public header */ + +#ifndef HH_SENF_senf_Utils_RestrictedInt_ +#define HH_SENF_senf_Utils_RestrictedInt_ 1 + +// Custom includes +#include +#include +#include + +//#include "RestrictedInt.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace senf { + + // Really ... the correct spelling is 'euclidean' as in the current boost + // Versions but older versions only have this spelling so we can' t use + // the correct class name ... + + template + class RestrictedInt + : public boost::ordered_euclidian_ring_operators< RestrictedInt, + boost::unit_steppable< RestrictedInt, + boost::shiftable< RestrictedInt, + boost::bitwise1< RestrictedInt, + senf::comparable_safe_bool< RestrictedInt > > > > > + { + public: + explicit RestrictedInt(Base value) : value_ (value) {} + RestrictedInt() : value_ () {} + +# define BinaryOp(op) \ + RestrictedInt & operator op ## = (RestrictedInt other) \ + { value_ op ## = other.value_; return *this; } +# define IncDecOp(op) \ + RestrictedInt & operator op () \ + { op value_; return *this; } +# define PrefixOp(op) \ + RestrictedInt const operator op () const \ + { return RestrictedInt(op value_); } +# define CompareOp(op) \ + bool operator op (RestrictedInt other) const \ + { return value_ op other.value_; } + + BinaryOp(+) + BinaryOp(-) + BinaryOp(*) + BinaryOp(/) + BinaryOp(%) + BinaryOp(>>) + BinaryOp(<<) + BinaryOp(&) + BinaryOp(|) + BinaryOp(^) + + IncDecOp(++) + IncDecOp(--) + + PrefixOp(~) + PrefixOp(-) + + CompareOp(<) + CompareOp(==) + +# undef CompareOp +# undef PrefixOp +# undef PostfixOp +# undef BinaryOp + + Base value() const + { return value_; } + + bool boolean_test() const + { return value_; } + + private: + Base value_; + }; + + template + std::ostream & operator<<(std::ostream & os, RestrictedInt value) + { os << value.value(); return os; } + + template + std::istream & operator>>(std::istream & is, RestrictedInt & value) + { Base v; is >> v; value = RestrictedInt(v); return is; } + +} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "RestrictedInt.cci" +//#include "RestrictedInt.ct" +//#include "RestrictedInt.cti" +#endif + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/senf/Utils/RestrictedInt.test.cc b/senf/Utils/RestrictedInt.test.cc new file mode 100644 index 0000000..c2348b7 --- /dev/null +++ b/senf/Utils/RestrictedInt.test.cc @@ -0,0 +1,112 @@ +// $Id$ +// +// Copyright (C) 2010 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief RestrictedInt.test unit tests */ + +//#include "RestrictedInt.test.hh" +//#include "RestrictedInt.test.ih" + +// Custom includes +#include "RestrictedInt.hh" + +#include +#include + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +namespace { + struct TestIntTag {}; + typedef senf::RestrictedInt TestInt; +} + +SENF_AUTO_UNIT_TEST(restrictedInt) +{ + TestInt i1; + TestInt i2(10); + + BOOST_CHECK_EQUAL( i1, TestInt(0) ); + BOOST_CHECK_EQUAL( i2, TestInt(10) ); + + BOOST_CHECK_EQUAL( i1+i2, TestInt(10) ); + + i1 = TestInt(6); + + BOOST_CHECK_EQUAL( i2-i1, TestInt(4) ); + + i2 = TestInt(0); + + BOOST_CHECK_EQUAL( -i1, TestInt(-6) ); + BOOST_CHECK( static_cast(i1) ); + BOOST_CHECK( static_cast(!i2) ); + + BOOST_CHECK_EQUAL( i1 ++, TestInt(6) ); + BOOST_CHECK_EQUAL( ++ i1, TestInt(8) ); + + BOOST_CHECK_EQUAL( i1 * TestInt(2), TestInt(16) ); + BOOST_CHECK_EQUAL( i1, TestInt(8) ); + BOOST_CHECK_EQUAL( i1 / TestInt(2), TestInt(4) ); + BOOST_CHECK_EQUAL( i1, TestInt(8) ); + BOOST_CHECK_EQUAL( i1 >> TestInt(1), TestInt(4) ); + BOOST_CHECK_EQUAL( i1, TestInt(8) ); + BOOST_CHECK_EQUAL( i1 << TestInt(1), TestInt(16) ); + BOOST_CHECK_EQUAL( i1, TestInt(8) ); + BOOST_CHECK_EQUAL( i1 & TestInt(2), TestInt(0) ); + BOOST_CHECK_EQUAL( i1, TestInt(8) ); + BOOST_CHECK_EQUAL( i1 | TestInt(2), TestInt(10) ); + BOOST_CHECK_EQUAL( i1, TestInt(8) ); + BOOST_CHECK_EQUAL( i1 ^ TestInt(2), TestInt(10) ); + BOOST_CHECK_EQUAL( i1, TestInt(8) ); + + BOOST_CHECK_EQUAL( ~~i1, i1 ); + SENF_CHECK_NOT_EQUAL( ~i1, i1 ); + + BOOST_CHECK( i1 > i2 ); + BOOST_CHECK( i2 < i1 ); + BOOST_CHECK( i1 >= i2 ); + BOOST_CHECK( i2 <= i1 ); + BOOST_CHECK( i1 == TestInt(8) ); + BOOST_CHECK( i1 != i2 ); + + BOOST_CHECK_EQUAL( i1.value(), 8 ); + + std::stringstream ss; + ss << i1; + BOOST_CHECK_EQUAL( ss.str(), "8" ); + ss >> i2; + BOOST_CHECK_EQUAL( i2, TestInt(8) ); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: