Extract consecutive strings logic into a separate class
This is going to be also used in the upcoming ClassManager class. It also makes FullSignatureOwning less cluttered.
This commit is contained in:
parent
d985ebc417
commit
74670f4683
3 changed files with 143 additions and 49 deletions
|
@ -18,6 +18,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "wren_types.hpp"
|
||||
#include "strings_in_vector.hpp"
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
|
@ -60,25 +61,13 @@ namespace wren {
|
|||
std::size_t m_signature_hash;
|
||||
};
|
||||
|
||||
class FullSignatureOwning : public FullSignatureBase {
|
||||
class FullSignatureOwning : public FullSignatureBase, private StringsInVector<3> {
|
||||
public:
|
||||
FullSignatureOwning (std::string_view module_name, std::string_view class_name, std::string_view signature);
|
||||
|
||||
std::string_view module_name() const { return make_string_view(m_module_range); }
|
||||
std::string_view class_name() const { return make_string_view(m_class_range); }
|
||||
std::string_view signature() const { return make_string_view(m_signature_range); }
|
||||
|
||||
private:
|
||||
struct WordRange {
|
||||
std::size_t start, length;
|
||||
};
|
||||
|
||||
std::string_view make_string_view(WordRange range) const;
|
||||
|
||||
std::vector<char> m_full_name;
|
||||
WordRange m_module_range;
|
||||
WordRange m_class_range;
|
||||
WordRange m_signature_range;
|
||||
std::string_view module_name() const { return as_string_view<0>(); }
|
||||
std::string_view class_name() const { return as_string_view<1>(); }
|
||||
std::string_view signature() const { return as_string_view<2>(); }
|
||||
};
|
||||
|
||||
//Temp because it's only supposed to be used to store temporaries
|
||||
|
|
136
include/wrenpp/strings_in_vector.hpp
Normal file
136
include/wrenpp/strings_in_vector.hpp
Normal file
|
@ -0,0 +1,136 @@
|
|||
/* 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;
|
||||
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>())>;
|
||||
} //namespace wren
|
|
@ -21,40 +21,9 @@
|
|||
|
||||
namespace wren::detail {
|
||||
FullSignatureOwning::FullSignatureOwning (std::string_view module_name, std::string_view class_name, std::string_view signature) :
|
||||
FullSignatureBase(std::hash<std::string_view>{}(module_name), std::hash<std::string_view>{}(class_name), std::hash<std::string_view>{}(signature))
|
||||
FullSignatureBase(std::hash<std::string_view>{}(module_name), std::hash<std::string_view>{}(class_name), std::hash<std::string_view>{}(signature)),
|
||||
StringsInVector<3>(module_name, class_name, signature)
|
||||
{
|
||||
m_full_name.resize(module_name.size() + 1 + class_name.size() + 1 + signature.size() + 1);
|
||||
char* buff = m_full_name.data();
|
||||
std::size_t buff_used = 0;
|
||||
|
||||
std::copy(module_name.cbegin(), module_name.cend(), buff + buff_used);
|
||||
m_module_range.start = buff_used;
|
||||
m_module_range.length = module_name.size();
|
||||
buff[buff_used + module_name.size()] = '\0';
|
||||
buff_used += module_name.size() + 1;
|
||||
|
||||
std::copy(class_name.cbegin(), class_name.cend(), buff + buff_used);
|
||||
m_class_range.start = buff_used;
|
||||
m_class_range.length = class_name.size();
|
||||
buff[buff_used + class_name.size()] = '\0';
|
||||
buff_used += class_name.size() + 1;
|
||||
|
||||
std::copy(signature.cbegin(), signature.cend(), buff + buff_used);
|
||||
m_signature_range.start = buff_used;
|
||||
m_signature_range.length = signature.size();
|
||||
buff[buff_used + signature.size()] = '\0';
|
||||
buff_used += signature.size() + 1;
|
||||
|
||||
assert(m_full_name[m_module_range.start + m_module_range.length] == '\0');
|
||||
assert(m_full_name[m_class_range.start + m_class_range.length] == '\0');
|
||||
assert(m_full_name[m_signature_range.start + m_signature_range.length] == '\0');
|
||||
assert(this->module_name() == module_name);
|
||||
assert(this->class_name() == class_name);
|
||||
assert(this->signature() == signature);
|
||||
}
|
||||
|
||||
std::string_view FullSignatureOwning::make_string_view(WordRange range) const {
|
||||
return {m_full_name.data() + range.start, range.length};
|
||||
}
|
||||
|
||||
TempFullSignature::TempFullSignature (const FullSignatureOwning& full_sig) :
|
||||
|
|
Loading…
Reference in a new issue