Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | */ |
| 12 | |
| 13 | #ifndef NDN_INTEREST_FILTER_HPP |
| 14 | #define NDN_INTEREST_FILTER_HPP |
| 15 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 16 | #include "name.hpp" |
| 17 | |
| 18 | namespace ndn { |
| 19 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 20 | class RegexPatternListMatcher; |
| 21 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 22 | class InterestFilter |
| 23 | { |
| 24 | public: |
| 25 | class Error : public std::runtime_error |
| 26 | { |
| 27 | public: |
| 28 | explicit |
| 29 | Error(const std::string& what) |
| 30 | : std::runtime_error(what) |
| 31 | { |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * @brief Create an Interest filter to match Interests by prefix |
| 37 | * |
| 38 | * This filter will match all Interests, whose name start with the given prefix |
| 39 | * |
| 40 | * Any Name can be implicitly converted to the InterestFilter. |
| 41 | */ |
| 42 | InterestFilter(const Name& prefix); |
| 43 | |
| 44 | /** |
| 45 | * @brief Create an Interest filter to match Interests by prefix URI |
| 46 | * |
| 47 | * This filter will match all Interests, whose name start with the given prefix |
| 48 | * |
| 49 | * Any const char* can be implicitly converted to the InterestFilter. |
| 50 | */ |
| 51 | InterestFilter(const char* prefixUri); |
| 52 | |
| 53 | /** |
| 54 | * @brief Create an Interest filter to match Interests by prefix URI |
| 55 | * |
| 56 | * This filter will match all Interests, whose name start with the given prefix |
| 57 | * |
| 58 | * Any std::string can be implicitly converted to the InterestFilter. |
| 59 | */ |
| 60 | InterestFilter(const std::string& prefixUri); |
| 61 | |
| 62 | /** |
| 63 | * @brief Create an Interest filter to match Interest by prefix and regular expression |
| 64 | * |
| 65 | * This filter will match all Interests, whose name start with the given prefix and |
| 66 | * other components of the Interest name match the given regular expression. |
| 67 | * For example, the following InterestFilter: |
| 68 | * |
| 69 | * InterestFilter("/hello", "<world><>+") |
| 70 | * |
| 71 | * will match all Interests, whose name has prefix `/hello`, which is followed by |
| 72 | * component `world` and has at least one more component after it. Examples: |
| 73 | * |
| 74 | * /hello/world/! |
| 75 | * /hello/world/x/y/z |
| 76 | * |
| 77 | * Note that regular expression will need to match all components (e.g., there is |
| 78 | * an implicit heading `^` and trailing `$` symbols in the regular expression). |
| 79 | */ |
| 80 | InterestFilter(const Name& prefix, const std::string& regexFilter); |
| 81 | |
| 82 | /** |
| 83 | * @brief Implicit conversion to Name (to provide backwards compatibility for onInterest callback) |
| 84 | */ |
| 85 | operator const Name&() const |
| 86 | { |
| 87 | if (static_cast<bool>(m_regexFilter)) { |
| 88 | throw Error("Please update OnInterest callback to accept const InterestFilter& " |
| 89 | "(non-trivial Interest filter is being used)"); |
| 90 | } |
| 91 | return m_prefix; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @brief Check if specified name matches the filter |
| 96 | */ |
| 97 | bool |
| 98 | doesMatch(const Name& name) const; |
| 99 | |
| 100 | const Name& |
| 101 | getPrefix() const |
| 102 | { |
| 103 | return m_prefix; |
| 104 | } |
| 105 | |
| 106 | bool |
| 107 | hasRegexFilter() const |
| 108 | { |
| 109 | return static_cast<bool>(m_regexFilter); |
| 110 | } |
| 111 | |
| 112 | const RegexPatternListMatcher& |
| 113 | getRegexFilter() const |
| 114 | { |
| 115 | return *m_regexFilter; |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | Name m_prefix; |
| 120 | shared_ptr<RegexPatternListMatcher> m_regexFilter; |
| 121 | }; |
| 122 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 123 | std::ostream& |
| 124 | operator<<(std::ostream& os, const InterestFilter& filter); |
| 125 | |
| 126 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 127 | inline |
| 128 | InterestFilter::InterestFilter(const Name& prefix) |
| 129 | : m_prefix(prefix) |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | inline |
| 134 | InterestFilter::InterestFilter(const char* prefixUri) |
| 135 | : m_prefix(prefixUri) |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | inline |
| 140 | InterestFilter::InterestFilter(const std::string& prefixUri) |
| 141 | : m_prefix(prefixUri) |
| 142 | { |
| 143 | } |
| 144 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 145 | } // namespace ndn |
| 146 | |
| 147 | #endif // NDN_INTEREST_FILTER_HPP |