mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-04 11:15:56 +00:00
Move tagged_integer to ripple/basics
This commit is contained in:
@@ -1529,6 +1529,8 @@
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\TaggedCache.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\tagged_integer.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\ToString.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\UnorderedContainers.h">
|
||||
@@ -1827,8 +1829,6 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClInclude Include="..\..\src\ripple\beast\utility\tagged_integer.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\beast\utility\temp_dir.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\beast\utility\weak_fn.h">
|
||||
@@ -4403,6 +4403,10 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\test\basics\tagged_integer_test.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\test\beast\aged_associative_container_test.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
|
||||
@@ -4435,10 +4439,6 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\test\beast\beast_tagged_integer_test.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\test\beast\beast_weak_fn_test.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
|
||||
|
||||
@@ -2085,6 +2085,9 @@
|
||||
<ClInclude Include="..\..\src\ripple\basics\TaggedCache.h">
|
||||
<Filter>ripple\basics</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\tagged_integer.h">
|
||||
<Filter>ripple\basics</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\ToString.h">
|
||||
<Filter>ripple\basics</Filter>
|
||||
</ClInclude>
|
||||
@@ -2463,9 +2466,6 @@
|
||||
<ClCompile Include="..\..\src\ripple\beast\utility\src\beast_PropertyStream.cpp">
|
||||
<Filter>ripple\beast\utility\src</Filter>
|
||||
</ClCompile>
|
||||
<ClInclude Include="..\..\src\ripple\beast\utility\tagged_integer.h">
|
||||
<Filter>ripple\beast\utility</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\beast\utility\temp_dir.h">
|
||||
<Filter>ripple\beast\utility</Filter>
|
||||
</ClInclude>
|
||||
@@ -5184,6 +5184,9 @@
|
||||
<ClCompile Include="..\..\src\test\basics\TaggedCache_test.cpp">
|
||||
<Filter>test\basics</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\test\basics\tagged_integer_test.cpp">
|
||||
<Filter>test\basics</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\test\beast\aged_associative_container_test.cpp">
|
||||
<Filter>test\beast</Filter>
|
||||
</ClCompile>
|
||||
@@ -5208,9 +5211,6 @@
|
||||
<ClCompile Include="..\..\src\test\beast\beast_PropertyStream_test.cpp">
|
||||
<Filter>test\beast</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\test\beast\beast_tagged_integer_test.cpp">
|
||||
<Filter>test\beast</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\test\beast\beast_weak_fn_test.cpp">
|
||||
<Filter>test\beast</Filter>
|
||||
</ClCompile>
|
||||
|
||||
223
src/ripple/basics/tagged_integer.h
Normal file
223
src/ripple/basics/tagged_integer.h
Normal file
@@ -0,0 +1,223 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef BEAST_UTILITY_TAGGED_INTEGER_H_INCLUDED
|
||||
#define BEAST_UTILITY_TAGGED_INTEGER_H_INCLUDED
|
||||
|
||||
#include <ripple/beast/hash/hash_append.h>
|
||||
#include <boost/operators.hpp>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/** A type-safe wrap around standard integral types
|
||||
|
||||
The tag is used to implement type safety, catching mismatched types at
|
||||
compile time. Multiple instantiations wrapping the same underlying integral
|
||||
type are distinct types (distinguished by tag) and will not interoperate. A
|
||||
tagged_integer supports all the usual assignment, arithmetic, comparison and
|
||||
shifting operations defined for the underlying type
|
||||
|
||||
The tag is not meant as a unit, which would require restricting the set of
|
||||
allowed arithmetic operations.
|
||||
*/
|
||||
template <class Int, class Tag>
|
||||
class tagged_integer : boost::operators<tagged_integer<Int, Tag>>
|
||||
, boost::shiftable<tagged_integer<Int, Tag>>
|
||||
{
|
||||
private:
|
||||
Int m_value;
|
||||
|
||||
public:
|
||||
using value_type = Int;
|
||||
using tag_type = Tag;
|
||||
|
||||
tagged_integer() = default;
|
||||
|
||||
template <
|
||||
class OtherInt,
|
||||
class = typename std::enable_if<
|
||||
std::is_integral<OtherInt>::value &&
|
||||
sizeof(OtherInt) <= sizeof(Int)>::type>
|
||||
explicit
|
||||
/* constexpr */
|
||||
tagged_integer(OtherInt value) noexcept
|
||||
: m_value(value)
|
||||
{
|
||||
}
|
||||
|
||||
bool
|
||||
operator<(const tagged_integer & rhs) const noexcept
|
||||
{
|
||||
return m_value < rhs.m_value;
|
||||
}
|
||||
|
||||
bool
|
||||
operator==(const tagged_integer & rhs) const noexcept
|
||||
{
|
||||
return m_value == rhs.m_value;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator+=(tagged_integer const& rhs) noexcept
|
||||
{
|
||||
m_value += rhs.m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator-=(tagged_integer const& rhs) noexcept
|
||||
{
|
||||
m_value -= rhs.m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator*=(tagged_integer const& rhs) noexcept
|
||||
{
|
||||
m_value *= rhs.m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator/=(tagged_integer const& rhs) noexcept
|
||||
{
|
||||
m_value /= rhs.m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator%=(tagged_integer const& rhs) noexcept
|
||||
{
|
||||
m_value %= rhs.m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator|=(tagged_integer const& rhs) noexcept
|
||||
{
|
||||
m_value |= rhs.m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator&=(tagged_integer const& rhs) noexcept
|
||||
{
|
||||
m_value &= rhs.m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator^=(tagged_integer const& rhs) noexcept
|
||||
{
|
||||
m_value ^= rhs.m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator<<=(const tagged_integer& rhs) noexcept
|
||||
{
|
||||
m_value <<= rhs.m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator>>=(const tagged_integer& rhs) noexcept
|
||||
{
|
||||
m_value >>= rhs.m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer
|
||||
operator~() const noexcept
|
||||
{
|
||||
return tagged_integer{~m_value};
|
||||
}
|
||||
|
||||
tagged_integer
|
||||
operator+() const noexcept
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer
|
||||
operator-() const noexcept
|
||||
{
|
||||
return tagged_integer{-m_value};
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator++ () noexcept
|
||||
{
|
||||
++m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator-- () noexcept
|
||||
{
|
||||
--m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
explicit
|
||||
operator Int() const noexcept
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
friend
|
||||
std::ostream&
|
||||
operator<< (std::ostream& s, tagged_integer const& t)
|
||||
{
|
||||
s << t.m_value;
|
||||
return s;
|
||||
}
|
||||
|
||||
friend
|
||||
std::istream&
|
||||
operator>> (std::istream& s, tagged_integer& t)
|
||||
{
|
||||
s >> t.m_value;
|
||||
return s;
|
||||
}
|
||||
|
||||
friend
|
||||
std::string
|
||||
to_string(tagged_integer const& t)
|
||||
{
|
||||
return std::to_string(t.m_value);
|
||||
}
|
||||
};
|
||||
|
||||
} // ripple
|
||||
|
||||
namespace beast {
|
||||
template <class Int, class Tag, class HashAlgorithm>
|
||||
struct is_contiguously_hashable<ripple::tagged_integer<Int, Tag>, HashAlgorithm>
|
||||
: public is_contiguously_hashable<Int, HashAlgorithm>
|
||||
{
|
||||
};
|
||||
|
||||
} // beast
|
||||
#endif
|
||||
|
||||
@@ -1,243 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef BEAST_UTILITY_TAGGED_INTEGER_H_INCLUDED
|
||||
#define BEAST_UTILITY_TAGGED_INTEGER_H_INCLUDED
|
||||
|
||||
#include <ripple/beast/hash/hash_append.h>
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** A type-safe wrap around standard unsigned integral types
|
||||
|
||||
The tag is used to implement type safety, catching mismatched types at
|
||||
compile time. Multiple instantiations wrapping the same underlying integral
|
||||
type are distinct types (distinguished by tag) and will not interoperate.
|
||||
|
||||
A tagged_integer supports all the comparison operators that are available
|
||||
for the underlying integral type. It only supports a subset of arithmetic
|
||||
operators, restricting mutation to only safe and meaningful types.
|
||||
*/
|
||||
template <class Int, class Tag>
|
||||
class tagged_integer
|
||||
{
|
||||
private:
|
||||
static_assert (std::is_unsigned <Int>::value,
|
||||
"The specified Int type must be unsigned");
|
||||
|
||||
Int m_value;
|
||||
|
||||
public:
|
||||
using value_type = Int;
|
||||
using tag_type = Tag;
|
||||
|
||||
tagged_integer() = default;
|
||||
|
||||
template <
|
||||
class OtherInt,
|
||||
class = typename std::enable_if <
|
||||
std::is_integral <OtherInt>::value &&
|
||||
sizeof (OtherInt) <= sizeof (Int)
|
||||
>::type
|
||||
>
|
||||
explicit
|
||||
/* constexpr */
|
||||
tagged_integer (OtherInt value) noexcept
|
||||
: m_value (value)
|
||||
{
|
||||
}
|
||||
|
||||
// Arithmetic operators
|
||||
tagged_integer&
|
||||
operator++ () noexcept
|
||||
{
|
||||
++m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer
|
||||
operator++ (int) noexcept
|
||||
{
|
||||
tagged_integer orig (*this);
|
||||
++(*this);
|
||||
return orig;
|
||||
}
|
||||
|
||||
tagged_integer&
|
||||
operator-- () noexcept
|
||||
{
|
||||
--m_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tagged_integer
|
||||
operator-- (int) noexcept
|
||||
{
|
||||
tagged_integer orig (*this);
|
||||
--(*this);
|
||||
return orig;
|
||||
}
|
||||
|
||||
template <class OtherInt>
|
||||
typename std::enable_if <
|
||||
std::is_integral <OtherInt>::value &&
|
||||
sizeof (OtherInt) <= sizeof (Int),
|
||||
tagged_integer <Int, Tag>>::type&
|
||||
operator+= (OtherInt rhs) noexcept
|
||||
{
|
||||
m_value += rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class OtherInt>
|
||||
typename std::enable_if <
|
||||
std::is_integral <OtherInt>::value &&
|
||||
sizeof (OtherInt) <= sizeof (Int),
|
||||
tagged_integer <Int, Tag>>::type&
|
||||
operator-= (OtherInt rhs) noexcept
|
||||
{
|
||||
m_value -= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class OtherInt>
|
||||
friend
|
||||
typename std::enable_if <
|
||||
std::is_integral <OtherInt>::value &&
|
||||
sizeof (OtherInt) <= sizeof (Int),
|
||||
tagged_integer <Int, Tag>>::type
|
||||
operator+ (tagged_integer const& lhs,
|
||||
OtherInt rhs) noexcept
|
||||
{
|
||||
return tagged_integer (lhs.m_value + rhs);
|
||||
}
|
||||
|
||||
template <class OtherInt>
|
||||
friend
|
||||
typename std::enable_if <
|
||||
std::is_integral <OtherInt>::value &&
|
||||
sizeof (OtherInt) <= sizeof (Int),
|
||||
tagged_integer <Int, Tag>>::type
|
||||
operator+ (OtherInt lhs,
|
||||
tagged_integer const& rhs) noexcept
|
||||
{
|
||||
return tagged_integer (lhs + rhs.m_value);
|
||||
}
|
||||
|
||||
template <class OtherInt>
|
||||
friend
|
||||
typename std::enable_if <
|
||||
std::is_integral <OtherInt>::value &&
|
||||
sizeof (OtherInt) <= sizeof (Int),
|
||||
tagged_integer <Int, Tag>>::type
|
||||
operator- (tagged_integer const& lhs,
|
||||
OtherInt rhs) noexcept
|
||||
{
|
||||
return tagged_integer (lhs.m_value - rhs);
|
||||
}
|
||||
|
||||
friend
|
||||
Int
|
||||
operator- (tagged_integer const& lhs,
|
||||
tagged_integer const& rhs) noexcept
|
||||
{
|
||||
return lhs.m_value - rhs.m_value;
|
||||
}
|
||||
|
||||
// Comparison operators
|
||||
friend
|
||||
bool
|
||||
operator== (tagged_integer const& lhs,
|
||||
tagged_integer const& rhs) noexcept
|
||||
{
|
||||
return lhs.m_value == rhs.m_value;
|
||||
}
|
||||
|
||||
friend
|
||||
bool
|
||||
operator!= (tagged_integer const& lhs,
|
||||
tagged_integer const& rhs) noexcept
|
||||
{
|
||||
return lhs.m_value != rhs.m_value;
|
||||
}
|
||||
|
||||
friend
|
||||
bool
|
||||
operator< (tagged_integer const& lhs,
|
||||
tagged_integer const& rhs) noexcept
|
||||
{
|
||||
return lhs.m_value < rhs.m_value;
|
||||
}
|
||||
|
||||
friend
|
||||
bool
|
||||
operator<= (tagged_integer const& lhs,
|
||||
tagged_integer const& rhs) noexcept
|
||||
{
|
||||
return lhs.m_value <= rhs.m_value;
|
||||
}
|
||||
|
||||
friend
|
||||
bool
|
||||
operator> (tagged_integer const& lhs,
|
||||
tagged_integer const& rhs) noexcept
|
||||
{
|
||||
return lhs.m_value > rhs.m_value;
|
||||
}
|
||||
|
||||
friend
|
||||
bool
|
||||
operator>= (tagged_integer const& lhs,
|
||||
tagged_integer const& rhs) noexcept
|
||||
{
|
||||
return lhs.m_value >= rhs.m_value;
|
||||
}
|
||||
|
||||
friend
|
||||
std::ostream&
|
||||
operator<< (std::ostream& s, tagged_integer const& t)
|
||||
{
|
||||
s << t.m_value;
|
||||
return s;
|
||||
}
|
||||
|
||||
friend
|
||||
std::istream&
|
||||
operator>> (std::istream& s, tagged_integer& t)
|
||||
{
|
||||
s >> t.m_value;
|
||||
return s;
|
||||
}
|
||||
};
|
||||
|
||||
template <class Int, class Tag, class HashAlgorithm>
|
||||
struct is_contiguously_hashable<tagged_integer<Int, Tag>, HashAlgorithm>
|
||||
: public is_contiguously_hashable<Int, HashAlgorithm>
|
||||
{
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#define RIPPLE_CORE_JOB_H_INCLUDED
|
||||
|
||||
#include <ripple/core/LoadMonitor.h>
|
||||
#include <functional>
|
||||
|
||||
#include <functional>
|
||||
|
||||
|
||||
239
src/test/basics/tagged_integer_test.cpp
Normal file
239
src/test/basics/tagged_integer_test.cpp
Normal file
@@ -0,0 +1,239 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
|
||||
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <BeastConfig.h>
|
||||
#include <ripple/basics/tagged_integer.h>
|
||||
#include <ripple/beast/unit_test.h>
|
||||
#include <type_traits>
|
||||
|
||||
namespace ripple {
|
||||
namespace test {
|
||||
|
||||
class tagged_integer_test
|
||||
: public beast::unit_test::suite
|
||||
{
|
||||
private:
|
||||
struct Tag1 { };
|
||||
struct Tag2 { };
|
||||
|
||||
// Static checks that types are not interoperable
|
||||
|
||||
using TagUInt1 = tagged_integer <std::uint32_t, Tag1>;
|
||||
using TagUInt2 = tagged_integer <std::uint32_t, Tag2>;
|
||||
using TagUInt3 = tagged_integer <std::uint64_t, Tag1>;
|
||||
|
||||
// Check construction of tagged_integers
|
||||
static_assert (std::is_constructible<TagUInt1, std::uint32_t>::value,
|
||||
"TagUInt1 should be constructible using a std::uint32_t");
|
||||
|
||||
static_assert (!std::is_constructible<TagUInt1, std::uint64_t>::value,
|
||||
"TagUInt1 should not be constructible using a std::uint64_t");
|
||||
|
||||
static_assert (std::is_constructible<TagUInt3, std::uint32_t>::value,
|
||||
"TagUInt3 should be constructible using a std::uint32_t");
|
||||
|
||||
static_assert (std::is_constructible<TagUInt3, std::uint64_t>::value,
|
||||
"TagUInt3 should be constructible using a std::uint64_t");
|
||||
|
||||
// Check assignment of tagged_integers
|
||||
static_assert (!std::is_assignable<TagUInt1, std::uint32_t>::value,
|
||||
"TagUInt1 should not be assignable with a std::uint32_t");
|
||||
|
||||
static_assert (!std::is_assignable<TagUInt1, std::uint64_t>::value,
|
||||
"TagUInt1 should not be assignable with a std::uint64_t");
|
||||
|
||||
static_assert (!std::is_assignable<TagUInt3, std::uint32_t>::value,
|
||||
"TagUInt3 should not be assignable with a std::uint32_t");
|
||||
|
||||
static_assert (!std::is_assignable<TagUInt3, std::uint64_t>::value,
|
||||
"TagUInt3 should not be assignable with a std::uint64_t");
|
||||
|
||||
static_assert (std::is_assignable<TagUInt1, TagUInt1>::value,
|
||||
"TagUInt1 should be assignable with a TagUInt1");
|
||||
|
||||
static_assert (!std::is_assignable<TagUInt1, TagUInt2>::value,
|
||||
"TagUInt1 should not be assignable with a TagUInt2");
|
||||
|
||||
static_assert (std::is_assignable<TagUInt3, TagUInt3>::value,
|
||||
"TagUInt3 should be assignable with a TagUInt1");
|
||||
|
||||
static_assert (!std::is_assignable<TagUInt1, TagUInt3>::value,
|
||||
"TagUInt1 should not be assignable with a TagUInt3");
|
||||
|
||||
static_assert (!std::is_assignable<TagUInt3, TagUInt1>::value,
|
||||
"TagUInt3 should not be assignable with a TagUInt1");
|
||||
|
||||
// Check convertibility of tagged_integers
|
||||
static_assert (!std::is_convertible<std::uint32_t, TagUInt1>::value,
|
||||
"std::uint32_t should not be convertible to a TagUInt1");
|
||||
|
||||
static_assert (!std::is_convertible<std::uint32_t, TagUInt3>::value,
|
||||
"std::uint32_t should not be convertible to a TagUInt3");
|
||||
|
||||
static_assert (!std::is_convertible<std::uint64_t, TagUInt3>::value,
|
||||
"std::uint64_t should not be convertible to a TagUInt3");
|
||||
|
||||
static_assert (!std::is_convertible<std::uint64_t, TagUInt2>::value,
|
||||
"std::uint64_t should not be convertible to a TagUInt2");
|
||||
|
||||
static_assert (!std::is_convertible<TagUInt1, TagUInt2>::value,
|
||||
"TagUInt1 should not be convertible to TagUInt2");
|
||||
|
||||
static_assert (!std::is_convertible<TagUInt1, TagUInt3>::value,
|
||||
"TagUInt1 should not be convertible to TagUInt3");
|
||||
|
||||
static_assert (!std::is_convertible<TagUInt2, TagUInt3>::value,
|
||||
"TagUInt2 should not be convertible to a TagUInt3");
|
||||
|
||||
|
||||
public:
|
||||
void run ()
|
||||
{
|
||||
using TagInt = tagged_integer<std::int32_t, Tag1>;
|
||||
|
||||
{
|
||||
testcase ("Comparison Operators");
|
||||
|
||||
TagInt const zero(0);
|
||||
TagInt const one(1);
|
||||
|
||||
BEAST_EXPECT(one == one);
|
||||
BEAST_EXPECT(!(one == zero));
|
||||
|
||||
BEAST_EXPECT(one != zero);
|
||||
BEAST_EXPECT(!(one != one));
|
||||
|
||||
BEAST_EXPECT(zero < one);
|
||||
BEAST_EXPECT(!(one < zero));
|
||||
|
||||
BEAST_EXPECT(one > zero);
|
||||
BEAST_EXPECT(!(zero > one));
|
||||
|
||||
BEAST_EXPECT(one >= one);
|
||||
BEAST_EXPECT(one >= zero);
|
||||
BEAST_EXPECT(!(zero >= one));
|
||||
|
||||
BEAST_EXPECT(zero <= one);
|
||||
BEAST_EXPECT(zero <= zero);
|
||||
BEAST_EXPECT(!(one <= zero));
|
||||
}
|
||||
|
||||
{
|
||||
testcase ("Increment/Decrement Operators");
|
||||
TagInt const zero(0);
|
||||
TagInt const one(1);
|
||||
TagInt a{0};
|
||||
++a;
|
||||
BEAST_EXPECT(a == one);
|
||||
--a;
|
||||
BEAST_EXPECT(a == zero);
|
||||
a++;
|
||||
BEAST_EXPECT(a == one);
|
||||
a--;
|
||||
BEAST_EXPECT(a == zero);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
testcase ("Arithmetic Operators");
|
||||
TagInt a{-2};
|
||||
BEAST_EXPECT(+a == TagInt{-2});
|
||||
BEAST_EXPECT(-a == TagInt{2});
|
||||
BEAST_EXPECT(TagInt{-3} + TagInt{4} == TagInt{1});
|
||||
BEAST_EXPECT(TagInt{-3} - TagInt{4} == TagInt{-7});
|
||||
BEAST_EXPECT(TagInt{-3} * TagInt{4} == TagInt{-12});
|
||||
BEAST_EXPECT(TagInt{8}/TagInt{4} == TagInt{2});
|
||||
BEAST_EXPECT(TagInt{7} %TagInt{4} == TagInt{3});
|
||||
|
||||
BEAST_EXPECT(~TagInt{8} == TagInt{~TagInt::value_type{8}});
|
||||
BEAST_EXPECT((TagInt{6} & TagInt{3}) == TagInt{2});
|
||||
BEAST_EXPECT((TagInt{6} | TagInt{3}) == TagInt{7});
|
||||
BEAST_EXPECT((TagInt{6} ^ TagInt{3}) == TagInt{5});
|
||||
|
||||
BEAST_EXPECT((TagInt{4} << TagInt{2}) == TagInt{16});
|
||||
BEAST_EXPECT((TagInt{16} >> TagInt{2}) == TagInt{4});
|
||||
}
|
||||
{
|
||||
testcase ("Assignment Operators");
|
||||
TagInt a{-2};
|
||||
TagInt b{0};
|
||||
b = a;
|
||||
BEAST_EXPECT(b == TagInt{-2});
|
||||
|
||||
// -3 + 4 == 1
|
||||
a = TagInt{-3};
|
||||
a += TagInt{4};
|
||||
BEAST_EXPECT(a == TagInt{1});
|
||||
|
||||
// -3 - 4 == -7
|
||||
a = TagInt{-3};
|
||||
a -= TagInt{4};
|
||||
BEAST_EXPECT(a == TagInt{-7});
|
||||
|
||||
// -3 * 4 == -12
|
||||
a = TagInt{-3};
|
||||
a *= TagInt{4};
|
||||
BEAST_EXPECT(a == TagInt{-12});
|
||||
|
||||
// 8/4 == 2
|
||||
a = TagInt{8};
|
||||
a /= TagInt{4};
|
||||
BEAST_EXPECT(a == TagInt{2});
|
||||
|
||||
// 7 % 4 == 3
|
||||
a = TagInt{7};
|
||||
a %= TagInt{4};
|
||||
BEAST_EXPECT(a == TagInt{3});
|
||||
|
||||
// 6 & 3 == 2
|
||||
a = TagInt{6};
|
||||
a /= TagInt{3};
|
||||
BEAST_EXPECT(a == TagInt{2});
|
||||
|
||||
// 6 | 3 == 7
|
||||
a = TagInt{6};
|
||||
a |= TagInt{3};
|
||||
BEAST_EXPECT(a == TagInt{7});
|
||||
|
||||
// 6 ^ 3 == 5
|
||||
a = TagInt{6};
|
||||
a ^= TagInt{3};
|
||||
BEAST_EXPECT(a == TagInt{5});
|
||||
|
||||
// 4 << 2 == 16
|
||||
a = TagInt{4};
|
||||
a <<= TagInt{2};
|
||||
BEAST_EXPECT(a == TagInt{16});
|
||||
|
||||
// 16 >> 2 == 4
|
||||
a = TagInt{16};
|
||||
a >>= TagInt{2};
|
||||
BEAST_EXPECT(a == TagInt{4});
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(tagged_integer,ripple_basics,ripple);
|
||||
|
||||
} // test
|
||||
} // ripple
|
||||
@@ -1,154 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#if BEAST_INCLUDE_BEASTCONFIG
|
||||
#include <BeastConfig.h>
|
||||
#endif
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <ripple/beast/utility/tagged_integer.h>
|
||||
#include <ripple/beast/unit_test.h>
|
||||
|
||||
namespace beast {
|
||||
|
||||
class tagged_integer_test
|
||||
: public unit_test::suite
|
||||
{
|
||||
private:
|
||||
struct Tag1 { };
|
||||
struct Tag2 { };
|
||||
|
||||
using TagInt1 = tagged_integer <std::uint32_t, Tag1>;
|
||||
using TagInt2 = tagged_integer <std::uint32_t, Tag2>;
|
||||
using TagInt3 = tagged_integer <std::uint64_t, Tag1>;
|
||||
|
||||
// Check construction of tagged_integers
|
||||
static_assert (std::is_constructible<TagInt1, std::uint32_t>::value,
|
||||
"TagInt1 should be constructible using a std::uint32_t");
|
||||
|
||||
static_assert (!std::is_constructible<TagInt1, std::uint64_t>::value,
|
||||
"TagInt1 should not be constructible using a std::uint64_t");
|
||||
|
||||
static_assert (std::is_constructible<TagInt3, std::uint32_t>::value,
|
||||
"TagInt3 should be constructible using a std::uint32_t");
|
||||
|
||||
static_assert (std::is_constructible<TagInt3, std::uint64_t>::value,
|
||||
"TagInt3 should be constructible using a std::uint64_t");
|
||||
|
||||
// Check assignment of tagged_integers
|
||||
static_assert (!std::is_assignable<TagInt1, std::uint32_t>::value,
|
||||
"TagInt1 should not be assignable with a std::uint32_t");
|
||||
|
||||
static_assert (!std::is_assignable<TagInt1, std::uint64_t>::value,
|
||||
"TagInt1 should not be assignable with a std::uint64_t");
|
||||
|
||||
static_assert (!std::is_assignable<TagInt3, std::uint32_t>::value,
|
||||
"TagInt3 should not be assignable with a std::uint32_t");
|
||||
|
||||
static_assert (!std::is_assignable<TagInt3, std::uint64_t>::value,
|
||||
"TagInt3 should not be assignable with a std::uint64_t");
|
||||
|
||||
static_assert (std::is_assignable<TagInt1, TagInt1>::value,
|
||||
"TagInt1 should be assignable with a TagInt1");
|
||||
|
||||
static_assert (!std::is_assignable<TagInt1, TagInt2>::value,
|
||||
"TagInt1 should not be assignable with a TagInt2");
|
||||
|
||||
static_assert (std::is_assignable<TagInt3, TagInt3>::value,
|
||||
"TagInt3 should be assignable with a TagInt1");
|
||||
|
||||
static_assert (!std::is_assignable<TagInt1, TagInt3>::value,
|
||||
"TagInt1 should not be assignable with a TagInt3");
|
||||
|
||||
static_assert (!std::is_assignable<TagInt3, TagInt1>::value,
|
||||
"TagInt3 should not be assignable with a TagInt1");
|
||||
|
||||
// Check convertibility of tagged_integers
|
||||
static_assert (!std::is_convertible<std::uint32_t, TagInt1>::value,
|
||||
"std::uint32_t should not be convertible to a TagInt1");
|
||||
|
||||
static_assert (!std::is_convertible<std::uint32_t, TagInt3>::value,
|
||||
"std::uint32_t should not be convertible to a TagInt3");
|
||||
|
||||
static_assert (!std::is_convertible<std::uint64_t, TagInt3>::value,
|
||||
"std::uint64_t should not be convertible to a TagInt3");
|
||||
|
||||
static_assert (!std::is_convertible<std::uint64_t, TagInt2>::value,
|
||||
"std::uint64_t should not be convertible to a TagInt2");
|
||||
|
||||
static_assert (!std::is_convertible<TagInt1, TagInt2>::value,
|
||||
"TagInt1 should not be convertible to TagInt2");
|
||||
|
||||
static_assert (!std::is_convertible<TagInt1, TagInt3>::value,
|
||||
"TagInt1 should not be convertible to TagInt3");
|
||||
|
||||
static_assert (!std::is_convertible<TagInt2, TagInt3>::value,
|
||||
"TagInt2 should not be convertible to a TagInt3");
|
||||
|
||||
public:
|
||||
void run ()
|
||||
{
|
||||
TagInt1 const zero (0);
|
||||
TagInt1 const one (1);
|
||||
|
||||
testcase ("Comparison Operators");
|
||||
|
||||
expect (zero >= zero, "Should be greater than or equal");
|
||||
expect (zero == zero, "Should be equal");
|
||||
|
||||
expect (one > zero, "Should be greater");
|
||||
expect (one >= zero, "Should be greater than or equal");
|
||||
expect (one != zero, "Should not be equal");
|
||||
|
||||
unexpected (one < zero, "Should be greater");
|
||||
unexpected (one <= zero, "Should not be greater than or equal");
|
||||
unexpected (one == zero, "Should not be equal");
|
||||
|
||||
testcase ("Arithmetic Operators");
|
||||
|
||||
TagInt1 tmp;
|
||||
|
||||
tmp = zero + 0u;
|
||||
expect (tmp == zero, "Should be equal");
|
||||
|
||||
tmp = 1u + zero;
|
||||
expect (tmp == one, "Should be equal");
|
||||
|
||||
expect(--tmp == zero, "Should be equal");
|
||||
expect(tmp++ == zero, "Should be equal");
|
||||
expect(tmp == one, "Should be equal");
|
||||
|
||||
expect(tmp-- == one, "Should be equal");
|
||||
expect(tmp == zero, "Should be equal");
|
||||
expect(++tmp == one, "Should be equal");
|
||||
|
||||
tmp = zero;
|
||||
|
||||
tmp += 1u;
|
||||
expect(tmp == one, "Should be equal");
|
||||
|
||||
tmp -= 1u;
|
||||
expect(tmp == zero, "Should be equal");
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(tagged_integer,utility,beast);
|
||||
|
||||
} // beast
|
||||
@@ -29,3 +29,4 @@
|
||||
#include <test/basics/Slice_test.cpp>
|
||||
#include <test/basics/StringUtilities_test.cpp>
|
||||
#include <test/basics/TaggedCache_test.cpp>
|
||||
#include <test/basics/tagged_integer_test.cpp>
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <test/beast/beast_tagged_integer_test.cpp>
|
||||
#include <test/beast/beast_weak_fn_test.cpp>
|
||||
#include <test/beast/beast_Zero_test.cpp>
|
||||
#include <test/beast/define_print.cpp>
|
||||
|
||||
Reference in New Issue
Block a user