From 40e697f7265b98dd99918d9c3f5541d9878d80b0 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 29 Aug 2012 21:25:42 +0200 Subject: [PATCH] added destructors for ParsedSource & Co. to free some resources --- src/loader/ParsedSource.cpp | 48 +++++++++++++++++++++++++++++++++---- src/loader/ParsedSource.h | 24 ++++++++++++++++++- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/loader/ParsedSource.cpp b/src/loader/ParsedSource.cpp index 1f7fc98..2075828 100644 --- a/src/loader/ParsedSource.cpp +++ b/src/loader/ParsedSource.cpp @@ -17,6 +17,12 @@ using std::vector; namespace loader { +ParsedSource::~ParsedSource() { + for (map::const_iterator i(declarations.begin()), end(declarations.end()); i != end; ++i) { + delete i->second; + } +} + void ParsedSource::AddDeclaration(Declaration *d) { map::iterator i(declarations.find(d->Identifier())); if (i != declarations.end()) { @@ -96,6 +102,14 @@ const Definition &ParsedSource::GetDefinition(const std::string &name) const { } +Definition::~Definition() { + if (isLiteral) { + delete reinterpret_cast(value); + } else { + delete reinterpret_cast(value); + } +} + void Definition::SetValue(Literal *v) { value = v; isLiteral = true; @@ -108,7 +122,7 @@ void Definition::SetValue(PropertyList *v) { Literal *Definition::GetLiteral() { if (isLiteral) { - return (Literal *)value; + return reinterpret_cast(value); } else { throw runtime_error("tried to access properties as literal"); } @@ -116,7 +130,7 @@ Literal *Definition::GetLiteral() { const Literal *Definition::GetLiteral() const { if (isLiteral) { - return (Literal *)value; + return reinterpret_cast(value); } else { throw runtime_error("tried to access properties as literal"); } @@ -124,7 +138,7 @@ const Literal *Definition::GetLiteral() const { PropertyList *Definition::GetProperties() { if (!isLiteral) { - return (PropertyList *)value; + return reinterpret_cast(value); } else { throw runtime_error("tried to access literal value as property list"); } @@ -132,7 +146,7 @@ PropertyList *Definition::GetProperties() { const PropertyList *Definition::GetProperties() const { if (!isLiteral) { - return (PropertyList *)value; + return reinterpret_cast(value); } else { throw runtime_error("tried to access literal value as property list"); } @@ -222,6 +236,26 @@ Literal::Literal(const string &typeName, PropertyList *properties) } +Literal::~Literal() { + switch (type) { + case ARRAY_VALUES: + for (vector::const_iterator i(values.begin()), end(values.end()); i != end; ++i) { + delete *i; + } + break; + case ARRAY_PROPS: + for (vector::const_iterator i(propertyLists.begin()), end(propertyLists.end()); i != end; ++i) { + delete *i; + } + break; + case OBJECT: + delete props; + break; + default: + break; + } +} + const vector &Literal::GetValues() const { if (type == ARRAY_VALUES) { @@ -328,6 +362,12 @@ const PropertyList *Literal::GetProperties() const { } +Value::~Value() { + if (isLiteral) { + delete literal; + } +} + const Literal &Value::GetLiteral() const { if (isLiteral) { return *literal; diff --git a/src/loader/ParsedSource.h b/src/loader/ParsedSource.h index 6351ae4..36f3810 100644 --- a/src/loader/ParsedSource.h +++ b/src/loader/ParsedSource.h @@ -42,6 +42,10 @@ public: Literal(const std::string &); Literal(int x, int y); Literal(const std::string &typeName, PropertyList *properties); + ~Literal(); +private: + Literal(const Literal &); + Literal &operator =(const Literal &); public: Type GetType() const { return type; } @@ -79,6 +83,10 @@ public: : literal(0), identifier(identifier), isLiteral(false) { } explicit Value(Literal *literal) : literal(literal), isLiteral(true) { } + ~Value(); +private: + Value(const Value &); + Value &operator =(const Value &); public: bool IsLiteral() const { return isLiteral; } @@ -96,7 +104,11 @@ private: class PropertyList { public: + PropertyList() { } ~PropertyList(); +private: + PropertyList(const PropertyList &); + PropertyList &operator =(const PropertyList &); public: void SetProperty(const std::string &name, Value *value) { @@ -122,6 +134,9 @@ public: Declaration(const std::string &typeName, const std::string &identifier) : typeName(typeName), identifier(identifier) { } virtual ~Declaration() { } +private: + Declaration(const Declaration &); + Declaration &operator =(const Declaration &); public: const std::string &TypeName() const { return typeName; } @@ -140,6 +155,10 @@ class Definition public: Definition(const std::string &typeName, const std::string &identifier) : Declaration(typeName, identifier), value(0), isLiteral(false) { } + virtual ~Definition(); +private: + Definition(const Definition &); + Definition &operator =(const Definition &); public: void SetValue(Literal *); @@ -163,7 +182,10 @@ class ParsedSource { public: ParsedSource() { } - ~ParsedSource() { } + ~ParsedSource(); +private: + ParsedSource(const ParsedSource &); + ParsedSource &operator =(const ParsedSource &); public: void AddDeclaration(Declaration *); -- 2.39.2