mirror of
https://github.com/debauchee/barrier.git
synced 2026-11-01 21:15:03 +08:00
Started over.
This commit is contained in:
46
base/BasicTypes.h
Normal file
46
base/BasicTypes.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef BASICTYPES_H
|
||||
#define BASICTYPES_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#if defined(CONFIG_PLATFORM_LINUX)
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef int8_t SInt8;
|
||||
typedef int16_t SInt16;
|
||||
typedef int32_t SInt32;
|
||||
typedef int64_t SInt64;
|
||||
|
||||
typedef uint8_t UInt8;
|
||||
typedef uint16_t UInt16;
|
||||
typedef uint32_t UInt32;
|
||||
typedef uint64_t UInt64;
|
||||
|
||||
#endif // CONFIG_PLATFORM_LINUX
|
||||
|
||||
#if defined(CONFIG_PLATFORM_SOLARIS)
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
typedef int8_t SInt8;
|
||||
typedef int16_t SInt16;
|
||||
typedef int32_t SInt32;
|
||||
typedef int64_t SInt64;
|
||||
|
||||
typedef uint8_t UInt8;
|
||||
typedef uint16_t UInt16;
|
||||
typedef uint32_t UInt32;
|
||||
typedef uint64_t UInt64;
|
||||
|
||||
#endif // CONFIG_PLATFORM_SOLARIS
|
||||
|
||||
#if defined(CONFIG_PLATFORM_WIN32)
|
||||
|
||||
// FIXME
|
||||
|
||||
#endif // CONFIG_PLATFORM_WIN32
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
19
base/CFunctionJob.cpp
Normal file
19
base/CFunctionJob.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "CFunctionJob.h"
|
||||
|
||||
//
|
||||
// CFunctionJob
|
||||
//
|
||||
|
||||
CFunctionJob::CFunctionJob(void (*func)(void*), void* arg) :
|
||||
m_func(func),
|
||||
m_arg(arg)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
void CFunctionJob::run()
|
||||
{
|
||||
if (m_func != NULL) {
|
||||
m_func(m_arg);
|
||||
}
|
||||
}
|
||||
18
base/CFunctionJob.h
Normal file
18
base/CFunctionJob.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef CFUNCTIONJOB_H
|
||||
#define CFUNCTIONJOB_H
|
||||
|
||||
#include "IJob.h"
|
||||
|
||||
class CFunctionJob : public IJob {
|
||||
public:
|
||||
CFunctionJob(void (*func)(void*), void* arg = NULL);
|
||||
|
||||
// IJob overrides
|
||||
virtual void run();
|
||||
|
||||
private:
|
||||
void (*m_func)(void*);
|
||||
void* m_arg;
|
||||
};
|
||||
|
||||
#endif
|
||||
180
base/CStopwatch.cpp
Normal file
180
base/CStopwatch.cpp
Normal file
@@ -0,0 +1,180 @@
|
||||
#include "CStopwatch.h"
|
||||
|
||||
//
|
||||
// CStopwatch
|
||||
//
|
||||
|
||||
CStopwatch::CStopwatch(bool triggered) :
|
||||
m_mark(0.0),
|
||||
m_triggered(triggered),
|
||||
m_stopped(triggered)
|
||||
{
|
||||
if (!triggered)
|
||||
m_mark = getClock();
|
||||
}
|
||||
|
||||
CStopwatch::~CStopwatch()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
double CStopwatch::reset()
|
||||
{
|
||||
if (m_stopped) {
|
||||
const double dt = m_mark;
|
||||
m_mark = 0.0;
|
||||
return dt;
|
||||
}
|
||||
else {
|
||||
const double t = getClock();
|
||||
const double dt = t - m_mark;
|
||||
m_mark = t;
|
||||
return dt;
|
||||
}
|
||||
}
|
||||
|
||||
void CStopwatch::stop()
|
||||
{
|
||||
if (m_stopped)
|
||||
return;
|
||||
|
||||
// save the elapsed time
|
||||
m_mark = getClock() - m_mark;
|
||||
m_stopped = true;
|
||||
}
|
||||
|
||||
void CStopwatch::start()
|
||||
{
|
||||
m_triggered = false;
|
||||
if (!m_stopped)
|
||||
return;
|
||||
|
||||
// set the mark such that it reports the time elapsed at stop()
|
||||
m_mark = getClock() - m_mark;
|
||||
m_stopped = false;
|
||||
}
|
||||
|
||||
void CStopwatch::setTrigger()
|
||||
{
|
||||
stop();
|
||||
m_triggered = true;
|
||||
}
|
||||
|
||||
double CStopwatch::getTime()
|
||||
{
|
||||
if (m_triggered) {
|
||||
const double dt = m_mark;
|
||||
start();
|
||||
return dt;
|
||||
}
|
||||
if (m_stopped)
|
||||
return m_mark;
|
||||
return getClock() - m_mark;
|
||||
}
|
||||
|
||||
CStopwatch::operator double()
|
||||
{
|
||||
return getTime();
|
||||
}
|
||||
|
||||
bool CStopwatch::isStopped() const
|
||||
{
|
||||
return m_stopped;
|
||||
}
|
||||
|
||||
double CStopwatch::getTime() const
|
||||
{
|
||||
if (m_stopped)
|
||||
return m_mark;
|
||||
return getClock() - m_mark;
|
||||
}
|
||||
|
||||
CStopwatch::operator double() const
|
||||
{
|
||||
return getTime();
|
||||
}
|
||||
|
||||
#if defined(CONFIG_PLATFORM_UNIX)
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
double CStopwatch::getClock() const
|
||||
{
|
||||
struct timeval t;
|
||||
gettimeofday(&t, NULL);
|
||||
return (double)t.tv_sec + 1.0e-6 * (double)t.tv_usec;
|
||||
}
|
||||
|
||||
#endif // CONFIG_PLATFORM_UNIX
|
||||
|
||||
#if defined(CONFIG_PLATFORM_WIN32)
|
||||
|
||||
// avoid getting a lot a crap from mmsystem.h that we don't need
|
||||
#define MMNODRV // Installable driver support
|
||||
#define MMNOSOUND // Sound support
|
||||
#define MMNOWAVE // Waveform support
|
||||
#define MMNOMIDI // MIDI support
|
||||
#define MMNOAUX // Auxiliary audio support
|
||||
#define MMNOMIXER // Mixer support
|
||||
#define MMNOJOY // Joystick support
|
||||
#define MMNOMCI // MCI support
|
||||
#define MMNOMMIO // Multimedia file I/O support
|
||||
#define MMNOMMSYSTEM // General MMSYSTEM functions
|
||||
|
||||
#include <windows.h>
|
||||
#include <mmsystem.h>
|
||||
|
||||
typedef WINMMAPI DWORD (WINAPI *PTimeGetTime)(void);
|
||||
|
||||
static double s_freq = 0.0;
|
||||
static HINSTANCE s_mmInstance = NULL;
|
||||
static PTimeGetTime s_tgt = NULL;
|
||||
|
||||
//
|
||||
// initialize local variables
|
||||
//
|
||||
|
||||
class CStopwatchInit {
|
||||
public:
|
||||
CStopwatchInit();
|
||||
~CStopwatchInit();
|
||||
};
|
||||
static CStopwatchInit s_init;
|
||||
|
||||
CStopwatchInit::CStopwatchInit()
|
||||
{
|
||||
LARGE_INTEGER freq;
|
||||
if (QueryPerformanceFrequency(&freq) && freq.QuadPart != 0) {
|
||||
s_freq = 1.0 / static_cast<double>(freq.QuadPart);
|
||||
}
|
||||
else {
|
||||
// load winmm.dll and get timeGetTime
|
||||
s_mmInstance = LoadLibrary("winmm");
|
||||
if (s_mmInstance)
|
||||
s_tgt = (PTimeGetTime)GetProcAddress(s_mmInstance, "timeGetTime");
|
||||
}
|
||||
}
|
||||
|
||||
CStopwatchInit::~CStopwatchInit()
|
||||
{
|
||||
if (s_mmInstance)
|
||||
FreeLibrary(reinterpret_cast<HMODULE>(s_mmInstance));
|
||||
}
|
||||
|
||||
double CStopwatch::getClock() const
|
||||
{
|
||||
// get time. we try three ways, in order of descending precision
|
||||
if (s_freq != 0.0) {
|
||||
LARGE_INTEGER c;
|
||||
QueryPerformanceCounter(&c);
|
||||
return s_freq * static_cast<double>(c.QuadPart);
|
||||
}
|
||||
else if (s_tgt) {
|
||||
return 0.001 * static_cast<double>(s_tgt());
|
||||
}
|
||||
else {
|
||||
return 0.001 * static_cast<double>(GetTickCount());
|
||||
}
|
||||
}
|
||||
|
||||
#endif // CONFIG_PLATFORM_WIN32
|
||||
57
base/CStopwatch.h
Normal file
57
base/CStopwatch.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#ifndef CSTOPWATCH_H
|
||||
#define CSTOPWATCH_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
class CStopwatch {
|
||||
public:
|
||||
// the default constructor does an implicit reset() or setTrigger().
|
||||
// if triggered == false then the clock starts ticking.
|
||||
CStopwatch(bool triggered = false);
|
||||
~CStopwatch();
|
||||
|
||||
// manipulators
|
||||
|
||||
// set the start time to the current time, returning the time since
|
||||
// the last reset. this does not remove the trigger if it's set nor
|
||||
// does it start a stopped clock. if the clock is stopped then
|
||||
// subsequent reset()'s will return 0.
|
||||
double reset();
|
||||
|
||||
// stop and start the stopwatch. while stopped, no time elapses.
|
||||
// stop() does not remove the trigger but start() does, even if
|
||||
// the clock was already started.
|
||||
void stop();
|
||||
void start();
|
||||
|
||||
// setTrigger() stops the clock like stop() except there's an
|
||||
// implicit start() the next time (non-const) getTime() is called.
|
||||
// this is useful when you want the clock to start the first time
|
||||
// you check it.
|
||||
void setTrigger();
|
||||
|
||||
// return the time since the last reset() (or call reset() and
|
||||
// return zero if the trigger is set).
|
||||
double getTime();
|
||||
operator double();
|
||||
|
||||
// accessors
|
||||
|
||||
// returns true if the watch is stopped
|
||||
bool isStopped() const;
|
||||
|
||||
// return the time since the last reset(). these cannot trigger
|
||||
// the clock to start so if the trigger is set it's as if it wasn't.
|
||||
double getTime() const;
|
||||
operator double() const;
|
||||
|
||||
private:
|
||||
double getClock() const;
|
||||
|
||||
private:
|
||||
double m_mark;
|
||||
bool m_triggered;
|
||||
bool m_stopped;
|
||||
};
|
||||
|
||||
#endif
|
||||
41
base/CString.h
Normal file
41
base/CString.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef CSTRING_H
|
||||
#define CSTRING_H
|
||||
|
||||
#include "common.h"
|
||||
#include <string>
|
||||
|
||||
#ifndef CSTRING_DEF_CTOR
|
||||
#define CSTRING_ALLOC1
|
||||
#define CSTRING_ALLOC2
|
||||
#define CSTRING_DEF_CTOR CString() : _Myt() { }
|
||||
#endif
|
||||
|
||||
// use to get appropriate type for string constants. it depends on
|
||||
// the internal representation type of CString.
|
||||
#define _CS(_x) _x
|
||||
|
||||
class CString : public std::string {
|
||||
public:
|
||||
typedef char _e;
|
||||
typedef _e CharT;
|
||||
typedef std::allocator<_e> _a;
|
||||
typedef std::string _Myt;
|
||||
typedef const_iterator _It;
|
||||
|
||||
// same constructors as base class
|
||||
CSTRING_DEF_CTOR
|
||||
CString(const _Myt& _x) : _Myt(_x) { }
|
||||
CString(const _Myt& _x, size_type _p, size_type _m CSTRING_ALLOC1) :
|
||||
_Myt(_x, _p, _m CSTRING_ALLOC2) { }
|
||||
CString(const _e *_s, size_type _n CSTRING_ALLOC1) :
|
||||
_Myt(_s, _n CSTRING_ALLOC2) { }
|
||||
CString(const _e *_s CSTRING_ALLOC1) :
|
||||
_Myt(_s CSTRING_ALLOC2) { }
|
||||
CString(size_type _n, _e _c CSTRING_ALLOC1) :
|
||||
_Myt(_n, _c CSTRING_ALLOC2) { }
|
||||
CString(_It _f, _It _l CSTRING_ALLOC1) :
|
||||
_Myt(_f, _l CSTRING_ALLOC2) { }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
11
base/IInterface.h
Normal file
11
base/IInterface.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef IINTERFACE_H
|
||||
#define IINTERFACE_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
class IInterface {
|
||||
public:
|
||||
virtual ~IInterface() { }
|
||||
};
|
||||
|
||||
#endif
|
||||
11
base/IJob.h
Normal file
11
base/IJob.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef IJOB_H
|
||||
#define IJOB_H
|
||||
|
||||
#include "IInterface.h"
|
||||
|
||||
class IJob : public IInterface {
|
||||
public:
|
||||
virtual void run() = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
24
base/Makefile
Normal file
24
base/Makefile
Normal file
@@ -0,0 +1,24 @@
|
||||
DEPTH=..
|
||||
include $(DEPTH)/Makecommon
|
||||
|
||||
#
|
||||
# target file
|
||||
#
|
||||
TARGET = base
|
||||
|
||||
#
|
||||
# source files
|
||||
#
|
||||
LCXXINCS = \
|
||||
$(NULL)
|
||||
CXXFILES = \
|
||||
XBase.cpp \
|
||||
CFunctionJob.cpp \
|
||||
CStopwatch.cpp \
|
||||
$(NULL)
|
||||
|
||||
targets: $(LIBTARGET)
|
||||
|
||||
$(LIBTARGET): $(OBJECTS)
|
||||
if test ! -d $(LIBDIR); then $(MKDIR) $(LIBDIR); fi
|
||||
$(ARF) $(LIBTARGET) $(OBJECTS)
|
||||
39
base/TMethodJob.h
Normal file
39
base/TMethodJob.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef CMETHODJOB_H
|
||||
#define CMETHODJOB_H
|
||||
|
||||
#include "IJob.h"
|
||||
|
||||
template <class T>
|
||||
class TMethodJob : public IJob {
|
||||
public:
|
||||
TMethodJob(T* object, void (T::*method)(void*), void* arg = NULL);
|
||||
|
||||
// IJob overrides
|
||||
virtual void run();
|
||||
|
||||
private:
|
||||
T* m_object;
|
||||
void (T::*m_method)(void*);
|
||||
void* m_arg;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline
|
||||
TMethodJob<T>::TMethodJob(T* object, void (T::*method)(void*), void* arg) :
|
||||
m_object(object),
|
||||
m_method(method),
|
||||
m_arg(arg)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline
|
||||
void TMethodJob<T>::run()
|
||||
{
|
||||
if (m_object != NULL) {
|
||||
(m_object->*m_method)(m_arg);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
72
base/XBase.cpp
Normal file
72
base/XBase.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "XBase.h"
|
||||
#include <errno.h>
|
||||
|
||||
// win32 wants a const char* argument to std::exception c'tor
|
||||
#if CONFIG_PLATFORM_WIN32
|
||||
#define STDEXCEPTARG ""
|
||||
#endif
|
||||
|
||||
// default to no argument
|
||||
#ifndef STDEXCEPTARG
|
||||
#define STDEXCEPTARG
|
||||
#endif
|
||||
|
||||
//
|
||||
// XBase
|
||||
//
|
||||
|
||||
XBase::XBase() : exception(STDEXCEPTARG), m_what()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
XBase::XBase(const CString& msg) : exception(STDEXCEPTARG), m_what(msg)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
XBase::~XBase()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
const char* XBase::what() const
|
||||
{
|
||||
if (m_what.empty()) {
|
||||
m_what = getWhat();
|
||||
}
|
||||
return m_what.c_str();
|
||||
}
|
||||
|
||||
CString XBase::format(const char* /*id*/,
|
||||
const char* fmt, ...) const throw()
|
||||
{
|
||||
// FIXME -- use id to lookup formating string
|
||||
// FIXME -- format string with arguments
|
||||
return fmt;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// MXErrno
|
||||
//
|
||||
|
||||
MXErrno::MXErrno() : m_errno(errno)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
MXErrno::MXErrno(int err) : m_errno(err)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
int MXErrno::getErrno() const
|
||||
{
|
||||
return m_errno;
|
||||
}
|
||||
|
||||
const char* MXErrno::getErrstr() const
|
||||
{
|
||||
return strerror(m_errno);
|
||||
}
|
||||
44
base/XBase.h
Normal file
44
base/XBase.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef XBASE_H
|
||||
#define XBASE_H
|
||||
|
||||
#include "CString.h"
|
||||
#include <exception>
|
||||
|
||||
class XBase : public std::exception {
|
||||
public:
|
||||
XBase();
|
||||
XBase(const CString& msg);
|
||||
virtual ~XBase();
|
||||
|
||||
// std::exception overrides
|
||||
virtual const char* what() const;
|
||||
|
||||
protected:
|
||||
// returns a human readable string describing the exception
|
||||
virtual CString getWhat() const throw() = 0;
|
||||
|
||||
// look up a message and format it
|
||||
virtual CString format(const char* id,
|
||||
const char* defaultFormat, ...) const throw();
|
||||
|
||||
private:
|
||||
mutable CString m_what;
|
||||
};
|
||||
|
||||
class MXErrno {
|
||||
public:
|
||||
MXErrno();
|
||||
MXErrno(int);
|
||||
|
||||
// manipulators
|
||||
|
||||
// accessors
|
||||
|
||||
int getErrno() const;
|
||||
const char* getErrstr() const;
|
||||
|
||||
private:
|
||||
int m_errno;
|
||||
};
|
||||
|
||||
#endif
|
||||
32
base/common.h
Normal file
32
base/common.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#if defined(__linux__)
|
||||
|
||||
#define CONFIG_PLATFORM_LINUX
|
||||
#define CONFIG_PLATFORM_UNIX
|
||||
#define CONFIG_TYPES_X11
|
||||
#define CONFIG_PTHREADS
|
||||
|
||||
#elif defined(__sun__)
|
||||
|
||||
#define CONFIG_PLATFORM_SOLARIS
|
||||
#define CONFIG_PLATFORM_UNIX
|
||||
#define CONFIG_TYPES_X11
|
||||
#define CONFIG_PTHREADS
|
||||
|
||||
#elif defined(_WINDOWS) && defined(WIN32)
|
||||
|
||||
#define CONFIG_PLATFORM_WIN32
|
||||
|
||||
#else
|
||||
|
||||
#error unsupported platform
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user