From 237a48ad36b6aa04b32e4c41f22ce46c7245604f Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 11 Mar 2024 21:03:05 -0400 Subject: [PATCH] cpp-common/bt2c: add bt2c::contains() 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 Change-Id: Idea480f9001272dfaf1dc0df60251983be8f3962 --- src/Makefile.am | 1 + src/cpp-common/bt2c/contains.hpp | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/cpp-common/bt2c/contains.hpp diff --git a/src/Makefile.am b/src/Makefile.am index 98d281a6..b66b1d6b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 00000000..913cf1a3 --- /dev/null +++ b/src/cpp-common/bt2c/contains.hpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Philippe Proulx + * + * 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 +bool contains(const T& container, const V& val) noexcept +{ + return container.find(val) != container.end(); +} + +} /* namespace bt2c */ + +#endif /* BABELTRACE_CPP_COMMON_BT2C_CONTAINS_HPP */ -- 2.34.1