Tidy up UnsignedInteger

This commit is contained in:
Vinnie Falco
2013-09-12 22:37:40 -07:00
parent ca695fa6e1
commit 962a95d770
6 changed files with 73 additions and 71 deletions

View File

@@ -323,7 +323,7 @@
<ClInclude Include="..\..\modules\beast_core\zip\zlib\zlib.h" /> <ClInclude Include="..\..\modules\beast_core\zip\zlib\zlib.h" />
<ClInclude Include="..\..\modules\beast_core\zip\zlib\zutil.h" /> <ClInclude Include="..\..\modules\beast_core\zip\zlib\zutil.h" />
<ClInclude Include="..\..\modules\beast_crypto\beast_crypto.h" /> <ClInclude Include="..\..\modules\beast_crypto\beast_crypto.h" />
<ClInclude Include="..\..\modules\beast_crypto\math\beast_UnsignedInteger.h" /> <ClInclude Include="..\..\modules\beast_crypto\math\UnsignedInteger.h" />
<ClInclude Include="..\..\modules\beast_db\beast_db.h" /> <ClInclude Include="..\..\modules\beast_db\beast_db.h" />
<ClInclude Include="..\..\modules\beast_db\keyvalue\beast_KeyvaDB.h" /> <ClInclude Include="..\..\modules\beast_db\keyvalue\beast_KeyvaDB.h" />
<ClInclude Include="..\..\modules\beast_extras\beast_extras.h" /> <ClInclude Include="..\..\modules\beast_extras\beast_extras.h" />
@@ -1265,7 +1265,7 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\modules\beast_crypto\beast_crypto.cpp" /> <ClCompile Include="..\..\modules\beast_crypto\beast_crypto.cpp" />
<ClCompile Include="..\..\modules\beast_crypto\math\beast_UnsignedInteger.cpp"> <ClCompile Include="..\..\modules\beast_crypto\math\UnsignedInteger.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>

View File

@@ -560,9 +560,6 @@
<ClInclude Include="..\..\modules\beast_core\diagnostic\beast_UnitTest.h"> <ClInclude Include="..\..\modules\beast_core\diagnostic\beast_UnitTest.h">
<Filter>beast_core\diagnostic</Filter> <Filter>beast_core\diagnostic</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\modules\beast_crypto\math\beast_UnsignedInteger.h">
<Filter>beast_crypto\math</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_crypto\beast_crypto.h"> <ClInclude Include="..\..\modules\beast_crypto\beast_crypto.h">
<Filter>beast_crypto</Filter> <Filter>beast_crypto</Filter>
</ClInclude> </ClInclude>
@@ -1028,6 +1025,9 @@
<ClInclude Include="..\..\modules\beast_core\diagnostic\Journal.h"> <ClInclude Include="..\..\modules\beast_core\diagnostic\Journal.h">
<Filter>beast_core\diagnostic</Filter> <Filter>beast_core\diagnostic</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\modules\beast_crypto\math\UnsignedInteger.h">
<Filter>beast_crypto\math</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\modules\beast_core\containers\beast_AbstractFifo.cpp"> <ClCompile Include="..\..\modules\beast_core\containers\beast_AbstractFifo.cpp">
@@ -1306,9 +1306,6 @@
<ClCompile Include="..\..\modules\beast_core\diagnostic\beast_UnitTest.cpp"> <ClCompile Include="..\..\modules\beast_core\diagnostic\beast_UnitTest.cpp">
<Filter>beast_core\diagnostic</Filter> <Filter>beast_core\diagnostic</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\modules\beast_crypto\math\beast_UnsignedInteger.cpp">
<Filter>beast_crypto\math</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\files\beast_RandomAccessFile.cpp"> <ClCompile Include="..\..\modules\beast_core\files\beast_RandomAccessFile.cpp">
<Filter>beast_core\files</Filter> <Filter>beast_core\files</Filter>
</ClCompile> </ClCompile>
@@ -1552,6 +1549,9 @@
<ClCompile Include="..\..\modules\beast_core\diagnostic\Journal.cpp"> <ClCompile Include="..\..\modules\beast_core\diagnostic\Journal.cpp">
<Filter>beast_core\diagnostic</Filter> <Filter>beast_core\diagnostic</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\modules\beast_crypto\math\UnsignedInteger.cpp">
<Filter>beast_crypto\math</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Text Include="..\..\TODO.txt" /> <Text Include="..\..\TODO.txt" />

View File

@@ -24,6 +24,6 @@
namespace beast namespace beast
{ {
#include "math/beast_UnsignedInteger.cpp" #include "math/UnsignedInteger.cpp"
} }

View File

@@ -44,7 +44,7 @@
namespace beast namespace beast
{ {
#include "math/beast_UnsignedInteger.h" #include "math/UnsignedInteger.h"
} }

View File

@@ -79,6 +79,8 @@ public:
HashValue m_seed; HashValue m_seed;
}; };
//--------------------------------------------------------------------------
/** Construct the object. /** Construct the object.
The values are uninitialized. The values are uninitialized.
*/ */
@@ -92,19 +94,35 @@ public:
this->operator= (other); this->operator= (other);
} }
/** Construct from raw memory. /** Construct from a raw memory.
The area pointed to by buffer must be at least Bytes in size, The area pointed to by buffer must be at least Bytes in size,
or else undefined behavior will result. or else undefined behavior will result.
*/ */
explicit UnsignedInteger (void const* buffer) /** @{ */
explicit UnsignedInteger (void const* buf)
{ {
memcpy (m_byte, buffer, Bytes); std::memcpy (begin(), buf, Bytes);
}
template <typename T>
explicit UnsignedInteger (T const* buf)
{
std::memcpy (begin(), buf, Bytes);
}
/** @} */
/** Construct from a sequence. */
template <class ForwardIterator>
UnsignedInteger (ForwardIterator start, ForwardIterator finish)
{
bassert (std::distance (start, finish) == Bytes);
std::copy (start, finish, begin());
} }
/** Assign from another value. */ /** Assign from another value. */
UnsignedInteger <Bytes>& operator= (UnsignedInteger const& other) noexcept UnsignedInteger <Bytes>& operator= (UnsignedInteger const& other) noexcept
{ {
memcpy (m_byte, other.m_byte, Bytes); std::memcpy (begin(), other.begin(), Bytes);
return *this; return *this;
} }
@@ -119,7 +137,7 @@ public:
UnsignedInteger <Bytes> result; UnsignedInteger <Bytes> result;
value = toNetworkByteOrder <IntegerType> (value); value = toNetworkByteOrder <IntegerType> (value);
result.clear (); result.clear ();
memcpy (result.end () - sizeof (value), &value, bmin (Bytes, sizeof (value))); std::memcpy (result.end () - sizeof (value), &value, bmin (Bytes, sizeof (value)));
return result; return result;
} }
@@ -132,22 +150,19 @@ public:
return result; return result;
} }
/** Fill with a particular byte value. /** Fill with a particular byte value. */
*/
void fill (value_type value) noexcept void fill (value_type value) noexcept
{ {
memset (m_byte, value, Bytes); std::fill (begin(), end(), value);
} }
/** Clear the contents to zero. /** Clear the contents to zero. */
*/
void clear () noexcept void clear () noexcept
{ {
fill (0); fill (0);
} }
/** Determine if all bits are zero. /** Determine if all bits are zero. */
*/
bool isZero () const noexcept bool isZero () const noexcept
{ {
for (int i = 0; i < Bytes; ++i) for (int i = 0; i < Bytes; ++i)
@@ -159,17 +174,14 @@ public:
return true; return true;
} }
/** Determine if any bit is non-zero. /** Determine if any bit is non-zero. */
*/
bool isNotZero () const noexcept bool isNotZero () const noexcept
{ {
return ! isZero (); return ! isZero ();
} }
/** Support conversion to `bool`. /** Support conversion to `bool`.
@return `true` if any bit is non-zero. @return `true` if any bit is non-zero.
@see SafeBool @see SafeBool
*/ */
bool asBoolean () const noexcept bool asBoolean () const noexcept
@@ -177,8 +189,7 @@ public:
return isNotZero (); return isNotZero ();
} }
/** Access a particular byte. /** Access a particular byte. */
*/
value_type& getByte (int byteIndex) noexcept value_type& getByte (int byteIndex) noexcept
{ {
bassert (byteIndex >= 0 && byteIndex < Bytes); bassert (byteIndex >= 0 && byteIndex < Bytes);
@@ -186,8 +197,7 @@ public:
return m_byte [byteIndex]; return m_byte [byteIndex];
} }
/** Access a particular byte as `const`. /** Access a particular byte as `const`. */
*/
value_type getByte (int byteIndex) const noexcept value_type getByte (int byteIndex) const noexcept
{ {
bassert (byteIndex >= 0 && byteIndex < Bytes); bassert (byteIndex >= 0 && byteIndex < Bytes);
@@ -195,99 +205,97 @@ public:
return m_byte [byteIndex]; return m_byte [byteIndex];
} }
/** Access a particular byte. /** Access a particular byte. */
*/
value_type& operator[] (int byteIndex) noexcept value_type& operator[] (int byteIndex) noexcept
{ {
return getByte (byteIndex); return getByte (byteIndex);
} }
/** Access a particular byte as `const`. /** Access a particular byte as `const`. */
*/
value_type const operator[] (int byteIndex) const noexcept value_type const operator[] (int byteIndex) const noexcept
{ {
return getByte (byteIndex); return getByte (byteIndex);
} }
/** Get an iterator to the beginning. /** Get an iterator to the beginning. */
*/
iterator begin () noexcept iterator begin () noexcept
{ {
return &m_byte [0]; return &m_byte [0];
} }
/** Get an iterator to the end. /** Get an iterator to past-the-end. */
*/ iterator end () noexcept
iterator end ()
{ {
return &m_byte [Bytes]; return &m_byte [Bytes];
} }
/** Get a const iterator to the beginning. /** Get a const iterator to the beginning. */
*/ const_iterator begin () const noexcept
{
return &m_byte [0];
}
/** Get a const iterator to past-the-end. */
const_iterator end () const noexcept
{
return &m_byte [Bytes];
}
/** Get a const iterator to the beginning. */
const_iterator cbegin () const noexcept const_iterator cbegin () const noexcept
{ {
return &m_byte [0]; return &m_byte [0];
} }
/** Get a const iterator to the end. /** Get a const iterator to past-the-end. */
*/
const_iterator cend () const noexcept const_iterator cend () const noexcept
{ {
return &m_byte [Bytes]; return &m_byte [Bytes];
} }
/** Compare two objects. /** Compare two objects. */
*/
int compare (UnsignedInteger <Bytes> const& other) const noexcept int compare (UnsignedInteger <Bytes> const& other) const noexcept
{ {
return memcmp (cbegin (), other.cbegin (), Bytes); return memcmp (cbegin (), other.cbegin (), Bytes);
} }
/** Determine equality. /** Determine equality. */
*/
bool operator== (UnsignedInteger <Bytes> const& other) const noexcept bool operator== (UnsignedInteger <Bytes> const& other) const noexcept
{ {
return compare (other) == 0; return compare (other) == 0;
} }
/** Determine inequality. /** Determine inequality. */
*/
bool operator!= (UnsignedInteger <Bytes> const& other) const noexcept bool operator!= (UnsignedInteger <Bytes> const& other) const noexcept
{ {
return compare (other) != 0; return compare (other) != 0;
} }
/** Ordered comparison. /** Ordered comparison. */
*/
bool operator< (UnsignedInteger <Bytes> const& other) const noexcept bool operator< (UnsignedInteger <Bytes> const& other) const noexcept
{ {
return compare (other) < 0; return compare (other) < 0;
} }
/** Ordered comparison. /** Ordered comparison. */
*/
bool operator<= (UnsignedInteger <Bytes> const& other) const noexcept bool operator<= (UnsignedInteger <Bytes> const& other) const noexcept
{ {
return compare (other) <= 0; return compare (other) <= 0;
} }
/** Ordered comparison. /** Ordered comparison. */
*/
bool operator> (UnsignedInteger <Bytes> const& other) const noexcept bool operator> (UnsignedInteger <Bytes> const& other) const noexcept
{ {
return compare (other) > 0; return compare (other) > 0;
} }
/** Ordered comparison. /** Ordered comparison. */
*/
bool operator>= (UnsignedInteger <Bytes> const& other) const noexcept bool operator>= (UnsignedInteger <Bytes> const& other) const noexcept
{ {
return compare (other) >= 0; return compare (other) >= 0;
} }
/** Perform bitwise logical-not. /** Perform bitwise logical-not. */
*/
UnsignedInteger <Bytes> operator~ () const noexcept UnsignedInteger <Bytes> operator~ () const noexcept
{ {
UnsignedInteger <Bytes> result; UnsignedInteger <Bytes> result;
@@ -298,8 +306,7 @@ public:
return result; return result;
} }
/** Perform bitwise logical-or. /** Perform bitwise logical-or. */
*/
UnsignedInteger <Bytes>& operator|= (UnsignedInteger <Bytes> const& rhs) noexcept UnsignedInteger <Bytes>& operator|= (UnsignedInteger <Bytes> const& rhs) noexcept
{ {
for (int i = 0; i < Bytes; ++i) for (int i = 0; i < Bytes; ++i)
@@ -308,8 +315,7 @@ public:
return *this; return *this;
} }
/** Perform bitwise logical-or. /** Perform bitwise logical-or. */
*/
UnsignedInteger <Bytes> operator| (UnsignedInteger <Bytes> const& rhs) const noexcept UnsignedInteger <Bytes> operator| (UnsignedInteger <Bytes> const& rhs) const noexcept
{ {
UnsignedInteger <Bytes> result; UnsignedInteger <Bytes> result;
@@ -320,8 +326,7 @@ public:
return result; return result;
} }
/** Perform bitwise logical-and. /** Perform bitwise logical-and. */
*/
UnsignedInteger <Bytes>& operator&= (UnsignedInteger <Bytes> const& rhs) noexcept UnsignedInteger <Bytes>& operator&= (UnsignedInteger <Bytes> const& rhs) noexcept
{ {
for (int i = 0; i < Bytes; ++i) for (int i = 0; i < Bytes; ++i)
@@ -330,8 +335,7 @@ public:
return *this; return *this;
} }
/** Perform bitwise logical-and. /** Perform bitwise logical-and. */
*/
UnsignedInteger <Bytes> operator& (UnsignedInteger <Bytes> const& rhs) const noexcept UnsignedInteger <Bytes> operator& (UnsignedInteger <Bytes> const& rhs) const noexcept
{ {
UnsignedInteger <Bytes> result; UnsignedInteger <Bytes> result;
@@ -342,8 +346,7 @@ public:
return result; return result;
} }
/** Perform bitwise logical-xor. /** Perform bitwise logical-xor. */
*/
UnsignedInteger <Bytes>& operator^= (UnsignedInteger <Bytes> const& rhs) noexcept UnsignedInteger <Bytes>& operator^= (UnsignedInteger <Bytes> const& rhs) noexcept
{ {
for (int i = 0; i < Bytes; ++i) for (int i = 0; i < Bytes; ++i)
@@ -352,8 +355,7 @@ public:
return *this; return *this;
} }
/** Perform bitwise logical-xor. /** Perform bitwise logical-xor. */
*/
UnsignedInteger <Bytes> operator^ (UnsignedInteger <Bytes> const& rhs) const noexcept UnsignedInteger <Bytes> operator^ (UnsignedInteger <Bytes> const& rhs) const noexcept
{ {
UnsignedInteger <Bytes> result; UnsignedInteger <Bytes> result;