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