blob: c4c904ebcc117c2fafa73674c66f92cfe64a1ab8 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev36b45772012-07-10 16:57:42 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev36b45772012-07-10 16:57:42 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyev36b45772012-07-10 16:57:42 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev36b45772012-07-10 16:57:42 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyev36b45772012-07-10 16:57:42 -070019
20#ifndef PAYLOAD_WITH_INDEX_H_
21#define PAYLOAD_WITH_INDEX_H_
22
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080023/// @cond include_hidden
24
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070025namespace ns3 {
26namespace ndn {
27namespace ndnSIM {
Alexander Afanasyev36b45772012-07-10 16:57:42 -070028
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080029template<typename PayloadTraits, typename IndexTraits>
30class payload_with_index {
Alexander Afanasyev36b45772012-07-10 16:57:42 -070031public:
32 typedef PayloadTraits::pointer_type iterator;
Alexander Afanasyev36b45772012-07-10 16:57:42 -070033
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034 typedef typename IndexTraits::
35 template index<PayloadTraits,
36 typename IndexTraits::template container_hook<parent_trie>::type>::type
37 index_container;
38
39 inline payload_with_index()
40 : index_(*this)
Alexander Afanasyev36b45772012-07-10 16:57:42 -070041 {
42 }
43
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 inline std::pair<iterator, bool>
45 insert(typename iterator payload)
Alexander Afanasyev36b45772012-07-10 16:57:42 -070046 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047 bool ok = policy_.insert(s_iterator_to(item.first));
48 if (!ok) {
49 item.first->erase(); // cannot insert
50 return std::make_pair(end(), false);
51 }
Alexander Afanasyev36b45772012-07-10 16:57:42 -070052
53 return item;
54 }
55
56 // inline void
57 // erase (const FullKey &key)
58 // {
59 // iterator foundItem, lastItem;
60 // bool reachLast;
Alexander Afanasyev07179012014-12-21 00:23:04 -080061 // std::tie (foundItem, reachLast, lastItem) = trie_.find (key);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062
Alexander Afanasyev36b45772012-07-10 16:57:42 -070063 // if (!reachLast || lastItem->payload () == PayloadTraits::empty_payload)
64 // return; // nothing to invalidate
65
66 // erase (lastItem);
67 // }
68
69 // inline void
70 // erase (iterator node)
71 // {
72 // if (node == end ()) return;
73
74 // policy_.erase (s_iterator_to (node));
75 // node->erase (); // will do cleanup here
76 // }
77
78 // inline void
79 // clear ()
80 // {
81 // policy_.clear ();
82 // trie_.clear ();
83 // }
84
85 // template<typename Modifier>
86 // bool
87 // modify (iterator position, Modifier mod)
88 // {
89 // if (position == end ()) return false;
90 // if (position->payload () == PayloadTraits::empty_payload) return false;
91
92 // mod (*position->payload ());
93 // policy_.update (position);
94 // return true;
95 // }
96
97private:
98 mutable index_container policy_;
99};
100
101} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700103} // ns3
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700104
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800105/// @endcond
106
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700107#endif // PAYLOAD_WITH_POLICY_H_