From: Mikko Rasa Date: Sun, 29 Aug 2021 12:15:50 +0000 (+0300) Subject: Use C++11 features with containers X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f24e7b96e76b63c9b9b8a6bce4c7a9db64276ea8;p=libs%2Fcore.git Use C++11 features with containers --- diff --git a/source/core/application.cpp b/source/core/application.cpp index 290f20b..2c6dc96 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -80,7 +80,7 @@ int Application::run(int argc, char **argv, void *data, void (*created_callback) else { IO::print(IO::cerr, " what(): %s\n", lines.front()); - for(vector::const_iterator i=lines.begin(); ++i!=lines.end(); ) + for(auto i=lines.begin(); ++i!=lines.end(); ) IO::print(IO::cerr, " %s\n", *i); } } diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index 0f8886a..ec821e0 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -1,4 +1,5 @@ #include +#include "algorithm.h" #include "getopt.h" using namespace std; @@ -13,10 +14,10 @@ GetOpt::GetOpt(): GetOpt::~GetOpt() { - for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i) - delete *i; - for(ArgumentList::iterator i=args.begin(); i!=args.end(); ++i) - delete *i; + for(OptionImpl *i: opts) + delete i; + for(ArgumentImpl *i: args) + delete i; } GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a) @@ -26,7 +27,7 @@ GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, if(t.is_list() && a!=REQUIRED_ARG) throw invalid_argument("GetOpt::add_option"); - for(OptionList::iterator i=opts.begin(); i!=opts.end(); ) + for(auto i=opts.begin(); i!=opts.end(); ) { if((s!=0 && (*i)->get_short()==s) || (*i)->get_long()==l) { @@ -41,43 +42,43 @@ GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, return *opts.back(); } -GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType a) +GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType y) { - if(a==NO_ARG) + if(y==NO_ARG) throw invalid_argument("GetOpt::add_argument"); bool have_list = false; bool have_optional = false; - for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i) + for(const ArgumentImpl *a: args) { - if((*i)->is_list_store()) + if(a->is_list_store()) have_list = true; - else if((*i)->get_type()==OPTIONAL_ARG) + else if(a->get_type()==OPTIONAL_ARG) have_optional = true; } - if(have_optional && (t.is_list() || a!=OPTIONAL_ARG)) + if(have_optional && (t.is_list() || y!=OPTIONAL_ARG)) throw invalid_argument("GetOpt::add_argument"); - if(have_list && (t.is_list() || a==OPTIONAL_ARG)) + if(have_list && (t.is_list() || y==OPTIONAL_ARG)) throw invalid_argument("GetOpt::add_argument"); - args.push_back(new ArgumentImpl(n, t, a)); + args.push_back(new ArgumentImpl(n, t, y)); return *args.back(); } GetOpt::OptionImpl &GetOpt::get_option(char s) { - for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i) - if((*i)->get_short()==s) - return **i; + auto i = find_if(opts, [s](const OptionImpl *o){ return o->get_short()==s; }); + if(i!=opts.end()) + return **i; throw usage_error(string("Unknown option -")+s); } GetOpt::OptionImpl &GetOpt::get_option(const string &l) { - for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i) - if((*i)->get_long()==l) - return **i; + auto i = find_if(opts, [&l](const OptionImpl *o){ return o->get_long()==l; }); + if(i!=opts.end()) + return **i; throw usage_error(string("Unknown option --")+l); } @@ -110,12 +111,12 @@ void GetOpt::operator()(unsigned argc, const char *const *argv) args_raw.push_back(argv[i]); i = 0; - for(ArgumentList::const_iterator j=args.begin(); j!=args.end(); ++j) + for(auto j=args.begin(); j!=args.end(); ++j) { if((*j)->is_list_store()) { unsigned end = args_raw.size(); - for(ArgumentList::const_iterator k=j; ++k!=args.end(); ) + for(auto k=j; ++k!=args.end(); ) --end; if(i==end && (*j)->get_type()==REQUIRED_ARG) throw usage_error((*j)->get_name()+" is required"); @@ -213,41 +214,41 @@ string GetOpt::generate_usage(const string &argv0, bool compact) const result += " [options]"; else { - for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i) + for(const OptionImpl *o: opts) { result += " ["; - if((*i)->get_short()) + if(o->get_short()) { - result += format("-%c", (*i)->get_short()); - if(!(*i)->get_long().empty()) + result += format("-%c", o->get_short()); + if(!o->get_long().empty()) result += '|'; - else if((*i)->get_arg_type()==OPTIONAL_ARG) - result += format("[%s]", (*i)->get_metavar()); - else if((*i)->get_arg_type()==REQUIRED_ARG) - result += format(" %s", (*i)->get_metavar()); + else if(o->get_arg_type()==OPTIONAL_ARG) + result += format("[%s]", o->get_metavar()); + else if(o->get_arg_type()==REQUIRED_ARG) + result += format(" %s", o->get_metavar()); } - if(!(*i)->get_long().empty()) + if(!o->get_long().empty()) { - result += format("--%s", (*i)->get_long()); + result += format("--%s", o->get_long()); - if((*i)->get_arg_type()==OPTIONAL_ARG) - result += format("[=%s]", (*i)->get_metavar()); - else if((*i)->get_arg_type()==REQUIRED_ARG) - result += format("=%s", (*i)->get_metavar()); + if(o->get_arg_type()==OPTIONAL_ARG) + result += format("[=%s]", o->get_metavar()); + else if(o->get_arg_type()==REQUIRED_ARG) + result += format("=%s", o->get_metavar()); } result += ']'; } } - for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i) + for(const ArgumentImpl *a: args) { result += ' '; - if((*i)->get_type()==OPTIONAL_ARG) + if(a->get_type()==OPTIONAL_ARG) result += '['; - result += format("<%s>", (*i)->get_name()); - if((*i)->is_list_store()) + result += format("<%s>", a->get_name()); + if(a->is_list_store()) result += " ..."; - if((*i)->get_type()==OPTIONAL_ARG) + if(a->get_type()==OPTIONAL_ARG) result += ']'; } @@ -256,58 +257,56 @@ string GetOpt::generate_usage(const string &argv0, bool compact) const string GetOpt::generate_help() const { - bool any_short = false; - for(OptionList::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i) - any_short = (*i)->get_short(); + bool any_short = any_of(opts.begin(), opts.end(), [](const OptionImpl *o){ return o->get_short(); }); string::size_type maxw = 0; list switches; - for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i) + for(const OptionImpl *o: opts) { string swtch; - if((*i)->get_short()) + if(o->get_short()) { - swtch += format("-%c", (*i)->get_short()); - if(!(*i)->get_long().empty()) + swtch += format("-%c", o->get_short()); + if(!o->get_long().empty()) swtch += ", "; - else if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch += format("[%s]", (*i)->get_metavar()); - else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch += format(" %s", (*i)->get_metavar()); + else if(o->get_arg_type()==OPTIONAL_ARG) + swtch += format("[%s]", o->get_metavar()); + else if(o->get_arg_type()==REQUIRED_ARG) + swtch += format(" %s", o->get_metavar()); } else if(any_short) swtch += " "; - if(!(*i)->get_long().empty()) + if(!o->get_long().empty()) { - swtch += format("--%s", (*i)->get_long()); + swtch += format("--%s", o->get_long()); - if((*i)->get_arg_type()==OPTIONAL_ARG) - swtch += format("[=%s]", (*i)->get_metavar()); - else if((*i)->get_arg_type()==REQUIRED_ARG) - swtch += format("=%s", (*i)->get_metavar()); + if(o->get_arg_type()==OPTIONAL_ARG) + swtch += format("[=%s]", o->get_metavar()); + else if(o->get_arg_type()==REQUIRED_ARG) + swtch += format("=%s", o->get_metavar()); } switches.push_back(swtch); maxw = max(maxw, swtch.size()); } list pargs; - for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i) + for(const ArgumentImpl *a: args) { - string parg = format("<%s>", (*i)->get_name()); + string parg = format("<%s>", a->get_name()); pargs.push_back(parg); maxw = max(maxw, parg.size()); } string result; result += "Options:\n"; - list::const_iterator j = switches.begin(); - for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j) + auto j = switches.begin(); + for(auto i=opts.begin(); i!=opts.end(); ++i, ++j) result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help()); if(!pargs.empty()) { result += "\nArguments:\n"; j = pargs.begin(); - for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i, ++j) + for(auto i=args.begin(); i!=args.end(); ++i, ++j) result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help()); } diff --git a/source/core/getopt.h b/source/core/getopt.h index 3b579a6..27ce4cf 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -203,12 +203,9 @@ private: { data.push_back(lexical_cast(a)); } }; - typedef std::list OptionList; - typedef std::list ArgumentList; - bool help; - OptionList opts; - ArgumentList args; + std::list opts; + std::list args; std::vector args_raw; public: diff --git a/source/core/maputils.h b/source/core/maputils.h index 66b4e3b..f5faa12 100644 --- a/source/core/maputils.h +++ b/source/core/maputils.h @@ -57,7 +57,7 @@ private: template typename T::mapped_type &get_item(T &map, const typename T::key_type &key) { - typename T::iterator i = map.find(key); + auto i = map.find(key); if(i==map.end()) throw key_error(key); @@ -67,7 +67,7 @@ typename T::mapped_type &get_item(T &map, const typename T::key_type &key) template const typename T::mapped_type &get_item(const T &map, const typename T::key_type &key) { - typename T::const_iterator i = map.find(key); + auto i = map.find(key); if(i==map.end()) throw key_error(key); @@ -86,7 +86,7 @@ typename T::iterator insert_unique(T &map, const typename T::key_type &key, cons if(map.count(key)) throw key_error(key); - return map.insert(typename T::value_type(key, item)).first; + return map.insert(std::make_pair(key, item)).first; } template diff --git a/source/core/typeregistry.h b/source/core/typeregistry.h index 2761085..dd5dc01 100644 --- a/source/core/typeregistry.h +++ b/source/core/typeregistry.h @@ -43,9 +43,7 @@ private: virtual void invoke(T arg) const { action(this->keyword, arg); } }; - typedef std::map TypeMap; - - TypeMap types; + std::map types; public: ~TypeRegistry(); @@ -65,8 +63,8 @@ public: template class A, typename T> TypeRegistry::~TypeRegistry() { - for(typename TypeMap::iterator i=types.begin(); i!=types.end(); ++i) - delete i->second; + for(auto &kvp: types) + delete kvp.second; } template class A, typename T> @@ -88,8 +86,8 @@ void TypeRegistry::invoke(const std::string &kw, T arg) const template class A, typename T> void TypeRegistry::invoke_all(T arg) const { - for(typename TypeMap::const_iterator i=types.begin(); i!=types.end(); ++i) - i->second->invoke(arg); + for(auto &kvp: types) + kvp.second->invoke(arg); } } // namespace Msp diff --git a/source/core/windows/process.cpp b/source/core/windows/process.cpp index 2f7b505..da76fe3 100644 --- a/source/core/windows/process.cpp +++ b/source/core/windows/process.cpp @@ -14,19 +14,19 @@ string quote_argument(const string &arg) string result; bool need_quotes = false; bool backslash = false; - for(string::const_iterator i=arg.begin(); i!=arg.end(); ++i) + for(char c: arg) { - if(*i=='\\') + if(c=='\\') backslash = true; - else if(*i=='"') + else if(c=='"') { if(backslash) result += '\\'; result += '\\'; } - else if(*i==' ') + else if(c==' ') need_quotes = true; - result += *i; + result += c; } if(need_quotes) @@ -56,8 +56,8 @@ void Process::platform_get_self_info(Private &priv) void Process::execute(const string &command, bool path_search, const Arguments &args) { string cmdline = quote_argument(command); - for(Arguments::const_iterator i=args.begin(); i!=args.end(); ++i) - append(cmdline, " ", quote_argument(*i)); + for(const string &a: args) + append(cmdline, " ", quote_argument(a)); STARTUPINFO startup; startup.cb = sizeof(STARTUPINFO); diff --git a/source/debug/backtrace.cpp b/source/debug/backtrace.cpp index f9057dc..de1f76e 100644 --- a/source/debug/backtrace.cpp +++ b/source/debug/backtrace.cpp @@ -43,9 +43,8 @@ Backtrace Backtrace::create() ostream &operator<<(ostream &out, const Backtrace &bt) { - const list &frames = bt.get_frames(); - for(list::const_iterator i=frames.begin(); i!=frames.end(); ++i) - out<<*i<<'\n'; + for(const Backtrace::StackFrame &f: bt.get_frames()) + out<::iterator i=scopes.begin(); i!=scopes.end(); ++i) + for(auto &kvp: scopes) { - ScopeInfo &si = i->second; + ScopeInfo &si = kvp.second; if(p==0) si.history.clear(); else @@ -34,7 +34,7 @@ void Profiler::add_scope(const string &name) { if(!scopes.count(name)) { - map::iterator i = scopes.insert(map::value_type(name, ScopeInfo())).first; + auto i = scopes.insert(make_pair(name, ScopeInfo())).first; i->second.history.resize(period); } } @@ -48,10 +48,10 @@ ProfilingScope *Profiler::enter(ProfilingScope *ps) void Profiler::record(const ProfilingScope &scope) { - map::iterator i = scopes.find(scope.get_name()); + auto i = scopes.find(scope.get_name()); if(i==scopes.end()) { - i = scopes.insert(map::value_type(scope.get_name(), ScopeInfo())).first; + i = scopes.insert(make_pair(scope.get_name(), ScopeInfo())).first; i->second.first_call = scope.get_entry_time(); i->second.history.resize(period); } diff --git a/source/fs/dir.cpp b/source/fs/dir.cpp index 7f81e4e..99de180 100644 --- a/source/fs/dir.cpp +++ b/source/fs/dir.cpp @@ -40,11 +40,10 @@ const Path &get_bin_dir(const string &argv0) if(argv0.find(DIRSEP)==string::npos) if(const char *path = getenv("PATH")) { - vector dirs = split(path, ITEMSEP); - for(vector::const_iterator i=dirs.begin(); i!=dirs.end(); ++i) - if(exists(Path(*i)/argv0)) + for(const string &d: split(path, ITEMSEP)) + if(exists(Path(d)/argv0)) { - exe = realpath(Path(*i)/argv0); + exe = realpath(Path(d)/argv0); break; } } @@ -70,9 +69,9 @@ not_a_directory::not_a_directory(const Path &p): void mkpath(const Path &path, int mode) { Path p; - for(Path::Iterator i=path.begin(); i!=path.end(); ++i) + for(const string &c: path) { - p /= *i; + p /= c; #ifdef _WIN32 if(p.size()==1 && p.is_absolute()) continue; @@ -90,10 +89,9 @@ void mkpath(const Path &path, int mode) void rmpath(const Path &path) { - list files = list_files(path); - for(list::iterator i=files.begin(); i!=files.end(); ++i) + for(const string &f: list_files(path)) { - Path p = path / *i; + Path p = path/f; if(is_dir(p)) rmpath(p); else @@ -181,9 +179,9 @@ Path get_sys_lib_dir() Path path_lookup(const string &name, const list &paths) { - for(list::const_iterator i=paths.begin(); i!=paths.end(); ++i) + for(const Path &p: paths) { - Path full = *i/name; + Path full = p/name; if(exists(full)) return realpath(full); } @@ -194,7 +192,7 @@ Path path_lookup(const string &name, const list &paths) Path path_lookup(const string &name) { const char *path = getenv("PATH"); - vector dirs = split(path, ITEMSEP); + list dirs = split(path, ITEMSEP); return path_lookup(name, list(dirs.begin(), dirs.end())); } diff --git a/source/fs/filemonitor.cpp b/source/fs/filemonitor.cpp index 5cc990d..5ff5b78 100644 --- a/source/fs/filemonitor.cpp +++ b/source/fs/filemonitor.cpp @@ -1,3 +1,4 @@ +#include #include "filemonitor.h" #include "filemonitor_platform.h" @@ -36,15 +37,14 @@ void FileMonitor::add_file(const FS::Path &path) void FileMonitor::remove_file(const FS::Path &path) { - for(vector::iterator i=files.begin(); i!=files.end(); ++i) - if(i->path==path) - { - cleanup_file(*i); - if(&*i!=&files.back()) - *i = files.back(); - files.pop_back(); - break; - } + auto i = find_member(files, path, &MonitoredFile::path); + if(i!=files.end()) + { + cleanup_file(*i); + if(&*i!=&files.back()) + *i = files.back(); + files.pop_back(); + } } } // namespace FS diff --git a/source/fs/path.cpp b/source/fs/path.cpp index 353c63e..a5a5a10 100644 --- a/source/fs/path.cpp +++ b/source/fs/path.cpp @@ -68,7 +68,7 @@ bool Path::is_absolute() const Path Path::subpath(unsigned start, unsigned count) const { Path result; - Iterator i = begin(); + auto i = begin(); for(unsigned j=0; (j=0) { - for(Iterator i=begin(); i!=end(); ++i, --n) + for(auto i=begin(); i!=end(); ++i, --n) if(!n) return *i; } else { - for(Iterator i=end(); i!=begin();) + for(auto i=end(); i!=begin();) { --i; if(!++n) @@ -264,10 +264,7 @@ void Path::Iterator::update() string::size_type start = 0; if(iter!=path->separators.begin()) - { - PositionArray::const_iterator prev = iter; - start = *--prev+1; - } + start = *prev(iter)+1; string::size_type slash = string::npos; if(iter!=path->separators.end()) diff --git a/source/fs/path.h b/source/fs/path.h index 82600dc..e0b593c 100644 --- a/source/fs/path.h +++ b/source/fs/path.h @@ -67,7 +67,7 @@ public: private: std::string path; - std::vector separators; + PositionArray separators; public: Path(); diff --git a/source/fs/unix/filemonitor.cpp b/source/fs/unix/filemonitor.cpp index e2ca3b7..11b2a8d 100644 --- a/source/fs/unix/filemonitor.cpp +++ b/source/fs/unix/filemonitor.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include "filemonitor.h" @@ -99,27 +100,26 @@ void FileMonitor::Private::events_available() for(unsigned i=0; i(event_buf+i); - for(vector::iterator j=monitor.files.begin(); j!=monitor.files.end(); ++j) - if(j->tag==event->wd) + auto j = find_member(monitor.files, event->wd, &MonitoredFile::tag); + if(j!=monitor.files.end()) + { + if(event->mask&IN_MODIFY) + j->modified = true; + if(((event->mask&IN_CLOSE_WRITE) && j->modified) || (event->mask&IN_DELETE_SELF)) { - if(event->mask&IN_MODIFY) - j->modified = true; - if(((event->mask&IN_CLOSE_WRITE) && j->modified) || (event->mask&IN_DELETE_SELF)) - { - j->modified = false; - changed_files.push_back(j->path); - } - if(event->mask&IN_IGNORED) - j->tag = -1; - break; + j->modified = false; + changed_files.push_back(j->path); } + if(event->mask&IN_IGNORED) + j->tag = -1; + } i += sizeof(struct inotify_event)+event->len; } - for(vector::const_iterator i=changed_files.begin(); i!=changed_files.end(); ++i) - monitor.signal_file_modified.emit(*i); + for(const FS::Path &p: changed_files) + monitor.signal_file_modified.emit(p); - for(vector::iterator j=monitor.files.begin(); j!=monitor.files.end(); ) + for(auto j=monitor.files.begin(); j!=monitor.files.end(); ) { if(j->tag==-1) monitor.files.erase(j++); diff --git a/source/fs/utils.cpp b/source/fs/utils.cpp index a2fe40b..e15aad4 100644 --- a/source/fs/utils.cpp +++ b/source/fs/utils.cpp @@ -43,10 +43,10 @@ Path fix_case(const Path &path) { bool found = true; Path result; - for(Path::Iterator i=path.begin(); i!=path.end(); ++i) + for(const string &c: path) { - if(!found || (result.empty() && (*i=="/" || *i=="."))) - result /= *i; + if(!found || (result.empty() && (c=="/" || c=="."))) + result /= c; else { list files; @@ -56,15 +56,15 @@ Path fix_case(const Path &path) files = list_files("."); found = false; - for(list::iterator j=files.begin(); (j!=files.end() && !found); ++j) - if(!strcasecmp(*j,*i)) + for(const string &f: files) + if(!strcasecmp(f, c)) { - result /= *j; + result /= f; found = true; } if(!found) - result /= *i; + result /= c; } } @@ -76,8 +76,8 @@ Path relative(const Path &path, const Path &base) if(path.is_absolute()!=base.is_absolute()) throw invalid_argument("FS::relative"); - Path::Iterator i = path.begin(); - Path::Iterator j = base.begin(); + auto i = path.begin(); + auto j = base.begin(); for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ; Path result; @@ -91,8 +91,8 @@ Path relative(const Path &path, const Path &base) Path common_ancestor(const Path &path1, const Path &path2) { - Path::Iterator i = path1.begin(); - Path::Iterator j = path2.begin(); + auto i = path1.begin(); + auto j = path2.begin(); Path result; for(; (i!=path1.end() && j!=path2.end() && *i==*j); ++i, ++j) result /= *i; @@ -104,8 +104,8 @@ int descendant_depth(const Path &path, const Path &parent) if(path.is_absolute()!=parent.is_absolute()) throw invalid_argument("FS::descendant_depth"); - Path::Iterator i = path.begin(); - Path::Iterator j = parent.begin(); + auto i = path.begin(); + auto j = parent.begin(); for(; (i!=path.end() && j!=parent.end() && *i==*j); ++i, ++j) ; if(j!=parent.end()) diff --git a/source/io/eventdispatcher.cpp b/source/io/eventdispatcher.cpp index 49b28ac..9c233ee 100644 --- a/source/io/eventdispatcher.cpp +++ b/source/io/eventdispatcher.cpp @@ -12,7 +12,7 @@ namespace IO { void EventDispatcher::add(EventObject &obj) { Slot slot(*this, obj); - set::iterator i = objects.find(slot); + auto i = objects.find(slot); if(i==objects.end()) { i = objects.insert(slot).first; @@ -25,7 +25,7 @@ void EventDispatcher::add(EventObject &obj) void EventDispatcher::remove(EventObject &obj) { - set::iterator i = objects.find(Slot(*this, obj)); + auto i = objects.find(Slot(*this, obj)); if(i!=objects.end()) { objects.erase(i); @@ -63,10 +63,9 @@ void EventDispatcher::tick(const Time::Timer &timer) void EventDispatcher::dispatch() { - const vector &result = poller.get_result(); - for(vector::const_iterator i=result.begin(); i!=result.end(); ++i) - if(objects.count(Slot(*this, *i->object))) - i->object->event(i->events); + for(const Poller::PolledObject &po: poller.get_result()) + if(objects.count(Slot(*this, *po.object))) + po.object->event(po.events); } diff --git a/source/io/poll.cpp b/source/io/poll.cpp index a5d2d02..4ca2f3d 100644 --- a/source/io/poll.cpp +++ b/source/io/poll.cpp @@ -1,4 +1,5 @@ #include +#include #include "eventobject.h" #include "poll.h" #include "poll_platform.h" @@ -25,20 +26,20 @@ void Poller::set_object(EventObject &obj, PollEvent ev) if(ev) obj.get_event_handle(); - for(vector::iterator i=objects.begin(); i!=objects.end(); ++i) - if(i->object==&obj) + auto i = find_member(objects, &obj, &PolledObject::object); + if(i!=objects.end()) + { + if(ev) + i->events = ev; + else { - if(ev) - i->events = ev; - else - { - *i = objects.back(); - objects.pop_back(); - objs_changed = true; - } - events_changed = true; - return; + *i = objects.back(); + objects.pop_back(); + objs_changed = true; } + events_changed = true; + return; + } if(!ev) return; diff --git a/source/io/unix/poll.cpp b/source/io/unix/poll.cpp index 7df113e..28d3aeb 100644 --- a/source/io/unix/poll.cpp +++ b/source/io/unix/poll.cpp @@ -62,11 +62,11 @@ void Poller::rebuild_array() priv->pfd.clear(); priv->pfd.reserve(objects.size()); - for(vector::const_iterator i=objects.begin(); i!=objects.end(); ++i) + for(const PolledObject &po: objects) { pollfd p; - p.fd = *i->object->get_event_handle(); - p.events = sys_poll_event(i->events); + p.fd = *po.object->get_event_handle(); + p.events = sys_poll_event(po.events); priv->pfd.push_back(p); } } diff --git a/source/io/windows/poll.cpp b/source/io/windows/poll.cpp index 7aafc6e..0d644cd 100644 --- a/source/io/windows/poll.cpp +++ b/source/io/windows/poll.cpp @@ -18,8 +18,8 @@ void Poller::rebuild_array() priv->handles.clear(); priv->handles.reserve(objects.size()); - for(vector::const_iterator i=objects.begin(); i!=objects.end(); ++i) - priv->handles.push_back(*i->object->get_event_handle()); + for(const PolledObject &po: objects) + priv->handles.push_back(*po.object->get_event_handle()); } void Poller::platform_poll(int timeout) diff --git a/source/stringcodec/codec.cpp b/source/stringcodec/codec.cpp index 21ddf8a..abec134 100644 --- a/source/stringcodec/codec.cpp +++ b/source/stringcodec/codec.cpp @@ -20,7 +20,7 @@ bool Codec::detect(const string &str) const Decoder *dec = create_decoder(IGNORE_ERRORS); bool result = true; - for(string::const_iterator i=str.begin(); (result && i!=str.end()); ) + for(auto i=str.begin(); (result && i!=str.end()); ) result = (dec->decode_char(str, i)!=-1); delete dec; @@ -30,8 +30,8 @@ bool Codec::detect(const string &str) const void Codec::Encoder::encode(const ustring &str, string &buf) { - for(ustring::const_iterator i=str.begin(); i!=str.end(); ++i) - encode_char(*i, buf); + for(unichar c: str) + encode_char(c, buf); } string Codec::Encoder::encode(const ustring &str) @@ -46,7 +46,7 @@ string Codec::Encoder::encode(const ustring &str) void Codec::Decoder::decode(const string &str, ustring &buf) { - for(string::const_iterator i=str.begin(); i!=str.end();) + for(auto i=str.begin(); i!=str.end();) { unichar c = decode_char(str, i); if(c!=-1) @@ -111,9 +111,8 @@ Codec *detect_codec(const string &str) bool is_latin1 = true; unsigned utf8_mb = 0; - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) + for(char c: str) { - unsigned char c = *i; if(c&0x80) { is_ascii = false; diff --git a/source/stringcodec/except.cpp b/source/stringcodec/except.cpp index 23ac3cd..db09a02 100644 --- a/source/stringcodec/except.cpp +++ b/source/stringcodec/except.cpp @@ -19,7 +19,7 @@ invalid_sequence::invalid_sequence(const string::const_iterator &begin, const st string invalid_sequence::format_sequence(const string::const_iterator &begin, const string::const_iterator &end) { string result; - for(string::const_iterator i=begin; i!=end; ++i) + for(auto i=begin; i!=end; ++i) append(result, " ", lexical_cast(static_cast(*i), Fmt().fill('0').width(2).hex().uppercase())); return result; } diff --git a/source/stringcodec/iso2022jp.cpp b/source/stringcodec/iso2022jp.cpp index ebfe5f7..296c1ef 100644 --- a/source/stringcodec/iso2022jp.cpp +++ b/source/stringcodec/iso2022jp.cpp @@ -91,7 +91,7 @@ unichar Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterato while(i!=str.end()) { - string::const_iterator j = i; + auto j = i; unichar result = -1; if(*j==033) diff --git a/source/stringcodec/jisx0208.cpp b/source/stringcodec/jisx0208.cpp index 008a5e1..c097824 100644 --- a/source/stringcodec/jisx0208.cpp +++ b/source/stringcodec/jisx0208.cpp @@ -32,7 +32,7 @@ unichar JisX0208::Decoder::decode_char(const string &str, string::const_iterator if(i==str.end()) return -1; - string::const_iterator j = i; + auto j = i; Kuten jis; jis.ku = *j++-0x20; diff --git a/source/stringcodec/utf16.cpp b/source/stringcodec/utf16.cpp index 09fb3b6..13d49ad 100644 --- a/source/stringcodec/utf16.cpp +++ b/source/stringcodec/utf16.cpp @@ -66,7 +66,7 @@ unichar Utf16::Decoder::decode_char(const string &str, string::const_iterator &i if(i==str.end()) return -1; - string::const_iterator j = i; + auto j = i; unichar unit = decode_unit(str, i, j); if(unit!=-1) @@ -97,7 +97,7 @@ unichar Utf16::Decoder::decode_char(const string &str, string::const_iterator &i { if(unit>=0xD800 && unit<=0xDBFF) { - string::const_iterator k = j; + auto k = j; unichar unit2 = -2; if(k!=str.end()) diff --git a/source/stringcodec/utf8.cpp b/source/stringcodec/utf8.cpp index b75b397..177386b 100644 --- a/source/stringcodec/utf8.cpp +++ b/source/stringcodec/utf8.cpp @@ -59,7 +59,7 @@ unichar Utf8::Decoder::decode_char(const string &str, string::const_iterator &i) for(; *i&mask; mask>>=1) ++bytes; - string::const_iterator j = i; + auto j = i; unichar result = (*j++)&(mask-1); diff --git a/source/strings/format.cpp b/source/strings/format.cpp index 773eefa..d2065e9 100644 --- a/source/strings/format.cpp +++ b/source/strings/format.cpp @@ -55,7 +55,7 @@ Fmt Formatter::get_conversion() if(pos==fmt.end()) throw format_error("Too many arguments for format"); - string::iterator i = pos; + auto i = pos; for(; i!=fmt.end(); ++i) if(isalpha(*i)) break; diff --git a/source/strings/lexicalcast.cpp b/source/strings/lexicalcast.cpp index f5b3a95..2fc4be3 100644 --- a/source/strings/lexicalcast.cpp +++ b/source/strings/lexicalcast.cpp @@ -113,7 +113,7 @@ T str_to_int(const string &s, const Fmt &f) if(s.empty()) throw lexical_error("conversion of '' to integer"); - string::const_iterator i = s.begin(); + auto i = s.begin(); // See if the input starts with a sign bool neg = false; @@ -389,7 +389,7 @@ T str_to_flt(const string &s, const Fmt &) if(s.empty()) throw lexical_error("conversion of '' to floating-point"); - string::const_iterator i = s.begin(); + auto i = s.begin(); // See if the input starts with a sign bool neg = false; diff --git a/source/strings/regex.cpp b/source/strings/regex.cpp index 68cf374..9167c6b 100644 --- a/source/strings/regex.cpp +++ b/source/strings/regex.cpp @@ -56,7 +56,7 @@ string bad_regex::make_where(const string &e, const string::const_iterator &i) Regex::Regex(const string &expr) { n_groups = 0; - string::const_iterator iter = expr.begin(); + auto iter = expr.begin(); code = compile(expr, iter, n_groups, false); ++n_groups; } @@ -123,7 +123,7 @@ Regex::Code Regex::compile(const string &expr, string::const_iterator &iter, uns if(!has_branches) { - for(string::const_iterator i=iter; i!=end;) + for(auto i=iter; i!=end;) { Code atom = parse_atom(expr, i, group); @@ -158,7 +158,7 @@ Regex::Code Regex::compile(const string &expr, string::const_iterator &iter, uns else { list branches; - for(string::const_iterator i=iter;;) + for(auto i=iter;;) { branches.push_back(compile(expr, i, group, true)); if(i==end) @@ -169,14 +169,14 @@ Regex::Code Regex::compile(const string &expr, string::const_iterator &iter, uns unsigned n_branches = branches.size(); Offset offset = (n_branches-1)*jump_size+branches.front().size(); - for(list::iterator i=++branches.begin(); i!=branches.end(); ++i) + for(auto i=++branches.begin(); i!=branches.end(); ++i) { result += ND_JUMP; write_int(offset, result); offset += i->size(); } - for(list::iterator i=branches.begin(); i!=branches.end();) + for(auto i=branches.begin(); i!=branches.end();) { result += *i; offset -= i->size()+jump_size; @@ -258,7 +258,7 @@ bool Regex::parse_repeat(const string &expr, string::const_iterator &i, Count &r rmin = 0; if(*i=='{') { - string::const_iterator begin = i; + auto begin = i; rmin = 0; for(++i; isdigit(*i); ++i) @@ -291,7 +291,7 @@ bool Regex::parse_repeat(const string &expr, string::const_iterator &i, Count &r Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &iter) { - string::const_iterator begin = iter; + auto begin = iter; Code result; ++iter; @@ -302,7 +302,7 @@ Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &ite ++iter; } - string::const_iterator end = iter; + auto end = iter; for(; (end!=str.end() && (end==iter || *end!=']')); ++end) ; if(end==str.end()) throw bad_regex("unmatched '['", str, begin); @@ -311,7 +311,7 @@ Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &ite unsigned type = 0; bool range = false; unsigned char first = 0, last = 0; - for(string::const_iterator i=iter; i!=end; ++i) + for(auto i=iter; i!=end; ++i) { unsigned char c = *i; if(range) @@ -364,16 +364,16 @@ Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &ite RegMatch Regex::match(const string &str) const { - RegMatch::GroupArray groups(n_groups); + vector groups(n_groups); - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) + for(auto i=str.begin(); i!=str.end(); ++i) if(run(str, i, groups)) return RegMatch(str, groups); return RegMatch(); } -bool Regex::run(const string &str, const string::const_iterator &begin, RegMatch::GroupArray &groups) const +bool Regex::run(const string &str, const string::const_iterator &begin, vector &groups) const { bool result = false; list ctx; @@ -381,7 +381,7 @@ bool Regex::run(const string &str, const string::const_iterator &begin, RegMatch ctx.front().citer = code.begin(); ctx.front().groups.resize(groups.size()); - for(string::const_iterator i=begin;;) + for(auto i=begin;;) { int c; if(i!=str.end()) @@ -389,7 +389,7 @@ bool Regex::run(const string &str, const string::const_iterator &begin, RegMatch else c = -1; - for(list::iterator j=ctx.begin(); j!=ctx.end();) + for(auto j=ctx.begin(); j!=ctx.end();) { bool terminate = false; bool negate_match = false; @@ -524,9 +524,9 @@ string Regex::disassemble() const { string result; - for(Code::const_iterator i=code.begin(); i!=code.end();) + for(auto i=code.begin(); i!=code.end();) { - Code::const_iterator j = i; + auto j = i; Offset offset = i-code.begin(); string decompiled = disassemble_instruction(i); string bytes; diff --git a/source/strings/regex.h b/source/strings/regex.h index 6792502..094a295 100644 --- a/source/strings/regex.h +++ b/source/strings/regex.h @@ -103,7 +103,7 @@ private: struct RunContext { Code::const_iterator citer; - RegMatch::GroupArray groups; + std::vector groups; }; Code code; @@ -128,7 +128,7 @@ public: RegMatch match(const std::string &str) const; private: - bool run(const std::string &, const std::string::const_iterator &, RegMatch::GroupArray &) const; + bool run(const std::string &, const std::string::const_iterator &, std::vector &) const; bool group_compare(const RegMatch::Group &, const RegMatch::Group &) const; public: diff --git a/source/strings/regmatch.cpp b/source/strings/regmatch.cpp index 4799cfb..0e702b5 100644 --- a/source/strings/regmatch.cpp +++ b/source/strings/regmatch.cpp @@ -5,14 +5,14 @@ using namespace std; namespace Msp { -RegMatch::RegMatch(const string &str, const GroupArray &g): - groups(g) +RegMatch::RegMatch(const string &str, const vector &grps): + groups(grps) { - for(GroupArray::iterator i=groups.begin(); i!=groups.end(); ++i) - if(i->match) + for(Group &g: groups) + if(g.match) { - i->length = i->end-i->begin; - i->str = str.substr(i->begin, i->length); + g.length = g.end-g.begin; + g.str = str.substr(g.begin, g.length); } } diff --git a/source/strings/regmatch.h b/source/strings/regmatch.h index c5116cd..b79104b 100644 --- a/source/strings/regmatch.h +++ b/source/strings/regmatch.h @@ -36,10 +36,8 @@ public: operator bool() const { return match; } }; - typedef std::vector GroupArray; - private: - GroupArray groups; + std::vector groups; public: /** Constructs a RegMatch representing a non-match. */ diff --git a/source/strings/utils.cpp b/source/strings/utils.cpp index 1b4cad0..6b659d0 100644 --- a/source/strings/utils.cpp +++ b/source/strings/utils.cpp @@ -40,14 +40,6 @@ vector do_split(const string &str, const string &sep, int max_split) return result; } -bool check_str(const string &str, int (*pred)(int)) -{ - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) - if(!pred(*i)) - return false; - return true; -} - } @@ -55,8 +47,8 @@ namespace Msp { int strcasecmp(const string &s1, const string &s2) { - string::const_iterator i1 = s1.begin(); - string::const_iterator i2 = s2.begin(); + auto i1 = s1.begin(); + auto i2 = s2.begin(); for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2) { const char c1 = ::tolower(*i1); @@ -71,30 +63,30 @@ int strcasecmp(const string &s1, const string &s2) string tolower(const string &str) { string result(str); - transform(result.begin(), result.end(), result.begin(), ::tolower); + transform(result.begin(), result.end(), result.begin(), [](char c){ return std::tolower(c); }); return result; } string toupper(const string &str) { string result(str); - transform(result.begin(), result.end(), result.begin(), ::toupper); + transform(result.begin(), result.end(), result.begin(), [](char c){ return std::toupper(c); }); return result; } bool isnumrc(const string &str) { - return check_str(str, isdigit); + return all_of(str.begin(), str.end(), [](char c){ return std::isdigit(c); }); } bool isalpha(const string &str) { - return check_str(str, isalpha); + return all_of(str.begin(), str.end(), [](char c){ return std::isalpha(c); }); } bool isalnum(const string &str) { - return check_str(str, isalnum); + return all_of(str.begin(), str.end(), [](char c){ return std::isalnum(c); }); } vector split(const string &str, const string &sep, int max_split) @@ -151,17 +143,17 @@ string c_unescape(const string &str) unsigned numeric_pos = 0; unsigned numeric_value = 0; string result; - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) + for(auto c: str) { if(numeric_type==16) { unsigned digit = 0; - if(*i>='0' && *i<='9') - digit = *i-'0'; - else if(*i>='a' && *i<='f') - digit = *i-'a'+10; - else if(*i>='A' && *i<='F') - digit = *i-'A'+10; + if(c>='0' && c<='9') + digit = c-'0'; + else if(c>='a' && c<='f') + digit = c-'a'+10; + else if(c>='A' && c<='F') + digit = c-'A'+10; else throw invalid_argument("c_unescape"); @@ -176,8 +168,8 @@ string c_unescape(const string &str) else if(numeric_type==8) { unsigned digit = 0; - if(*i>='0' && *i<='7') - digit = *i-'0'; + if(c>='0' && c<='7') + digit = c-'0'; else throw invalid_argument("c_unescape"); @@ -191,47 +183,47 @@ string c_unescape(const string &str) } else if(escape) { - if(*i=='x') + if(c=='x') { numeric_type = 16; numeric_pos = 0; numeric_value = 0; } - else if(*i>='0' && *i<='3') + else if(c>='0' && c<='3') { numeric_type = 8; numeric_pos = 1; - numeric_value = *i-'0'; + numeric_value = c-'0'; } - else if(*i=='n') + else if(c=='n') result += '\n'; - else if(*i=='t') + else if(c=='t') result += '\t'; - else if(*i=='r') + else if(c=='r') result += '\r'; - else if(*i=='b') + else if(c=='b') result += '\b'; - else if(*i=='v') + else if(c=='v') result += '\v'; - else if(*i=='a') + else if(c=='a') result += '\a'; - else if(*i=='f') + else if(c=='f') result += '\f'; - else if(*i=='\"') + else if(c=='\"') result += '\"'; - else if(*i=='\'') + else if(c=='\'') result += '\''; - else if(*i=='\\') + else if(c=='\\') result += '\\'; else throw invalid_argument("c_unescape"); escape = false; } - else if(*i=='\\') + else if(c=='\\') escape = true; else - result += *i; + result += c; } if(escape) @@ -244,37 +236,37 @@ string c_escape(const string &str, bool escape_8bit) { string result; - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) + for(char c: str) { - if(*i=='\n') + if(c=='\n') result += "\\n"; - else if(*i=='\t') + else if(c=='\t') result += "\\t"; - else if(*i=='\r') + else if(c=='\r') result += "\\r"; - else if(*i=='\b') + else if(c=='\b') result += "\\b"; - else if(*i=='\v') + else if(c=='\v') result += "\\v"; - else if(*i=='\a') + else if(c=='\a') result += "\\a"; - else if(*i=='\f') + else if(c=='\f') result += "\\f"; - else if(*i=='\"') + else if(c=='\"') result += "\\\""; - else if(*i=='\'') + else if(c=='\'') result += "\\\'"; - else if(*i=='\\') + else if(c=='\\') result += "\\\\"; - else if(static_cast(*i)<' ' || (escape_8bit && (*i&0x80))) + else if(static_cast(c)<' ' || (escape_8bit && (c&0x80))) { char buf[4] = { '\\', 0 }; for(unsigned j=0; j<3; ++j) - buf[1+j] = '0'+((static_cast(*i)>>(6-j*3))&7); + buf[1+j] = '0'+((static_cast(c)>>(6-j*3))&7); result.append(buf, 4); } else - result += *i; + result += c; } return result; diff --git a/source/time/datetime.cpp b/source/time/datetime.cpp index e8170cc..e1abcf7 100644 --- a/source/time/datetime.cpp +++ b/source/time/datetime.cpp @@ -288,7 +288,7 @@ TimeStamp DateTime::get_timestamp() const string DateTime::format(const string &fmt) const { string result; - for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i) + for(auto i=fmt.begin(); i!=fmt.end(); ++i) { if(*i=='%') { diff --git a/source/time/timer.cpp b/source/time/timer.cpp index c15cd28..9411fac 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include "timer.h" #include "utils.h" @@ -15,8 +15,8 @@ Timer::Timer(): Timer::~Timer() { - for(vector::iterator i=slots.begin(); i!=slots.end(); ++i) - delete i->slot; + for(const SlotProxy &s: slots) + delete s.slot; } Timer::Slot &Timer::add(const TimeDelta &td) @@ -44,14 +44,13 @@ Timer::Slot &Timer::add(const TimeStamp &ts) void Timer::cancel(Slot &slot) { MutexLock l(mutex); - for(vector::iterator i=slots.begin(); i!=slots.end(); ++i) - if(i->slot==&slot) - { - delete i->slot; - slots.erase(i); - make_heap(slots.begin(), slots.end()); - return; - } + auto i = find_member(slots, &slot, &SlotProxy::slot); + if(i!=slots.end()) + { + delete i->slot; + slots.erase(i); + make_heap(slots.begin(), slots.end()); + } } void Timer::tick()