cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ireFramework.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Andrew LaMora
7  Created on: 2005-02-28
8 
9  (C) Copyright 2005-2007 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 
20 */
21 
22 
29 #ifndef _ireFramework_h
30 #define _ireFramework_h
31 
32 #include <stdexcept>
33 
34 #include <cisstCommon/cmnLogger.h>
35 #include <cisstCommon/cmnThrow.h>
36 
37 
39 
40 
69 
70 private:
71 
72  //static PyObject* pInstance;
73  // NewPythonThread is true if a new Python thread should be (or was) started for the IRE
74  static bool NewPythonThread;
75 
76  // IRE States:
77  // IRE_NOT_CONSTRUCTED: initial state (before Singleton object created)
78  // IRE_INITIALIZED: once constructed and InitShell called (Python interpreter initialized)
79  // IRE_LAUNCHED: once LaunchIREShell called
80  // IRE_ACTIVE: set by callback from Python IRE code just before
81  // entering main event loop
82  // IRE_FINISHED: set by callback from Python IRE code just after
83  // exiting main event loop. Also set by FinalizeShell.
84  //
85  // In general, the IRE is expected to transition in order through the above states.
86  // Currently, there is little error checking for invalid transitions. The Reset method
87  // can be used to transition from IRE_FINISHED to IRE_INITIALIZED.
88  enum IRE_STATES { IRE_NOT_CONSTRUCTED, IRE_INITIALIZED, IRE_LAUNCHED,
89  IRE_ACTIVE, IRE_FINISHED };
90 
91  // The current state of the IRE (see above enum)
92  static IRE_STATES IRE_State;
93 
94  static void *IreThreadState;
95 
96  void InitShellInstance(void);
97 
98  void FinalizeShellInstance(void);
99  void LaunchIREShellInstance(const char * startup, bool newPythonThread, bool useIPython,
100  bool useStreambuf);
101 
102  void JoinIREShellInstance(double timeout);
103 
104 protected:
109  InitShellInstance();
110  }
111 
115  FinalizeShellInstance();
116  }
117 public:
118 
122  static ireFramework* Instance(void);
123 
124 
133  static inline void InitShell(void) throw(std::runtime_error) {
134  Instance()->InitShellInstance();
135  }
136 
137 
143  static inline void FinalizeShell(void) throw(std::runtime_error) {
144  Instance()->FinalizeShellInstance();
145  }
146 
147 
184  static inline void LaunchIREShell(const char *startup = "", bool newPythonThread = false, bool useIPython = false,
185  bool useStreambuf = true) throw(std::runtime_error) {
186  Instance()->LaunchIREShellInstance(startup, newPythonThread, useIPython, useStreambuf);
187  }
188 
195  static inline void *RunIRE_wxPython(const char *startup) {
196  try {
197  LaunchIREShell(startup, false, false, true);
198  }
199  catch (...) {
200  CMN_LOG_INIT_ERROR << "Could not launch IRE shell (wxPython)" << std::endl;
201  }
202  FinalizeShell();
203  return 0;
204  }
205 
212  static inline void *RunIRE_IPython(const char *startup) {
213  try {
214  LaunchIREShell(startup, false, true, false);
215  }
216  catch (...) {
217  CMN_LOG_INIT_ERROR << "Could not launch IRE shell (IPython)" << std::endl;
218  }
219  FinalizeShell();
220  return 0;
221  }
222 
225  static inline void JoinIREShell(double timeout) {
226  Instance()->JoinIREShellInstance(timeout);
227  }
228 
232  static bool IsStarting();
233 
237  static bool IsActive();
238 
240  static bool IsFinished();
241 
244  static void SetActiveFlag(bool flag);
245 
248  static bool IsInitialized();
249 
252  static void Reset();
253 
255  static void UnblockThreads();
256 
258  static void BlockThreads();
259 
261  static void PrintLog(const char * str, int len);
262 };
263 
264 #endif // _ireFramework_h
265 
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
static void * RunIRE_wxPython(const char *startup)
Definition: ireFramework.h:195
~ireFramework()
Definition: ireFramework.h:114
static void InitShell(void)
Definition: ireFramework.h:133
#define CMN_LOG_INIT_ERROR
Definition: cmnLogger.h:162
static void * RunIRE_IPython(const char *startup)
Definition: ireFramework.h:212
ireFramework()
Definition: ireFramework.h:108
Declaration of cmnLogger amd macros for human readable logging.
static void JoinIREShell(double timeout)
Definition: ireFramework.h:225
Macros to export the symbols of cisstInteractive (in a Dll).
static void LaunchIREShell(const char *startup="", bool newPythonThread=false, bool useIPython=false, bool useStreambuf=true)
Definition: ireFramework.h:184
static void FinalizeShell(void)
Definition: ireFramework.h:143
Declaration of the template function cmnThrow.
Class to manage the preparation of Python for the Interactive Robot Environment, and for launching th...
Definition: ireFramework.h:68