Flutter Engine
The Flutter Engine
options.h
Go to the documentation of this file.
1// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_BIN_OPTIONS_H_
6#define RUNTIME_BIN_OPTIONS_H_
7
8#include "bin/dartutils.h"
9#include "platform/globals.h"
10#include "platform/hashmap.h"
11#include "platform/syslog.h"
12
13namespace dart {
14namespace bin {
15
16typedef bool (*OptionProcessorCallback)(const char* arg,
17 CommandLineOptions* vm_options);
18
20 public:
21 OptionProcessor() : next_(first_) { first_ = this; }
22
23 virtual ~OptionProcessor() {}
24
25 // Returns true if name starts with "--".
26 static bool IsValidFlag(const char* name);
27
28 // Returns true if name starts with "-".
29 static bool IsValidShortFlag(const char* name);
30
31 virtual bool Process(const char* option, CommandLineOptions* options) = 0;
32
33 static bool TryProcess(const char* option, CommandLineOptions* options);
34
35 static const char* ProcessOption(const char* option, const char* name);
36
37 static bool ProcessEnvironmentOption(const char* arg,
38 CommandLineOptions* vm_options,
40
41 private:
42 static OptionProcessor* first_;
43 OptionProcessor* next_;
44
45 DISALLOW_ALLOCATION();
46 DISALLOW_COPY_AND_ASSIGN(OptionProcessor);
47};
48
50 public:
52 virtual bool Process(const char* option, CommandLineOptions* vm_options) {
53 return cb_(option, vm_options);
54 }
55
56 private:
58};
59
60#define DEFINE_CB_OPTION(callback) \
61 static CallbackOptionProcessor option_##callback(&callback);
62
63#define DEFINE_STRING_OPTION_CB(name, callback) \
64 class OptionProcessor_##name : public OptionProcessor { \
65 public: \
66 virtual bool Process(const char* option, CommandLineOptions* vm_options) { \
67 const char* value = \
68 OptionProcessor::ProcessOption(option, "--" #name "="); \
69 if (value == nullptr) { \
70 return false; \
71 } \
72 if (*value == '\0') { \
73 Syslog::PrintErr("Empty value for option " #name "\n"); \
74 return false; \
75 } \
76 callback; \
77 return true; \
78 } \
79 }; \
80 static OptionProcessor_##name option_##name;
81
82#define DEFINE_STRING_OPTION(name, variable) \
83 DEFINE_STRING_OPTION_CB(name, { variable = value; })
84
85#define DEFINE_ENUM_OPTION(name, enum_name, variable) \
86 DEFINE_STRING_OPTION_CB(name, { \
87 const char* const* kNames = k##enum_name##Names; \
88 for (intptr_t i = 0; kNames[i] != nullptr; i++) { \
89 if (strcmp(value, kNames[i]) == 0) { \
90 variable = static_cast<enum_name>(i); \
91 return true; \
92 } \
93 } \
94 Syslog::PrintErr( \
95 "Unrecognized value for " #name ": '%s'\nValid values are: ", value); \
96 for (intptr_t i = 0; kNames[i] != nullptr; i++) { \
97 Syslog::PrintErr("%s%s", i > 0 ? ", " : "", kNames[i]); \
98 } \
99 Syslog::PrintErr("\n"); \
100 })
101
102#define DEFINE_BOOL_OPTION_CB(name, callback) \
103 class OptionProcessor_##name : public OptionProcessor { \
104 public: \
105 virtual bool Process(const char* option, CommandLineOptions* vm_options) { \
106 const char* value = OptionProcessor::ProcessOption(option, "--" #name); \
107 if (value == nullptr) { \
108 return false; \
109 } \
110 if (*value == '=') { \
111 Syslog::PrintErr("Non-empty value for option " #name "\n"); \
112 return false; \
113 } \
114 if (*value != '\0') { \
115 return false; \
116 } \
117 callback(vm_options); \
118 return true; \
119 } \
120 }; \
121 static OptionProcessor_##name option_##name;
122
123#define DEFINE_BOOL_OPTION(name, variable) \
124 class OptionProcessor_##name : public OptionProcessor { \
125 public: \
126 virtual bool Process(const char* option, CommandLineOptions* vm_options) { \
127 const char* value = OptionProcessor::ProcessOption(option, "--" #name); \
128 if (value == nullptr) { \
129 return false; \
130 } \
131 if (*value == '=') { \
132 Syslog::PrintErr("Non-empty value for option " #name "\n"); \
133 return false; \
134 } \
135 if (*value != '\0') { \
136 return false; \
137 } \
138 variable = true; \
139 return true; \
140 } \
141 }; \
142 static OptionProcessor_##name option_##name;
143
144#define DEFINE_BOOL_OPTION_SHORT(short_name, long_name, variable) \
145 class OptionProcessor_##long_name : public OptionProcessor { \
146 public: \
147 virtual bool Process(const char* option, CommandLineOptions* vm_options) { \
148 const char* value = \
149 OptionProcessor::ProcessOption(option, "-" #short_name); \
150 if (value == nullptr) { \
151 value = OptionProcessor::ProcessOption(option, "--" #long_name); \
152 } \
153 if (value == nullptr) { \
154 return false; \
155 } \
156 if (*value == '=') { \
157 Syslog::PrintErr("Non-empty value for option " #long_name "\n"); \
158 return false; \
159 } \
160 if (*value != '\0') { \
161 return false; \
162 } \
163 variable = true; \
164 return true; \
165 } \
166 }; \
167 static OptionProcessor_##long_name option_##long_name;
168
169} // namespace bin
170} // namespace dart
171
172#endif // RUNTIME_BIN_OPTIONS_H_
const char * options
CallbackOptionProcessor(OptionProcessorCallback cb)
Definition: options.h:51
virtual bool Process(const char *option, CommandLineOptions *vm_options)
Definition: options.h:52
static bool TryProcess(const char *option, CommandLineOptions *options)
Definition: options.cc:34
static bool IsValidShortFlag(const char *name)
Definition: options.cc:16
static const char * ProcessOption(const char *option, const char *name)
Definition: options.cc:20
static bool ProcessEnvironmentOption(const char *arg, CommandLineOptions *vm_options, dart::SimpleHashMap **environment)
Definition: options.cc:54
virtual bool Process(const char *option, CommandLineOptions *options)=0
virtual ~OptionProcessor()
Definition: options.h:23
static bool IsValidFlag(const char *name)
Definition: options.cc:12
bool(* OptionProcessorCallback)(const char *arg, CommandLineOptions *vm_options)
Definition: options.h:16
static dart::SimpleHashMap * environment
Definition: gen_snapshot.cc:59
Definition: dart_vm.cc:33
const char *const name