]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/ParsedSource.cpp
added destructors for ParsedSource & Co. to free some resources
[l2e.git] / src / loader / ParsedSource.cpp
index ec263042a68cdba8d7c9c27e43479ae9fee1455f..2075828e19b707b28e0afac8e77ca811da588a47 100644 (file)
@@ -17,6 +17,12 @@ using std::vector;
 
 namespace loader {
 
+ParsedSource::~ParsedSource() {
+       for (map<string, Declaration *>::const_iterator i(declarations.begin()), end(declarations.end()); i != end; ++i) {
+               delete i->second;
+       }
+}
+
 void ParsedSource::AddDeclaration(Declaration *d) {
        map<string, Declaration *>::iterator i(declarations.find(d->Identifier()));
        if (i != declarations.end()) {
@@ -51,6 +57,59 @@ void ParsedSource::ExportIdentifier(const std::string &i) {
 }
 
 
+bool ParsedSource::IsDeclared(const std::string &name) const {
+       return declarations.count(name);
+}
+
+Declaration &ParsedSource::GetDeclaration(const std::string &name) {
+       map<string, Declaration *>::const_iterator i(declarations.find(name));
+       if (i != declarations.end()) {
+               return *i->second;
+       } else {
+               throw runtime_error("undeclared identifier " + name);
+       }
+}
+
+const Declaration &ParsedSource::GetDeclaration(const std::string &name) const {
+       map<string, Declaration *>::const_iterator i(declarations.find(name));
+       if (i != declarations.end()) {
+               return *i->second;
+       } else {
+               throw runtime_error("undeclared identifier " + name);
+       }
+}
+
+bool ParsedSource::IsDefined(const std::string &name) const {
+       return definitions.count(name);
+}
+
+Definition &ParsedSource::GetDefinition(const std::string &name) {
+       map<string, Definition *>::const_iterator i(definitions.find(name));
+       if (i != definitions.end()) {
+               return *i->second;
+       } else {
+               throw runtime_error("undefined identifier " + name);
+       }
+}
+
+const Definition &ParsedSource::GetDefinition(const std::string &name) const {
+       map<string, Definition *>::const_iterator i(definitions.find(name));
+       if (i != definitions.end()) {
+               return *i->second;
+       } else {
+               throw runtime_error("undefined identifier " + name);
+       }
+}
+
+
+Definition::~Definition() {
+       if (isLiteral) {
+               delete reinterpret_cast<Literal *>(value);
+       } else {
+               delete reinterpret_cast<PropertyList *>(value);
+       }
+}
+
 void Definition::SetValue(Literal *v) {
        value = v;
        isLiteral = true;
@@ -63,7 +122,7 @@ void Definition::SetValue(PropertyList *v) {
 
 Literal *Definition::GetLiteral() {
        if (isLiteral) {
-               return (Literal *)value;
+               return reinterpret_cast<Literal *>(value);
        } else {
                throw runtime_error("tried to access properties as literal");
        }
@@ -71,7 +130,7 @@ Literal *Definition::GetLiteral() {
 
 const Literal *Definition::GetLiteral() const {
        if (isLiteral) {
-               return (Literal *)value;
+               return reinterpret_cast<Literal *>(value);
        } else {
                throw runtime_error("tried to access properties as literal");
        }
@@ -79,7 +138,7 @@ const Literal *Definition::GetLiteral() const {
 
 PropertyList *Definition::GetProperties() {
        if (!isLiteral) {
-               return (PropertyList *)value;
+               return reinterpret_cast<PropertyList *>(value);
        } else {
                throw runtime_error("tried to access literal value as property list");
        }
@@ -87,7 +146,7 @@ PropertyList *Definition::GetProperties() {
 
 const PropertyList *Definition::GetProperties() const {
        if (!isLiteral) {
-               return (PropertyList *)value;
+               return reinterpret_cast<PropertyList *>(value);
        } else {
                throw runtime_error("tried to access literal value as property list");
        }
@@ -177,6 +236,26 @@ Literal::Literal(const string &typeName, PropertyList *properties)
 
 }
 
+Literal::~Literal() {
+       switch (type) {
+               case ARRAY_VALUES:
+                       for (vector<Value *>::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
+                               delete *i;
+                       }
+                       break;
+               case ARRAY_PROPS:
+                       for (vector<PropertyList *>::const_iterator i(propertyLists.begin()), end(propertyLists.end()); i != end; ++i) {
+                               delete *i;
+                       }
+                       break;
+               case OBJECT:
+                       delete props;
+                       break;
+               default:
+                       break;
+       }
+}
+
 
 const vector<Value *> &Literal::GetValues() const {
        if (type == ARRAY_VALUES) {
@@ -282,6 +361,29 @@ const PropertyList *Literal::GetProperties() const {
        }
 }
 
+
+Value::~Value() {
+       if (isLiteral) {
+               delete literal;
+       }
+}
+
+const Literal &Value::GetLiteral() const {
+       if (isLiteral) {
+               return *literal;
+       } else {
+               throw runtime_error("tried to access literal of identifier value");
+       }
+}
+
+const std::string &Value::GetIdentifier() const {
+       if (!isLiteral) {
+               return identifier;
+       } else {
+               throw runtime_error("tried to access identifier of literal value");
+       }
+}
+
 }