tag-host.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 Regents of the University of California.
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_CXX_DETAIL_TAG_HOST_HPP
23 #define NDN_CXX_DETAIL_TAG_HOST_HPP
24 
26 #include "ndn-cxx/tag.hpp"
27 
28 #include <map>
29 
30 namespace ndn {
31 
35 class TagHost
36 {
37 public:
42  template<typename T>
43  shared_ptr<T>
44  getTag() const;
45 
50  template<typename T>
51  void
52  setTag(shared_ptr<T> tag) const;
53 
57  template<typename T>
58  void
59  removeTag() const;
60 
61 private:
62  mutable std::map<int, shared_ptr<Tag>> m_tags;
63 };
64 
65 template<typename T>
66 shared_ptr<T>
68 {
69  static_assert(std::is_base_of<Tag, T>::value, "T must inherit from Tag");
70 
71  auto it = m_tags.find(T::getTypeId());
72  if (it == m_tags.end()) {
73  return nullptr;
74  }
75  return static_pointer_cast<T>(it->second);
76 }
77 
78 template<typename T>
79 void
80 TagHost::setTag(shared_ptr<T> tag) const
81 {
82  static_assert(std::is_base_of<Tag, T>::value, "T must inherit from Tag");
83 
84  if (tag == nullptr) {
85  m_tags.erase(T::getTypeId());
86  }
87  else {
88  m_tags[T::getTypeId()] = std::move(tag);
89  }
90 }
91 
92 template<typename T>
93 void
95 {
96  setTag<T>(nullptr);
97 }
98 
99 } // namespace ndn
100 
101 #endif // NDN_CXX_DETAIL_TAG_HOST_HPP
Base class to store tag information, e.g., inside Interest and Data packets.
Definition: tag-host.hpp:36
void removeTag() const
Remove a tag item.
Definition: tag-host.hpp:94
shared_ptr< T > getTag() const
Get a tag item.
Definition: tag-host.hpp:67
void setTag(shared_ptr< T > tag) const
Set (add or replace) a tag item.
Definition: tag-host.hpp:80
Common includes and macros used throughout the library.
Definition: data.cpp:25