diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2015-06-18 11:55:27 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2015-06-18 11:55:27 +0200 |
commit | a9e2221dfddfbe26eebd6fca92a99be9e8ec1082 (patch) | |
tree | 74a45df1993eeddf15a283f18ad195f89a706f20 /butl | |
parent | 6144c196df4a38689cc027781c356219548a4a90 (diff) |
Move some utilities from build2 to libbutl
Diffstat (limited to 'butl')
-rw-r--r-- | butl/utility | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/butl/utility b/butl/utility new file mode 100644 index 0000000..b1fa8f6 --- /dev/null +++ b/butl/utility @@ -0,0 +1,53 @@ +// file : butl/utility -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BUTL_UTILITY +#define BUTL_UTILITY + +#include <cstring> // strcmp + +namespace butl +{ + // Key comparators (i.e., to be used in sets, maps, etc). + // + struct compare_c_string + { + bool operator() (const char* x, const char* y) const + { + return std::strcmp (x, y) < 0; + } + }; + + struct compare_pointer_target + { + template <typename P> + bool operator() (const P& x, const P& y) const {return *x < *y;} + }; + + // Support for reverse iteration using range-based for-loop: + // + // for (... : reverse_iterate (x)) ... + // + template <typename T> + class reverse_range + { + T& x_; + + public: + reverse_range (T& x): x_ (x) {} + + auto begin () -> decltype (this->x_.rbegin ()) {return x_.rbegin ();} + auto end () -> decltype (this->x_.rend ()) {return x_.rend ();} + }; + + template <typename T> + inline reverse_range<T> + reverse_iterate (T& x) {return reverse_range<T> (x);} + + template <typename T> + inline reverse_range<const T> + reverse_iterate (const T& x) {return reverse_range<const T> (x);} +} + +#endif // BUTL_UTILITY |