Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * 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. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_TAG_HOST_HPP |
| 23 | #define NDN_TAG_HOST_HPP |
| 24 | |
| 25 | #include "common.hpp" |
| 26 | #include "tag.hpp" |
| 27 | |
| 28 | #include <map> |
| 29 | |
| 30 | namespace ndn { |
| 31 | |
| 32 | /** \brief Base class to store tag information (e.g., inside Interest and Data packets) |
| 33 | */ |
| 34 | class TagHost |
| 35 | { |
| 36 | public: |
| 37 | /** \brief get a tag item |
| 38 | * \tparam T type of the tag, which must be a subclass of ndn::Tag |
| 39 | * \retval nullptr if no Tag of type T is stored |
| 40 | */ |
| 41 | template<typename T> |
| 42 | shared_ptr<T> |
| 43 | getTag() const; |
| 44 | |
| 45 | /** \brief set a tag item |
| 46 | * \tparam T type of the tag, which must be a subclass of ndn::Tag |
| 47 | * \note Tag can be set even on a const tag host instance |
| 48 | */ |
| 49 | template<typename T> |
| 50 | void |
| 51 | setTag(shared_ptr<T> tag) const; |
| 52 | |
| 53 | /** \brief remove tag item |
| 54 | * \note Tag can be removed even on a const tag host instance |
| 55 | */ |
| 56 | template<typename T> |
| 57 | void |
| 58 | removeTag() const; |
| 59 | |
| 60 | private: |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 61 | mutable std::map<int, shared_ptr<Tag>> m_tags; |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 64 | template<typename T> |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 65 | shared_ptr<T> |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 66 | TagHost::getTag() const |
| 67 | { |
| 68 | static_assert(std::is_base_of<Tag, T>::value, "T must inherit from Tag"); |
| 69 | |
| 70 | auto it = m_tags.find(T::getTypeId()); |
| 71 | if (it == m_tags.end()) { |
| 72 | return nullptr; |
| 73 | } |
| 74 | return static_pointer_cast<T>(it->second); |
| 75 | } |
| 76 | |
| 77 | template<typename T> |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 78 | void |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 79 | TagHost::setTag(shared_ptr<T> tag) const |
| 80 | { |
| 81 | static_assert(std::is_base_of<Tag, T>::value, "T must inherit from Tag"); |
| 82 | |
| 83 | if (tag == nullptr) { |
| 84 | m_tags.erase(T::getTypeId()); |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 85 | } |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 86 | else { |
| 87 | m_tags[T::getTypeId()] = std::move(tag); |
| 88 | } |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | template<typename T> |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 92 | void |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 93 | TagHost::removeTag() const |
| 94 | { |
| 95 | setTag<T>(nullptr); |
| 96 | } |
| 97 | |
| 98 | } // namespace ndn |
| 99 | |
| 100 | #endif // NDN_TAG_HOST_HPP |