78 lines
2.2 KiB
C++
78 lines
2.2 KiB
C++
/**
|
|
This file is a part of rexy's matrix client
|
|
Copyright (C) 2019-2020 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_RJP_STRING_HPP
|
|
#define RAII_RJP_STRING_HPP
|
|
|
|
#include <rjp.h>
|
|
#include <rexy/string_base.hpp>
|
|
#include <utility> //exchange
|
|
#include <cstdlib> //memcpy
|
|
|
|
namespace raii{
|
|
|
|
namespace detail{
|
|
|
|
class rjp_allocator
|
|
{
|
|
public:
|
|
static void free(void* data);
|
|
static void* allocate(size_t size);
|
|
static void* copy(const void* data, size_t len);
|
|
};
|
|
|
|
}
|
|
|
|
//rjp allocated string
|
|
struct rjp_string : public rexy::string_intermediary<detail::rjp_allocator>
|
|
{
|
|
rjp_string(const rjp_string&) = default;
|
|
rjp_string(rjp_string&&) = default;
|
|
rjp_string& operator=(const rjp_string&) = default;
|
|
rjp_string& operator=(rjp_string&&) = default;
|
|
|
|
using rexy::string_intermediary<detail::rjp_allocator>::string_intermediary;
|
|
rjp_string(RJP_value* r):
|
|
rexy::string_intermediary<detail::rjp_allocator>(rexy::steal(r ? rjp_get_string(r)->value : nullptr),
|
|
r ? rjp_get_string(r)->length : 0,
|
|
r ? rjp_get_string(r)->length : 0)
|
|
{
|
|
if(r){
|
|
RJP_string* str = rjp_get_string(r);
|
|
str->value = nullptr;
|
|
str->length = 0;
|
|
rjp_set_null(r);
|
|
}
|
|
}
|
|
using rexy::string_intermediary<detail::rjp_allocator>::operator=;
|
|
rjp_string& operator=(RJP_value* r){
|
|
if(!r)
|
|
return *this;
|
|
reset();
|
|
RJP_string* str = rjp_get_string(r);
|
|
m_data = std::exchange(str->value, nullptr);
|
|
m_length = str->length;
|
|
m_cap = str->length;
|
|
str->length = 0;
|
|
return *this;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif
|