tclap: ArgTraits.h Source File

tclap 1.2.5

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_ARGTRAITS_H
28 #define TCLAP_ARGTRAITS_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 {
40 typedef ValueLike ValueCategory;
41 virtual ~ValueLike() {}
42 };
43
49 struct StringLike {
50 virtual ~StringLike() {}
51 };
52
58 struct StringLikeTrait {
59 typedef StringLike ValueCategory;
60 virtual ~StringLikeTrait() {}
61 };
62
68 struct ValueLikeTrait {
69 typedef ValueLike ValueCategory;
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> static short test(typename C::ValueCategory*);
102 template<typename C> static long test(...);
103 static const bool hasTrait = sizeof(test<T>(0)) == sizeof(short);
104
105 template <typename C, bool>
106 struct DefaultArgTrait {
107 typedef ValueLike ValueCategory;
108 };
109
110 template <typename C>
111 struct DefaultArgTrait<C, true> {
112 typedef typename C::ValueCategory ValueCategory;
113 };
114
115 public:
116 typedef typename DefaultArgTrait<T, hasTrait>::ValueCategory ValueCategory;
117 };
118
119 } // namespace
120
121 #endif
122
TCLAP::ArgTraits
Arg traits are used to get compile type specialization when parsing argument values.
Definition: ArgTraits.h:82
TCLAP::ArgTraits::ValueCategory
DefaultArgTrait< T, hasTrait >::ValueCategory ValueCategory
Definition: ArgTraits.h:116
TCLAP
Definition: Arg.h:48
TCLAP::StringLikeTrait
A class can inherit from this object to make it have string like traits.
Definition: ArgTraits.h:58
TCLAP::StringLikeTrait::ValueCategory
StringLike ValueCategory
Definition: ArgTraits.h:59
TCLAP::StringLikeTrait::~StringLikeTrait
virtual ~StringLikeTrait()
Definition: ArgTraits.h:60
TCLAP::StringLike
A string like argument value type is a value that can be set using operator=(string).
Definition: ArgTraits.h:49
TCLAP::StringLike::~StringLike
virtual ~StringLike()
Definition: ArgTraits.h:50
TCLAP::ValueLikeTrait
A class can inherit from this object to make it have value like traits.
Definition: ArgTraits.h:68
TCLAP::ValueLikeTrait::~ValueLikeTrait
virtual ~ValueLikeTrait()
Definition: ArgTraits.h:70
TCLAP::ValueLikeTrait::ValueCategory
ValueLike ValueCategory
Definition: ArgTraits.h:69
TCLAP::ValueLike
A value like argument value type is a value that can be set using operator>>.
Definition: ArgTraits.h:39
TCLAP::ValueLike::ValueCategory
ValueLike ValueCategory
Definition: ArgTraits.h:40
TCLAP::ValueLike::~ValueLike
virtual ~ValueLike()
Definition: ArgTraits.h:41