0032308: Configuration - make Xlib dependency optional
[occt.git] / src / Draw / Draw_Interpretor.hxx
CommitLineData
dda67c1c 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
5//
6// This file is part of Open CASCADE Technology software library.
7//
d5f74e42 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
dda67c1c 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.
13//
14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
16
17#ifndef _Draw_Interpretor_HeaderFile
18#define _Draw_Interpretor_HeaderFile
19
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>
29
30class TCollection_AsciiString;
31class TCollection_ExtendedString;
32
33//! Provides an encapsulation of the TCL interpretor to define Draw commands.
34class Draw_Interpretor
35{
36
37public:
38
39 //! Global callback function definition
40 typedef Standard_Integer (*CommandFunction )(Draw_Interpretor& theDI,
41 Standard_Integer theArgNb,
42 const char** theArgVec);
43
44 //! Callback for TCL (interface)
45 struct CallBackData
46 {
47
48 //! Main constructor
49 CallBackData (Draw_Interpretor* theDI) : myDI (theDI) {}
50
51 //! Destructor
52 virtual ~CallBackData() {}
53
54 //! Invoke function
55 virtual Standard_Integer Invoke (Draw_Interpretor& theDI,
56 Standard_Integer theArgNb,
57 const char** theArgVec) = 0;
58
59 Draw_Interpretor* myDI; //!< pointer to Draw Interpretor
60
61 // make sure allocation and de-allocation is done by the same memory allocator
62 DEFINE_STANDARD_ALLOC
63
64 };
65
66protected:
67
68 //! Callback implementation for global function definition
69 struct CallBackDataFunc : public CallBackData
70 {
71
72 CallBackDataFunc (Draw_Interpretor* theDI,
73 CommandFunction theFunc)
74 : CallBackData (theDI),
75 myFunc (theFunc) {}
76
77 virtual Standard_Integer Invoke (Draw_Interpretor& theDI,
78 Standard_Integer theArgNb,
79 const char** theArgVec)
80 {
81 return myFunc != NULL ? myFunc (theDI, theArgNb, theArgVec) : 1;
82 }
83
84 Draw_Interpretor::CommandFunction myFunc;
85
86 };
87
88 //! Callback implementation for class's method definition
89 template<typename theObjHandle>
90 struct CallBackDataMethod : public CallBackData
91 {
92 typedef typename theObjHandle::element_type element_type;
93 typedef Standard_Integer (element_type::*methodType)(Draw_Interpretor& , Standard_Integer , const char** );
94
95 CallBackDataMethod (Draw_Interpretor* theDI,
96 const theObjHandle& theObjPtr,
97 methodType theMethod)
98 : CallBackData (theDI),
99 myObjPtr (theObjPtr),
100 myMethod (theMethod) {}
101
102 virtual Standard_Integer Invoke (Draw_Interpretor& theDI,
103 Standard_Integer theArgNb,
104 const char** theArgVec)
105 {
106 return myMethod != NULL && !myObjPtr.IsNull()
107 ? (myObjPtr.operator->()->*myMethod) (theDI, theArgNb, theArgVec)
108 : 1;
109 }
110
111 theObjHandle myObjPtr;
112 methodType myMethod;
113
114 };
115
116public:
117
118 //! Empty constructor
119 Standard_EXPORT Draw_Interpretor();
120
121 //! Initialize TCL interpretor
122 Standard_EXPORT void Init();
123
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")
130 {
131 Add (theCommandName, theHelp, "", theFunction, theGroup);
132 }
133
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")
142 {
143 CallBackDataFunc* aCallback = new CallBackDataFunc (this, theFunction);
144 add (theCommandName, theHelp, theFileName, aCallback, theGroup);
145 }
146
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)
158 {
159 Draw_Interpretor::CallBackDataMethod<theHandleType>* aCallback =
160 new Draw_Interpretor::CallBackDataMethod<theHandleType> (this, theObjPtr, theMethod);
161 add (theCommandName, theHelp, theFileName, aCallback, theGroup);
162 }
163
164 //! Removes <theCommandName>, returns true if success (the command existed).
165 Standard_EXPORT Standard_Boolean Remove (const Standard_CString theCommandName);
166
167public:
168
169 Standard_EXPORT Standard_CString Result() const;
170
171 //! Resets the result to empty string
172 Standard_EXPORT void Reset();
173
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); }
177
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); }
181
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); }
185
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); }
189
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); }
193
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); }
197
198 //! Appends to the result the string as a list element
199 Standard_EXPORT void AppendElement (const Standard_CString theResult);
200
201 //! Eval the script and returns OK = 0, ERROR = 1
202 Standard_EXPORT Standard_Integer Eval (const Standard_CString theScript);
203
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);
208
209 //! Eval the content on the file and returns status
210 Standard_EXPORT Standard_Integer EvalFile (const Standard_CString theFileName);
211
785a9540 212 //! Eval the script "help command_name"
213 Standard_EXPORT Standard_Integer PrintHelp (const Standard_CString theCommandName);
214
dda67c1c 215 //! Returns True if the script is complete, no pending closing braces. (})
216 Standard_EXPORT static Standard_Boolean Complete (const Standard_CString theScript);
217
218public:
219
220 //! Destructor
221 Standard_EXPORT ~Draw_Interpretor();
222
223 Standard_EXPORT Draw_Interpretor (const Draw_PInterp& theInterp);
224
225 Standard_EXPORT void Set (const Draw_PInterp& theInterp);
226
227 Standard_EXPORT Draw_PInterp Interp() const;
228
229 //! Enables or disables logging of all commands and their results
230 Standard_EXPORT void SetDoLog (const Standard_Boolean theDoLog);
231
232 //! Enables or disables eachoing of all commands and their results to cout
233 Standard_EXPORT void SetDoEcho (const Standard_Boolean theDoEcho);
234
235 //! Returns true if logging of commands is enabled
236 Standard_EXPORT Standard_Boolean GetDoLog() const;
237
238 //! Returns true if echoing of commands is enabled
239 Standard_EXPORT Standard_Boolean GetDoEcho() const;
240
e05c25c1 241 //! Resets log (if opened) to zero size
242 Standard_EXPORT void ResetLog();
243
244 //! Writes a text string to the log (if opened);
245 //! end of line is not appended
246 Standard_EXPORT void AddLog (const Standard_CString theStr);
247
248 //! Returns current content of the log file as a text string
249 Standard_EXPORT TCollection_AsciiString GetLog();
250
251 //! Returns current value of the log file descriptor
252 Standard_Integer GetLogFileDescriptor() { return myFDLog; }
dda67c1c 253
d99f0355 254 //! Return TRUE if console output should be colorized; TRUE by default.
255 Standard_Boolean ToColorize() const { return myToColorize; }
256
257 //! Set if console output should be colorized.
258 Standard_EXPORT void SetToColorize (Standard_Boolean theToColorize);
259
dda67c1c 260protected:
261
262 Standard_EXPORT void add (Standard_CString theCommandName,
263 Standard_CString theHelp,
264 Standard_CString theFileName,
265 CallBackData* theCallback,
266 Standard_CString theGroup);
267
268private:
269
dda67c1c 270 Draw_PInterp myInterp;
d99f0355 271 Standard_Boolean isAllocated;
dda67c1c 272 Standard_Boolean myDoLog;
273 Standard_Boolean myDoEcho;
d99f0355 274 Standard_Boolean myToColorize;
e05c25c1 275 Standard_Integer myFDLog; //!< file descriptor of log file
dda67c1c 276
277public:
278
279 DEFINE_STANDARD_ALLOC
280
281};
282
283#endif // _Draw_Interpretor_HeaderFile