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