PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
script.h
Go to the documentation of this file.
1 /*
2  * script.h
3  *
4  * Abstract class for interface to external script languages
5  *
6  * Portable Tools Library
7  *
8  * Copyright (C) 2010 by Post Increment
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Tools Library.
21  *
22  * The Initial Developer of the Original Code is Post Increment
23  *
24  * Contributor(s): Craig Southeren
25  * Robert Jongbloed
26  */
27 
28 #ifndef PTLIB_SCRIPT_H
29 #define PTLIB_SCRIPT_H
30 
31 #ifdef P_USE_PRAGMA
32 #pragma interface
33 #endif
34 
35 #include <ptlib/pfactory.h>
36 #include <ptclib/vartype.h>
37 
38 
40 
43 class PScriptLanguage : public PObject
44 {
46  public:
47  static PScriptLanguage * Create(const PString & language);
48  static PStringArray GetLanguages();
49 
55 
59 
62  virtual PString GetLanguageName() const = 0;
64 
66  virtual bool IsInitialised() const = 0;
67 
70  virtual bool LoadFile(
71  const PFilePath & filename
72  ) = 0;
73 
76  virtual bool LoadText(
77  const PString & text
78  ) = 0;
79 
82  virtual bool Load(
83  const PString & script
84  );
85 
92  virtual bool Run(
93  const char * script = NULL
94  ) = 0;
95 
101  virtual bool CreateComposite(
102  const PString & name
103  ) = 0;
104 
108  virtual bool GetVar(
109  const PString & name,
110  PVarType & var
111  ) = 0;
112 
116  virtual bool SetVar(
117  const PString & name,
118  const PVarType & var
119  ) = 0;
120 
124  virtual bool GetBoolean(
125  const PString & name
126  ) = 0;
127 
131  virtual bool SetBoolean(
132  const PString & name,
133  bool value
134  ) = 0;
135 
139  virtual int GetInteger(
140  const PString & name
141  ) = 0;
142 
146  virtual bool SetInteger(
147  const PString & name,
148  int value
149  ) = 0;
150 
154  virtual double GetNumber(
155  const PString & name
156  ) = 0;
157 
161  virtual bool SetNumber(
162  const PString & name,
163  double value
164  ) = 0;
165 
169  virtual PString GetString(
170  const PString & name
171  ) = 0;
172 
176  virtual bool SetString(
177  const PString & name,
178  const char * value
179  ) = 0;
180 
186  virtual bool ReleaseVariable(
187  const PString & name
188  ) = 0;
189 
192  struct ParamVector : public vector<PVarType>
193  {
194  explicit ParamVector(size_t sz = 0) : vector<PVarType>(sz) { }
195  void Push(void * state);
196  void Pop(void * state);
197  };
198 
200  struct Signature {
201  Signature() { }
204  };
205 
232  virtual bool Call(
233  const PString & name,
234  const char * sigString = NULL,
235  ...
236  ) = 0;
237  virtual bool Call(
238  const PString & name,
239  Signature & signature
240  ) = 0;
241 
243  #define PDECLARE_ScriptFunctionNotifier(cls, fn) PDECLARE_NOTIFIER2(PScriptLanguage, cls, fn, PScriptLanguage::Signature &)
244 
248  virtual bool SetFunction(
249  const PString & name,
250  const FunctionNotifier & func
251  ) = 0;
253 
256  __inline bool IsLoaded() const { return m_loaded; }
258 
260  virtual int GetLastErrorCode() const { return m_lastErrorCode; }
261 
263  virtual const PString & GetLastErrorText() const { return m_lastErrorText; }
265 
268  virtual void OnError(int code, const PString & str);
269 
270  protected:
271  virtual bool InternalSetFunction(const PString & name, const FunctionNotifier & func);
272  virtual void InternalRemoveFunction(const PString & prefix);
273 
274  bool m_loaded;
277  typedef map<PString, FunctionNotifier> FunctionMap;
279 
280  PDECLARE_MUTEX(m_mutex);
281 };
282 
283 
284 #if P_LUA
285  PFACTORY_LOAD(PLua);
286 #endif
287 #if P_V8
288  PFACTORY_LOAD(PJavaScript);
289 #endif
290 
291 
292 #endif // PTLIB_SCRIPT_H
293 
virtual const PString & GetLastErrorText() const
Get the last error text for an operation.
Definition: script.h:263
virtual bool LoadFile(const PFilePath &filename)=0
Load a script from a file.
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
virtual bool Load(const PString &script)
Load script from a file (if exists) or assume is the actual script.
Signature of Lua function and callback.
Definition: script.h:200
This class describes a full description for a file on the particular platform.
Definition: filepath.h:61
~PScriptLanguage()
Destroy the script context.
PDECLARE_MUTEX(m_mutex)
virtual double GetNumber(const PString &name)=0
Get a variable in the script as a number value.
virtual bool Call(const PString &name, const char *sigString=NULL,...)=0
Call a specific function in the script.
map< PString, FunctionNotifier > FunctionMap
Definition: script.h:277
virtual bool SetNumber(const PString &name, double value)=0
Set a variable in the script as a number value.
This is an array collection class of PString objects.
Definition: pstring.h:2365
ParamVector(size_t sz=0)
Definition: script.h:194
virtual bool ReleaseVariable(const PString &name)=0
Release a variable name.
FunctionMap m_functions
Definition: script.h:278
static PScriptLanguage * Create(const PString &language)
virtual bool InternalSetFunction(const PString &name, const FunctionNotifier &func)
virtual bool Run(const char *script=NULL)=0
Run the script.
virtual bool LoadText(const PString &text)=0
Load script text.
virtual bool SetFunction(const PString &name, const FunctionNotifier &func)=0
Set a notifier as a script callable function.
ParamVector m_results
Definition: script.h:203
ParamVector m_arguments
Definition: script.h:202
Individual Parameter in ParamVector.
Definition: script.h:192
The PNotifier and PNotifierFunction classes build a completely type safe mechanism for calling arbitr...
Definition: notifier.h:109
virtual void InternalRemoveFunction(const PString &prefix)
#define PFACTORY_LOAD(ConcreteType)
Definition: pfactory.h:557
virtual bool SetVar(const PString &name, const PVarType &var)=0
Set a variable in the script See class description for how name is parsed.
virtual bool CreateComposite(const PString &name)=0
Create a composite structure.
bool m_loaded
Definition: script.h:274
virtual bool IsInitialised() const =0
Indicate language has initialised successfully.
virtual void OnError(int code, const PString &str)
Set m_lastErrorCode and m_lastErrorText members, with mutex.
virtual bool SetString(const PString &name, const char *value)=0
Set a variable in the script as a string value.
static PStringArray GetLanguages()
virtual int GetLastErrorCode() const
Get the last error text for an operation.
Definition: script.h:260
The character string class.
Definition: pstring.h:108
virtual bool SetBoolean(const PString &name, bool value)=0
Set a variable in the script as a string value.
virtual bool SetInteger(const PString &name, int value)=0
Set a variable in the script as an integer value.
A wrapper around a scripting language instance.
Definition: script.h:43
virtual PString GetString(const PString &name)=0
Get a variable in the script as a string value.
virtual bool GetVar(const PString &name, PVarType &var)=0
Get a variable in the script See class description for how name is parsed.
PString m_lastErrorText
Definition: script.h:276
__inline bool IsLoaded() const
Rerturn true if script is successfully loaded.
Definition: script.h:257
Signature()
Definition: script.h:201
virtual PString GetLanguageName() const =0
Get the name of this scripting language.
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
int m_lastErrorCode
Definition: script.h:275
virtual bool GetBoolean(const PString &name)=0
Get a variable in the script as a string value.
PScriptLanguage()
Create a context in which to execute a script.
PNotifierTemplate< Signature & > FunctionNotifier
Definition: script.h:242
virtual int GetInteger(const PString &name)=0
Get a variable in the script as an integer value.