Skip to content

Commit

Permalink
Using homemade unique_ptr for C++98 compliancy
Browse files Browse the repository at this point in the history
This homemade unique_ptr is oncly a templated pointer container for which
inner pointer never changes value but is copying other's value when
copying. I recommend not to use outside of picojson.h scope
  • Loading branch information
gbittoun committed Nov 18, 2017
1 parent abcdee0 commit 9bfff62
Showing 1 changed file with 91 additions and 17 deletions.
108 changes: 91 additions & 17 deletions picojson.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
Expand Down Expand Up @@ -1137,42 +1136,109 @@ inline bool operator!=(const value &x, const value &y) {
return !(x == y);
}

// Minimal implementation of unique_ptr for C++98 compliancy

template<typename T>
class unique_ptr
{
public:
unique_ptr() :
ptr(new T())
{
}

template<typename U>
unique_ptr(const U & v) :
ptr(new U(v))
{
}

~unique_ptr()
{
delete ptr;
}

// Copy constructor makes a copy of the inside object, not to be used outside of picojson
unique_ptr(const unique_ptr & other) :
ptr(new T(*other.ptr))
{
}

// Equality operator makes a copy of the inside object, not to be used outside of picojson
unique_ptr & operator=(const unique_ptr & other)
{
*ptr = *other.ptr;
return *this;
}

T & operator*()
{
return *ptr;
}

const T & operator*() const
{
return *ptr;
}

T * operator->()
{
return ptr;
}

const T * operator->() const
{
return ptr;
}

private:
T * ptr;
};

class value_holder
{
public:
virtual value & get() = 0;
virtual const value & get() const = 0;

virtual ~value_holder() {};
};

class value_ptr_holder : public value_holder
{
public:
value_ptr_holder() :
value_ptr(std::make_unique<value>())
value_ptr()
{
}

value_ptr_holder(bool b) :
value_ptr(std::make_unique<value>(b))
value_ptr(value(b))
{
}

value_ptr_holder(const std::string & s) :
value_ptr(std::make_unique<value>(s))
value_ptr(value(s))
{
}

value_ptr_holder(double d) :
value_ptr(std::make_unique<value>(d))
value_ptr(value(d))
{
}

const value & get() const
{
return *value_ptr;
}

value & get()
{
return *value_ptr;
}

private:
std::unique_ptr<picojson::value> value_ptr;
unique_ptr<picojson::value> value_ptr;
};

class value_ref_holder : public value_holder
Expand All @@ -1187,6 +1253,10 @@ class value_ref_holder : public value_holder
return value_ref;
}

const value & get() const {
return value_ref;
}

private:
value & value_ref;
};
Expand All @@ -1198,44 +1268,49 @@ class easyjson
//Constructors

easyjson() :
holder(std::make_unique<value_ptr_holder>()) {
holder(value_ptr_holder()) {
}

easyjson(picojson::value & v) :
holder(std::make_unique<value_ref_holder>(v)) {
holder(value_ref_holder(v)) {
}

easyjson(bool b):
holder(std::make_unique<value_ptr_holder>(b)) {
holder(value_ptr_holder(b)) {
}

easyjson(const char * c):
holder(std::make_unique<value_ptr_holder>(std::string(c))) {
holder(value_ptr_holder(std::string(c))) {
}

easyjson(const std::string & s):
holder(std::make_unique<value_ptr_holder>(s)) {
holder(value_ptr_holder(s)) {
}

easyjson(int value):
holder(std::make_unique<value_ptr_holder>(double(value))) {
holder(value_ptr_holder(double(value))) {
}

easyjson(double d):
holder(std::make_unique<value_ptr_holder>(d)) {
holder(value_ptr_holder(d)) {
}

// Copy constructor

easyjson(const easyjson & other) :
holder(std::make_unique<value_ptr_holder>()) {
holder(value_ptr_holder()) {
get_value() = other.get_value();
}


// picojson::value accessor

value & get_value() const
const value & get_value() const
{
return holder->get();
}

value & get_value()
{
return holder->get();
}
Expand Down Expand Up @@ -1274,7 +1349,6 @@ class easyjson
}

easyjson & operator=(const easyjson & other) {
holder = std::make_unique<value_ptr_holder>();
get_value() = other.get_value();
return *this;
}
Expand Down Expand Up @@ -1309,7 +1383,7 @@ class easyjson
}

private:
std::unique_ptr<value_holder> holder;
unique_ptr<value_holder> holder;
};

}
Expand Down

0 comments on commit 9bfff62

Please sign in to comment.