// **********************************************************************
//
// Copyright (c) 1998
// Object-Oriented Concepts, Inc.
// Billerica, MA, USA
//
// Copyright (c) 1998
// Object-Oriented Concepts GmbH
// Ettlingen, Germany
//
// All Rights Reserved
//
// **********************************************************************

#ifndef OB_TIMER_H
#define OB_TIMER_H

//
// The OBTimer class
//
class OBTimer 
{
    friend class OBTimerList;

    //
    // Hide copy-constructor and asignment operator
    //
    OBTimer(const OBTimer&);
    void operator=(const OBTimer&);

protected:

    OBTimer* next_; // Next timer in list
    bool activated_; // Set to true if activated
    timeval timeout_; // The timeout value

    // 
    // Timers are used only as sub classes
    // The bool parameter indicates if timeval is a relativ value
    //
    OBTimer();
    OBTimer(const timeval&, bool = false);
    virtual ~OBTimer();

public:

    //
    // Notification called when timer expires
    //
    virtual void notify() = 0;

    //
    // Activate and stop the timer
    //
    void activate(const timeval&, bool = false);
    void stop();

    //
    // Get the timeout value
    //
    const timeval& timeout() const { return timeout_; }
};

//
// The OBTimerList class
//
class OBTimerList
{
protected:

    OBTimer* head_;

    OBTimerList();
    ~OBTimerList();

public:

    //
    // Expire expired timers
    //
    void expire(const timeval&);

    //
    // List handling
    //
    bool isEmpty() const { return head_ == 0; }
    OBTimer* first() const { return head_; }
    void add(OBTimer*);
    void remove(OBTimer*);

    //
    // Get the TimerList singleton
    //
    static OBTimerList* instance();

    //
    // Helper functions for handling timeval values
    //
    static int timeCmp(const timeval&, const timeval&);
    static timeval timeSub(const timeval&, const timeval&);
    static timeval timeAdd(const timeval&, const timeval&);
    static timeval timeNow();
};

#endif
