rjp/rjp++/include/rjp.hpp
2020-03-03 17:56:36 -08:00

302 lines
7.9 KiB
C++

/**
rjp++
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RJP_HPP_INCLUDED
#define RJP_HPP_INCLUDED
#include <rjp.h>
#include <rexy/string.hpp>
#include <utility> //move
namespace rjp{
namespace detail{
struct allocator
{
static void free(void* data);
static void* allocate(size_t size);
static void* copy(const void* data, size_t len);
};
}
class string : public rexy::string_intermediary<detail::allocator>
{
protected:
public:
string(const string&) = default;
string(string&&) = default;
string& operator=(const string&) = default;
string& operator=(string&&) = default;
using rexy::string_intermediary<detail::allocator>::string_intermediary;
string(RJP_value* r);
using rexy::string_intermediary<detail::allocator>::operator=;
string& operator=(RJP_value* r);
};
class value
{
protected:
RJP_value* m_value;
bool m_managed = true;
protected:
value(RJP_value* val);
public:
value(void);
value(const value& val);
value(value&& val);
~value(void);
value& operator=(const value& val);
value& operator=(value&& val);
const RJP_value* raw(void)const;
RJP_value* raw(void);
protected:
static value create_unmanaged(RJP_value* val);
static void remove_management(value& val);
};
class integer : public value
{
public:
using underlying_type = RJP_int;
public:
integer(underlying_type i);
integer(void);
integer(const value& val);
integer(value&& val);
integer(const value& val, underlying_type i);
integer(value&& val, underlying_type i);
integer(const integer&) = default;
integer(integer&&) = default;
~integer(void) = default;
integer& operator=(const integer&) = default;
integer& operator=(integer&&) = default;
underlying_type get(void)const;
void set(underlying_type i);
};
class dfloat : public value
{
public:
using underlying_type = RJP_float;
public:
dfloat(underlying_type i);
dfloat(void);
dfloat(const value& val);
dfloat(value&& val);
dfloat(const value& val, underlying_type i);
dfloat(value&& val, underlying_type i);
dfloat(const dfloat&) = default;
dfloat(dfloat&&) = default;
~dfloat(void) = default;
dfloat& operator=(const dfloat&) = default;
dfloat& operator=(dfloat&&) = default;
underlying_type get(void)const;
void set(underlying_type i);
};
class boolean : public value
{
public:
using underlying_type = RJP_bool;
public:
boolean(underlying_type i);
boolean(void);
boolean(const value& val);
boolean(value&& val);
boolean(const value& val, underlying_type i);
boolean(value&& val, underlying_type i);
boolean(const boolean&) = default;
boolean(boolean&&) = default;
~boolean(void) = default;
boolean& operator=(const boolean&) = default;
boolean& operator=(boolean&&) = default;
underlying_type get(void)const;
void set(underlying_type i);
};
class null : public value
{
public:
null(void);
null(const value& val);
null(value&& val);
null(const null&) = default;
null(null&&) = default;
~null(void) = default;
null& operator=(const null&) = default;
null& operator=(null&&) = default;
};
class string_val : public value
{
public:
using underlying_type = RJP_string;
public:
string_val(const rexy::string_base& str);
string_val(string&& str);
string_val(void);
string_val(const value& val);
string_val(value&& val);
string_val(const value& val, const rexy::string_base& i);
string_val(value&& val, const rexy::string_base& i);
string_val(const value& val, string&& i);
string_val(value&& val, string&& i);
string_val(const string_val&) = default;
string_val(string_val&&) = default;
~string_val(void) = default;
string_val& operator=(const string_val&) = default;
string_val& operator=(string_val&&) = default;
rexy::static_string get(void)const&;
string get(void)&&;
void set(const rexy::string_base& str);
void set(string&& str);
};
class object : public value
{
public:
object(void);
object(const value& val);
object(value&& val);
object(const object&) = default;
object(object&&) = default;
~object(void) = default;
object& operator=(const object&) = default;
object& operator=(object&&) = default;
template<class T = value>
T add(const rexy::string_base& key){
RJP_value* newmemb = rjp_add_member_key_copy(m_value, key.get(), key.length());
return T(create_unmanaged(newmemb));
}
template<class T = value>
T add(rjp::string&& key, typename T::underlying_type i){
RJP_value* newmemb = rjp_add_member(m_value, key.get(), key.length());
key.release();
return T(create_unmanaged(newmemb), i);
}
template<class T = string_val>
string_val add(const rexy::string_base& key, const rexy::string_base& val){
RJP_value* newmemb = rjp_add_member_key_copy(m_value, key.get(), key.length());
return string_val(create_unmanaged(newmemb), val);
}
template<class T = string_val>
string_val add(const rexy::string_base& key, string&& val){
RJP_value* newmemb = rjp_add_member_key_copy(m_value, key.get(), key.length());
return string_val(create_unmanaged(newmemb), std::move(val));
}
template<class T = string_val>
string_val add(string&& key, const rexy::string_base& val){
auto keylen = key.length();
RJP_value* newmemb = rjp_add_member(m_value, key.release(), keylen);
return string_val(create_unmanaged(newmemb), val);
}
template<class T = string_val>
string_val add(string&& key, string&& val){
auto keylen = key.length();
RJP_value* newmemb = rjp_add_member(m_value, key.release(), keylen);
return string_val(create_unmanaged(newmemb), std::move(val));
}
template<class T = value>
T remove(const rexy::string_base& key){
RJP_value* removed = rjp_remove_member_by_key(m_value, key.get());
return T(value(removed));
}
template<class T = value, class Value>
T remove(Value&& val){
RJP_value* removed = rjp_remove_member(m_value, val.m_value);
return T(value(removed));
}
void destroy(const rexy::string_base& key);
void destroy(value&& val);
template<class T = value>
value& search(const rexy::string_base& key){
RJP_value* result = rjp_search_member(m_value, key.get());
return T(create_unmanaged(result));
}
};
class array : public value
{
public:
array(void);
array(const value& val);
array(value&& val);
array(const array&) = default;
array(array&&) = default;
~array(void) = default;
array& operator=(const array&) = default;
array& operator=(array&&) = default;
template<class T = value>
T add(void){
RJP_value* newelem = rjp_add_element(m_value);
return T(create_unmanaged(newelem));
}
template<class T = value>
T add(typename T::underlying_type i){
RJP_value* newelem = rjp_add_element(m_value);
return T(create_unmanaged(newelem), i);
}
template<class T = string_val>
string_val add(const rexy::string_base& val){
RJP_value* newelem = rjp_add_element(m_value);
return string_val(create_unmanaged(newelem), val);
}
template<class T = string_val>
string_val add(string&& val){
RJP_value* newelem = rjp_add_element(m_value);
return string_val(create_unmanaged(newelem), std::move(val));
}
template<class T = value, class Value>
T remove(Value&& val){
RJP_value* removed = rjp_remove_element(m_value, val.raw());
remove_management(val);
return T(value(removed));
}
void destroy(value&& val);
};
string to_json(const value& val, int format = RJP_FORMAT_PRETTY);
}
#endif