Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace with std::string_view for C++17 #221

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ option(BUILD_PACKAGE "Enable this to build the installer" ON)
option(USE_PCH "Use precompiled header files for compilation" ON)
option(CUSTOM_DOXYGEN_STYLE "Enable this option to use a custom doxygen style for HTML documentation; Otherwise the default will be used" ON)
option(BUILD_WEBSITE_DOCU "Enable this option to create the special docu for the website" OFF)
option(USE_LATEST_STD "Build the library using the latest std-library fetures and making it probably not backward compatible." OFF)

# one precompiled headers cannot be used for multiple ninja targets
# thats why we have to disable this option, when BUILD_STATIC or
Expand Down
14 changes: 7 additions & 7 deletions src/examples/scripting/chaiscript_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,36 @@ class chaiscript_visitor : public rttr::visitor
template<typename T>
void visit_global_method(const method_info<T>& info)
{
m_chai.add(chaiscript::fun(info.function_ptr), info.method_item.get_name().to_string());
m_chai.add(chaiscript::fun(info.function_ptr), std::string(info.method_item.get_name()));
}

/////////////////////////////////////////////////////////////////////////////////////

template<typename T>
void visit_method(const method_info<T>& info)
{
m_chai.add(chaiscript::fun(info.function_ptr), info.method_item.get_name().to_string());
m_chai.add(chaiscript::fun(info.function_ptr), std::string(info.method_item.get_name()));
}

/////////////////////////////////////////////////////////////////////////////////////

template<typename T>
void visit_property(const property_info<T>& info)
{
m_chai.add(chaiscript::fun(info.property_accessor), info.property_item.get_name().to_string());
m_chai.add(chaiscript::fun(info.property_accessor), std::string(info.property_item.get_name()));
}

template<typename T>
void visit_getter_setter_property(const property_getter_setter_info<T>& info)
{
m_chai.add(chaiscript::fun(info.property_getter), std::string("get_") + info.property_item.get_name().to_string());
m_chai.add(chaiscript::fun(info.property_setter), std::string("set_") + info.property_item.get_name().to_string());
m_chai.add(chaiscript::fun(info.property_getter), std::string("get_") + std::string(info.property_item.get_name()));
m_chai.add(chaiscript::fun(info.property_setter), std::string("set_") + std::string(info.property_item.get_name()));
}

template<typename T>
void visit_readonly_property(const property_info<T>& info)
{
m_chai.add(chaiscript::fun(info.property_accessor), info.property_item.get_name().to_string());
m_chai.add(chaiscript::fun(info.property_accessor), std::string(info.property_item.get_name()));
}

/////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -119,7 +119,7 @@ class chaiscript_visitor : public rttr::visitor
template<typename T>
static std::string get_type_name()
{
return rttr::type::template get<T>().get_name().to_string();
return std::string(rttr::type::template get<T>().get_name());
}

private:
Expand Down
24 changes: 19 additions & 5 deletions src/rttr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,38 @@ if (USE_PCH)
activate_precompiled_headers("detail/base/pch.h" SRC_FILES)
endif()

# Determine the minium required standard.
if (USE_LATEST_STD)
if (MAX_CXX_STANDARD GREATER_EQUAL 17)
list(APPEND COMPILE_DEFS "RTTR_USE_STD_STRING_VIEW")
set(MIN_CXX_STANDARD cxx_std_17)
endif ()
else ()
set(MIN_CXX_STANDARD cxx_std_11) # at least c++11 is needed to compile RTTR
endif ()

if (${BUILD_RTTR_DYNAMIC})
add_library(rttr_core SHARED ${UnityBuild} ${SRC_FILES} ${HPP_FILES})
add_library(RTTR::Core ALIAS rttr_core)

target_compile_definitions(rttr_core PRIVATE RTTR_DLL_EXPORTS)
target_compile_definitions(rttr_core PUBLIC RTTR_DLL)



set_target_properties(rttr_core PROPERTIES
VERSION ${RTTR_VERSION} SOVERSION ${RTTR_VERSION}
EXPORT_NAME Core
DEBUG_POSTFIX ${RTTR_DEBUG_POSTFIX}
CXX_STANDARD ${MAX_CXX_STANDARD}
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN 1)


target_compile_definitions(rttr_core PUBLIC ${COMPILE_DEFS})

target_link_libraries(rttr_core PRIVATE ${CMAKE_DL_LIBS})


if(${CMAKE_VERSION} VERSION_GREATER "3.8.0")
target_compile_features(rttr_core PUBLIC cxx_std_11) # at least c++11 is needed to compile RTTR
target_compile_features(rttr_core PUBLIC ${MIN_CXX_STANDARD})
endif()

target_include_directories(rttr_core PUBLIC
Expand Down Expand Up @@ -94,7 +104,9 @@ if (BUILD_STATIC)
EXPORT_NAME Core_Lib
CXX_STANDARD ${MAX_CXX_STANDARD}
DEBUG_POSTFIX ${RTTR_DEBUG_POSTFIX})


target_compile_definitions(rttr_core PUBLIC ${COMPILE_DEFS})

target_link_libraries(rttr_core_lib PRIVATE ${CMAKE_DL_LIBS})

if (MSVC)
Expand Down Expand Up @@ -139,6 +151,8 @@ if (BUILD_WITH_STATIC_RUNTIME_LIBS)
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../>
$<INSTALL_INTERFACE:include>)

target_compile_definitions(rttr_core PUBLIC ${COMPILE_DEFS})

target_link_libraries(rttr_core_s PRIVATE ${CMAKE_DL_LIBS})

set_target_properties(rttr_core_s PROPERTIES
Expand Down
26 changes: 25 additions & 1 deletion src/rttr/detail/base/core_prerequisites.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ namespace rttr
# define RTTR_END_DISABLE_OVERRIDE_WARNING
#endif

# define RTTR_BEGIN_DISABLE_SELF_ASSIGN_OVERLOADED_WARNING
# define RTTR_END_DISABLE_SELF_ASSIGN_OVERLOADED_WARNING

# define RTTR_DECLARE_PLUGIN_CTOR __attribute__((constructor))
# define RTTR_DECLARE_PLUGIN_DTOR __attribute__((destructor))

Expand Down Expand Up @@ -316,6 +319,15 @@ namespace rttr
# define RTTR_DECLARE_PLUGIN_CTOR __attribute__((__constructor__))
# define RTTR_DECLARE_PLUGIN_DTOR __attribute__((__destructor__))

#if defined(__has_warning) && __has_warning("-Wself-assign-overloaded")
# define RTTR_BEGIN_DISABLE_SELF_ASSIGN_OVERLOADED_WARNING _Pragma ("clang diagnostic push") \
_Pragma ("clang diagnostic ignored \"-Wself-assign-overloaded\"")
# define RTTR_END_DISABLE_SELF_ASSIGN_OVERLOADED_WARNING _Pragma ("clang diagnostic pop")
#else
# define RTTR_BEGIN_DISABLE_SELF_ASSIGN_OVERLOADED_WARNING
# define RTTR_END_DISABLE_SELF_ASSIGN_OVERLOADED_WARNING
#endif

#elif RTTR_COMPILER == RTTR_COMPILER_MSVC
# define RTTR_BEGIN_DISABLE_DEPRECATED_WARNING __pragma( warning( push )) \
__pragma( warning( disable: 4996))
Expand All @@ -332,11 +344,23 @@ namespace rttr
# define RTTR_DECLARE_PLUGIN_DTOR
# define RTTR_BEGIN_DISABLE_OVERRIDE_WARNING
# define RTTR_END_DISABLE_OVERRIDE_WARNING

# define RTTR_BEGIN_DISABLE_SELF_ASSIGN_OVERLOADED_WARNING
# define RTTR_END_DISABLE_SELF_ASSIGN_OVERLOADED_WARNING
#else
# pragma message("WARNING: unknown compiler, don't know how to disable deprecated warnings")
#endif

/////////////////////////////////////////////////////////////////////////////////////////
// Enable some C++17 Features
/////////////////////////////////////////////////////////////////////////////////////////
#ifdef RTTR_USE_STD_STRING_VIEW
#if __cplusplus >= 201703L
#define RTTR_ENABLE_STD_STRING_VIEW
#else
#error "Linking with this library needs C++17, as std::string_view is required."
#endif
#endif

} // end namespace rttr

#endif // RTTR_CORE_PREREQUISITES_H_
4 changes: 2 additions & 2 deletions src/rttr/detail/constructor/constructor_wrapper_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ void constructor_wrapper_base::create_signature_string() RTTR_NOEXCEPT
return;

auto param_info_list = get_parameter_infos();
m_signature = get_instantiated_type().get_raw_type().get_name().to_string() + "( ";
m_signature = string(get_instantiated_type().get_raw_type().get_name()) + "( ";
auto ref_list = get_is_reference();
auto const_list = get_is_const();
for (const auto& param : param_info_list)
{
m_signature += param.get_type().get_name() + string(is_const_list[const_list[param.get_index()]]) + string(is_ref_list[ref_list[param.get_index()]]);
m_signature += string(param.get_type().get_name()) + string(is_const_list[const_list[param.get_index()]]) + string(is_ref_list[ref_list[param.get_index()]]);
if (param.get_index() < param_info_list.size() - 1)
m_signature += ", ";
}
Expand Down
1 change: 1 addition & 0 deletions src/rttr/detail/impl/array_range_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "rttr/property.h"
#include "rttr/type.h"
#include <memory>
#include <functional>

namespace rttr
{
Expand Down
13 changes: 2 additions & 11 deletions src/rttr/detail/impl/string_view_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,6 @@ basic_string_view<CharT, Traits>::operator std::basic_string<CharT, Traits, Allo

/////////////////////////////////////////////////////////////////////////////////////////

template<typename CharT, typename Traits>
template<typename Allocator>
std::basic_string<CharT, Traits> basic_string_view<CharT, Traits>::to_string(const Allocator& a) const
{
return std::basic_string<CharT, Traits, Allocator>(begin(), end(), a);
}

/////////////////////////////////////////////////////////////////////////////////////////

template<typename CharT, typename Traits>
RTTR_CXX14_CONSTEXPR int basic_string_view<CharT, Traits>::compare(basic_string_view v) const RTTR_NOEXCEPT
{
Expand Down Expand Up @@ -586,14 +577,14 @@ template<typename CharT, typename Traits>
RTTR_INLINE std::basic_string<CharT, Traits> operator+(basic_string_view<CharT, Traits> lhs,
const std::basic_string<CharT, Traits>& rhs)
{
return (lhs.to_string() + rhs);
return (std::string(lhs) + rhs);
}

template<typename CharT, typename Traits>
RTTR_INLINE std::basic_string<CharT, Traits> operator+(const std::basic_string<CharT, Traits>& lhs,
basic_string_view<CharT, Traits> rhs)
{
return (lhs + rhs.to_string());
return (lhs + std::string(rhs));
}

template<typename CharT, typename Traits>
Expand Down
8 changes: 5 additions & 3 deletions src/rttr/detail/library/library_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <dlfcn.h>
#include <cstdio>

using namespace std;

namespace
{

Expand Down Expand Up @@ -91,7 +93,7 @@ static rttr::string_view get_error()
{
using namespace rttr;
auto err = dlerror();
return err ? string_view(err): string_view();
return err ? rttr::string_view(err): rttr::string_view();
}

} // end namespace anonymous
Expand Down Expand Up @@ -146,7 +148,7 @@ bool library_private::load_native()

if (!m_handle)
{
m_error_string = "Cannot load library " + m_file_name + " " + get_error();
m_error_string = "Cannot load library " + m_file_name + " " + string(get_error());
}
else
{
Expand All @@ -163,7 +165,7 @@ bool library_private::unload_native()
{
if (dlclose(m_handle))
{
m_error_string = "Cannot unload library: '" + m_file_name + "'" + get_error();
m_error_string = "Cannot unload library: '" + m_file_name + "'" + string(get_error());
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/rttr/detail/method/method_wrapper_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void method_wrapper_base::create_signature_string() RTTR_NOEXCEPT
auto const_list = get_is_const();
for (const auto& param : param_list)
{
m_signature += param.get_type().get_name().to_string() + string(is_const_list[const_list[param.get_index()]]) + string(is_ref_list[ref_list[param.get_index()]]);
m_signature += string(param.get_type().get_name()) + string(is_const_list[const_list[param.get_index()]]) + string(is_ref_list[ref_list[param.get_index()]]);
if (param.get_index() < param_list.size() - 1)
m_signature += ", ";
}
Expand Down
2 changes: 1 addition & 1 deletion src/rttr/detail/type/type_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ RTTR_LOCAL std::unique_ptr<type_data> make_type_data()
raw_type_info<T>::get_type().m_type_data, wrapper_type_info<T>::get_type().m_type_data,
array_raw_type<T>::get_type().m_type_data,

::rttr::detail::get_type_name<T>().to_string(), ::rttr::detail::get_type_name<T>(),
std::string{::rttr::detail::get_type_name<T>()}, ::rttr::detail::get_type_name<T>(),

get_size_of<T>::value(),
pointer_count<T>::value,
Expand Down
8 changes: 4 additions & 4 deletions src/rttr/detail/type/type_register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ static void move_pointer_and_ref_to_type(std::string& type_name)

static std::string normalize_orig_name(string_view name)
{
std::string normalized_name = name.to_string();
std::string normalized_name(name);

move_pointer_and_ref_to_type(normalized_name);
return normalized_name;
Expand Down Expand Up @@ -695,15 +695,15 @@ std::string type_register_private::derive_name(const type& t)
auto custom_name = t.get_raw_array_type().get_name();
auto raw_name_orig = normalize_orig_name( t.get_raw_array_type().get_full_name());

return derive_name_impl(src_name_orig, raw_name_orig, custom_name.to_string());
return derive_name_impl(src_name_orig, raw_name_orig, string(custom_name));
}
else if (t != t.get_raw_type())
{
auto src_name_orig = normalize_orig_name(t.get_full_name());
auto custom_name = t.get_raw_type().get_name();
auto raw_name_orig = normalize_orig_name( t.get_raw_type().get_full_name());

return derive_name_impl(src_name_orig, raw_name_orig, custom_name.to_string());
return derive_name_impl(src_name_orig, raw_name_orig, string(custom_name));
}
else
{
Expand All @@ -718,7 +718,7 @@ void type_register_private::register_custom_name(type& t, string_view custom_nam
if (!t.is_valid())
return;

update_custom_name(custom_name.to_string(), t);
update_custom_name(string(custom_name), t);

// we have to make a copy of the list, because we also perform an insertion with 'update_custom_name'
auto tmp_type_list = m_custom_name_to_id.value_data();
Expand Down
2 changes: 1 addition & 1 deletion src/rttr/detail/variant/variant_data_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ struct convert_from_enum

static RTTR_INLINE bool to(const T& from, std::string& to)
{
to = get_enumeration_name(from).to_string();
to = std::string(get_enumeration_name(from));
return (to.empty() == false);
}

Expand Down
4 changes: 2 additions & 2 deletions src/rttr/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class library_manager
auto& manager = get_instance();
std::lock_guard<std::mutex> lock(manager.m_library_mutex);

auto file_as_string = file_name.to_string();
std::string file_as_string(file_name);
auto itr = manager.m_library_map.find(file_as_string);
if (itr != manager.m_library_map.end())
return itr->second;
Expand All @@ -73,7 +73,7 @@ class library_manager
auto& manager = get_instance();
std::lock_guard<std::mutex> lock(manager.m_library_mutex);

auto itr = manager.m_library_map.find(item->get_file_name().to_string()); // because we use string_view to find the item
auto itr = manager.m_library_map.find(std::string(item->get_file_name())); // because we use string_view to find the item
if (itr != manager.m_library_map.end())
manager.m_library_map.erase(itr);
}
Expand Down
22 changes: 12 additions & 10 deletions src/rttr/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@
#define RTTR_STRING_VIEW_H_

#include "rttr/detail/base/core_prerequisites.h"

#include <string>
#include <ostream>

#ifdef RTTR_ENABLE_STD_STRING_VIEW

#include <string_view>
namespace rttr
{
template<typename... T>
using basic_string_view = std::basic_string_view<T...>;
using string_view = basic_string_view<char>;
}

#else

namespace rttr
{
/*!
* The class template \ref basic_string_view describes an non-owning reference to a
* constant contiguous sequence of char-like objects.
Expand Down Expand Up @@ -247,14 +256,6 @@ class basic_string_view
template<typename Allocator>
explicit operator std::basic_string<CharT, Traits, Allocator>() const;

/*!
* \brief Creates a `basic_string` with a copy of the content of the current view.
*
* \return A `basic_string` containing a copy of the characters of the current view.
*/
template<typename Allocator = std::allocator<CharT> >
std::basic_string<CharT, Traits> to_string(const Allocator& a = Allocator()) const;

/*!
* \brief The function compares the two views by calling `Traits::compare(data(), v.data(), length)`,
* where \p length is the small of `size()` and `v.size()`.
Expand Down Expand Up @@ -497,4 +498,5 @@ using string_view = basic_string_view<char>;

#include "rttr/detail/impl/string_view_impl.h"

#endif // RTTR_ENABLE_STD_STRING_VIEW
#endif // RTTR_STRING_VIEW_H_