blob: a638e5993610383714238965b1c6494092b95ca5 [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 Afanasyev30cb1172012-07-06 10:47:39 -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 Afanasyev30cb1172012-07-06 10:47:39 -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 Afanasyev30cb1172012-07-06 10:47:39 -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 Afanasyev30cb1172012-07-06 10:47:39 -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 Afanasyev30cb1172012-07-06 10:47:39 -070019
20#ifndef FUNCTOR_HOOK_H_
21#define FUNCTOR_HOOK_H_
22
23#include <boost/intrusive/parent_from_member.hpp>
24
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070025namespace ns3 {
26namespace ndn {
27namespace ndnSIM {
28namespace detail {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070029
30template<class BaseHook, class ValueType, int N>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080031struct FunctorHook {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070032 typedef typename BaseHook::template index<N>::type hook_type;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033 typedef hook_type* hook_ptr;
34 typedef const hook_type* const_hook_ptr;
35
36 typedef ValueType value_type;
37 typedef value_type* pointer;
38 typedef const value_type* const_pointer;
39
40 // Required static functions
41 static hook_ptr
42 to_hook_ptr(value_type& value)
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070043 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 return &value.policy_hook_.template get<N>();
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070045 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046
47 static const_hook_ptr
48 to_hook_ptr(const value_type& value)
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070049 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 return &value.policy_hook_.template get<N>();
51 }
52
53 static pointer
54 to_value_ptr(hook_ptr n)
55 {
56 return boost::intrusive::get_parent_from_member<value_type>(
57 static_cast<BaseHook*>(
58 boost::intrusive::get_parent_from_member<wrap<hook_type>>(n, &wrap<hook_type>::value_)),
59 &value_type::policy_hook_);
60 }
61 static const_pointer
62 to_value_ptr(const_hook_ptr n)
63 {
64 return boost::intrusive::get_parent_from_member<value_type>(
65 static_cast<const BaseHook*>(
66 boost::intrusive::get_parent_from_member<wrap<hook_type>>(n, &wrap<hook_type>::value_)),
67 &value_type::policy_hook_);
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070068 }
69};
70
71} // detail
72} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -070074} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070075
76#endif // FUNCTOR_HOOK_H_