diff --git a/include/wrenpp/callback_manager.hpp b/include/wrenpp/callback_manager.hpp index 073d86b..65bf237 100644 --- a/include/wrenpp/callback_manager.hpp +++ b/include/wrenpp/callback_manager.hpp @@ -18,6 +18,7 @@ #pragma once #include "wren_types.hpp" +#include "strings_in_vector.hpp" #include #include #include @@ -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 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 diff --git a/include/wrenpp/strings_in_vector.hpp b/include/wrenpp/strings_in_vector.hpp new file mode 100644 index 0000000..b88e2d7 --- /dev/null +++ b/include/wrenpp/strings_in_vector.hpp @@ -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 . + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace wren { + namespace detail { + template + struct StringsInVectorBase; + + struct StringsInVectorWordRange { + std::size_t start, length; + }; + + template + struct StringsInVectorBase { + explicit StringsInVectorBase (StringsInVectorWordRange range) : + m_word_range(range) + { } + + StringsInVectorWordRange m_word_range; + }; + + template + [[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(strings...); + } + } + + inline void set_zero (char& value) { + value = '\0'; + } + } //namespace detail + + template + 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 + class StringsInVectorImplem> : detail::StringsInVectorBase... { + typedef detail::StringsInVectorWordRange WordRange; + public: + template + StringsInVectorImplem (const Strings&... strings); + + template + std::string_view as_string_view() const; + + bool operator== (const StringsInVectorImplem& other) const; + bool operator< (const StringsInVectorImplem& other) const; + + private: + std::vector m_memory; + }; + + template + template + inline StringsInVectorImplem>::StringsInVectorImplem(const Strings&... strings) : + detail::StringsInVectorBase(WordRange{detail::string_offset_at_index(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(strings...) + Indices + ), ...); + + //zero out the byte after each string + (set_zero(m_memory[string_offset_at_index(strings...) + Indices + std::string_view(strings).size()]), ...); + } + + template + template + inline std::string_view StringsInVectorImplem>::as_string_view() const { + static_assert(Index < Count, "Index out of range"); + const auto& word_range = detail::StringsInVectorBase::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 + inline bool StringsInVectorImplem>::operator== (const StringsInVectorImplem& other) const { + return this->m_memory == other.m_memory; + } + + template + inline bool StringsInVectorImplem>::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 + using StringsInVector = StringsInVectorImplem())>; +} //namespace wren diff --git a/src/callback_manager.cpp b/src/callback_manager.cpp index 192fe4b..01bca7f 100644 --- a/src/callback_manager.cpp +++ b/src/callback_manager.cpp @@ -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{}(module_name), std::hash{}(class_name), std::hash{}(signature)) + FullSignatureBase(std::hash{}(module_name), std::hash{}(class_name), std::hash{}(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) :