'Better approach to integrate multiple types of Interfaces

I have, say, two devices.

class Device1{
  public:
  int GetData(){
  //...
  }
};

class Device2{
  public:
  bool GetData(){
  //...
  }
};

I have a a C# style timer class that runs a std::thread periodically. Time being, I am showing only a one-time call, rather than the periodic ones.

A Timer class looks like this:

class Timer {
    public:
        Timer(long long period,
            std::function<int()> f):
            m_timeperiod(period * 1000000){
                
        m_enable = true;
        m_thd = std::thread (f);
    }
        virtual ~Timer(){}
        void Wait()
        {
            m_thd.join();
        }

    private:
        long long m_timeperiod;
        std::atomic<bool> m_enable;
        //std::function <int()> m_action;
        void Loop();
        std::thread m_thd;
    };

The main looks like:

int main()
{
    Device1 d1;
    std::function<int()> fnCaller = std::bind(&Device1::GetData, &d1);
    Timer tmr(1,fnCaller);
    tmr.Wait();
    std::cout<<"Hello World";

    return 0;
}

This works fine for Device1, but I have to create a new Timer class for Device2.

class Timer2 {
    public:
        Timer(long long period,
            std::function<bool()> f):
            m_timeperiod(period * 1000000){
                
        m_enable = true;
        m_thd = std::thread (f);
    }
        virtual ~Timer(){}
        void Wait()
        {
            m_thd.join();
        }

    private:
        long long m_timeperiod;
        std::atomic<bool> m_enable;
        //std::function <int()> m_action;
        void Loop();
        std::thread m_thd;
    };

Later I might need to create a newer one for Device3, so on and so forth. What can I do to have a scalable solution?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source