diff options
Diffstat (limited to 'configutil/src/lib/hostfilter.h')
-rw-r--r-- | configutil/src/lib/hostfilter.h | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/configutil/src/lib/hostfilter.h b/configutil/src/lib/hostfilter.h new file mode 100644 index 00000000000..8c255c930cc --- /dev/null +++ b/configutil/src/lib/hostfilter.h @@ -0,0 +1,49 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include <unordered_set> +#include <string> + +/** + * Simple host filter which in its default empty state implicitly includes all + * hosts, or only an explicit subset iff at least one host has been provided + * to the filter as part of construction. + */ +class HostFilter { +public: + using HostSet = std::unordered_set<std::string>; +private: + HostSet _hosts; +public: + /** + * Empty host filter; all hosts are implicitly included. + */ + HostFilter() : _hosts() {} + + /** + * Explicitly given host set; only the hosts whose name exactly match + * one of the provided names will pass the includes(name) check. + */ + explicit HostFilter(const std::unordered_set<std::string>& hosts) + : _hosts(hosts) + { + } + + explicit HostFilter(std::unordered_set<std::string>&& hosts) + : _hosts(std::move(hosts)) + { + } + + HostFilter(HostFilter&&) = default; + HostFilter& operator=(HostFilter&&) = default; + + HostFilter(const HostFilter&) = default; + HostFilter& operator=(const HostFilter&) = default; + + bool includes(const std::string& candidate) const { + if (_hosts.empty()) { + return true; + } + return (_hosts.find(candidate) != _hosts.end()); + } +}; |