iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tuple.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_TYPING_TUPLE_H
37 #define INCLUDE_NRT_CORE_TYPING_TUPLE_H
38 
39 #include <tuple>
40 
41 namespace nrt
42 {
43  //! Add access to std::tuple elements by type
44  /*! This allows one to access an element of an std::tuple by element type, rather than by element index as already
45  provides by get<index>(tuple). Beware that we scan the tuple by increasing index starting at zero, so if you have
46  two or more elements in your tuple that have the same type, the first one will be returned. Thus you should ensure
47  that all elements in your tuple have a unique type. Example use is as follows:
48 
49  @code
50  std::tuple<int, float, double> tup(1, 2.0F, 3.0);
51  std::cout << nrt::tget<double>(tup) << std::endl; // displays 3
52  @endcode
53 
54  Here is an example of how to contain a tuple with two Point3D elements and access them by type. First we need to
55  wrap each one and create a new type. Once forwarding constructors are supported in GCC this will be easier. Then
56  we can access the elements by type (even though in the end they both derive from Point3D):
57 
58  @code
59  struct position : public nrt::Point3D<double>
60  {
61  position() : nrt::Point3D<double>() { }
62  position(nrt::Point3D<double> const & old) : nrt::Point3D<double>(old) { }
63  };
64 
65  struct velocity : public nrt::Point3D<double>
66  {
67  velocity() : nrt::Point3D<double>() { }
68  velocity(nrt::Point3D<double> const & old) : nrt::Point3D<double>(old) { }
69  };
70 
71  std::tuple<position, velocity> state;
72  std::get<0>(state) = nrt::Point3D<double>(1.0, 2.0, 3.0); // conventional tuple get()
73  std::get<1>(state) = nrt::Point3D<double>(4.0, 5.0, 6.0);
74 
75  std::cout << "Position: " << nrt::tget<position>(state) << std::endl; // by-type nrt::tget()
76  std::cout << "Velocity: " << nrt::tget<velocity>(state) << std::endl;
77  @endcode
78 
79  \ingroup typingmisc */
80  template <typename Elem, typename Tupl, size_t i = 0>
81  typename std::__add_ref<
82  typename std::enable_if<
83  std::is_same<Elem, typename std::tuple_element<i, Tupl>::type>::value, typename std::__add_ref<Elem>::type
84  >::type
85  >::type
86  tget(Tupl & t);
87 
88  //! Const version of nrt::tget()
89  /*! \ingroup typingmisc */
90  template <typename Elem, typename Tupl, size_t i = 0>
91  typename std::__add_c_ref<
92  typename std::enable_if<
93  std::is_same<Elem, typename std::tuple_element<i, Tupl>::type>::value, typename std::__add_c_ref<Elem>::type
94  >::type
95  >::type
96  tget(Tupl const & t);
97 
98 } // namespace nrt
99 
100 // Include implementation details of no interest to users
102 
103 #endif // INCLUDE_NRT_CORE_TYPING_TUPLE_H
104