aboutsummaryrefslogtreecommitdiff
path: root/libbutl/multi-index.mxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-09-22 23:32:28 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-09-22 23:32:28 +0200
commitc09cd7512491cee1e82c1ad8128ce9fd4bc3f79b (patch)
treea659ed768d849130ab5780a11b7f791a463a1a91 /libbutl/multi-index.mxx
parent2a00871f07067f8f9e2de08bb9c8f50e1bf6a650 (diff)
Initial modularization with both Clang and VC hacks
Note: gave up on VC about half way though.
Diffstat (limited to 'libbutl/multi-index.mxx')
-rw-r--r--libbutl/multi-index.mxx73
1 files changed, 73 insertions, 0 deletions
diff --git a/libbutl/multi-index.mxx b/libbutl/multi-index.mxx
new file mode 100644
index 0000000..f0ef298
--- /dev/null
+++ b/libbutl/multi-index.mxx
@@ -0,0 +1,73 @@
+// file : libbutl/multi-index.mxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#ifndef __cpp_modules
+#pragma once
+#endif
+
+// C includes.
+
+#ifndef __cpp_lib_modules
+#include <utility> // declval()
+#include <functional> // hash
+#endif
+
+// Other includes.
+
+#ifdef __cpp_modules
+export module butl.multi_index;
+#ifdef __cpp_lib_modules
+import std.core;
+#endif
+#endif
+
+#include <libbutl/export.hxx>
+
+LIBBUTL_MODEXPORT namespace butl
+{
+ // Google the "Emulating Boost.MultiIndex with Standard Containers" blog
+ // post for details.
+ //
+
+ template <typename T>
+ struct map_key
+ {
+ mutable const T* p;
+
+ map_key (const T* v = 0): p (v) {}
+ bool operator< (const map_key& x) const {return *p < *x.p;}
+ bool operator== (const map_key& x) const {return *p == *x.p;}
+ };
+
+ template <typename I>
+ struct map_iterator_adapter: I
+ {
+ typedef const typename I::value_type::second_type value_type;
+ typedef value_type* pointer;
+ typedef value_type& reference;
+
+ map_iterator_adapter () {}
+ map_iterator_adapter (I i): I (i) {}
+
+ map_iterator_adapter&
+ operator= (I i) {static_cast<I&> (*this) = i; return *this;}
+
+ reference operator* () const {return I::operator* ().second;}
+ pointer operator-> () const {return &I::operator-> ()->second;}
+ };
+}
+
+LIBBUTL_MODEXPORT namespace std
+{
+ template <typename T>
+ struct hash<butl::map_key<T>>: hash<T>
+ {
+ size_t
+ operator() (butl::map_key<T> x) const
+ noexcept (noexcept (declval<hash<T>> () (*x.p)))
+ {
+ return hash<T>::operator() (*x.p);
+ }
+ };
+}