fastdo  0.5.12
system.hpp
浏览该文件的文档.
1 #ifndef __SYSTEM_HPP__
2 #define __SYSTEM_HPP__
3 
4 namespace winux
5 {
6 #if defined(OS_WIN)
7  typedef HANDLE HPipe;
8  typedef HANDLE HProcess;
9 #else
10  typedef int HPipe;
11  typedef pid_t HProcess;
12 #endif
13 
19 WINUX_FUNC_DECL(int) CommandLineToArgv( winux::String const & cmd, winux::StringArray * argv );
20 
30  winux::String const & cmd,
31  HPipe * hStdinWritePipe,
32  HPipe * hStdoutReadPipe,
33  HPipe * hStderrReadPipe = NULL,
34  bool closeStdinIfStdinWritePipeIsNull = true
35 );
36 
46  winux::String const & cmd,
47  winux::String const & stdinStr,
48  winux::String * stdoutStr,
49  winux::String * stderrStr = NULL,
50  bool closeStdinIfStdinStrEmpty = true
51 );
52 
54  winux::String const & cmd,
55  winux::String const & stdinStr = "",
56  winux::String * stderrStr = NULL,
57  bool closeStdinIfStdinStrEmpty = true
58 );
59 
68 {
69 public:
78  CommandLineVars(
79  int argc,
80  char const ** argv,
81  Mixed const & desiredParams,
82  Mixed const & desiredOptions,
83  Mixed const & desiredFlags,
84  Mixed const & optionSymbols = "=,:"
85  );
86  int getParamsCount() const { return _params.getCount(); }
87  int getOptionsCount() const { return _options.getCount(); }
88  int getFlagsCount() const { return _flags.getCount(); }
89  int getValuesCount() const { return _values.getCount(); }
90 
91  bool hasParam( String const & name ) const { return _params.has(name); }
92  bool hasOption( String const & name ) const { return _options.has(name); }
93  bool hasFlag( String const & name ) const { return _flags.has(name); }
94  bool hasValue( String const & value ) const { return _values.has(value); }
95 
96  Mixed const & getParam( String const & name, Mixed const & defValue = "" ) const { return this->hasParam(name) ? _params[name] : defValue; }
97  Mixed const & getOption( String const & name, Mixed const & defValue = "" ) const { return this->hasOption(name) ? _options[name] : defValue; }
98  Mixed const & getFlag( int i ) const { return _flags[i]; }
99  Mixed const & getValue( int i ) const { return _values[i]; }
100 
101  int getParamIndexInArgv( String const & name ) const { return _paramIndexesInArgv.find(name) != _paramIndexesInArgv.end() ? _paramIndexesInArgv.at(name) : -1; }
102  int getOptionIndexInArgv( String const & name ) const { return _optionIndexesInArgv.find(name) != _optionIndexesInArgv.end() ? _optionIndexesInArgv.at(name) : -1; }
103  int getFlagIndexInArgv( String const & name ) const { return _flagIndexesInArgv.find(name) != _flagIndexesInArgv.end() ? _flagIndexesInArgv.at(name) : -1; }
104  int getValueIndexInArgv( String const & value ) const { return _valueIndexesInArgv.find(value) != _valueIndexesInArgv.end() ? _valueIndexesInArgv.at(value) : -1; }
105 
106  Mixed & getParams() { return _params; }
107  Mixed & getOptions() { return _options; }
108  Mixed & getFlags() { return _flags; }
109  Mixed & getValues() { return _values; }
110 
111  int getArgc() const { return _argc; }
112  char const ** getArgv() const { return _argv; }
113 private:
114  static void __MixedAppendToStringArray( Mixed const & mx, StringArray * arr );
115 
116  int _argc;
117  char const ** _argv;
118  StringArray _desiredParams;
119  StringArray _desiredOptions;
120  StringArray _desiredFlags;
121  StringArray _optionSymbols;
122 
123  Mixed _params;
124  std::map< String, int > _paramIndexesInArgv;
125  Mixed _options;
126  std::map< String, int > _optionIndexesInArgv;
127  Mixed _flags;
128  std::map< String, int > _flagIndexesInArgv;
129  Mixed _values;
130  std::map< String, int > _valueIndexesInArgv;
131  DISABLE_OBJECT_COPY(CommandLineVars)
132 };
133 
143 {
144  StringStringMap _params;
145  StringArray _values;
146  StringArray _flags;
147  static bool _IsName( String const & arg, StringArray const & prefixes );
148 public:
149  CommandLine( int argc, char * argv[], String const & paramPrefix = "- -- /" );
150  int getParamCount() const { return (int)_params.size(); }
151  int getValueCount() const { return (int)_values.size(); }
152  int getFlagCount() const { return (int)_flags.size(); }
153  bool hasParam( String const & name ) const { return isset( _params, name ); }
154  bool hasValue( String const & value ) const { return std::find( _values.begin(), _values.end(), value ) != _values.end(); }
155  bool hasFlag( String const & name ) const { return std::find( _flags.begin(), _flags.end(), name ) != _flags.end(); }
156  String getParam( String const & name, String const & defValue = "" ) const { return isset( _params, name ) ? _params.at(name) : defValue; }
157  String getValue( int i ) const { return _values[i]; }
158  String getFlag( int i ) const { return _flags[i]; }
159 
161 };
162 
165 {
166 public:
167  virtual ~ILockObj() { }
168  virtual bool tryLock() = 0;
169  virtual bool lock() = 0;
170  virtual bool unlock() = 0;
171 };
172 
175 {
176  ILockObj & _lockObj;
177 public:
178  ScopeGuard( ILockObj & lockObj ) : _lockObj(lockObj)
179  {
180  _lockObj.lock();
181  }
183  {
184  _lockObj.unlock();
185  }
187 };
188 
193 {
194 public:
195  MutexLockObj();
196  virtual ~MutexLockObj();
197  virtual bool tryLock();
198  virtual bool lock();
199  virtual bool unlock();
200 private:
202 
204 };
205 
207 class DllLoaderError : public Error
208 {
209 public:
210  enum {
211  DllLoader_FuncNotFound = 0x00000100,
212  DllLoader_ModuleNoLoaded
213  };
214 
215  DllLoaderError( int errType, AnsiString const & errStr ) throw() : Error( errType, errStr ) { }
216 };
217 
220 {
221 public:
222 
223 #if defined(OS_WIN)
224  typedef HMODULE ModuleHandle;
225 #else
226  typedef void * ModuleHandle;
227 #endif
228 
231  static String GetModulePath( void * funcInModule );
232 
233  DllLoader();
234  DllLoader( String const & dllName );
235 
236  ~DllLoader();
237 
238  operator bool() const { return _hDllModule != NULL; }
239  operator ModuleHandle() const { return _hDllModule; }
240 
242  char const * errStr() const;
243 
245  template < typename _PfnType >
246  class Function
247  {
248  public:
249  typedef _PfnType PfnType;
250  private:
251  AnsiString _funcName;
252  PfnType _pfn;
253  public:
254  Function() : _funcName(""), _pfn(0) { }
255  Function( AnsiString const & funcName, PfnType pfn ) : _funcName(funcName), _pfn(pfn) { }
256 
257  operator bool() const { return _pfn != NULL; }
258 
259  AnsiString const & getFuncName() const { return _funcName; }
260  void * get() const { return reinterpret_cast<void *>(_pfn); }
261 
262  template < typename... _ArgType >
263  typename winux::FuncTraits<PfnType>::ReturnType call( _ArgType&& ... arg )
264  {
265  if ( !_pfn ) throw DllLoaderError( DllLoaderError::DllLoader_FuncNotFound, _funcName + " is not found" );
266  return (*_pfn)( std::forward<_ArgType>(arg)... );
267  }
268  };
269 
271  void (* funcAddr( AnsiString const & funcName ) )();
272 
274  template < typename _PfnType >
275  Function<_PfnType> func( AnsiString const & funcName )
276  {
277  _PfnType pfn = (_PfnType)this->funcAddr(funcName);
278  return Function<_PfnType>( funcName, pfn );
279  }
280 
282 private:
283  ModuleHandle _hDllModule;
284 
286 };
287 
288 
289 } // namespace winux
290 
291 #endif // __SYSTEM_HPP__
命令行参数解析(OldVersion)
Definition: system.hpp:142
bool hasValue(String const &value) const
Definition: system.hpp:94
同步锁对象接口
Definition: system.hpp:164
int getValueIndexInArgv(String const &value) const
Definition: system.hpp:104
互斥锁
Definition: system.hpp:192
int getFlagCount() const
Definition: system.hpp:152
int ExecCommand(winux::String const &cmd, winux::String const &stdinStr, winux::String *stdoutStr, winux::String *stderrStr=NULL, bool closeStdinIfStdinStrEmpty=true)
新建子进程执行指定命令,等待子进程结束,并把字符串重定向了标准设备
winux::String dllModuleFile
Dll模块文件
Definition: system.hpp:281
ScopeGuard(ILockObj &lockObj)
Definition: system.hpp:178
int getFlagIndexInArgv(String const &name) const
Definition: system.hpp:103
#define WINUX_DLL
Definition: utilities.hpp:57
std::basic_string< char > AnsiString
Definition: utilities.hpp:165
int getValueCount() const
Definition: system.hpp:151
Mixed const & getParam(String const &name, Mixed const &defValue="") const
Definition: system.hpp:96
bool hasParam(String const &name) const
Definition: system.hpp:153
bool hasFlag(String const &name) const
Definition: system.hpp:93
Function< _PfnType > func(AnsiString const &funcName)
获取指定名字的Function对象,失败抛异常
Definition: system.hpp:275
winux::String GetExec(winux::String const &cmd, winux::String const &stdinStr="", winux::String *stderrStr=NULL, bool closeStdinIfStdinStrEmpty=true)
String getParam(String const &name, String const &defValue="") const
Definition: system.hpp:156
Mixed & getOptions()
Definition: system.hpp:107
Dll函数动态调用
Definition: system.hpp:246
int getParamIndexInArgv(String const &name) const
Definition: system.hpp:101
int getOptionsCount() const
Definition: system.hpp:87
virtual ~ILockObj()
Definition: system.hpp:167
bool hasValue(String const &value) const
Definition: system.hpp:154
std::map< String, String > StringStringMap
Definition: utilities.hpp:172
int getValuesCount() const
Definition: system.hpp:89
DllLoaderError(int errType, AnsiString const &errStr)
Definition: system.hpp:215
bool hasFlag(String const &name) const
Definition: system.hpp:155
Mixed const & getOption(String const &name, Mixed const &defValue="") const
Definition: system.hpp:97
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:78
DLL动态载入器
Definition: system.hpp:219
std::vector< String > StringArray
Definition: utilities.hpp:171
int HPipe
Definition: system.hpp:10
int getOptionIndexInArgv(String const &name) const
Definition: system.hpp:102
作用域范围保护
Definition: system.hpp:174
命令行变量解析器
Definition: system.hpp:67
int getFlagsCount() const
Definition: system.hpp:88
Function(AnsiString const &funcName, PfnType pfn)
Definition: system.hpp:255
int getParamsCount() const
Definition: system.hpp:86
String getValue(int i) const
Definition: system.hpp:157
bool isset(_MAP const &m, _KEY const &k)
检测map中是否有该键的值
Definition: utilities.hpp:185
bool hasParam(String const &name) const
Definition: system.hpp:91
int CommandLineToArgv(winux::String const &cmd, winux::StringArray *argv)
把命令行解析成Argv数组
void * ModuleHandle
Definition: system.hpp:226
String getFlag(int i) const
Definition: system.hpp:158
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:61
pid_t HProcess
Definition: system.hpp:11
int getParamCount() const
Definition: system.hpp:150
Mixed const & getValue(int i) const
Definition: system.hpp:99
Dll加载器错误
Definition: system.hpp:207
HProcess ExecCommandEx(winux::String const &cmd, HPipe *hStdinWritePipe, HPipe *hStdoutReadPipe, HPipe *hStderrReadPipe=NULL, bool closeStdinIfStdinWritePipeIsNull=true)
新建子进程执行指定命令,并用管道重定向了标准设备
int getArgc() const
Definition: system.hpp:111
bool hasOption(String const &name) const
Definition: system.hpp:92
混合体,能表示多种类型的值
Definition: utilities.hpp:586
错误类
Definition: utilities.hpp:413
winux::FuncTraits< PfnType >::ReturnType call(_ArgType &&...arg)
Definition: system.hpp:263
std::basic_string< tchar > String
Definition: utilities.hpp:162
Mixed const & getFlag(int i) const
Definition: system.hpp:98
void(*)() funcAddr(AnsiString const &funcName)
获取指定名字的函数地址
Definition: system.hpp:271
AnsiString const & getFuncName() const
Definition: system.hpp:259
跨平台基础功能库
Definition: archives.hpp:7
char const ** getArgv() const
Definition: system.hpp:112