我的函数包装器

#pragma once

#include "KRYObject.h"

template<typename R> class KRYFunction{};
template<typename R>
class KRYFunction < R() >
{
private:
    class FunctionBase
    {
    public:
        virtual ~FunctionBase(){}
        virtual R operator ()() = 0;
        virtual bool able() = 0;
    };
    class GlobalFunction :public FunctionBase
    {
    public:
        GlobalFunction(R(*handler)()) { m_handler = handler; }
        bool able() { return 0 != m_handler; }
        R operator ()() { return m_handler(); }
    private:
        R(*m_handler)();
    };
    template<typename C>
    class MemberFunction :public FunctionBase
    {
    public:
        MemberFunction(KRYWeakPtr<C> target, R(C::*handler)())
        {
            m_target = target;
            m_handler = handler;
        }
        bool able() { return m_target && 0 != m_handler; }
        R operator ()()
        {
            C *ptr = m_target.getPtr();
            return (ptr->*m_handler)();
        }
    private:
        KRYWeakPtr<C> m_target;
        R(C::*m_handler)();
    };
private:
    FunctionBase *m_fun;
public:
    KRYFunction() { m_fun = 0; }
    ~KRYFunction() { if (0 != m_fun){ delete m_fun; m_fun = 0; } }
    operator bool(){ return (0 != m_fun && m_fun->able()); }
    R operator ()()
    {
        return (*m_fun)();
    }
    template<typename C>
    void set(C* target, R(C::*handler)())
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new MemberFunction<C>(target, handler);
    }
    void set(R(*handler)())
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new GlobalFunction(handler);
    }
};

template<typename R, typename P1>
class KRYFunction < R(P1) >
{
private:
    class FunctionBase
    {
    public:
        virtual ~FunctionBase(){}
        virtual R operator ()(P1) = 0;
        virtual bool able() = 0;
    };
    class GlobalFunction :public FunctionBase
    {
    public:
        GlobalFunction(R(*handler)(P1)) { m_handler = handler; }
        bool able() { return 0 != m_handler; }
        R operator ()(P1 p1) { return m_handler(p1); }
    private:
        R(*m_handler)(P1);
    };
    template<typename C>
    class MemberFunction :public FunctionBase
    {
    public:
        MemberFunction(KRYWeakPtr<C> target, R(C::*handler)(P1))
        {
            m_target = target;
            m_handler = handler;
        }
        bool able() { return m_target && 0 != m_handler; }
        R operator ()(P1 p1)
        {
            C *ptr = m_target.getPtr();
            return (ptr->*m_handler)(p1);
        }
    private:
        KRYWeakPtr<C> m_target;
        R(C::*m_handler)(P1);
    };
private:
    FunctionBase *m_fun;
public:
    KRYFunction() { m_fun = 0; }
    ~KRYFunction() { if (0 != m_fun){ delete m_fun; m_fun = 0; } }
    operator bool(){ return (0 != m_fun && m_fun->able()); }
    R operator ()(P1 p1)
    {
        return (*m_fun)(p1);
    }
    template<typename C>
    void set(C* target, R(C::*handler)(P1))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new MemberFunction<C>(target, handler);
    }
    void set(R(*handler)(P1))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new GlobalFunction(handler);
    }
};

template<typename R, typename P1, typename P2>
class KRYFunction < R(P1, P2) >
{
private:
    class FunctionBase
    {
    public:
        virtual ~FunctionBase(){}
        virtual R operator ()(P1, P2) = 0;
        virtual bool able() = 0;
    };
    class GlobalFunction :public FunctionBase
    {
    public:
        GlobalFunction(R(*handler)(P1, P2)) { m_handler = handler; }
        bool able() { return 0 != m_handler; }
        R operator ()(P1 p1, P2 p2) { return m_handler(p1, p2); }
    private:
        R(*m_handler)(P1, P2);
    };
    template<typename C>
    class MemberFunction :public FunctionBase
    {
    public:
        MemberFunction(KRYWeakPtr<C> target, R(C::*handler)(P1, P2))
        {
            m_target = target;
            m_handler = handler;
        }
        bool able() { return m_target && 0 != m_handler; }
        R operator ()(P1 p1, P2 p2)
        {
            C *ptr = m_target.getPtr();
            return (ptr->*m_handler)(p1, p2);
        }
    private:
        KRYWeakPtr<C> m_target;
        R(C::*m_handler)(P1, P2);
    };
private:
    FunctionBase *m_fun;
public:
    KRYFunction() { m_fun = 0; }
    ~KRYFunction() { if (0 != m_fun){ delete m_fun; m_fun = 0; } }
    operator bool(){ return (0 != m_fun && m_fun->able()); }
    R operator ()(P1 p1, P2 p2)
    {
        return (*m_fun)(p1, p2);
    }
    template<typename C>
    void set(C* target, R(C::*handler)(P1, P2))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new MemberFunction<C>(target, handler);
    }
    void set(R(*handler)(P1, P2))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new GlobalFunction(handler);
    }
};

template<typename R, typename P1, typename P2, typename P3>
class KRYFunction < R(P1, P2, P3) >
{
private:
    class FunctionBase
    {
    public:
        virtual ~FunctionBase(){}
        virtual R operator ()(P1, P2, P3) = 0;
        virtual bool able() = 0;
    };
    class GlobalFunction :public FunctionBase
    {
    public:
        GlobalFunction(R(*handler)(P1, P2, P3)) { m_handler = handler; }
        bool able() { return 0 != m_handler; }
        R operator ()(P1 p1, P2 p2, P3 p3) { return m_handler(p1, p2, p3); }
    private:
        R(*m_handler)(P1, P2, P3);
    };
    template<typename C>
    class MemberFunction :public FunctionBase
    {
    public:
        MemberFunction(KRYWeakPtr<C> target, R(C::*handler)(P1, P2, P3))
        {
            m_target = target;
            m_handler = handler;
        }
        bool able() { return m_target && 0 != m_handler; }
        R operator ()(P1 p1, P2 p2, P3 p3)
        {
            C *ptr = m_target.getPtr();
            return (ptr->*m_handler)(p1, p2, p3);
        }
    private:
        KRYWeakPtr<C> m_target;
        R(C::*m_handler)(P1, P2, P3);
    };
private:
    FunctionBase *m_fun;
public:
    KRYFunction() { m_fun = 0; }
    ~KRYFunction() { if (0 != m_fun){ delete m_fun; m_fun = 0; } }
    operator bool(){ return (0 != m_fun && m_fun->able()); }
    R operator ()(P1 p1, P2 p2, P3 p3)
    {
        return (*m_fun)(p1, p2, p3);
    }
    template<typename C>
    void set(C* target, R(C::*handler)(P1, P2, P3))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new MemberFunction<C>(target, handler);
    }
    void set(R(*handler)(P1, P2, P3))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new GlobalFunction(handler);
    }
};

template<typename R, typename P1, typename P2, typename P3, typename P4>
class KRYFunction < R(P1, P2, P3, P4) >
{
private:
    class FunctionBase
    {
    public:
        virtual ~FunctionBase(){}
        virtual R operator ()(P1, P2, P3, P4) = 0;
        virtual bool able() = 0;
    };
    class GlobalFunction :public FunctionBase
    {
    public:
        GlobalFunction(R(*handler)(P1, P2, P3, P4)) { m_handler = handler; }
        bool able() { return 0 != m_handler; }
        R operator ()(P1 p1, P2 p2, P3 p3, P4 p4) { return m_handler(p1, p2, p3, p4); }
    private:
        R(*m_handler)(P1, P2, P3, P4);
    };
    template<typename C>
    class MemberFunction :public FunctionBase
    {
    public:
        MemberFunction(KRYWeakPtr<C> target, R(C::*handler)(P1, P2, P3, P4))
        {
            m_target = target;
            m_handler = handler;
        }
        bool able() { return m_target && 0 != m_handler; }
        R operator ()(P1 p1, P2 p2, P3 p3, P4 p4)
        {
            C *ptr = m_target.getPtr();
            return (ptr->*m_handler)(p1, p2, p3, p4);
        }
    private:
        KRYWeakPtr<C> m_target;
        R(C::*m_handler)(P1, P2, P3, P4);
    };
private:
    FunctionBase *m_fun;
public:
    KRYFunction() { m_fun = 0; }
    ~KRYFunction() { if (0 != m_fun){ delete m_fun; m_fun = 0; } }
    operator bool(){ return (0 != m_fun && m_fun->able()); }
    R operator ()(P1 p1, P2 p2, P3 p3, P4 p4)
    {
        return (*m_fun)(p1, p2, p3, p4);
    }
    template<typename C>
    void set(C* target, R(C::*handler)(P1, P2, P3, P4))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new MemberFunction<C>(target, handler);
    }
    void set(R(*handler)(P1, P2, P3, P4))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new GlobalFunction(handler);
    }
};

template<typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
class KRYFunction < R(P1, P2, P3, P4, P5) >
{
private:
    class FunctionBase
    {
    public:
        virtual ~FunctionBase(){}
        virtual R operator ()(P1, P2, P3, P4, P5) = 0;
        virtual bool able() = 0;
    };
    class GlobalFunction :public FunctionBase
    {
    public:
        GlobalFunction(R(*handler)(P1, P2, P3, P4, P5)) { m_handler = handler; }
        bool able() { return 0 != m_handler; }
        R operator ()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { return m_handler(p1, p2, p3, p4, p5); }
    private:
        R(*m_handler)(P1, P2, P3, P4, P5);
    };
    template<typename C>
    class MemberFunction :public FunctionBase
    {
    public:
        MemberFunction(KRYWeakPtr<C> target, R(C::*handler)(P1, P2, P3, P4, P5))
        {
            m_target = target;
            m_handler = handler;
        }
        bool able() { return m_target && 0 != m_handler; }
        R operator ()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
        {
            C *ptr = m_target.getPtr();
            return (ptr->*m_handler)(p1, p2, p3, p4, p5);
        }
    private:
        KRYWeakPtr<C> m_target;
        R(C::*m_handler)(P1, P2, P3, P4, P5);
    };
private:
    FunctionBase *m_fun;
public:
    KRYFunction() { m_fun = 0; }
    ~KRYFunction() { if (0 != m_fun){ delete m_fun; m_fun = 0; } }
    operator bool(){ return (0 != m_fun && m_fun->able()); }
    R operator ()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
    {
        return (*m_fun)(p1, p2, p3, p4, p5);
    }
    template<typename C>
    void set(C* target, R(C::*handler)(P1, P2, P3, P4, P5))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new MemberFunction<C>(target, handler);
    }
    void set(R(*handler)(P1, P2, P3, P4, P5))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new GlobalFunction(handler);
    }
};

template<typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
class KRYFunction < R(P1, P2, P3, P4, P5, P6) >
{
private:
    class FunctionBase
    {
    public:
        virtual ~FunctionBase(){}
        virtual R operator ()(P1, P2, P3, P4, P5, P6) = 0;
        virtual bool able() = 0;
    };
    class GlobalFunction :public FunctionBase
    {
    public:
        GlobalFunction(R(*handler)(P1, P2, P3, P4, P5, P6)) { m_handler = handler; }
        bool able() { return 0 != m_handler; }
        R operator ()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) { return m_handler(p1, p2, p3, p4, p5, p6); }
    private:
        R(*m_handler)(P1, P2, P3, P4, P5, P6);
    };
    template<typename C>
    class MemberFunction :public FunctionBase
    {
    public:
        MemberFunction(KRYWeakPtr<C> target, R(C::*handler)(P1, P2, P3, P4, P5, P6))
        {
            m_target = target;
            m_handler = handler;
        }
        bool able() { return m_target && 0 != m_handler; }
        R operator ()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
        {
            C *ptr = m_target.getPtr();
            return (ptr->*m_handler)(p1, p2, p3, p4, p5, p6);
        }
    private:
        KRYWeakPtr<C> m_target;
        R(C::*m_handler)(P1, P2, P3, P4, P5, P6);
    };
private:
    FunctionBase *m_fun;
public:
    KRYFunction() { m_fun = 0; }
    ~KRYFunction() { if (0 != m_fun){ delete m_fun; m_fun = 0; } }
    operator bool(){ return (0 != m_fun && m_fun->able()); }
    R operator ()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
    {
        return (*m_fun)(p1, p2, p3, p4, p5, p6);
    }
    template<typename C>
    void set(C* target, R(C::*handler)(P1, P2, P3, P4, P5, P6))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new MemberFunction<C>(target, handler);
    }
    void set(R(*handler)(P1, P2, P3, P4, P5, P6))
    {
        if (0 != m_fun){ delete m_fun; m_fun = 0; }
        m_fun = new GlobalFunction(handler);
    }
};
View Code
void gfun0()
{
cout << "gfun0" << endl;
}
void gfun1(int)
{
cout << "gfun1" << endl;
}

class Base:public KRYObject
{
public:
void fun0()
{
cout << "Base::fun0" << endl;
}
void fun1(int)
{
cout << "Base::fun1" << endl;
}
};
void main()
{
    KRYRefPtr<Base> ptr = new Base();
    KRYFunction<void()> fun0;
    fun0.set(gfun0); if(fun0)fun0();
    fun0.set(ptr.getPtr(), &Base::fun0); if (fun0)fun0();

    KRYFunction<void(int)> fun1;
    fun1.set(gfun1); if(fun1)fun1(1);
    fun1.set(ptr.getPtr(), &Base::fun1); if (fun1)fun1(1);
}

 

posted @ 2015-10-15 20:57  liusijian  阅读(165)  评论(0编辑  收藏  举报