00001 /** @file tcl/interp.h c++ wrapper for Tcl_Interp, translates between 00002 tcl error codes and c++ exceptions */ 00003 00004 /////////////////////////////////////////////////////////////////////// 00005 // 00006 // Copyright (c) 2000-2004 California Institute of Technology 00007 // Copyright (c) 2004-2007 University of Southern California 00008 // Rob Peters <rjpeters at usc dot edu> 00009 // 00010 // created: Wed Oct 11 10:25:36 2000 00011 // commit: $Id: interp.h 11876 2009-10-22 15:53:06Z icore $ 00012 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/tcl/interp.h $ 00013 // 00014 // -------------------------------------------------------------------- 00015 // 00016 // This file is part of GroovX 00017 // [http://ilab.usc.edu/rjpeters/groovx/] 00018 // 00019 // GroovX is free software; you can redistribute it and/or modify it 00020 // under the terms of the GNU General Public License as published by 00021 // the Free Software Foundation; either version 2 of the License, or 00022 // (at your option) any later version. 00023 // 00024 // GroovX is distributed in the hope that it will be useful, but 00025 // WITHOUT ANY WARRANTY; without even the implied warranty of 00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00027 // General Public License for more details. 00028 // 00029 // You should have received a copy of the GNU General Public License 00030 // along with GroovX; if not, write to the Free Software Foundation, 00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00032 // 00033 /////////////////////////////////////////////////////////////////////// 00034 00035 #ifndef GROOVX_TCL_INTERP_H_UTC20050628162420_DEFINED 00036 #define GROOVX_TCL_INTERP_H_UTC20050628162420_DEFINED 00037 00038 #include "tcl/conversions.h" 00039 #include "tcl/obj.h" 00040 00041 #include "rutz/fileposition.h" 00042 #include "rutz/shared_ptr.h" 00043 00044 struct Tcl_Interp; 00045 typedef struct Tcl_Obj Tcl_Obj; 00046 00047 namespace rutz 00048 { 00049 class fstring; 00050 } 00051 00052 namespace tcl 00053 { 00054 class interpreter; 00055 class list; 00056 00057 /// Different error-handling strategies for tcl::interpreter::eval(). 00058 enum error_strategy 00059 { 00060 THROW_ERROR, 00061 IGNORE_ERROR 00062 }; 00063 } 00064 00065 00066 // ######################################################## 00067 /// tcl::interpreter provides a wrapper around Tcl_Interp calls. 00068 /** The advantage over using the raw Tcl C API is that certain error 00069 conditions are handled in a more C++-ish way, by throwing 00070 exceptions. */ 00071 00072 class tcl::interpreter 00073 { 00074 interpreter& operator=(const interpreter&); 00075 00076 public: 00077 interpreter(Tcl_Interp* interp); 00078 interpreter(const interpreter& other) throw(); 00079 ~interpreter() throw(); 00080 00081 // Interpreter 00082 bool is_valid() const throw() { return m_interp != 0; } 00083 00084 /// Get the interpreter (if valid), otherwise throw an exception. 00085 Tcl_Interp* intp() const; 00086 00087 bool is_deleted() const throw(); 00088 void forget_interp() throw(); 00089 void destroy() throw(); 00090 00091 /// Wrapper around Tcl_PkgProvide(). 00092 void pkg_provide(const char* name, const char* version); 00093 00094 /// Evaluate the given expression, return its result as a bool. 00095 bool eval_boolean_expr(const tcl::obj& obj) const; 00096 00097 /// Evaluates code. 00098 /** If strategy is THROW_ERROR, then an exception is thrown if the 00099 evaluation produces an error. If strategy is IGNORE_ERROR, then 00100 a return value of true indicates a successful evaluation, and a 00101 return value of false indicates an error during evaluation. */ 00102 bool eval(const char* code, error_strategy strategy = THROW_ERROR); 00103 00104 /// Evaluates code. 00105 /** If strategy is THROW_ERROR, then an exception is thrown if the 00106 evaluation produces an error. If strategy is IGNORE_ERROR, then 00107 a return value of true indicates a successful evaluation, and a 00108 return value of false indicates an error during evaluation. */ 00109 bool eval(const rutz::fstring& code, error_strategy strategy = THROW_ERROR); 00110 00111 /// Evaluates code. 00112 /** If strategy is THROW_ERROR, then an exception is thrown if the 00113 evaluation produces an error. If strategy is IGNORE_ERROR, then 00114 a return value of true indicates a successful evaluation, and a 00115 return value of false indicates an error during evaluation. */ 00116 bool eval(const tcl::obj& code, error_strategy strategy = THROW_ERROR); 00117 00118 /// Evaluates code using Tcl_EvalObjv(), exploiting the fact that the object is already a list. 00119 /** If strategy is THROW_ERROR, then an exception is thrown if the 00120 evaluation produces an error. If strategy is IGNORE_ERROR, then 00121 a return value of true indicates a successful evaluation, and a 00122 return value of false indicates an error during evaluation. */ 00123 bool eval_objv(const tcl::list& objv, error_strategy strategy = THROW_ERROR); 00124 00125 /// Evaluate the tcl code in the named file. 00126 /** Returns true on success, or false on failure. */ 00127 bool eval_file(const char* fname); 00128 00129 void source_rc_file(); 00130 00131 // Result 00132 void reset_result() const; 00133 void append_result(const char* msg) const; 00134 void append_result(const rutz::fstring& msg) const; 00135 00136 template <class T> 00137 T get_result() const 00138 { 00139 return tcl::convert_to<T>(get_obj_result()); 00140 } 00141 00142 template <class T> 00143 void set_result(const T& x) 00144 { 00145 set_obj_result(tcl::convert_from(x).get()); 00146 } 00147 00148 // Variables 00149 void set_global_var(const char* var_name, const tcl::obj& var) const; 00150 void unset_global_var(const char* var_name) const; 00151 00152 template <class T> 00153 T get_global_var(const char* name1, const char* name2=0) const 00154 { 00155 return tcl::convert_to<T>(get_obj_global_var(name1, name2)); 00156 } 00157 00158 void link_int(const char* var_name, int* addr, bool read_only); 00159 void link_double(const char* var_name, double* addr, bool read_only); 00160 void link_boolean(const char* var_name, int* addr, bool read_only); 00161 00162 // Errors 00163 void handle_live_exception(const char* where, 00164 const rutz::file_pos& pos) throw(); 00165 void background_error() throw(); 00166 00167 void add_error_info(const char* info); 00168 00169 // Events 00170 static void clear_event_queue(); 00171 00172 // Commands/procedures 00173 bool has_command(const char* cmd_name) const; 00174 void delete_command(const char* cmd_name); 00175 00176 rutz::fstring get_proc_body(const char* proc_name); 00177 void create_proc(const char* namesp, const char* proc_name, 00178 const char* args, const char* body); 00179 void delete_proc(const char* namesp, const char* proc_name); 00180 00181 private: 00182 Tcl_Obj* get_obj_result() const; 00183 Tcl_Obj* get_obj_global_var(const char* name1, const char* name2) const; 00184 void set_obj_result(Tcl_Obj* obj); 00185 00186 Tcl_Interp* m_interp; 00187 }; 00188 00189 static const char __attribute__((used)) vcid_groovx_tcl_interp_h_utc20050628162420[] = "$Id: interp.h 11876 2009-10-22 15:53:06Z icore $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/tcl/interp.h $"; 00190 #endif // !GROOVX_TCL_INTERP_H_UTC20050628162420_DEFINED