blob: f771d16d3f3e2fa1b14fcf4e8e2c7948d36fa646 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -07002/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -07006 *
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 Afanasyevee8bb1e2014-05-02 17:39:54 -070020 */
21
22#ifndef NDN_DETAIL_INTEREST_FILTER_RECORD_HPP
23#define NDN_DETAIL_INTEREST_FILTER_RECORD_HPP
24
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070025#include "../name.hpp"
26#include "../interest.hpp"
27
28namespace ndn {
29
Junxiao Shi103d8ed2016-08-07 20:34:10 +000030/**
31 * @brief associates an InterestFilter with Interest callback
32 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070033class InterestFilterRecord : noncopyable
34{
35public:
Junxiao Shi103d8ed2016-08-07 20:34:10 +000036 /**
37 * @brief Construct an Interest filter record
38 *
39 * @param filter an InterestFilter that represents what Interest should invoke the callback
40 * @param interestCallback invoked when matching Interest is received
41 */
42 InterestFilterRecord(const InterestFilter& filter,
43 const InterestCallback& interestCallback)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070044 : m_filter(filter)
Junxiao Shi103d8ed2016-08-07 20:34:10 +000045 , m_interestCallback(interestCallback)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070046 {
47 }
48
49 /**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000050 * @return the filter
51 */
52 const InterestFilter&
53 getFilter() const
54 {
55 return m_filter;
56 }
57
58 /**
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070059 * @brief Check if Interest name matches the filter
60 * @param name Interest Name
61 */
62 bool
63 doesMatch(const Name& name) const
64 {
65 return m_filter.doesMatch(name);
66 }
67
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080068 /**
69 * @brief invokes the InterestCallback
Junxiao Shi76e0eb22016-08-08 05:54:10 +000070 * @note This method does nothing if the Interest callback is empty
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080071 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070072 void
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080073 invokeInterestCallback(const Interest& interest) const
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070074 {
Junxiao Shi76e0eb22016-08-08 05:54:10 +000075 if (m_interestCallback != nullptr) {
76 m_interestCallback(m_filter, interest);
77 }
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070078 }
79
80private:
81 InterestFilter m_filter;
Junxiao Shi103d8ed2016-08-07 20:34:10 +000082 InterestCallback m_interestCallback;
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070083};
84
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070085/**
Junxiao Shi103d8ed2016-08-07 20:34:10 +000086 * @brief Opaque type to identify an InterestFilterRecord
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070087 */
88class InterestFilterId;
89
90/**
91 * @brief Functor to match InterestFilterId
92 */
93class MatchInterestFilterId
94{
95public:
96 explicit
97 MatchInterestFilterId(const InterestFilterId* interestFilterId)
98 : m_id(interestFilterId)
99 {
100 }
101
102 bool
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700103 operator()(const shared_ptr<InterestFilterRecord>& interestFilterId) const
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700104 {
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000105 return reinterpret_cast<const InterestFilterId*>(interestFilterId.get()) == m_id;
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700106 }
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000107
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700108private:
109 const InterestFilterId* m_id;
110};
111
112} // namespace ndn
113
114#endif // NDN_DETAIL_INTEREST_FILTER_RECORD_HPP