rjp/rjp++/include/rjp.hpp

408 lines
10 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>
{
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;
public:
value(void);
explicit value(RJP_value* val, bool managed);
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);
RJP_data_type type(void)const;
value parent(void)const;
bool is_child_of(const value&)const;
string to_json(int flags = RJP_FORMAT_PRETTY);
protected:
static value create_unmanaged(RJP_value* val);
static value create_managed(RJP_value* val);
static void remove_management(value& val);
static void add_management(value& val);
};
class integer : public value
{
public:
using underlying_type = RJP_int;
public:
using value::value;
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:
using value::value;
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:
using value::value;
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;
bool get(void)const;
void set(bool i);
};
class null : public value
{
public:
using value::value;
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:
using value::value;
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 steal(void);
void set(const rexy::string_base& str);
void set(string&& str);
};
class member : public value
{
public:
member(void) = default;
member(const member&) = default;
member(member&&) = default;
member(const value& val):
value(val){}
member(value&& val):
value(std::move(val)){}
using value::value;
~member(void) = default;
member& operator=(const member&) = default;
member& operator=(member&&) = default;
rexy::static_string key(void)const{return rexy::static_string(rjp_member_key(m_value)->value, rjp_member_key(m_value)->length);}
string steal_key(void){return string(m_value);}
};
class iterator_base
{
protected:
class value_wrapper
{
private:
member m_value;
public:
value_wrapper(RJP_value* value, bool managed):
m_value(value, managed){}
member* operator->(void){
return &m_value;
}
};
};
class object_iterator : protected iterator_base
{
protected:
RJP_object_iterator m_it;
public:
object_iterator(void);
object_iterator(RJP_value* v);
object_iterator(const object_iterator&) = delete;
object_iterator(object_iterator&& o);
~object_iterator(void);
object_iterator& operator=(const object_iterator&) = delete;
object_iterator& operator=(object_iterator&& o);
bool operator==(const object_iterator& o)const;
bool operator!=(const object_iterator& o)const;
member operator*(void)const;
value_wrapper operator->(void)const;
object_iterator& operator++(void);
};
class object : public value
{
public:
using iterator = object_iterator;
using const_iterator = const iterator;
public:
using value::value;
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(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);
}
member add(const rexy::string_base& key);
member add(const rexy::string_base& key, const rexy::string_base& val);
member add(const rexy::string_base& key, string&& val);
member add(string&& key, const rexy::string_base& val);
member add(string&& key, string&& val);
value remove(const rexy::string_base& key);
value& remove(value& val);
void destroy(const rexy::string_base& key);
void destroy(value&& val);
bool has_child(const value&);
iterator begin(void);
const_iterator begin(void)const;
iterator end(void)const;
value search(const rexy::string_base& key);
};
class array_iterator : protected iterator_base
{
protected:
RJP_array_iterator m_it;
public:
array_iterator(void) = default;
array_iterator(RJP_value* v);
array_iterator(const array_iterator&) = delete;
array_iterator(array_iterator&& a);
~array_iterator(void);
array_iterator& operator=(const array_iterator&) = delete;
array_iterator& operator=(array_iterator&& o);
bool operator==(const array_iterator& o)const;
bool operator!=(const array_iterator& o)const;
value operator*(void)const;
value_wrapper operator->(void)const;
array_iterator& operator++(void);
};
class array : public value
{
public:
using iterator = array_iterator;
using const_iterator = const iterator;
public:
using value::value;
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;
value add(void);
string_val add(const rexy::string_base& val);
string_val add(string&& val);
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);
}
value& remove(value& val);
void destroy(value&& val);
iterator begin(void);
iterator end(void);
const_iterator begin(void)const;
const_iterator end(void)const;
};
string to_json(const value& val, int format = RJP_FORMAT_PRETTY);
value parse_json(const rexy::string_base& str);
value parse_json(const char* str);
namespace detail{
template<class To, class From, bool = std::is_same<std::remove_reference_t<To>,std::remove_reference_t<From>>::value>
struct convert_helper;
template<class To, class From>
struct convert_helper<To,From,false>{
static To perform(From&& t){
return To(std::move(t));
}
};
template<class To, class From>
struct convert_helper<To,From&,false>{
static To perform(const From& t){
return To(const_cast<RJP_value*>(t.raw()), false);
}
};
template<class To, class From>
struct convert_helper<To,From,true>{
static decltype(auto) perform(From&& t){
return std::forward<From>(t);
}
};
}
template<class To, class From>
To convert(From&& from){
return detail::convert_helper<To,From>::perform(std::forward<From>(from));
}
namespace detail{
template<class To, class From>
struct get_ref{
using type = std::remove_reference_t<To>;
};
template<class To, class From>
struct get_ref<To, From&>{
using type = std::remove_reference_t<To>&;
};
template<class To, class From>
struct get_ref<To, From&&>{
using type = std::remove_reference_t<To>&&;
};
}
template<class To, class From>
decltype(auto) cast(From&& from){
return static_cast<typename detail::get_ref<To,From>::type>(std::forward<From>(from));
}
}
#endif