| 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 | |
| 35 | /** |
| 36 | * A value like argument value type is a value that can be set using |
| 37 | * operator>>. This is the default value type. |
| 38 | */ |
| 39 | struct ValueLike { |
| 40 | typedef ValueLike ValueCategory; |
| 41 | virtual ~ValueLike() {} |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * A string like argument value type is a value that can be set using |
| 46 | * operator=(string). Useful if the value type contains spaces which |
| 47 | * will be broken up into individual tokens by operator>>. |
| 48 | */ |
| 49 | struct StringLike { |
| 50 | virtual ~StringLike() {} |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * A class can inherit from this object to make it have string like |
| 55 | * traits. This is a compile time thing and does not add any overhead |
| 56 | * to the inherenting class. |
| 57 | */ |
| 58 | struct StringLikeTrait { |
| 59 | typedef StringLike ValueCategory; |
| 60 | virtual ~StringLikeTrait() {} |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * A class can inherit from this object to make it have value like |
| 65 | * traits. This is a compile time thing and does not add any overhead |
| 66 | * to the inherenting class. |
| 67 | */ |
| 68 | struct ValueLikeTrait { |
| 69 | typedef ValueLike ValueCategory; |
| 70 | virtual ~ValueLikeTrait() {} |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * Arg traits are used to get compile type specialization when parsing |
| 75 | * argument values. Using an ArgTraits you can specify the way that |
| 76 | * values gets assigned to any particular type during parsing. The two |
| 77 | * supported types are StringLike and ValueLike. ValueLike is the |
| 78 | * default and means that operator>> will be used to assign values to |
| 79 | * the type. |
| 80 | */ |
| 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: |
| 118 | typedef typename DefaultArgTrait<T, hasTrait>::ValueCategory ValueCategory; |
| 119 | }; |
| 120 | |
| 121 | } // namespace TCLAP |
| 122 | |
| 123 | #endif // TCLAP_ARG_TRAITS_H |