From a78417177a0d7dac0cd521254340599739c35b4c Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Thu, 22 Dec 2011 21:10:10 +0000 Subject: [PATCH] Move cajun implementations from .inl files included by the headers to .cpp files Originally committed to SVN as r6004. --- .../libaegisub_vs2008.vcproj | 14 +- .../msbuild/libaegisub/libaegisub.vcxproj | 8 +- .../libaegisub/libaegisub.vcxproj.filters | 20 +- aegisub/libaegisub/Makefile | 3 + aegisub/libaegisub/common/cajun/elements.cpp | 174 ++++++++++++++++++ .../reader.inl => common/cajun/reader.cpp} | 49 ++--- .../writer.inl => common/cajun/writer.cpp} | 41 ++--- .../include/libaegisub/cajun/elements.h | 10 +- .../include/libaegisub/cajun/elements.inl | 172 ----------------- .../include/libaegisub/cajun/reader.h | 9 +- .../include/libaegisub/cajun/writer.h | 21 ++- aegisub/libaegisub/lagi_pre.h | 3 + aegisub/src/agi_pre.h | 3 - 13 files changed, 263 insertions(+), 264 deletions(-) create mode 100644 aegisub/libaegisub/common/cajun/elements.cpp rename aegisub/libaegisub/{include/libaegisub/cajun/reader.inl => common/cajun/reader.cpp} (85%) rename aegisub/libaegisub/{include/libaegisub/cajun/writer.inl => common/cajun/writer.cpp} (68%) delete mode 100644 aegisub/libaegisub/include/libaegisub/cajun/elements.inl diff --git a/aegisub/build/libaegisub_vs2008/libaegisub_vs2008.vcproj b/aegisub/build/libaegisub_vs2008/libaegisub_vs2008.vcproj index e125cbc06..e06fa785d 100644 --- a/aegisub/build/libaegisub_vs2008/libaegisub_vs2008.vcproj +++ b/aegisub/build/libaegisub_vs2008/libaegisub_vs2008.vcproj @@ -505,32 +505,32 @@ + + - - diff --git a/aegisub/build/msbuild/libaegisub/libaegisub.vcxproj b/aegisub/build/msbuild/libaegisub/libaegisub.vcxproj index a46e9a9ad..a262bd495 100644 --- a/aegisub/build/msbuild/libaegisub/libaegisub.vcxproj +++ b/aegisub/build/msbuild/libaegisub/libaegisub.vcxproj @@ -83,6 +83,9 @@ Create lagi_pre.h + + + @@ -107,8 +110,5 @@ - - - - \ No newline at end of file + diff --git a/aegisub/build/msbuild/libaegisub/libaegisub.vcxproj.filters b/aegisub/build/msbuild/libaegisub/libaegisub.vcxproj.filters index 59d3ba579..0ad646cf3 100644 --- a/aegisub/build/msbuild/libaegisub/libaegisub.vcxproj.filters +++ b/aegisub/build/msbuild/libaegisub/libaegisub.vcxproj.filters @@ -193,19 +193,19 @@ Source Files\Common + + cajun + + + cajun + + + cajun + Header Files - - cajun - - - cajun - - - cajun - - \ No newline at end of file + diff --git a/aegisub/libaegisub/Makefile b/aegisub/libaegisub/Makefile index 374ea516d..f76f7b3de 100644 --- a/aegisub/libaegisub/Makefile +++ b/aegisub/libaegisub/Makefile @@ -20,6 +20,9 @@ common/charset_conv.o: CXXFLAGS += $(CFLAGS_ICONV) unix/path.o: CXXFLAGS += -DP_DATA=\"$(P_DATA)\" -DP_DOC=\"$(P_DOC)\" -DP_LOCALE=\"$(P_LOCALE)\" SRC = \ + common/cajun/elements.cpp \ + common/cajun/reader.cpp \ + common/cajun/writer.cpp \ common/charset.cpp \ common/charset_conv.cpp \ common/charset_ucd.cpp \ diff --git a/aegisub/libaegisub/common/cajun/elements.cpp b/aegisub/libaegisub/common/cajun/elements.cpp new file mode 100644 index 000000000..c3f9b97ce --- /dev/null +++ b/aegisub/libaegisub/common/cajun/elements.cpp @@ -0,0 +1,174 @@ +/********************************************** + +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 +#include +#endif + +namespace json +{ + +///////////////////////// +// UnknownElement members +class CastVisitor : public ConstVisitor +{ + virtual void Visit(const Array&) { } + virtual void Visit(const Object&) { } + virtual void Visit(const Number&) { } + virtual void Visit(const String&) { } + virtual void Visit(const Boolean&) { } + virtual void Visit(const Null&) { } +}; + +template +class CastVisitor_T : public CastVisitor +{ +public: + const ElementTypeT *element; + CastVisitor_T() : element(0) { } + + // we don't know what this is, but it overrides one of the base's no-op functions + void Visit(const ElementTypeT& element) { this->element = &element; } +}; + +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 +class UnknownElement::Imp_T : public UnknownElement::Imp +{ +public: + Imp_T(const ElementTypeT& element) : m_Element(element) {} + virtual Imp* Clone() const { return new Imp_T(*this); } + + virtual void Accept(ConstVisitor& visitor) const { visitor.Visit(m_Element); } + virtual void Accept(Visitor& visitor) { visitor.Visit(m_Element); } + + virtual bool Compare(const Imp& imp) const + { + CastVisitor_T castVisitor; + imp.Accept(castVisitor); + return castVisitor.element && m_Element == *castVisitor.element; + } + +private: + ElementTypeT m_Element; +}; + +UnknownElement::UnknownElement() : m_pImp( new Imp_T( Null() ) ) {} +UnknownElement::UnknownElement(const UnknownElement& unknown) : m_pImp( unknown.m_pImp->Clone()) {} +UnknownElement::UnknownElement(const Object& object) : m_pImp( new Imp_T(object) ) {} +UnknownElement::UnknownElement(const Array& array) : m_pImp( new Imp_T(array) ) {} +UnknownElement::UnknownElement(double number) : m_pImp( new Imp_T(number) ) {} +UnknownElement::UnknownElement(int number) : m_pImp( new Imp_T(number) ) {} +UnknownElement::UnknownElement(long number) : m_pImp( new Imp_T(number) ) {} +UnknownElement::UnknownElement(bool boolean) : m_pImp( new Imp_T(boolean) ) {} +UnknownElement::UnknownElement(const char *string) : m_pImp( new Imp_T(string) ) {} +UnknownElement::UnknownElement(const String& string) : m_pImp( new Imp_T(string) ) {} +UnknownElement::UnknownElement(const Null& null) : m_pImp( new Imp_T(null) ) {} + +UnknownElement::~UnknownElement() { delete m_pImp; } + +UnknownElement::operator const Object& () const { return CastTo(); } +UnknownElement::operator const Array& () const { return CastTo(); } +UnknownElement::operator const Number& () const { return CastTo(); } +UnknownElement::operator const Boolean& () const { return CastTo(); } +UnknownElement::operator const String& () const { return CastTo(); } +UnknownElement::operator const Null& () const { return CastTo(); } + +UnknownElement::operator Object& () { return ConvertTo(); } +UnknownElement::operator Array& () { return ConvertTo(); } +UnknownElement::operator Number& () { return ConvertTo(); } +UnknownElement::operator Boolean& () { return ConvertTo(); } +UnknownElement::operator String& () { return ConvertTo(); } +UnknownElement::operator Null& () { return ConvertTo(); } + +UnknownElement& UnknownElement::operator = (const UnknownElement& unknown) +{ + delete m_pImp; + m_pImp = unknown.m_pImp->Clone(); + return *this; +} + +UnknownElement& UnknownElement::operator[] (const std::string& key) +{ + // the people want an object. make us one if we aren't already + return ConvertTo()[key]; +} + +const UnknownElement& UnknownElement::operator[] (const std::string& key) const +{ + // throws if we aren't an object + Object const& obj = CastTo(); + Object::const_iterator it = obj.find(key); + if (it == obj.end()) + throw Exception("Object member not found: " + key); + return it->second; +} + +UnknownElement& UnknownElement::operator[] (size_t index) +{ + // the people want an array. make us one if we aren't already + return ConvertTo()[index]; +} + +const UnknownElement& UnknownElement::operator[] (size_t index) const +{ + // throws if we aren't an array + return CastTo()[index]; +} + + +template +const ElementTypeT& UnknownElement::CastTo() const +{ + CastVisitor_T castVisitor; + m_pImp->Accept(castVisitor); + if (!castVisitor.element) + throw Exception("Bad cast"); + return *castVisitor.element; +} + +template +ElementTypeT& UnknownElement::ConvertTo() +{ + CastVisitor_T castVisitor; + Accept(castVisitor); + if (!castVisitor.element) + { + // we're not the right type. fix it & try again + *this = ElementTypeT(); + } + + return *this; +} + +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 diff --git a/aegisub/libaegisub/include/libaegisub/cajun/reader.inl b/aegisub/libaegisub/common/cajun/reader.cpp similarity index 85% rename from aegisub/libaegisub/include/libaegisub/cajun/reader.inl rename to aegisub/libaegisub/common/cajun/reader.cpp index 6f2cf36de..baf263c36 100644 --- a/aegisub/libaegisub/include/libaegisub/cajun/reader.inl +++ b/aegisub/libaegisub/common/cajun/reader.cpp @@ -6,9 +6,13 @@ Author: Terry Caton ***********************************************/ +#include "libaegisub/cajun/reader.h" + +#ifndef LAGI_PRE #include #include #include +#endif /* @@ -21,13 +25,12 @@ TODO: namespace json { - -inline std::istream& operator >> (std::istream& istr, UnknownElement& elementRoot) { +std::istream& operator >> (std::istream& istr, UnknownElement& elementRoot) { Reader::Read(elementRoot, istr); return istr; } -inline Reader::Location::Location() : +Reader::Location::Location() : m_nLine(0), m_nLineOffset(0), m_nDocOffset(0) @@ -102,13 +105,13 @@ public: /////////////////// // Reader (finally) -inline void Reader::Read(Object& object, std::istream& istr) { Read_i(object, istr); } -inline void Reader::Read(Array& array, std::istream& istr) { Read_i(array, istr); } -inline void Reader::Read(String& string, std::istream& istr) { Read_i(string, istr); } -inline void Reader::Read(Number& number, std::istream& istr) { Read_i(number, istr); } -inline void Reader::Read(Boolean& boolean, std::istream& istr) { Read_i(boolean, istr); } -inline void Reader::Read(Null& null, std::istream& istr) { Read_i(null, istr); } -inline void Reader::Read(UnknownElement& unknown, std::istream& istr) { Read_i(unknown, istr); } +void Reader::Read(Object& object, std::istream& istr) { Read_i(object, istr); } +void Reader::Read(Array& array, std::istream& istr) { Read_i(array, istr); } +void Reader::Read(String& string, std::istream& istr) { Read_i(string, istr); } +void Reader::Read(Number& number, std::istream& istr) { Read_i(number, istr); } +void Reader::Read(Boolean& boolean, std::istream& istr) { Read_i(boolean, istr); } +void Reader::Read(Null& null, std::istream& istr) { Read_i(null, istr); } +void Reader::Read(UnknownElement& unknown, std::istream& istr) { Read_i(unknown, istr); } template void Reader::Read_i(ElementTypeT& element, std::istream& istr) @@ -129,7 +132,7 @@ void Reader::Read_i(ElementTypeT& element, std::istream& istr) } } -inline void Reader::Scan(Tokens& tokens, InputStream& inputStream) +void Reader::Scan(Tokens& tokens, InputStream& inputStream) { while (EatWhiteSpace(inputStream), !inputStream.EOS()) { @@ -227,13 +230,13 @@ inline void Reader::Scan(Tokens& tokens, InputStream& inputStream) } -inline void Reader::EatWhiteSpace(InputStream& inputStream) +void Reader::EatWhiteSpace(InputStream& inputStream) { while (!inputStream.EOS() && ::isspace(inputStream.Peek())) inputStream.Get(); } -inline void Reader::MatchExpectedString(const std::string& sExpected, InputStream& inputStream) +void Reader::MatchExpectedString(const std::string& sExpected, InputStream& inputStream) { std::string::const_iterator it(sExpected.begin()), itEnd(sExpected.end()); @@ -249,7 +252,7 @@ inline void Reader::MatchExpectedString(const std::string& sExpected, InputStrea } -inline void Reader::MatchString(std::string& string, InputStream& inputStream) +void Reader::MatchString(std::string& string, InputStream& inputStream) { MatchExpectedString("\"", inputStream); @@ -287,7 +290,7 @@ inline void Reader::MatchString(std::string& string, InputStream& inputStream) } -inline void Reader::MatchNumber(std::string& sNumber, InputStream& inputStream) +void Reader::MatchNumber(std::string& sNumber, InputStream& inputStream) { const char sNumericChars[] = "0123456789.eE-+"; std::set numericChars; @@ -301,7 +304,7 @@ inline void Reader::MatchNumber(std::string& sNumber, InputStream& inputStream) } -inline void Reader::Parse(UnknownElement& element, Reader::TokenStream& tokenStream) +void Reader::Parse(UnknownElement& element, Reader::TokenStream& tokenStream) { if (tokenStream.EOS()) { throw ParseException("Unexpected end of token stream", Location(), Location()); // nowhere to point to @@ -360,7 +363,7 @@ inline void Reader::Parse(UnknownElement& element, Reader::TokenStream& tokenStr } -inline void Reader::Parse(Object& object, Reader::TokenStream& tokenStream) +void Reader::Parse(Object& object, Reader::TokenStream& tokenStream) { MatchExpectedToken(Token::TOKEN_OBJECT_BEGIN, tokenStream); @@ -396,7 +399,7 @@ inline void Reader::Parse(Object& object, Reader::TokenStream& tokenStream) } -inline void Reader::Parse(Array& array, Reader::TokenStream& tokenStream) +void Reader::Parse(Array& array, Reader::TokenStream& tokenStream) { MatchExpectedToken(Token::TOKEN_ARRAY_BEGIN, tokenStream); @@ -419,13 +422,13 @@ inline void Reader::Parse(Array& array, Reader::TokenStream& tokenStream) } -inline void Reader::Parse(String& string, Reader::TokenStream& tokenStream) +void Reader::Parse(String& string, Reader::TokenStream& tokenStream) { string = MatchExpectedToken(Token::TOKEN_STRING, tokenStream); } -inline void Reader::Parse(Number& number, Reader::TokenStream& tokenStream) +void Reader::Parse(Number& number, Reader::TokenStream& tokenStream) { const Token& currentToken = tokenStream.Peek(); // might need this later for throwing exception const std::string& sValue = MatchExpectedToken(Token::TOKEN_NUMBER, tokenStream); @@ -444,20 +447,20 @@ inline void Reader::Parse(Number& number, Reader::TokenStream& tokenStream) } -inline void Reader::Parse(Boolean& boolean, Reader::TokenStream& tokenStream) +void Reader::Parse(Boolean& boolean, Reader::TokenStream& tokenStream) { const std::string& sValue = MatchExpectedToken(Token::TOKEN_BOOLEAN, tokenStream); boolean = (sValue == "true"); } -inline void Reader::Parse(Null&, Reader::TokenStream& tokenStream) +void Reader::Parse(Null&, Reader::TokenStream& tokenStream) { MatchExpectedToken(Token::TOKEN_NULL, tokenStream); } -inline const std::string& Reader::MatchExpectedToken(Token::Type nExpected, Reader::TokenStream& tokenStream) +const std::string& Reader::MatchExpectedToken(Token::Type nExpected, Reader::TokenStream& tokenStream) { if (tokenStream.EOS()) { diff --git a/aegisub/libaegisub/include/libaegisub/cajun/writer.inl b/aegisub/libaegisub/common/cajun/writer.cpp similarity index 68% rename from aegisub/libaegisub/include/libaegisub/cajun/writer.inl rename to aegisub/libaegisub/common/cajun/writer.cpp index 9f5a0a51a..a9b939330 100644 --- a/aegisub/libaegisub/include/libaegisub/cajun/writer.inl +++ b/aegisub/libaegisub/common/cajun/writer.cpp @@ -6,9 +6,12 @@ Author: Terry Caton ***********************************************/ -#include "writer.h" +#include "libaegisub/cajun/writer.h" + +#ifndef LAGI_PRE #include #include +#endif /* @@ -21,20 +24,12 @@ TODO: namespace json { -inline Writer::Writer(std::ostream& ostr) : +Writer::Writer(std::ostream& ostr) : m_ostr(ostr), m_nTabDepth(0) {} -template -void Writer::Write(const ElementTypeT& element, std::ostream& ostr) -{ - Writer writer(ostr); - writer.Write(element); - ostr.flush(); // all done -} - -inline void Writer::Write(const Array& array) +void Writer::Write(const Array& array) { if (array.empty()) m_ostr << "[]"; @@ -59,7 +54,7 @@ inline void Writer::Write(const Array& array) } } -inline void Writer::Write(const Object& object) +void Writer::Write(const Object& object) { if (object.empty()) m_ostr << "{}"; @@ -83,17 +78,17 @@ inline void Writer::Write(const Object& object) } } -inline void Writer::Write(const Number& numberElement) +void Writer::Write(const Number& numberElement) { m_ostr << std::setprecision(20) << numberElement; } -inline void Writer::Write(const Boolean& booleanElement) +void Writer::Write(const Boolean& booleanElement) { m_ostr << (booleanElement ? "true" : "false"); } -inline void Writer::Write(const String& stringElement) +void Writer::Write(const String& stringElement) { m_ostr << '"'; @@ -118,21 +113,21 @@ inline void Writer::Write(const String& stringElement) m_ostr << '"'; } -inline void Writer::Write(const Null& ) +void Writer::Write(const Null& ) { m_ostr << "null"; } -inline void Writer::Write(const UnknownElement& unknown) +void Writer::Write(const UnknownElement& unknown) { unknown.Accept(*this); } -inline void Writer::Visit(const Array& array) { Write(array); } -inline void Writer::Visit(const Object& object) { Write(object); } -inline void Writer::Visit(const Number& number) { Write(number); } -inline void Writer::Visit(const String& string) { Write(string); } -inline void Writer::Visit(const Boolean& boolean) { Write(boolean); } -inline void Writer::Visit(const Null& null) { Write(null); } +void Writer::Visit(const Array& array) { Write(array); } +void Writer::Visit(const Object& object) { Write(object); } +void Writer::Visit(const Number& number) { Write(number); } +void Writer::Visit(const String& string) { Write(string); } +void Writer::Visit(const Boolean& boolean) { Write(boolean); } +void Writer::Visit(const Null& null) { Write(null); } } // end namespace diff --git a/aegisub/libaegisub/include/libaegisub/cajun/elements.h b/aegisub/libaegisub/include/libaegisub/cajun/elements.h index 4694bdab1..7fcd0e90a 100644 --- a/aegisub/libaegisub/include/libaegisub/cajun/elements.h +++ b/aegisub/libaegisub/include/libaegisub/cajun/elements.h @@ -8,11 +8,13 @@ Author: Terry Caton #pragma once +#ifndef LAGI_PRE #include #include #include #include #include +#endif namespace json { @@ -115,11 +117,6 @@ private: template class Imp_T; - class CastVisitor; - - template - class CastVisitor_T; - template const ElementTypeT& CastTo() const; @@ -139,6 +136,3 @@ public: }; } // end namespace - - -#include "elements.inl" diff --git a/aegisub/libaegisub/include/libaegisub/cajun/elements.inl b/aegisub/libaegisub/include/libaegisub/cajun/elements.inl deleted file mode 100644 index cc09076da..000000000 --- a/aegisub/libaegisub/include/libaegisub/cajun/elements.inl +++ /dev/null @@ -1,172 +0,0 @@ -/********************************************** - -License: BSD -Project Webpage: http://cajun-jsonapi.sourceforge.net/ -Author: Terry Caton - -***********************************************/ - -#include "visitor.h" -#include -#include -#include - -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 -class UnknownElement::Imp_T : public UnknownElement::Imp -{ -public: - Imp_T(const ElementTypeT& element) : m_Element(element) {} - virtual Imp* Clone() const { return new Imp_T(*this); } - - virtual void Accept(ConstVisitor& visitor) const { visitor.Visit(m_Element); } - virtual void Accept(Visitor& visitor) { visitor.Visit(m_Element); } - - virtual bool Compare(const Imp& imp) const - { - CastVisitor_T castVisitor; - imp.Accept(castVisitor); - return castVisitor.element && m_Element == *castVisitor.element; - } - -private: - ElementTypeT m_Element; -}; - -class UnknownElement::CastVisitor : public ConstVisitor -{ - virtual void Visit(const Array&) {} - virtual void Visit(const Object&) {} - virtual void Visit(const Number&) {} - virtual void Visit(const String&) {} - virtual void Visit(const Boolean&) {} - virtual void Visit(const Null&) {} -}; - -template -class UnknownElement::CastVisitor_T : public CastVisitor -{ -public: - const ElementTypeT *element; - CastVisitor_T() : element(0) { } - - // we don't know what this is, but it overrides one of the base's no-op functions - void Visit(const ElementTypeT& element) { this->element = &element; } -}; - -inline UnknownElement::UnknownElement() : m_pImp( new Imp_T( Null() ) ) {} -inline UnknownElement::UnknownElement(const UnknownElement& unknown) : m_pImp( unknown.m_pImp->Clone()) {} -inline UnknownElement::UnknownElement(const Object& object) : m_pImp( new Imp_T(object) ) {} -inline UnknownElement::UnknownElement(const Array& array) : m_pImp( new Imp_T(array) ) {} -inline UnknownElement::UnknownElement(double number) : m_pImp( new Imp_T(number) ) {} -inline UnknownElement::UnknownElement(int number) : m_pImp( new Imp_T(number) ) {} -inline UnknownElement::UnknownElement(long number) : m_pImp( new Imp_T(number) ) {} -inline UnknownElement::UnknownElement(bool boolean) : m_pImp( new Imp_T(boolean) ) {} -inline UnknownElement::UnknownElement(const char *string) : m_pImp( new Imp_T(string) ) {} -inline UnknownElement::UnknownElement(const String& string) : m_pImp( new Imp_T(string) ) {} -inline UnknownElement::UnknownElement(const Null& null) : m_pImp( new Imp_T(null) ) {} - -inline UnknownElement::~UnknownElement() { delete m_pImp; } - -inline UnknownElement::operator const Object& () const { return CastTo(); } -inline UnknownElement::operator const Array& () const { return CastTo(); } -inline UnknownElement::operator const Number& () const { return CastTo(); } -inline UnknownElement::operator const Boolean& () const { return CastTo(); } -inline UnknownElement::operator const String& () const { return CastTo(); } -inline UnknownElement::operator const Null& () const { return CastTo(); } - -inline UnknownElement::operator Object& () { return ConvertTo(); } -inline UnknownElement::operator Array& () { return ConvertTo(); } -inline UnknownElement::operator Number& () { return ConvertTo(); } -inline UnknownElement::operator Boolean& () { return ConvertTo(); } -inline UnknownElement::operator String& () { return ConvertTo(); } -inline UnknownElement::operator Null& () { return ConvertTo(); } - -inline UnknownElement& UnknownElement::operator = (const UnknownElement& unknown) -{ - delete m_pImp; - m_pImp = unknown.m_pImp->Clone(); - return *this; -} - -inline UnknownElement& UnknownElement::operator[] (const std::string& key) -{ - // the people want an object. make us one if we aren't already - return ConvertTo()[key]; -} - -inline const UnknownElement& UnknownElement::operator[] (const std::string& key) const -{ - // throws if we aren't an object - Object const& obj = CastTo(); - Object::const_iterator it = obj.find(key); - if (it == obj.end()) - throw Exception("Object member not found: " + key); - return it->second; -} - -inline UnknownElement& UnknownElement::operator[] (size_t index) -{ - // the people want an array. make us one if we aren't already - return ConvertTo()[index]; -} - -inline const UnknownElement& UnknownElement::operator[] (size_t index) const -{ - // throws if we aren't an array - return CastTo()[index]; -} - - -template -const ElementTypeT& UnknownElement::CastTo() const -{ - CastVisitor_T castVisitor; - m_pImp->Accept(castVisitor); - if (!castVisitor.element) - throw Exception("Bad cast"); - return *castVisitor.element; -} - -template -ElementTypeT& UnknownElement::ConvertTo() -{ - CastVisitor_T castVisitor; - Accept(castVisitor); - if (!castVisitor.element) - { - // we're not the right type. fix it & try again - *this = ElementTypeT(); - } - - return *this; -} - -inline void UnknownElement::Accept(ConstVisitor& visitor) const { m_pImp->Accept(visitor); } -inline void UnknownElement::Accept(Visitor& visitor) { m_pImp->Accept(visitor); } - - -inline bool UnknownElement::operator == (const UnknownElement& element) const -{ - return m_pImp->Compare(*element.m_pImp); -} - -} // end namespace diff --git a/aegisub/libaegisub/include/libaegisub/cajun/reader.h b/aegisub/libaegisub/include/libaegisub/cajun/reader.h index cd02977ba..b129098d1 100644 --- a/aegisub/libaegisub/include/libaegisub/cajun/reader.h +++ b/aegisub/libaegisub/include/libaegisub/cajun/reader.h @@ -9,8 +9,11 @@ Author: Terry Caton #pragma once #include "elements.h" + +#ifndef LAGI_PRE #include #include +#endif namespace json { @@ -95,7 +98,7 @@ private: class TokenStream; typedef std::vector Tokens; - template + template static void Read_i(ElementTypeT& element, std::istream& istr); // scanning istream into token sequence @@ -118,8 +121,4 @@ private: const std::string& MatchExpectedToken(Token::Type nExpected, TokenStream& tokenStream); }; - } // End namespace - - -#include "reader.inl" diff --git a/aegisub/libaegisub/include/libaegisub/cajun/writer.h b/aegisub/libaegisub/include/libaegisub/cajun/writer.h index 16907cc27..1cf700acd 100644 --- a/aegisub/libaegisub/include/libaegisub/cajun/writer.h +++ b/aegisub/libaegisub/include/libaegisub/cajun/writer.h @@ -18,7 +18,12 @@ class Writer : private ConstVisitor { public: template - static void Write(const ElementTypeT& element, std::ostream& ostr); + static void Write(const ElementTypeT& element, std::ostream& ostr) + { + Writer writer(ostr); + writer.Write(element); + ostr.flush(); // all done + } private: Writer(std::ostream& ostr); @@ -30,12 +35,12 @@ private: void Write(const Null& null); void Write(const UnknownElement& unknown); - virtual void Visit(const Array& array); - virtual void Visit(const Object& object); - virtual void Visit(const Number& number); - virtual void Visit(const String& string); - virtual void Visit(const Boolean& boolean); - virtual void Visit(const Null& null); + void Visit(const Array& array); + void Visit(const Object& object); + void Visit(const Number& number); + void Visit(const String& string); + void Visit(const Boolean& boolean); + void Visit(const Null& null); std::ostream& m_ostr; int m_nTabDepth; @@ -43,5 +48,3 @@ private: } // End namespace - -#include "writer.inl" diff --git a/aegisub/libaegisub/lagi_pre.h b/aegisub/libaegisub/lagi_pre.h index b442d625b..f218859d5 100644 --- a/aegisub/libaegisub/lagi_pre.h +++ b/aegisub/libaegisub/lagi_pre.h @@ -38,13 +38,16 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include +#include #include #include diff --git a/aegisub/src/agi_pre.h b/aegisub/src/agi_pre.h index b7c884ab2..b3bee3f15 100644 --- a/aegisub/src/agi_pre.h +++ b/aegisub/src/agi_pre.h @@ -56,9 +56,6 @@ // Setup #define AGI_PRE -// Common C++ -#include - // General headers #include #include