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"
|
|
|
|
|
|
|
|
#ifndef LAGI_PRE
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#endif
|
|
|
|
|
2011-12-22 22:10:22 +01:00
|
|
|
namespace {
|
|
|
|
using namespace json;
|
|
|
|
|
|
|
|
class CastVisitorBase : public Visitor, public ConstVisitor {
|
|
|
|
void Visit(Array&) { }
|
|
|
|
void Visit(Object&) { }
|
2011-12-22 22:12:25 +01:00
|
|
|
void Visit(Integer&) { }
|
|
|
|
void Visit(Double&) { }
|
2011-12-22 22:10:22 +01:00
|
|
|
void Visit(String&) { }
|
|
|
|
void Visit(Boolean&) { }
|
|
|
|
void Visit(Null&) { is_null = true; }
|
|
|
|
void Visit(Array const&) { }
|
|
|
|
void Visit(Object const&) { }
|
2011-12-22 22:12:25 +01:00
|
|
|
void Visit(Integer const&) { }
|
|
|
|
void Visit(Double const&) { }
|
2011-12-22 22:10:22 +01:00
|
|
|
void Visit(String const&) { }
|
|
|
|
void Visit(Boolean const&) { }
|
|
|
|
void Visit(Null const&) { is_null = true; }
|
|
|
|
public:
|
|
|
|
bool is_null;
|
|
|
|
CastVisitorBase() : is_null(false) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct CastVisitor : public CastVisitorBase {
|
|
|
|
T *element;
|
|
|
|
CastVisitor() : element(0) { }
|
|
|
|
void Visit(T& ele) { element = &ele; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
namespace json
|
|
|
|
{
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
// UnknownElement members
|
|
|
|
class UnknownElement::Imp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~Imp() {}
|
|
|
|
virtual Imp* Clone() const = 0;
|
|
|
|
|
|
|
|
virtual bool Compare(const Imp& imp) const = 0;
|
|
|
|
|
|
|
|
virtual void Accept(ConstVisitor& visitor) const = 0;
|
|
|
|
virtual void Accept(Visitor& visitor) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename ElementTypeT>
|
|
|
|
class UnknownElement::Imp_T : public UnknownElement::Imp
|
|
|
|
{
|
|
|
|
public:
|
2011-12-22 22:10:22 +01:00
|
|
|
Imp_T(const ElementTypeT& element) : m_Element(element) { }
|
|
|
|
Imp* Clone() const { return new Imp_T<ElementTypeT>(*this); }
|
2011-12-22 22:10:10 +01:00
|
|
|
|
2011-12-22 22:10:22 +01: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
|
|
|
|
2011-12-22 22:10:22 +01:00
|
|
|
bool Compare(const Imp& imp) const
|
2011-12-22 22:10:10 +01:00
|
|
|
{
|
2011-12-22 22:10:22 +01:00
|
|
|
CastVisitor<const ElementTypeT> castVisitor;
|
2011-12-22 22:10:10 +01:00
|
|
|
imp.Accept(castVisitor);
|
|
|
|
return castVisitor.element && m_Element == *castVisitor.element;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ElementTypeT m_Element;
|
|
|
|
};
|
|
|
|
|
|
|
|
UnknownElement::UnknownElement() : m_pImp( new Imp_T<Null>( Null() ) ) {}
|
|
|
|
UnknownElement::UnknownElement(const UnknownElement& unknown) : m_pImp( unknown.m_pImp->Clone()) {}
|
|
|
|
UnknownElement::UnknownElement(const Object& object) : m_pImp( new Imp_T<Object>(object) ) {}
|
|
|
|
UnknownElement::UnknownElement(const Array& array) : m_pImp( new Imp_T<Array>(array) ) {}
|
2011-12-22 22:12:25 +01:00
|
|
|
UnknownElement::UnknownElement(double number) : m_pImp( new Imp_T<Double>(number) ) {}
|
|
|
|
UnknownElement::UnknownElement(int number) : m_pImp( new Imp_T<Integer>(number) ) {}
|
|
|
|
UnknownElement::UnknownElement(int64_t number) : m_pImp( new Imp_T<Integer>(number) ) {}
|
|
|
|
UnknownElement::UnknownElement(long number) : m_pImp( new Imp_T<Integer>(number) ) {}
|
2011-12-22 22:10:10 +01:00
|
|
|
UnknownElement::UnknownElement(bool boolean) : m_pImp( new Imp_T<Boolean>(boolean) ) {}
|
|
|
|
UnknownElement::UnknownElement(const char *string) : m_pImp( new Imp_T<String>(string) ) {}
|
|
|
|
UnknownElement::UnknownElement(const String& string) : m_pImp( new Imp_T<String>(string) ) {}
|
|
|
|
UnknownElement::UnknownElement(const Null& null) : m_pImp( new Imp_T<Null>(null) ) {}
|
|
|
|
|
|
|
|
UnknownElement::~UnknownElement() { delete m_pImp; }
|
|
|
|
|
2011-12-22 22:10:22 +01:00
|
|
|
UnknownElement::operator Object const&() const { return CastTo<Object>(); }
|
|
|
|
UnknownElement::operator Array const&() const { return CastTo<Array>(); }
|
2011-12-22 22:12:25 +01:00
|
|
|
UnknownElement::operator Integer const&() const { return CastTo<Integer>(); }
|
|
|
|
UnknownElement::operator Double const&() const { return CastTo<Double>(); }
|
2011-12-22 22:10:22 +01:00
|
|
|
UnknownElement::operator Boolean const&() const { return CastTo<Boolean>(); }
|
|
|
|
UnknownElement::operator String const&() const { return CastTo<String>(); }
|
|
|
|
UnknownElement::operator Null const&() const { return CastTo<Null>(); }
|
|
|
|
|
|
|
|
UnknownElement::operator Object&() { return CastTo<Object>(); }
|
|
|
|
UnknownElement::operator Array&() { return CastTo<Array>(); }
|
2011-12-22 22:12:25 +01:00
|
|
|
UnknownElement::operator Integer&() { return CastTo<Integer>(); }
|
|
|
|
UnknownElement::operator Double&() { return CastTo<Double>(); }
|
2011-12-22 22:10:22 +01:00
|
|
|
UnknownElement::operator Boolean&() { return CastTo<Boolean>(); }
|
|
|
|
UnknownElement::operator String&() { return CastTo<String>(); }
|
|
|
|
UnknownElement::operator Null&() { return CastTo<Null>(); }
|
|
|
|
|
|
|
|
UnknownElement& UnknownElement::operator =(const UnknownElement& unknown)
|
2011-12-22 22:10:10 +01:00
|
|
|
{
|
|
|
|
delete m_pImp;
|
|
|
|
m_pImp = unknown.m_pImp->Clone();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:22 +01:00
|
|
|
UnknownElement& UnknownElement::operator[](const std::string& key)
|
2011-12-22 22:10:10 +01:00
|
|
|
{
|
2011-12-22 22:10:22 +01:00
|
|
|
return CastTo<Object>()[key];
|
2011-12-22 22:10:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const UnknownElement& UnknownElement::operator[] (const std::string& key) const
|
|
|
|
{
|
|
|
|
// throws if we aren't an object
|
|
|
|
Object const& obj = CastTo<Object>();
|
|
|
|
Object::const_iterator it = obj.find(key);
|
|
|
|
if (it == obj.end())
|
|
|
|
throw Exception("Object member not found: " + key);
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:22 +01:00
|
|
|
UnknownElement& UnknownElement::operator[](size_t index) { return CastTo<Array>()[index]; }
|
|
|
|
UnknownElement const& UnknownElement::operator[](size_t index) const { return CastTo<Array>()[index]; }
|
2011-12-22 22:10:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
template <typename ElementTypeT>
|
2011-12-22 22:10:22 +01:00
|
|
|
ElementTypeT const& UnknownElement::CastTo() const
|
2011-12-22 22:10:10 +01:00
|
|
|
{
|
2011-12-22 22:10:22 +01:00
|
|
|
CastVisitor<const ElementTypeT> castVisitor;
|
2011-12-22 22:10:32 +01:00
|
|
|
Accept(castVisitor);
|
2011-12-22 22:10:10 +01:00
|
|
|
if (!castVisitor.element)
|
|
|
|
throw Exception("Bad cast");
|
|
|
|
return *castVisitor.element;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ElementTypeT>
|
2011-12-22 22:10:22 +01:00
|
|
|
ElementTypeT& UnknownElement::CastTo()
|
2011-12-22 22:10:10 +01:00
|
|
|
{
|
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); }
|
|
|
|
|
|
|
|
|
|
|
|
bool UnknownElement::operator == (const UnknownElement& element) const
|
|
|
|
{
|
|
|
|
return m_pImp->Compare(*element.m_pImp);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace
|