iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OptionalImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
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 #ifndef INCLUDE_NRT_CORE_DESIGN_DETAILS_OPTIONALIMPL_H
36 #define INCLUDE_NRT_CORE_DESIGN_DETAILS_OPTIONALIMPL_H
37 
38 //! @cond PRIVATE_NEVERDEFINED
39 template <class T> inline
40 nrt::Optional<T>::Optional() : ParentType()
41 { }
42 
43 template <class T> inline
44 nrt::Optional<T>::Optional( OptionalNoneType const & /* none */) : ParentType()
45 { }
46 
47 template <class T> inline
48 nrt::Optional<T>::Optional( Optional const & other ) : ParentType( other )
49 { }
50 
51 template <class T>
52 template <class U> inline
53 nrt::Optional<T>::Optional( Optional<U> const & other,
54  typename std::enable_if<!std::is_reference<T>::value &&
55  std::is_reference<U>::value &&
56  std::is_same<typename std::remove_reference<U>::type, T>::value,
57  bool>::type ) : ParentType()
58 {
59  if( other )
60  operator=( *other );
61 }
62 
63 template <class T>
64 template <class U> inline
65 nrt::Optional<T>::Optional( Optional<U> const & other,
66  typename std::enable_if<std::is_reference<T>::value &&
67  !std::is_reference<U>::value &&
68  std::is_same<typename std::remove_reference<T>::type, U>::value,
69  bool>::type )
70 {
71  static_assert( !std::is_reference<T>::value, "Cannot copy create an Optional reference from an Optional value type" );
72  static_assert( std::is_same<U, typename std::remove_reference<T>::type>::value, "Cannot convert to required type" );
73 }
74 
75 template <class T> inline
76 nrt::Optional<T>::Optional( Optional && other ) : ParentType( std::move( other ) )
77 { }
78 
79 template <class T>
80 template <class U> inline
81 nrt::Optional<T>::Optional( Optional<U> && other,
82  typename std::enable_if<!std::is_reference<T>::value &&
83  std::is_reference<U>::value &&
84  std::is_same<typename std::remove_reference<U>::type, T>::value,
85  bool>::type ) : ParentType()
86 {
87  if( other )
88  operator=( std::move( *other ) );
89 
90  other.clear();
91 }
92 
93 template <class T>
94 template <class U> inline
95 nrt::Optional<T>::Optional( Optional<U> && /*other*/,
96  typename std::enable_if<std::is_reference<T>::value &&
97  !std::is_reference<U>::value &&
98  std::is_same<typename std::remove_reference<T>::type, U>::value,
99  bool>::type )
100 {
101  static_assert( !std::is_reference<T>::value, "Cannot move create an Optional reference from an Optional value type" );
102  static_assert( std::is_same<U, typename std::remove_reference<T>::type>::value, "Cannot convert to required type" );
103 }
104 
105 template <class T> inline
106 nrt::Optional<T>::Optional( ArgumentTypeCopy otherT ) : ParentType( otherT )
107 { }
108 
109 template <class T>
110 template <class U> inline
111 nrt::Optional<T>::Optional( U && otherT,
112  typename std::enable_if<!std::is_reference<T>::value &&
113  std::is_same<U, typename std::remove_reference<T>::type>::value,
114  bool>::type ) : ParentType( std::move( otherT ) )
115 { }
116 
117 template <class T>
118 template <class U> inline
119 nrt::Optional<T>::Optional( U && /* otherT */,
120  typename std::enable_if<std::is_reference<T>::value &&
121  std::is_same<U, typename std::remove_reference<T>::type>::value,
122  bool>::type )
123 {
124  static_assert( !std::is_reference<T>::value, "Cannot create an Optional reference from an r-value" );
125  static_assert( std::is_same<U, typename std::remove_reference<T>::type>::value, "Cannot convert to required type" );
126 }
127 
128 template <class T> inline
129 auto nrt::Optional<T>::ifSet( std::function<void(ArgumentTypeCopy)> func ) -> Optional &
130 {
131  if( isSet() )
132  func( operator*() );
133 
134  return *this;
135 }
136 
137 template <class T> inline
138 auto nrt::Optional<T>::ifSet( std::function<void(ArgumentTypeCopy)> func ) const -> Optional const &
139 {
140  if( isSet() )
141  func( operator*() );
142 
143  return *this;
144 }
145 
146 template <class T> inline
147 void nrt::Optional<T>::otherwise( std::function<void()> func ) const
148 {
149  if( !isSet() )
150  func();
151 }
152 
153 //! @endcond
154 #endif // INCLUDE_NRT_CORE_DESIGN_DETAILS_OPTIONALIMPL_H