1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef TOOLS_GN_CONFIG_VALUES_EXTRACTORS_H_
6 #define TOOLS_GN_CONFIG_VALUES_EXTRACTORS_H_
12 #include "tools/gn/config.h"
13 #include "tools/gn/config_values.h"
14 #include "tools/gn/target.h"
18 // Provides a way to iterate through all ConfigValues applying to a given
19 // target. This is more complicated than normal because the target has a list
20 // of configs applying to it, and also config values on the target itself.
22 // This iterator allows one to iterate through all of these in a defined order
23 // in one convenient loop. The order is defined to be the ConfigValues on the
24 // target itself first, then the applying configs, in order.
27 // for (ConfigValueIterator iter(target); !iter.done(); iter.Next())
28 // DoSomething(iter.cur());
29 class ConfigValuesIterator
{
31 explicit ConfigValuesIterator(const Target
* target
)
37 return cur_index_
>= static_cast<int>(target_
->configs().size());
40 const ConfigValues
& cur() const {
42 return target_
->config_values();
43 return target_
->configs()[cur_index_
].ptr
->resolved_values();
46 // Returns the origin of who added this config, if any. This will always be
47 // null for the config values of a target itself.
48 const ParseNode
* origin() const {
51 return target_
->configs()[cur_index_
].origin
;
58 // Returns the config holding the current config values, or NULL for those
59 // config values associated with the target itself.
60 const Config
* GetCurrentConfig() const {
63 return target_
->configs()[cur_index_
].ptr
;
67 const Target
* target_
;
69 // Represents an index into the target_'s configs() or, when -1, the config
70 // values on the target itself.
74 template<typename T
, class Writer
>
75 inline void ConfigValuesToStream(
76 const ConfigValues
& values
,
77 const std::vector
<T
>& (ConfigValues::* getter
)() const,
80 const std::vector
<T
>& v
= (values
.*getter
)();
81 for (size_t i
= 0; i
< v
.size(); i
++)
85 // Writes a given config value that applies to a given target. This collects
86 // all values from the target itself and all configs that apply, and writes
88 template<typename T
, class Writer
>
89 inline void RecursiveTargetConfigToStream(
91 const std::vector
<T
>& (ConfigValues::* getter
)() const,
94 for (ConfigValuesIterator
iter(target
); !iter
.done(); iter
.Next())
95 ConfigValuesToStream(iter
.cur(), getter
, writer
, out
);
98 // Writes the values out as strings with no transformation.
99 void RecursiveTargetConfigStringsToStream(
100 const Target
* target
,
101 const std::vector
<std::string
>& (ConfigValues::* getter
)() const,
102 const EscapeOptions
& escape_options
,
105 #endif // TOOLS_GN_CONFIG_VALUES_EXTRACTORS_H_