This commit breaks the ARM64 version, I will fix it next. Lots going on here. DynafuncMaker got updated to store strings to back the ModuleAndName objects that get hardcoded in the assembly glue function. ModuleAndName is not a typedef to a tuple anymore, because I discovered that tuples suck. They get always pushed on the stack when passed as parameter, instead the new implementation gets passed into 2 registers being it a standard layout type. dhandy::bt::string got updated so it can be used as a literal value for non-type template parameters, which allowed for a really easy to use `wren::MN<>` helper. Code now fully requires c++20.
143 lines
5 KiB
C++
143 lines
5 KiB
C++
/* Copyright 2020-2022, Michele Santullo
|
|
* This file is part of wrenpp.
|
|
*
|
|
* Wrenpp is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Wrenpp is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with wrenpp. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <vector>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
|
|
namespace wren {
|
|
namespace detail {
|
|
template <std::size_t Num>
|
|
struct StringsInVectorBase;
|
|
|
|
struct StringsInVectorWordRange {
|
|
std::size_t start, length;
|
|
};
|
|
|
|
template <std::size_t Num>
|
|
struct StringsInVectorBase {
|
|
explicit StringsInVectorBase (StringsInVectorWordRange range) :
|
|
m_word_range(range)
|
|
{ }
|
|
|
|
StringsInVectorWordRange m_word_range;
|
|
};
|
|
|
|
template <std::size_t Index, typename String, typename... Strings>
|
|
[[gnu::pure]]
|
|
std::size_t string_offset_at_index (const String& string, const Strings&... strings) {
|
|
static_assert(Index < sizeof...(Strings) + 1, "Index out of range");
|
|
|
|
if constexpr (not Index) {
|
|
return 0;
|
|
}
|
|
else {
|
|
return std::string_view{string}.size() + string_offset_at_index<Index - 1>(strings...);
|
|
}
|
|
}
|
|
|
|
inline void set_zero (char& value) {
|
|
value = '\0';
|
|
}
|
|
} //namespace detail
|
|
|
|
template <std::size_t Count, typename IndexSeq>
|
|
class StringsInVectorImplem;
|
|
|
|
//It's a class that stores a series of strings in a vector, consecutively
|
|
//and interleaved by a null char. Objects of this class are safe to move
|
|
//and copy, move doesn't invalidate previously obtained string_views
|
|
//and strings_views are guaranteed to be null-terminated, so the result
|
|
//of data() is safe to pass to C functions.
|
|
template <std::size_t Count, std::size_t... Indices>
|
|
class StringsInVectorImplem<Count, std::index_sequence<Indices...>> : detail::StringsInVectorBase<Indices>... {
|
|
typedef detail::StringsInVectorWordRange WordRange;
|
|
template <typename... Strings> friend std::vector<char> strings_to_vector(const Strings&...);
|
|
public:
|
|
template <typename... Strings>
|
|
StringsInVectorImplem (const Strings&... strings);
|
|
|
|
template <std::size_t Index>
|
|
std::string_view as_string_view() const;
|
|
|
|
bool operator== (const StringsInVectorImplem& other) const;
|
|
bool operator< (const StringsInVectorImplem& other) const;
|
|
|
|
private:
|
|
std::vector<char> m_memory;
|
|
};
|
|
|
|
template <std::size_t Count, std::size_t... Indices>
|
|
template <typename... Strings>
|
|
inline StringsInVectorImplem<Count, std::index_sequence<Indices...>>::StringsInVectorImplem(const Strings&... strings) :
|
|
detail::StringsInVectorBase<Indices>(WordRange{detail::string_offset_at_index<Indices>(strings...) + Indices, std::string_view{strings}.size()})...
|
|
{
|
|
using detail::string_offset_at_index;
|
|
using detail::set_zero;
|
|
|
|
const std::size_t buffer_size = (std::string_view{strings}.size() + ...) + sizeof...(Indices);
|
|
m_memory.resize(buffer_size);
|
|
(std::copy(
|
|
std::string_view{strings}.cbegin(),
|
|
std::string_view{strings}.cend(),
|
|
m_memory.begin() + string_offset_at_index<Indices>(strings...) + Indices
|
|
), ...);
|
|
|
|
//zero out the byte after each string
|
|
(set_zero(m_memory[string_offset_at_index<Indices>(strings...) + Indices + std::string_view(strings).size()]), ...);
|
|
}
|
|
|
|
template <std::size_t Count, std::size_t... Indices>
|
|
template <std::size_t Index>
|
|
inline std::string_view StringsInVectorImplem<Count, std::index_sequence<Indices...>>::as_string_view() const {
|
|
static_assert(Index < Count, "Index out of range");
|
|
const auto& word_range = detail::StringsInVectorBase<Index>::m_word_range;
|
|
assert(m_memory.size() >= word_range.start + word_range.length + 1);
|
|
|
|
std::string_view retval{
|
|
m_memory.data() + word_range.start,
|
|
word_range.length
|
|
};
|
|
assert(*(retval.data() + retval.length()) == '\0');
|
|
return retval;
|
|
}
|
|
|
|
template <std::size_t Count, std::size_t... Indices>
|
|
inline bool StringsInVectorImplem<Count, std::index_sequence<Indices...>>::operator== (const StringsInVectorImplem& other) const {
|
|
return this->m_memory == other.m_memory;
|
|
}
|
|
|
|
template <std::size_t Count, std::size_t... Indices>
|
|
inline bool StringsInVectorImplem<Count, std::index_sequence<Indices...>>::operator< (const StringsInVectorImplem& other) const {
|
|
std::string_view this_memory{m_memory.data(), m_memory.size()};
|
|
std::string_view other_memory{other.m_memory.data(), other.m_memory.size()};
|
|
return this_memory < other_memory;
|
|
}
|
|
|
|
template <std::size_t Count>
|
|
using StringsInVector = StringsInVectorImplem<Count, decltype(std::make_index_sequence<Count>())>;
|
|
|
|
template <typename... Strings>
|
|
inline std::vector<char> strings_to_vector (const Strings&... strings) {
|
|
StringsInVector<sizeof...(Strings)> siv{strings...};
|
|
return siv.m_memory;
|
|
}
|
|
} //namespace wren
|