tclap  1.4.0
ArgTraits.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 /******************************************************************************
4  *
5  * file: ArgTraits.h
6  *
7  * Copyright (c) 2007, Daniel Aarno, Michael E. Smoot .
8  * Copyright (c) 2017 Google LLC
9  * All rights reserved.
10  *
11  * See the file COPYING in the top directory of this distribution for
12  * more information.
13  *
14  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  *
22  *****************************************************************************/
23 
24 // This is an internal tclap file, you should probably not have to
25 // include this directly
26 
27 #ifndef TCLAP_ARG_TRAITS_H
28 #define TCLAP_ARG_TRAITS_H
29 
30 namespace TCLAP {
31 
32 // We use two empty structs to get compile type specialization
33 // function to work
34 
39 struct ValueLike {
41  virtual ~ValueLike() {}
42 };
43 
49 struct StringLike {
50  virtual ~StringLike() {}
51 };
52 
60  virtual ~StringLikeTrait() {}
61 };
62 
70  virtual ~ValueLikeTrait() {}
71 };
72 
81 template <typename T>
82 class ArgTraits {
83  // This is a bit silly, but what we want to do is:
84  // 1) If there exists a specialization of ArgTraits for type X,
85  // use it.
86  //
87  // 2) If no specialization exists but X has the typename
88  // X::ValueCategory, use the specialization for X::ValueCategory.
89  //
90  // 3) If neither (1) nor (2) defines the trait, use the default
91  // which is ValueLike.
92 
93  // This is the "how":
94  //
95  // test<T>(0) (where 0 is the NULL ptr) will match
96  // test(typename C::ValueCategory*) iff type T has the
97  // corresponding typedef. If it does not test(...) will be
98  // matched. This allows us to determine if T::ValueCategory
99  // exists by checking the sizeof for the test function (return
100  // value must have different sizeof).
101  template <typename C>
102  static short test(typename C::ValueCategory *); // NOLINT
103  template <typename C>
104  static long test(...); // NOLINT
105  static const bool hasTrait = sizeof(test<T>(0)) == sizeof(short); // NOLINT
106 
107  template <typename C, bool>
108  struct DefaultArgTrait {
109  typedef ValueLike ValueCategory;
110  };
111 
112  template <typename C>
113  struct DefaultArgTrait<C, true> {
114  typedef typename C::ValueCategory ValueCategory;
115  };
116 
117 public:
119 };
120 
121 } // namespace TCLAP
122 
123 #endif // TCLAP_ARG_TRAITS_H
Arg traits are used to get compile type specialization when parsing argument values.
Definition: ArgTraits.h:82
virtual ~ValueLikeTrait()
Definition: ArgTraits.h:70
StringLike ValueCategory
Definition: ArgTraits.h:59
A value like argument value type is a value that can be set using operator>>.
Definition: ArgTraits.h:39
A string like argument value type is a value that can be set using operator=(string).
Definition: ArgTraits.h:49
A class can inherit from this object to make it have string like traits.
Definition: ArgTraits.h:58
virtual ~StringLike()
Definition: ArgTraits.h:50
ValueLike ValueCategory
Definition: ArgTraits.h:40
A class can inherit from this object to make it have value like traits.
Definition: ArgTraits.h:68
Definition: Arg.h:46
DefaultArgTrait< T, hasTrait >::ValueCategory ValueCategory
Definition: ArgTraits.h:118
virtual ~StringLikeTrait()
Definition: ArgTraits.h:60
ValueLike ValueCategory
Definition: ArgTraits.h:69
virtual ~ValueLike()
Definition: ArgTraits.h:41