tclprocwrapper.cc

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2002-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Wed Jan 30 11:47:10 2002
00010 // commit: $Id: tclprocwrapper.cc 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/tcl-io/tclprocwrapper.cc $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_TCL_IO_TCLPROCWRAPPER_CC_UTC20050712162004_DEFINED
00035 #define GROOVX_TCL_IO_TCLPROCWRAPPER_CC_UTC20050712162004_DEFINED
00036 
00037 #include "tcl-io/tclprocwrapper.h"
00038 
00039 #include "io/reader.h"
00040 #include "io/writer.h"
00041 
00042 #include "rutz/sfmt.h"
00043 
00044 using rutz::fstring;
00045 
00046 namespace
00047 {
00048   fstring uniqName()
00049   {
00050     static int c = 0;
00051     return rutz::sfmt("::ProcWrapper_UniqCmd%d", ++c);
00052   }
00053 }
00054 
00055 tcl::ProcWrapper::ProcWrapper(const tcl::interpreter& interp) :
00056   itsInterp(interp),
00057   itsName(uniqName()),
00058   itsArgs(),
00059   itsBody()
00060 {}
00061 
00062 tcl::ProcWrapper::ProcWrapper(const tcl::interpreter& interp,
00063                               const fstring& name) :
00064   itsInterp(interp),
00065   itsName(name),
00066   itsArgs(),
00067   itsBody()
00068 {}
00069 
00070 tcl::ProcWrapper::~ProcWrapper() throw()
00071 {
00072   // We must check is_valid() in case we are here because the application
00073   // is being exited, causing the interpreter to be deleted, in which case
00074   // we can't use the intrepeter (and don't need to, since it will destroy
00075   // the proc on its own)
00076   if (itsInterp.is_valid())
00077     {
00078       if (itsInterp.has_command(itsName.c_str()))
00079         itsInterp.delete_proc("", itsName.c_str());
00080     }
00081 }
00082 
00083 void tcl::ProcWrapper::read_from(io::reader& reader)
00084 {
00085   fstring args, body;
00086   reader.read_value("args", args);
00087   reader.read_value("body", body);
00088   define(args, body);
00089 }
00090 
00091 void tcl::ProcWrapper::write_to(io::writer& writer) const
00092 {
00093   writer.write_value("args", itsArgs);
00094   writer.write_value("body", itsBody);
00095 }
00096 
00097 void tcl::ProcWrapper::define(const fstring& args, const fstring& body)
00098 {
00099   itsArgs = args;
00100   itsBody = body;
00101   itsInterp.create_proc("", itsName.c_str(),
00102                        itsArgs.c_str(), itsBody.c_str());
00103 }
00104 
00105 bool tcl::ProcWrapper::isNoop() const
00106 {
00107   return (itsArgs.is_empty() && itsBody.is_empty());
00108 }
00109 
00110 void tcl::ProcWrapper::invoke(const fstring& args)
00111 {
00112   if (isNoop()) return;
00113 
00114   itsInterp.eval(rutz::sfmt("%s %s", itsName.c_str(), args.c_str()));
00115 }
00116 
00117 fstring tcl::ProcWrapper::fullSpec() const
00118 {
00119   return rutz::sfmt("{%s} {%s}", itsArgs.c_str(), itsBody.c_str());
00120 }
00121 
00122 static const char __attribute__((used)) vcid_groovx_tcl_io_tclprocwrapper_cc_utc20050712162004[] = "$Id: tclprocwrapper.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00123 #endif // !GROOVX_TCL_IO_TCLPROCWRAPPER_CC_UTC20050712162004_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.