Sub in std::array for boost::array and beast::FixedArray

This commit is contained in:
Howard Hinnant
2014-04-03 14:02:26 -04:00
parent 90842073bf
commit cc354ee9f2
8 changed files with 19 additions and 226 deletions

View File

@@ -147,7 +147,6 @@
<ClInclude Include="..\..\beast\cxx14\memory.h" />
<ClInclude Include="..\..\beast\cxx14\type_traits.h" />
<ClInclude Include="..\..\beast\cxx14\utility.h" />
<ClInclude Include="..\..\beast\FixedArray.h" />
<ClInclude Include="..\..\beast\HeapBlock.h" />
<ClInclude Include="..\..\beast\http\basic_message.h" />
<ClInclude Include="..\..\beast\http\impl\http-parser\http_parser.h" />

View File

@@ -735,9 +735,6 @@
<ClInclude Include="..\..\beast\http\ParsedURL.h">
<Filter>beast\http</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\FixedArray.h">
<Filter>beast</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\Crypto.h">
<Filter>beast</Filter>
</ClInclude>

View File

@@ -1,166 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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_FIXEDARRAY_H_INCLUDED
#define BEAST_FIXEDARRAY_H_INCLUDED
#include "Config.h"
#include <cstddef>
#include <iterator>
#include <stdexcept>
namespace beast {
// Ideas from Boost
/** An array whose size is determined at compile-time.
The interface tries to follow std::vector as closely as possible within
the limitations of having a fixed size.
*/
template <class T, std::size_t N>
class FixedArray
{
public:
T values [N];
typedef T value_type;
typedef T* iterator;
typedef T const* const_iterator;
typedef T& reference;
typedef T const& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// iterators
iterator begin() { return values; }
const_iterator begin() const { return values; }
const_iterator cbegin() const { return values; }
iterator end() { return values+N; }
const_iterator end() const { return values+N; }
const_iterator cend() const { return values+N; }
typedef std::reverse_iterator <iterator> reverse_iterator;
typedef std::reverse_iterator <const_iterator> const_reverse_iterator;
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
reference operator[](size_type i)
{
bassert (i < N);
return values[i];
}
const_reference operator[](size_type i) const
{
bassert (i < N);
return values[i];
}
reference at(size_type i) { rangecheck(i); return values[i]; }
const_reference at(size_type i) const { rangecheck(i); return values[i]; }
reference front() { return values[0]; }
reference back() { return values[N-1]; }
const_reference front () const { return values[0]; }
const_reference back() const { return values[N-1]; }
static size_type size() { return N; }
static bool empty() { return false; }
static size_type max_size() { return N; }
enum { static_size = N };
T const* data() const { return values; }
T* data() { return values; }
T* c_array() { return values; }
template <typename T2>
FixedArray<T,N>& operator= (FixedArray<T2,N> const& rhs)
{
std::copy (rhs.begin(), rhs.end(), begin());
return *this;
}
void assign (T const& value) { fill (value); }
void fill (T const& value)
{
std::fill_n (begin(), size(), value);
}
void clear ()
{
fill (T ());
}
static void rangecheck (size_type i)
{
if (i >= size())
throw std::out_of_range ("FixedArray<>: index out of range");
}
};
//------------------------------------------------------------------------------
template <class T, std::size_t N>
bool operator== (FixedArray <T, N> const& lhs, FixedArray <T, N> const& rhs)
{
return std::equal (lhs.begin(), lhs.end(), rhs.begin());
}
template <class T, std::size_t N>
bool operator!= (FixedArray <T, N> const& lhs, FixedArray <T, N> const& rhs)
{
return !(lhs==rhs);
}
template <class T, std::size_t N>
bool operator< (FixedArray <T, N> const& lhs, FixedArray <T, N> const& rhs)
{
return std::lexicographical_compare (
lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <class T, std::size_t N>
bool operator> (FixedArray <T, N> const& lhs, FixedArray <T, N> const& rhs)
{
return rhs<lhs;
}
template <class T, std::size_t N>
bool operator<= (FixedArray <T, N> const& lhs, FixedArray <T, N> const& rhs)
{
return !(rhs<lhs);
}
template <class T, std::size_t N>
bool operator>= (FixedArray <T, N> const& lhs, FixedArray <T, N> const& rhs)
{
return !(lhs<rhs);
}
}
#endif

View File

@@ -24,7 +24,6 @@
#include "../utility/meta.h"
#if BEAST_USE_BOOST_FEATURES
#include <boost/array.hpp>
#include <boost/tuple/tuple.hpp>
#endif
@@ -155,17 +154,6 @@ struct is_contiguously_hashable <boost::tuple<T...>>
};
#endif
// boost::array
template <class T, std::size_t N>
struct is_contiguously_hashable <boost::array<T, N>>
: public std::integral_constant <bool,
is_contiguously_hashable<T>::value &&
sizeof(T)*N == sizeof(boost::array<T, N>)>
{
};
static_assert (is_contiguously_hashable <boost::array<char, 3>>::value, "");
#endif // BEAST_USE_BOOST_FEATURES
/** @} */
@@ -302,17 +290,6 @@ typename std::enable_if
>::type
hash_append (Hasher& h, std::array<T, N> const& a) noexcept;
#if BEAST_USE_BOOST_FEATURES
template <class Hasher, class T, std::size_t N>
typename std::enable_if
<
!is_contiguously_hashable<boost::array<T, N>>::value
>::type
hash_append (Hasher& h, boost::array<T, N> const& a) noexcept;
#endif // BEAST_USE_BOOST_FEATURES
// std::tuple
template <class Hasher>
@@ -953,24 +930,6 @@ hash_append (Hasher& h, std::vector<T, Alloc> const& v) noexcept
h.append (v.data(), v.size()*sizeof(T));
}
#if BEAST_USE_BOOST_FEATURES
// boost::array
template <class Hasher, class T, std::size_t N>
inline
std::enable_if_t
<
!is_contiguously_hashable<T>::value
>
hash_append (Hasher& h, boost::array<T, N> const& v) noexcept
{
for (auto const& t : v)
hash_append (h, t);
}
#endif // BEAST_USE_BOOST_FEATURES
template <class Hasher, class T0, class T1, class ...T>
inline
void

View File

@@ -21,6 +21,7 @@
#define BEAST_ASIO_BASICS_FIXEDINPUTBUFFER_H_INCLUDED
#include "../../../beast/asio/buffer_sequence.h"
#include <array>
namespace beast {
namespace asio {
@@ -191,7 +192,7 @@ public:
}
private:
boost::array <std::uint8_t, Bytes> m_storage;
std::array <std::uint8_t, Bytes> m_storage;
boost::asio::mutable_buffer m_buffer;
};

View File

@@ -21,8 +21,8 @@
#define RIPPLE_TYPES_CRYPTOIDENTIFIER_H_INCLUDED
#include "../../beast/beast/ByteOrder.h"
#include "../../beast/beast/FixedArray.h"
#include "../../beast/beast/crypto/Sha256.h"
#include <array>
#include "Base58.h"
@@ -74,8 +74,10 @@ public:
if (Checked)
{
beast::Sha256::digest_type digest;
beast::Sha256::hash (beast::Sha256::hash (value.storage().cbegin(),
value.storage().cend() - post_size), digest);
auto const& vs = value.storage();
beast::Sha256::hash (beast::Sha256::hash (vs.data(),
vs.data() + (vs.size() - post_size)),
digest);
// We use the first 4 bytes as a checksum
std::copy (digest.begin(), digest.begin() + 4,
value.end());
@@ -90,12 +92,12 @@ public:
static value_type createFromInteger (UnsignedIntegralType i)
{
static_bassert (size >= sizeof (UnsignedIntegralType));
beast::FixedArray <std::uint8_t, size> data;
std::array <std::uint8_t, size> data;
data.fill (0);
i = beast::toNetworkByteOrder <UnsignedIntegralType> (i);
std::memcpy (data.end () - sizeof (i), &i, std::min (size, sizeof (i)));
std::memcpy (data.data () + (data.size() - sizeof (i)), &i, std::min (size, sizeof (i)));
value_type value;
construct (data.begin(), data.end(), value);
construct (data.data(), data.data() + data.size(), value);
return value;
}
};
@@ -105,11 +107,11 @@ public:
{
typename value_type::storage_type const& storage (value.storage());
// We will convert to little endian with an extra pad byte
beast::FixedArray <std::uint8_t, value_type::storage_size + 1> le;
std::array <std::uint8_t, value_type::storage_size + 1> le;
std::reverse_copy (storage.begin(), storage.end(), le.begin());
// Set pad byte zero to make BIGNUM always positive
le.back() = 0;
return Base58::raw_encode (le.begin(), le.end(),
return Base58::raw_encode (le.data(), le.data() + le.size(),
Base58::getRippleAlphabet(), Checked);
}
@@ -119,7 +121,7 @@ public:
value_type value;
bool success (! s.empty());
if (success && !Base58::raw_decode (&s.front(), &s.back()+1,
value.storage().begin(), value_type::storage_size, Checked,
value.storage().data(), value_type::storage_size, Checked,
Base58::getRippleAlphabet()))
success = false;
if (success && value.storage()[0] != Token)

View File

@@ -23,11 +23,11 @@
#include "../../beast/beast/crypto/MurmurHash.h"
#include "../../beast/beast/container/hardened_hash.h"
#include <boost/array.hpp>
#include <array>
namespace ripple {
/** A padded FixedArray used with IdentifierType traits. */
/** A padded std::array used with IdentifierType traits. */
template <std::size_t PreSize, std::size_t Size, std::size_t PostSize>
class IdentifierStorage
{
@@ -45,7 +45,7 @@ public:
static size_type const post_size = PostSize;
static size_type const storage_size = pre_size + size + post_size;
typedef boost::array <
typedef std::array <
std::uint8_t, storage_size> storage_type;
/** Value hashing function.

View File

@@ -21,6 +21,7 @@
#define RIPPLE_TYPES_RIPPLEACCOUNTID_H_INCLUDED
#include "CryptoIdentifier.h"
#include <array>
namespace ripple {
@@ -46,11 +47,11 @@ public:
{
value_type::storage_type const& storage (value.storage());
// We will convert to little endian with an extra pad byte
beast::FixedArray <std::uint8_t, value_type::storage_size + 1> le;
std::array <std::uint8_t, value_type::storage_size + 1> le;
std::reverse_copy (storage.begin(), storage.end(), le.begin());
// Set pad byte zero to make BIGNUM always positive
le.back() = 0;
return Base58::raw_encode (le.begin(), le.end(),
return Base58::raw_encode (le.data(), le.data() + le.size(),
Base58::getRippleAlphabet(), checked);
}
};