⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functorold.h

📁 loki库的源代码。loki库是以模板技术和面向对象技术为基础的c++类库。
💻 H
📖 第 1 页 / 共 5 页
字号:
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2001 by Andrei Alexandrescu
// This code accompanies the book:
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
//     Patterns Applied". Copyright (c) 2001. Addison-Wesley.
// Permission to use, copy, modify, distribute and sell this software for any
//     purpose is hereby granted without fee, provided that the above copyright
//     notice appear in all copies and that both that copyright notice and this
//     permission notice appear in supporting documentation.
// The author or Addison-Welsey Longman make no representations about the
//     suitability of this software for any purpose. It is provided "as is"
//     without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////

// Last update: Feb 25, 2003
//
// Functor's Template-Ctor now has a Loki::Disambiguate parameter.
// Use it, when the VC complains about an ambiguity. For example if you
// want to create a functor with a functor of different but compatible type.
// Functor<R, Arguments> Fun(AnotherFun, Disambiguate());
//
// Changed BindFirst from
// typename Private::BinderFirstTraits<Fctor>::BoundFunctorType
// BindFirst(const Fctor& fun, typename Fctor::Parm1 bound);
// to
// template <class R, class TList, class Thread>
// Functor<R, Private::BinderFirstTraitsHelper<TList>::ParmList, Thread>
// BindFirst(const Functor<R, TList, Thread>& fun, Functor<R, TList, Thread>::Parm1 bound)
// The old return type was to complicated for the VC and responsible for
// some C1001-Internal Compiler errors.
//
// All functors will return an object of the udt VoidAsType if the template
// parameter R is void.
// *Do not* use this returned object. It is only used as a workaround.

#ifndef FUNCTOR_INC_
#define FUNCTOR_INC_

#include "Typelist.h"
#include "EmptyType.h"
#include "SmallObj.h"
#include "TypeTraits.h"
#include <typeinfo>
#include <memory>
#include "MSVC6Helpers.h"
namespace Loki
{
////////////////////////////////////////////////////////////////////////////////
// class template FunctorImpl (internal)
////////////////////////////////////////////////////////////////////////////////

    namespace Private
    {
    template <typename R, class ThreadingModel>
    struct FunctorImplBase : public SmallObject<ThreadingModel>
    {
        typedef R ResultType;
        typedef typename Select
		<
			IsVoid<R>::value != 0,
			VoidAsType,
			R
		>::Result NewRType;
        typedef EmptyType Parm1;
        typedef EmptyType Parm2;
        typedef EmptyType Parm3;
        typedef EmptyType Parm4;
        typedef EmptyType Parm5;
        typedef EmptyType Parm6;
        typedef EmptyType Parm7;
        typedef EmptyType Parm8;
        typedef EmptyType Parm9;
        typedef EmptyType Parm10;
        typedef EmptyType Parm11;
        typedef EmptyType Parm12;
        typedef EmptyType Parm13;
        typedef EmptyType Parm14;
        typedef EmptyType Parm15;

        virtual FunctorImplBase<R, ThreadingModel>* DoClone() const = 0;
        template <class U>
        static U* Clone(U* pObj)
        {
            if (!pObj) return 0;
			U* pClone = static_cast<U*>(pObj->DoClone());
			assert(typeid(*pClone) == typeid(*pObj));
            return pClone;
        }
		virtual ~FunctorImplBase () {}
    };

////////////////////////////////////////////////////////////////////////////////
// macro DEFINE_CLONE_FUNCTORIMPL
// Implements the DoClone function for a functor implementation
////////////////////////////////////////////////////////////////////////////////

#define DEFINE_CLONE_FUNCTORIMPL(Cls) \
    virtual Private::FunctorImplBase<ResultType, ThreadModel>* \
	DoClone() const { return new Cls(*this); }


////////////////////////////////////////////////////////////////////////////////
// class template FunctorImplHelper
// Base template
////////////////////////////////////////////////////////////////////////////////
    template <unsigned int TListLength>
    struct FunctorImplHelper;

////////////////////////////////////////////////////////////////////////////////
// class template FunctorImplHelper
// Specialization for 0 (zero) parameters
////////////////////////////////////////////////////////////////////////////////

    template <>
    struct FunctorImplHelper<0>
    {
        template <typename R, class TList, class ThreadingModel>
        class In : public Private::FunctorImplBase<R, ThreadingModel>
        {
        public:
            typedef R ResultType;
			typedef typename Select
			<
				IsVoid<R>::value != 0,
				VoidAsType,
				R
			>::Result NewRType;
            virtual NewRType operator()() = 0;
        };
    };


////////////////////////////////////////////////////////////////////////////////
// class template FunctorImplHelper
// Specialization for 1 parameter
////////////////////////////////////////////////////////////////////////////////

    template <>
    struct FunctorImplHelper<1>
    {
        template <typename R, class TList, class ThreadingModel>
        class In : public Private::FunctorImplBase<R, ThreadingModel>
        {
            typedef typename TL::TypeAt<TList, 0>::Result P1;

        public:
            typedef R ResultType;
			typedef typename Select
			<
				IsVoid<R>::value != 0,
				VoidAsType,
				R
			>::Result NewRType;
            typedef typename TypeTraits<P1>::ParameterType Parm1;
            virtual NewRType operator()(Parm1) = 0;
        };
    };


////////////////////////////////////////////////////////////////////////////////
// class template FunctorImplHelper
// Specialization for 2 parameters
////////////////////////////////////////////////////////////////////////////////
   template <>
   struct FunctorImplHelper<2>
   {
       template <typename R, class TList, class ThreadingModel>
       class In : public Private::FunctorImplBase<R, ThreadingModel>
       {
           typedef typename TL::TypeAt<TList, 0>::Result P1;
           typedef typename TL::TypeAt<TList, 1>::Result P2;

       public:
           typedef R ResultType;
		   typedef typename Select
			<
				IsVoid<R>::value != 0,
				VoidAsType,
				R
			>::Result NewRType;
           typedef typename TypeTraits<P1>::ParameterType Parm1;
           typedef typename TypeTraits<P2>::ParameterType Parm2;
           virtual NewRType operator()(Parm1, Parm2) = 0;
       };
   };
////////////////////////////////////////////////////////////////////////////////
// class template FunctorImplHelper
// Specialization for 3 parameters
////////////////////////////////////////////////////////////////////////////////
   template <>
   struct FunctorImplHelper<3>
   {
       template <typename R, class TList, class ThreadingModel>
       class In : public Private::FunctorImplBase<R, ThreadingModel>
       {
           typedef typename TL::TypeAt<TList, 0>::Result P1;
           typedef typename TL::TypeAt<TList, 1>::Result P2;
           typedef typename TL::TypeAt<TList, 2>::Result P3;

       public:
           typedef R ResultType;
           typedef typename Select
			<
				IsVoid<R>::value != 0,
				VoidAsType,
				R
			>::Result NewRType;
		   typedef typename TypeTraits<P1>::ParameterType Parm1;
           typedef typename TypeTraits<P2>::ParameterType Parm2;
           typedef typename TypeTraits<P3>::ParameterType Parm3;
           virtual NewRType operator()(Parm1, Parm2, Parm3) = 0;
       };
   };
////////////////////////////////////////////////////////////////////////////////
// class template FunctorImplHelper
// Specialization for 4 parameters
////////////////////////////////////////////////////////////////////////////////
   template <>
   struct FunctorImplHelper<4>
   {
       template <typename R, class TList, class ThreadingModel>
       class In : public Private::FunctorImplBase<R, ThreadingModel>
       {
           typedef typename TL::TypeAt<TList, 0>::Result P1;
           typedef typename TL::TypeAt<TList, 1>::Result P2;
           typedef typename TL::TypeAt<TList, 2>::Result P3;
           typedef typename TL::TypeAt<TList, 3>::Result P4;

       public:
           typedef R ResultType;
           typedef typename Select
			<
				IsVoid<R>::value != 0,
				VoidAsType,
				R
			>::Result NewRType;
		   typedef typename TypeTraits<P1>::ParameterType Parm1;
           typedef typename TypeTraits<P2>::ParameterType Parm2;
           typedef typename TypeTraits<P3>::ParameterType Parm3;
           typedef typename TypeTraits<P4>::ParameterType Parm4;
           virtual NewRType operator()(Parm1, Parm2, Parm3, Parm4) = 0;
       };
   };
////////////////////////////////////////////////////////////////////////////////
// class template FunctorImplHelper
// Specialization for 5 parameters
////////////////////////////////////////////////////////////////////////////////
   template <>
   struct FunctorImplHelper<5>
   {
       template <typename R, class TList, class ThreadingModel>
       class In : public Private::FunctorImplBase<R, ThreadingModel>
       {
           typedef typename TL::TypeAt<TList, 0>::Result P1;
           typedef typename TL::TypeAt<TList, 1>::Result P2;
           typedef typename TL::TypeAt<TList, 2>::Result P3;
           typedef typename TL::TypeAt<TList, 3>::Result P4;
           typedef typename TL::TypeAt<TList, 4>::Result P5;

       public:
           typedef R ResultType;
		   typedef typename Select
			<
				IsVoid<R>::value != 0,
				VoidAsType,
				R
			>::Result NewRType;
           typedef typename TypeTraits<P1>::ParameterType Parm1;
           typedef typename TypeTraits<P2>::ParameterType Parm2;
           typedef typename TypeTraits<P3>::ParameterType Parm3;
           typedef typename TypeTraits<P4>::ParameterType Parm4;
           typedef typename TypeTraits<P5>::ParameterType Parm5;
           virtual NewRType operator()(Parm1, Parm2, Parm3, Parm4, Parm5) = 0;
       };
   };
////////////////////////////////////////////////////////////////////////////////
// class template FunctorImplHelper
// Specialization for 6 parameters
////////////////////////////////////////////////////////////////////////////////
   template <>
   struct FunctorImplHelper<6>
   {
       template <typename R, class TList, class ThreadingModel>
       class In : public Private::FunctorImplBase<R, ThreadingModel>
       {
           typedef typename TL::TypeAt<TList, 0>::Result P1;
           typedef typename TL::TypeAt<TList, 1>::Result P2;
           typedef typename TL::TypeAt<TList, 2>::Result P3;
           typedef typename TL::TypeAt<TList, 3>::Result P4;
           typedef typename TL::TypeAt<TList, 4>::Result P5;
           typedef typename TL::TypeAt<TList, 5>::Result P6;

       public:
           typedef R ResultType;
           typedef typename Select
			<
				IsVoid<R>::value != 0,
				VoidAsType,
				R
			>::Result NewRType;
		   typedef typename TypeTraits<P1>::ParameterType Parm1;
           typedef typename TypeTraits<P2>::ParameterType Parm2;
           typedef typename TypeTraits<P3>::ParameterType Parm3;
           typedef typename TypeTraits<P4>::ParameterType Parm4;
           typedef typename TypeTraits<P5>::ParameterType Parm5;
           typedef typename TypeTraits<P6>::ParameterType Parm6;
           virtual NewRType operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6) = 0;
       };
   };
////////////////////////////////////////////////////////////////////////////////
// class template FunctorImplHelper
// Specialization for 7 parameters
////////////////////////////////////////////////////////////////////////////////
   template <>
   struct FunctorImplHelper<7>
   {
       template <typename R, class TList, class ThreadingModel>
       class In : public Private::FunctorImplBase<R, ThreadingModel>
       {
           typedef typename TL::TypeAt<TList, 0>::Result P1;
           typedef typename TL::TypeAt<TList, 1>::Result P2;
           typedef typename TL::TypeAt<TList, 2>::Result P3;
           typedef typename TL::TypeAt<TList, 3>::Result P4;
           typedef typename TL::TypeAt<TList, 4>::Result P5;
           typedef typename TL::TypeAt<TList, 5>::Result P6;
           typedef typename TL::TypeAt<TList, 6>::Result P7;

       public:
           typedef R ResultType;
		   typedef typename Select
			<
				IsVoid<R>::value != 0,
				VoidAsType,
				R
			>::Result NewRType;
           typedef typename TypeTraits<P1>::ParameterType Parm1;
           typedef typename TypeTraits<P2>::ParameterType Parm2;
           typedef typename TypeTraits<P3>::ParameterType Parm3;
           typedef typename TypeTraits<P4>::ParameterType Parm4;
           typedef typename TypeTraits<P5>::ParameterType Parm5;
           typedef typename TypeTraits<P6>::ParameterType Parm6;
           typedef typename TypeTraits<P7>::ParameterType Parm7;
           virtual NewRType operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6, Parm7) = 0;
       };
   };
////////////////////////////////////////////////////////////////////////////////
// class template FunctorImplHelper
// Specialization for 8 parameters
////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -