290 lines
10 KiB
C++
290 lines
10 KiB
C++
/**
|
|
This file is a part of rexy's matrix client
|
|
Copyright (C) 2019 rexy712
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program 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 Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef RAII_STRING_BASE_HPP
|
|
#define RAII_STRING_BASE_HPP
|
|
|
|
#include <type_traits> //is_same, integral_contant, enable_if, etc
|
|
#include <utility> //forward
|
|
#include <cstdlib> //size_t
|
|
|
|
namespace raii{
|
|
|
|
class string_expr{};
|
|
|
|
//Base of all RAII strings. Its use is allowing passing of raii strings to functions without knowing the exact type
|
|
class string_base : public string_expr
|
|
{
|
|
protected:
|
|
size_t m_length = 0;
|
|
char* m_data = nullptr;
|
|
|
|
protected:
|
|
constexpr string_base(void) = default;
|
|
constexpr string_base(size_t len):
|
|
m_length(len){}
|
|
//Initialize without copying
|
|
constexpr string_base(char* data, size_t len):
|
|
m_length(len), m_data(data){}
|
|
//Copy ctor (do nothing)
|
|
string_base(const string_base&){}
|
|
~string_base(void) = default;
|
|
|
|
public:
|
|
//Stop managing stored pointer. Does not free.
|
|
char* release(void);
|
|
|
|
//Length of string not including null terminator
|
|
constexpr size_t length(void)const{return m_length;}
|
|
//direct access to managed pointer
|
|
constexpr char* get(void){return m_data;}
|
|
constexpr const char* get(void)const{return m_data;}
|
|
constexpr operator char*(void){return m_data;}
|
|
constexpr operator const char*(void)const{return m_data;}
|
|
//true if m_data is not null
|
|
constexpr operator bool(void)const{return m_data;}
|
|
|
|
char& operator[](size_t i);
|
|
const char& operator[](size_t i)const;
|
|
};
|
|
|
|
|
|
//Supplies all functions that string_base can't implement
|
|
template<class Allocator>
|
|
class string_intermediary : public string_base
|
|
{
|
|
public:
|
|
using allocator_type = Allocator;
|
|
|
|
public:
|
|
string_intermediary(void) = default;
|
|
string_intermediary(char* data, size_t len);
|
|
string_intermediary(const char* data, size_t len);
|
|
string_intermediary(const char* data);
|
|
string_intermediary(size_t len);
|
|
|
|
//normal copy and move ctors
|
|
string_intermediary(const string_intermediary& b);
|
|
string_intermediary(string_intermediary&& s);
|
|
|
|
string_intermediary(const string_base& b);
|
|
|
|
//dtor
|
|
~string_intermediary(void);
|
|
|
|
string_intermediary& operator=(const string_intermediary& s);
|
|
string_intermediary& operator=(string_intermediary&& s);
|
|
//Copy from c string
|
|
string_intermediary& operator=(const char* c);
|
|
//Copy from other string_base
|
|
string_intermediary& operator=(const string_base& s);
|
|
|
|
//Replace managed pointer. Frees existing value
|
|
void reset(char* val = nullptr);
|
|
void reset(char* val, size_t len);
|
|
bool resize(size_t newsize);
|
|
void append(const char* data, size_t len);
|
|
void append(const char* data);
|
|
void append(const string_base& s);
|
|
|
|
private:
|
|
string_intermediary& _copy_string(const char* s, size_t len);
|
|
};
|
|
|
|
//Like an expression template but not really
|
|
template<class Left, class Right>
|
|
class string_cat_expr : public string_expr
|
|
{
|
|
private:
|
|
Left m_l;
|
|
Right m_r;
|
|
|
|
public:
|
|
template<class T, class U>
|
|
constexpr string_cat_expr(T&& l, U&& r);
|
|
constexpr string_cat_expr(const string_cat_expr& s);
|
|
constexpr string_cat_expr(string_cat_expr&& s);
|
|
|
|
constexpr size_t length(void)const;
|
|
template<class Alloc>
|
|
operator string_intermediary<Alloc>(void);
|
|
constexpr const Left& left(void)const;
|
|
constexpr const Right& right(void)const;
|
|
};
|
|
|
|
class static_string : public string_base
|
|
{
|
|
public:
|
|
constexpr static_string(void) = default;
|
|
template<size_t N>
|
|
constexpr static_string(const char(&str)[N]);
|
|
constexpr static_string(const char* str, size_t len);
|
|
static_string(const char* c);
|
|
constexpr static_string(const static_string& s);
|
|
constexpr static_string(static_string&& s);
|
|
~static_string(void) = default;
|
|
|
|
static_string& operator=(const char* c);
|
|
static_string& operator=(const static_string& s);
|
|
static_string& operator=(static_string&&) = delete;
|
|
};
|
|
|
|
|
|
namespace detail{
|
|
std::true_type is_string_helper(string_expr);
|
|
std::false_type is_string_helper(...);
|
|
template<class T>
|
|
struct is_string{
|
|
static constexpr bool value = std::is_same<std::true_type,decltype(is_string_helper(std::declval<T>()))>::value;
|
|
};
|
|
std::true_type is_string_base(string_base*);
|
|
std::false_type is_string_base(...);
|
|
template<class T>
|
|
struct is_concrete_string{
|
|
static constexpr bool value = std::is_same<std::true_type,decltype(is_string_base(std::declval<typename std::decay<T>::type*>()))>::value;
|
|
};
|
|
template<class... Args>
|
|
std::true_type is_tuple_helper(std::tuple<Args...>);
|
|
std::false_type is_tuple_helper(...);
|
|
template<class T>
|
|
struct is_tuple{
|
|
static constexpr bool value = std::is_same<std::true_type,decltype(is_tuple_helper(std::declval<T>()))>::value;
|
|
};
|
|
|
|
//check for member function 'length'
|
|
template<class T>
|
|
struct has_len{
|
|
template<class U, class V>
|
|
struct check;
|
|
|
|
template<class U>
|
|
static std::true_type test(check<U,decltype(&U::length)>*);
|
|
template<class U>
|
|
static std::false_type test(...);
|
|
|
|
static constexpr bool value = std::is_same<std::true_type,decltype(test<T>(0))>::value;
|
|
};
|
|
template<class Targ>
|
|
struct appender
|
|
{
|
|
private:
|
|
Targ& m_targ;
|
|
size_t m_pos = 0;
|
|
public:
|
|
appender(Targ& t);
|
|
template<class L, class R>
|
|
void operator()(const string_cat_expr<L,R>& str);
|
|
void operator()(const string_base& str);
|
|
};
|
|
}
|
|
}
|
|
|
|
#include "raii/string_base.tpp"
|
|
|
|
namespace{
|
|
constexpr inline raii::static_string operator"" _ss(const char* str, size_t len){
|
|
return raii::static_string(str, len);
|
|
}
|
|
}
|
|
template<class Str1, class Str2, typename std::enable_if<raii::detail::is_concrete_string<Str1>::value&&raii::detail::is_concrete_string<Str2>::value,void>::type* = nullptr>
|
|
bool operator==(Str1&& left, Str2&& right){
|
|
return left && right && left.length() == right.length() && !strcmp(left.get(), right.get());
|
|
}
|
|
template<class Str1, class Str2, typename std::enable_if<raii::detail::is_concrete_string<Str1>::value&&raii::detail::is_concrete_string<Str2>::value,void>::type* = nullptr>
|
|
bool operator!=(Str1&& left, Str2&& right){
|
|
return !(left == right);
|
|
}
|
|
|
|
template<class Right, typename std::enable_if<raii::detail::is_string<Right>::value && std::is_rvalue_reference<Right&&>::value,void>::type* = nullptr>
|
|
constexpr auto operator+(const char* left, Right&& right){
|
|
return raii::string_cat_expr<raii::static_string,typename std::remove_reference<Right>::type>(raii::static_string(left), std::move(right));
|
|
}
|
|
template<class Right, typename std::enable_if<raii::detail::is_string<Right>::value && !std::is_rvalue_reference<Right&&>::value,void>::type* = nullptr>
|
|
constexpr auto operator+(const char* left, Right&& right){
|
|
return raii::string_cat_expr<raii::static_string,decltype(std::forward<Right>(right))>(raii::static_string(left), std::forward<Right>(right));
|
|
}
|
|
|
|
template<class Left, typename std::enable_if<raii::detail::is_string<Left>::value && std::is_rvalue_reference<Left&&>::value,void>::type* = nullptr>
|
|
constexpr auto operator+(Left&& left, const char* right){
|
|
return raii::string_cat_expr<typename std::remove_reference<Left>::type,raii::static_string>(std::move(left), raii::static_string(right));
|
|
}
|
|
template<class Left, typename std::enable_if<raii::detail::is_string<Left>::value && !std::is_rvalue_reference<Left&&>::value,void>::type* = nullptr>
|
|
constexpr auto operator+(Left&& left, const char* right){
|
|
return raii::string_cat_expr<decltype(std::forward<Left>(left)),raii::static_string>(std::forward<Left>(left), raii::static_string(right));
|
|
}
|
|
|
|
template<class Left,
|
|
class Right,
|
|
typename std::enable_if<
|
|
raii::detail::is_string<Left>::value &&
|
|
raii::detail::is_string<Right>::value &&
|
|
std::is_rvalue_reference<Left&&>::value &&
|
|
std::is_rvalue_reference<Right&&>::value,
|
|
void>::type* = nullptr>
|
|
constexpr auto operator+(Left&& l, Right&& r){
|
|
return raii::string_cat_expr<typename std::remove_reference<Left>::type,typename std::remove_reference<Right>::type>(std::move(l), std::move(r));
|
|
}
|
|
template<class Left,
|
|
class Right,
|
|
typename std::enable_if<
|
|
raii::detail::is_string<Left>::value &&
|
|
raii::detail::is_string<Right>::value &&
|
|
!std::is_rvalue_reference<Left&&>::value &&
|
|
!std::is_rvalue_reference<Right&&>::value,
|
|
void>::type* = nullptr>
|
|
constexpr auto operator+(Left&& l, Right&& r){
|
|
return raii::string_cat_expr<decltype(std::forward<Left>(l)),decltype(std::forward<Right>(r))>(std::forward<Left>(l), std::forward<Right>(r));
|
|
}
|
|
template<class Left,
|
|
class Right,
|
|
typename std::enable_if<
|
|
raii::detail::is_string<Left>::value &&
|
|
raii::detail::is_string<Right>::value &&
|
|
std::is_rvalue_reference<Left&&>::value &&
|
|
!std::is_rvalue_reference<Right&&>::value,
|
|
void>::type* = nullptr>
|
|
constexpr auto operator+(Left&& l, Right&& r){
|
|
return raii::string_cat_expr<typename std::remove_reference<Left>::type,decltype(std::forward<Right>(r))>(std::move(l), std::forward<Right>(r));
|
|
}
|
|
template<class Left,
|
|
class Right,
|
|
typename std::enable_if<
|
|
raii::detail::is_string<Left>::value &&
|
|
raii::detail::is_string<Right>::value &&
|
|
!std::is_rvalue_reference<Left&&>::value &&
|
|
std::is_rvalue_reference<Right&&>::value,
|
|
void>::type* = nullptr>
|
|
constexpr auto operator+(Left&& l, Right&& r){
|
|
return raii::string_cat_expr<decltype(std::forward<Left>(l)),typename std::remove_reference<Right>::type>(std::forward<Left>(l), std::move(r));
|
|
}
|
|
|
|
template<class Left, class Right, typename std::enable_if<raii::detail::is_string<Left>::value&&raii::detail::is_string<Right>::value,void>::type* = nullptr>
|
|
decltype(auto) operator+=(Left& l, Right&& r){
|
|
return l = (l + std::forward<Right>(r));
|
|
}
|
|
template<class Left, typename std::enable_if<raii::detail::is_string<Left>::value,void>::type* = nullptr>
|
|
decltype(auto) operator+=(Left& l, const char* r){
|
|
return l = (l + r);
|
|
}
|
|
|
|
#ifdef RAII_BINARY_HPP
|
|
#include "raii/binary_string_conv.hpp"
|
|
#endif
|
|
|
|
#endif
|