fastdo  0.6.16
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 
30 WINUX_FUNC_DECL(uint) GetPid( void );
32 WINUX_FUNC_DECL(uint) GetTid( void );
33 
34 WINUX_FUNC_DECL(size_t) CommandLineToArgvA( AnsiString const & cmd, AnsiStringArray * argv );
41 #if defined(_UNICODE) || defined(UNICODE)
42 inline size_t CommandLineToArgv( UnicodeString const & cmd, UnicodeStringArray * argv ) { return CommandLineToArgvW( cmd, argv ); }
43 #else
44 inline size_t CommandLineToArgv( AnsiString const & cmd, AnsiStringArray * argv ) { return CommandLineToArgvA( cmd, argv ); }
45 #endif
46 
56  String const & cmd,
57  HPipe * hStdinWritePipe,
58  HPipe * hStdoutReadPipe,
59  HPipe * hStderrReadPipe = NULL,
60  bool closeStdinIfStdinWritePipeIsNull = true
61 );
62 
72  String const & cmd,
73  String const & stdinStr,
74  String * stdoutStr,
75  String * stderrStr = NULL,
76  bool closeStdinIfStdinStrEmpty = true
77 );
78 
87  String const & cmd,
88  String const & stdinStr = $T(""),
89  String * stderrStr = NULL,
90  bool closeStdinIfStdinStrEmpty = true
91 );
92 
93 
95 
104 {
105 public:
115  int argc,
116  winux::tchar const ** argv,
117  Mixed const & desiredParams,
118  Mixed const & desiredOptions,
119  Mixed const & desiredFlags,
120  Mixed const & optionSymbols = $T("=,:")
121  );
122 
124  size_t getParamsCount() const { return _params.getCount(); }
126  size_t getOptionsCount() const { return _options.getCount(); }
128  size_t getFlagsCount() const { return _flags.getCount(); }
130  size_t getValuesCount() const { return _values.getCount(); }
131 
133  bool hasParam( String const & name ) const { return _params.has(name); }
135  bool hasOption( String const & name ) const { return _options.has(name); }
137  bool hasFlag( String const & name ) const { return _flags.has(name); }
139  bool hasValue( String const & value ) const { return _values.has(value); }
140 
142  Mixed const & getParam( String const & name, Mixed const & defValue = $T("") ) const { return this->hasParam(name) ? _params[name] : defValue; }
144  Mixed const & getOption( String const & name, Mixed const & defValue = $T("") ) const { return this->hasOption(name) ? _options[name] : defValue; }
146  Mixed const & getFlag( size_t i ) const { return _flags[i]; }
148  Mixed const & getValue( size_t i ) const { return _values[i]; }
149 
151  size_t getParamIndexInArgv( String const & name ) const { return _paramIndexesInArgv.find(name) != _paramIndexesInArgv.end() ? _paramIndexesInArgv.at(name) : npos; }
153  size_t getOptionIndexInArgv( String const & name ) const { return _optionIndexesInArgv.find(name) != _optionIndexesInArgv.end() ? _optionIndexesInArgv.at(name) : npos; }
155  size_t getFlagIndexInArgv( String const & name ) const { return _flagIndexesInArgv.find(name) != _flagIndexesInArgv.end() ? _flagIndexesInArgv.at(name) : npos; }
157  size_t getValueIndexInArgv( String const & value ) const { return _valueIndexesInArgv.find(value) != _valueIndexesInArgv.end() ? _valueIndexesInArgv.at(value) : npos; }
158 
160  Mixed & getParams() { return _params; }
162  Mixed & getOptions() { return _options; }
164  Mixed & getFlags() { return _flags; }
166  Mixed & getValues() { return _values; }
167 
169  Mixed dump() const
170  {
171  CommandLineVars * p = const_cast<CommandLineVars *>(this);
172  return $c{
173  { $T("params"), p->getParams() },
174  { $T("options"), p->getOptions() },
175  { $T("flags"), p->getFlags() },
176  { $T("values"), p->getValues() },
177  };
178  }
179 
181  int getArgc() const { return _argc; }
183  winux::tchar const ** getArgv() const { return _argv; }
184 
185 private:
186  int _argc; // main()命令行参数个数
187  winux::tchar const ** _argv; // main()命令行参数
188 
189  StringArray _desiredParams; // 要识别的参数名
190  StringArray _desiredOptions; // 要识别的选项名
191  StringArray _desiredFlags; // 要识别的旗标名
192  StringArray _optionSymbols; // 选项赋值符号
193 
194  Mixed _params; // 参数Collection
195  std::map< String, size_t > _paramIndexesInArgv; // 参数在argv中的索引
196  Mixed _options; // 选项Collection
197  std::map< String, size_t > _optionIndexesInArgv; // 选项在argv中的索引
198  Mixed _flags; // 旗标Array
199  std::map< String, size_t > _flagIndexesInArgv; // 旗标在argv中的索引
200  Mixed _values; // 值Array
201  std::map< String, size_t > _valueIndexesInArgv; // 值在argv中的索引
202 
204 };
205 
208 {
209 public:
210  virtual ~ILockObj() { }
211  virtual bool tryLock() = 0;
212  virtual bool lock() = 0;
213  virtual bool unlock() = 0;
214 };
215 
218 {
219 public:
220  ScopeGuard( ILockObj & lockObj ) : _lockObj(lockObj)
221  {
222  _lockObj.lock();
223  }
225  {
226  _lockObj.unlock();
227  }
228 
229 private:
230  ILockObj & _lockObj;
232 };
233 
238 {
239 public:
240  MutexNative();
241  virtual ~MutexNative();
242  virtual bool tryLock() override;
243  virtual bool lock() override;
244  virtual bool unlock() override;
245 
246 private:
248 
250 };
251 
254 {
255 public:
256  enum {
257  dllFuncNotFound = 0x00000100,
258  dllModuleNoLoaded
259  };
260 
261  DllLoaderError( int errType, AnsiString const & errStr ) throw() : SystemError( errType, errStr ) { }
262 };
263 
266 {
267 public:
268 
269 #if defined(OS_WIN)
270  typedef HMODULE ModuleHandle;
271 #else
272  typedef void * ModuleHandle;
273 #endif
274 
277  static String GetModulePath( void * funcInModule );
278 
279  DllLoader();
280  DllLoader( String const & dllName );
281 
282  ~DllLoader();
283 
284  operator bool() const { return _hDllModule != NULL; }
285  operator ModuleHandle() const { return _hDllModule; }
286 
288  char const * errStr() const;
289 
291  template < typename _PfnType >
292  class Function
293  {
294  public:
295  typedef _PfnType PfnType;
296  private:
297  AnsiString _funcName;
298  PfnType _pfn;
299  public:
300  Function() : _funcName(""), _pfn(0) { }
301  Function( AnsiString const & funcName, PfnType pfn ) : _funcName(funcName), _pfn(pfn) { }
302 
303  operator bool() const { return _pfn != NULL; }
304 
306  AnsiString const & getFuncName() const { return _funcName; }
307 
309  void * get() const { return reinterpret_cast<void *>(_pfn); }
310 
312  template < typename... _ArgType >
313  typename FuncTraits<PfnType>::ReturnType call( _ArgType&& ... arg )
314  {
315  if ( !_pfn ) throw DllLoaderError( DllLoaderError::dllFuncNotFound, _funcName + " is not found" );
316  return (*_pfn)( std::forward<_ArgType>(arg)... );
317  }
318  };
319 
321  void (* funcAddr( AnsiString const & funcName ) )();
322 
324  template < typename _PfnType >
325  Function<_PfnType> func( AnsiString const & funcName )
326  {
327  _PfnType pfn = (_PfnType)this->funcAddr(funcName);
328  return Function<_PfnType>( funcName, pfn );
329  }
330 
332 
333 private:
334  ModuleHandle _hDllModule;
335  AnsiString _errStr;
336 
338 };
339 
342 {
343  fmfUnspec = 0,
350 };
351 
354 {
355 public:
357  FileMapping();
358 
363  FileMapping( String const & filePath, FileMappingFlag flag = fmfWriteCopy );
364 
370  FileMapping(
371  #if defined(OS_WIN)
372  HANDLE file,
373  #else
374  int file,
375  #endif
376  bool isPeekFile,
377  FileMappingFlag flag
378  );
379 
381  virtual ~FileMapping();
382 
388  bool create( String const & filePath, FileMappingFlag flag );
389 
396  bool create(
397  #if defined(OS_WIN)
398  HANDLE file,
399  #else
400  int file,
401  #endif
402  bool isPeekFile,
403  FileMappingFlag flag
404  );
405 
407  void destroy();
408 
410  bool loadFile( String const & filePath, FileMappingFlag flag );
411 
413  bool loadFile(
414  #if defined(OS_WIN)
415  HANDLE file,
416  #else
417  int file,
418  #endif
419  bool isPeekFile,
420  FileMappingFlag flag
421  );
422 
424  size_t getFileSize() const;
425 
427  void unloadFile();
428 
435  bool map( FileMappingFlag flag = fmfUnspec, size_t size = 0, offset_t offset = 0 );
436 
438  void unmap();
439 
441  void * get() const { return _p; }
442 
444  template < typename _Ty >
445  _Ty * get() const { return reinterpret_cast<_Ty *>(_p); }
446 
448  size_t size() const { return _size; }
449 
450  operator bool() const;
451 
452 private:
453  // 零初始化
454  void _zeroInit();
455 
456 #if defined(OS_WIN)
457  HANDLE _file;
458  HANDLE _fileMapping;
459 #else
460  int _file;
461 #endif
462  void * _p; // 内存地址
463  size_t _size; // 映射的大小
464  FileMappingFlag _flag; // 记下创建标记
465  bool _isPeekFile; // 是否为使用外部文件资源,即不管理文件资源
466 
468 };
469 
475 {
476 public:
478  SharedMemory();
479 
484  SharedMemory( String const & shmName, size_t size );
485 
486  virtual ~SharedMemory();
487 
493  bool create( String const & shmName, size_t size );
494 
496  void destroy();
497 
499  void * lock();
500 
502  void unlock();
503 
505  void * get() { return _data ? _data : this->lock(); }
506 
507 
508 private:
509  String _shmName;
510 #if defined(OS_WIN)
511  HANDLE _shm;
512 #else
513  int _shm;
514 #endif
515  void * _data;
516  size_t _size;
517 
519 };
520 
522 template < typename _PodType >
524 {
525 public:
528 
533  SharedMemoryT( String const & shmName, size_t size = -1 )
534  {
535  this->create( shmName, size );
536  }
537 
543  bool create( String const & shmName, size_t size = -1 )
544  {
545  return SharedMemory::create( shmName, size == -1 ? sizeof(_PodType) : size );
546  }
547 
548  _PodType * get() { return reinterpret_cast<_PodType *>( this->SharedMemory::get() ); }
549 
550  _PodType * operator -> () { return this->get(); }
551 };
552 
553 } // namespace winux
554 
555 #endif // __SYSTEM_HPP__
XString< char > AnsiString
Definition: utilities.hpp:257
bool hasValue(String const &value) const
是否有此值
Definition: system.hpp:139
size_t getParamsCount() const
获取参数个数
Definition: system.hpp:124
同步锁对象接口
Definition: system.hpp:207
XString< wchar > UnicodeString
Definition: utilities.hpp:258
size_t getValuesCount() const
获取值个数
Definition: system.hpp:130
Mixed构造集合辅助类
Definition: utilities.hpp:1209
Mixed & getParams()
获取全部参数
Definition: system.hpp:160
ScopeGuard(ILockObj &lockObj)
Definition: system.hpp:220
写时拷贝执行
Definition: system.hpp:348
#define WINUX_DLL
Definition: utilities.hpp:60
winux::tchar const ** getArgv() const
获取argv
Definition: system.hpp:183
Mixed const & getFlag(size_t i) const
获取指定索引的旗标
Definition: system.hpp:146
FuncTraits< PfnType >::ReturnType call(_ArgType &&...arg)
函数调用
Definition: system.hpp:313
size_t getFlagsCount() const
获取旗标个数
Definition: system.hpp:128
Mixed const & getValue(size_t i) const
获取指定索引的值
Definition: system.hpp:148
uint GetPid(void)
获取当前进程ID
bool hasFlag(String const &name) const
是否有此旗标
Definition: system.hpp:137
Function< _PfnType > func(AnsiString const &funcName)
获取指定名字的Function对象,失败抛异常
Definition: system.hpp:325
int pid_t
Definition: threads.hpp:13
Mixed & getOptions()
获取全部选项
Definition: system.hpp:162
XString< tchar > String
Definition: utilities.hpp:261
Dll函数动态调用
Definition: system.hpp:292
Mixed const & getParam(String const &name, Mixed const &defValue=$T("")) const
获取指定名字的参数
Definition: system.hpp:142
FileMappingFlag
文件映射旗标
Definition: system.hpp:341
virtual ~ILockObj()
Definition: system.hpp:210
SharedMemoryT()
构造函数0
Definition: system.hpp:527
size_t size() const
获取数据大小
Definition: system.hpp:448
DllLoaderError(int errType, AnsiString const &errStr)
Definition: system.hpp:261
文件映射。可以用来读写大文件
Definition: system.hpp:353
共享内存(POD类型数据)类模板
Definition: system.hpp:523
写时拷贝
Definition: system.hpp:345
size_t CommandLineToArgvA(AnsiString const &cmd, AnsiStringArray *argv)
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:85
DLL动态载入器
Definition: system.hpp:265
函数特征
Definition: utilities.hpp:8
int HPipe
Definition: system.hpp:25
String GetExec(String const &cmd, String const &stdinStr=$T(""), String *stderrStr=NULL, bool closeStdinIfStdinStrEmpty=true)
执行命令,返回标准输出内容
SharedMemoryT(String const &shmName, size_t size=-1)
构造函数1
Definition: system.hpp:533
char tchar
Definition: utilities.hpp:245
系统相关错误
Definition: system.hpp:10
共享内存,可以跨进程访问。常用于进程间通讯
Definition: system.hpp:474
Mixed dump() const
倾泻全部
Definition: system.hpp:169
原生互斥锁
Definition: system.hpp:237
String dllModuleFile
DLL模块文件
Definition: system.hpp:331
作用域范围保护
Definition: system.hpp:217
Mixed & getFlags()
获取全部旗标
Definition: system.hpp:164
Mixed & getValues()
获取全部值
Definition: system.hpp:166
static constexpr size_t const npos
非位置,值为-1。
Definition: utilities.hpp:285
HProcess ExecCommandEx(String const &cmd, HPipe *hStdinWritePipe, HPipe *hStdoutReadPipe, HPipe *hStderrReadPipe=NULL, bool closeStdinIfStdinWritePipeIsNull=true)
新建子进程执行指定命令,并用管道重定向了标准设备
命令行变量解析器
Definition: system.hpp:103
XStringArray< char > AnsiStringArray
Definition: utilities.hpp:266
size_t getOptionsCount() const
获取选项个数
Definition: system.hpp:126
bool create(String const &shmName, size_t size)
创建共享内存
Function(AnsiString const &funcName, PfnType pfn)
Definition: system.hpp:301
SystemError(int errType, AnsiString const &errStr)
Definition: system.hpp:17
bool hasParam(String const &name) const
是否有此参数
Definition: system.hpp:133
size_t getParamIndexInArgv(String const &name) const
获取指定参数在argv中的索引
Definition: system.hpp:151
unsigned int uint
Definition: utilities.hpp:215
size_t CommandLineToArgv(AnsiString const &cmd, AnsiStringArray *argv)
把命令行解析成Argv数组。不支持命令行& && | ||
Definition: system.hpp:44
void * ModuleHandle
Definition: system.hpp:272
uint GetTid(void)
获取当前线程ID
int ExecCommand(String const &cmd, String const &stdinStr, String *stdoutStr, String *stderrStr=NULL, bool closeStdinIfStdinStrEmpty=true)
新建子进程执行指定命令,等待子进程结束,并把字符串重定向了标准设备
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:64
pid_t HProcess
Definition: system.hpp:26
Dll加载器错误
Definition: system.hpp:253
void * get()
获取数据指针(自动lock())
Definition: system.hpp:505
int getArgc() const
获取argc
Definition: system.hpp:181
bool hasOption(String const &name) const
是否有此选项
Definition: system.hpp:135
size_t getOptionIndexInArgv(String const &name) const
获取指定选项在argv中的索引
Definition: system.hpp:153
XStringArray< wchar > UnicodeStringArray
Definition: utilities.hpp:267
混合体,能表示多种类型的值
Definition: utilities.hpp:1440
错误类
Definition: utilities.hpp:838
size_t getValueIndexInArgv(String const &value) const
获取指定值在argv中的索引
Definition: system.hpp:157
Mixed const & getOption(String const &name, Mixed const &defValue=$T("")) const
获取指定名字的选项
Definition: system.hpp:144
bool create(String const &shmName, size_t size=-1)
创建共享内存
Definition: system.hpp:543
size_t getFlagIndexInArgv(String const &name) const
获取指定旗标在argv中的索引
Definition: system.hpp:155
XStringArray< tchar > StringArray
Definition: utilities.hpp:272
size_t CommandLineToArgvW(UnicodeString const &cmd, UnicodeStringArray *argv)
void(*)() funcAddr(AnsiString const &funcName)
获取指定名字的函数地址
Definition: system.hpp:321
未指定
Definition: system.hpp:343
AnsiString const & getFuncName() const
函数名
Definition: system.hpp:306
跨平台基础功能库
Definition: archives.hpp:7
intptr_t offset_t
Definition: utilities.hpp:225