fastdo  0.6.8
system.hpp
浏览该文件的文档.
1 #ifndef __SYSTEM_HPP__
2 #define __SYSTEM_HPP__
3 //
4 // system 提供一些系统平台相关的功能
5 //
6 
7 namespace winux
8 {
10 class SystemError : public Error
11 {
12 public:
13  enum {
15  };
16 
17  SystemError( int errType, AnsiString const & errStr ) throw() : Error( errType, errStr ) { }
18 };
19 
20 
21 #if defined(OS_WIN)
22  typedef HANDLE HPipe;
23  typedef HANDLE HProcess;
24 #else
25  typedef int HPipe;
26  typedef pid_t HProcess;
27 #endif
28 
29 
30 WINUX_FUNC_DECL(size_t) CommandLineToArgvA( AnsiString const & cmd, AnsiStringArray * argv );
37 #if defined(_UNICODE) || defined(UNICODE)
38 inline size_t CommandLineToArgv( UnicodeString const & cmd, UnicodeStringArray * argv ) { return CommandLineToArgvW( cmd, argv ); }
39 #else
40 inline size_t CommandLineToArgv( AnsiString const & cmd, AnsiStringArray * argv ) { return CommandLineToArgvA( cmd, argv ); }
41 #endif
42 
52  String const & cmd,
53  HPipe * hStdinWritePipe,
54  HPipe * hStdoutReadPipe,
55  HPipe * hStderrReadPipe = NULL,
56  bool closeStdinIfStdinWritePipeIsNull = true
57 );
58 
68  String const & cmd,
69  String const & stdinStr,
70  String * stdoutStr,
71  String * stderrStr = NULL,
72  bool closeStdinIfStdinStrEmpty = true
73 );
74 
77  String const & cmd,
78  String const & stdinStr = "",
79  String * stderrStr = NULL,
80  bool closeStdinIfStdinStrEmpty = true
81 );
82 
83 
85 
94 {
95 public:
105  int argc,
106  char const ** argv,
107  Mixed const & desiredParams,
108  Mixed const & desiredOptions,
109  Mixed const & desiredFlags,
110  Mixed const & optionSymbols = "=,:"
111  );
112 
114  size_t getParamsCount() const { return _params.getCount(); }
116  size_t getOptionsCount() const { return _options.getCount(); }
118  size_t getFlagsCount() const { return _flags.getCount(); }
120  size_t getValuesCount() const { return _values.getCount(); }
121 
123  bool hasParam( String const & name ) const { return _params.has(name); }
125  bool hasOption( String const & name ) const { return _options.has(name); }
127  bool hasFlag( String const & name ) const { return _flags.has(name); }
129  bool hasValue( String const & value ) const { return _values.has(value); }
130 
132  Mixed const & getParam( String const & name, Mixed const & defValue = "" ) const { return this->hasParam(name) ? _params[name] : defValue; }
134  Mixed const & getOption( String const & name, Mixed const & defValue = "" ) const { return this->hasOption(name) ? _options[name] : defValue; }
136  Mixed const & getFlag( size_t i ) const { return _flags[i]; }
138  Mixed const & getValue( size_t i ) const { return _values[i]; }
139 
141  size_t getParamIndexInArgv( String const & name ) const { return _paramIndexesInArgv.find(name) != _paramIndexesInArgv.end() ? _paramIndexesInArgv.at(name) : npos; }
143  size_t getOptionIndexInArgv( String const & name ) const { return _optionIndexesInArgv.find(name) != _optionIndexesInArgv.end() ? _optionIndexesInArgv.at(name) : npos; }
145  size_t getFlagIndexInArgv( String const & name ) const { return _flagIndexesInArgv.find(name) != _flagIndexesInArgv.end() ? _flagIndexesInArgv.at(name) : npos; }
147  size_t getValueIndexInArgv( String const & value ) const { return _valueIndexesInArgv.find(value) != _valueIndexesInArgv.end() ? _valueIndexesInArgv.at(value) : npos; }
148 
150  Mixed & getParams() { return _params; }
152  Mixed & getOptions() { return _options; }
154  Mixed & getFlags() { return _flags; }
156  Mixed & getValues() { return _values; }
157 
159  Mixed dump() const
160  {
161  CommandLineVars * p = const_cast<CommandLineVars *>(this);
162  return $c{
163  { "params", p->getParams() },
164  { "options", p->getOptions() },
165  { "flags", p->getFlags() },
166  { "values", p->getValues() },
167  };
168  }
169 
171  int getArgc() const { return _argc; }
173  char const ** getArgv() const { return _argv; }
174 
175 private:
176  int _argc; // main()命令行参数个数
177  char const ** _argv; // main()命令行参数
178 
179  StringArray _desiredParams; // 要识别的参数名
180  StringArray _desiredOptions; // 要识别的选项名
181  StringArray _desiredFlags; // 要识别的旗标名
182  StringArray _optionSymbols; // 选项赋值符号
183 
184  Mixed _params; // 参数Collection
185  std::map< String, size_t > _paramIndexesInArgv; // 参数在argv中的索引
186  Mixed _options; // 选项Collection
187  std::map< String, size_t > _optionIndexesInArgv; // 选项在argv中的索引
188  Mixed _flags; // 旗标Array
189  std::map< String, size_t > _flagIndexesInArgv; // 旗标在argv中的索引
190  Mixed _values; // 值Array
191  std::map< String, size_t > _valueIndexesInArgv; // 值在argv中的索引
192 
194 };
195 
198 {
199 public:
200  virtual ~ILockObj() { }
201  virtual bool tryLock() = 0;
202  virtual bool lock() = 0;
203  virtual bool unlock() = 0;
204 };
205 
208 {
209  ILockObj & _lockObj;
210 public:
211  ScopeGuard( ILockObj & lockObj ) : _lockObj(lockObj)
212  {
213  _lockObj.lock();
214  }
216  {
217  _lockObj.unlock();
218  }
220 };
221 
226 {
227 public:
228  MutexLockObj();
229  virtual ~MutexLockObj();
230  virtual bool tryLock() override;
231  virtual bool lock() override;
232  virtual bool unlock() override;
233 private:
235 
237 };
238 
241 {
242 public:
243  enum {
244  dllFuncNotFound = 0x00000100,
245  dllModuleNoLoaded
246  };
247 
248  DllLoaderError( int errType, AnsiString const & errStr ) throw() : SystemError( errType, errStr ) { }
249 };
250 
253 {
254 public:
255 
256 #if defined(OS_WIN)
257  typedef HMODULE ModuleHandle;
258 #else
259  typedef void * ModuleHandle;
260 #endif
261 
264  static String GetModulePath( void * funcInModule );
265 
266  DllLoader();
267  DllLoader( String const & dllName );
268 
269  ~DllLoader();
270 
271  operator bool() const { return _hDllModule != NULL; }
272  operator ModuleHandle() const { return _hDllModule; }
273 
275  char const * errStr() const;
276 
278  template < typename _PfnType >
279  class Function
280  {
281  public:
282  typedef _PfnType PfnType;
283  private:
284  AnsiString _funcName;
285  PfnType _pfn;
286  public:
287  Function() : _funcName(""), _pfn(0) { }
288  Function( AnsiString const & funcName, PfnType pfn ) : _funcName(funcName), _pfn(pfn) { }
289 
290  operator bool() const { return _pfn != NULL; }
291 
293  AnsiString const & getFuncName() const { return _funcName; }
294 
296  void * get() const { return reinterpret_cast<void *>(_pfn); }
297 
299  template < typename... _ArgType >
300  typename FuncTraits<PfnType>::ReturnType call( _ArgType&& ... arg )
301  {
302  if ( !_pfn ) throw DllLoaderError( DllLoaderError::dllFuncNotFound, _funcName + " is not found" );
303  return (*_pfn)( std::forward<_ArgType>(arg)... );
304  }
305  };
306 
308  void (* funcAddr( AnsiString const & funcName ) )();
309 
311  template < typename _PfnType >
312  Function<_PfnType> func( AnsiString const & funcName )
313  {
314  _PfnType pfn = (_PfnType)this->funcAddr(funcName);
315  return Function<_PfnType>( funcName, pfn );
316  }
317 
319 private:
320  ModuleHandle _hDllModule;
321  String _errStr;
322 
324 };
325 
331 {
332 public:
334  SharedMemory();
335 
340  SharedMemory( int shmKey, size_t size );
341 
342  virtual ~SharedMemory();
343 
349  bool create( int shmKey, size_t size );
350 
352  void destroy();
353 
355  void * lock();
356 
358  void unlock();
359 
361  void * get();
362 
363 private:
364 #if defined(OS_WIN)
365  HANDLE _shm;
366 #else
367  int _shm;
368 #endif
369  void * _data;
370  size_t _size;
371 
373 };
374 
376 template < typename _PodType >
378 {
379 public:
382 
387  SharedMemoryT( int shmKey, size_t size = -1 )
388  {
389  this->create( shmKey, size );
390  }
391 
397  bool create( int shmKey, size_t size = -1 )
398  {
399  return SharedMemory::create( shmKey, size == -1 ? sizeof(_PodType) : size );
400  }
401 
402  _PodType * operator -> ()
403  {
404  return reinterpret_cast<_PodType *>( this->get() );
405  }
406 };
407 
408 } // namespace winux
409 
410 #endif // __SYSTEM_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
bool hasValue(String const &value) const
是否有此值
Definition: system.hpp:129
size_t getParamsCount() const
获取参数个数
Definition: system.hpp:114
同步锁对象接口
Definition: system.hpp:197
XString< wchar > UnicodeString
Definition: utilities.hpp:213
互斥锁
Definition: system.hpp:225
size_t getValuesCount() const
获取值个数
Definition: system.hpp:120
Mixed构造集合辅助类
Definition: utilities.hpp:741
Mixed & getParams()
获取全部参数
Definition: system.hpp:150
ScopeGuard(ILockObj &lockObj)
Definition: system.hpp:211
#define WINUX_DLL
Definition: utilities.hpp:60
Mixed const & getFlag(size_t i) const
获取指定索引的旗标
Definition: system.hpp:136
Mixed const & getParam(String const &name, Mixed const &defValue="") const
获取指定名字的参数
Definition: system.hpp:132
FuncTraits< PfnType >::ReturnType call(_ArgType &&...arg)
函数调用
Definition: system.hpp:300
size_t getFlagsCount() const
获取旗标个数
Definition: system.hpp:118
Mixed const & getValue(size_t i) const
获取指定索引的值
Definition: system.hpp:138
bool hasFlag(String const &name) const
是否有此旗标
Definition: system.hpp:127
Function< _PfnType > func(AnsiString const &funcName)
获取指定名字的Function对象,失败抛异常
Definition: system.hpp:312
Mixed & getOptions()
获取全部选项
Definition: system.hpp:152
XString< tchar > String
Definition: utilities.hpp:216
Dll函数动态调用
Definition: system.hpp:279
virtual ~ILockObj()
Definition: system.hpp:200
SharedMemoryT()
构造函数0
Definition: system.hpp:381
String GetExec(String const &cmd, String const &stdinStr="", String *stderrStr=NULL, bool closeStdinIfStdinStrEmpty=true)
执行命令,返回标准输出内容
bool create(int shmKey, size_t size=-1)
创建共享内存
Definition: system.hpp:397
DllLoaderError(int errType, AnsiString const &errStr)
Definition: system.hpp:248
共享内存(POD类型数据)类模板
Definition: system.hpp:377
size_t CommandLineToArgvA(AnsiString const &cmd, AnsiStringArray *argv)
Mixed const & getOption(String const &name, Mixed const &defValue="") const
获取指定名字的选项
Definition: system.hpp:134
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:81
DLL动态载入器
Definition: system.hpp:252
函数特征
Definition: utilities.hpp:8
int HPipe
Definition: system.hpp:25
系统相关错误
Definition: system.hpp:10
共享内存,可以跨进程访问。常用于进程间通讯
Definition: system.hpp:330
Mixed dump() const
倾泻全部
Definition: system.hpp:159
String dllModuleFile
DLL模块文件
Definition: system.hpp:318
作用域范围保护
Definition: system.hpp:207
Mixed & getFlags()
获取全部旗标
Definition: system.hpp:154
Mixed & getValues()
获取全部值
Definition: system.hpp:156
static constexpr size_t const npos
非位置,值为-1。
Definition: utilities.hpp:240
HProcess ExecCommandEx(String const &cmd, HPipe *hStdinWritePipe, HPipe *hStdoutReadPipe, HPipe *hStderrReadPipe=NULL, bool closeStdinIfStdinWritePipeIsNull=true)
新建子进程执行指定命令,并用管道重定向了标准设备
命令行变量解析器
Definition: system.hpp:93
XStringArray< char > AnsiStringArray
Definition: utilities.hpp:221
size_t getOptionsCount() const
获取选项个数
Definition: system.hpp:116
Function(AnsiString const &funcName, PfnType pfn)
Definition: system.hpp:288
SystemError(int errType, AnsiString const &errStr)
Definition: system.hpp:17
bool hasParam(String const &name) const
是否有此参数
Definition: system.hpp:123
size_t getParamIndexInArgv(String const &name) const
获取指定参数在argv中的索引
Definition: system.hpp:141
size_t CommandLineToArgv(AnsiString const &cmd, AnsiStringArray *argv)
把命令行解析成Argv数组。不支持命令行& && | ||
Definition: system.hpp:40
void * ModuleHandle
Definition: system.hpp:259
int ExecCommand(String const &cmd, String const &stdinStr, String *stdoutStr, String *stderrStr=NULL, bool closeStdinIfStdinStrEmpty=true)
新建子进程执行指定命令,等待子进程结束,并把字符串重定向了标准设备
SharedMemoryT(int shmKey, size_t size=-1)
构造函数1
Definition: system.hpp:387
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:64
pid_t HProcess
Definition: system.hpp:26
bool create(int shmKey, size_t size)
创建共享内存
Dll加载器错误
Definition: system.hpp:240
int getArgc() const
获取argc
Definition: system.hpp:171
bool hasOption(String const &name) const
是否有此选项
Definition: system.hpp:125
size_t getOptionIndexInArgv(String const &name) const
获取指定选项在argv中的索引
Definition: system.hpp:143
XStringArray< wchar > UnicodeStringArray
Definition: utilities.hpp:222
混合体,能表示多种类型的值
Definition: utilities.hpp:750
错误类
Definition: utilities.hpp:505
size_t getValueIndexInArgv(String const &value) const
获取指定值在argv中的索引
Definition: system.hpp:147
size_t getFlagIndexInArgv(String const &name) const
获取指定旗标在argv中的索引
Definition: system.hpp:145
XStringArray< tchar > StringArray
Definition: utilities.hpp:227
size_t CommandLineToArgvW(UnicodeString const &cmd, UnicodeStringArray *argv)
void(*)() funcAddr(AnsiString const &funcName)
获取指定名字的函数地址
Definition: system.hpp:308
AnsiString const & getFuncName() const
函数名
Definition: system.hpp:293
跨平台基础功能库
Definition: archives.hpp:7
char const ** getArgv() const
获取argv
Definition: system.hpp:173