Skip to content

Commit

Permalink
Fixed: array view initialized list test
Browse files Browse the repository at this point in the history
  • Loading branch information
abbyssoul committed Mar 13, 2020
1 parent 9791821 commit ed8b7e3
Show file tree
Hide file tree
Showing 21 changed files with 138 additions and 132 deletions.
2 changes: 1 addition & 1 deletion include/solace/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ makeArray(MemoryManager& memManager, typename Array<T>::size_type initialSize) {

// Emplce new default constructed values T into a new memory space.
// This may throw if construction of T{} throws
initArray<T>(maybeBuffer.unwrap().view(), initialSize);
initArray(arrayView<T>(maybeBuffer.unwrap().view()));

return makeArray<T>(maybeBuffer.moveResult(), initialSize); // No except c-tor
}
Expand Down
71 changes: 27 additions & 44 deletions include/solace/arrayView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,15 @@ class LIFETIME_HINT_POINTER(T) ArrayView {
{}

constexpr ArrayView(ArrayView const& other) noexcept
: _memory(other._memory)
: _memory{other._memory}
{}

constexpr ArrayView(ArrayView&& other) noexcept
: _memory(mv(other._memory))
: _memory{mv(other._memory)}
{}

/** Construct an array from C-style array with the given size */
constexpr ArrayView(T* ptr, size_type arraySize)
: _memory{wrapMemory(ptr, sizeof(T) * arraySize)}
{}

constexpr ArrayView(T* beginSeq, T* endSeq) noexcept
: _memory{wrapMemory(beginSeq, sizeof(T) * (endSeq - beginSeq))}
{}

template <size_t size>
constexpr ArrayView(T (&carray)[size]) noexcept
: _memory{wrapMemory(carray)}
{}

constexpr ArrayView(ViewType memview) noexcept :
_memory(mv(memview))
constexpr ArrayView(ViewType memview) noexcept
: _memory{mv(memview)}
{}

public:
Expand Down Expand Up @@ -166,30 +152,30 @@ class LIFETIME_HINT_POINTER(T) ArrayView {
const_reference operator[] (size_type index) const {
index = assertIndexInRange(index, 0, size(), "ArrayView[] const");

return _memory.template dataAs<T>()[index];
return _memory.template dataAs<T>(sizeof(T) * index);
}


reference operator[] (size_type index) {
index = assertIndexInRange(index, 0, size(), "ArrayView[]");

return _memory.template dataAs<T>()[index];
}
return _memory.template dataAs<T>(sizeof(T) * index);
}


Iterator begin() noexcept {
return _memory.empty()
? nullptr
: _memory.template dataAs<T>();
: static_cast<Iterator>(_memory.dataAddress());
}

Iterator end() noexcept { return begin() + size(); }

const_iterator begin() const noexcept {
return _memory.empty()
? nullptr
: _memory.template dataAs<T>();
}
: static_cast<const_iterator>(_memory.dataAddress());
}

const_iterator end() const noexcept { return (begin() + size()); }

Expand All @@ -213,7 +199,7 @@ class LIFETIME_HINT_POINTER(T) ArrayView {
set(size_type index, F&& f) {
index = assertIndexInRange(index, 0, size(), "ArrayView.set()");

_memory.template dataAs<T>()[index] = f();
_memory.template dataAs<T>(sizeof(T) * index) = f();
}


Expand Down Expand Up @@ -315,7 +301,7 @@ class LIFETIME_HINT_POINTER(T) ArrayView {
}

template<typename F>
std::enable_if_t<isCallable<F, const T&>::value,
std::enable_if_t<isCallable<F, T const&>::value,
const ArrayView<T>& >
forEach(F&& f) const {
for (const auto& x : *this) {
Expand All @@ -327,14 +313,13 @@ class LIFETIME_HINT_POINTER(T) ArrayView {

template<typename F>
std::enable_if_t<
isCallable<F, size_type, const T&>::value,
isCallable<F, size_type, T const&>::value,
const ArrayView<T>& >
forEach(F&& f) const {
auto const thisSize = size();
auto const pData = _memory.template dataAs<T>();

for (size_type i = 0; i < thisSize; ++i) {
f(i, pData[i]);
size_type i = 0;
for (auto item : *this) {
f(i, item);
i += 1;
}

return *this;
Expand All @@ -347,12 +332,11 @@ class LIFETIME_HINT_POINTER(T) ArrayView {
isCallable<F, size_type, T&>::value,
ArrayView<T>& >
forEach(F&& f) {
auto const thisSize = size();
auto const pData = _memory.template dataAs<T>();

for (size_type i = 0; i < thisSize; ++i) {
f(i, pData[i]);
}
size_type i = 0;
for (auto item : *this) {
f(i, item);
i += 1;
}

return *this;
}
Expand Down Expand Up @@ -447,36 +431,35 @@ constexpr ArrayView<T> arrayView(MutableMemoryView memView, typename ArrayView<T
return {memView.slice(0, len * sizeof(T))};
}


/** Syntactic sugar to create ArrayView without spelling out the type name. */
template <typename T>
[[nodiscard]] constexpr ArrayView<T> arrayView(T* ptr, typename ArrayView<T>::size_type size) {
return {ptr, narrow_cast<typename ArrayView<const T>::size_type>(size)};
return {wrapMemory(ptr, sizeof(T) * size)};
}

/** Syntactic sugar to create ArrayView without spelling out the type name. */
template <typename T>
[[nodiscard]] constexpr ArrayView<T const> arrayView(T const* ptr, typename ArrayView<T>::size_type size) {
return {ptr, narrow_cast<typename ArrayView<T const>::size_type>(size)};
return {wrapMemory(ptr, sizeof(T) * size)};
}

/** Syntactic sugar to create ArrayView without spelling out the type name. */
template <typename T, size_t N>
[[nodiscard]] constexpr ArrayView<T> arrayView(T (&carray)[N]) noexcept {
return {carray};
return {wrapMemory(carray)};
}


/** Syntactic sugar to create ArrayView without spelling out the type name. */
template <typename T>
[[nodiscard]] constexpr ArrayView<T> arrayView(T* begin, T* end) {
return {begin, end};
return {wrapMemory(begin, sizeof(T) * (end - begin))};
}

/** Syntactic sugar to create ArrayView without spelling out the type name. */
template <typename T>
[[nodiscard]] constexpr ArrayView<T const> arrayView(T const* begin, T const* end) {
return {begin, end};
return {wrapMemory(begin, sizeof(T) * (end - begin))};
}


Expand Down
10 changes: 5 additions & 5 deletions include/solace/details/array_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ struct ExceptionGuard {


template <typename T>
void initArray(MutableMemoryView bufferView, typename ArrayView<T>::size_type arraySize) {
void initArray(ArrayView<T> arr) {
if constexpr (std::is_nothrow_default_constructible<T>::value) {
auto pos = bufferView.template dataAs<T>();
for (decltype(arraySize) i = 0; i < arraySize; ++i) {
ctor(*pos++);
for (auto& pos : arr) {
ctor(pos);
}
} else {
ExceptionGuard<T> guard{bufferView.template dataAs<T>()};
auto arraySize = arr.size();
ExceptionGuard<T> guard{arr.begin()};
for (decltype(arraySize) i = 0; i < arraySize; ++i) {
ctor(*guard.pos);
++guard.pos; // Important to be on a different line, in case of exception.
Expand Down
13 changes: 6 additions & 7 deletions include/solace/memoryView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,17 @@ class LIFETIME_HINT_POINTER(void) MemoryView {
Optional<MemoryAddress> dataAddress(size_type offset) const noexcept;

template <typename T>
T const* dataAs() const {
assertTrue(_size == 0 || sizeof(T) <= size(), "Not enough room to emplace type T");
T const& dataAs() const {
assertTrue(sizeof(T) <= size(), "Not enough room for value of type T");

return reinterpret_cast<T const*>(_dataAddress);
return *reinterpret_cast<T const*>(_dataAddress);
}

template <typename T>
T const* dataAs(size_type offset) const {
assertIndexInRange(offset, 0, this->size());
assertIndexInRange(offset + sizeof(T), offset, this->size());
T const& dataAs(size_type offset) const {
assertTrue(offset + sizeof(T) <= size(), "Not enough room for value of type T");

return reinterpret_cast<T const*>(begin() + offset);
return *reinterpret_cast<T const*>(begin() + offset);
}


Expand Down
20 changes: 9 additions & 11 deletions include/solace/mutableMemoryView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,16 @@ class MutableMemoryView


using MemoryView::dataAs;

template <typename T>
T* dataAs() {
return const_cast<T*>(MemoryView::dataAs<T>());
T& dataAs() {
return const_cast<T&>(MemoryView::dataAs<T>());
}

// template <typename T>
// T* dataAs(size_type offset = 0) {
// assertIndexInRange(offset, 0, this->size());
// assertIndexInRange(offset + sizeof(T), offset, this->size() + 1);

// return reinterpret_cast<T*>(dataAddress() + offset);
// }
template <typename T>
T& dataAs(size_type offset) {
return const_cast<T&>(MemoryView::dataAs<T>(offset));
}


/**
Expand Down Expand Up @@ -206,14 +204,14 @@ class MutableMemoryView
constexpr auto const spaceRequired = sizeof(T);
assertTrue(spaceRequired <= size(), "No room to construct new value");

T& dest = *dataAs<T>();
T& dest = dataAs<T>();
return ctor(dest, fwd<Args>(args)...);
}

template<typename T>
void destruct() noexcept(std::is_nothrow_destructible<T>::value) {
// Note: dataAs<> does assertion for the storage size
dtor(*dataAs<T>());
dtor(dataAs<T>());
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/solace/output_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ operator<< (std::ostream& ostr, hashing::MessageDigest const& a) {
i != end; ++i) {
auto const encodedView = (*i).view();
ostr << "0x";
ostr.write(encodedView.dataAs<char>(), encodedView.size());
ostr.write(static_cast<char const*>(encodedView.dataAddress()), encodedView.size());
}

ostr << ']';
Expand Down
2 changes: 1 addition & 1 deletion include/solace/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class String {
* @return Immutable Memory View into the string data.
*/
/*constexpr*/ StringView view() const noexcept {
return {_buffer.view().dataAs<char>(), size()};
return {_buffer.view().slice(0, sizeof(value_type)*size())};
}

public:
Expand Down
6 changes: 3 additions & 3 deletions include/solace/stringView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class LIFETIME_HINT_POINTER(char) StringView {

StringView(MemoryView data) noexcept
: _size{narrow_cast<size_type>(data.size())}
, _data{data.dataAs<char>()}
, _data{static_cast<const_iterator>(data.dataAddress())}
{}

StringView& swap(StringView& rhs) noexcept {
Expand Down Expand Up @@ -445,8 +445,8 @@ struct StringLiteral: public StringView {
constexpr StringLiteral() noexcept = default;

template<size_t N>
constexpr StringLiteral(char const (&str)[N]) :
StringView(N -1, &str[0])
constexpr StringLiteral(char const (&str)[N]) noexcept
: StringView{N - 1, &str[0]}
{
}
};
Expand Down
11 changes: 6 additions & 5 deletions include/solace/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ class Vector {
}

pointer data() noexcept {
return _buffer.view().template dataAs<T>();
return static_cast<pointer>(_buffer.view().dataAddress());
}

const_pointer data() const noexcept {
return _buffer.view().template dataAs<T>();
}
return static_cast<const_pointer>(_buffer.view().dataAddress());
}

ArrayView<T const> view() const noexcept {
return arrayView<T const>(_buffer.view(), _position);
Expand Down Expand Up @@ -481,13 +481,14 @@ Result<Vector<T>, Error> makeVectorOf(MemoryManager& memManager, std::initialize
return maybeBuffer.moveError();
}

auto arr = arrayView<T>(maybeBuffer.unwrap().view());
if constexpr (std::is_nothrow_copy_constructible<T>::value) {
auto pos = maybeBuffer.unwrap().view().template dataAs<T>();
auto pos = arr.begin();
for (T const& i : list) {
ctor(*pos++, i);
}
} else {
ExceptionGuard<T> guard{maybeBuffer.unwrap().view().template dataAs<T>()};
ExceptionGuard<T> guard{arr.begin()};
for (T const& i : list) {
ctor(*guard.pos, i);
++guard.pos;
Expand Down
8 changes: 4 additions & 4 deletions src/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ static const byte prUrl2six[256] = {


Result<void, Error>
base64decode(ByteWriter& dest, MemoryView const& src, byte const* decodingTable) {
base64decode(ByteWriter& dest, MemoryView src, byte const* decodingTable) {
if (src.empty()) {
return makeError(SystemErrors::NODATA, "base64decode");
}

byte const* bufin = src.dataAs<byte>();
byte const* bufin = src.begin();
while (decodingTable[*(bufin++)] <= 63) // Count decodable bytes
{}

auto nprbytes = (bufin - src.dataAs<byte>()) - 1;
bufin = src.dataAs<byte>();
auto nprbytes = (bufin - src.begin()) - 1;
bufin = src.begin();

while (nprbytes > 4) {
byte const encoded[] = {
Expand Down
7 changes: 4 additions & 3 deletions src/hashing/md5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void md5_process(MD5::State& ctx, const byte data[64]) {
}


void md5_update(MD5::State& ctx, const byte *input, MD5::size_type inputLen) {
void md5_update(MD5::State& ctx, MemoryView::const_iterator input, MD5::size_type inputLen) {
MD5::size_type fill;
uint32 left;

Expand Down Expand Up @@ -201,8 +201,9 @@ MD5::size_type MD5::getDigestLength() const {
}


HashingAlgorithm& MD5::update(MemoryView input) {
md5_update(_state, input.dataAs<byte>(), input.size());
HashingAlgorithm&
MD5::update(MemoryView input) {
md5_update(_state, input.begin(), input.size());

return (*this);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hashing/messageDigest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace Solace::hashing;


MessageDigest::MessageDigest(MemoryView viewBytes)
: _storage{makeArray<byte>(viewBytes.size(), viewBytes.dataAs<byte>()).moveResult()}
: _storage{makeArray<byte>(viewBytes.size(), viewBytes.begin()).moveResult()}
{ }


Expand Down
Loading

0 comments on commit ed8b7e3

Please sign in to comment.