57 lines
1.3 KiB
C++
57 lines
1.3 KiB
C++
#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){
|
|
rjp_free(data);
|
|
}
|
|
static void* allocate(size_t size){
|
|
return rjp_alloc(size);
|
|
}
|
|
static void* copy(const void* data, size_t len){
|
|
char* tmp = reinterpret_cast<char*>(allocate(len));
|
|
memcpy(tmp, data, len-1);
|
|
tmp[len-1] = 0;
|
|
return tmp;
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
//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){}
|
|
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;
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|