blob: 56dc89e160e847d8a4705602f99c05fb9f5534e9 [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) 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 MULTI_TYPE_CONTAINER_H_
22#define MULTI_TYPE_CONTAINER_H_
23
24#include <boost/mpl/inherit_linearly.hpp>
25#include <boost/mpl/inherit.hpp>
26#include <boost/mpl/at.hpp>
27
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070028namespace ns3 {
29namespace ndn {
30namespace ndnSIM {
31namespace detail {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070032
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033template<class T>
34struct wrap {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070035 T value_;
36};
37
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038template<class Vector>
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070039struct multi_type_container
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 : public boost::mpl::inherit_linearly<Vector, boost::mpl::inherit<wrap<boost::mpl::_2>,
41 boost::mpl::_1>>::type {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070042 template<int N>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 struct index {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070044 typedef typename boost::mpl::at_c<Vector, N>::type type;
45 };
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070047 template<class T>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048 T&
49 get()
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070050 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 return static_cast<wrap<T>&>(*this).value_;
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070052 }
53
54 template<class T>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080055 const T&
56 get() const
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070057 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080058 return static_cast<const wrap<T>&>(*this).value_;
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070059 }
60
61 template<int N>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062 typename boost::mpl::at_c<Vector, N>::type&
63 get()
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070064 {
65 typedef typename boost::mpl::at_c<Vector, N>::type T;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 return static_cast<wrap<T>&>(*this).value_;
67 }
68
69 template<int N>
70 const typename boost::mpl::at_c<Vector, N>::type&
71 get() const
72 {
73 typedef typename boost::mpl::at_c<Vector, N>::type T;
74 return static_cast<const wrap<T>&>(*this).value_;
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070075 }
76};
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070078} // detail
79} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070080} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -070081} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070082
83#endif // MULTI_TYPE_CONTAINER_H_