namespace Msp {
+Application::Application():
+ exit_code(0),
+ loop_mode_(TICK_SLEEP)
+{ }
+
/**
Constructs an instance of the registered application class and runs it. If the
application throws a UsageError, the static usage() function is called.
if(exc && !exc->get_backtrace().get_frames().empty())
{
cerr<<" backtrace:\n";
- const Debug::Backtrace::FrameSeq &frames=exc->get_backtrace().get_frames();
- for(Debug::Backtrace::FrameSeq::const_iterator i=frames.begin(); i!=frames.end(); ++i)
+ const list<Debug::Backtrace::StackFrame> &frames=exc->get_backtrace().get_frames();
+ for(list<Debug::Backtrace::StackFrame>::const_iterator i=frames.begin(); i!=frames.end(); ++i)
cerr<<" "<<*i<<'\n';
}
#endif
cerr<<"The programmer was lazy and didn't write a usage() function for this application.\n";
}
-Application::Application():
- exit_code(0),
- loop_mode_(TICK_SLEEP)
-{ }
-
/**
Default main loop. Behavior depends on loop_mode_. A custom main loop should
monitor the done member variable and return exit_code.
app_->sighandler(s);
}
+
Application::RegBase::RegBase()
{
if(reg_app_)
*/
class Application
{
-public:
- virtual ~Application() { }
-
- static int run(int, char **, void * =0);
- static void usage(const char *, const char *, bool);
- static void *get_data() { return data_; }
protected:
enum LoopMode
{
NONE, /// No main loop - main() will just return
- SLEEP, /// Only sleep in the main loop - useful for servers
+ SLEEP, /// Only sleep in the main loop - useful for threaded servers
TICK_SLEEP, /// Call tick every iteration, with a short sleep in between
TICK_YIELD, /// Call tick every iteration, with sched_yield in between
TICK_BUSY /// Call tick every iteration
};
bool done;
- int exit_code;
+ int exit_code;
+private:
+ LoopMode loop_mode_;
+ Semaphore sleep_sem_;
+
+ static RegBase *reg_app_;
+ static Application *app_;
+ static void *data_;
+
+protected:
Application();
+public:
+ virtual ~Application() { }
+
+ static int run(int, char **, void * =0);
+ static void usage(const char *, const char *, bool);
+ static void *get_data() { return data_; }
+
+protected:
virtual int main();
void catch_signal(int);
void set_loop_mode(LoopMode);
virtual void tick() { }
virtual void sighandler(int) { }
private:
- LoopMode loop_mode_;
- Semaphore sleep_sem_;
+ static void sighandler_(int);
Application(const Application &);
Application &operator=(const Application &);
-
- static RegBase *reg_app_;
- static Application *app_;
- static void *data_;
-
- static void sighandler_(int);
- static void sigalrm_(int) { }
};
} // namespace Msp
*/
class Exception: public std::exception
{
+private:
+ std::string wot;
+ std::string wer;
+ Debug::Backtrace bt;
+
public:
Exception(const std::string &);
~Exception() throw() { }
Exception &at(const std::string &) throw();
const char *where() const throw() { return wer.c_str(); }
const Debug::Backtrace &get_backtrace() const throw() { return bt; }
-private:
- std::string wot;
- std::string wer;
- Debug::Backtrace bt;
-
};
/**
*/
class KeyError: public Exception
{
+private:
+ std::string key;
+
public:
KeyError(const std::string &w_): Exception(w_) { }
KeyError(const std::string &w_, const std::string &k);
- const std::string &get_key() const { return key; }
~KeyError() throw() { }
-private:
- std::string key;
+
+ const std::string &get_key() const { return key; }
};
/**
*/
class UsageError: public Exception
{
+private:
+ bool brief;
+
public:
UsageError(const std::string &r, bool b=true): Exception(r), brief(b) { }
bool get_brief() const { return brief; }
-private:
- bool brief;
};
/**
*/
class SystemError: public Exception
{
+private:
+ int err;
+
public:
SystemError(const std::string &, int);
int get_error_code() const { return err; }
-private:
- int err;
+private:
static std::string build_what(const std::string &, int);
};
namespace Msp {
+GetOpt::~GetOpt()
+{
+ for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
+ delete *i;
+}
+
/**
Generates a single line that gives an overview about the known options.
args.push_back(argv[i]);
}
-GetOpt::~GetOpt()
-{
- for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
- delete *i;
-}
-
GetOpt::OptBase &GetOpt::get_option(char s)
{
for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
OptBase(char s, const std::string &l, ArgType a): shrt(s), lng(l), arg_type(a), seen_count(0) { }
};
- const std::vector<std::string> &get_args() const { return args; }
-
- template<typename T>
- OptBase &add_option(char s, const std::string &l, T &d, ArgType a=NO_ARG)
- { opts.push_back(new Option<T>(s, l, d, a)); return *opts.back(); }
-
- template<typename T>
- OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a=REQUIRED_ARG)
- { opts.push_back(new ListOption<std::list<T> >(s, l, d, a)); return *opts.back(); }
-
- template<typename T>
- OptBase &add_option(const std::string &l, T &d, ArgType a)
- { return add_option(0, l, d, a); }
-
- std::string generate_usage(const std::string &) const;
- std::string generate_help() const;
- void operator()(unsigned, const char *const *);
-
- ~GetOpt();
private:
template<typename T>
class Option: public OptBase
T &data;
};
- std::list<OptBase *> opts;
+ std::list<OptBase *> opts;
std::vector<std::string> args;
+public:
+ ~GetOpt();
+
+ const std::vector<std::string> &get_args() const { return args; }
+
+ template<typename T>
+ OptBase &add_option(char s, const std::string &l, T &d, ArgType a=NO_ARG)
+ { opts.push_back(new Option<T>(s, l, d, a)); return *opts.back(); }
+
+ template<typename T>
+ OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a=REQUIRED_ARG)
+ { opts.push_back(new ListOption<std::list<T> >(s, l, d, a)); return *opts.back(); }
+
+ template<typename T>
+ OptBase &add_option(const std::string &l, T &d, ArgType a)
+ { return add_option(0, l, d, a); }
+
+ std::string generate_usage(const std::string &) const;
+ std::string generate_help() const;
+ void operator()(unsigned, const char *const *);
+private:
+
OptBase &get_option(char);
OptBase &get_option(const std::string &);
unsigned process_long(const char *const *);
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifdef WIN32
#include <windows.h>
#endif
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_CORE_MUTEX_H_
#define MSP_CORE_MUTEX_H_
class Mutex
{
+ friend class Semaphore;
+
+private:
+ MutexHandle mutex;
+
public:
#ifndef WIN32
Mutex() { pthread_mutex_init(&mutex, 0); }
int unlock() { return !ReleaseMutex(mutex); }
~Mutex() { CloseHandle(mutex); }
#endif
-private:
- MutexHandle mutex;
-
- friend class Semaphore;
};
/**
*/
class MutexLock
{
+private:
+ Mutex &mutex;
+
public:
MutexLock(Mutex &m, bool l=true): mutex(m) { if(l) mutex.lock(); }
~MutexLock() { mutex.unlock(); }
int lock() { return mutex.lock(); }
private:
- Mutex &mutex;
-
MutexLock(const MutexLock &);
MutexLock &operator=(const MutexLock &);
};
Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_CORE_REFPTR_H_
#define MSP_CORE_REFPTR_H_
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef WIN32
#include <sys/time.h>
#endif
init();
}
+void Semaphore::init()
+{
+#ifdef WIN32
+ count=0;
+ sem=CreateSemaphore(0, 0, 32, 0);
+#else
+ pthread_cond_init(&sem, 0);
+#endif
+}
+
+Semaphore::~Semaphore()
+{
+ if(own_mutex)
+ delete mutex;
+#ifdef WIN32
+ CloseHandle(sem);
+#else
+ pthread_cond_destroy(&sem);
+#endif
+}
+
#ifdef WIN32
int Semaphore::signal()
{
#endif
}
-Semaphore::~Semaphore()
-{
- if(own_mutex)
- delete mutex;
-#ifdef WIN32
- CloseHandle(sem);
-#else
- pthread_cond_destroy(&sem);
-#endif
-}
-
-void Semaphore::init()
-{
-#ifdef WIN32
- count=0;
- sem=CreateSemaphore(0, 0, 32, 0);
-#else
- pthread_cond_init(&sem, 0);
-#endif
-}
-
} // namespace Msp
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_CORE_SEMAPHORE_H_
#define MSP_CORE_SEMAPHORE_H_
class Semaphore
{
-public:
- Semaphore();
- Semaphore(Mutex &);
- int signal();
- int broadcast();
- int wait();
- int wait(const Time::TimeDelta &);
- Mutex &get_mutex() { return *mutex; }
- ~Semaphore();
private:
Mutex *mutex;
bool own_mutex;
unsigned count;
#endif
+public:
+ Semaphore();
+ Semaphore(Mutex &);
+private:
void init();
+public:
+ ~Semaphore();
+
+ int signal();
+ int broadcast();
+ int wait();
+ int wait(const Time::TimeDelta &);
+ Mutex &get_mutex() { return *mutex; }
};
#ifndef WIN32
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef WIN32
#include <signal.h>
#endif
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_CORE_THREAD_H_
#define MSP_CORE_THREAD_H_
*/
class Thread
{
+private:
+ ThreadHandle thread_;
+ bool launched_;
+
+protected:
+ Thread(): launched_(false) { }
public:
+ virtual ~Thread();
+
void join();
void cancel();
void kill();
- virtual ~Thread();
protected:
- Thread(): launched_(false) { }
void launch();
virtual void main()=0;
void check_cancel();
-private:
- ThreadHandle thread_;
- bool launched_;
-
- Thread(const Thread &);
- Thread &operator=(const Thread &);
+private:
static
#ifdef WIN32
DWORD WINAPI
void *
#endif
main_(void *t) { (reinterpret_cast<Thread *>(t))->main(); return 0; }
+
+ Thread(const Thread &);
+ Thread &operator=(const Thread &);
};
} // namespace Msp
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_CORE_TYPES_H_
#define MSP_CORE_TYPES_H_
/* $Id$
This file is part of libmspcore
-Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2008 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
ostream &operator<<(ostream &out, const Backtrace &bt)
{
- const Backtrace::FrameSeq &frames=bt.get_frames();
- for(Backtrace::FrameSeq::const_iterator i=frames.begin(); i!=frames.end(); ++i)
+ const list<Backtrace::StackFrame> &frames=bt.get_frames();
+ for(list<Backtrace::StackFrame>::const_iterator i=frames.begin(); i!=frames.end(); ++i)
out<<*i<<'\n';
return out;
Copyright © 2007 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_DEBUG_BACKTRACE_H_
#define MSP_DEBUG_BACKTRACE_H_
void *address;
std::string file;
std::string symbol;
-
- //StackFrame(void *a, const std::string &s): address(a), symbol(s) { }
};
- typedef std::list<StackFrame> FrameSeq;
- const FrameSeq &get_frames() const { return frames; }
+private:
+ std::list<StackFrame> frames;
+
+public:
+ const std::list<StackFrame> &get_frames() const { return frames; }
static Backtrace create();
-private:
- FrameSeq frames;
};
std::ostream &operator<<(std::ostream &, const Backtrace &);
Copyright © 2007 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_DEBUG_DEMANGLE_H_
#define MSP_DEBUG_DEMANGLE_H_
+/* $Id$
+
+This file is part of libmspcore
+Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
#include "../time/utils.h"
#include "profilingscope.h"
-/* $Id$ */
+/* $Id$
+
+This file is part of libmspcore
+Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
#include <cstdlib>
#include <sstream>
#include <iomanip>
-/* $Id$ */
+/* $Id$
+
+This file is part of libmspcore
+Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
#ifndef MSP_TIME_DATETIME_H_
#define MSP_TIME_DATETIME_H_
*/
class DateTime
{
+private:
+ int year;
+ unsigned char month;
+ unsigned char mday;
+ unsigned char hour;
+ unsigned char minute;
+ unsigned char second;
+ unsigned usec;
+ TimeZone zone;
+
public:
DateTime(const TimeStamp &);
DateTime(const TimeStamp &, const TimeZone &);
DateTime(int, unsigned char, unsigned char);
DateTime(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char);
DateTime(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned);
-
+private:
+ void init(const TimeStamp &);
+ void init(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned);
+
+public:
int get_year() const { return year; }
unsigned char get_month() const { return month; }
unsigned char get_mday() const { return mday; }
TimeStamp get_timestamp() const;
std::string format(const std::string &) const;
std::string format_rfc3339() const;
-private:
- int year;
- unsigned char month;
- unsigned char mday;
- unsigned char hour;
- unsigned char minute;
- unsigned char second;
- unsigned usec;
- TimeZone zone;
- void init(const TimeStamp &);
- void init(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned);
+private:
void add_raw(RawTime);
void normalize();
void validate() const;
-/*
+/* $Id$
+
This file is part of libmspcore
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#include <sstream>
#include <iomanip>
#include "timedelta.h"
-/*
+/* $Id$
+
This file is part of libmspcore
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_TIME_TIMEDELTA_H_
#define MSP_TIME_TIMEDELTA_H_
*/
class TimeDelta
{
+private:
+ RawTime usec;
+
public:
/**
Constructs a zero TimeDelta.
bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
operator const void *() const { return usec ? this : 0; }
-private:
- RawTime usec;
};
template<typename T>
-/*
+/* $Id$
+
This file is part of libmspcore
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
-/*
+/* $Id$
+
This file is part of libmspcore
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
-/*
+/* $Id$
+
This file is part of libmspcore
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_TIME_TIMESTAMP_H_
#define MSP_TIME_TIMESTAMP_H_
*/
class TimeStamp
{
+private:
+ RawTime usec;
+
public:
/**
Construct a TimeStamp that represents an arbitarily distant point in the
TimeStamp operator-(const TimeDelta &t) const { return TimeStamp(usec-t.raw()); }
TimeStamp &operator-=(const TimeDelta &t) { usec-=t.raw(); return *this; }
TimeDelta operator-(const TimeStamp &t) const { return TimeDelta(usec-t.usec); }
+
bool operator>=(const TimeStamp &t) const { return usec>=t.usec; }
bool operator>(const TimeStamp &t) const { return usec>t.usec; }
bool operator<=(const TimeStamp &t) const { return usec<=t.usec; }
bool operator<(const TimeStamp &t) const { return usec<t.usec; }
bool operator==(const TimeStamp &t) const { return usec==t.usec; }
bool operator!=(const TimeStamp &t) const { return usec!=t.usec; }
+
operator const void *() const { return usec>0 ? this : 0; }
static TimeStamp from_unixtime(time_t t) { return TimeStamp(t*1000000LL); }
-private:
- RawTime usec;
};
} // namespace Time
+/* $Id$
+
+This file is part of libmspcore
+Copyright © 2006,2008 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
#ifndef MSP_TIME_TYPES_H_
#define MSP_TIME_TYPES_H_
-/*
+/* $Id$
+
This file is part of libmspcore
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#include "units.h"
namespace Msp {
-/*
+/* $Id$
+
This file is part of libmspcore
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_TIME_UNITS_H_
#define MSP_TIME_UNITS_H_
-/*
+/* $Id$
+
This file is part of libmspcore
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifdef WIN32
#include <windows.h>
#else
-/*
+/* $Id$
+
This file is part of libmspcore
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
+
#ifndef MSP_TIME_UTILS_H_
#define MSP_TIME_UTILS_H_