tclpkg-matlabengine.cc

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2001-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: Tue Jul 10 12:16:44 2001
00010 // commit: $Id: tclpkg-matlabengine.cc 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/pkgs/matlabengine/tclpkg-matlabengine.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_PKGS_MATLABENGINE_TCLPKG_MATLABENGINE_CC_UTC20050628170029_DEFINED
00035 #define GROOVX_PKGS_MATLABENGINE_TCLPKG_MATLABENGINE_CC_UTC20050628170029_DEFINED
00036 
00037 #include "pkgs/matlabengine/tclpkg-matlabengine.h"
00038 
00039 #include "pkgs/mtx/mtxobj.h"
00040 
00041 #include "mtx/matlabinterface.h"
00042 
00043 #include "nub/object.h"
00044 #include "nub/objfactory.h"
00045 #include "nub/ref.h"
00046 
00047 #include "rutz/sfmt.h"
00048 
00049 #include "tcl/objpkg.h"
00050 #include "tcl/pkg.h"
00051 
00052 #include "rutz/error.h"
00053 
00054 #include "rutz/trace.h"
00055 
00056 #ifndef GVX_NO_MATLAB
00057 
00058 #include <engine.h>
00059 
00060 class MatlabEngine : public nub::object
00061 {
00062 private:
00063   MatlabEngine(const MatlabEngine&);
00064   MatlabEngine& operator=(const MatlabEngine&);
00065 
00066 protected:
00067   MatlabEngine() :
00068     itsEngine(engOpen("matlab -nosplash")),
00069     itsBufSize(512),
00070     itsBuf(0)
00071   {
00072     if (itsEngine == 0)
00073       {
00074         throw rutz::error("couldn't open MATLAB engine", SRC_POS);
00075       }
00076 
00077     itsBuf = new char[itsBufSize];
00078     engOutputBuffer(itsEngine, itsBuf, itsBufSize-1);
00079   }
00080 
00081 public:
00082   static MatlabEngine* make() { return new MatlabEngine; }
00083 
00084   virtual ~MatlabEngine() throw()
00085   {
00086     delete [] itsBuf;
00087     engClose(itsEngine);
00088   }
00089 
00090   const char* evalString(const char* cmd)
00091   {
00092     int result = engEvalString(itsEngine, cmd);
00093     return (result == 0) ? (itsBuf+2) : "";
00094   }
00095 
00096   nub::ref<MtxObj> getMtx(const char* name)
00097   {
00098     mxArray* arr = engGetVariable(itsEngine, name);
00099     if (arr == 0)
00100       {
00101         throw rutz::error(rutz::sfmt("no such MATLAB variable: '%s'",
00102                                      name), SRC_POS);
00103       }
00104 
00105     nub::ref<MtxObj> m(new MtxObj(make_mtx(arr, mtx::COPY)));
00106 
00107     mxDestroyArray(arr);
00108 
00109     return m;
00110   }
00111 
00112   void putArray(const mxArray* arr, const char* name)
00113   {
00114     int result = engPutVariable(itsEngine, name, arr);
00115     if (result != 0)
00116       {
00117         throw rutz::error("error while putting mxArray "
00118                           "into MATLAB engine", SRC_POS);
00119       }
00120   }
00121 
00122 private:
00123   Engine* itsEngine;
00124   const int itsBufSize;
00125   char* itsBuf;
00126 };
00127 
00128 #else // defined(GVX_NO_MATLAB)
00129 
00130 class MatlabEngine : public nub::object
00131 {
00132 public:
00133   virtual ~MatlabEngine() throw() {}
00134 
00135   static MatlabEngine* make() { return new MatlabEngine; }
00136 
00137   void noSupport()
00138   {
00139     throw rutz::error("matlab is not supported in this build", SRC_POS);
00140   }
00141 
00142   const char* evalString(const char*)
00143   {
00144     noSupport();
00145     return "can't happen";
00146   }
00147 
00148   nub::ref<MtxObj> getMtx(const char*)
00149   {
00150     noSupport();
00151     return nub::ref<MtxObj>(new MtxObj(mtx::empty_mtx()));
00152   }
00153 };
00154 
00155 #endif
00156 
00157 extern "C"
00158 int Matlabengine_Init(Tcl_Interp* interp)
00159 {
00160 GVX_TRACE("Matlabengine_Init");
00161 
00162   GVX_PKG_CREATE(pkg, interp, "MatlabEngine", "4.$Revision: 10065 $");
00163   tcl::def_basic_type_cmds<MatlabEngine>(pkg, SRC_POS);
00164 
00165   pkg->def( "eval", "engine_id command", &MatlabEngine::evalString, SRC_POS );
00166   pkg->def( "get", "engine_id mtx_name", &MatlabEngine::getMtx, SRC_POS );
00167 
00168   pkg->eval("proc meval {args} { return [eval MatlabEngine::eval $args] }");
00169   pkg->eval("proc getMtx {args} { return [eval MatlabEngine::get $args] }");
00170 
00171   nub::obj_factory::instance().register_creator(&MatlabEngine::make);
00172 
00173   GVX_PKG_RETURN(pkg);
00174 }
00175 
00176 static const char __attribute__((used)) vcid_groovx_pkgs_matlabengine_tclpkg_matlabengine_cc_utc20050628170029[] = "$Id: tclpkg-matlabengine.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00177 #endif // !GROOVX_PKGS_MATLABENGINE_TCLPKG_MATLABENGINE_CC_UTC20050628170029_DEFINED

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