Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:20:11

0001 // Created on: 1995-02-23
0002 // Created by: Remi LEQUETTE
0003 // Copyright (c) 1995-1999 Matra Datavision
0004 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0005 //
0006 // This file is part of Open CASCADE Technology software library.
0007 //
0008 // This library is free software; you can redistribute it and/or modify it under
0009 // the terms of the GNU Lesser General Public License version 2.1 as published
0010 // by the Free Software Foundation, with special exception defined in the file
0011 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0012 // distribution for complete text of the license and disclaimer of any warranty.
0013 //
0014 // Alternatively, this file may be used under the terms of Open CASCADE
0015 // commercial license or contractual agreement.
0016 
0017 #ifndef _Draw_Interpretor_HeaderFile
0018 #define _Draw_Interpretor_HeaderFile
0019 
0020 #include <Standard.hxx>
0021 #include <Standard_DefineAlloc.hxx>
0022 #include <Standard_Macro.hxx>
0023 #include <Standard_Boolean.hxx>
0024 #include <Draw_PInterp.hxx>
0025 #include <Standard_SStream.hxx>
0026 #include <Standard_Real.hxx>
0027 
0028 class TCollection_AsciiString;
0029 class TCollection_ExtendedString;
0030 
0031 //! Provides an encapsulation of the TCL interpretor to define Draw commands.
0032 class Draw_Interpretor
0033 {
0034 
0035 public:
0036 
0037   //! Global callback function definition
0038   typedef Standard_Integer (*CommandFunction )(Draw_Interpretor& theDI,
0039                                                Standard_Integer  theArgNb,
0040                                                const char**      theArgVec);
0041 
0042   //! Callback for TCL (interface)
0043   struct CallBackData
0044   {
0045 
0046     //! Main constructor
0047     CallBackData (Draw_Interpretor* theDI) : myDI (theDI) {}
0048 
0049     //! Destructor
0050     virtual ~CallBackData() {}
0051 
0052     //! Invoke function
0053     virtual Standard_Integer Invoke (Draw_Interpretor& theDI,
0054                                      Standard_Integer  theArgNb,
0055                                      const char**      theArgVec) = 0;
0056 
0057     Draw_Interpretor* myDI; //!< pointer to Draw Interpretor
0058 
0059     // make sure allocation and de-allocation is done by the same memory allocator
0060     DEFINE_STANDARD_ALLOC
0061 
0062   };
0063 
0064 protected:
0065 
0066   //! Callback implementation for global function definition
0067   struct CallBackDataFunc : public CallBackData
0068   {
0069 
0070     CallBackDataFunc (Draw_Interpretor* theDI,
0071                       CommandFunction   theFunc)
0072     : CallBackData (theDI),
0073       myFunc (theFunc) {}
0074 
0075     virtual Standard_Integer Invoke (Draw_Interpretor& theDI,
0076                                      Standard_Integer  theArgNb,
0077                                      const char**      theArgVec)
0078     {
0079       return myFunc != NULL ? myFunc (theDI, theArgNb, theArgVec) : 1;
0080     }
0081 
0082     Draw_Interpretor::CommandFunction myFunc;
0083 
0084   };
0085 
0086   //! Callback implementation for class's method definition
0087   template<typename theObjHandle>
0088   struct CallBackDataMethod : public CallBackData
0089   {
0090     typedef typename theObjHandle::element_type     element_type;
0091     typedef Standard_Integer (element_type::*methodType)(Draw_Interpretor& , Standard_Integer , const char** );
0092 
0093     CallBackDataMethod (Draw_Interpretor*   theDI,
0094                         const theObjHandle& theObjPtr,
0095                         methodType          theMethod)
0096     : CallBackData (theDI),
0097       myObjPtr (theObjPtr),
0098       myMethod (theMethod) {}
0099 
0100     virtual Standard_Integer Invoke (Draw_Interpretor& theDI,
0101                                      Standard_Integer  theArgNb,
0102                                      const char**      theArgVec)
0103     {
0104       return myMethod != NULL && !myObjPtr.IsNull()
0105            ? (myObjPtr.operator->()->*myMethod) (theDI, theArgNb, theArgVec)
0106            : 1;
0107     }
0108 
0109     theObjHandle myObjPtr;
0110     methodType   myMethod;
0111 
0112   };
0113 
0114 public:
0115 
0116   //! Empty constructor
0117   Standard_EXPORT Draw_Interpretor();
0118 
0119   //! Initialize TCL interpretor
0120   Standard_EXPORT void Init();
0121 
0122   //! Creates a new command with name <theCommandName>, help string <theHelp> in group <theGroup>.
0123   //! @param theFunction callback implementation
0124   inline void Add (Standard_CString theCommandName,
0125                    Standard_CString theHelp,
0126                    CommandFunction  theFunction,
0127                    Standard_CString theGroup = "User Commands")
0128   {
0129     Add (theCommandName, theHelp, "", theFunction, theGroup);
0130   }
0131 
0132   //! Creates a new command with name <theCommandName>, help string <theHelp> in group <theGroup>.
0133   //! @theFunction callback implementation
0134   //! @theFileName the name of the file that contains the implementation of the command
0135   inline void Add (Standard_CString theCommandName,
0136                    Standard_CString theHelp,
0137                    Standard_CString theFileName,
0138                    CommandFunction  theFunction,
0139                    Standard_CString theGroup = "User Commands")
0140   {
0141     CallBackDataFunc* aCallback = new CallBackDataFunc (this, theFunction);
0142     add (theCommandName, theHelp, theFileName, aCallback, theGroup);
0143   }
0144 
0145   //! Creates a new command with name <theCommandName>, help string <theHelp> in group <theGroup>.
0146   //! @param theObjPtr   callback class instance
0147   //! @param theMethod   callback implementation
0148   //! @param theFileName the name of the file that contains the implementation of the command
0149   template<typename theHandleType>
0150   inline void Add (Standard_CString     theCommandName,
0151                    Standard_CString     theHelp,
0152                    Standard_CString     theFileName,
0153                    const theHandleType& theObjPtr,
0154                    typename Draw_Interpretor::CallBackDataMethod<theHandleType>::methodType theMethod,
0155                    Standard_CString     theGroup)
0156   {
0157     Draw_Interpretor::CallBackDataMethod<theHandleType>* aCallback =
0158       new Draw_Interpretor::CallBackDataMethod<theHandleType> (this, theObjPtr, theMethod);
0159     add (theCommandName, theHelp, theFileName, aCallback, theGroup);
0160   }
0161 
0162   //! Removes <theCommandName>, returns true if success (the command existed).
0163   Standard_EXPORT Standard_Boolean Remove (const Standard_CString theCommandName);
0164 
0165 public:
0166   
0167   Standard_EXPORT Standard_CString Result() const;
0168 
0169   //! Resets the result to empty string
0170   Standard_EXPORT void Reset();
0171 
0172   //! Appends to the result
0173   Standard_EXPORT Draw_Interpretor& Append (const Standard_CString theResult);
0174   inline Draw_Interpretor& operator<< (const Standard_CString theResult) { return Append (theResult); }
0175 
0176   //! Appends to the result
0177   Standard_EXPORT Draw_Interpretor& Append (const TCollection_AsciiString& theResult);
0178   inline Draw_Interpretor& operator<< (const TCollection_AsciiString& theResult) { return Append (theResult); }
0179 
0180   //! Appends to the result
0181   Standard_EXPORT Draw_Interpretor& Append (const TCollection_ExtendedString& theResult);
0182   inline Draw_Interpretor& operator<< (const TCollection_ExtendedString& theResult) { return Append (theResult); }
0183 
0184   //! Appends to the result
0185   Standard_EXPORT Draw_Interpretor& Append (const Standard_Integer theResult);
0186   inline Draw_Interpretor& operator<< (const Standard_Integer theResult) { return Append (theResult); }
0187 
0188   //! Appends to the result
0189   Standard_EXPORT Draw_Interpretor& Append (const Standard_Real theResult);
0190   inline Draw_Interpretor& operator<< (const Standard_Real theResult) { return Append (theResult); }
0191 
0192   //! Appends to the result
0193   Standard_EXPORT Draw_Interpretor& Append (const Standard_SStream& theResult);
0194   inline Draw_Interpretor& operator<< (const Standard_SStream& theResult) { return Append (theResult); }
0195 
0196   //! Appends to the result the string as a list element
0197   Standard_EXPORT void AppendElement (const Standard_CString theResult);
0198 
0199   //! Eval the script and returns OK = 0, ERROR = 1
0200   Standard_EXPORT Standard_Integer Eval (const Standard_CString theScript);
0201 
0202   //! Eval the script and returns OK = 0, ERROR = 1
0203   //! Store the script in the history record.
0204   Standard_EXPORT Standard_Integer RecordAndEval (const Standard_CString theScript,
0205                                                   const Standard_Integer theFlags = 0);
0206 
0207   //! Eval the content on the file and returns status
0208   Standard_EXPORT Standard_Integer EvalFile (const Standard_CString theFileName);
0209 
0210   //! Eval the script "help command_name"
0211   Standard_EXPORT Standard_Integer PrintHelp (const Standard_CString theCommandName);
0212 
0213   //! Returns True if the script is complete, no pending closing braces. (})
0214   Standard_EXPORT static Standard_Boolean Complete (const Standard_CString theScript);
0215 
0216 public:
0217   
0218   //! Destructor
0219   Standard_EXPORT ~Draw_Interpretor();
0220 
0221   Standard_EXPORT Draw_Interpretor (const Draw_PInterp& theInterp);
0222 
0223   Standard_EXPORT void Set (const Draw_PInterp& theInterp);
0224 
0225   Standard_EXPORT Draw_PInterp Interp() const;
0226 
0227   //! Enables or disables logging of all commands and their results
0228   Standard_EXPORT void SetDoLog (const Standard_Boolean theDoLog);
0229 
0230   //! Enables or disables eachoing of all commands and their results to cout
0231   Standard_EXPORT void SetDoEcho (const Standard_Boolean theDoEcho);
0232 
0233   //! Returns true if logging of commands is enabled
0234   Standard_EXPORT Standard_Boolean GetDoLog() const;
0235 
0236   //! Returns true if echoing of commands is enabled
0237   Standard_EXPORT Standard_Boolean GetDoEcho() const;
0238 
0239   //! Resets log (if opened) to zero size
0240   Standard_EXPORT void ResetLog();
0241 
0242   //! Writes a text string to the log (if opened);
0243   //! end of line is not appended
0244   Standard_EXPORT void AddLog (const Standard_CString theStr);
0245 
0246   //! Returns current content of the log file as a text string
0247   Standard_EXPORT TCollection_AsciiString GetLog();
0248 
0249   //! Returns current value of the log file descriptor
0250   Standard_Integer GetLogFileDescriptor() { return myFDLog; }
0251 
0252   //! Return TRUE if console output should be colorized; TRUE by default.
0253   Standard_Boolean ToColorize() const { return myToColorize; }
0254 
0255   //! Set if console output should be colorized.
0256   Standard_EXPORT void SetToColorize (Standard_Boolean theToColorize);
0257 
0258 protected:
0259 
0260   Standard_EXPORT void add (Standard_CString theCommandName,
0261                             Standard_CString theHelp,
0262                             Standard_CString theFileName,
0263                             CallBackData*    theCallback,
0264                             Standard_CString theGroup);
0265 
0266 private:
0267 
0268   Draw_PInterp     myInterp;
0269   Standard_Boolean isAllocated;
0270   Standard_Boolean myDoLog;
0271   Standard_Boolean myDoEcho;
0272   Standard_Boolean myToColorize;
0273   Standard_Integer myFDLog;          //!< file descriptor of log file 
0274 
0275 public:
0276 
0277   DEFINE_STANDARD_ALLOC
0278 
0279 };
0280 
0281 #endif // _Draw_Interpretor_HeaderFile