pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r2728 - in branches/pingus_sdl/src: . lisp


From: jsalmon3
Subject: [Pingus-CVS] r2728 - in branches/pingus_sdl/src: . lisp
Date: Sat, 14 Jul 2007 06:12:47 +0200

Author: jsalmon3
Date: 2007-07-14 06:12:39 +0200 (Sat, 14 Jul 2007)
New Revision: 2728

Modified:
   branches/pingus_sdl/src/file_reader.cpp
   branches/pingus_sdl/src/lisp/lisp.cpp
   branches/pingus_sdl/src/lisp/lisp.hpp
   branches/pingus_sdl/src/lisp/parser.cpp
   branches/pingus_sdl/src/lisp/parser.hpp
   branches/pingus_sdl/src/lisp/properties.cpp
   branches/pingus_sdl/src/lisp/property_iterator.hpp
   branches/pingus_sdl/src/resource_manager.cpp
   branches/pingus_sdl/src/sexpr_file_reader.cpp
   branches/pingus_sdl/src/sexpr_file_reader.hpp
Log:
Use boost::shared_ptr for the Lisp objects to prevent memory leaks

Modified: branches/pingus_sdl/src/file_reader.cpp
===================================================================
--- branches/pingus_sdl/src/file_reader.cpp     2007-07-14 03:28:17 UTC (rev 
2727)
+++ branches/pingus_sdl/src/file_reader.cpp     2007-07-14 04:12:39 UTC (rev 
2728)
@@ -160,8 +160,7 @@
 FileReader
 FileReader::parse(const std::string& filename)
 {
-  // FIXME: memory leak, somebody must delete the lisp::Lisp
-  lisp::Lisp* sexpr = lisp::Parser::parse(filename);
+  boost::shared_ptr<lisp::Lisp> sexpr = lisp::Parser::parse(filename);
   if (sexpr)
     {
       return SExprFileReader(sexpr->get_list_elem(0));

Modified: branches/pingus_sdl/src/lisp/lisp.cpp
===================================================================
--- branches/pingus_sdl/src/lisp/lisp.cpp       2007-07-14 03:28:17 UTC (rev 
2727)
+++ branches/pingus_sdl/src/lisp/lisp.cpp       2007-07-14 04:12:39 UTC (rev 
2728)
@@ -50,11 +50,11 @@
   memcpy(v.string, str.c_str(), str.size()+1);
 }
 
-Lisp::Lisp(const std::vector<Lisp*>& list_elements)
+Lisp::Lisp(const std::vector<boost::shared_ptr<Lisp> >& list_elements)
   : type(TYPE_LIST)
 {
   v.list.size = list_elements.size();
-  v.list.entries = new Lisp* [v.list.size];
+  v.list.entries = new boost::shared_ptr<Lisp> [v.list.size];
   for(size_t i = 0; i < v.list.size; ++i) {
     v.list.entries[i] = list_elements[i];
   }
@@ -65,8 +65,6 @@
   if(type == TYPE_SYMBOL || type == TYPE_STRING) {
     delete[] v.string;
   } else if(type == TYPE_LIST) {
-    for(size_t i = 0; i < v.list.size; ++i)
-      delete v.list.entries[i];
     delete[] v.list.entries;
   }
 }

Modified: branches/pingus_sdl/src/lisp/lisp.hpp
===================================================================
--- branches/pingus_sdl/src/lisp/lisp.hpp       2007-07-14 03:28:17 UTC (rev 
2727)
+++ branches/pingus_sdl/src/lisp/lisp.hpp       2007-07-14 04:12:39 UTC (rev 
2728)
@@ -25,6 +25,7 @@
 #include <sstream>
 #include <stdexcept>
 #include <assert.h>
+#include <boost/shared_ptr.hpp>
 
 namespace lisp
 {
@@ -43,7 +44,7 @@
 
   /// construct a new Lisp object symbol or string object
   Lisp(LispType newtype, const std::string& value);
-  Lisp(const std::vector<Lisp*>& list_elements);
+  Lisp(const std::vector<boost::shared_ptr<Lisp> >& list_elements);
   Lisp(int val);
   Lisp(float val);
   Lisp(bool val);
@@ -56,7 +57,7 @@
   {
     return v.list.size;
   }
-  Lisp* get_list_elem(size_t i) const
+  boost::shared_ptr<Lisp> get_list_elem(size_t i) const
   {
     assert(i < v.list.size);
     return v.list.entries[i];
@@ -111,7 +112,7 @@
   union
   {
     struct {
-      Lisp** entries;
+      boost::shared_ptr<Lisp>* entries;
       size_t size;
     } list;
     char* string;

Modified: branches/pingus_sdl/src/lisp/parser.cpp
===================================================================
--- branches/pingus_sdl/src/lisp/parser.cpp     2007-07-14 03:28:17 UTC (rev 
2727)
+++ branches/pingus_sdl/src/lisp/parser.cpp     2007-07-14 04:12:39 UTC (rev 
2728)
@@ -64,7 +64,7 @@
   delete lexer;
 }
 
-Lisp*
+boost::shared_ptr<Lisp>
 Parser::parse(const std::string& filename)
 {
   IFileStream in(filename);
@@ -77,7 +77,7 @@
   return parse(in, filename);
 }
 
-Lisp*
+boost::shared_ptr<Lisp>
 Parser::parse(std::istream& stream, const std::string& filename)
 {
   std::auto_ptr<Parser> parser (new Parser());
@@ -89,7 +89,7 @@
   if(parser->token != Lexer::TOKEN_OPEN_PAREN)
     throw ParseError(parser.get(), "file doesn't start with '('");
 
-  std::auto_ptr<Lisp> result (parser->parse());
+  boost::shared_ptr<Lisp> result(parser->parse());
   if(parser->token != Lexer::TOKEN_EOF) {
     if(parser->token == Lexer::TOKEN_CLOSE_PAREN)
       throw ParseError(parser.get(), "too many ')'");
@@ -97,80 +97,73 @@
       throw ParseError(parser.get(), "extra tokens at end of file");
   }
     
-  return result.release();
+  return result;
 }
 
-Lisp*
+boost::shared_ptr<Lisp>
 Parser::parse()
 {
-  std::vector<Lisp*> entries;
-  try {
-    while(token != Lexer::TOKEN_CLOSE_PAREN && token != Lexer::TOKEN_EOF) {
-      switch(token) {
-        case Lexer::TOKEN_OPEN_PAREN:
+  std::vector<boost::shared_ptr<Lisp> > entries;
+  while(token != Lexer::TOKEN_CLOSE_PAREN && token != Lexer::TOKEN_EOF) {
+    switch(token) {
+      case Lexer::TOKEN_OPEN_PAREN:
+        token = lexer->getNextToken();
+      
+        // Handle (_ "blup") strings that need to be translated
+        if(token == Lexer::TOKEN_SYMBOL
+            && strcmp(lexer->getString(), "_") == 0) {
           token = lexer->getNextToken();
-        
-          // Handle (_ "blup") strings that need to be translated
-          if(token == Lexer::TOKEN_SYMBOL
-              && strcmp(lexer->getString(), "_") == 0) {
-            token = lexer->getNextToken();
-            if(token != Lexer::TOKEN_STRING)
-              throw ParseError(this, "Expected string after '(_' sequence");
-            entries.push_back(new Lisp(Lisp::TYPE_STRING, lexer->getString()));
-            
-            token = lexer->getNextToken();
-            if(token != Lexer::TOKEN_CLOSE_PAREN)
-              throw ParseError(this, "Expected ')' after '(_ ""' sequence");
-            break;
-          }
-        
-          entries.push_back(parse());
-          if(token != Lexer::TOKEN_CLOSE_PAREN) {
-            if(token == Lexer::TOKEN_EOF)
-              throw ParseError(this, "Expected ')' token, got EOF");
-            else
-              throw ParseError(this, "Expected ')' token");
-          }
+          if(token != Lexer::TOKEN_STRING)
+            throw ParseError(this, "Expected string after '(_' sequence");
+          entries.push_back(boost::shared_ptr<Lisp>(new 
Lisp(Lisp::TYPE_STRING, lexer->getString())));
+          
+          token = lexer->getNextToken();
+          if(token != Lexer::TOKEN_CLOSE_PAREN)
+            throw ParseError(this, "Expected ')' after '(_ ""' sequence");
           break;
-        case Lexer::TOKEN_SYMBOL:
-          entries.push_back(new Lisp(Lisp::TYPE_SYMBOL, lexer->getString()));
-          break;
-        case Lexer::TOKEN_STRING:
-          entries.push_back(new Lisp(Lisp::TYPE_STRING, lexer->getString()));
-          break;
-        case Lexer::TOKEN_INTEGER: {
-          int val;
-          sscanf(lexer->getString(), "%d", &val);
-          entries.push_back(new Lisp(val));
-          break;
         }
-        case Lexer::TOKEN_REAL: {
-          float val;
-          sscanf(lexer->getString(), "%f", &val);
-          entries.push_back(new Lisp(val));
-          break;
+      
+        entries.push_back(parse());
+        if(token != Lexer::TOKEN_CLOSE_PAREN) {
+          if(token == Lexer::TOKEN_EOF)
+            throw ParseError(this, "Expected ')' token, got EOF");
+          else
+            throw ParseError(this, "Expected ')' token");
         }
-        case Lexer::TOKEN_TRUE:
-          entries.push_back(new Lisp(true));
-          break;
-        case Lexer::TOKEN_FALSE:
-          entries.push_back(new Lisp(false));
-          break;
-        default:
-          // this should never happen
-          assert(false);
+        break;
+      case Lexer::TOKEN_SYMBOL:
+        entries.push_back(boost::shared_ptr<Lisp>(new Lisp(Lisp::TYPE_SYMBOL, 
lexer->getString())));
+        break;
+      case Lexer::TOKEN_STRING:
+        entries.push_back(boost::shared_ptr<Lisp>(new Lisp(Lisp::TYPE_STRING, 
lexer->getString())));
+        break;
+      case Lexer::TOKEN_INTEGER: {
+        int val;
+        sscanf(lexer->getString(), "%d", &val);
+        entries.push_back(boost::shared_ptr<Lisp>(new Lisp(val)));
+        break;
       }
-
-      token = lexer->getNextToken();
+      case Lexer::TOKEN_REAL: {
+        float val;
+        sscanf(lexer->getString(), "%f", &val);
+        entries.push_back(boost::shared_ptr<Lisp>(new Lisp(val)));
+        break;
+      }
+      case Lexer::TOKEN_TRUE:
+        entries.push_back(boost::shared_ptr<Lisp>(new Lisp(true)));
+        break;
+      case Lexer::TOKEN_FALSE:
+        entries.push_back(boost::shared_ptr<Lisp>(new Lisp(false)));
+        break;
+      default:
+        // this should never happen
+        assert(false);
     }
-  } catch(...) {
-    for(std::vector<Lisp*>::iterator i = entries.begin();
-        i != entries.end(); ++i)
-      delete *i;
-    throw;
+
+    token = lexer->getNextToken();
   }
   
-  return new Lisp(entries);
+  return boost::shared_ptr<Lisp>(new Lisp(entries));
 }
 
 } // end of namespace lisp

Modified: branches/pingus_sdl/src/lisp/parser.hpp
===================================================================
--- branches/pingus_sdl/src/lisp/parser.hpp     2007-07-14 03:28:17 UTC (rev 
2727)
+++ branches/pingus_sdl/src/lisp/parser.hpp     2007-07-14 04:12:39 UTC (rev 
2728)
@@ -20,6 +20,7 @@
 #define __LISPPARSER_H__
 
 #include <string>
+#include <boost/shared_ptr.hpp>
 #include "lexer.hpp"
 
 namespace lisp
@@ -31,15 +32,15 @@
 {
 public:
   ~Parser();
-  static Lisp* parse(const std::string& filename);
-  static Lisp* parse(std::istream& stream, const std::string& filename = "");
+  static boost::shared_ptr<Lisp> parse(const std::string& filename);
+  static boost::shared_ptr<Lisp> parse(std::istream& stream, const 
std::string& filename = "");
 
 private:
   friend class ParseError;
 
   Parser();
   
-  Lisp* parse();
+  boost::shared_ptr<Lisp> parse();
   
   std::string filename;
   Lexer* lexer;

Modified: branches/pingus_sdl/src/lisp/properties.cpp
===================================================================
--- branches/pingus_sdl/src/lisp/properties.cpp 2007-07-14 03:28:17 UTC (rev 
2727)
+++ branches/pingus_sdl/src/lisp/properties.cpp 2007-07-14 04:12:39 UTC (rev 
2728)
@@ -14,14 +14,14 @@
         throw std::runtime_error("Lisp is not a list");
 
       for(size_t i = 0; i < lisp->get_list_size(); ++i) {
-        const Lisp* child = lisp->get_list_elem(i);
+        const boost::shared_ptr<Lisp> child = lisp->get_list_elem(i);
         if(i == 0 && child->get_type() == Lisp::TYPE_SYMBOL)
           continue;
         if(child->get_type() != Lisp::TYPE_LIST)
           throw std::runtime_error("child of properties lisp is not a list");
         if(child->get_list_size() > 1)
           {    
-            const Lisp* name = child->get_list_elem(0);
+            const boost::shared_ptr<Lisp> name = child->get_list_elem(0);
             if(name->get_type() != Lisp::TYPE_SYMBOL)
               throw std::runtime_error("property has no symbol as name");
             properties.insert(std::make_pair(

Modified: branches/pingus_sdl/src/lisp/property_iterator.hpp
===================================================================
--- branches/pingus_sdl/src/lisp/property_iterator.hpp  2007-07-14 03:28:17 UTC 
(rev 2727)
+++ branches/pingus_sdl/src/lisp/property_iterator.hpp  2007-07-14 04:12:39 UTC 
(rev 2728)
@@ -8,11 +8,11 @@
 {
 
 struct ListEntry {
-  ListEntry(const lisp::Lisp* lisp)
+  ListEntry(const boost::shared_ptr<lisp::Lisp> lisp)
     : lisp(lisp), used(false)
   {}
   
-  const Lisp* lisp;
+  const boost::shared_ptr<lisp::Lisp> lisp;
   bool used;
 };
 typedef std::multimap<std::string, ListEntry> PropertyMap;

Modified: branches/pingus_sdl/src/resource_manager.cpp
===================================================================
--- branches/pingus_sdl/src/resource_manager.cpp        2007-07-14 03:28:17 UTC 
(rev 2727)
+++ branches/pingus_sdl/src/resource_manager.cpp        2007-07-14 04:12:39 UTC 
(rev 2728)
@@ -46,7 +46,7 @@
 ResourceManager::add_resources(const std::string& filename)
 {
   std::cout << "ResourceManager: " << filename << std::endl;
-  lisp::Lisp* sexpr = lisp::Parser::parse(filename);
+  boost::shared_ptr<lisp::Lisp> sexpr = lisp::Parser::parse(filename);
   if (sexpr)
     {
       SExprFileReader reader(sexpr->get_list_elem(0));
@@ -66,7 +66,6 @@
                     << "\ngot " << reader.get_name()
                     << std::endl;
         }
-      delete sexpr;
     }
   else
     {

Modified: branches/pingus_sdl/src/sexpr_file_reader.cpp
===================================================================
--- branches/pingus_sdl/src/sexpr_file_reader.cpp       2007-07-14 03:28:17 UTC 
(rev 2727)
+++ branches/pingus_sdl/src/sexpr_file_reader.cpp       2007-07-14 04:12:39 UTC 
(rev 2728)
@@ -36,9 +36,9 @@
 class SExprFileReaderImpl: public FileReaderImpl
 {
 public:
-  lisp::Lisp* sexpr;
+  boost::shared_ptr<lisp::Lisp> sexpr;
 
-  SExprFileReaderImpl(lisp::Lisp* sexpr_) 
+  SExprFileReaderImpl(boost::shared_ptr<lisp::Lisp> sexpr_) 
     : sexpr(sexpr_)
   {
     assert(sexpr->get_type() == lisp::Lisp::TYPE_LIST &&
@@ -62,7 +62,7 @@
 
   bool read_int   (const char* name, int& v) const 
   {
-    lisp::Lisp* item = get_subsection_item(name);
+    boost::shared_ptr<lisp::Lisp> item = get_subsection_item(name);
     if (item && item->get_type() == lisp::Lisp::TYPE_INT)
       {
         v = item->get_int();
@@ -73,7 +73,7 @@
 
   bool read_float (const char* name, float& v) const 
   {
-    lisp::Lisp* item = get_subsection_item(name);
+    boost::shared_ptr<lisp::Lisp> item = get_subsection_item(name);
     if (item)
       {
         if (item->get_type() == lisp::Lisp::TYPE_FLOAT)
@@ -96,7 +96,7 @@
 
   bool read_bool  (const char* name, bool& v) const 
   {
-    lisp::Lisp* item = get_subsection_item(name);
+    boost::shared_ptr<lisp::Lisp> item = get_subsection_item(name);
     if (item && item->get_type() == lisp::Lisp::TYPE_BOOL)
       {
         v = item->get_bool();
@@ -107,13 +107,13 @@
 
   bool read_string(const char* name, std::string& v) const 
   {
-    lisp::Lisp* sub = get_subsection(name);
+    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
     if (sub)
       {
         v = "";
         for(size_t i = 1; i < sub->get_list_size(); ++i)
           {
-            lisp::Lisp* item = sub->get_list_elem(i);
+           boost::shared_ptr<lisp::Lisp> item = sub->get_list_elem(i);
             if (item->get_type() == lisp::Lisp::TYPE_STRING)
               {
                 v += item->get_string();
@@ -130,7 +130,7 @@
 
   bool read_vector(const char* name, Vector3f& v) const
   {
-    lisp::Lisp* sub = get_subsection(name);
+    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
     if (sub && sub->get_list_size() == 4)
       {
         v = Vector3f(sub->get_list_elem(1)->get_float(),
@@ -143,7 +143,7 @@
 
   bool read_size(const char* name, Size& v) const
   {
-    lisp::Lisp* sub = get_subsection(name);
+    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
     if (sub && sub->get_list_size() == 3)
       {
         v.width  = sub->get_list_elem(1)->get_int();
@@ -155,7 +155,7 @@
 
   bool read_vector2i(const char* name, Vector2i& v) const
   {
-    lisp::Lisp* sub = get_subsection(name);
+    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
     if (sub && sub->get_list_size() == 3)
       {
         v.x = sub->get_list_elem(1)->get_int();
@@ -167,7 +167,7 @@
 
   bool read_color (const char* name, Color& v) const
   {
-    lisp::Lisp* sub = get_subsection(name);
+    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
     if (sub && sub->get_list_size() == 5)
       {
         v = Color(int(sub->get_list_elem(1)->get_float() * 255),
@@ -181,7 +181,7 @@
 
   bool read_desc  (const char* name, ResDescriptor& v) const 
   {
-    lisp::Lisp* sub = get_subsection(name);
+    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
     if (sub)
       {
         SExprFileReader reader(sub);
@@ -194,7 +194,7 @@
 
   bool read_section(const char* name, FileReader& v) const 
   {
-    lisp::Lisp* cur = get_subsection(name);
+    boost::shared_ptr<lisp::Lisp> cur = get_subsection(name);
     if (cur)
       {
         v = SExprFileReader(cur);
@@ -219,7 +219,7 @@
 
     for(size_t i = 1; i < sexpr->get_list_size(); ++i)
       { // iterate over subsections
-        lisp::Lisp* sub = sexpr->get_list_elem(i);
+       boost::shared_ptr<lisp::Lisp> sub = sexpr->get_list_elem(i);
         lst.push_back(sub->get_list_elem(0)->get_symbol());
         std::cout << sub->get_list_elem(0)->get_symbol() << std::endl;
       }
@@ -228,30 +228,30 @@
   }
 
 private:
-  lisp::Lisp* get_subsection_item(const char* name) const
+  boost::shared_ptr<lisp::Lisp> get_subsection_item(const char* name) const
   {
-    lisp::Lisp* sub = get_subsection(name);
+    boost::shared_ptr<lisp::Lisp> sub = get_subsection(name);
     if (sub && sub->get_list_size() == 2)
       {
         return sub->get_list_elem(1);
       }
-    return 0;
+    return boost::shared_ptr<lisp::Lisp>();
   }
 
-  lisp::Lisp* get_subsection(const char* name) const
+  boost::shared_ptr<lisp::Lisp> get_subsection(const char* name) const
   {
     for(size_t i = 1; i < sexpr->get_list_size(); ++i)
       { // iterate over subsections
-        lisp::Lisp* sub = sexpr->get_list_elem(i);
+       boost::shared_ptr<lisp::Lisp> sub = sexpr->get_list_elem(i);
         if (strcmp(sub->get_list_elem(0)->get_symbol(), name) == 0)
           return sub;
       }
-    return 0;
+    return boost::shared_ptr<lisp::Lisp>();
   } 
 
 };
 
-SExprFileReader::SExprFileReader(lisp::Lisp* lisp)
+SExprFileReader::SExprFileReader(boost::shared_ptr<lisp::Lisp> lisp)
   : FileReader(boost::shared_ptr<FileReaderImpl>(new 
SExprFileReaderImpl(lisp)))
 {
 }

Modified: branches/pingus_sdl/src/sexpr_file_reader.hpp
===================================================================
--- branches/pingus_sdl/src/sexpr_file_reader.hpp       2007-07-14 03:28:17 UTC 
(rev 2727)
+++ branches/pingus_sdl/src/sexpr_file_reader.hpp       2007-07-14 04:12:39 UTC 
(rev 2728)
@@ -34,7 +34,7 @@
 {
 private:
 public:
-  SExprFileReader(lisp::Lisp* lisp);
+  SExprFileReader(boost::shared_ptr<lisp::Lisp> lisp);
 };
 
 #endif





reply via email to

[Prev in Thread] Current Thread [Next in Thread]