天天看点

STL 适配器实现

// Adaptor function objects: pointers to member functions.

// There are a total of 16 = 2^4 function objects in this family.

//  (1) Member functions taking no arguments vs member functions taking

//       one argument.

//  (2) Call through pointer vs call through reference.

//  (3) Member function with void return type vs member function with

//      non-void return type.

//  (4) Const vs non-const member function.

// Note that choice (3) is nothing more than a workaround: according

//  to the draft, compilers should handle void and non-void the same way.

//  This feature is not yet widely implemented, though.  You can only use

//  member functions returning void if your compiler supports partial

//  specialization.

// All of this complexity is in the function objects themselves.  You can

//  ignore it by using the helper function mem_fun and mem_fun_ref,

//  which create whichever type of adaptor is appropriate.

//  (mem_fun1 and mem_fun1_ref are no longer part of the C++ standard,

//  but they are provided for backward compatibility.)

template <class _Ret, class _Tp>

class mem_fun_t : public unary_function<_Tp*,_Ret> {

public:

  explicit mem_fun_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}

  _Ret operator()(_Tp* __p) const { return (__p->*_M_f)(); }

private:

  _Ret (_Tp::*_M_f)();

};

//函数接口转函数对象接口的适配器(内部调用参数为指针类型)

template <class _Ret, class _Tp>

class const_mem_fun_t : public unary_function<const _Tp*,_Ret> {

public:

  explicit const_mem_fun_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}

  _Ret operator()(const _Tp* __p) const { return (__p->*_M_f)(); }

private:

  _Ret (_Tp::*_M_f)() const;

};

//const函数接口转函数对象接口的适配器(内部调用参数为const指针类型)

template <class _Ret, class _Tp>

class mem_fun_ref_t : public unary_function<_Tp,_Ret> {

public:

  explicit mem_fun_ref_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}

  _Ret operator()(_Tp& __r) const { return (__r.*_M_f)(); }

private:

  _Ret (_Tp::*_M_f)();

};

//内部调用参数为引用类型

template <class _Ret, class _Tp>

class const_mem_fun_ref_t : public unary_function<_Tp,_Ret> {

public:

  explicit const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}

  _Ret operator()(const _Tp& __r) const { return (__r.*_M_f)(); }

private:

  _Ret (_Tp::*_M_f)() const;

};

//内部调用参数为const引用类型

template <class _Ret, class _Tp, class _Arg>

class mem_fun1_t : public binary_function<_Tp*,_Arg,_Ret> {

public:

  explicit mem_fun1_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}

  _Ret operator()(_Tp* __p, _Arg __x) const { return (__p->*_M_f)(__x); }

private:

  _Ret (_Tp::*_M_f)(_Arg);

};

template <class _Ret, class _Tp, class _Arg>

class const_mem_fun1_t : public binary_function<const _Tp*,_Arg,_Ret> {

public:

  explicit const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}

  _Ret operator()(const _Tp* __p, _Arg __x) const

    { return (__p->*_M_f)(__x); }

private:

  _Ret (_Tp::*_M_f)(_Arg) const;

};

//接受一个参数的适配器

template <class _Ret, class _Tp, class _Arg>

class mem_fun1_ref_t : public binary_function<_Tp,_Arg,_Ret> {

public:

  explicit mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}

  _Ret operator()(_Tp& __r, _Arg __x) const { return (__r.*_M_f)(__x); }

private:

  _Ret (_Tp::*_M_f)(_Arg);

};

template <class _Ret, class _Tp, class _Arg>

class const_mem_fun1_ref_t : public binary_function<_Tp,_Arg,_Ret> {

public:

  explicit const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}

  _Ret operator()(const _Tp& __r, _Arg __x) const { return (__r.*_M_f)(__x); }

private:

  _Ret (_Tp::*_M_f)(_Arg) const;

};

template <class _Tp>

class mem_fun_t<void, _Tp> : public unary_function<_Tp*,void> {

public:

  explicit mem_fun_t(void (_Tp::*__pf)()) : _M_f(__pf) {}

  void operator()(_Tp* __p) const { (__p->*_M_f)(); }

private:

  void (_Tp::*_M_f)();

};

template <class _Tp>

class const_mem_fun_t<void, _Tp> : public unary_function<const _Tp*,void> {

public:

  explicit const_mem_fun_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}

  void operator()(const _Tp* __p) const { (__p->*_M_f)(); }

private:

  void (_Tp::*_M_f)() const;

};

template <class _Tp>

class mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> {

public:

  explicit mem_fun_ref_t(void (_Tp::*__pf)()) : _M_f(__pf) {}

  void operator()(_Tp& __r) const { (__r.*_M_f)(); }

private:

  void (_Tp::*_M_f)();

};

template <class _Tp>

class const_mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> {

public:

  explicit const_mem_fun_ref_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}

  void operator()(const _Tp& __r) const { (__r.*_M_f)(); }

private:

  void (_Tp::*_M_f)() const;

};

template <class _Tp, class _Arg>

class mem_fun1_t<void, _Tp, _Arg> : public binary_function<_Tp*,_Arg,void> {

public:

  explicit mem_fun1_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}

  void operator()(_Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }

private:

  void (_Tp::*_M_f)(_Arg);

};

template <class _Tp, class _Arg>

class const_mem_fun1_t<void, _Tp, _Arg>

  : public binary_function<const _Tp*,_Arg,void> {

public:

  explicit const_mem_fun1_t(void (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}

  void operator()(const _Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }

private:

  void (_Tp::*_M_f)(_Arg) const;

};

template <class _Tp, class _Arg>

class mem_fun1_ref_t<void, _Tp, _Arg>

  : public binary_function<_Tp,_Arg,void> {

public:

  explicit mem_fun1_ref_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}

  void operator()(_Tp& __r, _Arg __x) const { (__r.*_M_f)(__x); }

private:

  void (_Tp::*_M_f)(_Arg);

};

template <class _Tp, class _Arg>

class const_mem_fun1_ref_t<void, _Tp, _Arg>

  : public binary_function<_Tp,_Arg,void> {

public:

  explicit const_mem_fun1_ref_t(void (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}

  void operator()(const _Tp& __r, _Arg __x) const { (__r.*_M_f)(__x); }

private:

  void (_Tp::*_M_f)(_Arg) const;

};

// Mem_fun adaptor helper functions.  There are only two:

//  mem_fun and mem_fun_ref.  (mem_fun1 and mem_fun1_ref

//  are provided for backward compatibility, but they are no longer

//  part of the C++ standard.)

template <class _Ret, class _Tp>

inline mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)())

  { return mem_fun_t<_Ret,_Tp>(__f); }

template <class _Ret, class _Tp>

inline const_mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)() const)

  { return const_mem_fun_t<_Ret,_Tp>(__f); }

template <class _Ret, class _Tp>

inline mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)())

  { return mem_fun_ref_t<_Ret,_Tp>(__f); }

template <class _Ret, class _Tp>

inline const_mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)() const)

  { return const_mem_fun_ref_t<_Ret,_Tp>(__f); }

template <class _Ret, class _Tp, class _Arg>

inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun(_Ret (_Tp::*__f)(_Arg))

  { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); }

template <class _Ret, class _Tp, class _Arg>

inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun(_Ret (_Tp::*__f)(_Arg) const)

  { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); }

template <class _Ret, class _Tp, class _Arg>

inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun_ref(_Ret (_Tp::*__f)(_Arg))

  { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }

template <class _Ret, class _Tp, class _Arg>

inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg>

mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)

  { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }

template <class _Ret, class _Tp, class _Arg>

inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg))

  { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); }

template <class _Ret, class _Tp, class _Arg>

inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg) const)

  { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); }

template <class _Ret, class _Tp, class _Arg>

inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun1_ref(_Ret (_Tp::*__f)(_Arg))

  { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }

template <class _Ret, class _Tp, class _Arg>

inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg>

mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const)

  { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }

//对上面各个函数对象的简化包装