Method one:
The interrupt class is the interface. The device interrupt class is the specific driver interrupt handler, it is the friend class of driver class.
The demo code:
class Interrupt{public: Interrupt(void); static void Register(int interrupt_numberber, Interrupt* intThisPtr); // wrapper functions to ISR() static void Interrupt_0(void); static void Interrupt_1(void); static void Interrupt_2(void); /* etc.*/ virtual void ISR(void) = 0; private: static Interrupt* ISRVectorTable[MAX_INTERRUPTS]; }; void Interrupt::Interrupt_0(void) { ISRVectorTable[0]->ISR(); } void Interrupt::Interrupt_1(void) { ISRVectorTable[1]->ISR(); } /* etc. */ void Interrupt::Register(int interrupt_number, Interrupt* intThisPtr) { ISRVectorTable[interrupt_number] = intThisPtr; }
class Timer; // forward declaration class TimerInterrupt : public Interrupt { public: TimerInterrupt(Timer* ownerptr); virtual void ISR(void); private: Timer* InterruptOwnerPtr; }; class Timer { friend class TimerInterrupt; public: Timer(void); int GetCount(void) { return Count; } private: TimerInterrupt* InterruptPtr; int Count; };
Timer::Timer(void) { InterruptPtr = new TimerInterrupt(this); } TimerInterrupt::TimerInterrupt(Timer* owner) { InterruptOwnerPtr = owner; // Allows interrupt to access owner‘s data Interrupt::Register(TIMER_INTERRUPT_NUMBER, this); }
原文:https://www.cnblogs.com/zjbfvfv/p/11943884.html