cpp-common/bt2c: add bt2c::contains()
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 12 Mar 2024 01:03:05 +0000 (21:03 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 12 Mar 2024 01:03:05 +0000 (21:03 -0400)
This new function template returns whether or not some STL container
contains some value instead of using the awkward find() and end()
methods each time.

Similar to new STL contains() methods of C++20.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Idea480f9001272dfaf1dc0df60251983be8f3962

src/Makefile.am
src/cpp-common/bt2c/contains.hpp [new file with mode: 0644]

index 98d281a637003a42698bd767b9bbd5ed2301c169..b66b1d6b1585aa3a105693535a3a8e0abad75099 100644 (file)
@@ -153,6 +153,7 @@ cpp_common_libcpp_common_la_SOURCES = \
        cpp-common/bt2c/align.hpp \
        cpp-common/bt2c/c-string-view.hpp \
        cpp-common/bt2c/call.hpp \
+       cpp-common/bt2c/contains.hpp \
        cpp-common/bt2c/dummy.cpp \
        cpp-common/bt2c/endian.hpp \
        cpp-common/bt2c/exc.hpp \
diff --git a/src/cpp-common/bt2c/contains.hpp b/src/cpp-common/bt2c/contains.hpp
new file mode 100644 (file)
index 0000000..913cf1a
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2024 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_CONTAINS_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_CONTAINS_HPP
+
+namespace bt2c {
+
+/*
+ * Returns whether or not the STL container `container` contains the
+ * value `val`.
+ */
+template <typename T, typename V>
+bool contains(const T& container, const V& val) noexcept
+{
+    return container.find(val) != container.end();
+}
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_CONTAINS_HPP */
This page took 0.025238 seconds and 4 git commands to generate.