From 39d6af76874890a20eec9665b67e9364c749c139 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sun, 22 Mar 2020 01:33:11 +0200 Subject: [PATCH] Integrate XML module. Integrate the XML module into the host plugin and get it to compile. --- module/CMakeLists.txt | 6 + module/Library/IO/INI.cpp | 190 +++++----- module/Library/IO/INI.hpp | 76 ++-- module/Library/XML.cpp | 309 ++++++++++++++++ module/Library/XML.hpp | 10 + module/Library/XML/Attribute.cpp | 63 ++++ module/Library/XML/Attribute.hpp | 442 ++++++++++++++++++++++ module/Library/XML/Common.cpp | 43 +++ module/Library/XML/Common.hpp | 434 ++++++++++++++++++++++ module/Library/XML/Document.cpp | 38 ++ module/Library/XML/Document.hpp | 253 +++++++++++++ module/Library/XML/Node.cpp | 101 +++++ module/Library/XML/Node.hpp | 609 +++++++++++++++++++++++++++++++ module/Library/XML/Text.cpp | 90 +++++ module/Library/XML/Text.hpp | 362 ++++++++++++++++++ module/Register.cpp | 2 + sqrat/sqrat/sqratObject.h | 60 ++- sqrat/sqrat/sqratUtil.h | 8 +- 18 files changed, 2961 insertions(+), 135 deletions(-) create mode 100644 module/Library/XML.cpp create mode 100644 module/Library/XML.hpp create mode 100644 module/Library/XML/Attribute.cpp create mode 100644 module/Library/XML/Attribute.hpp create mode 100644 module/Library/XML/Common.cpp create mode 100644 module/Library/XML/Common.hpp create mode 100644 module/Library/XML/Document.cpp create mode 100644 module/Library/XML/Document.hpp create mode 100644 module/Library/XML/Node.cpp create mode 100644 module/Library/XML/Node.hpp create mode 100644 module/Library/XML/Text.cpp create mode 100644 module/Library/XML/Text.hpp diff --git a/module/CMakeLists.txt b/module/CMakeLists.txt index d50a63a5..cb4c2f00 100644 --- a/module/CMakeLists.txt +++ b/module/CMakeLists.txt @@ -61,6 +61,12 @@ add_library(SqModule MODULE Library/System/Path.cpp Library/System/Path.hpp Library/Utils.cpp Library/Utils.hpp Library/Utils/Buffer.cpp Library/Utils/Buffer.hpp + Library/XML.cpp Library/XML.hpp + Library/XML/Attribute.cpp Library/XML/Attribute.hpp + Library/XML/Common.cpp Library/XML/Common.hpp + Library/XML/Document.cpp Library/XML/Document.hpp + Library/XML/Node.cpp Library/XML/Node.hpp + Library/XML/Text.cpp Library/XML/Text.hpp Misc/Broadcast.cpp Misc/Constants.cpp Misc/Exports.cpp diff --git a/module/Library/IO/INI.cpp b/module/Library/IO/INI.cpp index 871c1062..b993f8d0 100644 --- a/module/Library/IO/INI.cpp +++ b/module/Library/IO/INI.cpp @@ -37,7 +37,7 @@ void IniResult::Check() const } // ------------------------------------------------------------------------------------------------ -void DocumentRef::Validate() const +void IniDocumentRef::Validate() const { // Is the document handle valid? if (!m_Ptr) @@ -47,7 +47,7 @@ void DocumentRef::Validate() const } // ------------------------------------------------------------------------------------------------ -Int32 Entries::Cmp(const Entries & o) const +Int32 IniEntries::Cmp(const IniEntries & o) const { if (m_Elem == o.m_Elem) { @@ -64,7 +64,7 @@ Int32 Entries::Cmp(const Entries & o) const } // ------------------------------------------------------------------------------------------------ -void Entries::Next() +void IniEntries::Next() { // Are there any other elements ahead? if (!m_List.empty() && m_Elem != m_List.end()) @@ -74,7 +74,7 @@ void Entries::Next() } // ------------------------------------------------------------------------------------------------ -void Entries::Prev() +void IniEntries::Prev() { // Are there any other elements behind? if (!m_List.empty() && m_Elem != m_List.begin()) @@ -84,7 +84,7 @@ void Entries::Prev() } // ------------------------------------------------------------------------------------------------ -void Entries::Advance(Int32 n) +void IniEntries::Advance(Int32 n) { // Are there any other elements ahead? if (m_List.empty() || m_Elem == m_List.end()) @@ -99,7 +99,7 @@ void Entries::Advance(Int32 n) } // ------------------------------------------------------------------------------------------------ -void Entries::Retreat(Int32 n) +void IniEntries::Retreat(Int32 n) { // Are there any other elements behind? if (m_List.empty() || m_Elem == m_List.begin()) @@ -114,7 +114,7 @@ void Entries::Retreat(Int32 n) } // ------------------------------------------------------------------------------------------------ -CSStr Entries::GetItem() const +CSStr IniEntries::GetItem() const { // is the current element valid? if (m_List.empty() || m_Elem == m_List.end()) @@ -126,7 +126,7 @@ CSStr Entries::GetItem() const } // ------------------------------------------------------------------------------------------------ -CSStr Entries::GetComment() const +CSStr IniEntries::GetComment() const { // is the current element valid? if (m_List.empty() || m_Elem == m_List.end()) @@ -138,7 +138,7 @@ CSStr Entries::GetComment() const } // ------------------------------------------------------------------------------------------------ -Int32 Entries::GetOrder() const +Int32 IniEntries::GetOrder() const { // is the current element valid? if (m_List.empty() || m_Elem == m_List.end()) @@ -150,7 +150,7 @@ Int32 Entries::GetOrder() const } // ------------------------------------------------------------------------------------------------ -Int32 Document::Cmp(const Document & o) const +Int32 IniDocument::Cmp(const IniDocument & o) const { if (m_Doc == o.m_Doc) { @@ -167,7 +167,7 @@ Int32 Document::Cmp(const Document & o) const } // ------------------------------------------------------------------------------------------------ -IniResult Document::LoadFile(CSStr filepath) +IniResult IniDocument::LoadFile(CSStr filepath) { // Validate the handle m_Doc.Validate(); @@ -176,7 +176,7 @@ IniResult Document::LoadFile(CSStr filepath) } // ------------------------------------------------------------------------------------------------ -IniResult Document::LoadData(CSStr source, Int32 size) +IniResult IniDocument::LoadData(CSStr source, Int32 size) { // Validate the handle m_Doc.Validate(); @@ -185,7 +185,7 @@ IniResult Document::LoadData(CSStr source, Int32 size) } // ------------------------------------------------------------------------------------------------ -IniResult Document::SaveFile(CSStr filepath, bool signature) +IniResult IniDocument::SaveFile(CSStr filepath, bool signature) { // Validate the handle m_Doc.Validate(); @@ -194,7 +194,7 @@ IniResult Document::SaveFile(CSStr filepath, bool signature) } // ------------------------------------------------------------------------------------------------ -Object Document::SaveData(bool signature) +Object IniDocument::SaveData(bool signature) { // Validate the handle m_Doc.Validate(); @@ -214,7 +214,7 @@ Object Document::SaveData(bool signature) } // ------------------------------------------------------------------------------------------------ -Entries Document::GetAllSections() const +IniEntries IniDocument::GetAllSections() const { // Validate the handle m_Doc.Validate(); @@ -223,11 +223,11 @@ Entries Document::GetAllSections() const // Obtain all sections from the INI document m_Doc->GetAllSections(entries); // Return the entries and take over content - return Entries(m_Doc, entries); + return IniEntries(m_Doc, entries); } // ------------------------------------------------------------------------------------------------ -Entries Document::GetAllKeys(CSStr section) const +IniEntries IniDocument::GetAllKeys(CSStr section) const { // Validate the handle m_Doc.Validate(); @@ -236,11 +236,11 @@ Entries Document::GetAllKeys(CSStr section) const // Obtain all sections from the INI document m_Doc->GetAllKeys(section, entries); // Return the entries and take over content - return Entries(m_Doc, entries); + return IniEntries(m_Doc, entries); } // ------------------------------------------------------------------------------------------------ -Entries Document::GetAllValues(CSStr section, CSStr key) const +IniEntries IniDocument::GetAllValues(CSStr section, CSStr key) const { // Validate the handle m_Doc.Validate(); @@ -249,11 +249,11 @@ Entries Document::GetAllValues(CSStr section, CSStr key) const // Obtain all sections from the INI document m_Doc->GetAllValues(section, key, entries); // Return the entries and take over content - return Entries(m_Doc, entries); + return IniEntries(m_Doc, entries); } // ------------------------------------------------------------------------------------------------ -Int32 Document::GetSectionSize(CSStr section) const +Int32 IniDocument::GetSectionSize(CSStr section) const { // Validate the handle m_Doc.Validate(); @@ -262,7 +262,7 @@ Int32 Document::GetSectionSize(CSStr section) const } // ------------------------------------------------------------------------------------------------ -bool Document::HasMultipleKeys(CSStr section, CSStr key) const +bool IniDocument::HasMultipleKeys(CSStr section, CSStr key) const { // Validate the handle m_Doc.Validate(); @@ -278,7 +278,7 @@ bool Document::HasMultipleKeys(CSStr section, CSStr key) const } // ------------------------------------------------------------------------------------------------ -CCStr Document::GetValue(CSStr section, CSStr key, CSStr def) const +CCStr IniDocument::GetValue(CSStr section, CSStr key, CSStr def) const { // Validate the handle m_Doc.Validate(); @@ -287,7 +287,7 @@ CCStr Document::GetValue(CSStr section, CSStr key, CSStr def) const } // ------------------------------------------------------------------------------------------------ -SQInteger Document::GetInteger(CSStr section, CSStr key, SQInteger def) const +SQInteger IniDocument::GetInteger(CSStr section, CSStr key, SQInteger def) const { // Validate the handle m_Doc.Validate(); @@ -296,7 +296,7 @@ SQInteger Document::GetInteger(CSStr section, CSStr key, SQInteger def) const } // ------------------------------------------------------------------------------------------------ -SQFloat Document::GetFloat(CSStr section, CSStr key, SQFloat def) const +SQFloat IniDocument::GetFloat(CSStr section, CSStr key, SQFloat def) const { // Validate the handle m_Doc.Validate(); @@ -305,7 +305,7 @@ SQFloat Document::GetFloat(CSStr section, CSStr key, SQFloat def) const } // ------------------------------------------------------------------------------------------------ -bool Document::GetBoolean(CSStr section, CSStr key, bool def) const +bool IniDocument::GetBoolean(CSStr section, CSStr key, bool def) const { // Validate the handle m_Doc.Validate(); @@ -314,7 +314,7 @@ bool Document::GetBoolean(CSStr section, CSStr key, bool def) const } // ------------------------------------------------------------------------------------------------ -IniResult Document::SetValue(CSStr section, CSStr key, CSStr value, bool force, CSStr comment) +IniResult IniDocument::SetValue(CSStr section, CSStr key, CSStr value, bool force, CSStr comment) { // Validate the handle m_Doc.Validate(); @@ -323,7 +323,7 @@ IniResult Document::SetValue(CSStr section, CSStr key, CSStr value, bool force, } // ------------------------------------------------------------------------------------------------ -IniResult Document::SetInteger(CSStr section, CSStr key, SQInteger value, bool hex, bool force, CSStr comment) +IniResult IniDocument::SetInteger(CSStr section, CSStr key, SQInteger value, bool hex, bool force, CSStr comment) { // Validate the handle m_Doc.Validate(); @@ -332,7 +332,7 @@ IniResult Document::SetInteger(CSStr section, CSStr key, SQInteger value, bool h } // ------------------------------------------------------------------------------------------------ -IniResult Document::SetFloat(CSStr section, CSStr key, SQFloat value, bool force, CSStr comment) +IniResult IniDocument::SetFloat(CSStr section, CSStr key, SQFloat value, bool force, CSStr comment) { // Validate the handle m_Doc.Validate(); @@ -341,7 +341,7 @@ IniResult Document::SetFloat(CSStr section, CSStr key, SQFloat value, bool force } // ------------------------------------------------------------------------------------------------ -IniResult Document::SetBoolean(CSStr section, CSStr key, bool value, bool force, CSStr comment) +IniResult IniDocument::SetBoolean(CSStr section, CSStr key, bool value, bool force, CSStr comment) { // Validate the handle m_Doc.Validate(); @@ -350,7 +350,7 @@ IniResult Document::SetBoolean(CSStr section, CSStr key, bool value, bool force, } // ------------------------------------------------------------------------------------------------ -bool Document::DeleteValue(CSStr section, CSStr key, CSStr value, bool empty) +bool IniDocument::DeleteValue(CSStr section, CSStr key, CSStr value, bool empty) { // Validate the handle m_Doc.Validate(); @@ -381,36 +381,36 @@ void Register_INI(HSQUIRRELVM vm) .Func(_SC("Check"), &IniResult::Check) ); - inins.Bind(_SC("Entries"), - Class< Entries >(vm, EntriesTypename::Str) + inins.Bind(_SC("IniEntries"), + Class< IniEntries >(vm, EntriesTypename::Str) // Constructors .Ctor() - .Ctor< const Entries & >() + .Ctor< const IniEntries & >() // Core Meta-methods .SquirrelFunc(_SC("_typename"), &EntriesTypename::Fn) - .Func(_SC("_tostring"), &Entries::ToString) - .Func(_SC("cmp"), &Entries::Cmp) + .Func(_SC("_tostring"), &IniEntries::ToString) + .Func(_SC("cmp"), &IniEntries::Cmp) // Properties - .Prop(_SC("Valid"), &Entries::IsValid) - .Prop(_SC("Empty"), &Entries::IsEmpty) - .Prop(_SC("References"), &Entries::GetRefCount) - .Prop(_SC("Size"), &Entries::GetSize) - .Prop(_SC("Item"), &Entries::GetItem) - .Prop(_SC("Comment"), &Entries::GetComment) - .Prop(_SC("Order"), &Entries::GetOrder) + .Prop(_SC("Valid"), &IniEntries::IsValid) + .Prop(_SC("Empty"), &IniEntries::IsEmpty) + .Prop(_SC("References"), &IniEntries::GetRefCount) + .Prop(_SC("Size"), &IniEntries::GetSize) + .Prop(_SC("Item"), &IniEntries::GetItem) + .Prop(_SC("Comment"), &IniEntries::GetComment) + .Prop(_SC("Order"), &IniEntries::GetOrder) // Member Methods - .Func(_SC("Reset"), &Entries::Reset) - .Func(_SC("Next"), &Entries::Next) - .Func(_SC("Prev"), &Entries::Prev) - .Func(_SC("Advance"), &Entries::Advance) - .Func(_SC("Retreat"), &Entries::Retreat) - .Func(_SC("Sort"), &Entries::Sort) - .Func(_SC("SortByKeyOrder"), &Entries::SortByKeyOrder) - .Func(_SC("SortByLoadOrder"), &Entries::SortByLoadOrder) + .Func(_SC("Reset"), &IniEntries::Reset) + .Func(_SC("Next"), &IniEntries::Next) + .Func(_SC("Prev"), &IniEntries::Prev) + .Func(_SC("Advance"), &IniEntries::Advance) + .Func(_SC("Retreat"), &IniEntries::Retreat) + .Func(_SC("Sort"), &IniEntries::Sort) + .Func(_SC("SortByKeyOrder"), &IniEntries::SortByKeyOrder) + .Func(_SC("SortByLoadOrder"), &IniEntries::SortByLoadOrder) ); - inins.Bind(_SC("Document"), - Class< Document, NoCopy< Document > >(vm, DocumentTypename::Str) + inins.Bind(_SC("IniDocument"), + Class< IniDocument, NoCopy< IniDocument > >(vm, DocumentTypename::Str) // Constructors .Ctor() .Ctor< bool >() @@ -418,50 +418,50 @@ void Register_INI(HSQUIRRELVM vm) .Ctor< bool, bool, bool >() // Core Meta-methods .SquirrelFunc(_SC("_typename"), &DocumentTypename::Fn) - .Func(_SC("_tostring"), &Document::ToString) - .Func(_SC("cmp"), &Document::Cmp) + .Func(_SC("_tostring"), &IniDocument::ToString) + .Func(_SC("cmp"), &IniDocument::Cmp) // Properties - .Prop(_SC("Valid"), &Document::IsValid) - .Prop(_SC("Empty"), &Document::IsEmpty) - .Prop(_SC("References"), &Document::GetRefCount) - .Prop(_SC("Unicode"), &Document::GetUnicode, &Document::SetUnicode) - .Prop(_SC("MultiKey"), &Document::GetMultiKey, &Document::SetMultiKey) - .Prop(_SC("MultiLine"), &Document::GetMultiLine, &Document::SetMultiLine) - .Prop(_SC("Spaces"), &Document::GetSpaces, &Document::SetSpaces) + .Prop(_SC("Valid"), &IniDocument::IsValid) + .Prop(_SC("Empty"), &IniDocument::IsEmpty) + .Prop(_SC("References"), &IniDocument::GetRefCount) + .Prop(_SC("Unicode"), &IniDocument::GetUnicode, &IniDocument::SetUnicode) + .Prop(_SC("MultiKey"), &IniDocument::GetMultiKey, &IniDocument::SetMultiKey) + .Prop(_SC("MultiLine"), &IniDocument::GetMultiLine, &IniDocument::SetMultiLine) + .Prop(_SC("Spaces"), &IniDocument::GetSpaces, &IniDocument::SetSpaces) // Member Methods - .Func(_SC("Reset"), &Document::Reset) - .Func(_SC("LoadFile"), &Document::LoadFile) - .Overload< IniResult (Document::*)(CSStr) >(_SC("LoadString"), &Document::LoadData) - .Overload< IniResult (Document::*)(CSStr, Int32) >(_SC("LoadString"), &Document::LoadData) - .Overload< IniResult (Document::*)(CSStr) >(_SC("SaveFile"), &Document::SaveFile) - .Overload< IniResult (Document::*)(CSStr, bool) >(_SC("SaveFile"), &Document::SaveFile) - .Func(_SC("SaveData"), &Document::SaveData) - .Func(_SC("GetSections"), &Document::GetAllSections) - .Func(_SC("GetKeys"), &Document::GetAllKeys) - .Func(_SC("GetValues"), &Document::GetAllValues) - .Func(_SC("GetSectionSize"), &Document::GetSectionSize) - .Func(_SC("HasMultipleKeys"), &Document::HasMultipleKeys) - .Func(_SC("GetValue"), &Document::GetValue) - .Func(_SC("GetInteger"), &Document::GetInteger) - .Func(_SC("GetFloat"), &Document::GetFloat) - .Func(_SC("GetBoolean"), &Document::GetBoolean) - .Overload< IniResult (Document::*)(CSStr, CSStr, CSStr) >(_SC("SetValue"), &Document::SetValue) - .Overload< IniResult (Document::*)(CSStr, CSStr, CSStr, bool) >(_SC("SetValue"), &Document::SetValue) - .Overload< IniResult (Document::*)(CSStr, CSStr, CSStr, bool, CSStr) >(_SC("SetValue"), &Document::SetValue) - .Overload< IniResult (Document::*)(CSStr, CSStr, SQInteger) >(_SC("SetInteger"), &Document::SetInteger) - .Overload< IniResult (Document::*)(CSStr, CSStr, SQInteger, bool) >(_SC("SetInteger"), &Document::SetInteger) - .Overload< IniResult (Document::*)(CSStr, CSStr, SQInteger, bool, bool) >(_SC("SetInteger"), &Document::SetInteger) - .Overload< IniResult (Document::*)(CSStr, CSStr, SQInteger, bool, bool, CSStr) >(_SC("SetInteger"), &Document::SetInteger) - .Overload< IniResult (Document::*)(CSStr, CSStr, SQFloat) >(_SC("SetFloat"), &Document::SetFloat) - .Overload< IniResult (Document::*)(CSStr, CSStr, SQFloat, bool) >(_SC("SetFloat"), &Document::SetFloat) - .Overload< IniResult (Document::*)(CSStr, CSStr, SQFloat, bool, CSStr) >(_SC("SetFloat"), &Document::SetFloat) - .Overload< IniResult (Document::*)(CSStr, CSStr, bool) >(_SC("SetBoolean"), &Document::SetBoolean) - .Overload< IniResult (Document::*)(CSStr, CSStr, bool, bool) >(_SC("SetBoolean"), &Document::SetBoolean) - .Overload< IniResult (Document::*)(CSStr, CSStr, bool, bool, CSStr) >(_SC("SetBoolean"), &Document::SetBoolean) - .Overload< bool (Document::*)(CSStr) >(_SC("DeleteValue"), &Document::DeleteValue) - .Overload< bool (Document::*)(CSStr, CSStr) >(_SC("DeleteValue"), &Document::DeleteValue) - .Overload< bool (Document::*)(CSStr, CSStr, CSStr) >(_SC("DeleteValue"), &Document::DeleteValue) - .Overload< bool (Document::*)(CSStr, CSStr, CSStr, bool) >(_SC("DeleteValue"), &Document::DeleteValue) + .Func(_SC("Reset"), &IniDocument::Reset) + .Func(_SC("LoadFile"), &IniDocument::LoadFile) + .Overload< IniResult (IniDocument::*)(CSStr) >(_SC("LoadString"), &IniDocument::LoadData) + .Overload< IniResult (IniDocument::*)(CSStr, Int32) >(_SC("LoadString"), &IniDocument::LoadData) + .Overload< IniResult (IniDocument::*)(CSStr) >(_SC("SaveFile"), &IniDocument::SaveFile) + .Overload< IniResult (IniDocument::*)(CSStr, bool) >(_SC("SaveFile"), &IniDocument::SaveFile) + .Func(_SC("SaveData"), &IniDocument::SaveData) + .Func(_SC("GetSections"), &IniDocument::GetAllSections) + .Func(_SC("GetKeys"), &IniDocument::GetAllKeys) + .Func(_SC("GetValues"), &IniDocument::GetAllValues) + .Func(_SC("GetSectionSize"), &IniDocument::GetSectionSize) + .Func(_SC("HasMultipleKeys"), &IniDocument::HasMultipleKeys) + .Func(_SC("GetValue"), &IniDocument::GetValue) + .Func(_SC("GetInteger"), &IniDocument::GetInteger) + .Func(_SC("GetFloat"), &IniDocument::GetFloat) + .Func(_SC("GetBoolean"), &IniDocument::GetBoolean) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, CSStr) >(_SC("SetValue"), &IniDocument::SetValue) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, CSStr, bool) >(_SC("SetValue"), &IniDocument::SetValue) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, CSStr, bool, CSStr) >(_SC("SetValue"), &IniDocument::SetValue) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, SQInteger) >(_SC("SetInteger"), &IniDocument::SetInteger) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, SQInteger, bool) >(_SC("SetInteger"), &IniDocument::SetInteger) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, SQInteger, bool, bool) >(_SC("SetInteger"), &IniDocument::SetInteger) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, SQInteger, bool, bool, CSStr) >(_SC("SetInteger"), &IniDocument::SetInteger) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, SQFloat) >(_SC("SetFloat"), &IniDocument::SetFloat) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, SQFloat, bool) >(_SC("SetFloat"), &IniDocument::SetFloat) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, SQFloat, bool, CSStr) >(_SC("SetFloat"), &IniDocument::SetFloat) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, bool) >(_SC("SetBoolean"), &IniDocument::SetBoolean) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, bool, bool) >(_SC("SetBoolean"), &IniDocument::SetBoolean) + .Overload< IniResult (IniDocument::*)(CSStr, CSStr, bool, bool, CSStr) >(_SC("SetBoolean"), &IniDocument::SetBoolean) + .Overload< bool (IniDocument::*)(CSStr) >(_SC("DeleteValue"), &IniDocument::DeleteValue) + .Overload< bool (IniDocument::*)(CSStr, CSStr) >(_SC("DeleteValue"), &IniDocument::DeleteValue) + .Overload< bool (IniDocument::*)(CSStr, CSStr, CSStr) >(_SC("DeleteValue"), &IniDocument::DeleteValue) + .Overload< bool (IniDocument::*)(CSStr, CSStr, CSStr, bool) >(_SC("DeleteValue"), &IniDocument::DeleteValue) ); RootTable(vm).Bind(_SC("SqIni"), inins); diff --git a/module/Library/IO/INI.hpp b/module/Library/IO/INI.hpp index 0cd17006..b67f275e 100644 --- a/module/Library/IO/INI.hpp +++ b/module/Library/IO/INI.hpp @@ -171,10 +171,10 @@ public: /* ------------------------------------------------------------------------------------------------ * Manages a reference counted INI document instance. */ -class DocumentRef +class IniDocumentRef { // -------------------------------------------------------------------------------------------- - friend class Document; + friend class IniDocument; public: @@ -231,7 +231,7 @@ private: /* -------------------------------------------------------------------------------------------- * Base constructor. */ - DocumentRef(bool utf8, bool multikey, bool multiline) + IniDocumentRef(bool utf8, bool multikey, bool multiline) : m_Ptr(new Type(utf8, multikey, multiline)), m_Ref(new Counter(1)) { /* ... */ @@ -242,7 +242,7 @@ public: /* -------------------------------------------------------------------------------------------- * Default constructor (null). */ - DocumentRef() + IniDocumentRef() : m_Ptr(NULL), m_Ref(NULL) { /* ... */ @@ -251,7 +251,7 @@ public: /* -------------------------------------------------------------------------------------------- * Copy constructor. */ - DocumentRef(const DocumentRef & o) + IniDocumentRef(const IniDocumentRef & o) : m_Ptr(o.m_Ptr), m_Ref(o.m_Ref) { @@ -261,7 +261,7 @@ public: /* -------------------------------------------------------------------------------------------- * Move constructor. */ - DocumentRef(DocumentRef && o) + IniDocumentRef(IniDocumentRef && o) : m_Ptr(o.m_Ptr), m_Ref(o.m_Ref) { @@ -272,7 +272,7 @@ public: /* -------------------------------------------------------------------------------------------- * Destructor. */ - ~DocumentRef() + ~IniDocumentRef() { Drop(); } @@ -280,7 +280,7 @@ public: /* -------------------------------------------------------------------------------------------- * Copy assignment operator. */ - DocumentRef & operator = (const DocumentRef & o) + IniDocumentRef & operator = (const IniDocumentRef & o) { if (m_Ptr != o.m_Ptr) { @@ -295,7 +295,7 @@ public: /* -------------------------------------------------------------------------------------------- * Move assignment operator. */ - DocumentRef & operator = (DocumentRef && o) + IniDocumentRef & operator = (IniDocumentRef && o) { if (m_Ptr != o.m_Ptr) { @@ -310,7 +310,7 @@ public: /* -------------------------------------------------------------------------------------------- * Perform an equality comparison between two document instances. */ - bool operator == (const DocumentRef & o) const + bool operator == (const IniDocumentRef & o) const { return (m_Ptr == o.m_Ptr); } @@ -318,7 +318,7 @@ public: /* -------------------------------------------------------------------------------------------- * Perform an inequality comparison between two document instances. */ - bool operator != (const DocumentRef & o) const + bool operator != (const IniDocumentRef & o) const { return (m_Ptr != o.m_Ptr); } @@ -395,15 +395,15 @@ public: /* ------------------------------------------------------------------------------------------------ * Class that can access and iterate a series of entries in the INI document. */ -class Entries +class IniEntries { // -------------------------------------------------------------------------------------------- - friend class Document; + friend class IniDocument; protected: // -------------------------------------------------------------------------------------------- - typedef DocumentRef::Type::TNamesDepend Container; + typedef IniDocumentRef::Type::TNamesDepend Container; // -------------------------------------------------------------------------------------------- typedef Container::iterator Iterator; @@ -411,7 +411,7 @@ protected: /* -------------------------------------------------------------------------------------------- * Default constructor. */ - Entries(const DocumentRef & ini, Container & list) + IniEntries(const IniDocumentRef & ini, Container & list) : m_Doc(ini), m_List(), m_Elem() { m_List.swap(list); @@ -421,7 +421,7 @@ protected: private: // --------------------------------------------------------------------------------------------- - DocumentRef m_Doc; // The document that contains the elements. + IniDocumentRef m_Doc; // The document that contains the elements. Container m_List; // The list of elements to iterate. Iterator m_Elem; // The currently processed element. @@ -430,7 +430,7 @@ public: /* -------------------------------------------------------------------------------------------- * Default constructor. (null) */ - Entries() + IniEntries() : m_Doc(), m_List(), m_Elem(m_List.end()) { /* ... */ @@ -439,7 +439,7 @@ public: /* -------------------------------------------------------------------------------------------- * Copy constructor. */ - Entries(const Entries & o) + IniEntries(const IniEntries & o) : m_Doc(o.m_Doc), m_List(o.m_List), m_Elem() { Reset(); @@ -448,7 +448,7 @@ public: /* -------------------------------------------------------------------------------------------- * Destructor. */ - ~Entries() + ~IniEntries() { /* ... */ } @@ -456,7 +456,7 @@ public: /* -------------------------------------------------------------------------------------------- * Copy assignment operator. */ - Entries & operator = (const Entries & o) + IniEntries & operator = (const IniEntries & o) { m_Doc = o.m_Doc; m_List = o.m_List; @@ -467,7 +467,7 @@ public: /* -------------------------------------------------------------------------------------------- * Used by the script engine to compare two instances of this type. */ - Int32 Cmp(const Entries & o) const; + Int32 Cmp(const IniEntries & o) const; /* -------------------------------------------------------------------------------------------- * Used by the script engine to convert an instance of this type to a string. @@ -551,7 +551,7 @@ public: { if (!m_List.empty()) { - m_List.sort(DocumentRef::Type::Entry::KeyOrder()); + m_List.sort(IniDocumentRef::Type::Entry::KeyOrder()); } } @@ -562,7 +562,7 @@ public: { if (!m_List.empty()) { - m_List.sort(DocumentRef::Type::Entry::KeyOrder()); + m_List.sort(IniDocumentRef::Type::Entry::KeyOrder()); } } @@ -573,7 +573,7 @@ public: { if (!m_List.empty()) { - m_List.sort(DocumentRef::Type::Entry::LoadOrder()); + m_List.sort(IniDocumentRef::Type::Entry::LoadOrder()); } } @@ -596,34 +596,34 @@ public: /* ------------------------------------------------------------------------------------------------ * Class that can read/write and alter the contents of INI files. */ -class Document +class IniDocument { protected: // -------------------------------------------------------------------------------------------- - typedef DocumentRef::Type::TNamesDepend Container; + typedef IniDocumentRef::Type::TNamesDepend Container; /* -------------------------------------------------------------------------------------------- * Copy constructor. (disabled) */ - Document(const Document & o); + IniDocument(const IniDocument & o); /* -------------------------------------------------------------------------------------------- * Copy assignment operator. (disabled) */ - Document & operator = (const Document & o); + IniDocument & operator = (const IniDocument & o); private: // --------------------------------------------------------------------------------------------- - DocumentRef m_Doc; // The main INI document instance. + IniDocumentRef m_Doc; // The main INI document instance. public: /* -------------------------------------------------------------------------------------------- * Default constructor. */ - Document() + IniDocument() : m_Doc(false, false, true) { /* ... */ @@ -632,7 +632,7 @@ public: /* -------------------------------------------------------------------------------------------- * Explicit constructor. */ - Document(bool utf8) + IniDocument(bool utf8) : m_Doc(utf8, false, true) { /* ... */ @@ -641,7 +641,7 @@ public: /* -------------------------------------------------------------------------------------------- * Explicit constructor. */ - Document(bool utf8, bool multikey) + IniDocument(bool utf8, bool multikey) : m_Doc(utf8, multikey, true) { /* ... */ @@ -650,7 +650,7 @@ public: /* -------------------------------------------------------------------------------------------- * Explicit constructor. */ - Document(bool utf8, bool multikey, bool multiline) + IniDocument(bool utf8, bool multikey, bool multiline) : m_Doc(utf8, multikey, multiline) { /* ... */ @@ -659,7 +659,7 @@ public: /* -------------------------------------------------------------------------------------------- * Destructor. */ - ~Document() + ~IniDocument() { /* ... */ } @@ -667,7 +667,7 @@ public: /* -------------------------------------------------------------------------------------------- * Used by the script engine to compare two instances of this type. */ - Int32 Cmp(const Document & o) const; + Int32 Cmp(const IniDocument & o) const; /* -------------------------------------------------------------------------------------------- * Used by the script engine to convert an instance of this type to a string. @@ -812,17 +812,17 @@ public: /* -------------------------------------------------------------------------------------------- * Retrieve all section names. */ - Entries GetAllSections() const; + IniEntries GetAllSections() const; /* -------------------------------------------------------------------------------------------- * Retrieve all unique key names in a section. */ - Entries GetAllKeys(CSStr section) const; + IniEntries GetAllKeys(CSStr section) const; /* -------------------------------------------------------------------------------------------- * Retrieve all values for a specific key. */ - Entries GetAllValues(CSStr section, CSStr key) const; + IniEntries GetAllValues(CSStr section, CSStr key) const; /* -------------------------------------------------------------------------------------------- * Query the number of keys in a specific section. diff --git a/module/Library/XML.cpp b/module/Library/XML.cpp new file mode 100644 index 00000000..7db3d2d9 --- /dev/null +++ b/module/Library/XML.cpp @@ -0,0 +1,309 @@ +// ------------------------------------------------------------------------------------------------ +#include "Library/XML.hpp" +#include "Library/XML/Attribute.hpp" +#include "Library/XML/Text.hpp" +#include "Library/XML/Node.hpp" +#include "Library/XML/Document.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +// ================================================================================================ +void Register_XML(HSQUIRRELVM vm) +{ + Table xmlns(vm); + + xmlns.Bind(_SC("XmlParseResult"), Class< XmlParseResult >(vm, _SC("SqXmlParseResult")) + // Constructors + .Ctor() + .Ctor< const XmlParseResult & >() + // Core Meta-methods + .Func(_SC("_cmp"), &XmlParseResult::Cmp) + .SquirrelFunc(_SC("_typename"), &XmlParseResult::Typename) + .Func(_SC("_tostring"), &XmlParseResult::ToString) + // Properties + .Prop(_SC("Valid"), &XmlParseResult::IsValid) + .Prop(_SC("References"), &XmlParseResult::GetRefCount) + .Prop(_SC("Ok"), &XmlParseResult::IsOk) + .Prop(_SC("Status"), &XmlParseResult::GetStatus) + .Prop(_SC("Offset"), &XmlParseResult::GetOffset) + .Prop(_SC("Encoding"), &XmlParseResult::GetEncoding) + .Prop(_SC("Description"), &XmlParseResult::GetDescription) + // Member Methods + .Func(_SC("Check"), &XmlParseResult::Check) + ); + + xmlns.Bind(_SC("XmlAttribute"), Class< XmlAttribute >(vm, _SC("SqXmlAttribute")) + // Constructors + .Ctor() + .Ctor< const XmlAttribute & >() + // Core Meta-methods + .Func(_SC("_cmp"), &XmlAttribute::Cmp) + .SquirrelFunc(_SC("_typename"), &XmlAttribute::Typename) + .Func(_SC("_tostring"), &XmlAttribute::ToString) + // Properties + .Prop(_SC("Valid"), &XmlAttribute::IsValid) + .Prop(_SC("References"), &XmlAttribute::GetRefCount) + .Prop(_SC("Empty"), &XmlAttribute::IsEmpty) + .Prop(_SC("Hash"), &XmlAttribute::GetHashValue) + .Prop(_SC("Name"), &XmlAttribute::GetName, &XmlAttribute::SetName) + .Prop(_SC("Value"), &XmlAttribute::GetValue, &XmlAttribute::SetValue) + .Prop(_SC("Int"), &XmlAttribute::GetInt, &XmlAttribute::SetInt) + .Prop(_SC("Uint"), &XmlAttribute::GetUint, &XmlAttribute::SetUint) + .Prop(_SC("Float"), &XmlAttribute::GetFloat, &XmlAttribute::SetFloat) + .Prop(_SC("Double"), &XmlAttribute::GetDouble, &XmlAttribute::SetDouble) + .Prop(_SC("Long"), &XmlAttribute::GetLong, &XmlAttribute::SetLong) + .Prop(_SC("Ulong"), &XmlAttribute::GetUlong, &XmlAttribute::SetUlong) + .Prop(_SC("Bool"), &XmlAttribute::GetBool, &XmlAttribute::SetBool) + .Prop(_SC("Next"), &XmlAttribute::NextAttribute) + .Prop(_SC("Prev"), &XmlAttribute::PrevAttribute) + // Member Methods + .Func(_SC("SetName"), &XmlAttribute::ApplyName) + .Func(_SC("SetValue"), &XmlAttribute::ApplyValue) + .Func(_SC("AsString"), &XmlAttribute::AsString) + .Func(_SC("AsInt"), &XmlAttribute::AsInt) + .Func(_SC("AsUint"), &XmlAttribute::AsUint) + .Func(_SC("AsFloat"), &XmlAttribute::AsFloat) + .Func(_SC("AsDouble"), &XmlAttribute::AsDouble) + .Func(_SC("AsLong"), &XmlAttribute::AsLong) + .Func(_SC("AsUlong"), &XmlAttribute::AsUlong) + .Func(_SC("AsBool"), &XmlAttribute::AsBool) + .Func(_SC("SetString"), &XmlAttribute::ApplyString) + .Func(_SC("SetInt"), &XmlAttribute::ApplyInt) + .Func(_SC("SetUint"), &XmlAttribute::ApplyUint) + .Func(_SC("SetFloat"), &XmlAttribute::ApplyFloat) + .Func(_SC("SetDouble"), &XmlAttribute::ApplyDouble) + .Func(_SC("SetLong"), &XmlAttribute::ApplyLong) + .Func(_SC("SetUlong"), &XmlAttribute::ApplyUlong) + .Func(_SC("SetBool"), &XmlAttribute::ApplyBool) + ); + + xmlns.Bind(_SC("XmlText"), Class< XmlText >(vm, _SC("SqXmlText")) + // Constructors + .Ctor() + .Ctor< const XmlText & >() + // Core Meta-methods + .Func(_SC("_cmp"), &XmlText::Cmp) + .SquirrelFunc(_SC("_typename"), &XmlText::Typename) + .Func(_SC("_tostring"), &XmlText::ToString) + // Properties + .Prop(_SC("Valid"), &XmlText::IsValid) + .Prop(_SC("References"), &XmlText::GetRefCount) + .Prop(_SC("Empty"), &XmlText::IsEmpty) + .Prop(_SC("Value"), &XmlText::GetValue) + .Prop(_SC("Int"), &XmlText::GetInt, &XmlText::SetInt) + .Prop(_SC("Uint"), &XmlText::GetUint, &XmlText::SetUint) + .Prop(_SC("Float"), &XmlText::GetFloat, &XmlText::SetFloat) + .Prop(_SC("Double"), &XmlText::GetDouble, &XmlText::SetDouble) + .Prop(_SC("Long"), &XmlText::GetLong, &XmlText::SetLong) + .Prop(_SC("Ulong"), &XmlText::GetUlong, &XmlText::SetUlong) + .Prop(_SC("Bool"), &XmlText::GetBool, &XmlText::SetBool) + .Prop(_SC("Data"), &XmlText::GetData) + // Member Methods + .Func(_SC("AsString"), &XmlText::AsString) + .Func(_SC("AsInt"), &XmlText::AsInt) + .Func(_SC("AsUint"), &XmlText::AsUint) + .Func(_SC("AsFloat"), &XmlText::AsFloat) + .Func(_SC("AsDouble"), &XmlText::AsDouble) + .Func(_SC("AsLong"), &XmlText::AsLong) + .Func(_SC("AsUlong"), &XmlText::AsUlong) + .Func(_SC("AsBool"), &XmlText::AsBool) + .Func(_SC("SetString"), &XmlText::ApplyString) + .Func(_SC("SetInt"), &XmlText::ApplyInt) + .Func(_SC("SetUint"), &XmlText::ApplyUint) + .Func(_SC("SetFloat"), &XmlText::ApplyFloat) + .Func(_SC("SetDouble"), &XmlText::ApplyDouble) + .Func(_SC("SetLong"), &XmlText::ApplyLong) + .Func(_SC("SetUlong"), &XmlText::ApplyUlong) + .Func(_SC("SetBool"), &XmlText::ApplyBool) + ); + + xmlns.Bind(_SC("XmlNode"), Class< XmlNode >(vm, _SC("SqXmlNode")) + // Constructors + .Ctor() + .Ctor< const XmlNode & >() + // Core Meta-methods + .Func(_SC("_cmp"), &XmlNode::Cmp) + .SquirrelFunc(_SC("_typename"), &XmlNode::Typename) + .Func(_SC("_tostring"), &XmlNode::ToString) + // Properties + .Prop(_SC("Valid"), &XmlNode::IsValid) + .Prop(_SC("References"), &XmlNode::GetRefCount) + .Prop(_SC("Empty"), &XmlNode::IsEmpty) + .Prop(_SC("Hash"), &XmlNode::GetHashValue) + .Prop(_SC("OffsetDebug"), &XmlNode::GetOffsetDebug) + .Prop(_SC("Type"), &XmlNode::GetType) + .Prop(_SC("Name"), &XmlNode::GetName, &XmlNode::SetName) + .Prop(_SC("Value"), &XmlNode::GetValue, &XmlNode::SetValue) + .Prop(_SC("FirstAttr"), &XmlNode::GetFirstAttr) + .Prop(_SC("LastAttr"), &XmlNode::GetLastAttr) + .Prop(_SC("FirstChild"), &XmlNode::GetFirstChild) + .Prop(_SC("LastChild"), &XmlNode::GetLastChild) + .Prop(_SC("NextSibling"), &XmlNode::GetNextSibling) + .Prop(_SC("PrevSibling"), &XmlNode::GetPrevSibling) + .Prop(_SC("Parent"), &XmlNode::GetParent) + .Prop(_SC("Root"), &XmlNode::GetRoot) + .Prop(_SC("XmlText"), &XmlNode::GetText) + .Prop(_SC("ChildValue"), &XmlNode::GetChildValue) + // Member Methods + .Overload< XmlParseResult (XmlNode::*)(CSStr) >(_SC("AppendBuffer"), &XmlNode::AppendBuffer) + .Overload< XmlParseResult (XmlNode::*)(CSStr, Uint32) >(_SC("AppendBuffer"), &XmlNode::AppendBuffer) + .Overload< XmlParseResult (XmlNode::*)(CSStr, Uint32, Int32) >(_SC("AppendBuffer"), &XmlNode::AppendBuffer) + .Func(_SC("SetName"), &XmlNode::ApplyName) + .Func(_SC("SetValue"), &XmlNode::ApplyValue) + .Func(_SC("GetChild"), &XmlNode::Child) + .Func(_SC("GetAttr"), &XmlNode::GetAttribute) + .Func(_SC("GetAttribute"), &XmlNode::GetAttribute) + .Func(_SC("GetAttrFrom"), &XmlNode::AttributeFrom) + .Func(_SC("GetAttributeFrom"), &XmlNode::AttributeFrom) + .Func(_SC("GetNextSibling"), &XmlNode::NextSibling) + .Func(_SC("GetPrevSibling"), &XmlNode::PrevSibling) + .Func(_SC("GetChildValue"), &XmlNode::ChildValue) + .Func(_SC("AppendAttr"), &XmlNode::AppendAttr) + .Func(_SC("PrependAttr"), &XmlNode::PrependAttr) + .Func(_SC("InsertAttrAfter"), &XmlNode::InsertAttrAfter) + .Func(_SC("InsertAttrBefore"), &XmlNode::InsertAttrBefore) + .Func(_SC("AppendAttrCopy"), &XmlNode::AppendAttrCopy) + .Func(_SC("PrependAttrCopy"), &XmlNode::PrependAttrCopy) + .Func(_SC("InsertAttrCopyAfter"), &XmlNode::InsertAttrCopyAfter) + .Func(_SC("InsertAttrCopyBefore"), &XmlNode::InsertAttrCopyBefore) + .Func(_SC("AppendChild"), &XmlNode::AppendChild) + .Func(_SC("PrependChild"), &XmlNode::PrependChild) + .Func(_SC("AppendChildNode"), &XmlNode::AppendChildNode) + .Func(_SC("PrependChildNode"), &XmlNode::PrependChildNode) + .Func(_SC("AppendChildType"), &XmlNode::AppendChildType) + .Func(_SC("PrependChildType"), &XmlNode::PrependChildType) + .Func(_SC("InsertChildAfter"), &XmlNode::InsertChildAfter) + .Func(_SC("InsertChildBefore"), &XmlNode::InsertChildBefore) + .Func(_SC("InsertChildTypeAfter"), &XmlNode::InsertChildTypeAfter) + .Func(_SC("InsertChildTypeBefore"), &XmlNode::InsertChildTypeBefore) + .Func(_SC("AppendCopy"), &XmlNode::AppendCopy) + .Func(_SC("PrependCopy"), &XmlNode::PrependCopy) + .Func(_SC("InsertCopyAfter"), &XmlNode::InsertCopyAfter) + .Func(_SC("InsertCopyBefore"), &XmlNode::InsertCopyBefore) + .Func(_SC("AppendMove"), &XmlNode::AppendMove) + .Func(_SC("PrependMove"), &XmlNode::PrependMove) + .Func(_SC("InsertMoveAfter"), &XmlNode::InsertMoveAfter) + .Func(_SC("InsertMoveBefore"), &XmlNode::InsertMoveBefore) + .Func(_SC("RemoveAttr"), &XmlNode::RemoveAttr) + .Func(_SC("RemoveAttrInst"), &XmlNode::RemoveAttrInst) + .Func(_SC("RemoveChild"), &XmlNode::RemoveChild) + .Func(_SC("RemoveChildInst"), &XmlNode::RemoveChildInst) + .Overload< XmlNode (XmlNode::*)(CSStr, CSStr) const >(_SC("FindChildByAttr"), &XmlNode::FindChildByAttr) + .Overload< XmlNode (XmlNode::*)(CSStr, CSStr, CSStr) const >(_SC("FindChildByAttr"), &XmlNode::FindChildByAttr) + .Func(_SC("FindElemByPath"), &XmlNode::FindElemByPath) + ); + + xmlns.Bind(_SC("XmlDocument"), Class< XmlDocument, NoCopy< XmlDocument > >(vm, _SC("SqXmlDocument")) + // Constructors + .Ctor() + // Core Meta-methods + .Func(_SC("_cmp"), &XmlDocument::Cmp) + .SquirrelFunc(_SC("_typename"), &XmlDocument::Typename) + .Func(_SC("_tostring"), &XmlDocument::ToString) + // Properties + .Prop(_SC("Valid"), &XmlDocument::IsValid) + .Prop(_SC("References"), &XmlDocument::GetRefCount) + .Prop(_SC("XmlNode"), &XmlDocument::GetNode) + // Member Methods + .Overload< void (XmlDocument::*)(void) > (_SC("Reset"), &XmlDocument::Reset) + .Overload < void (XmlDocument::*)(const XmlDocument &) >(_SC("Reset"), &XmlDocument::Reset) + .Overload< XmlParseResult (XmlDocument::*)(CSStr) >(_SC("LoadString"), &XmlDocument::LoadData) + .Overload< XmlParseResult (XmlDocument::*)(CSStr, Uint32) >(_SC("LoadString"), &XmlDocument::LoadData) + .Overload< XmlParseResult (XmlDocument::*)(CSStr) >(_SC("LoadFile"), &XmlDocument::LoadFile) + .Overload< XmlParseResult (XmlDocument::*)(CSStr, Uint32) >(_SC("LoadFile"), &XmlDocument::LoadFile) + .Overload< XmlParseResult (XmlDocument::*)(CSStr, Uint32, Int32) >(_SC("LoadFile"), &XmlDocument::LoadFile) + .Overload < void (XmlDocument::*)(CSStr) > (_SC("SaveFile"), &XmlDocument::SaveFile) + .Overload < void (XmlDocument::*)(CSStr, CSStr) > (_SC("SaveFile"), &XmlDocument::SaveFile) + .Overload < void (XmlDocument::*)(CSStr, CSStr, Uint32) > (_SC("SaveFile"), &XmlDocument::SaveFile) + .Overload < void (XmlDocument::*)(CSStr, CSStr, Uint32, Int32) > (_SC("SaveFile"), &XmlDocument::SaveFile) + ); + + RootTable(vm).Bind(_SC("SqXml"), xmlns); + + ConstTable(vm).Enum(_SC("SqXmlNodeType"), Enumeration(vm) + .Const(_SC("Null"), static_cast< Int32 >(node_null)) + .Const(_SC("XmlDocument"), static_cast< Int32 >(node_document)) + .Const(_SC("Element"), static_cast< Int32 >(node_element)) + .Const(_SC("PCData"), static_cast< Int32 >(node_pcdata)) + .Const(_SC("CData"), static_cast< Int32 >(node_cdata)) + .Const(_SC("Comment"), static_cast< Int32 >(node_comment)) + .Const(_SC("Pi"), static_cast< Int32 >(node_pi)) + .Const(_SC("Declaration"), static_cast< Int32 >(node_declaration)) + .Const(_SC("Doctype"), static_cast< Int32 >(node_doctype)) + ); + + ConstTable(vm).Enum(_SC("SqXmlParse"), Enumeration(vm) + .Const(_SC("Minimal"), static_cast< Int32 >(parse_minimal)) + .Const(_SC("Default"), static_cast< Int32 >(parse_default)) + .Const(_SC("Full"), static_cast< Int32 >(parse_full)) + .Const(_SC("Pi"), static_cast< Int32 >(parse_pi)) + .Const(_SC("Comments"), static_cast< Int32 >(parse_comments)) + .Const(_SC("CData"), static_cast< Int32 >(parse_cdata)) + .Const(_SC("WSPCData"), static_cast< Int32 >(parse_ws_pcdata)) + .Const(_SC("Escapes"), static_cast< Int32 >(parse_escapes)) + .Const(_SC("EOL"), static_cast< Int32 >(parse_eol)) + .Const(_SC("WConvAttribute"), static_cast< Int32 >(parse_wconv_attribute)) + .Const(_SC("WNormAttribute"), static_cast< Int32 >(parse_wnorm_attribute)) + .Const(_SC("Declaration"), static_cast< Int32 >(parse_declaration)) + .Const(_SC("Doctype"), static_cast< Int32 >(parse_doctype)) + .Const(_SC("WSPCDataSingle"), static_cast< Int32 >(parse_ws_pcdata_single)) + .Const(_SC("TrimPCData"), static_cast< Int32 >(parse_trim_pcdata)) + .Const(_SC("Fragment"), static_cast< Int32 >(parse_fragment)) + .Const(_SC("EmbedPCData"), static_cast< Int32 >(parse_embed_pcdata)) + ); + + ConstTable(vm).Enum(_SC("SqXmlEncoding"), Enumeration(vm) + .Const(_SC("Auto"), static_cast< Int32 >(encoding_auto)) + .Const(_SC("Utf8"), static_cast< Int32 >(encoding_utf8)) + .Const(_SC("Utf16LE"), static_cast< Int32 >(encoding_utf16_le)) + .Const(_SC("Utf16BE"), static_cast< Int32 >(encoding_utf16_be)) + .Const(_SC("Utf16"), static_cast< Int32 >(encoding_utf16)) + .Const(_SC("Utf32LE"), static_cast< Int32 >(encoding_utf32_le)) + .Const(_SC("Utf32BE"), static_cast< Int32 >(encoding_utf32_be)) + .Const(_SC("Utf32"), static_cast< Int32 >(encoding_utf32)) + .Const(_SC("WChar"), static_cast< Int32 >(encoding_wchar)) + .Const(_SC("Latin1"), static_cast< Int32 >(encoding_latin1)) + ); + + ConstTable(vm).Enum(_SC("SqXmlFormat"), Enumeration(vm) + .Const(_SC("Indent"), static_cast< Int32 >(format_indent)) + .Const(_SC("WriteBOM"), static_cast< Int32 >(format_write_bom)) + .Const(_SC("Raw"), static_cast< Int32 >(format_raw)) + .Const(_SC("NoDeclaration"), static_cast< Int32 >(format_no_declaration)) + .Const(_SC("NoEscapes"), static_cast< Int32 >(format_no_escapes)) + .Const(_SC("SaveFileText"), static_cast< Int32 >(format_save_file_text)) + .Const(_SC("IndentAttributes"), static_cast< Int32 >(format_indent_attributes)) + .Const(_SC("Default"), static_cast< Int32 >(format_default)) + ); + + ConstTable(vm).Enum(_SC("SqXmlParseStatus"), Enumeration(vm) + .Const(_SC("Ok"), static_cast< Int32 >(status_ok)) + .Const(_SC("FileNotFound"), static_cast< Int32 >(status_file_not_found)) + .Const(_SC("IOError"), static_cast< Int32 >(status_io_error)) + .Const(_SC("OutOfMemory"), static_cast< Int32 >(status_out_of_memory)) + .Const(_SC("InternalError"), static_cast< Int32 >(status_internal_error)) + .Const(_SC("UnrecognizedTag"), static_cast< Int32 >(status_unrecognized_tag)) + .Const(_SC("BadPi"), static_cast< Int32 >(status_bad_pi)) + .Const(_SC("BadComment"), static_cast< Int32 >(status_bad_comment)) + .Const(_SC("BadCData"), static_cast< Int32 >(status_bad_cdata)) + .Const(_SC("BadDoctype"), static_cast< Int32 >(status_bad_doctype)) + .Const(_SC("BadPCData"), static_cast< Int32 >(status_bad_pcdata)) + .Const(_SC("BadStartElement"), static_cast< Int32 >(status_bad_start_element)) + .Const(_SC("BadAttribute"), static_cast< Int32 >(status_bad_attribute)) + .Const(_SC("BadEndElement"), static_cast< Int32 >(status_bad_end_element)) + .Const(_SC("EndElementMismatch"), static_cast< Int32 >(status_end_element_mismatch)) + .Const(_SC("AppendInvalidRoot"), static_cast< Int32 >(status_append_invalid_root)) + .Const(_SC("NoDocumentElement"), static_cast< Int32 >(status_no_document_element)) + ); + + ConstTable(vm).Enum(_SC("SqXmlXpathValueType"), Enumeration(vm) + .Const(_SC("None"), static_cast< Int32 >(xpath_type_none)) + .Const(_SC("NodeSet"), static_cast< Int32 >(xpath_type_node_set)) + .Const(_SC("Number"), static_cast< Int32 >(xpath_type_number)) + .Const(_SC("String"), static_cast< Int32 >(xpath_type_string)) + .Const(_SC("Boolean"), static_cast< Int32 >(xpath_type_boolean)) + ); +} + +} // Namespace:: SqMod diff --git a/module/Library/XML.hpp b/module/Library/XML.hpp new file mode 100644 index 00000000..4f307161 --- /dev/null +++ b/module/Library/XML.hpp @@ -0,0 +1,10 @@ +#pragma once + +// ------------------------------------------------------------------------------------------------ +#include "Base/Shared.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + + +} // Namespace:: SqMod diff --git a/module/Library/XML/Attribute.cpp b/module/Library/XML/Attribute.cpp new file mode 100644 index 00000000..22f1a8a6 --- /dev/null +++ b/module/Library/XML/Attribute.cpp @@ -0,0 +1,63 @@ +// ------------------------------------------------------------------------------------------------ +#include "Library/XML/Attribute.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +// ------------------------------------------------------------------------------------------------ +SQInteger XmlAttribute::Typename(HSQUIRRELVM vm) +{ + static const SQChar name[] = _SC("SqXmlAttribute"); + sq_pushstring(vm, name, sizeof(name)); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +LightObj XmlAttribute::AsLong(const SLongInt & def) const +{ + return LightObj(SqTypeIdentity< SLongInt >{}, SqVM(), m_Attr.as_llong(def.GetNum())); +} + +// ------------------------------------------------------------------------------------------------ +LightObj XmlAttribute::AsUlong(const ULongInt & def) const +{ + return LightObj(SqTypeIdentity< ULongInt >{}, SqVM(), m_Attr.as_ullong(def.GetNum())); +} + +// ------------------------------------------------------------------------------------------------ +bool XmlAttribute::ApplyLong(const SLongInt & value) +{ + return m_Attr.set_value(value.GetNum()); +} + +// ------------------------------------------------------------------------------------------------ +bool XmlAttribute::ApplyUlong(const ULongInt & value) +{ + return m_Attr.set_value(value.GetNum()); +} + +// ------------------------------------------------------------------------------------------------ +LightObj XmlAttribute::GetLong() const +{ + return LightObj(SqTypeIdentity< SLongInt >{}, SqVM(), m_Attr.as_llong()); +} + +// ------------------------------------------------------------------------------------------------ +void XmlAttribute::SetLong(const SLongInt & value) +{ + m_Attr = value.GetNum(); +} + +// ------------------------------------------------------------------------------------------------ +LightObj XmlAttribute::GetUlong() const +{ + return LightObj(SqTypeIdentity< ULongInt >{}, SqVM(), m_Attr.as_ullong()); +} + +// ------------------------------------------------------------------------------------------------ +void XmlAttribute::SetUlong(const ULongInt & value) +{ + m_Attr = value.GetNum(); +} + +} // Namespace:: SqMod diff --git a/module/Library/XML/Attribute.hpp b/module/Library/XML/Attribute.hpp new file mode 100644 index 00000000..d589e30c --- /dev/null +++ b/module/Library/XML/Attribute.hpp @@ -0,0 +1,442 @@ +#pragma once + +// ------------------------------------------------------------------------------------------------ +#include "Library/XML/Common.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +/* ------------------------------------------------------------------------------------------------ + * A light-weight handle for manipulating attributes in DOM tree. +*/ +class XmlAttribute +{ + // -------------------------------------------------------------------------------------------- + friend class XmlNode; + +protected: + + // -------------------------------------------------------------------------------------------- + typedef xml_attribute Type; + + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + XmlAttribute(const DocumentRef doc, const Type & attr) + : m_Doc(doc), m_Attr(attr) + { + /* ... */ + } + +private: + + // --------------------------------------------------------------------------------------------- + DocumentRef m_Doc; // The main xml document instance. + Type m_Attr; // The managed node attribute. + +public: + + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + XmlAttribute() + : m_Doc(), m_Attr() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Copy constructor. (disabled) + */ + XmlAttribute(const XmlAttribute & o) + : m_Doc(o.m_Doc), m_Attr(o.m_Attr) + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~XmlAttribute() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator. (disabled) + */ + XmlAttribute & operator = (const XmlAttribute & o) + { + m_Doc = o.m_Doc; + m_Attr = o.m_Attr; + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to compare two instances of this type. + */ + Int32 Cmp(const XmlAttribute & o) + { + if (m_Attr == o.m_Attr) + { + return 0; + } + else if (m_Attr > o.m_Attr) + { + return 1; + } + else + { + return -1; + } + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a string. + */ + CSStr ToString() const + { + return m_Attr.value(); + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to retrieve the name from instances of this type. + */ + static SQInteger Typename(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * See whether this instance references a valid xml document. + */ + bool IsValid() const + { + return m_Doc; + } + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to this document instance. + */ + Uint32 GetRefCount() const + { + return m_Doc.Count(); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the attribute is empty. + */ + bool IsEmpty() const + { + return m_Attr.empty(); + } + + /* -------------------------------------------------------------------------------------------- + * Get hash value (unique for handles to the same object). + */ + SQInteger GetHashValue() const + { + return (SQInteger)m_Attr.hash_value(); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve attribute name. + */ + CSStr GetName() const + { + return m_Attr.name(); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve attribute name. + */ + void SetName(CSStr name) + { + if (!m_Attr.set_name(name)) + { + STHROWF("Unable to set XML attribute name"); + } + } + + /* -------------------------------------------------------------------------------------------- + * Modify the attribute name. + */ + bool ApplyName(CSStr name) + { + return m_Attr.set_name(name); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve attribute value. + */ + CSStr GetValue() const + { + return m_Attr.value(); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve attribute value. + */ + void SetValue(CSStr name) + { + if (!m_Attr.set_value(name)) + { + STHROWF("Unable to set XML attribute value"); + } + } + + /* -------------------------------------------------------------------------------------------- + * Modify the attribute value. + */ + bool ApplyValue(CSStr value) + { + return m_Attr.set_value(value); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a string or the specified default value if empty. + */ + CSStr AsString(CSStr def) const + { + return m_Attr.as_string(def); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a integer or the specified default value if empty. + */ + Int32 AsInt(Int32 def) const + { + return m_Attr.as_int(def); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a unsigned integer or the specified default value if empty. + */ + Uint32 AsUint(Uint32 def) const + { + return m_Attr.as_uint(def); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a floating point or the specified default value if empty. + */ + SQFloat AsFloat(SQFloat def) const + { + return (SQFloat)m_Attr.as_float(def); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a double floating point or the specified default value if empty. + */ + SQFloat AsDouble(SQFloat def) const + { + return (SQFloat)m_Attr.as_double(def); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a long integer or the specified default value if empty. + */ + LightObj AsLong(const SLongInt & def) const; + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a unsigned long integer or the specified default value if empty. + */ + LightObj AsUlong(const ULongInt & def) const; + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a boolean or the specified default value if empty. + */ + bool AsBool(bool def) const + { + return m_Attr.as_bool(def); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a string. + */ + bool ApplyString(CSStr value) + { + return m_Attr.set_value(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a integer. + */ + bool ApplyInt(Int32 value) + { + return m_Attr.set_value(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a unsigned integer. + */ + bool ApplyUint(Uint32 value) + { + return m_Attr.set_value(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a floating point. + */ + bool ApplyFloat(SQFloat value) + { + return m_Attr.set_value(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a double floating point. + */ + bool ApplyDouble(SQFloat value) + { + return m_Attr.set_value(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a long integer. + */ + bool ApplyLong(const SLongInt & value); + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a unsigned long integer. + */ + bool ApplyUlong(const ULongInt & value); + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a boolean. + */ + bool ApplyBool(bool value) + { + return m_Attr.set_value(value); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a string. + */ + CSStr GetString() const + { + return m_Attr.as_string(); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a string. + */ + void SetString(CSStr value) + { + m_Attr = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a integer. + */ + Int32 GetInt() const + { + return m_Attr.as_int(); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a integer. + */ + void SetInt(Int32 value) + { + m_Attr = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a unsigned integer. + */ + Uint32 GetUint() const + { + return m_Attr.as_uint(); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a unsigned integer. + */ + void SetUint(Uint32 value) + { + m_Attr = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a floating point. + */ + SQFloat GetFloat() const + { + return static_cast< SQFloat >(m_Attr.as_float()); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a floating point. + */ + void SetFloat(SQFloat value) + { + m_Attr = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a double floating point. + */ + SQFloat GetDouble() const + { + return static_cast< SQFloat >(m_Attr.as_double()); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a double floating point. + */ + void SetDouble(SQFloat value) + { + m_Attr = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a long integer. + */ + LightObj GetLong() const; + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a long integer. + */ + void SetLong(const SLongInt & value); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a unsigned long integer. + */ + LightObj GetUlong() const; + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a unsigned long integer. + */ + void SetUlong(const ULongInt & value); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a boolean. + */ + bool GetBool() const + { + return m_Attr.as_bool(); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a boolean. + */ + void SetBool(bool value) + { + m_Attr = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve next attribute in the attribute list of the parent node. + */ + XmlAttribute NextAttribute() const + { + return XmlAttribute(m_Doc, m_Attr.next_attribute()); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve previous attribute in the attribute list of the parent node. + */ + XmlAttribute PrevAttribute() const + { + return XmlAttribute(m_Doc, m_Attr.previous_attribute()); + } +}; + +} // Namespace:: SqMod diff --git a/module/Library/XML/Common.cpp b/module/Library/XML/Common.cpp new file mode 100644 index 00000000..8553a799 --- /dev/null +++ b/module/Library/XML/Common.cpp @@ -0,0 +1,43 @@ +// ------------------------------------------------------------------------------------------------ +#include "Library/XML/Common.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +// ------------------------------------------------------------------------------------------------ +void DocumentRef::Validate() const +{ + if (!m_Ptr) + { + STHROWF("Invalid XML document reference"); + } +} + +// ------------------------------------------------------------------------------------------------ +SQInteger XmlParseResult::Typename(HSQUIRRELVM vm) +{ + static const SQChar name[] = _SC("SqXmlParseResult"); + sq_pushstring(vm, name, sizeof(name)); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +void XmlParseResult::Validate() const +{ + // Is the documen handle valid? + if (!m_Doc) + { + STHROWF("Invalid XML document reference"); + } +} + +// ------------------------------------------------------------------------------------------------ +void XmlParseResult::Check() const +{ + if (m_Result.status != status_ok) + { + STHROWF("XML parse error [%s]", m_Result.description()); + } +} + +} // Namespace:: SqMod diff --git a/module/Library/XML/Common.hpp b/module/Library/XML/Common.hpp new file mode 100644 index 00000000..1451424d --- /dev/null +++ b/module/Library/XML/Common.hpp @@ -0,0 +1,434 @@ +#pragma once + +// ------------------------------------------------------------------------------------------------ +#include "Base/Shared.hpp" + +// ------------------------------------------------------------------------------------------------ +#include "Library/Numeric/LongInt.hpp" + +// ------------------------------------------------------------------------------------------------ +#include +#include + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +/* ------------------------------------------------------------------------------------------------ + * SOFTWARE INFORMATION +*/ +#define SQXML_NAME "Squirrel XML Module" +#define SQXML_AUTHOR "Sandu Liviu Catalin (S.L.C)" +#define SQXML_COPYRIGHT "Copyright (C) 2018 Sandu Liviu Catalin" +#define SQXML_HOST_NAME "SqModXMLHost" +#define SQXML_VERSION 001 +#define SQXML_VERSION_STR "0.0.1" +#define SQXML_VERSION_MAJOR 0 +#define SQXML_VERSION_MINOR 0 +#define SQXML_VERSION_PATCH 1 + +// ------------------------------------------------------------------------------------------------ +using namespace pugi; + +// ------------------------------------------------------------------------------------------------ +class XmlNode; +class XmlText; +class XmlDocument; +class XmlAttribute; +class XPathNode; +class XPathNodeSet; +class XPathVariable; +class XPathVariableSet; +class XPathVariableQuery; +class XmlParseResult; + +/* ------------------------------------------------------------------------------------------------ + * Manages a reference counted xml document instance. +*/ +class DocumentRef +{ + // -------------------------------------------------------------------------------------------- + friend class XmlDocument; + +public: + + // -------------------------------------------------------------------------------------------- + typedef xml_document Type; /* The managed type. */ + + // -------------------------------------------------------------------------------------------- + typedef Type* Pointer; /* Pointer to the managed type. */ + typedef const Type* ConstPtr; /* Constant pointer to the managed type. */ + + // -------------------------------------------------------------------------------------------- + typedef Type& Reference; /* Reference to the managed type. */ + typedef const Type& ConstRef; /* Constant reference to the managed type. */ + + // -------------------------------------------------------------------------------------------- + typedef unsigned int Counter; /* Reference counter type. */ + + /* -------------------------------------------------------------------------------------------- + * Validate the managed handle and throw exception if invalid. + */ + void Validate() const; + +private: + + // -------------------------------------------------------------------------------------------- + Pointer m_Ptr; /* The document reader, writer and manager instance. */ + Counter* m_Ref; /* Reference count to the managed instance. */ + + /* -------------------------------------------------------------------------------------------- + * Grab a strong reference to a document instance. + */ + void Grab() + { + if (m_Ptr) + { + ++(*m_Ref); + } + } + + /* -------------------------------------------------------------------------------------------- + * Drop a strong reference to a document instance. + */ + void Drop() + { + if (m_Ptr && --(*m_Ref) == 0) + { + delete m_Ptr; + delete m_Ref; + m_Ptr = NULL; + m_Ref = NULL; + } + } + + /* -------------------------------------------------------------------------------------------- + * Base constructor. + */ + DocumentRef(VoidP /* unused */) + : m_Ptr(new Type()) + , m_Ref(new Counter(1)) + { + /* ... */ + } + +public: + + /* -------------------------------------------------------------------------------------------- + * Default constructor (null). + */ + DocumentRef() + : m_Ptr(NULL), m_Ref(NULL) + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Copy constructor. + */ + DocumentRef(const DocumentRef & o) + : m_Ptr(o.m_Ptr), m_Ref(o.m_Ref) + + { + Grab(); + } + + /* -------------------------------------------------------------------------------------------- + * Move constructor. + */ + DocumentRef(DocumentRef && o) + : m_Ptr(o.m_Ptr), m_Ref(o.m_Ref) + { + o.m_Ptr = NULL; + o.m_Ref = NULL; + } + + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~DocumentRef() + { + Drop(); + } + + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator. + */ + DocumentRef & operator = (const DocumentRef & o) + { + if (m_Ptr != o.m_Ptr) + { + Drop(); + m_Ptr = o.m_Ptr; + m_Ref = o.m_Ref; + Grab(); + } + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * Move assignment operator. + */ + DocumentRef & operator = (DocumentRef && o) + { + if (m_Ptr != o.m_Ptr) + { + m_Ptr = o.m_Ptr; + m_Ref = o.m_Ref; + o.m_Ptr = NULL; + o.m_Ref = NULL; + } + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * Perform an equality comparison between two document instances. + */ + bool operator == (const DocumentRef & o) const + { + return (m_Ptr == o.m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * Perform an inequality comparison between two document instances. + */ + bool operator != (const DocumentRef & o) const + { + return (m_Ptr != o.m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * Implicit conversion to boolean for use in boolean operations. + */ + operator bool () const + { + return m_Ptr; + } + + /* -------------------------------------------------------------------------------------------- + * Implicit conversion to the managed instance pointer. + */ + operator Pointer () + { + return m_Ptr; + } + + /* -------------------------------------------------------------------------------------------- + * Implicit conversion to the managed instance pointer. + */ + operator ConstPtr () const + { + return m_Ptr; + } + + /* -------------------------------------------------------------------------------------------- + * Implicit conversion to the managed instance reference. + */ + operator Reference () + { + assert(m_Ptr); + return *m_Ptr; + } + + /* -------------------------------------------------------------------------------------------- + * Implicit conversion to the managed instance reference. + */ + operator ConstRef () const + { + assert(m_Ptr); + return *m_Ptr; + } + + /* -------------------------------------------------------------------------------------------- + * Member operator for dereferencing the managed pointer. + */ + Pointer operator -> () const + { + assert(m_Ptr); + return m_Ptr; + } + + /* -------------------------------------------------------------------------------------------- + * Indirection operator for obtaining a reference of the managed pointer. + */ + Reference operator * () const + { + assert(m_Ptr); + return *m_Ptr; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the number of active references to the managed instance. + */ + Counter Count() const + { + return (m_Ptr && m_Ref) ? (*m_Ref) : 0; + } +}; + +/* ------------------------------------------------------------------------------------------------ + * Allows the user to inspect the result of certain operations and act accordingly. +*/ +class XmlParseResult +{ + // -------------------------------------------------------------------------------------------- + friend class XmlDocument; + friend class XmlNode; + +protected: + + // -------------------------------------------------------------------------------------------- + typedef xml_parse_result Result; + + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + XmlParseResult(DocumentRef doc, const Result & result) + : m_Doc(std::move(doc)), m_Result(result) + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Validate the document reference and throw an error if invalid. + */ + void Validate() const; + +private: + + // --------------------------------------------------------------------------------------------- + DocumentRef m_Doc; /* The main xml document instance. */ + Result m_Result; /* The managed parse result. */ + +public: + + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + XmlParseResult() + : m_Doc(), m_Result() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Copy constructor. (disabled) + */ + XmlParseResult(const XmlParseResult & o) + : m_Doc(o.m_Doc), m_Result(o.m_Result) + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~XmlParseResult() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator. (disabled) + */ + XmlParseResult & operator = (const XmlParseResult & o) + { + m_Doc = o.m_Doc; + m_Result = o.m_Result; + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to compare two instances of this type. + */ + Int32 Cmp(const XmlParseResult & o) + { + if (m_Result.status == o.m_Result.status) + { + return 0; + } + else if (m_Result.status > o.m_Result.status) + { + return 1; + } + else + { + return -1; + } + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a string. + */ + CSStr ToString() const + { + return m_Result.description(); + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to retrieve the name from instances of this type. + */ + static SQInteger Typename(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * See whether this instance references a valid xml document. + */ + bool IsValid() const + { + return m_Doc; + } + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to this document instance. + */ + Uint32 GetRefCount() const + { + return m_Doc.Count(); + } + + /* -------------------------------------------------------------------------------------------- + * Cast to bool operator. + */ + bool IsOk() const + { + return m_Result; + } + + /* -------------------------------------------------------------------------------------------- + * Parsing status code. + */ + Int32 GetStatus() const + { + return (Int32)m_Result.status; + } + + /* -------------------------------------------------------------------------------------------- + * Last parsed offset. (in char_t units from start of input data) + */ + SQInteger GetOffset() const + { + return (SQInteger)m_Result.offset; + } + + /* -------------------------------------------------------------------------------------------- + * Source document encoding. + */ + Int32 GetEncoding() const + { + return m_Result.encoding; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve error description as a string. + */ + CSStr GetDescription() const + { + return m_Result.description(); + } + + /* -------------------------------------------------------------------------------------------- + * Check the parse result and throw the necessary errors. + */ + void Check() const; +}; + +} // Namespace:: SqMod diff --git a/module/Library/XML/Document.cpp b/module/Library/XML/Document.cpp new file mode 100644 index 00000000..90cfa657 --- /dev/null +++ b/module/Library/XML/Document.cpp @@ -0,0 +1,38 @@ +// ------------------------------------------------------------------------------------------------ +#include "Library/XML/Document.hpp" +#include "Library/XML/Node.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +// ------------------------------------------------------------------------------------------------ +SQInteger XmlDocument::Typename(HSQUIRRELVM vm) +{ + static const SQChar name[] = _SC("SqXmlDocument"); + sq_pushstring(vm, name, sizeof(name)); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +void XmlDocument::CanLoad() const +{ + // Is the document even valid? + m_Doc.Validate(); + // Are there any other references? + if (m_Doc.Count() > 1) + { + // To load new values now, would mean to cause undefined behavior in existing references + STHROWF("Loading is disabled while document is referenced"); + } +} + +// ------------------------------------------------------------------------------------------------ +XmlNode XmlDocument::GetNode() const +{ + // Validate the document handle + m_Doc.Validate(); + // Return the requested information + return XmlNode(m_Doc, m_Doc->document_element()); +} + +} // Namespace:: SqMod \ No newline at end of file diff --git a/module/Library/XML/Document.hpp b/module/Library/XML/Document.hpp new file mode 100644 index 00000000..5679248f --- /dev/null +++ b/module/Library/XML/Document.hpp @@ -0,0 +1,253 @@ +#pragma once + +// ------------------------------------------------------------------------------------------------ +#include "Library/XML/Common.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +/* ------------------------------------------------------------------------------------------------ + * Class that can read/write and alter the contents of XML files. +*/ +class XmlDocument +{ +protected: + + // -------------------------------------------------------------------------------------------- + typedef xml_document Type; + + /* -------------------------------------------------------------------------------------------- + * Copy constructor. (disabled) + */ + XmlDocument(const XmlDocument & o); + + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator. (disabled) + */ + XmlDocument & operator = (const XmlDocument & o); + + /* -------------------------------------------------------------------------------------------- + * See if the document is allowed to overwrite its contents and throw an error if not. + */ + void CanLoad() const; + +private: + + // --------------------------------------------------------------------------------------------- + DocumentRef m_Doc; // The main xml document instance. + +public: + + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + XmlDocument() + : m_Doc(nullptr) + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~XmlDocument() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to compare two instances of this type. + */ + Int32 Cmp(const XmlDocument & o) const + { + if (m_Doc && !o.m_Doc) + { + return 1; + } + else if (!m_Doc && o.m_Doc) + { + return -1; + } + else if (!m_Doc && !o.m_Doc) + { + return 0; + } + else if (*m_Doc == *o.m_Doc) + { + return 0; + } + else if (*m_Doc > *o.m_Doc) + { + return 1; + } + else + { + return -1; + } + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a string. + */ + CSStr ToString() const + { + // Do we manage a valid document? + if (m_Doc) + { + return m_Doc->name(); + } + // Default to an empty name + return _SC(""); + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to retrieve the name from instances of this type. + */ + static SQInteger Typename(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * See whether this instance references a valid xml document. + */ + bool IsValid() const + { + return m_Doc; + } + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to this document instance. + */ + Uint32 GetRefCount() const + { + return m_Doc.Count(); + } + + /* -------------------------------------------------------------------------------------------- + * Removes all nodes, leaving the empty document. + */ + void Reset() + { + // Validate the document handle + m_Doc.Validate(); + // Perform the requested operation + m_Doc->reset(); + } + + /* -------------------------------------------------------------------------------------------- + * Removes all nodes, then copies the entire contents of the specified document. + */ + void Reset(const XmlDocument & doc) + { + // Validate the document handles + m_Doc.Validate(); + doc.m_Doc.Validate(); + // Perform the requested operation + m_Doc->reset(*(doc.m_Doc)); + } + + /* -------------------------------------------------------------------------------------------- + * Load document from zero-terminated string. (LoadString collides with the windows api) + */ + XmlParseResult LoadData(CSStr source) + { + // Make sure that we are allowed to load in data + CanLoad(); + // Perform the requested operation and return the result + return XmlParseResult(m_Doc, m_Doc->load_string(source)); + } + + /* -------------------------------------------------------------------------------------------- + * Load document from zero-terminated string. (LoadString collides with the windows api) + */ + XmlParseResult LoadData(CSStr source, Uint32 options) + { + // Make sure that we are allowed to load in data + CanLoad(); + // Perform the requested operation and return the result + return XmlParseResult(m_Doc, m_Doc->load_string(source, options)); + } + + /* -------------------------------------------------------------------------------------------- + * Load document from file on disk. + */ + XmlParseResult LoadFile(CSStr filepath) + { + // Make sure that we are allowed to load in data + CanLoad(); + // Perform the requested operation and return the result + return XmlParseResult(m_Doc, m_Doc->load_file(filepath)); + } + + /* -------------------------------------------------------------------------------------------- + * Load document from file on disk. + */ + XmlParseResult LoadFile(CSStr filepath, Uint32 options) + { + // Make sure that we are allowed to load in data + CanLoad(); + // Perform the requested operation and return the result + return XmlParseResult(m_Doc, m_Doc->load_file(filepath, options)); + } + + /* -------------------------------------------------------------------------------------------- + * Load document from file on disk. + */ + XmlParseResult LoadFile(CSStr filepath, Uint32 options, Int32 encoding) + { + // Make sure that we are allowed to load in data + CanLoad(); + // Perform the requested operation and return the result + return XmlParseResult(m_Doc, m_Doc->load_file(filepath, options, + static_cast< xml_encoding >(encoding))); + } + + /* -------------------------------------------------------------------------------------------- + * Save XML to file on disk. + */ + void SaveFile(CSStr filepath) + { + // Validate the document handle + m_Doc.Validate(); + // Perform the requested operation + m_Doc->save_file(filepath); + } + + /* -------------------------------------------------------------------------------------------- + * Save XML to file on disk. + */ + void SaveFile(CSStr filepath, CSStr indent) + { + // Validate the document handle + m_Doc.Validate(); + // Perform the requested operation + m_Doc->save_file(filepath, indent); + } + + /* -------------------------------------------------------------------------------------------- + * Save XML to file on disk. + */ + void SaveFile(CSStr filepath, CSStr indent, Uint32 format) + { + // Validate the document handle + m_Doc.Validate(); + // Perform the requested operation + m_Doc->save_file(filepath, indent, format); + } + + /* -------------------------------------------------------------------------------------------- + * Save XML to file on disk. + */ + void SaveFile(CSStr filepath, CSStr indent, Uint32 format, Int32 encoding) + { + // Validate the document handle + m_Doc.Validate(); + // Perform the requested operation + m_Doc->save_file(filepath, indent, format, static_cast< xml_encoding >(encoding)); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the document root node. + */ + XmlNode GetNode() const; +}; + +} // Namespace:: SqMod diff --git a/module/Library/XML/Node.cpp b/module/Library/XML/Node.cpp new file mode 100644 index 00000000..76e01566 --- /dev/null +++ b/module/Library/XML/Node.cpp @@ -0,0 +1,101 @@ +// ------------------------------------------------------------------------------------------------ +#include "Library/XML/Node.hpp" +#include "Library/XML/Attribute.hpp" +#include "Library/XML/Text.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +// ------------------------------------------------------------------------------------------------ +SQInteger XmlNode::Typename(HSQUIRRELVM vm) +{ + static const SQChar name[] = _SC("SqXmlNode"); + sq_pushstring(vm, name, sizeof(name)); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::GetFirstAttr() const +{ + return XmlAttribute(m_Doc, m_Node.first_attribute()); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::GetLastAttr() const +{ + return XmlAttribute(m_Doc, m_Node.last_attribute()); +} + +// ------------------------------------------------------------------------------------------------ +XmlText XmlNode::GetText() const +{ + return XmlText(m_Doc, m_Node.text()); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::GetAttribute(CSStr name) const +{ + return XmlAttribute(m_Doc, m_Node.attribute(name)); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::AttributeFrom(CSStr name, XmlAttribute & attr) const +{ + return XmlAttribute(m_Doc, m_Node.attribute(name, attr.m_Attr)); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::AppendAttr(CSStr name) +{ + return XmlAttribute(m_Doc, m_Node.append_attribute(name)); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::PrependAttr(CSStr name) +{ + return XmlAttribute(m_Doc, m_Node.prepend_attribute(name)); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::InsertAttrAfter(CSStr name, const XmlAttribute & attr) +{ + return XmlAttribute(m_Doc, m_Node.insert_attribute_after(name, attr.m_Attr)); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::InsertAttrBefore(CSStr name, const XmlAttribute & attr) +{ + return XmlAttribute(m_Doc, m_Node.insert_attribute_before(name, attr.m_Attr)); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::AppendAttrCopy(const XmlAttribute & proto) +{ + return XmlAttribute(m_Doc, m_Node.append_copy(proto.m_Attr)); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::PrependAttrCopy(const XmlAttribute & proto) +{ + return XmlAttribute(m_Doc, m_Node.prepend_copy(proto.m_Attr)); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::InsertAttrCopyAfter(const XmlAttribute & proto, const XmlAttribute & attr) +{ + return XmlAttribute(m_Doc, m_Node.insert_copy_after(proto.m_Attr, attr.m_Attr)); +} + +// ------------------------------------------------------------------------------------------------ +XmlAttribute XmlNode::InsertAttrCopyBefore(const XmlAttribute & proto, const XmlAttribute & attr) +{ + return XmlAttribute(m_Doc, m_Node.insert_copy_before(proto.m_Attr, attr.m_Attr)); +} + +// ------------------------------------------------------------------------------------------------ +bool XmlNode::RemoveAttrInst(const XmlAttribute & attr) +{ + return m_Node.remove_attribute(attr.m_Attr); +} + +} // Namespace:: SqMod diff --git a/module/Library/XML/Node.hpp b/module/Library/XML/Node.hpp new file mode 100644 index 00000000..788e314c --- /dev/null +++ b/module/Library/XML/Node.hpp @@ -0,0 +1,609 @@ +#pragma once + +// ------------------------------------------------------------------------------------------------ +#include "Library/XML/Common.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +/* ------------------------------------------------------------------------------------------------ + * A light-weight handle for manipulating nodes in DOM tree. +*/ +class XmlNode +{ + // -------------------------------------------------------------------------------------------- + friend class XmlDocument; + friend class XmlText; + +protected: + + // -------------------------------------------------------------------------------------------- + typedef xml_node Type; + + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + XmlNode(const DocumentRef doc, const Type & node) + : m_Doc(doc), m_Node(node) + { + /* ... */ + } + +private: + + // --------------------------------------------------------------------------------------------- + DocumentRef m_Doc; // The main xml document instance. + Type m_Node; // The managed document node. + +public: + + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + XmlNode() + : m_Doc(), m_Node() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Copy constructor. (disabled) + */ + XmlNode(const XmlNode & o) + : m_Doc(o.m_Doc), m_Node(o.m_Node) + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~XmlNode() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator. (disabled) + */ + XmlNode & operator = (const XmlNode & o) + { + m_Doc = o.m_Doc; + m_Node = o.m_Node; + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to compare two instances of this type. + */ + Int32 Cmp(const XmlNode & o) + { + if (m_Node == o.m_Node) + { + return 0; + } + else if (m_Node > o.m_Node) + { + return 1; + } + else + { + return -1; + } + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a string. + */ + CSStr ToString() const + { + return m_Node.value(); + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to retrieve the name from instances of this type. + */ + static SQInteger Typename(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * See whether this instance references a valid xml document. + */ + bool IsValid() const + { + return m_Doc; + } + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to this document instance. + */ + Uint32 GetRefCount() const + { + return m_Doc.Count(); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the node is empty. + */ + bool IsEmpty() const + { + return m_Node.empty(); + } + + /* -------------------------------------------------------------------------------------------- + * Get hash value (unique for handles to the same object). + */ + SQInteger GetHashValue() const + { + return static_cast< SQInteger >(m_Node.hash_value()); + } + + /* -------------------------------------------------------------------------------------------- + * Get node offset in parsed file/string (in char_t units) for debugging purposes. + */ + SQInteger GetOffsetDebug() const + { + return static_cast< SQInteger >(m_Node.offset_debug()); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve node type. + */ + Int32 GetType() const + { + return static_cast< Int32 >(m_Node.type()); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve node name. + */ + CSStr GetName() const + { + return m_Node.name(); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve node name. + */ + void SetName(CSStr name) + { + if (!m_Node.set_name(name)) + { + STHROWF("Unable to set XML node name"); + } + } + + /* -------------------------------------------------------------------------------------------- + * Modify the node name. + */ + bool ApplyName(CSStr name) + { + return m_Node.set_name(name); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve node value. + */ + CSStr GetValue() const + { + return m_Node.value(); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve node value. + */ + void SetValue(CSStr name) + { + if (!m_Node.set_value(name)) + { + STHROWF("Unable to set XML node value"); + } + } + + /* -------------------------------------------------------------------------------------------- + * Modify the node value. + */ + bool ApplyValue(CSStr value) + { + return m_Node.set_value(value); + } + + /* -------------------------------------------------------------------------------------------- + * Parses buffer as an XML document fragment and appends all nodes as children of this node. + */ + XmlParseResult AppendBuffer(CSStr source) + { + // Is the specified source buffer even valid? + if (source) + { + return XmlParseResult(m_Doc, m_Node.append_buffer(source, + std::char_traits< SQChar >::length(source) * sizeof(SQChar))); + } + // Return the default result + return XmlParseResult(); + } + + /* -------------------------------------------------------------------------------------------- + * Parses buffer as an XML document fragment and appends all nodes as children of this node. + */ + XmlParseResult AppendBuffer(CSStr source, Uint32 options) + { + // Is the specified source buffer even valid? + if (source) + { + return XmlParseResult(m_Doc, m_Node.append_buffer(source, + std::char_traits< SQChar >::length(source) * sizeof(SQChar), options)); + } + // Return the default result + return XmlParseResult(); + } + + /* -------------------------------------------------------------------------------------------- + * Parses buffer as an XML document fragment and appends all nodes as children of this node. + */ + XmlParseResult AppendBuffer(CSStr source, Uint32 options, Int32 encoding) + { + // Is the specified source buffer even valid? + if (source) + { + return XmlParseResult(m_Doc, m_Node.append_buffer(source, + std::char_traits< SQChar >::length(source) * sizeof(SQChar), + options, (xml_encoding)encoding)); + } + // Return the default result + return XmlParseResult(); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the first child attribute. + */ + XmlAttribute GetFirstAttr() const; + + /* -------------------------------------------------------------------------------------------- + * Retrieve the last child attribute. + */ + XmlAttribute GetLastAttr() const; + + /* -------------------------------------------------------------------------------------------- + * Retrieve the first child node. + */ + XmlNode GetFirstChild() const + { + return XmlNode(m_Doc, m_Node.first_child()); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the last child node. + */ + XmlNode GetLastChild() const + { + return XmlNode(m_Doc, m_Node.last_child()); + } + + /* -------------------------------------------------------------------------------------------- + * Get next sibling in the children list of the parent node. + */ + XmlNode GetNextSibling() const + { + return XmlNode(m_Doc, m_Node.next_sibling()); + } + + /* -------------------------------------------------------------------------------------------- + * Get previous sibling in the children list of the parent node + */ + XmlNode GetPrevSibling() const + { + return XmlNode(m_Doc, m_Node.previous_sibling()); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the parent node. + */ + XmlNode GetParent() const + { + return XmlNode(m_Doc, m_Node.parent()); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the root node. + */ + XmlNode GetRoot() const + { + return XmlNode(m_Doc, m_Node.root()); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the text object for the current node. + */ + XmlText GetText() const; + + /* -------------------------------------------------------------------------------------------- + * Retrieve child node with the specified name. + */ + XmlNode Child(CSStr name) const + { + return XmlNode(m_Doc, m_Node.child(name)); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve child attribute with the specified name. + */ + XmlAttribute GetAttribute(CSStr name) const; + + /* -------------------------------------------------------------------------------------------- + * Retrieve next sibling with the specified name. + */ + XmlNode NextSibling(CSStr name) const + { + return XmlNode(m_Doc, m_Node.next_sibling(name)); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve previous sibling with the specified name. + */ + XmlNode PrevSibling(CSStr name) const + { + return XmlNode(m_Doc, m_Node.previous_sibling(name)); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve child attribute, starting the search from a hint. + */ + XmlAttribute AttributeFrom(CSStr name, XmlAttribute & attr) const; + + /* -------------------------------------------------------------------------------------------- + * Get child value of current node; that is, value of the first child node of type PCDATA/CDATA. + */ + CSStr GetChildValue() const + { + return m_Node.child_value(); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve child value of child with specified name. + */ + CSStr ChildValue(CSStr name) const + { + return m_Node.child_value(name); + } + + /* -------------------------------------------------------------------------------------------- + * Append a child attribute with the specified name. + */ + XmlAttribute AppendAttr(CSStr name); + + /* -------------------------------------------------------------------------------------------- + * Prepend a child attribute with the specified name. + */ + XmlAttribute PrependAttr(CSStr name); + + /* -------------------------------------------------------------------------------------------- + * Insert a child attribute with the specified name, after the specified node. + */ + XmlAttribute InsertAttrAfter(CSStr name, const XmlAttribute & attr); + + /* -------------------------------------------------------------------------------------------- + * Insert a child attribute with the specified name, before the specified node. + */ + XmlAttribute InsertAttrBefore(CSStr name, const XmlAttribute & attr); + + /* -------------------------------------------------------------------------------------------- + * Append a copy of the specified attribute as a child. + */ + XmlAttribute AppendAttrCopy(const XmlAttribute & proto); + + /* -------------------------------------------------------------------------------------------- + * Prepend a copy of the specified attribute as a child. + */ + XmlAttribute PrependAttrCopy(const XmlAttribute & proto); + + /* -------------------------------------------------------------------------------------------- + * Insert a copy of the specified attribute as a child after the specified attribute. + */ + XmlAttribute InsertAttrCopyAfter(const XmlAttribute & proto, const XmlAttribute & attr); + + /* -------------------------------------------------------------------------------------------- + * Insert a copy of the specified attribute as a child before the specified attribute. + */ + XmlAttribute InsertAttrCopyBefore(const XmlAttribute & proto, const XmlAttribute & attr); + + /* -------------------------------------------------------------------------------------------- + * Append a basic child node with the specified name. + */ + XmlNode AppendChild(CSStr name) + { + return XmlNode(m_Doc, m_Node.append_child(name)); + } + + /* -------------------------------------------------------------------------------------------- + * Prepend a basic child node with the specified name. + */ + XmlNode PrependChild(CSStr name) + { + return XmlNode(m_Doc, m_Node.prepend_child(name)); + } + + /* -------------------------------------------------------------------------------------------- + * Append a basic child node. + */ + XmlNode AppendChildNode() + { + return XmlNode(m_Doc, m_Node.append_child()); + } + + /* -------------------------------------------------------------------------------------------- + * Prepend a basic child node. + */ + XmlNode PrependChildNode() + { + return XmlNode(m_Doc, m_Node.prepend_child()); + } + + /* -------------------------------------------------------------------------------------------- + * Append a basic child node with the specified type. + */ + XmlNode AppendChildType(Int32 type) + { + return XmlNode(m_Doc, m_Node.append_child(static_cast< xml_node_type >(type))); + } + + /* -------------------------------------------------------------------------------------------- + * Prepend a basic child node with the specified type. + */ + XmlNode PrependChildType(Int32 type) + { + return XmlNode(m_Doc, m_Node.prepend_child(static_cast< xml_node_type >(type))); + } + + /* -------------------------------------------------------------------------------------------- + * Insert a basic child node with the specified name, after the specified node. + */ + XmlNode InsertChildAfter(CSStr name, const XmlNode & node) + { + return XmlNode(m_Doc, m_Node.insert_child_after(name, node.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Insert a basic child node with the specified name, before the specified node. + */ + XmlNode InsertChildBefore(CSStr name, const XmlNode & node) + { + return XmlNode(m_Doc, m_Node.insert_child_before(name, node.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Insert a basic child node with the specified type, after the specified node. + */ + XmlNode InsertChildTypeAfter(Int32 type, const XmlNode & node) + { + return XmlNode(m_Doc, m_Node.insert_child_after(static_cast< xml_node_type >(type), + node.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Insert a basic child node with the specified type, before the specified node. + */ + XmlNode InsertChildTypeBefore(Int32 type, const XmlNode & node) + { + return XmlNode(m_Doc, m_Node.insert_child_before(static_cast< xml_node_type >(type), + node.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Append a copy of the specified node as a child. + */ + XmlNode AppendCopy(const XmlNode & proto) + { + return XmlNode(m_Doc, m_Node.append_copy(proto.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Prepend a copy of the specified node as a child. + */ + XmlNode PrependCopy(const XmlNode & proto) + { + return XmlNode(m_Doc, m_Node.prepend_copy(proto.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Insert a copy of the specified node as a child after the specified node. + */ + XmlNode InsertCopyAfter(const XmlNode & proto, const XmlNode & node) + { + return XmlNode(m_Doc, m_Node.insert_copy_after(proto.m_Node, node.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Insert a copy of the specified node as a child before the specified node. + */ + XmlNode InsertCopyBefore(const XmlNode & proto, const XmlNode & node) + { + return XmlNode(m_Doc, m_Node.insert_copy_before(proto.m_Node, node.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Append the specified node as a child and take ownership of it. + */ + XmlNode AppendMove(const XmlNode & proto) + { + return XmlNode(m_Doc, m_Node.append_copy(proto.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Prepend the specified node as a child and take ownership of it. + */ + XmlNode PrependMove(const XmlNode & proto) + { + return XmlNode(m_Doc, m_Node.prepend_copy(proto.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Insert the specified node as a child after the specified node and take ownership of it. + */ + XmlNode InsertMoveAfter(const XmlNode & proto, const XmlNode & node) + { + return XmlNode(m_Doc, m_Node.insert_copy_after(proto.m_Node, node.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Insert the specified node as a child before the specified node and take ownership of it. + */ + XmlNode InsertMoveBefore(const XmlNode & proto, const XmlNode & node) + { + return XmlNode(m_Doc, m_Node.insert_copy_before(proto.m_Node, node.m_Node)); + } + + /* -------------------------------------------------------------------------------------------- + * Remove the child attribute matching the specified name. + */ + bool RemoveAttr(CSStr name) + { + return m_Node.remove_attribute(name); + } + + /* -------------------------------------------------------------------------------------------- + * Remove the specified attribute. + */ + bool RemoveAttrInst(const XmlAttribute & attr); + + /* -------------------------------------------------------------------------------------------- + * Remove the child node matching the specified name. + */ + bool RemoveChild(CSStr name) + { + return m_Node.remove_child(name); + } + + /* -------------------------------------------------------------------------------------------- + * Remove the specified node. + */ + bool RemoveChildInst(const XmlNode & node) + { + return m_Node.remove_child(node.m_Node); + } + + /* -------------------------------------------------------------------------------------------- + * Find child node by attribute name/value. + */ + XmlNode FindChildByAttr(CSStr attr_name, CSStr attr_value) const + { + return XmlNode(m_Doc, m_Node.find_child_by_attribute(attr_name, attr_value)); + } + + /* -------------------------------------------------------------------------------------------- + * Find child node by attribute name/value. + */ + XmlNode FindChildByAttr(CSStr name, CSStr attr_name, CSStr attr_value) const + { + return XmlNode(m_Doc, m_Node.find_child_by_attribute(name, attr_name, attr_value)); + } + + /* -------------------------------------------------------------------------------------------- + * Search for a node by path consisting of node names and . or .. elements. + */ + XmlNode FindElemByPath(CSStr path, SQChar delimiter) const + { + return XmlNode(m_Doc, m_Node.first_element_by_path(path, delimiter)); + } +}; + +} // Namespace:: SqMod diff --git a/module/Library/XML/Text.cpp b/module/Library/XML/Text.cpp new file mode 100644 index 00000000..42887756 --- /dev/null +++ b/module/Library/XML/Text.cpp @@ -0,0 +1,90 @@ +// ------------------------------------------------------------------------------------------------ +#include "Library/XML/Text.hpp" +#include "Library/XML/Node.hpp" + +// ------------------------------------------------------------------------------------------------ +#include + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +// ------------------------------------------------------------------------------------------------ +SQInteger XmlText::Typename(HSQUIRRELVM vm) +{ + static const SQChar name[] = _SC("SqXmlText"); + sq_pushstring(vm, name, sizeof(name)); + return 1; +} + +// ------------------------------------------------------------------------------------------------ +Int32 XmlText::Cmp(const XmlText & o) +{ + if (strcmp(m_Text.get(), o.m_Text.get()) == 0) + { + return 0; + } + else if (strlen(m_Text.get()) > strlen(o.m_Text.get())) + { + return 1; + } + else + { + return -1; + } +} + +// ------------------------------------------------------------------------------------------------ +LightObj XmlText::AsLong(const SLongInt & def) const +{ + return LightObj(SqTypeIdentity< SLongInt >{}, SqVM(), m_Text.as_llong(def.GetNum())); +} + +// ------------------------------------------------------------------------------------------------ +LightObj XmlText::AsUlong(const ULongInt & def) const +{ + return LightObj(SqTypeIdentity< ULongInt >{}, SqVM(), m_Text.as_ullong(def.GetNum())); +} + +// ------------------------------------------------------------------------------------------------ +bool XmlText::ApplyLong(const SLongInt & value) +{ + return m_Text.set(value.GetNum()); +} + +// ------------------------------------------------------------------------------------------------ +bool XmlText::ApplyUlong(const ULongInt & value) +{ + return m_Text.set(value.GetNum()); +} + +// ------------------------------------------------------------------------------------------------ +LightObj XmlText::GetLong() const +{ + return LightObj(SqTypeIdentity< SLongInt >{}, SqVM(), m_Text.as_llong()); +} + +// ------------------------------------------------------------------------------------------------ +void XmlText::SetLong(const SLongInt & value) +{ + m_Text = value.GetNum(); +} + +// ------------------------------------------------------------------------------------------------ +LightObj XmlText::GetUlong() const +{ + return LightObj(SqTypeIdentity< SLongInt >{}, SqVM(), m_Text.as_ullong()); +} + +// ------------------------------------------------------------------------------------------------ +void XmlText::SetUlong(const ULongInt & value) +{ + m_Text = value.GetNum(); +} + +// ------------------------------------------------------------------------------------------------ +XmlNode XmlText::GetData() const +{ + return XmlNode(m_Doc, m_Text.data()); +} + +} // Namespace:: SqMod diff --git a/module/Library/XML/Text.hpp b/module/Library/XML/Text.hpp new file mode 100644 index 00000000..ffc2f44d --- /dev/null +++ b/module/Library/XML/Text.hpp @@ -0,0 +1,362 @@ +#pragma once + +// ------------------------------------------------------------------------------------------------ +#include "Library/XML/Common.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +/* ------------------------------------------------------------------------------------------------ + * A helper for working with text inside PCDATA nodes. +*/ +class XmlText +{ + // -------------------------------------------------------------------------------------------- + friend class XmlNode; + +protected: + + // -------------------------------------------------------------------------------------------- + typedef xml_text Type; + + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + XmlText(const DocumentRef doc, const Type & text) + : m_Doc(doc), m_Text(text) + { + /* ... */ + } + +private: + + // --------------------------------------------------------------------------------------------- + DocumentRef m_Doc; // The main xml document instance. + Type m_Text; // The managed document node. + +public: + + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + XmlText() + : m_Doc(), m_Text() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Copy constructor. (disabled) + */ + XmlText(const XmlText & o) + : m_Doc(o.m_Doc), m_Text(o.m_Text) + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~XmlText() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator. (disabled) + */ + XmlText & operator = (const XmlText & o) + { + m_Doc = o.m_Doc; + m_Text = o.m_Text; + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to compare two instances of this type. + */ + Int32 Cmp(const XmlText & o); + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a string. + */ + CSStr ToString() const + { + return m_Text.get(); + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to retrieve the name from instances of this type. + */ + static SQInteger Typename(HSQUIRRELVM vm); + + /* -------------------------------------------------------------------------------------------- + * See whether this instance references a valid xml document. + */ + bool IsValid() const + { + return m_Doc; + } + + /* -------------------------------------------------------------------------------------------- + * Return the number of active references to this document instance. + */ + Uint32 GetRefCount() const + { + return m_Doc.Count(); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the text is empty. + */ + bool IsEmpty() const + { + return m_Text.empty(); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the text value. + */ + CSStr GetValue() const + { + return m_Text.get(); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a string or the specified default value if empty. + */ + CSStr AsString(CSStr def) const + { + return m_Text.as_string(def); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a integer or the specified default value if empty. + */ + Int32 AsInt(Int32 def) const + { + return m_Text.as_int(def); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a unsigned integer or the specified default value if empty. + */ + Uint32 AsUint(Uint32 def) const + { + return m_Text.as_uint(def); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a floating point or the specified default value if empty. + */ + SQFloat AsFloat(SQFloat def) const + { + return static_cast< SQFloat >(m_Text.as_float(static_cast< Float32 >(def))); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a double floating point or the specified default value if empty. + */ + SQFloat AsDouble(SQFloat def) const + { + return static_cast< SQFloat >(m_Text.as_double(static_cast< Float64 >(def))); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a long integer or the specified default value if empty. + */ + LightObj AsLong(const SLongInt & def) const; + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a unsigned long integer or the specified default value if empty. + */ + LightObj AsUlong(const ULongInt & def) const; + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a boolean or the specified default value if empty. + */ + bool AsBool(bool def) const + { + return m_Text.as_bool(def); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a string. + */ + bool ApplyString(CSStr value) + { + return m_Text.set(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a integer. + */ + bool ApplyInt(Int32 value) + { + return m_Text.set(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a unsigned integer. + */ + bool ApplyUint(Uint32 value) + { + return m_Text.set(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a floating point. + */ + bool ApplyFloat(SQFloat value) + { + return m_Text.set(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a double floating point. + */ + bool ApplyDouble(SQFloat value) + { + return m_Text.set(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a long integer. + */ + bool ApplyLong(const SLongInt & value); + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a unsigned long integer. + */ + bool ApplyUlong(const ULongInt & value); + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a boolean. + */ + bool ApplyBool(bool value) + { + return m_Text.set(value); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a string. + */ + CSStr GetString() const + { + return m_Text.as_string(); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a string. + */ + void SetString(CSStr value) + { + m_Text = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a integer. + */ + Int32 GetInt() const + { + return m_Text.as_int(); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a integer. + */ + void SetInt(Int32 value) + { + m_Text = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a unsigned integer. + */ + Uint32 GetUint() const + { + return m_Text.as_uint(); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a unsigned integer. + */ + void SetUint(Uint32 value) + { + m_Text = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a floating point. + */ + SQFloat GetFloat() const + { + return static_cast< SQFloat >(m_Text.as_float()); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a floating point. + */ + void SetFloat(SQFloat value) + { + m_Text = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a double floating point. + */ + SQFloat GetDouble() const + { + return static_cast< SQFloat >(m_Text.as_double()); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a double floating point. + */ + void SetDouble(SQFloat value) + { + m_Text = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a long integer. + */ + LightObj GetLong() const; + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a long integer. + */ + void SetLong(const SLongInt & value); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a unsigned long integer. + */ + LightObj GetUlong() const; + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a unsigned long integer. + */ + void SetUlong(const ULongInt & value); + + /* -------------------------------------------------------------------------------------------- + * Retrieve the value as a boolean. + */ + bool GetBool() const + { + return m_Text.as_bool(); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the value as a boolean. + */ + void SetBool(bool value) + { + m_Text = value; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the data node (node_pcdata or node_cdata) for this object. + */ + XmlNode GetData() const; +}; + +} // Namespace:: SqMod diff --git a/module/Register.cpp b/module/Register.cpp index f03fd90c..e25dba86 100644 --- a/module/Register.cpp +++ b/module/Register.cpp @@ -43,6 +43,7 @@ extern void Register_SQLite(HSQUIRRELVM vm); extern void Register_String(HSQUIRRELVM vm); extern void Register_System(HSQUIRRELVM vm); extern void Register_Utils(HSQUIRRELVM vm); +extern void Register_XML(HSQUIRRELVM vm); // ------------------------------------------------------------------------------------------------ extern void Register_Constants(HSQUIRRELVM vm); @@ -89,6 +90,7 @@ bool RegisterAPI(HSQUIRRELVM vm) Register_String(vm); Register_System(vm); Register_Utils(vm); + Register_XML(vm); Register_Constants(vm); Register_Log(vm); diff --git a/sqrat/sqrat/sqratObject.h b/sqrat/sqrat/sqratObject.h index 67cb333d..dcad1a23 100644 --- a/sqrat/sqrat/sqratObject.h +++ b/sqrat/sqrat/sqratObject.h @@ -145,6 +145,36 @@ public: } } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Constructs an Object from a C++ type + /// + /// \param t Identity of the type to create. + /// \param v VM that the object will exist in. + /// \param a Arguments to forward to the type constructor. + /// + /// \tparam T Type of instance + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + template + Object(SqTypeIdentity< T > SQ_UNUSED_ARG(t), HSQUIRRELVM v, A&&... a) : vm(v), release(true) { + // Create the instance and guard it to make sure it gets deleted in case of exceptions + DeleteGuard< T > instance(new T(std::forward< A >(a)...)); + // Preserve the stack state + const StackGuard sg(vm); + // Push the instance on the stack + ClassType::PushInstance(vm, instance); + // Attempt to retrieve it + if (SQ_FAILED(sq_getstackobj(vm, -1, &obj))) { + sq_resetobject(&obj); + // nothing to release anymore + release = false; + } else { + sq_addref(vm, &obj); + } + // Stop guarding the instance + instance.Release(); + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Destructor /// @@ -815,7 +845,7 @@ struct LightObj { } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Constructs an LightObj from a C++ instance + /// Constructs a LightObj from a C++ instance /// /// \param instance Pointer to a C++ class instance that has been bound already /// \param v VM that the object will exist in @@ -837,6 +867,34 @@ struct LightObj { } } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Constructs a LightObj from a C++ type + /// + /// \param t Identity of the type to create. + /// \param v VM that the object will exist in. + /// \param a Arguments to forward to the type constructor. + /// + /// \tparam T Type of instance + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + template + LightObj(SqTypeIdentity< T > SQ_UNUSED_ARG(t), HSQUIRRELVM vm, A&&... a) { + // Create the instance and guard it to make sure it gets deleted in case of exceptions + DeleteGuard< T > instance(new T(std::forward< A >(a)...)); + // Preserve the stack state + const StackGuard sg(vm); + // Push the instance on the stack + ClassType::PushInstance(vm, instance); + // Attempt to retrieve it + if (SQ_FAILED(sq_getstackobj(vm, -1, &mObj))) { + sq_resetobject(&mObj); + } else { + sq_addref(vm, &mObj); + } + // Stop guarding the instance + instance.Release(); + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Constructs an LightObj from a C++ instance wrapped inside a DeleteGuard /// diff --git a/sqrat/sqrat/sqratUtil.h b/sqrat/sqrat/sqratUtil.h index c8e515cb..00c17ea5 100644 --- a/sqrat/sqrat/sqratUtil.h +++ b/sqrat/sqrat/sqratUtil.h @@ -202,7 +202,10 @@ class WeakPtr; /// @endcond -// Helper class that defines a VM that can be used as a fallback VM in case no other one is given to a piece of code +/// Provides the member typedef `type` that names T (i.e., the identity transformation). +template< class T > struct SqTypeIdentity { using type = T; }; + +/// Helper class that defines a VM that can be used as a fallback VM in case no other one is given to a piece of code class DefaultVM { private: static HSQUIRRELVM& StaticVM() { @@ -224,6 +227,9 @@ public: } }; +/// Alias for DefaultVM::Get() +inline HSQUIRRELVM SqVM() noexcept { return DefaultVM::Get(); } + #if !defined (SCRAT_NO_ERROR_CHECKING) && !defined (SCRAT_USE_EXCEPTIONS) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// The class that must be used to deal with errors that Sqrat has