home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / ObjectTcl-1.1 / OtclObject.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-30  |  6.4 KB  |  198 lines

  1. /*  _ __ ___ _
  2.  * | |\ /  /| |  $Id: OtclObject.C,v 1.5 1995/05/09 16:14:40 deans Exp $
  3.  * | | /  / | |  Copyright (C) 1995 IXI Limited.
  4.  * |_|/__/_\|_|  IXI Limited, Cambridge, England.
  5.  *
  6.  * Component   : OtclObject.C
  7.  *
  8.  * Author      : Dean Sheehan (deans@x.co.uk)
  9.  *
  10.  * Description : Contains the implementation for OtclObjeect class that modesl
  11.  *               instances of classes (C++ or Object Tcl classes).
  12.  *
  13.  * License     :
  14.             Object Tcl License & Copyright
  15.             ------------------------------
  16.  
  17. IXI Object Tcl software, both binary and source (hereafter, Software) is copyrighted by IXI Limited (IXI), and ownership remains with IXI. 
  18.  
  19. IXI grants you (herafter, Licensee) a license to use the Software for academic, research and internal business purposes only, without a fee. Licensee may distribute the binary and source code (if required) to third parties provided that the copyright notice and this statement appears on all copies and that no charge is associated with such copies. 
  20.  
  21. Licensee may make derivative works. However, if Licensee distributes any derivative work based on or derived from the Software, then Licensee will (1) notify IXI regarding its distribution of the derivative work, and (2) clearly notify users that such derivative work is a modified version and not the original IXI Object Tcl distributed by IXI. IXI strongly recommends that Licensee provide IXI the right to incorporate such modifications into future releases of the Software under these license terms. 
  22.  
  23. Any Licensee wishing to make commercial use of the Software should contact IXI, to negotiate an appropriate license for such commercial use. Commercial use includes (1) integration of all or part of the source code into a product for sale or license by or on behalf of Licensee to third parties, or (2) distribution of the binary code or source code to third parties that need it to utilize a commercial product sold or licensed by or on behalf of Licensee. 
  24.  
  25. IXI MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. IXI SHALL NOT BE LIABLE FOR ANY DAMAGES WHATSOEVER SUFFERED BY THE USERS OF THIS SOFTWARE. 
  26.  
  27. Copyright (C) 1995, IXI Limited 
  28.  
  29. By using or copying this Software, Licensee agrees to abide by the copyright law and all other applicable laws of England and the U.S., including, but not limited to, export control laws, and the terms of this license. IXI shall have the right to terminate this license immediately by written notice upon Licensee's breach of, or non-compliance with, any of its terms. Licensee may be held legally responsible for any copyright infringement that is caused or encouraged by Licensee's failure to abide by the terms of this license. 
  30.  
  31. Comments and questions are welcome and can be sent to
  32. otcl@x.co.uk 
  33.  
  34. For more information on copyright and licensing issues, contact: 
  35. Legal Department, IXI Limited, Vision Park, Cambridge CB4 4ZR,
  36. ENGLAND. 
  37.  
  38.  *
  39.  */
  40.  
  41. // System Includes
  42. #include <stdlib.h>
  43. #include <string.h>
  44.  
  45. // Local Includes
  46. #include "OtclObject.H"
  47. #include "OtclClass.H"
  48. #include "OtclPart.H"
  49.  
  50. // Local Defines
  51. #define UNKNOWN_METHOD_NAME "unknown"
  52.  
  53. OtclObject::OtclObject ()
  54. {
  55.    otclop = NULL;
  56.    self = NULL;
  57.    discarding = OTCL_FALSE;
  58. }
  59.  
  60. OtclObject::~OtclObject ()
  61. {
  62.    if (otclop != NULL)
  63.    {
  64.       delete otclop;
  65.    }
  66.    if (self != NULL)
  67.    {
  68.       free(self);
  69.    }
  70. }
  71.  
  72. void OtclObject::setSelf (char *s)
  73. {
  74.    self = strdup(s);
  75. }
  76.  
  77. void OtclObject::setPart (OtclPart *part)
  78. {
  79.    otclop = part;
  80. }
  81.  
  82. int OtclObject::discard (Tcl_Interp *interp, int fromCpp)
  83. {
  84.    discarding = OTCL_TRUE;
  85.  
  86.    // discardPart also 'deletes' the part!
  87.    int returnCode = otclop->discardPart(interp,fromCpp);
  88.  
  89.    otclop = NULL;
  90.  
  91.    delete this;
  92.  
  93.    return returnCode;
  94. }
  95.  
  96. void OtclObject::addObjectScope (Tcl_Interp *interp)
  97. {
  98.    Tcl_SetVar(interp,OTCL_SELF_ATTRIBUTE,self,0);
  99. }
  100.  
  101. void OtclObject::removeObjectScope (Tcl_Interp *)
  102. {
  103.    // remove self attribute
  104.    // This is done when the call frame is cleaned up
  105.    // by the OtclClass::destroyClassScope
  106. }
  107.  
  108. int OtclObject::executeMethod (Tcl_Interp *interp, char *methodName,
  109.                                int argc, char *argv[])
  110. {
  111.    int found = OTCL_FALSE;
  112.    int result;
  113.    if (*methodName == '-')
  114.    {
  115.       // Need to check the number of arguments as well!!!!!
  116.  
  117.       // Special case. The method name is -<Parent> where Parent is the
  118.       // name of the superclass where to find the method (or start searching
  119.       // from anyway). The OTCL_PART_ATTRIBUTE_NAME in the current interp
  120.       // frame specifies the current OtclPart that is calling (if any)
  121.       char *otclPartStr = Tcl_GetVar2(interp,OTCL_PART_ATTRIBUTE_NAME,NULL,0);
  122.       if (otclPartStr == NULL)
  123.       {
  124.          // Couldn't have been calling from inside any instance method
  125.          // Error !
  126.          return TCL_ERROR;
  127.       }
  128.       OtclPartOtcl *otclpPtr;
  129.       sscanf(otclPartStr,"%lx",(long**)&otclpPtr);
  130.       result = otclpPtr->executeParentMethod(interp,methodName+1,
  131.                                              argv[0],argc - 1, & argv[1],
  132.                                              &found);
  133.    }
  134.    else
  135.    {
  136.       // Search the heirarchy for the method
  137.       result = otclop->executeMethod(interp,methodName,argc,argv,&found);
  138.    }
  139.  
  140.    if (found == OTCL_FALSE)
  141.    {
  142.       // Look for a method called "unknown" in the class
  143.  
  144.       // Build and argv structure that has the actual method name as the
  145.       // first argument and then try to execute the "unknoewn" method.
  146.       // Free the new argv structure afterwards
  147.  
  148.       int newArgc = argc+1;
  149.       char **newArgv = (char**)malloc(newArgc * sizeof(char*));
  150.       newArgv[0] = methodName;
  151.       for (int i = 0; i < argc; i++)
  152.       {
  153.          newArgv[i+1] = argv[i];
  154.       }
  155.       result = otclop->executeMethod(interp,UNKNOWN_METHOD_NAME,
  156.                                       newArgc,newArgv,&found);
  157.       free((char*)newArgv);
  158.  
  159.       if (found == OTCL_TRUE)
  160.       {
  161.          return result;
  162.       }
  163.  
  164.       Otcl::setTclResult(interp,INST_METHOD_NOT_FOUND_ERR,methodName,
  165.                          otclop->giveClassName());
  166.       return TCL_ERROR;
  167.    }
  168.    else
  169.    {
  170.       return result;
  171.    }
  172. }
  173.  
  174. OtclPart *OtclObject::giveOtclPart (void)
  175. {
  176.    return otclop;
  177. }
  178.  
  179. char *OtclObject::getSelf (void)
  180. {
  181.    return self;
  182. }
  183.  
  184. int OtclObject::discardingParts (void)
  185. {
  186.    return discarding;
  187. }
  188.  
  189. void *OtclObject::toCpp (char *cppClassName)
  190. {
  191.    return otclop->toCpp(cppClassName);
  192. }
  193.  
  194. OtclPart **OtclObject::getPartPtrPtr (void)
  195. {
  196.    return &otclop;
  197. }
  198.