fork download
  1. struct ISomeInterface
  2. {
  3. virtual unsigned doSomething() = 0;
  4. };
  5.  
  6. template<typename TInterface, typename TDerived>
  7. class CImplBase : public TInterface
  8. {
  9. public:
  10.  
  11. typedef TDerived derived_class;
  12. typedef unsigned (typename derived_class :: *param_set_proc_t)( const std::wstring &paramName, void *pv ); //!!!
  13. typedef unsigned (typename derived_class :: *param_get_proc_t)( const std::wstring &paramName, void *pv );
  14.  
  15. struct CParamHandlerInfo{
  16. typename param_set_proc_t set_proc;
  17. typename param_get_proc_t get_proc;
  18. CParamHandlerInfo( param_set_proc_t s, param_get_proc_t g ) : set_proc(s), get_proc(g) {}
  19. };
  20.  
  21. std::map< std::wstring , CParamHandlerInfo > paramHandlers;
  22.  
  23. void addParamHandler( const std::wstring &name, param_set_proc_t s, param_get_proc_t g )
  24. {
  25. paramHandlers[name] = CParamHandlerInfo(s,g);
  26. }
  27.  
  28. typename std::map< std::wstring , CParamHandlerInfo >::const_iterator
  29. findParamHandler( const std::wstring &name )
  30. {
  31. return paramHandlers.find( name );
  32. }
  33.  
  34.  
  35. unsigned setParam(const std::wstring &paramName, void *pv )
  36. {
  37. std::map< std::wstring , CParamHandlerInfo >::const_iterator cit = findParamHandler( paramName );
  38. if (cit==paramHandlers.end())
  39. return 0;
  40. return this->*(cit->set_proc)(paramName,pv);
  41. }
  42.  
  43. CLIMETHOD(getParam) (const std::wstring &paramName, void *pv )
  44. {
  45. std::map< std::wstring , CParamHandlerInfo >::const_iterator cit = findParamHandler( paramName );
  46. if (cit==paramHandlers.end())
  47. return 0;
  48. return this->*(cit->get_proc)(paramName,pv);
  49. }
  50.  
  51.  
  52. };
  53.  
  54. // Использование
  55. class CImpl : public CImplBase<ISomeInterface,CImpl>
  56. {
  57. public:
  58.  
  59. unsigned paramSetParamHandler( const std::wstring &paramName, void *pv )
  60. {
  61. return 0;
  62. }
  63.  
  64. unsigned paramGetParamHandler( const std::wstring &paramName, void *pv )
  65. {
  66. return 0;
  67. }
  68.  
  69. CImpl()
  70. {
  71. addParamHandler( L"param", &paramSetParamHandler, &paramGetParamHandler );
  72. //addParamHandler( L"param", &CImpl::paramSetParamHandler, &CImpl::paramGetParamHandler ); // или так?
  73. }
  74.  
  75. unsigned doSomething()
  76. {
  77. return 0;
  78. }
  79.  
  80. };
  81.  
  82. int main()
  83. {
  84. CImpl impl;
  85. impl.getParam(L"param", (void*)0 );
  86. }
  87.  
  88.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:12: error: expected unqualified-id before 'typename'
prog.cpp:12: error: expected `)' before 'typename'
prog.cpp:13: error: expected unqualified-id before 'typename'
prog.cpp:13: error: expected `)' before 'typename'
prog.cpp:16: error: expected nested-name-specifier before 'param_set_proc_t'
prog.cpp:16: error: expected ';' before 'set_proc'
prog.cpp:17: error: expected nested-name-specifier before 'param_get_proc_t'
prog.cpp:17: error: expected ';' before 'get_proc'
prog.cpp:18: error: expected `)' before 's'
prog.cpp:21: error: ISO C++ forbids declaration of 'map' with no type
prog.cpp:21: error: invalid use of '::'
prog.cpp:21: error: expected ';' before '<' token
prog.cpp:23: error: expected unqualified-id before '&' token
prog.cpp:23: error: expected ',' or '...' before '&' token
prog.cpp:28: error: 'map' in namespace 'std' does not name a type
prog.cpp:28: error: expected unqualified-id before '<' token
prog.cpp:35: error: expected unqualified-id before '&' token
prog.cpp:35: error: expected ',' or '...' before '&' token
prog.cpp:43: error: 'getParam' has not been declared
prog.cpp:43: error: expected unqualified-id before '&' token
prog.cpp:43: error: expected ',' or '...' before '&' token
prog.cpp:43: error: ISO C++ forbids declaration of 'CLIMETHOD' with no type
prog.cpp:43: error: 'CLIMETHOD' declared as function returning a function
prog.cpp: In member function 'void CImplBase<TInterface, TDerived>::addParamHandler()':
prog.cpp:25: error: 'paramHandlers' was not declared in this scope
prog.cpp:25: error: 'name' was not declared in this scope
prog.cpp:25: error: 's' was not declared in this scope
prog.cpp:25: error: 'g' was not declared in this scope
prog.cpp: In member function 'unsigned int CImplBase<TInterface, TDerived>::setParam()':
prog.cpp:37: error: 'map' is not a member of 'std'
prog.cpp:37: error: 'wstring' is not a member of 'std'
prog.cpp:37: error: expected primary-expression before '>' token
prog.cpp:37: error: '::const_iterator' has not been declared
prog.cpp:37: error: expected `;' before 'cit'
prog.cpp:38: error: 'cit' was not declared in this scope
prog.cpp:38: error: 'paramHandlers' was not declared in this scope
prog.cpp:40: error: 'cit' was not declared in this scope
prog.cpp:40: error: 'paramName' was not declared in this scope
prog.cpp:40: error: 'pv' was not declared in this scope
prog.cpp: In member function 'int CImplBase<TInterface, TDerived>::CLIMETHOD(int)':
prog.cpp:45: error: 'map' is not a member of 'std'
prog.cpp:45: error: 'wstring' is not a member of 'std'
prog.cpp:45: error: expected primary-expression before '>' token
prog.cpp:45: error: '::const_iterator' has not been declared
prog.cpp:45: error: expected `;' before 'cit'
prog.cpp:46: error: 'cit' was not declared in this scope
prog.cpp:46: error: 'paramHandlers' was not declared in this scope
prog.cpp:48: error: 'cit' was not declared in this scope
prog.cpp:48: error: 'paramName' was not declared in this scope
prog.cpp:48: error: 'pv' was not declared in this scope
prog.cpp: At global scope:
prog.cpp:59: error: expected unqualified-id before '&' token
prog.cpp:59: error: expected ',' or '...' before '&' token
prog.cpp:64: error: expected unqualified-id before '&' token
prog.cpp:64: error: expected ',' or '...' before '&' token
prog.cpp: In constructor 'CImpl::CImpl()':
prog.cpp:71: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&CImpl::paramSetParamHandler'
prog.cpp:71: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&CImpl::paramGetParamHandler'
prog.cpp:71: error: no matching function for call to 'CImpl::addParamHandler(const wchar_t [6], unsigned int (CImpl::*)(), unsigned int (CImpl::*)())'
prog.cpp:23: note: candidates are: void CImplBase<TInterface, TDerived>::addParamHandler() [with TInterface = ISomeInterface, TDerived = CImpl]
prog.cpp: In function 'int main()':
prog.cpp:85: error: 'class CImpl' has no member named 'getParam'
stdout
Standard output is empty