Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Script.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "Script.h"
14 
15 
16 // >>>>>> @Date:2013/07/24 <Modify> A.Ozaki
17 //
18 // Moved the ScriptClass to DataObjects library from CoreToolkits library.
19 //
20 using namespace kome::objects;
21 
22 
23 #include <crtdbg.h>
24 #ifdef _DEBUG
25  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
26  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
27 #endif // _DEBUG
28 
29 //
30 // <<<<<< @Date:2013/07/24 <Modify> A.Ozaki
31 
32 
33 
34 // static memeber
35 std::string Script::m_filePath;
36 
37 
38 // constructor
41  mgr.addScript( this );
42 
43 // >>>>>> @Date:2013/07/29 <Add> A.Ozaki
44  m_script = "";
46 // <<<<<< @Date:2013/07/29 <Add> A.Ozaki
47 
48 }
49 
50 // destructor
53  mgr.removeScript( this );
54 }
55 
56 // set the script
57 void Script::setScript( const char* script ) {
58  m_script = NVL( script, "" );
59 }
60 
61 // get the script
62 const char* Script::getScript() {
63  return m_script.c_str();
64 }
65 
66 // get template
67 std::string Script::getTemplate() {
68  std::string t = onGetTemplate();
69 
70  return t;
71 }
72 
73 // >>>>>> @Date:2013/07/29 <Add> A.Ozaki
74 // set variant
76 {
77  m_refVariant = var;
78 }
79 
80 // get variant
82 {
83  return m_refVariant;
84 }
85 
86 // set parameter
88 {
89  m_pParams = pParams;
90 }
91 
92 // get parameter
94 {
95  return m_pParams;
96 }
97 
98 // <<<<<< @Date:2013/07/29 <Add> A.Ozaki
99 
100 // run
101 //std::string Script::run( const char* script ) {
102 std::string Script::run( const char* script, kome::objects::Variant &var, kome::objects::Parameters *pParams ) {
103  setScript( script );
104 // >>>>>> @Date:2013/07/29 <Add> A.Ozaki
105  setVariant( var );
106  setParameters( pParams );
107 // <<<<<< @Date:2013/07/29 <Add> A.Ozaki
108 
109  std::string ret = run();
110 
111  return ret;
112 }
113 
114 // run
115 std::string Script::run() {
116 // >>>>>> @Date:2013/07/29 <Add> A.Ozaki
117  std::string ret = onRun( m_script.c_str( ), m_refVariant, m_pParams );
118 // <<<<<< @Date:2013/07/29 <Add> A.Ozaki
119 
120  return ret;
121 }
122 
123 // clear varibles
125  m_variables.clear();
126 }
127 
128 // add variable
129 void Script::addVariable( const char* name, const char* desc ) {
130  // check parameters
131  std::string n = NVL( name, "" );
132  if( n.empty() ) {
133  return;
134  }
135 
136  // search
137  int idx = -1;
138  for( unsigned int i = 0; i < m_variables.size() && idx < 0; i++ ) {
139  if( m_variables[ i ].name.compare( n ) == 0 ) {
140  idx = (int)i;
141  }
142  }
143 
144  // add
145  if( idx < 0 ) {
146  m_variables.resize( m_variables.size() + 1 );
147  m_variables.back().name = n;
148  m_variables.back().desc = NVL( desc, "" );
149  }
150  else {
151  m_variables[ idx ].desc = NVL( desc, "" );
152  }
153 }
154 
155 // get the variable count
157  return m_variables.size();
158 }
159 
160 // get variable name
161 const char* Script::getVariableName( const unsigned int idx ) {
162  if( idx >= m_variables.size() ) {
163  return NULL;
164  }
165 
166  return m_variables[ idx ].name.c_str();
167 }
168 
169 // get variable description
170 const char* Script::getVariableDescription( const unsigned int idx ) {
171  if( idx >= m_variables.size() ) {
172  return NULL;
173  }
174 
175  return m_variables[ idx ].desc.c_str();
176 }
177 
178 // set the script file path
179 void Script::setScriptFilePath( const char* path ) {
180  m_filePath = NVL( path, "" );
181 }
182 
183 // get the script file path
185  return m_filePath.c_str();
186 }
187 
188 
189 //
190 // ScriptManager
191 //
192 
193 // constructor
195 }
196 
197 
198 // destructor
200  while( m_scriptSet.size() > 0 ) {
201  delete *( m_scriptSet.begin() );
202  }
203 }
204 
205 // add script
207  if( script != NULL ) {
208  m_scriptSet.insert( script );
209  }
210 }
211 
212 // remove script
214  m_scriptSet.erase( script );
215 }
216 
217 // get the instance
219  static ScriptManager mgr;
220 
221  return mgr;
222 }
const char * getVariableName(const unsigned int idx)
gets the variable name
Definition: Script.cpp:161
std::string run()
run the script
Definition: Script.cpp:115
virtual std::string onGetTemplate()=0
This method is called from the getTempalte method. (abstract method)
void setParameters(kome::objects::Parameters *pParams)
sets the variant(Active Object)
Definition: Script.cpp:87
kome::objects::Variant m_refVariant
Definition: Script.h:74
void setVariant(kome::objects::Variant &var)
sets the variant(Active Object)
Definition: Script.cpp:75
unsigned int getNumberOfVariables()
gets the number of variables
Definition: Script.cpp:156
void removeScript(Script *script)
removes script
Definition: Script.cpp:213
ScriptManager()
constructor
Definition: Script.cpp:194
std::vector< VariableInfo > m_variables
Definition: Script.h:67
script abstract class
Definition: Script.h:39
interfarces of Script class
std::set< Script * > m_scriptSet
Definition: Script.h:245
std::string getTemplate()
gets the template
Definition: Script.cpp:67
void setScript(const char *script)
sets the script
Definition: Script.cpp:57
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
static void setScriptFilePath(const char *path)
sets the script file path
Definition: Script.cpp:179
#define NULL
Definition: CoreMacros.h:18
script manager class
Definition: Script.h:229
const kome::objects::Parameters * getParameters(void)
gets the parameter
Definition: Script.cpp:93
class that treats data of various types
Definition: Variant.h:26
static ScriptManager & getInstance()
gets the instance. (This is the only object.)
Definition: Script.cpp:218
static std::string m_filePath
Definition: Script.h:70
parameters of plug-in function management class
Definition: Parameters.h:28
virtual ~Script()
destructor
Definition: Script.cpp:51
std::string m_script
Definition: Script.h:55
const kome::objects::Variant & getVariant(void)
gets the variant
Definition: Script.cpp:81
void clearVariables()
clears variables
Definition: Script.cpp:124
void addScript(Script *script)
adds script
Definition: Script.cpp:206
kome::objects::Parameters * m_pParams
Definition: Script.h:77
const char * getScript()
gets the script
Definition: Script.cpp:62
virtual ~ScriptManager()
destructor
Definition: Script.cpp:199
void addVariable(const char *name, const char *desc)
adds variable
Definition: Script.cpp:129
virtual std::string onRun(const char *script, kome::objects::Variant &var, kome::objects::Parameters *pParams)=0
This method is called from the run method. (abstract method)
static const char * getScriptFilePath()
gets the script file path
Definition: Script.cpp:184
const char * getVariableDescription(const unsigned int idx)
gets the variable description
Definition: Script.cpp:170
Script()
constructor
Definition: Script.cpp:39