Skip to content

Commit

Permalink
simplify code (qicosmos#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Sep 4, 2023
1 parent 32fd321 commit 5e2b585
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions include/cinatra/string_resize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,30 @@ inline void resize(std::string& str, std::size_t sz) {
inline void resize(std::string& str, std::size_t sz) { str.resize(sz); }
#else

#if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
template <typename Money_t, Money_t std::string::*p>
template <typename Function, Function func_ptr>
class string_thief {
public:
friend void string_set_length_hacker(std::string& bank, std::size_t sz) {
(bank.*p)(sz);
}
};
#elif defined(_MSVC_STL_VERSION)
template <typename Money_t, Money_t std::string::*p>
class string_thief {
public:
friend void string_set_length_hacker(std::string& bank, std::size_t sz) {
(bank.*p)._Myval2._Mysize = sz;
friend void string_set_length_hacker(std::string& self, std::size_t sz) {
#if defined(_MSVC_STL_VERSION)
(self.*func_ptr)._Myval2._Mysize = sz;
#else
(self.*func_ptr)(sz);
#endif
}
};
#endif

#if defined(__GLIBCXX__) // libstdc++
template class string_thief<void(std::string::size_type),
template class string_thief<decltype(&std::string::_M_set_length),
&std::string::_M_set_length>;
#elif defined(_LIBCPP_VERSION)
template class string_thief<void(std::string::size_type),
template class string_thief<decltype(&std::string::__set_size),
&std::string::__set_size>;
#elif defined(_MSVC_STL_VERSION)
template class string_thief<decltype(std::string::_Mypair),
template class string_thief<decltype(&std::string::_Mypair),
&std::string::_Mypair>;
#endif

#if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) || \
defined(_MSVC_STL_VERSION)
void string_set_length_hacker(std::string& bank, std::size_t sz);
#endif
void string_set_length_hacker(std::string&, std::size_t);

inline void resize(std::string& str, std::size_t sz) {
#if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) || \
Expand Down

0 comments on commit 5e2b585

Please sign in to comment.