blob: 40105118fb242c185b0f127c8ec897d79f36c79e [file] [log] [blame]
Alexander Afanasyev36b45772012-07-10 16:57:42 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#ifndef PAYLOAD_WITH_INDEX_H_
22#define PAYLOAD_WITH_INDEX_H_
23
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070024namespace ns3 {
25namespace ndn {
26namespace ndnSIM {
Alexander Afanasyev36b45772012-07-10 16:57:42 -070027
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080028template<typename PayloadTraits, typename IndexTraits>
29class payload_with_index {
Alexander Afanasyev36b45772012-07-10 16:57:42 -070030public:
31 typedef PayloadTraits::pointer_type iterator;
Alexander Afanasyev36b45772012-07-10 16:57:42 -070032
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033 typedef typename IndexTraits::
34 template index<PayloadTraits,
35 typename IndexTraits::template container_hook<parent_trie>::type>::type
36 index_container;
37
38 inline payload_with_index()
39 : index_(*this)
Alexander Afanasyev36b45772012-07-10 16:57:42 -070040 {
41 }
42
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 inline std::pair<iterator, bool>
44 insert(typename iterator payload)
Alexander Afanasyev36b45772012-07-10 16:57:42 -070045 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 bool ok = policy_.insert(s_iterator_to(item.first));
47 if (!ok) {
48 item.first->erase(); // cannot insert
49 return std::make_pair(end(), false);
50 }
Alexander Afanasyev36b45772012-07-10 16:57:42 -070051
52 return item;
53 }
54
55 // inline void
56 // erase (const FullKey &key)
57 // {
58 // iterator foundItem, lastItem;
59 // bool reachLast;
60 // boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061
Alexander Afanasyev36b45772012-07-10 16:57:42 -070062 // if (!reachLast || lastItem->payload () == PayloadTraits::empty_payload)
63 // return; // nothing to invalidate
64
65 // erase (lastItem);
66 // }
67
68 // inline void
69 // erase (iterator node)
70 // {
71 // if (node == end ()) return;
72
73 // policy_.erase (s_iterator_to (node));
74 // node->erase (); // will do cleanup here
75 // }
76
77 // inline void
78 // clear ()
79 // {
80 // policy_.clear ();
81 // trie_.clear ();
82 // }
83
84 // template<typename Modifier>
85 // bool
86 // modify (iterator position, Modifier mod)
87 // {
88 // if (position == end ()) return false;
89 // if (position->payload () == PayloadTraits::empty_payload) return false;
90
91 // mod (*position->payload ());
92 // policy_.update (position);
93 // return true;
94 // }
95
96private:
97 mutable index_container policy_;
98};
99
100} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700102} // ns3
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700103
104#endif // PAYLOAD_WITH_POLICY_H_