aboutsummaryrefslogtreecommitdiff
path: root/butl
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-06-18 11:55:27 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-06-18 11:55:27 +0200
commita9e2221dfddfbe26eebd6fca92a99be9e8ec1082 (patch)
tree74a45df1993eeddf15a283f18ad195f89a706f20 /butl
parent6144c196df4a38689cc027781c356219548a4a90 (diff)
Move some utilities from build2 to libbutl
Diffstat (limited to 'butl')
-rw-r--r--butl/utility53
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