1 // Created on: 1995-02-23
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #ifndef _Draw_Interpretor_HeaderFile
18 #define _Draw_Interpretor_HeaderFile
20 #include <Standard.hxx>
21 #include <Standard_DefineAlloc.hxx>
22 #include <Standard_Macro.hxx>
23 #include <Standard_Boolean.hxx>
24 #include <Draw_PInterp.hxx>
25 #include <Standard_SStream.hxx>
26 #include <Standard_CString.hxx>
27 #include <Standard_Integer.hxx>
28 #include <Standard_Real.hxx>
30 class TCollection_AsciiString;
31 class TCollection_ExtendedString;
33 //! Provides an encapsulation of the TCL interpretor to define Draw commands.
34 class Draw_Interpretor
39 //! Global callback function definition
40 typedef Standard_Integer (*CommandFunction )(Draw_Interpretor& theDI,
41 Standard_Integer theArgNb,
42 const char** theArgVec);
44 //! Callback for TCL (interface)
49 CallBackData (Draw_Interpretor* theDI) : myDI (theDI) {}
52 virtual ~CallBackData() {}
55 virtual Standard_Integer Invoke (Draw_Interpretor& theDI,
56 Standard_Integer theArgNb,
57 const char** theArgVec) = 0;
59 Draw_Interpretor* myDI; //!< pointer to Draw Interpretor
61 // make sure allocation and de-allocation is done by the same memory allocator
68 //! Callback implementation for global function definition
69 struct CallBackDataFunc : public CallBackData
72 CallBackDataFunc (Draw_Interpretor* theDI,
73 CommandFunction theFunc)
74 : CallBackData (theDI),
77 virtual Standard_Integer Invoke (Draw_Interpretor& theDI,
78 Standard_Integer theArgNb,
79 const char** theArgVec)
81 return myFunc != NULL ? myFunc (theDI, theArgNb, theArgVec) : 1;
84 Draw_Interpretor::CommandFunction myFunc;
88 //! Callback implementation for class's method definition
89 template<typename theObjHandle>
90 struct CallBackDataMethod : public CallBackData
92 typedef typename theObjHandle::element_type element_type;
93 typedef Standard_Integer (element_type::*methodType)(Draw_Interpretor& , Standard_Integer , const char** );
95 CallBackDataMethod (Draw_Interpretor* theDI,
96 const theObjHandle& theObjPtr,
98 : CallBackData (theDI),
100 myMethod (theMethod) {}
102 virtual Standard_Integer Invoke (Draw_Interpretor& theDI,
103 Standard_Integer theArgNb,
104 const char** theArgVec)
106 return myMethod != NULL && !myObjPtr.IsNull()
107 ? (myObjPtr.operator->()->*myMethod) (theDI, theArgNb, theArgVec)
111 theObjHandle myObjPtr;
118 //! Empty constructor
119 Standard_EXPORT Draw_Interpretor();
121 //! Initialize TCL interpretor
122 Standard_EXPORT void Init();
124 //! Creates a new command with name <theCommandName>, help string <theHelp> in group <theGroup>.
125 //! @param theFunction callback implementation
126 inline void Add (Standard_CString theCommandName,
127 Standard_CString theHelp,
128 CommandFunction theFunction,
129 Standard_CString theGroup = "User Commands")
131 Add (theCommandName, theHelp, "", theFunction, theGroup);
134 //! Creates a new command with name <theCommandName>, help string <theHelp> in group <theGroup>.
135 //! @theFunction callback implementation
136 //! @theFileName the name of the file that contains the implementation of the command
137 inline void Add (Standard_CString theCommandName,
138 Standard_CString theHelp,
139 Standard_CString theFileName,
140 CommandFunction theFunction,
141 Standard_CString theGroup = "User Commands")
143 CallBackDataFunc* aCallback = new CallBackDataFunc (this, theFunction);
144 add (theCommandName, theHelp, theFileName, aCallback, theGroup);
147 //! Creates a new command with name <theCommandName>, help string <theHelp> in group <theGroup>.
148 //! @param theObjPtr callback class instance
149 //! @param theMethod callback implementation
150 //! @param theFileName the name of the file that contains the implementation of the command
151 template<typename theHandleType>
152 inline void Add (Standard_CString theCommandName,
153 Standard_CString theHelp,
154 Standard_CString theFileName,
155 const theHandleType& theObjPtr,
156 typename Draw_Interpretor::CallBackDataMethod<theHandleType>::methodType theMethod,
157 Standard_CString theGroup)
159 Draw_Interpretor::CallBackDataMethod<theHandleType>* aCallback =
160 new Draw_Interpretor::CallBackDataMethod<theHandleType> (this, theObjPtr, theMethod);
161 add (theCommandName, theHelp, theFileName, aCallback, theGroup);
164 //! Removes <theCommandName>, returns true if success (the command existed).
165 Standard_EXPORT Standard_Boolean Remove (const Standard_CString theCommandName);
169 Standard_EXPORT Standard_CString Result() const;
171 //! Resets the result to empty string
172 Standard_EXPORT void Reset();
174 //! Appends to the result
175 Standard_EXPORT Draw_Interpretor& Append (const Standard_CString theResult);
176 inline Draw_Interpretor& operator<< (const Standard_CString theResult) { return Append (theResult); }
178 //! Appends to the result
179 Standard_EXPORT Draw_Interpretor& Append (const TCollection_AsciiString& theResult);
180 inline Draw_Interpretor& operator<< (const TCollection_AsciiString& theResult) { return Append (theResult); }
182 //! Appends to the result
183 Standard_EXPORT Draw_Interpretor& Append (const TCollection_ExtendedString& theResult);
184 inline Draw_Interpretor& operator<< (const TCollection_ExtendedString& theResult) { return Append (theResult); }
186 //! Appends to the result
187 Standard_EXPORT Draw_Interpretor& Append (const Standard_Integer theResult);
188 inline Draw_Interpretor& operator<< (const Standard_Integer theResult) { return Append (theResult); }
190 //! Appends to the result
191 Standard_EXPORT Draw_Interpretor& Append (const Standard_Real theResult);
192 inline Draw_Interpretor& operator<< (const Standard_Real theResult) { return Append (theResult); }
194 //! Appends to the result
195 Standard_EXPORT Draw_Interpretor& Append (const Standard_SStream& theResult);
196 inline Draw_Interpretor& operator<< (const Standard_SStream& theResult) { return Append (theResult); }
198 //! Appends to the result the string as a list element
199 Standard_EXPORT void AppendElement (const Standard_CString theResult);
201 //! Eval the script and returns OK = 0, ERROR = 1
202 Standard_EXPORT Standard_Integer Eval (const Standard_CString theScript);
204 //! Eval the script and returns OK = 0, ERROR = 1
205 //! Store the script in the history record.
206 Standard_EXPORT Standard_Integer RecordAndEval (const Standard_CString theScript,
207 const Standard_Integer theFlags = 0);
209 //! Eval the content on the file and returns status
210 Standard_EXPORT Standard_Integer EvalFile (const Standard_CString theFileName);
212 //! Eval the script "help command_name"
213 Standard_EXPORT Standard_Integer PrintHelp (const Standard_CString theCommandName);
215 //! Returns True if the script is complete, no pending closing braces. (})
216 Standard_EXPORT static Standard_Boolean Complete (const Standard_CString theScript);
221 Standard_EXPORT ~Draw_Interpretor();
223 Standard_EXPORT Draw_Interpretor (const Draw_PInterp& theInterp);
225 Standard_EXPORT void Set (const Draw_PInterp& theInterp);
227 Standard_EXPORT Draw_PInterp Interp() const;
229 //! Enables or disables logging of all commands and their results
230 Standard_EXPORT void SetDoLog (const Standard_Boolean theDoLog);
232 //! Enables or disables eachoing of all commands and their results to cout
233 Standard_EXPORT void SetDoEcho (const Standard_Boolean theDoEcho);
235 //! Returns true if logging of commands is enabled
236 Standard_EXPORT Standard_Boolean GetDoLog() const;
238 //! Returns true if echoing of commands is enabled
239 Standard_EXPORT Standard_Boolean GetDoEcho() const;
241 //! Returns log stream
242 Standard_EXPORT Standard_SStream& Log();
246 Standard_EXPORT void add (Standard_CString theCommandName,
247 Standard_CString theHelp,
248 Standard_CString theFileName,
249 CallBackData* theCallback,
250 Standard_CString theGroup);
254 Standard_Boolean isAllocated;
255 Draw_PInterp myInterp;
256 Standard_Boolean myDoLog;
257 Standard_Boolean myDoEcho;
258 Standard_SStream myLog;
262 DEFINE_STANDARD_ALLOC
266 #endif // _Draw_Interpretor_HeaderFile