1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_CMDLINE_CMDLINE_TYPE_PARSER_H_
18 #define ART_CMDLINE_CMDLINE_TYPE_PARSER_H_
19 
20 #include "cmdline_parse_result.h"
21 
22 namespace art {
23 
24 // Base class for user-defined CmdlineType<T> specializations.
25 //
26 // Not strictly necessary, but if the specializations fail to Define all of these functions
27 // the compilation will fail.
28 template <typename T>
29 struct CmdlineTypeParser {
30   // Return value of parsing attempts. Represents a Success(T value) or an Error(int code)
31   using Result = CmdlineParseResult<T>;
32 
33   // Parse a single value for an argument definition out of the wildcard component.
34   //
35   // e.g. if the argument definition was "foo:_", and the user-provided input was "foo:bar",
36   // then args is "bar".
ParseCmdlineTypeParser37   Result Parse(const std::string& args ATTRIBUTE_UNUSED) {
38     assert(false);
39     return Result::Failure("Missing type specialization and/or value map");
40   }
41 
42   // Parse a value and append it into the existing value so far, for argument
43   // definitions which are marked with AppendValues().
44   //
45   // The value is parsed out of the wildcard component as in Parse.
46   //
47   // If the initial value does not exist yet, a default value is created by
48   // value-initializing with 'T()'.
ParseAndAppendCmdlineTypeParser49   Result ParseAndAppend(const std::string& args ATTRIBUTE_UNUSED,
50                         T& existing_value ATTRIBUTE_UNUSED) {
51     assert(false);
52     return Result::Failure("Missing type specialization and/or value map");
53   }
54 
55   // Runtime type name of T, so that we can print more useful error messages.
NameCmdlineTypeParser56   static const char* Name() { assert(false); return "UnspecializedType"; }
57 
58   // Whether or not your type can parse argument definitions defined without a "_"
59   // e.g. -Xenable-profiler just mutates the existing profiler struct in-place
60   // so it doesn't need to do any parsing other than token recognition.
61   //
62   // If this is false, then either the argument definition has a _, from which the parsing
63   // happens, or the tokens get mapped to a value list/map from which a 1:1 matching occurs.
64   //
65   // This should almost *always* be false!
66   static constexpr bool kCanParseBlankless = false;
67 
68  protected:
69   // Don't accidentally initialize instances of this directly; they will assert at runtime.
70   CmdlineTypeParser() = default;
71 };
72 
73 
74 }  // namespace art
75 
76 #endif  // ART_CMDLINE_CMDLINE_TYPE_PARSER_H_
77