tclcl-mappings.h
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:8k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * Copyright (c) @ Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *  This product includes software developed by the MASH Research
  16.  *  Group at the University of California Berkeley.
  17.  * 4. Neither the name of the University nor of the Research Group may be
  18.  *    used to endorse or promote products derived from this software without
  19.  *    specific prior written permission.
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  * @(#) $Header: /cvsroot/otcl-tclcl/tclcl/tclcl-mappings.h,v 1.15 2001/08/21 01:26:42 lloydlim Exp $
  34.  */
  35. #ifndef Tclcl_mappings_h
  36. #define Tclcl_mappings_h
  37. class TclObject;
  38. class Tcl;
  39. template <class T>
  40. class TclObjectHelper {
  41. protected:
  42. typedef int (T::*TMethod)(int argc,const char*const* argv);
  43. static int dispatch_(ClientData clientData, Tcl_Interp * /*interp*/,
  44.      int argc, char *argv[]) {
  45. Tcl& tcl = Tcl::instance();
  46. T *o = (T*) tcl.lookup(argv[0]);
  47. if (o!=NULL) {
  48. TMethod *pMethod = (TMethod*) clientData;
  49. return (o->*(*pMethod))(argc-2, argv+2);
  50. }
  51. tcl.resultf("Could not find TclObject for '%s'", argv[0]);
  52. return TCL_ERROR;
  53. }
  54. };
  55. class TclArguments {
  56. public:
  57. TclArguments(int argc, const char * const *argv)
  58. : current_(2), argc_(argc), argv_(argv) { }
  59. ~TclArguments() { }
  60. int arg(int &value);
  61. int arg(unsigned int &value);
  62. int arg(unsigned short &value);
  63. int arg(double &value);
  64. int arg(const char *&value);
  65. int arg(TclObject *&value);
  66. inline int more_args() const {
  67. return (current_ < argc_);
  68. }
  69. inline int current() const { return current_; }
  70. void add_error() const;
  71. private:
  72. int next(const char *&arg);
  73. int current_, argc_;
  74. const char * const *argv_;
  75. };
  76. /*-------------------------------------------------------------------------*/
  77. #define DECLARE_OTCLCLASS(classname, otclname)                              
  78.                                                                             
  79. static class classname ## _Class : public TclClass,                         
  80.    public TclObjectHelper<classname> {      
  81. public:                                                                     
  82. typedef TclObjectHelper<classname> MyHelper;                        
  83. typedef classname MyTclObject;                                      
  84.                                                                     
  85. classname ## _Class() : TclClass(otclname) { }                      
  86. TclObject *create(int argc, const char*const* argv);                
  87. void otcl_mappings();                                               
  88. } classname ## _class_obj
  89. /*-------------------------------------------------------------------------*/
  90. #define DEFINE_ABSTRACT_OTCL_CLASS(classname, otclname)                     
  91. DECLARE_OTCLCLASS(classname, otclname);                                     
  92.                                                                             
  93. inline TclObject *classname ## _Class::create(int /*argc*/,                 
  94.       const char*const* /*argv*/) { 
  95.         Tcl::instance().resultf("cannot create object of abstract class "   
  96. "'%s'", classname_);                        
  97. return NULL;                                                        
  98. }                                                                           
  99.                                                                             
  100. void classname ## _Class::otcl_mappings()
  101. /*-------------------------------------------------------------------------*/
  102. #define DEFINE_OTCL_CLASS(classname, otclname)                              
  103. DECLARE_OTCLCLASS(classname, otclname);                                     
  104.                                                                             
  105. inline TclObject *classname ## _Class::create(int /*argc*/,                 
  106.       const char*const* /*argv*/) { 
  107. return (new MyTclObject);                     
  108. }                                                                           
  109.                                                                             
  110. void classname ## _Class::otcl_mappings()
  111. /*-------------------------------------------------------------------------*/
  112. #define INSTPROC(c_method)                                                  
  113. {                                                                           
  114. static MyHelper::TMethod method = &MyTclObject::c_method;           
  115. OTclAddIMethod(class_, #c_method, MyHelper::dispatch_,              
  116.        (void*) &method, NULL);                              
  117. }
  118. #define INSTPROC_PUBLIC(c_method)  INSTPROC(c_method)
  119. #define INSTPROC_PRIVATE(c_method) INSTPROC(c_method)
  120. /*-------------------------------------------------------------------------*/
  121. #define PROC(c_static_method)                                               
  122. {                                                                           
  123. int (*method)(int,const char*const*) = &MyTclObject::c_static_method;
  124. OTclAddPMethod((OTclObject*)class_, #c_static_method,               
  125.        TclObject::dispatch_static_proc,                     
  126.        (void*) method, NULL);                               
  127. }
  128. #define PROC_PUBLIC(c_static_method)   PROC(c_static_method)
  129. #define PROC_PRIVATE(c_static_method)  PROC(c_static_method)
  130. /*-------------------------------------------------------------------------*/
  131. #define BEGIN_PARSE_ARGS(argc, argv)                                        
  132. TclArguments _args_(argc, argv)
  133. /*-------------------------------------------------------------------------*/
  134. #define END_PARSE_ARGS                                                      
  135. if (_args_.more_args()) {                                                   
  136. Tcl::instance().resultf("Extra arguments (starting at "             
  137. "argument %d)", _args_.current()-1);        
  138. _args_.add_error();                                                 
  139. return TCL_ERROR;                                                   
  140. }
  141. /*-------------------------------------------------------------------------*/
  142. #define ARG(var)                                                            
  143. do {                                                                        
  144. if (_args_.arg(var)==TCL_ERROR) {                                   
  145. return TCL_ERROR;                                           
  146. }                                                                   
  147. } while (0)
  148. /*-------------------------------------------------------------------------*/
  149. #define ARG_DEFAULT(var, default)                                           
  150. do {                                                                        
  151.      if (!_args_.more_args()) {                                          
  152.                 var = default;                                              
  153. }                                                                   
  154. else if (_args_.arg(var)==TCL_ERROR) {                              
  155. return TCL_ERROR;                                           
  156. }                                                                   
  157. } while (0)
  158. /*-------------------------------------------------------------------------*/
  159. #endif // Tclcl_mappings_h