1. pygame
  2. Untitled project
  3. imgblit

Commits

Lenard Lindstrom  committed a6f8fa3

Update the scanner code to use the new memory management reference classes

  • Participants
  • Parent commits 69d605c
  • Branches Reference

Comments (0)

Files changed (6)

File include/imgblit/internal.h

-#ifndef IMGBLIT_INTERNAL_H
-#define IMBBLIT_INTERNAL_H
-
-#include <cassert>
-#include <stdint.h>
-#include <ostream>
-
-namespace imgblit {
-    struct datum_t {
-        datum_t() : d_ref_count(0) { }
-        virtual ~datum_t() { }
-        virtual void write(std::ostream &os) const;
-        intptr_t d_ref_count;
-    };
-
-    // Assume compiler optimization removes guard function calls
-    // when the C asserts are disable--the NDEBUG macro is defined.
-    template<typename T> T *
-    not_null_guard(T *p)
-    {
-        assert(p != 0);
-        return p;
-    }
-};
-
-#endif

File include/imgblit/internal.hpp

View file
+#ifndef IMGBLIT_INTERNAL_HPP
+#define IMBBLIT_INTERNAL_HPP
+
+#include <cassert>
+
+namespace imgblit {
+    // Assume compiler optimization removes guard function calls
+    // when the C asserts are disable--the NDEBUG macro is defined.
+    template<typename T> T *
+    not_null_guard(T *p)
+    {
+        assert(p != 0);
+        return p;
+    }
+};
+
+#endif

File include/imgblit/scanner.h

-#ifndef IMGBLIT_SCANNER_H
-#define IMGBLIT_SCANNER_H
-
-#include "memory.h"
-#include <string>
-#include <vector>
-#include <ostream>
-
-using std::string;
-using std::ostream;
-using std::vector;
-
-namespace imgblit {
-    struct token_t;
-
-    class Token : public Reference {
-
-    public:
-        Token() { }
-        Token(const string &source,
-              string::size_type lexeme_start,
-              string::size_type lexeme_size,
-              const char *token_class);
-    protected:
-        token_t *get_token_p() const;
-    };
-
-
-    class Scanner {
-    
-    public:
-        Scanner(const char *source);
-        virtual ~Scanner();
-        virtual void reset();
-        Token get_token();
-        void push_back_token(const Token t);
-    protected:
-        Token read_token();
-        string scr_source;
-        vector<Token> scr_next;
-        string::size_type scr_read_start;
-    private:
-        Scanner();
-        Scanner(const Scanner &);
-        Scanner & operator=(const Scanner &);
-    };
-
-    class Parser {
-
-    public:
-        Parser(Scanner &source);
-        virtual ~Parser();
-        Reference parse();
-    protected:
-        Scanner &psr_source;
-    private:
-        Parser();
-        Parser(const Parser &);
-        Parser & operator=(const Parser &);
-    };
-};
-
-#endif

File include/imgblit/scanner.hpp

View file
+#ifndef IMGBLIT_SCANNER_HPP
+#define IMGBLIT_SCANNER_HPP
+
+#include "imgblit/memory.hpp"
+#include "imgblit/internal.hpp"
+#include <string>
+#include <vector>
+#include <ostream>
+#include <stdint.h>
+
+using std::string;
+using std::ostream;
+using std::vector;
+
+namespace imgblit {
+    class Token : public Datum {
+
+    public:
+        Token(const string &source, intptr_t lexeme_start, intptr_t lexeme_size,
+              const char *token_class) :
+            tk_lexeme(source, lexeme_start, lexeme_size),
+            tk_class(not_null_guard(token_class)) { }
+        const char * const tk_class;
+        const string tk_lexeme;
+        void write(ostream &os) const;
+    private:
+        Token();
+        Token(const Token &);
+        Token &operator =(const Token &);
+    };
+
+    typedef ClassReference<Token> TokenRef;
+
+
+    class Scanner {
+    
+    public:
+        Scanner(const char *source);
+        virtual ~Scanner();
+        virtual void reset();
+        TokenRef get_token();
+        void push_back_token(const TokenRef t);
+    protected:
+        TokenRef read_token();
+        string scr_source;
+        vector<TokenRef> scr_next;
+        string::size_type scr_read_start;
+    private:
+        Scanner();
+        Scanner(const Scanner &);
+        Scanner & operator=(const Scanner &);
+    };
+
+    class Parser {
+
+    public:
+        Parser(Scanner &source);
+        virtual ~Parser();
+        Reference parse();
+    protected:
+        Scanner &psr_source;
+    private:
+        Parser();
+        Parser(const Parser &);
+        Parser & operator=(const Parser &);
+    };
+};
+
+#endif

File src/scanner.cpp

View file
-#include "imgblit/scanner.h"
-#include "imgblit/internal.h"
+#include "imgblit/scanner.hpp"
 #include <ostream>
 
+using namespace imgblit;
 using std::ostream;
 
-namespace imgblit {
-    struct token_t : public datum_t {
-        token_t(const string &source,
-                string::size_type lexeme_start,
-                string::size_type lexeme_size,
-                const char *token_class) :
-            tk_lexeme(source, lexeme_start, lexeme_size),
-            tk_class(not_null_guard(token_class)) { }
-        const char * const tk_class;
-        const string tk_lexeme;
-        void write(ostream &os) const;
-    private:
-        token_t();
-        token_t(const token_t &);
-        token_t &operator =(const token_t&);
-    };
 
-    void
-    token_t::write(ostream &os) const
-    {
-        os << "Token(\"" << tk_lexeme << "\"" << ", \"" << tk_class << "\")";
-    }
-};
-
-using namespace imgblit;
-
-Token::Token(const string &source,
-             string::size_type lexeme_start,
-             string::size_type lexeme_size,
-             const char *token_class) :
-    Reference(new token_t(source,
-                          lexeme_start,
-                          lexeme_size,
-                          token_class))
+void
+Token::write(ostream &os) const
 {
+    os << "Token(\"" << tk_lexeme << "\"" << ", \"" << tk_class << "\")";
 }
 
-token_t *
-Token::get_token_p() const
-{
-    return not_null_guard(dynamic_cast<token_t *>(get_datum_p()));
-}
 
 Scanner::Scanner(const char *source) :
     scr_source(source), scr_read_start(0)
     scr_next.push_back(read_token());
 }
 
-Token
+TokenRef
 Scanner::get_token()
 {
-    Token t = scr_next.back();
+    TokenRef t = scr_next.back();
     scr_next.pop_back();
     if (scr_next.empty()) {
         scr_next.push_back(read_token());
 }
 
 void
-Scanner::push_back_token(const Token t)
+Scanner::push_back_token(const TokenRef t)
 {
     scr_next.push_back(t);
 }
 
-Token
+TokenRef
 Scanner::read_token()
 {
     const char * const token_class = "token";
     }
     scr_read_start = i;
     if (i == end) {
-        return Token();
+        return TokenRef();
     }
     while (scr_read_start != end &&
            !isspace(scr_source[scr_read_start])) {
         ++scr_read_start;
     }
-    return Token(scr_source, i, scr_read_start - i, token_class);
+    return TokenRef(new Token(scr_source, i, scr_read_start - i, token_class));
 }
 
 Parser::Parser(Scanner &source) : psr_source(source)

File test/testscanner.cpp

View file
-#include "imgblit/scanner.h"
+#include "imgblit/scanner.hpp"
 #include <sstream>
 #include <cassert>
 
 int main()
 {
     Scanner scan("one two three");
-    Token t_one = scan.get_token();
-    Token t_two = scan.get_token();
-    Token t_three = scan.get_token();
-    Token t_none = scan.get_token();
+    TokenRef t_one(scan.get_token());
+    TokenRef t_two(scan.get_token());
+    TokenRef t_three(scan.get_token());
+    TokenRef t_none(scan.get_token());
     stringstream capture;
 
     // Three tokens are returned. The fourth is the non reference.
-    capture << static_cast<bool>(t_one) << static_cast<bool>(t_two)
-	    << static_cast<bool>(t_three) << static_cast<bool>(t_none);
+    capture << !!t_one << !!t_two << !!t_three << !!t_none;
     assert(capture.str() == "1110");
 
     // The token lexems are "one", "two", and "three".
     // Go back to the beginning of the source string.
     capture.str("");
     scan.reset();
-    Token t_reset_one = scan.get_token();
-    Token t_reset_two = scan.get_token();
-    Token t_reset_three = scan.get_token();
-    Token t_reset_none = scan.get_token();
+    TokenRef t_reset_one(scan.get_token());
+    TokenRef t_reset_two(scan.get_token());
+    TokenRef t_reset_three(scan.get_token());
+    TokenRef t_reset_none(scan.get_token());
     capture << t_reset_one << t_reset_two << t_reset_three << t_reset_none;
     assert(capture.str() == ("Token(\"one\", \"token\")"
 			     "Token(\"two\", \"token\")"