Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * 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 Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * 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 Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * 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 Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 19 | |
| 20 | #ifndef PAYLOAD_WITH_INDEX_H_ |
| 21 | #define PAYLOAD_WITH_INDEX_H_ |
| 22 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 23 | namespace ns3 { |
| 24 | namespace ndn { |
| 25 | namespace ndnSIM { |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 27 | template<typename PayloadTraits, typename IndexTraits> |
| 28 | class payload_with_index { |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 29 | public: |
| 30 | typedef PayloadTraits::pointer_type iterator; |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 32 | typedef typename IndexTraits:: |
| 33 | template index<PayloadTraits, |
| 34 | typename IndexTraits::template container_hook<parent_trie>::type>::type |
| 35 | index_container; |
| 36 | |
| 37 | inline payload_with_index() |
| 38 | : index_(*this) |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 42 | inline std::pair<iterator, bool> |
| 43 | insert(typename iterator payload) |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 44 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | bool ok = policy_.insert(s_iterator_to(item.first)); |
| 46 | if (!ok) { |
| 47 | item.first->erase(); // cannot insert |
| 48 | return std::make_pair(end(), false); |
| 49 | } |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 50 | |
| 51 | return item; |
| 52 | } |
| 53 | |
| 54 | // inline void |
| 55 | // erase (const FullKey &key) |
| 56 | // { |
| 57 | // iterator foundItem, lastItem; |
| 58 | // bool reachLast; |
Alexander Afanasyev | 0717901 | 2014-12-21 00:23:04 -0800 | [diff] [blame] | 59 | // std::tie (foundItem, reachLast, lastItem) = trie_.find (key); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 61 | // if (!reachLast || lastItem->payload () == PayloadTraits::empty_payload) |
| 62 | // return; // nothing to invalidate |
| 63 | |
| 64 | // erase (lastItem); |
| 65 | // } |
| 66 | |
| 67 | // inline void |
| 68 | // erase (iterator node) |
| 69 | // { |
| 70 | // if (node == end ()) return; |
| 71 | |
| 72 | // policy_.erase (s_iterator_to (node)); |
| 73 | // node->erase (); // will do cleanup here |
| 74 | // } |
| 75 | |
| 76 | // inline void |
| 77 | // clear () |
| 78 | // { |
| 79 | // policy_.clear (); |
| 80 | // trie_.clear (); |
| 81 | // } |
| 82 | |
| 83 | // template<typename Modifier> |
| 84 | // bool |
| 85 | // modify (iterator position, Modifier mod) |
| 86 | // { |
| 87 | // if (position == end ()) return false; |
| 88 | // if (position->payload () == PayloadTraits::empty_payload) return false; |
| 89 | |
| 90 | // mod (*position->payload ()); |
| 91 | // policy_.update (position); |
| 92 | // return true; |
| 93 | // } |
| 94 | |
| 95 | private: |
| 96 | mutable index_container policy_; |
| 97 | }; |
| 98 | |
| 99 | } // ndnSIM |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 100 | } // ndn |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 101 | } // ns3 |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 102 | |
| 103 | #endif // PAYLOAD_WITH_POLICY_H_ |