pkg.h

Go to the documentation of this file.
00001 
00004 
00005 //
00006 // Copyright (c) 1999-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: Tue Jun 15 12:33:59 1999
00011 // commit: $Id: pkg.h 10065 2007-04-12 05:54:56Z rjpeters $
00012 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/tcl/pkg.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 //
00034 
00035 #ifndef GROOVX_TCL_PKG_H_UTC20050628162421_DEFINED
00036 #define GROOVX_TCL_PKG_H_UTC20050628162421_DEFINED
00037 
00038 #include "tcl/makecmd.h"
00039 
00040 #include "rutz/fileposition.h"
00041 
00042 struct Tcl_Interp;
00043 
00044 namespace rutz
00045 {
00046   struct file_pos;
00047 }
00048 
00049 namespace tcl
00050 {
00051   class command;
00052   class interpreter;
00053   class pkg;
00054 
00055   const int NO_EXPORT = 1 << 0;
00056 }
00057 
00059 //
00060 // tcl::pkg class definition
00061 //
00063 
00065 
00080 
00081 
00082 class tcl::pkg
00083 {
00084 private:
00086 
00087   pkg(Tcl_Interp* interp, const char* name, const char* version);
00088 
00090   ~pkg() throw();
00091 
00092 public:
00093   static const int STATUS_OK;
00094   static const int STATUS_ERR;
00095 
00097   static pkg* create_in_macro(Tcl_Interp* interp,
00098                               const char* name, const char* version)
00099   {
00100     return new pkg(interp, name, version);
00101   }
00102 
00103   typedef void (exit_callback)();
00104 
00106 
00109   void on_exit(exit_callback* callback);
00110 
00112 
00117   static int destroy_on_unload(Tcl_Interp* interp, const char* pkgname);
00118 
00120 
00125   static pkg* lookup(tcl::interpreter& interp,
00126                      const char* name, const char* version = 0) throw();
00127 
00130   int init_status() const throw();
00131 
00133   void set_init_status_error() throw();
00134 
00136   tcl::interpreter& interp() throw();
00137 
00139   void handle_live_exception(const rutz::file_pos& pos) throw();
00140 
00142 
00146   const char* namesp_name() throw();
00147 
00149 
00154   const char* pkg_name() const throw();
00155 
00157   const char* version() const throw();
00158 
00160 
00165   void namesp_alias(const char* namesp, const char* pattern = "*");
00166 
00168 
00173   void inherit_namesp(const char* namesp, const char* pattern = "*");
00174 
00176 
00179   void inherit_pkg(const char* name, const char* version = 0);
00180 
00182   void eval(const char* script);
00183 
00185   void link_var(const char* var_name, int& var);
00186 
00188   void link_var(const char* var_name, double& var);
00189 
00191 
00192   void link_var_copy(const char* var_name, int var);
00193 
00195 
00196   void link_var_copy(const char* var_name, double var);
00197 
00199 
00200   void link_var_const(const char* var_name, int& var);
00201 
00203 
00204   void link_var_const(const char* var_name, double& var);
00205 
00206 
00207   template <class Func>
00208   inline void def(const char* cmd_name, const char* usage, Func f,
00209                   const rutz::file_pos& src_pos, int flags = 0)
00210   {
00211     make_command(interp(), f, make_pkg_cmd_name(cmd_name, flags),
00212                  usage, src_pos);
00213   }
00214 
00215   template <class Func>
00216   inline void def_vec(const char* cmd_name, const char* usage, Func f,
00217                       unsigned int keyarg /*default is 1*/,
00218                       const rutz::file_pos& src_pos, int flags = 0)
00219   {
00220     make_vec_command(interp(), f, make_pkg_cmd_name(cmd_name, flags),
00221                      usage, keyarg, src_pos);
00222   }
00223 
00224   template <class Func>
00225   inline void def_raw(const char* cmd_name, const arg_spec& spec,
00226                       const char* usage, Func f,
00227                       const rutz::file_pos& src_pos, int flags = 0)
00228   {
00229     make_generic_command(interp(), f, make_pkg_cmd_name(cmd_name, flags),
00230                          usage, spec, src_pos);
00231   }
00232 
00233   template <class Func>
00234   inline void def_vec_raw(const char* cmd_name, const arg_spec& spec,
00235                           const char* usage, Func f,
00236                           unsigned int keyarg /*default is 1*/,
00237                           const rutz::file_pos& src_pos, int flags = 0)
00238   {
00239     make_generic_vec_command(interp(), f, make_pkg_cmd_name(cmd_name, flags),
00240                              usage, spec, keyarg, src_pos);
00241   }
00242 
00243   template <class C>
00244   void def_action(const char* cmd_name, void (C::* action_func) (),
00245                   const rutz::file_pos& src_pos, int flags = 0)
00246   {
00247     def_vec( cmd_name, action_usage, action_func, 1, src_pos, flags );
00248   }
00249 
00250   template <class C>
00251   void def_action(const char* cmd_name, void (C::* action_func) () const,
00252                   const rutz::file_pos& src_pos, int flags = 0)
00253   {
00254     def_vec( cmd_name, action_usage, action_func, 1, src_pos, flags );
00255   }
00256 
00257   template <class C, class T>
00258   void def_getter(const char* cmd_name, T (C::* getter_func) () const,
00259                   const rutz::file_pos& src_pos, int flags = 0)
00260   {
00261     def_vec( cmd_name, getter_usage, getter_func, 1, src_pos, flags );
00262   }
00263 
00264   template <class C, class T>
00265   void def_setter(const char* cmd_name, void (C::* setter_func) (T),
00266                   const rutz::file_pos& src_pos, int flags = 0)
00267   {
00268     def_vec( cmd_name, setter_usage, setter_func, 1, src_pos, flags );
00269   }
00270 
00271   template <class C, class T>
00272   void def_get_set(const char* cmd_name,
00273                    T (C::* getter_func) () const,
00274                    void (C::* setter_func) (T),
00275                    const rutz::file_pos& src_pos, int flags = 0)
00276   {
00277     def_getter( cmd_name, getter_func, src_pos, flags );
00278     def_setter( cmd_name, setter_func, src_pos, flags );
00279   }
00280 
00282   static void verbose_init(bool verbose) throw();
00283 
00285 
00288   int finish_init() throw();
00289 
00290 private:
00291   pkg(const pkg&); // not implemented
00292   pkg& operator=(const pkg&); // not implemented
00293 
00300   const char* make_pkg_cmd_name(const char* cmd_name, int flags);
00301 
00302   static const char* const action_usage;
00303   static const char* const getter_usage;
00304   static const char* const setter_usage;
00305 
00306   struct impl;
00307   friend struct impl;
00308   impl* rep;
00309 };
00310 
00311 #include "rutz/debug.h"
00312 GVX_DBG_REGISTER
00313 
00314 /*
00315   These macros make it slightly more convenient to make sure that
00316   *_Init() package initialization functions don't leak any exceptions
00317   (as they are called directly from C code within the Tcl core).
00318  */
00319 
00321 
00329 #define GVX_PKG_CREATE(pkg, interp, pkgname, pkgversion)              \
00330                                                                       \
00331 int GVX_PKG_STATUS = tcl::pkg::STATUS_ERR;                            \
00332 {                                                                     \
00333   tcl::pkg* pkg = 0;                                                  \
00334                                                                       \
00335   try                                                                 \
00336     { pkg = tcl::pkg::create_in_macro(interp, pkgname, pkgversion); } \
00337   catch (...)                                                         \
00338     { return 1; }                                                     \
00339                                                                       \
00340   static bool recursive_initialization = false;                       \
00341   GVX_ASSERT(!recursive_initialization);                              \
00342   recursive_initialization = true;                                    \
00343                                                                       \
00344   try                                                                 \
00345   {
00346 
00347 
00349 #define GVX_PKG_RETURN(pkg)                     \
00350   }                                             \
00351   catch(...)                                    \
00352   {                                             \
00353     pkg->handle_live_exception(SRC_POS);        \
00354   }                                             \
00355   recursive_initialization = false;             \
00356   GVX_PKG_STATUS = pkg->finish_init();          \
00357 }                                               \
00358 return GVX_PKG_STATUS;
00359 
00361 #define GVX_PKG_FINISH(pkg)                     \
00362   }                                             \
00363   catch(...)                                    \
00364   {                                             \
00365     pkg->handle_live_exception(SRC_POS);        \
00366   }                                             \
00367   recursive_initialization = false;             \
00368   GVX_PKG_STATUS = pkg->finish_init();          \
00369 }
00370 
00371 
00372 static const char __attribute__((used)) vcid_groovx_tcl_pkg_h_utc20050628162421[] = "$Id: pkg.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00373 #endif // !GROOVX_TCL_PKG_H_UTC20050628162421_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:41 2008 by Doxygen version 1.5.5.