2015-09-25 21:37:19 +02:00
|
|
|
// File: fobject.h
|
|
|
|
// Provides: class FObject
|
|
|
|
//
|
|
|
|
// Base class
|
|
|
|
// ══════════
|
|
|
|
//
|
|
|
|
// ▕▔▔▔▔▔▔▔▔▔▏
|
|
|
|
// ▕ FObject ▏
|
|
|
|
// ▕▁▁▁▁▁▁▁▁▁▏
|
2015-05-23 13:35:12 +02:00
|
|
|
|
|
|
|
#ifndef _FOBJECT_H
|
|
|
|
#define _FOBJECT_H
|
|
|
|
|
2017-02-25 15:18:29 +01:00
|
|
|
#include <stdint.h>
|
2015-05-23 13:35:12 +02:00
|
|
|
#include <sys/time.h> // need for gettimeofday
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
2015-08-22 18:53:52 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
#include "fevent.h"
|
|
|
|
|
2015-09-25 21:37:19 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
#define null NULL
|
|
|
|
|
|
|
|
typedef unsigned char uChar;
|
|
|
|
typedef unsigned int uInt;
|
|
|
|
typedef unsigned long uLong;
|
2017-02-25 15:18:29 +01:00
|
|
|
typedef uint8_t uInt8;
|
|
|
|
typedef uint16_t uInt16;
|
|
|
|
typedef uint32_t uInt32;
|
|
|
|
typedef uint64_t uInt64;
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-09-27 00:46:05 +02:00
|
|
|
typedef signed int sInt;
|
|
|
|
typedef signed long sLong;
|
2017-02-25 15:18:29 +01:00
|
|
|
typedef int8_t sInt8;
|
|
|
|
typedef int16_t sInt16;
|
|
|
|
typedef int32_t sInt32;
|
2016-09-27 00:46:05 +02:00
|
|
|
typedef int64_t sInt64;
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-09-27 00:46:05 +02:00
|
|
|
typedef long double lDouble;
|
2015-05-23 13:35:12 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class FObject
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
|
|
|
|
class FObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct timer_data
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
timeval interval;
|
|
|
|
timeval timeout;
|
|
|
|
FObject* object;
|
|
|
|
};
|
2016-09-27 00:46:05 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
// Typedef
|
2017-01-28 22:03:15 +01:00
|
|
|
typedef std::list<FObject*> FObjectList;
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-09-30 04:55:28 +02:00
|
|
|
// Constructor
|
2015-09-27 16:45:28 +02:00
|
|
|
explicit FObject (FObject* = 0);
|
2016-11-02 00:37:58 +01:00
|
|
|
|
2016-09-30 04:55:28 +02:00
|
|
|
// Destructor
|
2015-05-23 13:35:12 +02:00
|
|
|
virtual ~FObject();
|
2016-09-30 04:55:28 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
// Accessors
|
2015-05-23 13:35:12 +02:00
|
|
|
virtual const char* getClassName() const;
|
2016-11-02 00:37:58 +01:00
|
|
|
FObject* getParent() const;
|
2017-01-28 22:03:15 +01:00
|
|
|
FObjectList getChildren() const;
|
2016-11-02 00:37:58 +01:00
|
|
|
int numOfChildren() const;
|
|
|
|
|
|
|
|
// Inquiries
|
|
|
|
bool hasParent() const;
|
|
|
|
bool hasChildren() const;
|
|
|
|
bool isTimerInUpdating() const;
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
void removeParent();
|
|
|
|
void addChild (FObject*);
|
|
|
|
void delChild (FObject*);
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-09-30 04:55:28 +02:00
|
|
|
// Timer methods
|
2016-11-02 00:37:58 +01:00
|
|
|
static void getCurrentTime (timeval&);
|
|
|
|
int addTimer (int);
|
|
|
|
bool delTimer (int);
|
|
|
|
bool delOwnTimer();
|
|
|
|
bool delAllTimer();
|
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
protected:
|
2017-01-02 08:07:46 +01:00
|
|
|
// Typedef
|
|
|
|
typedef std::vector<timer_data> TimerList;
|
|
|
|
|
2016-09-30 04:55:28 +02:00
|
|
|
// Event handler
|
2016-10-17 08:44:38 +02:00
|
|
|
virtual bool event (FEvent*);
|
2015-05-23 13:35:12 +02:00
|
|
|
virtual void onTimer (FTimerEvent*);
|
|
|
|
|
2017-01-02 08:07:46 +01:00
|
|
|
// Data Members
|
|
|
|
static TimerList* timer_list;
|
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
private:
|
2016-09-30 04:55:28 +02:00
|
|
|
// Disable copy constructor
|
2015-05-23 13:35:12 +02:00
|
|
|
FObject (const FObject&);
|
2016-11-02 00:37:58 +01:00
|
|
|
|
2016-09-30 04:55:28 +02:00
|
|
|
// Disable assignment operator (=)
|
2015-05-23 13:35:12 +02:00
|
|
|
FObject& operator = (const FObject&);
|
2016-11-02 00:37:58 +01:00
|
|
|
|
|
|
|
// Data Members
|
|
|
|
FObject* parent_obj;
|
2017-01-28 22:03:15 +01:00
|
|
|
FObjectList children_list;
|
2016-11-02 00:37:58 +01:00
|
|
|
bool has_parent;
|
|
|
|
static bool timer_modify_lock;
|
2015-05-23 13:35:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline const char* FObject::getClassName() const
|
|
|
|
{ return "FObject"; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2015-09-20 05:44:50 +02:00
|
|
|
inline FObject* FObject::getParent() const
|
2016-09-27 00:46:05 +02:00
|
|
|
{ return parent_obj; }
|
2015-05-23 13:35:12 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-01-28 22:03:15 +01:00
|
|
|
inline FObject::FObjectList FObject::getChildren() const
|
2016-11-02 00:37:58 +01:00
|
|
|
{ return children_list; }
|
2015-05-23 13:35:12 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
inline int FObject::numOfChildren() const
|
|
|
|
{ return int(children_list.size()); }
|
2015-05-23 13:35:12 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-11-02 00:37:58 +01:00
|
|
|
inline bool FObject::hasParent() const
|
|
|
|
{ return has_parent; }
|
2015-05-23 13:35:12 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline bool FObject::hasChildren() const
|
|
|
|
{ return bool( ! children_list.empty() ); }
|
|
|
|
|
2016-10-01 23:18:49 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline bool FObject::isTimerInUpdating() const
|
|
|
|
{ return timer_modify_lock; }
|
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FObject::removeParent()
|
|
|
|
{ parent_obj = 0; }
|
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Operator functions for timeval
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
static inline timeval operator + (const timeval& t1, const timeval& t2)
|
|
|
|
{
|
|
|
|
timeval tmp;
|
|
|
|
tmp.tv_sec = t1.tv_sec + t2.tv_sec;
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 )
|
|
|
|
{
|
|
|
|
tmp.tv_sec++;
|
|
|
|
tmp.tv_usec -= 1000000;
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
static inline timeval operator - (const timeval& t1, const timeval& t2)
|
|
|
|
{
|
|
|
|
timeval tmp;
|
|
|
|
tmp.tv_sec = t1.tv_sec - t2.tv_sec;
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 )
|
|
|
|
{
|
|
|
|
tmp.tv_sec--;
|
|
|
|
tmp.tv_usec += 1000000;
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
static inline timeval& operator += (timeval& t1, const timeval& t2)
|
|
|
|
{
|
|
|
|
t1.tv_sec += t2.tv_sec;
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
if ( (t1.tv_usec += t2.tv_usec) >= 1000000 )
|
|
|
|
{
|
|
|
|
t1.tv_sec++;
|
|
|
|
t1.tv_usec -= 1000000;
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
return t1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
static inline bool operator < (const timeval& t1, const timeval& t2)
|
|
|
|
{
|
|
|
|
return (t1.tv_sec < t2.tv_sec)
|
|
|
|
|| (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _FOBJECT_H
|