tclpkg-hook.cc

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2000-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: Thu Oct  5 13:51:43 2000
00010 // commit: $Id: tclpkg-hook.cc 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/visx/tclpkg-hook.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_VISX_TCLPKG_HOOK_CC_UTC20050628171008_DEFINED
00035 #define GROOVX_VISX_TCLPKG_HOOK_CC_UTC20050628171008_DEFINED
00036 
00037 #include "visx/tclpkg-hook.h"
00038 
00039 #include "tcl/pkg.h"
00040 
00041 #include "rutz/trace.h"
00042 
00043 namespace
00044 {
00045   size_t TOTAL = 0;
00046 }
00047 
00048 #if 0
00049 #include <cstdlib>
00050 #include <cstdio>
00051 
00052 void* operator new(size_t bytes)
00053 {
00054   TOTAL += bytes;
00055   printf("%d bytes  ", (int) bytes);
00056   rutz::backtrace::current().print();
00057   return malloc(bytes);
00058 }
00059 
00060 void operator delete(void* space)
00061 {
00062   free(space);
00063 }
00064 #endif
00065 
00066 namespace HookTcl
00067 {
00068   size_t memUsage() { return TOTAL; }
00069 }
00070 
00071 #include "rutz/debug.h"
00072 GVX_DBG_REGISTER
00073 
00074 #include <cstring>
00075 #include <tcl.h>
00076 
00077 class GenericObj
00078 {
00079 public:
00080   virtual ~GenericObj() {}
00081 
00082   static char* stringAlloc(unsigned int size) { return Tcl_Alloc(size); }
00083   static void stringFree(char* space) { Tcl_Free(space); }
00084 
00085   virtual GenericObj* clone() const = 0;
00086   virtual char* asString() const = 0;
00087 };
00088 
00089 extern Tcl_ObjType genericObjType;
00090 
00091 void genericFreeInternalRepProc(Tcl_Obj* objPtr)
00092 {
00093 GVX_TRACE("genericFreeInternalRepProc");
00094   GenericObj* gobj =
00095     static_cast<GenericObj*>(objPtr->internalRep.otherValuePtr);
00096 
00097   delete gobj;
00098 
00099   objPtr->internalRep.otherValuePtr = 0;
00100 }
00101 
00102 void genericDupInternalRepProc(Tcl_Obj* srcPtr, Tcl_Obj* dupPtr)
00103 {
00104 GVX_TRACE("genericDupInternalRepProc");
00105   GenericObj* gobj =
00106     static_cast<GenericObj*>(srcPtr->internalRep.otherValuePtr);
00107 
00108   if (dupPtr->typePtr != 0 && dupPtr->typePtr->freeIntRepProc != 0)
00109     {
00110       dupPtr->typePtr->freeIntRepProc(dupPtr);
00111     }
00112 
00113   dupPtr->internalRep.otherValuePtr = static_cast<void*>(gobj->clone());
00114 
00115   dupPtr->typePtr = &genericObjType;
00116 }
00117 
00118 void genericUpdateStringProc(Tcl_Obj* objPtr)
00119 {
00120 GVX_TRACE("genericUpdateStringProc");
00121   GenericObj* gobj =
00122     static_cast<GenericObj*>(objPtr->internalRep.otherValuePtr);
00123 
00124   GVX_ASSERT(gobj != 0);
00125 
00126   GVX_ASSERT(objPtr->bytes == 0);
00127 
00128   objPtr->bytes = gobj->asString();
00129   objPtr->length = strlen(objPtr->bytes);
00130 }
00131 
00132 int genericSetFromAnyProc(Tcl_Interp* interp, Tcl_Obj* /*objPtr*/)
00133 {
00134 GVX_TRACE("genericSetFromAnyProc");
00135 
00136   Tcl_AppendResult(interp, "can't convert to generic object type", 0);
00137   return TCL_ERROR;
00138 }
00139 
00140 Tcl_ObjType genericObjType =
00141   {
00142     const_cast<char*>("generic"),
00143     genericFreeInternalRepProc,
00144     genericDupInternalRepProc,
00145     genericUpdateStringProc,
00146     genericSetFromAnyProc
00147   };
00148 
00149 #include <cstdio>
00150 #include <cstdlib>
00151 
00152 class MyObj : public GenericObj
00153 {
00154   MyObj(const MyObj&);
00155   MyObj& operator=(const MyObj&);
00156 
00157   static int counter;
00158 
00159 public:
00160   MyObj(int v) : value(v)
00161   {
00162     ++counter;
00163   }
00164 
00165   virtual ~MyObj()
00166   {
00167     value = -12345678;
00168     --counter;
00169   }
00170 
00171   static void convert(Tcl_Obj* objPtr)
00172   {
00173     if (objPtr->typePtr == &genericObjType)
00174       return;
00175 
00176     const char* strrep = Tcl_GetString(objPtr);
00177 
00178     int v = atoi(strrep);
00179 
00180     if (objPtr->typePtr != 0 && objPtr->typePtr->freeIntRepProc != 0)
00181       {
00182         objPtr->typePtr->freeIntRepProc(objPtr);
00183       }
00184 
00185     objPtr->typePtr = &genericObjType;
00186 
00187     objPtr->internalRep.otherValuePtr = new MyObj(v);
00188   }
00189 
00190   static Tcl_Obj* make(int v)
00191   {
00192     Tcl_Obj* objPtr = Tcl_NewObj();
00193     objPtr->typePtr = &genericObjType;
00194     objPtr->internalRep.otherValuePtr = new MyObj(v);
00195 
00196     Tcl_InvalidateStringRep(objPtr);
00197 
00198     return objPtr;
00199   }
00200 
00201   static void info(Tcl_Obj* p)
00202   {
00203     dbg_eval_nl(3, p->typePtr ? p->typePtr->name : "");
00204     dbg_eval_nl(3, p->refCount);
00205 
00206     convert(p);
00207 
00208     MyObj* gobj =
00209       static_cast<MyObj*>(p->internalRep.otherValuePtr);
00210 
00211     dbg_eval_nl(3, p);
00212     dbg_eval_nl(3, p->typePtr ? p->typePtr->name : "");
00213     dbg_eval_nl(3, p->refCount);
00214     dbg_eval_nl(3, p->bytes);
00215     dbg_eval_nl(3, p->length);
00216     dbg_eval_nl(3, gobj);
00217     dbg_eval_nl(3, gobj->value);
00218     dbg_eval_nl(3, MyObj::counter);
00219   }
00220 
00221   virtual MyObj* clone() const
00222   {
00223     return new MyObj(value);
00224   }
00225 
00226   virtual char* asString() const
00227   {
00228     char* space = stringAlloc(32);
00229 
00230     snprintf(space, 32, "%d", value);
00231 
00232     return space;
00233   }
00234 
00235   int value;
00236 };
00237 
00238 int MyObj::counter = 0;
00239 
00240 #include "gfx/gbvec.h"
00241 #include "rutz/arrayvalue.h"
00242 #include "rutz/iter.h"
00243 #include "tcl/itertcl.h"
00244 #include "tcl/dict.h"
00245 
00246 namespace
00247 {
00248   GbVec3<double> data[4];
00249 
00250   rutz::array_value<GbVec3<double> > arraydata;
00251 
00252   const rutz::array_value<GbVec3<double> >& getArray() { return arraydata; }
00253 
00254   unsigned int getArraySize() { return arraydata.array_size(); }
00255 
00256   void setArray(const rutz::array_value<GbVec3<double> >& a) { arraydata = a; }
00257 
00258   rutz::fwd_iter<GbVec3<double > > getData()
00259   {
00260     return rutz::fwd_iter<GbVec3<double> >(&data[0], &data[4]);
00261   }
00262 
00263   void setData(rutz::fwd_iter<const GbVec3<double> > iter)
00264   {
00265     int i = 0;
00266     while (i < 4 && !iter.at_end())
00267       {
00268         data[i] = *iter;
00269         ++iter; ++i;
00270       }
00271   }
00272 
00273   tcl::obj dictGet(tcl::dict dict, const char* key)
00274   {
00275     return dict.get<tcl::obj>(key);
00276   }
00277 }
00278 
00279 #include "tcl-gfx/toglet.h"
00280 #if defined(GVX_GL_PLATFORM_GLX)
00281 #  include <GL/gl.h>
00282 #elif defined(GVX_GL_PLATFORM_AGL)
00283 #  include <AGL/gl.h>
00284 #endif
00285 
00286 namespace
00287 {
00288   void bug()
00289   {
00290     nub::soft_ref<Toglet> t = Toglet::getCurrent();
00291 
00292     glTranslated(-2.56, -2.56, 0.0);
00293 
00294     const int SIZE = 512;
00295 
00296     unsigned char buf[SIZE*SIZE];
00297 
00298     unsigned char* bytes = &buf[0];
00299 
00300     for (int y = 0; y < SIZE; ++y)
00301       for (int x = 0; x < SIZE; ++x)
00302         *bytes++ = (x*y) % 256;
00303 
00304     glRasterPos3d(2.56, 2.56, 0.0);
00305 
00306     glBitmap(0, 0, 0.0f, 0.0f,
00307              -256, -256,
00308              static_cast<const GLubyte*>(0));
00309 
00310     glDrawPixels(SIZE, SIZE,
00311                  GL_LUMINANCE, GL_UNSIGNED_BYTE,
00312                  static_cast<GLvoid*>(&buf[0]));
00313 
00314     t->swapBuffers();
00315   }
00316 }
00317 
00318 extern "C"
00319 int Hook_Init(Tcl_Interp* interp)
00320 {
00321   GVX_PKG_CREATE(pkg, interp, "Hook", "4.$Revision: 10065 $");
00322 
00323   pkg->def( "::bug", "", &bug, SRC_POS );
00324 
00325   pkg->def( "::memUsage", 0, HookTcl::memUsage, SRC_POS );
00326 
00327   Tcl_RegisterObjType(&genericObjType);
00328 
00329   pkg->def( "::myobj", "val", MyObj::make, SRC_POS );
00330   pkg->def( "::myinfo", "obj", MyObj::info, SRC_POS );
00331 
00332   pkg->def( "::getData", 0, getData, SRC_POS );
00333   pkg->def( "::setData", "list", setData, SRC_POS );
00334 
00335   pkg->def( "::getArray", 0, getArray, SRC_POS );
00336   pkg->def( "::getArraySize", 0, getArraySize, SRC_POS );
00337   pkg->def( "::setArray", "list", setArray, SRC_POS );
00338 
00339   pkg->def( "::dictGet", "dict key", dictGet, SRC_POS );
00340 
00341   GVX_PKG_RETURN(pkg);
00342 }
00343 
00344 static const char __attribute__((used)) vcid_groovx_visx_tclpkg_hook_cc_utc20050628171008[] = "$Id: tclpkg-hook.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00345 #endif // !GROOVX_VISX_TCLPKG_HOOK_CC_UTC20050628171008_DEFINED

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