blob: b0daadde09d97cd0d22ea79c8238e89a609781fd [file] [log] [blame]
Alexander Afanasyev30cb1172012-07-06 10:47:39 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2012 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 FUNCTOR_HOOK_H_
22#define FUNCTOR_HOOK_H_
23
24#include <boost/intrusive/parent_from_member.hpp>
25
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070026namespace ns3 {
27namespace ndn {
28namespace ndnSIM {
29namespace detail {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070030
31template<class BaseHook, class ValueType, int N>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080032struct FunctorHook {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070033 typedef typename BaseHook::template index<N>::type hook_type;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034 typedef hook_type* hook_ptr;
35 typedef const hook_type* const_hook_ptr;
36
37 typedef ValueType value_type;
38 typedef value_type* pointer;
39 typedef const value_type* const_pointer;
40
41 // Required static functions
42 static hook_ptr
43 to_hook_ptr(value_type& value)
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070044 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 return &value.policy_hook_.template get<N>();
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070046 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047
48 static const_hook_ptr
49 to_hook_ptr(const value_type& value)
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070050 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 return &value.policy_hook_.template get<N>();
52 }
53
54 static pointer
55 to_value_ptr(hook_ptr n)
56 {
57 return boost::intrusive::get_parent_from_member<value_type>(
58 static_cast<BaseHook*>(
59 boost::intrusive::get_parent_from_member<wrap<hook_type>>(n, &wrap<hook_type>::value_)),
60 &value_type::policy_hook_);
61 }
62 static const_pointer
63 to_value_ptr(const_hook_ptr n)
64 {
65 return boost::intrusive::get_parent_from_member<value_type>(
66 static_cast<const BaseHook*>(
67 boost::intrusive::get_parent_from_member<wrap<hook_type>>(n, &wrap<hook_type>::value_)),
68 &value_type::policy_hook_);
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070069 }
70};
71
72} // detail
73} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -070075} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070076
77#endif // FUNCTOR_HOOK_H_