Previous 199869 Revisions Next

r18549 Monday 15th October, 2012 at 20:18:32 UTC by Aaron Giles
Intermediate delegate WIP as I shuffle things around.
This is just a point-in-time, "it compiles and seems to work
like before" checkpoint.
[src/emu]delegate.h

trunk/src/emu/delegate.h
r18548r18549
430430      return reinterpret_cast<delegate_generic_class *>(result);
431431   }
432432
433   // internal state
434   const char *            m_name;            // name string
435   delegate_generic_class *   m_object;         // pointer to the post-cast object
436   late_bind_func            m_latebinder;      // late binding helper
437   delegate_generic_function   m_raw_function;      // raw static function pointer
438   delegate_mfp            m_raw_mfp;         // raw member function pointer
439};
440
441
442
443//**************************************************************************
444//  COMPATIBLE DELEGATES
445//**************************************************************************
446
447433#if (USE_DELEGATE_TYPE == DELEGATE_TYPE_COMPATIBLE)
448
449// ======================> delegate_base
450
451// general delegate class template supporting up to 4 parameters
452template<typename _ReturnType, typename _P1Type = _noparam, typename _P2Type = _noparam, typename _P3Type = _noparam, typename _P4Type = _noparam, typename _P5Type = _noparam>
453class delegate_base : public delegate_common_base
454{
455public:
456   // define our traits
457   template<class _FunctionClass>
458   struct traits
459   {
460      typedef typename delegate_traits<_FunctionClass, _ReturnType, _P1Type, _P2Type, _P3Type, _P4Type, _P5Type>::member_func_type member_func_type;
461      typedef typename delegate_traits<_FunctionClass, _ReturnType, _P1Type, _P2Type, _P3Type, _P4Type, _P5Type>::static_func_type static_func_type;
462      typedef typename delegate_traits<_FunctionClass, _ReturnType, _P1Type, _P2Type, _P3Type, _P4Type, _P5Type>::static_ref_func_type static_ref_func_type;
463   };
464   typedef typename traits<delegate_generic_class>::static_func_type generic_static_func;
465
466   // generic constructor
467   delegate_base()
468      : m_function(NULL),
469        m_callobject(NULL) { }
470
471   // copy constructor
472   delegate_base(const delegate_base &src)
473      : delegate_common_base(src),
474        m_function(src.m_function),
475        m_callobject(src.is_mfp() ? reinterpret_cast<delegate_generic_class *>(this) : src.m_object) { }
476
477   // copy constructor with late bind
478   delegate_base(const delegate_base &src, delegate_late_bind &object)
479      : delegate_common_base(src),
480        m_function(src.m_function),
481        m_callobject(src.is_mfp() ? reinterpret_cast<delegate_generic_class *>(this) : src.m_object)
482   {
483      late_bind(object);
484   }
485
486   // construct from member function with object pointer
487   template<class _FunctionClass>
488   delegate_base(typename traits<_FunctionClass>::member_func_type funcptr, const char *name, _FunctionClass *object)
489      : delegate_common_base(name, &late_bind_helper<_FunctionClass>, funcptr),
490        m_function(&delegate_base::method_stub<_FunctionClass>),
491        m_callobject(reinterpret_cast<delegate_generic_class *>(this))
492   {
493      bind(reinterpret_cast<delegate_generic_class *>(object));
494   }
495
496   // construct from static function with object pointer
497   template<class _FunctionClass>
498   delegate_base(typename traits<_FunctionClass>::static_func_type funcptr, const char *name, _FunctionClass *object)
499      : delegate_common_base(name, &late_bind_helper<_FunctionClass>, reinterpret_cast<delegate_generic_function>(funcptr)),
500        m_function(reinterpret_cast<generic_static_func>(funcptr)),
501        m_callobject(NULL)
502   {
503      bind(reinterpret_cast<delegate_generic_class *>(object));
504   }
505
506   // construct from static reference function with object pointer
507   template<class _FunctionClass>
508   delegate_base(typename traits<_FunctionClass>::static_ref_func_type funcptr, const char *name, _FunctionClass *object)
509      : delegate_common_base(name, &late_bind_helper<_FunctionClass>, reinterpret_cast<delegate_generic_function>(funcptr)),
510        m_function(reinterpret_cast<generic_static_func>(funcptr)),
511        m_callobject(NULL)
512   {
513      bind(reinterpret_cast<delegate_generic_class *>(object));
514   }
515
516   // copy operator
517   delegate_base &operator=(const delegate_base &src)
518   {
519      if (this != &src)
520      {
521         delegate_common_base::copy(src);
522         m_callobject = src.is_mfp() ? reinterpret_cast<delegate_generic_class *>(this) : src.m_object;
523         m_function = src.m_function;
524      }
525      return *this;
526   }
527
528   // call the function
529   _ReturnType operator()() const { return (*m_function)(m_callobject); }
530   _ReturnType operator()(_P1Type p1) const { return (*m_function)(m_callobject, p1); }
531   _ReturnType operator()(_P1Type p1, _P2Type p2) const { return (*m_function)(m_callobject, p1, p2); }
532   _ReturnType operator()(_P1Type p1, _P2Type p2, _P3Type p3) const { return (*m_function)(m_callobject, p1, p2, p3); }
533   _ReturnType operator()(_P1Type p1, _P2Type p2, _P3Type p3, _P4Type p4) const { return (*m_function)(m_callobject, p1, p2, p3, p4); }
534   _ReturnType operator()(_P1Type p1, _P2Type p2, _P3Type p3, _P4Type p4, _P5Type p5) const { return (*m_function)(m_callobject, p1, p2, p3, p4, p5); }
535
536   // late binding
537   void late_bind(delegate_late_bind &object) { bind((*m_latebinder)(object)); }
538
539protected:
540   // bind the actual object
541   void bind(delegate_generic_class *object)
542   {
543      m_object = object;
544      if (!is_mfp()) m_callobject = m_object;
545   }
546
547   // internal helpers
548   bool is_mfp() const { return m_callobject == reinterpret_cast<const delegate_generic_class *>(this); }
549
550434   // helper stub that calls the member function; we need one for each parameter count
551   template<class _FunctionClass>
435   template<class _FunctionClass, typename _ReturnType>
552436    static _ReturnType method_stub(delegate_generic_class *object)
553437    {
554438       typedef _ReturnType (_FunctionClass::*mfptype)();
555       delegate_base *_this = reinterpret_cast<delegate_base *>(object);
439       delegate_common_base *_this = reinterpret_cast<delegate_common_base *>(object);
556440       mfptype &mfp = _this->m_raw_mfp.mfp<mfptype>();
557441       return (reinterpret_cast<_FunctionClass *>(_this->m_object)->*mfp)();
558442    }
559443
560   template<class _FunctionClass>
444   template<class _FunctionClass, typename _ReturnType, typename _P1Type>
561445    static _ReturnType method_stub(delegate_generic_class *object, _P1Type p1)
562446    {
563447       typedef _ReturnType (_FunctionClass::*mfptype)(_P1Type p1);
564       delegate_base *_this = reinterpret_cast<delegate_base *>(object);
448       delegate_common_base *_this = reinterpret_cast<delegate_common_base *>(object);
565449       mfptype &mfp = _this->m_raw_mfp.mfp<mfptype>();
566450       return (reinterpret_cast<_FunctionClass *>(_this->m_object)->*mfp)(p1);
567451    }
568452
569   template<class _FunctionClass>
453   template<class _FunctionClass, typename _ReturnType, typename _P1Type, typename _P2Type>
570454    static _ReturnType method_stub(delegate_generic_class *object, _P1Type p1, _P2Type p2)
571455    {
572456       typedef _ReturnType (_FunctionClass::*mfptype)(_P1Type p1, _P2Type p2);
573       delegate_base *_this = reinterpret_cast<delegate_base *>(object);
457       delegate_common_base *_this = reinterpret_cast<delegate_common_base *>(object);
574458       mfptype &mfp = _this->m_raw_mfp.mfp<mfptype>();
575459       return (reinterpret_cast<_FunctionClass *>(_this->m_object)->*mfp)(p1, p2);
576460    }
577461
578   template<class _FunctionClass>
462   template<class _FunctionClass, typename _ReturnType, typename _P1Type, typename _P2Type, typename _P3Type>
579463    static _ReturnType method_stub(delegate_generic_class *object, _P1Type p1, _P2Type p2, _P3Type p3)
580464    {
581465       typedef _ReturnType (_FunctionClass::*mfptype)(_P1Type p1, _P2Type p2, _P3Type p3);
582       delegate_base *_this = reinterpret_cast<delegate_base *>(object);
466       delegate_common_base *_this = reinterpret_cast<delegate_common_base *>(object);
583467       mfptype &mfp = _this->m_raw_mfp.mfp<mfptype>();
584468       return (reinterpret_cast<_FunctionClass *>(_this->m_object)->*mfp)(p1, p2, p3);
585469    }
586470
587   template<class _FunctionClass>
471   template<class _FunctionClass, typename _ReturnType, typename _P1Type, typename _P2Type, typename _P3Type, typename _P4Type>
588472    static _ReturnType method_stub(delegate_generic_class *object, _P1Type p1, _P2Type p2, _P3Type p3, _P4Type p4)
589473    {
590474       typedef _ReturnType (_FunctionClass::*mfptype)(_P1Type p1, _P2Type p2, _P3Type p3, _P4Type p4);
591       delegate_base *_this = reinterpret_cast<delegate_base *>(object);
475       delegate_common_base *_this = reinterpret_cast<delegate_common_base *>(object);
592476       mfptype &mfp = _this->m_raw_mfp.mfp<mfptype>();
593477       return (reinterpret_cast<_FunctionClass *>(_this->m_object)->*mfp)(p1, p2, p3, p4);
594478    }
595479
596   template<class _FunctionClass>
480   template<class _FunctionClass, typename _ReturnType, typename _P1Type, typename _P2Type, typename _P3Type, typename _P4Type, typename _P5Type>
597481    static _ReturnType method_stub(delegate_generic_class *object, _P1Type p1, _P2Type p2, _P3Type p3, _P4Type p4, _P5Type p5)
598482    {
599483       typedef _ReturnType (_FunctionClass::*mfptype)(_P1Type p1, _P2Type p2, _P3Type p3, _P4Type p4, _P5Type p5);
600       delegate_base *_this = reinterpret_cast<delegate_base *>(object);
484       delegate_common_base *_this = reinterpret_cast<delegate_common_base *>(object);
601485       mfptype &mfp = _this->m_raw_mfp.mfp<mfptype>();
602486       return (reinterpret_cast<_FunctionClass *>(_this->m_object)->*mfp)(p1, p2, p3, p4, p5);
603487    }
488#endif
604489
605490   // internal state
606   generic_static_func         m_function;         // generic static function pointer
607   delegate_generic_class *   m_callobject;      // pointer to the object used for calling
491   const char *            m_name;            // name string
492   delegate_generic_class *   m_object;         // pointer to the post-cast object
493   late_bind_func            m_latebinder;      // late binding helper
494   delegate_generic_function   m_raw_function;      // raw static function pointer
495   delegate_mfp            m_raw_mfp;         // raw member function pointer
608496};
609497
610#endif
611498
612499
613500//**************************************************************************
614//  GCC DELEGATES
501//  TEMPLATIZED DELEGATE BASE
615502//**************************************************************************
616503
617#if (USE_DELEGATE_TYPE == DELEGATE_TYPE_INTERNAL)
618
619504// ======================> delegate_base
620505
506// general delegate class template supporting up to 4 parameters
621507template<typename _ReturnType, typename _P1Type = _noparam, typename _P2Type = _noparam, typename _P3Type = _noparam, typename _P4Type = _noparam, typename _P5Type = _noparam>
622508class delegate_base : public delegate_common_base
623509{
510#if (USE_DELEGATE_TYPE == DELEGATE_TYPE_COMPATIBLE)
511   delegate_generic_class *copy_callobject(const delegate_base &src) { return src.is_mfp() ? reinterpret_cast<delegate_generic_class *>(this) : src.m_object; }
512#else
513   delegate_generic_class *copy_callobject(const delegate_base &src) { return src.m_callobject; }
514#endif
515
624516public:
625517   // define our traits
626518   template<class _FunctionClass>
r18548r18549
634526
635527   // generic constructor
636528   delegate_base()
637      : m_function(NULL) { }
529      : m_function(NULL),
530        m_callobject(NULL) { }
638531
639532   // copy constructor
640533   delegate_base(const delegate_base &src)
641534      : delegate_common_base(src),
642        m_function(src.m_function) { }
535        m_function(src.m_function),
536        m_callobject(copy_callobject(src)) { }
643537
644538   // copy constructor with late bind
645539   delegate_base(const delegate_base &src, delegate_late_bind &object)
646540      : delegate_common_base(src),
647        m_function(src.m_function)
541        m_function(src.m_function),
542        m_callobject(copy_callobject(src))
648543   {
649544      late_bind(object);
650545   }
r18548r18549
653548   template<class _FunctionClass>
654549   delegate_base(typename traits<_FunctionClass>::member_func_type funcptr, const char *name, _FunctionClass *object)
655550      : delegate_common_base(name, &late_bind_helper<_FunctionClass>, funcptr),
551#if (USE_DELEGATE_TYPE == DELEGATE_TYPE_COMPATIBLE)
552        m_function(&delegate_base::method_stub<_FunctionClass, _ReturnType>),
553        m_callobject(reinterpret_cast<delegate_generic_class *>(this))
554#else
656555        m_function(NULL)
556#endif
657557   {
658558      bind(reinterpret_cast<delegate_generic_class *>(object));
659559   }
r18548r18549
662562   template<class _FunctionClass>
663563   delegate_base(typename traits<_FunctionClass>::static_func_type funcptr, const char *name, _FunctionClass *object)
664564      : delegate_common_base(name, &late_bind_helper<_FunctionClass>, reinterpret_cast<delegate_generic_function>(funcptr)),
665        m_function(reinterpret_cast<generic_static_func>(funcptr))
565        m_function(reinterpret_cast<generic_static_func>(funcptr)),
566        m_callobject(NULL)
666567   {
667568      bind(reinterpret_cast<delegate_generic_class *>(object));
668569   }
r18548r18549
671572   template<class _FunctionClass>
672573   delegate_base(typename traits<_FunctionClass>::static_ref_func_type funcptr, const char *name, _FunctionClass *object)
673574      : delegate_common_base(name, &late_bind_helper<_FunctionClass>, reinterpret_cast<delegate_generic_function>(funcptr)),
674        m_function(reinterpret_cast<generic_static_func>(funcptr))
575        m_function(reinterpret_cast<generic_static_func>(funcptr)),
576        m_callobject(NULL)
675577   {
676578      bind(reinterpret_cast<delegate_generic_class *>(object));
677579   }
r18548r18549
682584      if (this != &src)
683585      {
684586         delegate_common_base::copy(src);
587         m_callobject = copy_callobject(src);
685588         m_function = src.m_function;
686589      }
687590      return *this;
688591   }
689592
690593   // call the function
594#if (USE_DELEGATE_TYPE == DELEGATE_TYPE_COMPATIBLE)
595   _ReturnType operator()() const { return (*m_function)(m_callobject); }
596   _ReturnType operator()(_P1Type p1) const { return (*m_function)(m_callobject, p1); }
597   _ReturnType operator()(_P1Type p1, _P2Type p2) const { return (*m_function)(m_callobject, p1, p2); }
598   _ReturnType operator()(_P1Type p1, _P2Type p2, _P3Type p3) const { return (*m_function)(m_callobject, p1, p2, p3); }
599   _ReturnType operator()(_P1Type p1, _P2Type p2, _P3Type p3, _P4Type p4) const { return (*m_function)(m_callobject, p1, p2, p3, p4); }
600   _ReturnType operator()(_P1Type p1, _P2Type p2, _P3Type p3, _P4Type p4, _P5Type p5) const { return (*m_function)(m_callobject, p1, p2, p3, p4, p5); }
601#else
691602   _ReturnType operator()() const { return (*m_function)(m_object); }
692603   _ReturnType operator()(_P1Type p1) const { return (*m_function)(m_object, p1); }
693604   _ReturnType operator()(_P1Type p1, _P2Type p2) const { return (*m_function)(m_object, p1, p2); }
694605   _ReturnType operator()(_P1Type p1, _P2Type p2, _P3Type p3) const { return (*m_function)(m_object, p1, p2, p3); }
695606   _ReturnType operator()(_P1Type p1, _P2Type p2, _P3Type p3, _P4Type p4) const { return (*m_function)(m_object, p1, p2, p3, p4); }
696607   _ReturnType operator()(_P1Type p1, _P2Type p2, _P3Type p3, _P4Type p4, _P5Type p5) const { return (*m_function)(m_object, p1, p2, p3, p4, p5); }
608#endif
697609
698610   // late binding
699611   void late_bind(delegate_late_bind &object) { bind((*m_latebinder)(object)); }
r18548r18549
703615   void bind(delegate_generic_class *object)
704616   {
705617      m_object = object;
618     
619      // update callobject to match, unless it is pointing to ourself
620      if (m_callobject != reinterpret_cast<delegate_generic_class *>(this))
621         m_callobject = m_object;
622
623#if (USE_DELEGATE_TYPE != DELEGATE_TYPE_COMPATIBLE)
624      // update the function
706625      if (m_object != NULL && is_mfp())
707626         m_function = reinterpret_cast<generic_static_func>(m_raw_mfp.convert_to_generic(m_object));
627#endif
708628   }
709629
710630   // internal state
711631   generic_static_func         m_function;         // generic static function pointer
632   delegate_generic_class *   m_callobject;      // pointer to the object used for calling
712633};
713634
714#endif
715635
716636
717637//**************************************************************************

Previous 199869 Revisions Next


© 1997-2024 The MAME Team