Separated rjp++'s single include file

This commit is contained in:
rexy712 2020-03-06 16:13:28 -08:00
parent 26e3cf4b2c
commit 59413d6d48
18 changed files with 522 additions and 437 deletions

66
rjp++/include/array.hpp Normal file
View File

@ -0,0 +1,66 @@
#ifndef RJP_ARRAY_HPP
#define RJP_ARRAY_HPP
#include "value.hpp"
#include "integral.hpp"
#include "string_val.hpp"
#include "iterator.hpp"
#include <rjp.h>
#include <rexy/string.hpp>
namespace rjp{
class array_iterator : protected detail::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);
value& remove(value& val);
void destroy(value&& val);
iterator begin(void);
iterator end(void);
const_iterator begin(void)const;
const_iterator end(void)const;
};
}
#endif

View File

@ -0,0 +1,94 @@
#ifndef RJP_INTEGRAL_HPP
#define RJP_INTEGRAL_HPP
#include "value.hpp"
#include <rjp.h>
namespace rjp{
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;
};
}
#endif

View File

@ -0,0 +1,27 @@
#ifndef RJP_ITERATOR_BASE_HPP
#define RJP_ITERATOR_BASE_HPP
#include <rjp.h>
#include "member.hpp"
#include "value.hpp"
namespace rjp::detail{
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;
}
};
};
}
#endif

34
rjp++/include/member.hpp Normal file
View File

@ -0,0 +1,34 @@
#ifndef RJP_MEMBER_HPP
#define RJP_MEMBER_HPP
#include "value.hpp"
#include "string.hpp"
#include <rexy/string.hpp>
#include <utility> //move
namespace rjp{
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);}
};
}
#endif

73
rjp++/include/object.hpp Normal file
View File

@ -0,0 +1,73 @@
#ifndef RJP_OBJECT_HPP
#define RJP_OBJECT_HPP
#include "value.hpp"
#include "integral.hpp"
#include "string.hpp"
#include "string_val.hpp"
#include "iterator.hpp"
#include <rjp.h>
#include <rexy/string.hpp>
namespace rjp{
class object_iterator : protected detail::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;
member add(const rexy::string_base& key);
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);
};
}
#endif

View File

@ -1,407 +1,14 @@
/**
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));
}
}
#include <rjp++/array.hpp>
#include <rjp++/integral.hpp>
#include <rjp++/iterator.hpp>
#include <rjp++/member.hpp>
#include <rjp++/object.hpp>
#include <rjp++/rjp_internal.hpp>
#include <rjp++/string.hpp>
#include <rjp++/string_val.hpp>
#include <rjp++/value.hpp>
#endif

View File

@ -0,0 +1,83 @@
/**
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_INTERNAL
#define RJP_HPP_INTERNAL
#include <rjp.h>
#include "string.hpp"
#include "value.hpp"
#include <utility> //move
namespace rjp{
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

34
rjp++/include/string.hpp Normal file
View File

@ -0,0 +1,34 @@
#ifndef RJP_STRING_HPP
#define RJP_STRING_HPP
#include <rexy/string.hpp>
#include <rjp.h>
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);
};
}
#endif

View File

@ -0,0 +1,42 @@
#ifndef RJP_STRING_VAL_HPP
#define RJP_STRING_VAL_HPP
#include <rjp.h>
#include <rexy/string.hpp>
#include "string.hpp"
#include "value.hpp"
namespace rjp{
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);
};
}
#endif

43
rjp++/include/value.hpp Normal file
View File

@ -0,0 +1,43 @@
#ifndef RJP_VALUE_HPP
#define RJP_VALUE_HPP
#include <rjp.h>
#include "string.hpp"
namespace rjp{
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);
};
}
#endif

View File

@ -16,7 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rjp.hpp>
#include "rjp.h"
#include "string.hpp"
#include <cstring> //memcpy
namespace rjp::detail{

View File

@ -16,7 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rjp.hpp>
#include "array.hpp"
#include "rjp.h"
#include <utility> //move, swap
namespace rjp{

View File

@ -16,7 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rjp.hpp>
#include "integral.hpp"
#include "rjp.h"
#include <utility> //move
namespace rjp{

View File

@ -16,8 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rjp.hpp>
#include <rexy/string_base.hpp>
#include "object.hpp"
#include "rjp.h"
#include <utility> //move
namespace rjp{
@ -39,30 +39,6 @@ namespace rjp{
RJP_value* newmemb = rjp_add_member_key_copy(m_value, key.get(), key.length());
return create_unmanaged(newmemb);
}
member object::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());
rjp_set_string_copy(newmemb, val.get(), val.length());
return create_unmanaged(newmemb);
}
member object::add(const rexy::string_base& key, string&& val){
RJP_value* newmemb = rjp_add_member_key_copy(m_value, key.get(), key.length());
auto len = val.length();
rjp_set_string(newmemb, val.release(), len);
return create_unmanaged(newmemb);
}
member object::add(string&& key, const rexy::string_base& val){
auto keylen = key.length();
RJP_value* newmemb = rjp_add_member(m_value, key.release(), keylen);
rjp_set_string_copy(newmemb, val.get(), val.length());
return create_unmanaged(newmemb);
}
member object::add(string&& key, string&& val){
auto keylen = key.length();
auto len = val.length();
RJP_value* newmemb = rjp_add_member(m_value, key.release(), keylen);
rjp_set_string(newmemb, val.release(), len);
return create_unmanaged(newmemb);
}
value object::remove(const rexy::string_base& key){
RJP_value* removed = rjp_remove_member_by_key(m_value, key.get());

View File

@ -16,7 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rjp.hpp>
#include "rjp_internal.hpp"
#include "value.hpp"
namespace rjp{

View File

@ -16,8 +16,9 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rexy/string_base.hpp>
#include <rjp.hpp>
#include <rexy/string.hpp>
#include "rjp.h"
#include "string.hpp"
#include <utility> //exchange
namespace rjp{

View File

@ -16,8 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rjp.hpp>
#include <rexy/string_base.hpp>
#include "string_val.hpp"
#include "rjp.h"
#include <utility> //move
namespace rjp{

View File

@ -16,7 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rjp.hpp>
#include "value.hpp"
#include "rjp.h"
#include <utility> //swap, move, exchange
namespace rjp{