mirror of
https://github.com/edubart/otclient.git
synced 2026-01-09 07:41:29 +01:00
Huge engine change, replace all std::shared_ptrs
Create a new shared pointer type stdext::shared_object_ptr and stdext::shared_obj using boost::intrusive_ptr Advantages: * half memory usage * faster and lightweight Disadvantages: * using weak_ptr is not supported anymore * compiling seems slower
This commit is contained in:
@@ -30,9 +30,8 @@ class OTMLDocument;
|
||||
class OTMLParser;
|
||||
class OTMLEmitter;
|
||||
|
||||
typedef std::shared_ptr<OTMLNode> OTMLNodePtr;
|
||||
typedef std::shared_ptr<OTMLDocument> OTMLDocumentPtr;
|
||||
typedef std::weak_ptr<OTMLNode> OTMLNodeWeakPtr;
|
||||
typedef stdext::shared_object_ptr<OTMLNode> OTMLNodePtr;
|
||||
typedef stdext::shared_object_ptr<OTMLDocument> OTMLDocumentPtr;
|
||||
typedef std::vector<OTMLNodePtr> OTMLNodeList;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -52,7 +52,7 @@ OTMLDocumentPtr OTMLDocument::parse(std::istream& in, const std::string& source)
|
||||
|
||||
std::string OTMLDocument::emit()
|
||||
{
|
||||
return OTMLEmitter::emitNode(shared_from_this()) + "\n";
|
||||
return OTMLEmitter::emitNode(asOTMLNode()) + "\n";
|
||||
}
|
||||
|
||||
bool OTMLDocument::save(const std::string& fileName)
|
||||
|
||||
@@ -77,14 +77,14 @@ OTMLNodePtr OTMLNode::at(const std::string& childTag)
|
||||
}
|
||||
}
|
||||
if(!res)
|
||||
throw OTMLException(shared_from_this(), stdext::format("child node with tag '%s' not found", childTag));
|
||||
throw OTMLException(asOTMLNode(), stdext::format("child node with tag '%s' not found", childTag));
|
||||
return res;
|
||||
}
|
||||
|
||||
OTMLNodePtr OTMLNode::atIndex(int childIndex)
|
||||
{
|
||||
if(childIndex >= size() || childIndex < 0)
|
||||
throw OTMLException(shared_from_this(), stdext::mkstr("child node with index '%d' not found", childIndex));
|
||||
throw OTMLException(asOTMLNode(), stdext::mkstr("child node with index '%d' not found", childIndex));
|
||||
return m_children[childIndex];
|
||||
}
|
||||
|
||||
@@ -109,7 +109,6 @@ void OTMLNode::addChild(const OTMLNodePtr& newChild)
|
||||
while(it != m_children.end()) {
|
||||
OTMLNodePtr node = (*it);
|
||||
if(node != newChild && node->tag() == newChild->tag()) {
|
||||
node->setParent(nullptr);
|
||||
it = m_children.erase(it);
|
||||
} else
|
||||
++it;
|
||||
@@ -120,7 +119,6 @@ void OTMLNode::addChild(const OTMLNodePtr& newChild)
|
||||
}
|
||||
|
||||
m_children.push_back(newChild);
|
||||
newChild->setParent(shared_from_this());
|
||||
}
|
||||
|
||||
bool OTMLNode::removeChild(const OTMLNodePtr& oldChild)
|
||||
@@ -128,7 +126,6 @@ bool OTMLNode::removeChild(const OTMLNodePtr& oldChild)
|
||||
auto it = std::find(m_children.begin(), m_children.end(), oldChild);
|
||||
if(it != m_children.end()) {
|
||||
m_children.erase(it);
|
||||
oldChild->setParent(nullptr);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -138,8 +135,6 @@ bool OTMLNode::replaceChild(const OTMLNodePtr& oldChild, const OTMLNodePtr& newC
|
||||
{
|
||||
auto it = std::find(m_children.begin(), m_children.end(), oldChild);
|
||||
if(it != m_children.end()) {
|
||||
oldChild->setParent(nullptr);
|
||||
newChild->setParent(shared_from_this());
|
||||
it = m_children.erase(it);
|
||||
m_children.insert(it, newChild);
|
||||
return true;
|
||||
@@ -169,8 +164,6 @@ void OTMLNode::merge(const OTMLNodePtr& node)
|
||||
|
||||
void OTMLNode::clear()
|
||||
{
|
||||
for(const OTMLNodePtr& child : m_children)
|
||||
child->setParent(nullptr);
|
||||
m_children.clear();
|
||||
}
|
||||
|
||||
@@ -198,6 +191,6 @@ OTMLNodePtr OTMLNode::clone()
|
||||
|
||||
std::string OTMLNode::emit()
|
||||
{
|
||||
return OTMLEmitter::emitNode(shared_from_this(), 0);
|
||||
return OTMLEmitter::emitNode(asOTMLNode(), 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
#include "declarations.h"
|
||||
|
||||
class OTMLNode : public std::enable_shared_from_this<OTMLNode>
|
||||
class OTMLNode : public stdext::shared_object
|
||||
{
|
||||
public:
|
||||
virtual ~OTMLNode() { }
|
||||
@@ -35,7 +35,6 @@ public:
|
||||
|
||||
std::string tag() { return m_tag; }
|
||||
int size() { return m_children.size(); }
|
||||
OTMLNodePtr parent() { return m_parent.lock(); }
|
||||
std::string source() { return m_source; }
|
||||
std::string rawValue() { return m_value; }
|
||||
|
||||
@@ -52,7 +51,6 @@ public:
|
||||
void setValue(const std::string& value) { m_value = value; }
|
||||
void setNull(bool null) { m_null = null; }
|
||||
void setUnique(bool unique) { m_unique = unique; }
|
||||
void setParent(const OTMLNodePtr& parent) { m_parent = parent; }
|
||||
void setSource(const std::string& source) { m_source = source; }
|
||||
|
||||
OTMLNodePtr get(const std::string& childTag);
|
||||
@@ -91,11 +89,12 @@ public:
|
||||
|
||||
virtual std::string emit();
|
||||
|
||||
OTMLNodePtr asOTMLNode() { return self_cast<OTMLNode>(); }
|
||||
|
||||
protected:
|
||||
OTMLNode() : m_unique(false), m_null(false) { }
|
||||
|
||||
OTMLNodeList m_children;
|
||||
OTMLNodeWeakPtr m_parent;
|
||||
std::string m_tag;
|
||||
std::string m_value;
|
||||
std::string m_source;
|
||||
@@ -123,7 +122,7 @@ template<typename T>
|
||||
T OTMLNode::value() {
|
||||
T ret;
|
||||
if(!stdext::cast(m_value, ret))
|
||||
throw OTMLException(shared_from_this(), stdext::format("failed to cast node value '%s' to type '%s'", m_value, stdext::demangle_type<T>()));
|
||||
throw OTMLException(asOTMLNode(), stdext::format("failed to cast node value '%s' to type '%s'", m_value, stdext::demangle_type<T>()));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ void OTMLParser::parseLine(std::string line)
|
||||
// a depth below, change parent to previous parent
|
||||
} else if(depth < currentDepth) {
|
||||
for(int i=0;i<currentDepth-depth;++i)
|
||||
currentParent = currentParent->parent();
|
||||
currentParent = parentMap[currentParent];
|
||||
// if it isn't the current depth, it's a syntax error
|
||||
} else if(depth != currentDepth)
|
||||
throw OTMLException(doc, "invalid indentation depth, are you indenting correctly?", currentLine);
|
||||
@@ -198,5 +198,6 @@ void OTMLParser::parseNode(const std::string& data)
|
||||
}
|
||||
|
||||
currentParent->addChild(node);
|
||||
parentMap[node] = currentParent;
|
||||
previousNode = node;
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ private:
|
||||
int currentLine;
|
||||
OTMLDocumentPtr doc;
|
||||
OTMLNodePtr currentParent;
|
||||
std::unordered_map<OTMLNodePtr, OTMLNodePtr> parentMap;
|
||||
OTMLNodePtr previousNode;
|
||||
std::istream& in;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user