2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef HEADER_SUPERTUX_LISP_LISP_HPP
18 #define HEADER_SUPERTUX_LISP_LISP_HPP
40 LispType get_type() const
43 const Lisp* get_car() const
44 { return v.cons.car; }
45 const Lisp* get_cdr() const
46 { return v.cons.cdr; }
48 bool get(std::string& val) const
50 if(type != TYPE_STRING && type != TYPE_SYMBOL)
56 std::string get_symbol() const
58 assert(type == TYPE_SYMBOL);
62 std::string get_string() const
64 assert(type == TYPE_STRING);
68 bool get(unsigned int& val) const
70 if(type != TYPE_INTEGER)
76 bool get(int& val) const
78 if(type != TYPE_INTEGER)
86 assert(type == TYPE_INTEGER);
90 bool get(float& val) const
92 if(type != TYPE_REAL) {
93 if(type == TYPE_INTEGER) {
94 val = (float) v.integer;
103 float get_float() const
105 assert(type == TYPE_REAL);
109 bool get(bool& val) const
111 if(type != TYPE_BOOLEAN)
117 bool get_bool() const
119 assert(type == TYPE_BOOLEAN);
123 /** convenience functions which traverse the list until a child with a
124 * specified name is found. The value part is then interpreted in a specific
125 * way. The functions return true, if a child was found and could be
126 * interpreted correctly, otherwise false is returned and the variable value
128 * (Please note that searching the lisp structure is O(n) so these functions
129 * are not a good idea for performance critical areas)
132 bool get(const char* name, T& val) const
134 const Lisp* lisp = get_lisp(name);
138 if(lisp->get_type() != TYPE_CONS)
140 lisp = lisp->get_car();
143 return lisp->get(val);
147 bool get(const char* name, std::vector<T>& vec) const
151 const Lisp* child = get_lisp(name);
155 for( ; child != 0; child = child->get_cdr()) {
157 if(!child->get_car())
159 if(child->get_car()->get(val)) {
167 const Lisp* get_lisp(const char* name) const;
168 const Lisp* get_lisp(const std::string& name) const
169 { return get_lisp(name.c_str()); }
172 void print(int indent = 0) const;
176 Lisp(LispType newtype);
194 } // end of namespace lisp