67 lines
1.9 KiB
C++
67 lines
1.9 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_RJP_STRING_HPP
|
|
#define RAII_RJP_STRING_HPP
|
|
|
|
#include <rjp.h>
|
|
#include "raii/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 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 string_intermediary<detail::rjp_allocator>::string_intermediary;
|
|
rjp_string(RJP_value* r):
|
|
string_intermediary<detail::rjp_allocator>(r ? std::exchange(r->string.value, nullptr) : nullptr, r ? r->string.length : 0, r ? r->string.length : 0){}
|
|
using string_intermediary<detail::rjp_allocator>::operator=;
|
|
rjp_string& operator=(RJP_value* r){
|
|
if(!r)
|
|
return *this;
|
|
reset();
|
|
m_data = std::exchange(r->string.value, nullptr);
|
|
m_length = r->string.length;
|
|
m_cap = r->string.length;
|
|
return *this;
|
|
}
|
|
};
|
|
rjp_string rjp_string_from_key(RJP_value* val);
|
|
}
|
|
|
|
#endif
|