Skip to content

Commit

Permalink
Move tagged_integer to ripple/basics
Browse files Browse the repository at this point in the history
  • Loading branch information
bachase authored and nbougalis committed Aug 10, 2017
1 parent d90a064 commit 9ae717c
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 410 deletions.
12 changes: 6 additions & 6 deletions Builds/VisualStudio2015/RippleD.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
12 changes: 6 additions & 6 deletions Builds/VisualStudio2015/RippleD.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down
223 changes: 223 additions & 0 deletions src/ripple/basics/tagged_integer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright 2014, Nikolaos D. Bougalis <[email protected]>
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

Loading

0 comments on commit 9ae717c

Please sign in to comment.