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
30namespace TCLAP {
31
32// We use two empty structs to get compile type specialization
33// function to work
34
39struct ValueLike {
41 virtual ~ValueLike() {}
42};
43
49struct StringLike {
50 virtual ~StringLike() {}
51};
52
64
74 virtual ~ValueLikeTrait() {}
75};
76
85template <typename T>
86class ArgTraits {
87 // This is a bit silly, but what we want to do is:
88 // 1) If there exists a specialization of ArgTraits for type X,
89 // use it.
90 //
91 // 2) If no specialization exists but X has the typename
92 // X::ValueCategory, use the specialization for X::ValueCategory.
93 //
94 // 3) If neither (1) nor (2) defines the trait, use the default
95 // which is ValueLike.
96
97 // This is the "how":
98 //
99 // test<T>(0) (where 0 is the NULL ptr) will match
100 // test(typename C::ValueCategory*) iff type T has the
101 // corresponding typedef. If it does not test(...) will be
102 // matched. This allows us to determine if T::ValueCategory
103 // exists by checking the sizeof for the test function (return
104 // value must have different sizeof).
105 template <typename C>
106 static short test(typename C::ValueCategory *); // NOLINT
107 template <typename C>
108 static long test(...); // NOLINT
109 static const bool hasTrait = sizeof(test<T>(0)) == sizeof(short); // NOLINT
110
111 template <typename C, bool>
112 struct DefaultArgTrait {
113 typedef ValueLike ValueCategory;
114 };
115
116 template <typename C>
117 struct DefaultArgTrait<C, true> {
118 typedef typename C::ValueCategory ValueCategory;
119 };
120
121public:
123};
124
125} // namespace TCLAP
126
127#endif // TCLAP_ARG_TRAITS_H
Arg traits are used to get compile type specialization when parsing argument values.
Definition ArgTraits.h:86
DefaultArgTrait< T, hasTrait >::ValueCategory ValueCategory
Definition ArgTraits.h:122
Definition Arg.h:52
A class can inherit from this object to make it have string like traits.
Definition ArgTraits.h:60
StringLike ValueCategory
Definition ArgTraits.h:61
virtual ~StringLikeTrait()
Definition ArgTraits.h:62
A string like argument value type is a value that can be set using operator=(string).
Definition ArgTraits.h:49
virtual ~StringLike()
Definition ArgTraits.h:50
A class can inherit from this object to make it have value like traits.
Definition ArgTraits.h:72
virtual ~ValueLikeTrait()
Definition ArgTraits.h:74
ValueLike ValueCategory
Definition ArgTraits.h:73
A value like argument value type is a value that can be set using operator>>.
Definition ArgTraits.h:39
ValueLike ValueCategory
Definition ArgTraits.h:40
virtual ~ValueLike()
Definition ArgTraits.h:41