From d5dd704b2576f878809e87dbb8ff8591b9bdbce4 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 23 May 2011 21:50:15 +0300 Subject: [PATCH] Style updates --- source/core/application.cpp | 30 ++-- source/core/application.h | 4 +- source/core/except.cpp | 6 +- source/core/except.h | 2 +- source/core/getopt.cpp | 234 ++++++++++++++------------------ source/core/getopt.h | 71 ++++++---- source/core/main.cpp | 2 +- source/core/mutex.h | 8 +- source/core/refptr.h | 59 ++++---- source/core/semaphore.cpp | 22 +-- source/core/thread.cpp | 6 +- source/core/thread.h | 2 +- source/core/variant.h | 6 +- source/debug/backtrace.cpp | 12 +- source/debug/demangle.cpp | 6 +- source/debug/profiler.cpp | 32 ++--- source/debug/profilingscope.cpp | 4 +- source/time/datetime.cpp | 142 +++++++++---------- source/time/timedelta.cpp | 12 +- source/time/timedelta.h | 4 +- source/time/timer.cpp | 14 +- source/time/timer.h | 2 +- source/time/timezone.cpp | 64 ++++----- source/time/utils.cpp | 18 +-- 24 files changed, 378 insertions(+), 384 deletions(-) diff --git a/source/core/application.cpp b/source/core/application.cpp index badc523..139f94f 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -19,6 +19,10 @@ using namespace std; namespace Msp { +Application *Application::app_ = 0; +Application::Starter *Application::starter_ = 0; +void *Application::data_ = 0; + Application::Application(): exit_code(0) { } @@ -32,13 +36,13 @@ the library normally does it automatically at program startup. */ int Application::run(int argc, char **argv, void *data) { - static bool called=false; + static bool called = false; if(called) { cerr<<"Trying to call Application::run_app twice!\n"; return 125; } - called=true; + called = true; if(!starter_) { @@ -46,13 +50,13 @@ int Application::run(int argc, char **argv, void *data) return 126; } - data_=data; + data_ = data; try { try { - app_=starter_->create_app(argc, argv); + app_ = starter_->create_app(argc, argv); } catch(const UsageError &e) { @@ -60,9 +64,9 @@ int Application::run(int argc, char **argv, void *data) return 1; } - int result=app_->main(); - Application *a=app_; - app_=0; + int result = app_->main(); + Application *a = app_; + app_ = 0; delete a; return result; } @@ -71,18 +75,18 @@ int Application::run(int argc, char **argv, void *data) delete app_; #ifdef WIN32 - string msg=Debug::demangle(typeid(e).name())+":\n"+e.what(); + string msg = Debug::demangle(typeid(e).name())+":\n"+e.what(); MessageBoxA(0, msg.c_str(), "Uncaught exception", MB_OK|MB_ICONERROR); #else cerr<<"An uncaught exception occurred.\n"; cerr<<" type: "<(&e); + const Exception *exc = dynamic_cast(&e); if(exc && !exc->get_backtrace().get_frames().empty()) { cerr<<" backtrace:\n"; - const list &frames=exc->get_backtrace().get_frames(); + const list &frames = exc->get_backtrace().get_frames(); for(list::const_iterator i=frames.begin(); i!=frames.end(); ++i) cerr<<" "<<*i<<'\n'; } @@ -151,11 +155,7 @@ Application::Starter::Starter() if(starter_) throw InvalidState("Can't create more than one Starter instance"); - starter_=this; + starter_ = this; } -Application *Application::app_=0; -Application::RegBase *Application::reg_app_=0; -void *Application::data_=0; - } // namespace Msp diff --git a/source/core/application.h b/source/core/application.h index 305fbfe..b51ac3c 100644 --- a/source/core/application.h +++ b/source/core/application.h @@ -36,6 +36,8 @@ private: static Application *app_; static void *data_; + Application(const Application &); + Application &operator=(const Application &); protected: Application(); public: @@ -55,8 +57,6 @@ private: static void sighandler_(int); }; - Application(const Application &); - Application &operator=(const Application &); template class RegisteredApplication: public Application diff --git a/source/core/except.cpp b/source/core/except.cpp index 90deb40..bdcdd7d 100644 --- a/source/core/except.cpp +++ b/source/core/except.cpp @@ -20,14 +20,14 @@ Exception::Exception(const string &w): wot(w) { #ifdef WITH_EXCEPTION_BACKTRACE - bt=Debug::Backtrace::create(); + bt = Debug::Backtrace::create(); #endif } Exception &Exception::at(const std::string &w) throw() { - wer=w; - wot=wer+": "+wot; + wer = w; + wot = wer+": "+wot; return *this; } diff --git a/source/core/except.h b/source/core/except.h index 6d34d4b..a58b3ca 100644 --- a/source/core/except.h +++ b/source/core/except.h @@ -77,7 +77,7 @@ private: bool brief; public: - UsageError(const std::string &r, bool b=true): Exception(r), brief(b) { } + UsageError(const std::string &r, bool b = true): Exception(r), brief(b) { } bool get_brief() const { return brief; } }; diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index 4989775..74f1476 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -1,9 +1,10 @@ /* $Id$ This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ + #include "getopt.h" using namespace std; @@ -16,102 +17,25 @@ GetOpt::~GetOpt() delete *i; } -/** -Generates a single line that gives an overview about the known options. - -@param argv0 The program name to be used in the usage string - -@return The generated usage string -*/ -string GetOpt::generate_usage(const string &argv0) const +GetOpt::OptBase &GetOpt::get_option(char s) { - ostringstream line; - - line<::const_iterator i=opts.begin(); i!=opts.end(); ++i) - { - line<<" ["; - if((*i)->get_short()) - { - line<<'-'<<(*i)->get_short(); - if(!(*i)->get_long().empty()) - line<<'|'; - else if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<'['<<(*i)->get_metavar()<<']'; - else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<' '<<(*i)->get_metavar(); - } - if(!(*i)->get_long().empty()) - { - line<<"--"<<(*i)->get_long(); - - if((*i)->get_arg_type()==OPTIONAL_ARG) - line<<"[="<<(*i)->get_metavar()<<']'; - else if((*i)->get_arg_type()==REQUIRED_ARG) - line<<'='<<(*i)->get_metavar(); - } - line<<']'; - } - - return line.str(); + for(list::iterator i=opts.begin(); i!=opts.end(); ++i) + if((*i)->get_short()==s) + return **i; + throw UsageError(string("Unknown option -")+s); } -/** -Generates help for known options in tabular format, one option per line. -The returned string will have a linefeed at the end. -*/ -string GetOpt::generate_help() const +GetOpt::OptBase &GetOpt::get_option(const string &l) { - bool any_short=false; - for(list::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i) - any_short=(*i)->get_short(); - - string::size_type maxw=0; - list switches; - for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i) - { - ostringstream swtch; - if((*i)->get_short()) - { - swtch<<'-'<<(*i)->get_short(); - if(!(*i)->get_long().empty()) - swtch<<", "; - else if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch<<'['<<(*i)->get_metavar()<<']'; - else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch<<' '<<(*i)->get_metavar(); - } - else if(any_short) - swtch<<" "; - if(!(*i)->get_long().empty()) - { - swtch<<"--"<<(*i)->get_long(); - - if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch<<"[="<<(*i)->get_metavar()<<']'; - else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch<<'='<<(*i)->get_metavar(); - } - switches.push_back(swtch.str()); - maxw=max(maxw, switches.back().size()); - } - - string result; - list::const_iterator j=switches.begin(); - for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j) - { - result+=" "+*j; - result+=string(maxw+2-j->size(), ' '); - result+=(*i)->get_help(); - result+='\n'; - } - - return result; + for(list::iterator i=opts.begin(); i!=opts.end(); ++i) + if((*i)->get_long()==l) + return **i; + throw UsageError(string("Unknown option --")+l); } void GetOpt::operator()(unsigned argc, const char *const *argv) { - unsigned i=1; + unsigned i = 1; for(; i::iterator i=opts.begin(); i!=opts.end(); ++i) - if((*i)->get_short()==s) - return **i; - throw UsageError(string("Unknown option -")+s); -} - -GetOpt::OptBase &GetOpt::get_option(const string &l) -{ - for(list::iterator i=opts.begin(); i!=opts.end(); ++i) - if((*i)->get_long()==l) - return **i; - throw UsageError(string("Unknown option --")+l); -} - -/** -Processes the given argument as a long option. - -@param argp Pointer to the argument - -@return The number of arguments eaten (1 or 2) -*/ unsigned GetOpt::process_long(const char *const *argp) { // Skip the -- - const char *arg=argp[0]+2; + const char *arg = argp[0]+2; // See if the argument contains an = - unsigned equals=0; + unsigned equals = 0; for(; arg[equals] && arg[equals]!='='; ++equals) ; - OptBase &opt=get_option(string(arg, equals)); + OptBase &opt = get_option(string(arg, equals)); if(arg[equals]) // Process the part after the = as option argument @@ -186,22 +87,15 @@ unsigned GetOpt::process_long(const char *const *argp) return 1; } -/** -Processes short options from the given argument. - -@param argp Pointer to the argument - -@return The number of arguments eaten (1 or 2) -*/ unsigned GetOpt::process_short(const char *const *argp) { // Skip the - - const char *arg=argp[0]+1; + const char *arg = argp[0]+1; // Loop through all characters in the argument for(; *arg; ++arg) { - OptBase &opt=get_option(*arg); + OptBase &opt = get_option(*arg); if(arg[1] && opt.get_arg_type()!=NO_ARG) { @@ -225,6 +119,88 @@ unsigned GetOpt::process_short(const char *const *argp) return 1; } +string GetOpt::generate_usage(const string &argv0) const +{ + ostringstream line; + + line<::const_iterator i=opts.begin(); i!=opts.end(); ++i) + { + line<<" ["; + if((*i)->get_short()) + { + line<<'-'<<(*i)->get_short(); + if(!(*i)->get_long().empty()) + line<<'|'; + else if((*i)->get_arg_type()==OPTIONAL_ARG) + line<<'['<<(*i)->get_metavar()<<']'; + else if((*i)->get_arg_type()==REQUIRED_ARG) + line<<' '<<(*i)->get_metavar(); + } + if(!(*i)->get_long().empty()) + { + line<<"--"<<(*i)->get_long(); + + if((*i)->get_arg_type()==OPTIONAL_ARG) + line<<"[="<<(*i)->get_metavar()<<']'; + else if((*i)->get_arg_type()==REQUIRED_ARG) + line<<'='<<(*i)->get_metavar(); + } + line<<']'; + } + + return line.str(); +} + +string GetOpt::generate_help() const +{ + bool any_short = false; + for(list::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i) + any_short = (*i)->get_short(); + + string::size_type maxw = 0; + list switches; + for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i) + { + ostringstream swtch; + if((*i)->get_short()) + { + swtch<<'-'<<(*i)->get_short(); + if(!(*i)->get_long().empty()) + swtch<<", "; + else if((*i)->get_arg_type()==OPTIONAL_ARG) + swtch<<'['<<(*i)->get_metavar()<<']'; + else if((*i)->get_arg_type()==REQUIRED_ARG) + swtch<<' '<<(*i)->get_metavar(); + } + else if(any_short) + swtch<<" "; + if(!(*i)->get_long().empty()) + { + swtch<<"--"<<(*i)->get_long(); + + if((*i)->get_arg_type()==OPTIONAL_ARG) + swtch<<"[="<<(*i)->get_metavar()<<']'; + else if((*i)->get_arg_type()==REQUIRED_ARG) + swtch<<'='<<(*i)->get_metavar(); + } + switches.push_back(swtch.str()); + maxw = max(maxw, switches.back().size()); + } + + string result; + list::const_iterator j = switches.begin(); + for(list::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j) + { + result += " "+*j; + result += string(maxw+2-j->size(), ' '); + result += (*i)->get_help(); + result += '\n'; + } + + return result; +} + GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a): shrt(s), @@ -236,14 +212,14 @@ GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a): GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h) { - help=h; + help = h; return *this; } GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m) { - help=h; - metavar=m; + help = h; + metavar = m; return *this; } diff --git a/source/core/getopt.h b/source/core/getopt.h index 94e8b1e..8e0f49a 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -1,9 +1,10 @@ /* $Id$ This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ + #ifndef MSP_CORE_GETOPT_H_ #define MSP_CORE_GETOPT_H_ @@ -26,29 +27,31 @@ public: class OptBase { - public: - OptBase &set_help(const std::string &); - OptBase &set_help(const std::string &, const std::string &); - char get_short() const { return shrt; } - const std::string &get_long() const { return lng; } - ArgType get_arg_type() const { return arg_type; } - const std::string &get_help() const { return help; } - const std::string &get_metavar() const { return metavar; } - unsigned get_seen_count() const { return seen_count; } - void process(); - void process(const std::string &); - virtual ~OptBase() { } protected: - char shrt; + char shrt; std::string lng; - ArgType arg_type; - unsigned seen_count; + ArgType arg_type; + unsigned seen_count; std::string help; std::string metavar; OptBase(char, const std::string &, ArgType); - virtual void store()=0; - virtual void store(const std::string &)=0; + public: + virtual ~OptBase() { } + + OptBase &set_help(const std::string &); + OptBase &set_help(const std::string &, const std::string &); + char get_short() const { return shrt; } + const std::string &get_long() const { return lng; } + ArgType get_arg_type() const { return arg_type; } + const std::string &get_help() const { return help; } + const std::string &get_metavar() const { return metavar; } + unsigned get_seen_count() const { return seen_count; } + void process(); + void process(const std::string &); + protected: + virtual void store() = 0; + virtual void store(const std::string &) = 0; }; private: @@ -68,7 +71,7 @@ private: if(ss.fail()) throw UsageError("Invalid argument for --"+lng); - data=tmp; + data = tmp; } private: T &data; @@ -106,33 +109,47 @@ public: const std::vector &get_args() const { return args; } template - OptBase &add_option(char s, const std::string &l, T &d, ArgType a=NO_ARG) + OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG) { opts.push_back(new Option(s, l, d, a)); return *opts.back(); } template - OptBase &add_option(char s, const std::string &l, std::list &d, ArgType a=REQUIRED_ARG) + OptBase &add_option(char s, const std::string &l, std::list &d, ArgType a = REQUIRED_ARG) { opts.push_back(new ListOption >(s, l, d, a)); return *opts.back(); } template 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 &); + +public: + /** Processes argc/argv style command line arguments. The contents of argv + will be unchanged; use get_args to access non-option arguments. */ + void operator()(unsigned, const char *const *); + +private: + /** Processes a long option. Returns the number of arguments eaten. */ unsigned process_long(const char *const *); + + /** Processes short options. Returns the number of arguments eaten. */ unsigned process_short(const char *const *); + +public: + /** Generates a single line that describes known options. */ + std::string generate_usage(const std::string &) const; + + /** Generates help for known options in tabular format, one option per + line. The returned string will have a linefeed at the end. */ + std::string generate_help() const; }; -template<> inline void GetOpt::Option::store() { data=true; } +template<> inline void GetOpt::Option::store() { data = true; } template<> inline void GetOpt::Option::store() { ++data; } template<> inline void GetOpt::Option::store(const std::string &a) -{ data=a; } +{ data = a; } template<> inline void GetOpt::ListOption >::store(const std::string &a) { data.push_back(a); } diff --git a/source/core/main.cpp b/source/core/main.cpp index 4a90b74..f73ef81 100644 --- a/source/core/main.cpp +++ b/source/core/main.cpp @@ -15,7 +15,7 @@ Distributed under the LGPL int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nCmdShow*/) { int argc = 0; - LPWSTR *argv=CommandLineToArgvW(GetCommandLineW(), &argc); + LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc); return Msp::Application::run(argc, (char **)argv, hInstance); } #endif diff --git a/source/core/mutex.h b/source/core/mutex.h index 50a64e5..c0c2682 100644 --- a/source/core/mutex.h +++ b/source/core/mutex.h @@ -28,7 +28,7 @@ public: int unlock() { return pthread_mutex_unlock(&mutex); } ~Mutex() { pthread_mutex_destroy(&mutex); } #else - Mutex() { mutex=CreateMutex(0, false, 0); } + Mutex() { mutex = CreateMutex(0, false, 0); } int lock() { return WaitForSingleObject(mutex, INFINITE)==WAIT_OBJECT_0; } int trylock() { return WaitForSingleObject(mutex, 0)==WAIT_OBJECT_0; } int unlock() { return !ReleaseMutex(mutex); } @@ -45,7 +45,7 @@ private: Mutex &mutex; public: - MutexLock(Mutex &m, bool l=true): mutex(m) { if(l) mutex.lock(); } + MutexLock(Mutex &m, bool l = true): mutex(m) { if(l) mutex.lock(); } ~MutexLock() { mutex.unlock(); } int lock() { return mutex.lock(); } @@ -66,7 +66,7 @@ public: T &operator*() const { return *data; } T *operator->() const { return data; } - void clear() { mutex=0; data=0; } + void clear() { mutex=0; data = 0; } private: RefPtr mutex; T *data; @@ -80,7 +80,7 @@ public: MutexPtr(const MutexPtr &p): RefCount(p), mutex(p.mutex), data(p.data) { } T &operator*() const { return *data; } T *operator->() const { return data; } - void clear() { decref(); data=0; } + void clear() { decref(); data = 0; } ~MutexPtr() { decref(); } protected: Mutex &mutex; diff --git a/source/core/refptr.h b/source/core/refptr.h index ddada0b..20450f1 100644 --- a/source/core/refptr.h +++ b/source/core/refptr.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspcore -Copyright © 2006-2007, 2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2006-2007, 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -17,22 +17,33 @@ destroyed, the data is deleted as well. template class RefPtr { + template friend class RefPtr; + private: enum { KEEP = 1U<<(sizeof(unsigned)*8-1) }; + T *data; + unsigned *count; + public: RefPtr(): data(0), count(0) { } RefPtr(T *d): data(d), count(data ? new unsigned(1) : 0) { } +private: + RefPtr(T *d, unsigned *c): data(d), count(d ? c : 0) { incref(); } - // Must have this or the compiler will generate a default copy-c'tor +public: + /* Must have this or the compiler will generate a default copy-c'tor despite + the template version */ RefPtr(const RefPtr &p): data(p.data), count(p.count) { incref(); } template RefPtr(const RefPtr &p): data(p.data), count(p.count) { incref(); } + ~RefPtr() { decref(); } + RefPtr &operator=(T *d) { decref(); @@ -46,14 +57,22 @@ public: template RefPtr &operator=(const RefPtr &p) { return assign(p); } - - ~RefPtr() { decref(); } - /** - Makes the RefPtr release its reference of the data. Note that if there are - other RefPtrs left with the same data, it might still get deleted - automatically. - */ +private: + template + RefPtr &assign(const RefPtr &p) + { + decref(); + data = p.data; + count = p.count; + incref(); + return *this; + } + +public: + /** Makes the RefPtr release its reference of the data without deleting it. + Note that if there are other RefPtrs left with the same data, it might + still get deleted automatically. */ T *release() { T *d = data; @@ -63,10 +82,8 @@ public: return d; } - /** - Marks the data to not be deleted. This affects all RefPtrs with the same - data. - */ + /** Marks the data to not be deleted. This affects all RefPtrs with the + same data. */ void keep() { if(count) @@ -82,23 +99,7 @@ public: static RefPtr cast_dynamic(const RefPtr &p) { return RefPtr(dynamic_cast(p.data), p.count); } - template friend class RefPtr; private: - T *data; - unsigned *count; - - RefPtr(T *d, unsigned *c): data(d), count(d ? c : 0) { incref(); } - - template - RefPtr &assign(const RefPtr &p) - { - decref(); - data = p.data; - count = p.count; - incref(); - return *this; - } - void incref() { if(!count) return; diff --git a/source/core/semaphore.cpp b/source/core/semaphore.cpp index bd1180b..139cae2 100644 --- a/source/core/semaphore.cpp +++ b/source/core/semaphore.cpp @@ -33,8 +33,8 @@ Semaphore::Semaphore(Mutex &m): void Semaphore::init() { #ifdef WIN32 - count=0; - sem=CreateSemaphore(0, 0, 32, 0); + count = 0; + sem = CreateSemaphore(0, 0, 32, 0); #else pthread_cond_init(&sem, 0); #endif @@ -57,9 +57,9 @@ int Semaphore::signal() if(count==0) return 0; - int ret=!ReleaseSemaphore(sem, 1, 0); + int ret = !ReleaseSemaphore(sem, 1, 0); - unsigned old_count=count; + unsigned old_count = count; mutex->unlock(); while(count==old_count) Sleep(0); @@ -72,7 +72,7 @@ int Semaphore::broadcast() { if(count==0) return 0; - int ret=!ReleaseSemaphore(sem, count, 0); + int ret = !ReleaseSemaphore(sem, count, 0); mutex->unlock(); while(count) @@ -86,7 +86,7 @@ int Semaphore::wait() { ++count; mutex->unlock(); - DWORD ret=WaitForSingleObject(sem, INFINITE); + DWORD ret = WaitForSingleObject(sem, INFINITE); mutex->lock(); --count; @@ -97,13 +97,13 @@ int Semaphore::wait() int Semaphore::wait(const Time::TimeDelta &d) { #ifndef WIN32 - Time::TimeStamp ts=Time::now()+d; + Time::TimeStamp ts = Time::now()+d; timespec timeout; - timeout.tv_sec=ts.raw()/1000000; - timeout.tv_nsec=(ts.raw()%1000000)*1000; + timeout.tv_sec = ts.raw()/1000000; + timeout.tv_nsec = (ts.raw()%1000000)*1000; - int r=pthread_cond_timedwait(&sem, &mutex->mutex, &timeout); + int r = pthread_cond_timedwait(&sem, &mutex->mutex, &timeout); if(r==ETIMEDOUT) return 1; else if(r) @@ -112,7 +112,7 @@ int Semaphore::wait(const Time::TimeDelta &d) #else ++count; mutex->lock(); - DWORD ret=WaitForSingleObject(sem, (DWORD)(d/Time::usec)); + DWORD ret = WaitForSingleObject(sem, (DWORD)(d/Time::usec)); mutex->unlock(); --count; return ret==WAIT_OBJECT_0; diff --git a/source/core/thread.cpp b/source/core/thread.cpp index 0fc86cf..810b1f8 100644 --- a/source/core/thread.cpp +++ b/source/core/thread.cpp @@ -26,7 +26,7 @@ void Thread::join() #else pthread_join(thread_, 0); #endif - launched_=false; + launched_ = false; } /** @@ -67,11 +67,11 @@ void Thread::launch() #ifdef WIN32 DWORD dummy; // Win9x needs the lpTthreadId parameter - thread_=CreateThread(0, 0, &main_, this, 0, &dummy); + thread_ = CreateThread(0, 0, &main_, this, 0, &dummy); #else pthread_create(&thread_, 0, &main_, this); #endif - launched_=true; + launched_ = true; } } // namespace Msp diff --git a/source/core/thread.h b/source/core/thread.h index 8d323b3..92923f3 100644 --- a/source/core/thread.h +++ b/source/core/thread.h @@ -35,7 +35,7 @@ public: void kill(); protected: void launch(); - virtual void main()=0; + virtual void main() = 0; void check_cancel(); private: diff --git a/source/core/variant.h b/source/core/variant.h index f78851f..8d6db9a 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -44,14 +44,14 @@ public: Variant &operator=(const T &v) { delete store; - store=new Store::Type>(v); + store = new Store::Type>(v); return *this; } Variant &operator=(const Variant &v) { delete store; - store=(v.store ? v.store->clone() : 0); + store = (v.store ? v.store->clone() : 0); return *this; } @@ -59,7 +59,7 @@ public: T &value() const { typedef typename RemoveConst::Type NCT; - Store *s=dynamic_cast *>(store); + Store *s = dynamic_cast *>(store); if(!s) throw InvalidState("Type mismatch"); return s->data; diff --git a/source/debug/backtrace.cpp b/source/debug/backtrace.cpp index 25a5e21..f43a9ed 100644 --- a/source/debug/backtrace.cpp +++ b/source/debug/backtrace.cpp @@ -23,22 +23,22 @@ Backtrace Backtrace::create() { #if !defined(WIN32) && defined(__GLIBC__) void *addresses[50]; - int count=::backtrace(addresses, 50); + int count = ::backtrace(addresses, 50); Backtrace bt; Dl_info dli; for(int i=0; i &frames=bt.get_frames(); + const list &frames = bt.get_frames(); for(list::const_iterator i=frames.begin(); i!=frames.end(); ++i) out<<*i<<'\n'; diff --git a/source/debug/demangle.cpp b/source/debug/demangle.cpp index fc6ca78..053fd52 100644 --- a/source/debug/demangle.cpp +++ b/source/debug/demangle.cpp @@ -20,13 +20,13 @@ string demangle(const string &sym) { #ifdef __GNUC__ int status; - char *dm=abi::__cxa_demangle(sym.c_str(), 0, 0, &status); + char *dm = abi::__cxa_demangle(sym.c_str(), 0, 0, &status); string result; if(status==0) - result=dm; + result = dm; else - result=sym; + result = sym; free(dm); diff --git a/source/debug/profiler.cpp b/source/debug/profiler.cpp index af13800..ba69a61 100644 --- a/source/debug/profiler.cpp +++ b/source/debug/profiler.cpp @@ -24,15 +24,15 @@ void Profiler::set_period(unsigned p) if(p==period) return; - period=p; + period = p; for(map::iterator i=scopes.begin(); i!=scopes.end(); ++i) { - ScopeInfo &si=i->second; + ScopeInfo &si = i->second; if(p==0) si.history.clear(); else si.history.assign(period, Time::zero); - si.hist_pos=0; + si.hist_pos = 0; } } @@ -40,46 +40,46 @@ void Profiler::add_scope(const std::string &name) { if(!scopes.count(name)) { - map::iterator i=scopes.insert(map::value_type(name, ScopeInfo())).first; + map::iterator i = scopes.insert(map::value_type(name, ScopeInfo())).first; i->second.history.resize(period); } } ProfilingScope *Profiler::enter(ProfilingScope *ps) { - ProfilingScope *old=inner; - inner=ps; + ProfilingScope *old = inner; + inner = ps; return old; } void Profiler::record(const string &scope_name, const string &parent, const Time::TimeDelta &time, const Time::TimeDelta &child_t) { - map::iterator i=scopes.find(scope_name); + map::iterator i = scopes.find(scope_name); if(i==scopes.end()) { - i=scopes.insert(map::value_type(scope_name, ScopeInfo())).first; + i = scopes.insert(map::value_type(scope_name, ScopeInfo())).first; i->second.history.resize(period); } - ScopeInfo &si=i->second; + ScopeInfo &si = i->second; ++si.calls; ++si.called_from[parent]; - si.total_time+=time; - si.self_time+=time-child_t; + si.total_time += time; + si.self_time += time-child_t; if(period) { - si.avg_time+=time/period-si.history[si.hist_pos]/period; - si.history[si.hist_pos++]=time; + si.avg_time += time/period-si.history[si.hist_pos]/period; + si.history[si.hist_pos++] = time; if(si.hist_pos>=period) - si.hist_pos-=period; + si.hist_pos -= period; } else - si.avg_time=si.total_time/si.calls; + si.avg_time = si.total_time/si.calls; } const Profiler::ScopeInfo &Profiler::scope(const string &sn) const { - map::const_iterator i=scopes.find(sn); + map::const_iterator i = scopes.find(sn); if(i==scopes.end()) throw KeyError("Unknown scope"); diff --git a/source/debug/profilingscope.cpp b/source/debug/profilingscope.cpp index ffb093a..3922234 100644 --- a/source/debug/profilingscope.cpp +++ b/source/debug/profilingscope.cpp @@ -22,10 +22,10 @@ ProfilingScope::ProfilingScope(Profiler &p, const string &n): ProfilingScope::~ProfilingScope() { - const Time::TimeDelta dt=Time::now()-start_t; + const Time::TimeDelta dt = Time::now()-start_t; if(parent) { - parent->child_t+=dt; + parent->child_t += dt; profiler.record(name, parent->name, dt, child_t); } else diff --git a/source/time/datetime.cpp b/source/time/datetime.cpp index 8623886..6617f86 100644 --- a/source/time/datetime.cpp +++ b/source/time/datetime.cpp @@ -79,80 +79,80 @@ DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, uns void DateTime::add_days(int days) { - int new_year=year; + int new_year = year; /* Leap years have a 400 year cycle, so any 400 consecutive years have a - constant number of days (400*365+97=146097) */ - new_year+=days/146097*400; - days%=146097; + constant number of days (400*365+97 = 146097) */ + new_year += days/146097*400; + days %= 146097; if(days<0) { - new_year-=400; - days+=146097; + new_year -= 400; + days += 146097; } // Fudge factor for leap day - int fudge=(month<=2)?1:0; + int fudge = (month<=2)?1:0; // (Almost) every 4 year cycle has 1 leap year and 3 normal years - unsigned cycles=days/1461; - days%=1461; + unsigned cycles = days/1461; + days %= 1461; - new_year+=cycles*4; + new_year += cycles*4; // See how many non-leap-years we counted as leap years and reclaim the lost days // XXX This breaks with negative years - unsigned missed_leap_days=((year-fudge)%100+cycles*4)/100; + unsigned missed_leap_days = ((year-fudge)%100+cycles*4)/100; if((year-fudge)%400+cycles*4>=400) --missed_leap_days; - days+=missed_leap_days; + days += missed_leap_days; // Count single years from the 4 year cycle - cycles=days/365; - days%=365; + cycles = days/365; + days %= 365; - new_year+=cycles; + new_year += cycles; if((year-fudge)%4+cycles>=4) { // We passed a leap year - decrement days if(days==0) { - days=is_leap_year(new_year-fudge)?365:364; + days = is_leap_year(new_year-fudge)?365:364; --new_year; } else --days; } - year=new_year; + year = new_year; // Step months while(mday+days>month_days(year, month)) { - days-=month_days(year, month); + days -= month_days(year, month); ++month; if(month>12) { ++year; - month=1; + month = 1; } } - mday+=days; + mday += days; } void DateTime::set_timezone(const TimeZone &tz) { - zone=tz; + zone = tz; } void DateTime::convert_timezone(const TimeZone &tz) { add_raw((zone.get_offset()-tz.get_offset()).raw()); - zone=tz; + zone = tz; } DateTime DateTime::operator+(const TimeDelta &td) const @@ -183,19 +183,19 @@ DateTime &DateTime::operator-=(const TimeDelta &td) int DateTime::cmp(const DateTime &dt) const { - if(int c=cmp_(year, dt.year)) + if(int c = cmp_(year, dt.year)) return c; - if(int c=cmp_(month, dt.month)) + if(int c = cmp_(month, dt.month)) return c; - if(int c=cmp_(mday, dt.mday)) + if(int c = cmp_(mday, dt.mday)) return c; - if(int c=cmp_(hour, dt.hour)) + if(int c = cmp_(hour, dt.hour)) return c; - if(int c=cmp_(minute, dt.minute)) + if(int c = cmp_(minute, dt.minute)) return c; - if(int c=cmp_(second, dt.second)) + if(int c = cmp_(second, dt.second)) return c; - if(int c=cmp_(usec, dt.usec)) + if(int c = cmp_(usec, dt.usec)) return c; return 0; } @@ -205,14 +205,14 @@ TimeStamp DateTime::get_timestamp() const if(year<-289701 || year>293641) throw InvalidState("DateTime is not representable as a TimeStamp"); - RawTime raw=(((hour*60LL)+minute)*60+second)*1000000+usec; - int days=(year-1970)*365; - days+=(year-1)/4-(year-1)/100+(year-1)/400-477; + RawTime raw = (((hour*60LL)+minute)*60+second)*1000000+usec; + int days = (year-1970)*365; + days += (year-1)/4-(year-1)/100+(year-1)/400-477; for(unsigned i=1; i(offs/Time::min)); + int m = abs(static_cast(offs/Time::min)); ss<<(offs(raw/86400000000LL); - raw%=86400000000LL; + int days = static_cast(raw/86400000000LL); + raw %= 86400000000LL; if(raw<0) { --days; - raw+=86400000000LL; + raw += 86400000000LL; } - usec+=raw%1000000; raw/=1000000; - second+=raw%60; raw/=60; - minute+=raw%60; raw/=60; - hour+=raw%24; raw/=24; + usec+=raw%1000000; raw /= 1000000; + second+=raw%60; raw /= 60; + minute+=raw%60; raw /= 60; + hour+=raw%24; raw /= 24; add_days(days); normalize(); @@ -317,22 +317,22 @@ void DateTime::add_raw(RawTime raw) void DateTime::normalize() { - second+=usec/1000000; - usec%=1000000; - minute+=second/60; - second%=60; - hour+=minute/60; - minute%=60; - mday+=hour/24; - hour%=24; + second += usec/1000000; + usec %= 1000000; + minute += second/60; + second %= 60; + hour += minute/60; + minute %= 60; + mday += hour/24; + hour %= 24; while(mday>month_days(year, month)) { - mday-=month_days(year, month); + mday -= month_days(year, month); ++month; if(month>12) { ++year; - month=1; + month = 1; } } } diff --git a/source/time/timedelta.cpp b/source/time/timedelta.cpp index d94d8c5..c74ffce 100644 --- a/source/time/timedelta.cpp +++ b/source/time/timedelta.cpp @@ -25,8 +25,8 @@ void print_part(ostream &out, RawTime &value, RawTime unit, char sep, bool &firs out<slot; - const TimeStamp &stamp=next->get_timeout(); - const TimeStamp t=now(); + next = slots.begin()->slot; + const TimeStamp &stamp = next->get_timeout(); + const TimeStamp t = now(); if(stamp<=t) break; else if(block) @@ -125,7 +125,7 @@ bool Timer::Slot::increment() { if(!interval) return false; - timeout+=interval; + timeout += interval; return true; } diff --git a/source/time/timer.h b/source/time/timer.h index a0dabcd..26ebf64 100644 --- a/source/time/timer.h +++ b/source/time/timer.h @@ -84,7 +84,7 @@ public: Note: If there are no active timers when a blocking tick is executed, it won't return until a timer is added from another thread. */ - void tick(bool block=true); + void tick(bool block = true); TimeStamp get_next_timeout() const; }; diff --git a/source/time/timezone.cpp b/source/time/timezone.cpp index 43d6d10..9c14624 100644 --- a/source/time/timezone.cpp +++ b/source/time/timezone.cpp @@ -28,9 +28,9 @@ using Msp::Time::TimeZone; #ifndef WIN32 long get_long(char *&ptr) { - long result=0; + long result = 0; for(unsigned i=0; i<4; ++i) - result=(result<<8)+static_cast(*ptr++); + result = (result<<8)+static_cast(*ptr++); return result; } #endif @@ -39,64 +39,64 @@ TimeZone get_local_timezone() { #ifdef WIN32 TIME_ZONE_INFORMATION tzinfo; - DWORD dst=GetTimeZoneInformation(&tzinfo); + DWORD dst = GetTimeZoneInformation(&tzinfo); if(dst==TIME_ZONE_ID_INVALID) throw Msp::SystemError("Failed to get time zone information", GetLastError()); - int offset=tzinfo.Bias; + int offset = tzinfo.Bias; if(dst==TIME_ZONE_ID_STANDARD) - offset+=tzinfo.StandardBias; + offset += tzinfo.StandardBias; else if(dst==TIME_ZONE_ID_DAYLIGHT) - offset+=tzinfo.DaylightBias; + offset += tzinfo.DaylightBias; return TimeZone(offset); #else - int fd=open("/etc/localtime", O_RDONLY); + int fd = open("/etc/localtime", O_RDONLY); if(fd>=-1) { char hdr[44]; - int len=read(fd, hdr, sizeof(hdr)); - long gmtoff=-1; + int len = read(fd, hdr, sizeof(hdr)); + long gmtoff = -1; string name; if(len==44 && hdr[0]=='T' && hdr[1]=='Z' && hdr[2]=='i' && hdr[3]=='f') { - char *ptr=hdr+20; - long isgmtcnt=get_long(ptr); - long isstdcnt=get_long(ptr); - long leapcnt=get_long(ptr); - long timecnt=get_long(ptr); - long typecnt=get_long(ptr); - long charcnt=get_long(ptr); - int size=timecnt*5+typecnt*6+isgmtcnt+isstdcnt+leapcnt*8+charcnt; + char *ptr = hdr+20; + long isgmtcnt = get_long(ptr); + long isstdcnt = get_long(ptr); + long leapcnt = get_long(ptr); + long timecnt = get_long(ptr); + long typecnt = get_long(ptr); + long charcnt = get_long(ptr); + int size = timecnt*5+typecnt*6+isgmtcnt+isstdcnt+leapcnt*8+charcnt; char buf[size]; - len=read(fd, buf, size); + len = read(fd, buf, size); if(len==size) { - ptr=buf; - int index=-1; - time_t cur_time=Msp::Time::now().to_unixtime(); + ptr = buf; + int index = -1; + time_t cur_time = Msp::Time::now().to_unixtime(); for(int i=0; i0) - index=ptr[index]; - ptr+=timecnt; + index = ptr[index]; + ptr += timecnt; - int abbrind=0; + int abbrind = 0; for(int i=0; i=0 && i==index) || (index<0 && !ptr[4] && gmtoff==-1)) { - gmtoff=get_long(ptr); + gmtoff = get_long(ptr); ++ptr; - abbrind=*ptr++; + abbrind = *ptr++; } else - ptr+=6; + ptr += 6; } - name=ptr+abbrind; + name = ptr+abbrind; } } close(fd); @@ -124,13 +124,13 @@ TimeZone::TimeZone(int minutes_west): { ostringstream ss; ss.fill('0'); - int m=abs(minutes_west); + int m = abs(minutes_west); ss<<"UTC"<<(minutes_west<0 ? '-' : '+')<(ft.dwHighDateTime)<<32))/10; + epoch = (ft.dwLowDateTime+(static_cast(ft.dwHighDateTime)<<32))/10; } FILETIME ft; -- 2.45.2