blob: 68cb7d5b38c9b9d78165961d3cd161408ad571e5 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev90164962014-03-06 08:29:59 +00002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyev90164962014-03-06 08:29:59 +00004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev90164962014-03-06 08:29:59 +00006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyev90164962014-03-06 08:29:59 +000020 */
21
22#ifndef NDN_INTEREST_FILTER_HPP
23#define NDN_INTEREST_FILTER_HPP
24
Alexander Afanasyev90164962014-03-06 08:29:59 +000025#include "name.hpp"
26
27namespace ndn {
28
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070029class RegexPatternListMatcher;
30
Alexander Afanasyev90164962014-03-06 08:29:59 +000031class InterestFilter
32{
33public:
34 class Error : public std::runtime_error
35 {
36 public:
37 explicit
38 Error(const std::string& what)
39 : std::runtime_error(what)
40 {
41 }
42 };
43
44 /**
45 * @brief Create an Interest filter to match Interests by prefix
46 *
47 * This filter will match all Interests, whose name start with the given prefix
48 *
49 * Any Name can be implicitly converted to the InterestFilter.
50 */
51 InterestFilter(const Name& prefix);
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 const char* can be implicitly converted to the InterestFilter.
59 */
60 InterestFilter(const char* prefixUri);
61
62 /**
63 * @brief Create an Interest filter to match Interests by prefix URI
64 *
65 * This filter will match all Interests, whose name start with the given prefix
66 *
67 * Any std::string can be implicitly converted to the InterestFilter.
68 */
69 InterestFilter(const std::string& prefixUri);
70
71 /**
72 * @brief Create an Interest filter to match Interest by prefix and regular expression
73 *
74 * This filter will match all Interests, whose name start with the given prefix and
75 * other components of the Interest name match the given regular expression.
76 * For example, the following InterestFilter:
77 *
78 * InterestFilter("/hello", "<world><>+")
79 *
80 * will match all Interests, whose name has prefix `/hello`, which is followed by
81 * component `world` and has at least one more component after it. Examples:
82 *
83 * /hello/world/!
84 * /hello/world/x/y/z
85 *
86 * Note that regular expression will need to match all components (e.g., there is
87 * an implicit heading `^` and trailing `$` symbols in the regular expression).
88 */
89 InterestFilter(const Name& prefix, const std::string& regexFilter);
90
91 /**
92 * @brief Implicit conversion to Name (to provide backwards compatibility for onInterest callback)
93 */
94 operator const Name&() const
95 {
96 if (static_cast<bool>(m_regexFilter)) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070097 BOOST_THROW_EXCEPTION(Error("Please update OnInterest callback to accept const "
98 "InterestFilter& (non-trivial Interest filter is being used)"));
Alexander Afanasyev90164962014-03-06 08:29:59 +000099 }
100 return m_prefix;
101 }
102
103 /**
104 * @brief Check if specified name matches the filter
105 */
106 bool
107 doesMatch(const Name& name) const;
108
109 const Name&
110 getPrefix() const
111 {
112 return m_prefix;
113 }
114
115 bool
116 hasRegexFilter() const
117 {
118 return static_cast<bool>(m_regexFilter);
119 }
120
121 const RegexPatternListMatcher&
122 getRegexFilter() const
123 {
124 return *m_regexFilter;
125 }
126
127private:
128 Name m_prefix;
129 shared_ptr<RegexPatternListMatcher> m_regexFilter;
130};
131
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700132std::ostream&
133operator<<(std::ostream& os, const InterestFilter& filter);
134
135
Alexander Afanasyev90164962014-03-06 08:29:59 +0000136inline
137InterestFilter::InterestFilter(const Name& prefix)
138 : m_prefix(prefix)
139{
140}
141
142inline
143InterestFilter::InterestFilter(const char* prefixUri)
144 : m_prefix(prefixUri)
145{
146}
147
148inline
149InterestFilter::InterestFilter(const std::string& prefixUri)
150 : m_prefix(prefixUri)
151{
152}
153
Alexander Afanasyev90164962014-03-06 08:29:59 +0000154} // namespace ndn
155
156#endif // NDN_INTEREST_FILTER_HPP