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
|
|
|
|
2017-04-09 20:08:53 +02:00
|
|
|
#ifndef FOBJECT_H
|
|
|
|
#define FOBJECT_H
|
2015-05-23 13:35:12 +02:00
|
|
|
|
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>
|
2017-07-23 01:19:59 +02:00
|
|
|
#include <cstring>
|
2015-05-23 13:35:12 +02:00
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
2015-08-22 18:53:52 +02:00
|
|
|
|
2017-08-06 17:02:19 +02:00
|
|
|
#include "fc.h"
|
2015-05-23 13:35:12 +02:00
|
|
|
#include "fevent.h"
|
2017-09-07 23:35:24 +02:00
|
|
|
#include "ftypes.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:
|
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-03-12 00:29:56 +01:00
|
|
|
FObject* getChild (int) const;
|
2017-08-24 00:47:35 +02:00
|
|
|
const FObjectList& getChildren() const;
|
2016-11-02 00:37:58 +01:00
|
|
|
int numOfChildren() const;
|
|
|
|
|
|
|
|
// Inquiries
|
|
|
|
bool hasParent() const;
|
|
|
|
bool hasChildren() const;
|
2017-03-08 23:48:30 +01:00
|
|
|
bool isChild (FObject*) const;
|
|
|
|
bool isDirectChild (FObject*) const;
|
2017-06-11 17:47:50 +02:00
|
|
|
bool isWidget() const;
|
2017-07-23 01:19:59 +02:00
|
|
|
bool isInstanceOf (const char*) const;
|
2016-11-02 00:37:58 +01:00
|
|
|
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-03-19 17:18:07 +01:00
|
|
|
struct timer_data
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
timeval interval;
|
|
|
|
timeval timeout;
|
|
|
|
FObject* object;
|
|
|
|
};
|
|
|
|
|
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;
|
2017-06-11 17:47:50 +02:00
|
|
|
bool widget_object;
|
2017-01-02 08:07:46 +01:00
|
|
|
|
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-08-24 00:47:35 +02:00
|
|
|
inline const 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() ); }
|
|
|
|
|
2017-03-08 23:48:30 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline bool FObject::isDirectChild (FObject* obj) const
|
|
|
|
{ return bool( obj->getParent() == this ); }
|
|
|
|
|
2017-06-11 17:47:50 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline bool FObject::isWidget() const
|
|
|
|
{ return widget_object; }
|
|
|
|
|
2017-07-23 01:19:59 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline bool FObject::isInstanceOf (const char* classname) const
|
|
|
|
{ return ( classname ) ? bool(strcmp(classname, getClassName()) == 0) : false; }
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-04-09 20:08:53 +02:00
|
|
|
#endif // FOBJECT_H
|