Monthly Archives: August 2009

pointer to member functions

Though with a similiar grammar as that of free funstions and static class member functions,  pointers to member functions  are not addresses, but offsets – how can the addresses be determined when the class is not initialised on the stack/heap yet?

Below are two ways to  define and use member function pointers:

  1: // C-style version
  2: void shrink(raw_path_groups_type rpgs)
  3: {
  4:     // define the pointer
  5:     typedef detectiveT<walkerT, equal_predicateT> this_class;
  6:     typedef void (this_class::*func_type)(raw_path_groups_type); 
  7: 
  8:     boost::array<func_type, 5> f = 
  9:     {
 10:         &this_class::single_threaded_process, 
 11:         &this_class::concurrent_process_thread_group,
 12:         &this_class::concurrent_process_ppl,
 13:         &this_class::concurrent_process_tbb,
 14:         &this_class::concurrent_boost_task   
 15:     };  
 16:        
 17:     (this->*f[m_thread_mode])(rpgs); // invoke it
 18:     //alternatively, boost::bind(f[m_thread_mode], this, rpgs)();    
 19: }

alternatively, below is a boost::function version.

  1: // boost::function-style version
  2: void shrink(raw_path_groups_type rpgs)
  3: {
  4:     typedef detectiveT<walkerT, equal_predicateT> this_class;
  5:     typedef boost::function<void(this_class*, raw_path_groups_type)> funct_type;
  6:     
  7:     boost::array<funct_type, 5> f = 
  8:     {
  9:         // boost::mem_fn is necessary for VC++10 to resolve ambiguity with tr1;
 10:         // but necessary not for VC9 
 11:         boost::mem_fn(&this_class::single_threaded_process), 
 12:         boost::mem_fn(&this_class::concurrent_process_thread_group),
 13:         boost::mem_fn(&this_class::concurrent_process_ppl),
 14:         boost::mem_fn(&this_class::concurrent_process_tbb),
 15:         boost::mem_fn(&this_class::concurrent_boost_task)   
 16:     }; 
 17:     
 18:     f[m_thread_mode](this, rpgs); 
 19:     //alternatively, boost::bind(f[m_thread_mode], this, rpgs)();                
 20: } 

Advertisements

WinUSB 2.0 available

WinUSB 2.0 is released with WDK for Windows 7 and Windows Server 2008 R2. I have have time to check the difference it, at first glance of of winusb.h:

  • macros like

IN/OUT 

are replaced with compiler directives 

__in/__out,

  • __in_bcount(ValueLength)

is introduced to for bounds check.

 

Hope I could find time to check it more thoroughly and complete the WinUSB++ Series soon.

Hope.

C++ Template Metaprogramming

Having almost finished reading "C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond" from cover to cover, I have to admit it’s not only a door that opens a new world before me,  but also a stage that lifts me higher, when I look back, to see things more clear.

Metaprogramming is about computing types to types, what are the points of doing that? Anyway, what we are doing in programming are virtually computing values to values(through functions), and types are actually the bounds that limit us. Without types(impossible to date) or with minimum types(as in dynamic languages like Python), life will seem to be much easier,  think about it, pick a symbol, say a, and use it as a file, a float, a string or whatever you like would be really fun. But it’s only a dream as yet.

Living in today’s world, being able to cross types, even only a few, is a forward step toward the Utopia of type-less world, and kind of liberation. That’s my understanding of the type computing part of metaprogramming.

Metaprogramming is also about integral values, which assists in type computing and crossing the compile-time and run-time world. However,  as we’ve been living long enough with values, this part is not so mind-drilling.