FRIBParallelanalysis  1.0
FrameworkforMPIParalleldataanalysisatFRIB
TCLInterpreter.h
1 /*
2  This software is Copyright by the Board of Trustees of Michigan
3  State University (c) Copyright 2005.
4 
5  You may use this software under the terms of the GNU public license
6  (GPL). The terms of this license are described at:
7 
8  http://www.gnu.org/licenses/gpl.txt
9 
10  Author:
11  Ron Fox
12  NSCL
13  Michigan State University
14  East Lansing, MI 48824-1321
15 */
16 
17 // CTCLInterpreter.h:
18 //
19 // This file defines the CTCLInterpreter class.
20 // Most of the manipulator functions follow quite closely the calling
21 // sequences of the TCL functions as described in
22 // "TCL and the TK Toolkit" by Ousterhout... .with the interpreter
23 // parameter taken from the object's attribute. Refer to that, or another
24 // TCL book for documentation on the purpose and parameters of each function
25 //
26 //
27 // Author:
28 // Ron Fox
29 // NSCL
30 // Michigan State University
31 // East Lansing, MI 48824-1321
32 // mailto:fox@nscl.msu.edu
33 //
34 // Copyright 1999 NSCL, All Rights Reserved.
35 //
37 
38 
39 
40 #ifndef TCLInterpreter_H //Reuired for current class
41 #define TCLInterpreter_H
42 
43 #ifndef TCLSTRING_H
44 #include "TCLString.h"
45 #endif
46 
47 #ifndef TCLPLUSTYPES_H
48 #include <libtclplusdatatypes.h>
49 #endif
50 
51 
52 #include <tcl.h>
53 #include <string>
54 #include <vector>
55 
56 class CTCLObject;
57 
58 
60 {
61  Tcl_Interp* m_pInterpreter; // // Pointer to the interpreter.
62 
63 public:
64  //Default constructor - Create new interpreter.
65 
66  CTCLInterpreter () :
67  m_pInterpreter(Tcl_CreateInterp()) { }
68  virtual ~CTCLInterpreter ( ) { // Destructor.
69  Tcl_DeleteInterp(m_pInterpreter);
70  }
71 
72  //Constructor with arguments - Bind to existing interp.
73  CTCLInterpreter ( Tcl_Interp* am_pInterpreter ) :
74  m_pInterpreter (am_pInterpreter) { }
75 
76  //Copy constructor -- not allowed.
77 private:
78  CTCLInterpreter (const CTCLInterpreter& aCTCLInterpreter ) ;
79 public:
80  //Operator= Assignment Operator -- not allowed
81 private:
82  CTCLInterpreter operator= (const CTCLInterpreter& aCTCLInterpreter);
83 public:
84 
85  //Operator== Allowed but not so useful.
86  //Update to access 1:M associated class attributes
87  int operator== (const CTCLInterpreter& aCTCLInterpreter)
88  {
89  return (
90  (m_pInterpreter == aCTCLInterpreter.m_pInterpreter)
91  );
92  }
93  // Selectors -- note that to allow these objects to interact with other
94  // objects and C code, the read selector for m_pInterpreter
95  // doesn't return a const pointer.
96 public:
97  Tcl_Interp* getInterpreter()
98  {
99  return m_pInterpreter;
100  }
101  // Mutator - Allowed only be derived classes:
102 protected:
103  void setInterpreter (Tcl_Interp* am_pInterpreter)
104  {
105  m_pInterpreter = am_pInterpreter;
106  }
107  // The remainder of the operations are actual TCL interfaces through the
108  // interpreter encpsulated by this class.
109 
110 public:
111  // Evaluate script from a string:
112 
113  std::string Eval (const char* pScript) ;
114  std::string Eval(const CTCLString& rScript) {
115  return Eval((const char*)rScript);
116  }
117  std::string Eval(const std::string& rScript) {
118  return Eval(rScript.c_str());
119  }
120  //
121  // Evaluate a scripts stored in a file.
122  //
123  std::string EvalFile (const char* pFilename) ;
124  std::string EvalFile(const CTCLString& rFilename) {
125  return EvalFile((const char*)(rFilename));
126  }
127  std::string EvalFile(const std::string& rFilename) {
128  return EvalFile(rFilename.c_str());
129  }
130  // Evaluate a script at the global level (stack empty context).
131  //
132  std::string GlobalEval (const char* pScript) ;
133  std::string GlobalEval (const CTCLString& rScript) {
134  return GlobalEval((const char*)rScript);
135  }
136  std::string GlobalEval(const std::string& rScript) {
137  return GlobalEval(rScript.c_str());
138  }
139  // Evaluate a script and store it it's commands in the
140  // history list for later retrieval
141  //
142  std::string RecordAndEval (const char* pScript, TCLPLUS::Bool_t fEval=TCLPLUS::kfFALSE);
143  std::string RecordAndEval(const CTCLString& rScript,
144  TCLPLUS::Bool_t fEval=TCLPLUS::kfFALSE) {
145  return RecordAndEval((const char*)(rScript), fEval);
146  }
147  std::string RecordAndEval(const std::string& rScript,
148  TCLPLUS::Bool_t fEval = TCLPLUS::kfFALSE) {
149  return RecordAndEval(rScript.c_str(), fEval);
150  }
151  //
152  // Evaluate and expression with a string result:
153  //
154  std::string ExprString (const char* pExpression) ;
155  std::string ExprString(const CTCLString& rExpr) {
156  return ExprString((const char*)(rExpr));
157  }
158  std::string ExprString(const std::string& rExpr) {
159  return ExprString(rExpr.c_str());
160  }
161  // Evaluate string
162  //
163  TCLPLUS::Long_t ExprLong (const char* pExpression) ;
164  TCLPLUS::Long_t ExprLong (std::string& rExpression) {
165  return ExprLong(rExpression.c_str());
166  }
167  TCLPLUS::Long_t ExprLong (const CTCLString& rExpr) {
168  return ExprLong((const char*)(rExpr));
169  }
170  // Evaluate a double:
171  //
172  TCLPLUS::DFloat_t ExprDouble (const char* pExpression) ;
173  TCLPLUS::DFloat_t ExprDouble (const CTCLString& rExpression) {
174  return ExprDouble ((const char*)(rExpression));
175  }
176  TCLPLUS::DFloat_t ExprDouble(const std::string& rExpression) {
177  return ExprDouble(rExpression.c_str());
178  }
179  // Evaluate a boolean:
180  //
181  TCLPLUS::Bool_t ExprBoolean (const char* pExpression) ;
182  TCLPLUS::Bool_t ExprBoolean (const CTCLString& rExpression) {
183  return ExprBoolean((const char*)(rExpression));
184  }
185  TCLPLUS::Bool_t ExprBoolean(const std::string& rExpression) {
186  return ExprBoolean(rExpression.c_str());
187  }
188  // Substitute for tilde in some filename.
189  //
190  std::string TildeSubst (const char* pFilename) const ;
191  std::string TildeSubst (const CTCLString& rName) const {
192  return TildeSubst((const char*)(rName));
193  }
194  std::string TildeSubst (const std::string& rName) const {
195  return TildeSubst(rName.c_str());
196  }
197 
198  std::string PosixError () const ;
199  static std::string ErrnoId () ;
200  static std::string SignalId (TCLPLUS::UInt_t nSignal) ;
201  static std::string SignalMsg (TCLPLUS::UInt_t nSignal) ;
202  void DetachProcess (const std::vector<TCLPLUS::UInt_t>& rPids) const ;
203  void DetachProcess(TCLPLUS::UInt_t nPids, TCLPLUS::Int_t* pPids) const {
204  Tcl_DetachPids(nPids, (Tcl_Pid*)pPids);
205  }
206  void ReapDetachedProcesses () const {
207  Tcl_ReapDetachedProcs();
208  }
209  void AddCommand (const char* pCommandName,
210  Tcl_CmdProc* pCommandProcessor,
211  ClientData pData,
212  Tcl_CmdDeleteProc* pDeleteProcessor=
213  reinterpret_cast<Tcl_CmdDeleteProc*>(reinterpret_cast<size_t>(TCLPLUS::kpNULL))) const ;
214  void AddCommand(const std::string& rCommandName,
215  Tcl_CmdProc* pCommandProcessor,
216  ClientData pData,
217  Tcl_CmdDeleteProc* pDeleteProcessor =
218  reinterpret_cast<Tcl_CmdDeleteProc*>(reinterpret_cast<size_t>(TCLPLUS::kpNULL))) const {
219  AddCommand(rCommandName.c_str(), pCommandProcessor, pData,
220  pDeleteProcessor);
221  }
222  void AddCommand(const CTCLString& rCommandName,
223  Tcl_CmdProc* pCommandProcessor,
224  ClientData pData,
225  Tcl_CmdDeleteProc* pDeleteProcessor =
226  reinterpret_cast<Tcl_CmdDeleteProc*>(reinterpret_cast<size_t>(TCLPLUS::kpNULL))) const {
227  AddCommand((const char*)(rCommandName), pCommandProcessor,
228  pData, pDeleteProcessor);
229  }
230  void UnregisterCommand (const char* pCommandName) const ;
231  void UnregisterCommand (const std::string& rCommandName) const {
232  UnregisterCommand(rCommandName.c_str());
233  }
234  void UnregisterCommand (const CTCLString& rCommandName) const {
235  UnregisterCommand((const char*)(rCommandName));
236  }
237  std::string GetResultString () const ;
238 
239  // Access to Tcl Channels inquiry:
240  //
241  Tcl_Channel GetChannel(
242  const std::string& rName,
243  TCLPLUS::Int_t* pMode=(TCLPLUS::Int_t*)TCLPLUS::kpNULL
244  );
245  std::vector<std::string> GetChannelNames(const std::string& rPattern);
246 
247 
248  //
249  // These two support access to the interpreter:
250  //
251  Tcl_Interp* operator-> () // Access interp->struct elements.
252  { return m_pInterpreter; }
253  //
254  // Supports casts to Tcl_Interp*
255  //
256  operator Tcl_Interp* () {
257  return m_pInterpreter;
258  }
259  // Modern support to set the interpreter result:
260 
261  void setResult(const char* resultString);
262  void setResult(std::string resultString);
263  void setResult(Tcl_Obj* resultObj);
264  void setResult(CTCLObject& resultObj);
265 };
266 
267 
268 
269 #endif
Tcl_Channel GetChannel(const std::string &rName, TCLPLUS::Int_t *pMode=(TCLPLUS::Int_t *) TCLPLUS::kpNULL)
Definition: TCLInterpreter.cpp:599
Definition: TCLString.h:28
Definition: TCLInterpreter.h:59
std::vector< std::string > GetChannelNames(const std::string &rPattern)
Definition: TCLInterpreter.cpp:617
void setResult(const char *resultString)
Definition: TCLInterpreter.cpp:647
Definition: TCLObject.h:50