command.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: Fri Jun 11 14:50:43 1999
00011 // commit: $Id: command.h 10065 2007-04-12 05:54:56Z rjpeters $
00012 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/tcl/command.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_COMMAND_H_UTC20050628162421_DEFINED
00036 #define GROOVX_TCL_COMMAND_H_UTC20050628162421_DEFINED
00037 
00038 #include "tcl/argspec.h"
00039 #include "tcl/conversions.h"
00040 #include "tcl/obj.h"
00041 
00042 #include "rutz/shared_ptr.h"
00043 
00044 typedef struct Tcl_Obj Tcl_Obj;
00045 
00046 namespace rutz
00047 {
00048   class file_pos;
00049   class fstring;
00050   template <class T> class shared_ptr;
00051 }
00052 
00053 namespace tcl
00054 {
00055   class arg_spec;
00056   class function;
00057   class command;
00058   class call_context;
00059   class arg_dispatcher;
00060   class interpreter;
00061 }
00062 
00064 class tcl::function
00065 {
00066 public:
00067   virtual ~function() throw();
00068 
00070 
00073   virtual void invoke(tcl::call_context& ctx) = 0;
00074 };
00075 
00077 
00100 
00101 
00102 class tcl::command
00103 {
00104 public:
00106 
00113   command(rutz::shared_ptr<tcl::function> callback,
00114           const char* usage, const arg_spec& spec);
00115 
00117   ~command() throw();
00118 
00120   rutz::fstring usage_string() const;
00121 
00123   bool allows_argc(unsigned int objc) const;
00124 
00126   bool rejects_argc(unsigned int objc) const;
00127 
00129   void call(tcl::interpreter& interp,
00130             unsigned int objc, Tcl_Obj* const objv[]);
00131 
00133   rutz::shared_ptr<arg_dispatcher> get_dispatcher() const;
00134 
00136   void set_dispatcher(rutz::shared_ptr<arg_dispatcher> dpx);
00137 
00138 private:
00139   command(const command&); // not implemented
00140   command& operator=(const command&); // not implemented
00141 
00142   class impl;
00143   impl* const rep;
00144 };
00145 
00147 
00158 
00159 
00160 class tcl::arg_dispatcher
00161 {
00162 public:
00164   virtual ~arg_dispatcher() throw();
00165 
00169   virtual void dispatch(tcl::interpreter& interp,
00170                         unsigned int objc, Tcl_Obj* const objv[],
00171                         tcl::function& callback) = 0;
00172 };
00173 
00175 
00182 
00183 
00184 class tcl::call_context
00185 {
00186 public:
00187   friend class tcl::command;
00188 
00190   call_context(tcl::interpreter& interp,
00191                unsigned int objc, Tcl_Obj* const* objv);
00192 
00194   virtual ~call_context() throw();
00195 
00197   tcl::interpreter& interp() const throw() { return m_interp; }
00198 
00200   unsigned int objc() const throw() { return m_objc; }
00201 
00202 
00205   template <class T>
00206   typename returnable<T>::type get_arg(unsigned int argn)
00207     {
00208       return tcl::convert_to<T>(get_objv(argn));
00209     }
00210 
00211 
00213   template <class T>
00214   void set_result(T t)
00215     {
00216       set_obj_result(tcl::convert_from<T>(t));
00217     }
00218 
00220   Tcl_Obj* const* get_raw_args() const throw()
00221     {
00222       return m_objv;
00223     }
00224 
00225 protected:
00227   virtual Tcl_Obj* get_objv(unsigned int n) throw() { return m_objv[n]; }
00228 
00230   virtual void set_obj_result(const tcl::obj& obj);
00231 
00232 private:
00233   call_context(const call_context&);
00234   call_context& operator=(const call_context&);
00235 
00236   tcl::interpreter&        m_interp;
00237   unsigned int       const m_objc;
00238   Tcl_Obj* const*    const m_objv;
00239 };
00240 
00241 static const char __attribute__((used)) vcid_groovx_tcl_command_h_utc20050628162421[] = "$Id: command.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00242 #endif // !GROOVX_TCL_COMMAND_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.