2011-12-22 22:10:10 +01:00
|
|
|
/**********************************************
|
|
|
|
|
|
|
|
License: BSD
|
|
|
|
Project Webpage: http://cajun-jsonapi.sourceforge.net/
|
|
|
|
Author: Terry Caton
|
|
|
|
***********************************************/
|
|
|
|
|
|
|
|
#include "libaegisub/cajun/elements.h"
|
|
|
|
|
|
|
|
#include "libaegisub/cajun/visitor.h"
|
|
|
|
|
2014-05-23 00:40:16 +02:00
|
|
|
#include <utility>
|
2011-12-22 22:10:10 +01:00
|
|
|
|
2011-12-22 22:10:22 +01:00
|
|
|
namespace {
|
2014-06-28 05:44:47 +02:00
|
|
|
using namespace json;
|
|
|
|
|
|
|
|
class CastVisitorBase : public Visitor {
|
|
|
|
void Visit(Array&) override { }
|
|
|
|
void Visit(Object&) override { }
|
|
|
|
void Visit(Integer&) override { }
|
|
|
|
void Visit(Double&) override { }
|
|
|
|
void Visit(String&) override { }
|
|
|
|
void Visit(Boolean&) override { }
|
|
|
|
void Visit(Null&) override { is_null = true; }
|
2011-12-22 22:10:10 +01:00
|
|
|
public:
|
2014-06-28 05:44:47 +02:00
|
|
|
bool is_null = false;
|
|
|
|
};
|
2011-12-22 22:10:10 +01:00
|
|
|
|
2014-06-28 05:44:47 +02:00
|
|
|
template<typename T>
|
|
|
|
struct CastVisitor final : public CastVisitorBase {
|
|
|
|
T *element = nullptr;
|
|
|
|
void Visit(T& ele) override { element = &ele; }
|
2011-12-22 22:10:10 +01:00
|
|
|
};
|
2014-06-28 05:44:47 +02:00
|
|
|
}
|
2011-12-22 22:10:10 +01:00
|
|
|
|
2014-06-28 05:44:47 +02:00
|
|
|
namespace json {
|
|
|
|
class UnknownElement::Imp {
|
2011-12-22 22:10:10 +01:00
|
|
|
public:
|
2014-06-28 05:44:47 +02:00
|
|
|
virtual ~Imp() {}
|
|
|
|
virtual void Accept(ConstVisitor& visitor) const = 0;
|
|
|
|
virtual void Accept(Visitor& visitor) = 0;
|
|
|
|
};
|
|
|
|
}
|
2011-12-22 22:10:10 +01:00
|
|
|
|
2014-06-28 05:44:47 +02:00
|
|
|
namespace {
|
|
|
|
template <typename ElementTypeT>
|
|
|
|
class Imp_T final : public UnknownElement::Imp {
|
|
|
|
ElementTypeT m_Element;
|
2011-12-22 22:10:10 +01:00
|
|
|
|
2014-06-28 05:44:47 +02:00
|
|
|
public:
|
|
|
|
Imp_T(ElementTypeT element) : m_Element(std::move(element)) { }
|
2011-12-22 22:10:10 +01:00
|
|
|
|
2014-06-28 05:44:47 +02:00
|
|
|
void Accept(ConstVisitor& visitor) const { visitor.Visit(m_Element); }
|
|
|
|
void Accept(Visitor& visitor) { visitor.Visit(m_Element); }
|
2011-12-22 22:10:10 +01:00
|
|
|
};
|
2014-06-28 05:44:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace json {
|
|
|
|
UnknownElement::UnknownElement() : m_pImp(new Imp_T<Null>(Null())) {}
|
|
|
|
UnknownElement::UnknownElement(UnknownElement&& unknown) : m_pImp(std::move(unknown.m_pImp)) {}
|
|
|
|
UnknownElement::UnknownElement(int number) : m_pImp(new Imp_T<Integer>(number)) {}
|
|
|
|
UnknownElement::UnknownElement(const char *string) : m_pImp(new Imp_T<String>(string)) {}
|
2011-12-22 22:10:10 +01:00
|
|
|
|
2013-10-27 15:15:39 +01:00
|
|
|
UnknownElement::~UnknownElement() { }
|
2011-12-22 22:12:46 +01:00
|
|
|
|
|
|
|
#define DEFINE_UE_TYPE(Type) \
|
2014-06-28 05:44:47 +02:00
|
|
|
UnknownElement::UnknownElement(Type val) : m_pImp(new Imp_T<Type>(std::move(val))) { } \
|
2011-12-22 22:12:46 +01:00
|
|
|
UnknownElement::operator Type const&() const { return CastTo<Type>(); } \
|
|
|
|
UnknownElement::operator Type&() { return CastTo<Type>(); }
|
|
|
|
|
2011-12-28 22:27:06 +01:00
|
|
|
DEFINE_UE_TYPE(Object)
|
|
|
|
DEFINE_UE_TYPE(Array)
|
|
|
|
DEFINE_UE_TYPE(Integer)
|
|
|
|
DEFINE_UE_TYPE(Double)
|
|
|
|
DEFINE_UE_TYPE(Boolean)
|
|
|
|
DEFINE_UE_TYPE(String)
|
|
|
|
DEFINE_UE_TYPE(Null)
|
2011-12-22 22:10:22 +01:00
|
|
|
|
2014-06-28 05:44:47 +02:00
|
|
|
UnknownElement& UnknownElement::operator=(UnknownElement&& unknown) {
|
|
|
|
m_pImp = std::move(unknown.m_pImp);
|
2011-12-22 22:10:10 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ElementTypeT>
|
2014-06-28 05:44:47 +02:00
|
|
|
ElementTypeT const& UnknownElement::CastTo() const {
|
|
|
|
CastVisitor<ElementTypeT> castVisitor;
|
|
|
|
const_cast<UnknownElement *>(this)->Accept(castVisitor);
|
2011-12-22 22:10:10 +01:00
|
|
|
if (!castVisitor.element)
|
|
|
|
throw Exception("Bad cast");
|
|
|
|
return *castVisitor.element;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ElementTypeT>
|
2014-06-28 05:44:47 +02:00
|
|
|
ElementTypeT& UnknownElement::CastTo() {
|
2011-12-22 22:10:22 +01:00
|
|
|
CastVisitor<ElementTypeT> castVisitor;
|
2011-12-22 22:10:10 +01:00
|
|
|
Accept(castVisitor);
|
2011-12-22 22:10:22 +01:00
|
|
|
|
|
|
|
// If this element is uninitialized, implicitly convert it to the desired type
|
|
|
|
if (castVisitor.is_null) {
|
2011-12-22 22:10:10 +01:00
|
|
|
*this = ElementTypeT();
|
2011-12-22 22:10:22 +01:00
|
|
|
return *this;
|
2011-12-22 22:10:10 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:22 +01:00
|
|
|
// Otherwise throw an exception
|
|
|
|
if (!castVisitor.element)
|
|
|
|
throw Exception("Bad cast");
|
|
|
|
return *castVisitor.element;
|
2011-12-22 22:10:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnknownElement::Accept(ConstVisitor& visitor) const { m_pImp->Accept(visitor); }
|
|
|
|
void UnknownElement::Accept(Visitor& visitor) { m_pImp->Accept(visitor); }
|
|
|
|
}
|