matrix_thing/include/raii/binary_string_conv.hpp

54 lines
2.1 KiB
C++

#ifndef RAII_BINARY_STRING_CONV_HPP
#define RAII_BINARY_STRING_CONV_HPP
#include "raii/string.hpp"
#include "raii/binary.hpp"
namespace raii{
template<class Al, class Str = raii::string, typename std::enable_if<raii::detail::is_concrete_string<Str>::value,void>::type* = nullptr>
auto binary_to_string(const binary_data<Al>& b){
Str s(b.size()+1);
memcpy(s.get(), b.get(), b.size());
s[b.size()] = 0;
return s;
}
template<class Al, class Str = raii::string, typename std::enable_if<raii::detail::is_concrete_string<Str>::value && std::is_same<typename std::decay<Al>::type,typename Str::allocator_type>::value,void>::type* = nullptr>
auto binary_to_string(binary_data<Al>&& b){
Str s;
s.reset(b.get(), b.size());
b.release();
return s;
}
template<class Bin = raii::binary, typename std::enable_if<raii::detail::is_binary_type<Bin>::value,void>::type* = nullptr>
auto string_to_binary(const string_base& s){
Bin b(s.length()+1);
b.append(s.get(), s.length()+1);
return b;
}
template<class Al, class Bin = raii::binary, typename std::enable_if<raii::detail::is_binary_type<Bin>::value && std::is_same<typename std::decay<Al>::type,typename Bin::allocator_type>::value,void>::type* = nullptr>
auto string_to_binary(string_intermediary<Al>&& s){
Bin b;
b.reset(s.get(), s.length()+1);
s.release();
return b;
}
}
template<class L, class R, typename std::enable_if<raii::detail::is_binary_type<L>::value && raii::detail::is_concrete_string<R>::value,void>::type* = nullptr>
decltype(auto) operator+=(L& l, R&& r){
l.append(r.get(), r.length()+1);
return l;
}
template<class L, class R, typename std::enable_if<raii::detail::is_binary_type<L>::value && raii::detail::is_string<R>::value && !raii::detail::is_concrete_string<R>::value,void>::type* = nullptr>
decltype(auto) operator+=(L& l, R&& r){
raii::string concrete = r;
return (l = concrete);
}
template<class L, class R, typename std::enable_if<raii::detail::is_concrete_string<L>::value && raii::detail::is_binary_type<R>::value,void>::type* = nullptr>
decltype(auto) operator+=(L& l, R&& r){
l.resize(l.length(), r.size() + 1);
memcpy(l.get()+l.length()+1, r.get(), r.size());
return l;
}
#endif