config_values_extractors.h revision 58537e28ecd584eab876aee8be7156509866d23a
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.
4
5#ifndef TOOLS_GN_CONFIG_VALUES_EXTRACTORS_H_
6#define TOOLS_GN_CONFIG_VALUES_EXTRACTORS_H_
7
8#include <ostream>
9#include <string>
10#include <vector>
11
12#include "tools/gn/config.h"
13#include "tools/gn/config_values.h"
14#include "tools/gn/target.h"
15
16struct EscapeOptions;
17
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.
21//
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.
25//
26// Example:
27//   for (ConfigValueIterator iter(target); !iter.done(); iter.Next())
28//     DoSomething(iter->cur());
29class ConfigValuesIterator {
30 public:
31  explicit ConfigValuesIterator(const Target* target)
32      : target_(target),
33        cur_index_(-1) {
34  }
35
36  bool done() const {
37    return cur_index_ >= static_cast<int>(target_->configs().size());
38  }
39
40  const ConfigValues& cur() const {
41    if (cur_index_ == -1)
42      return target_->config_values();
43    return target_->configs()[cur_index_]->config_values();
44  }
45
46  void Next() {
47    cur_index_++;
48  }
49
50  // Returns the config holding the current config values, or NULL for those
51  // config values associated with the target itself.
52  const Config* GetCurrentConfig() const {
53    if (cur_index_ == -1)
54      return NULL;
55    return target_->configs()[cur_index_];
56  }
57
58 private:
59  const Target* target_;
60
61  // Represents an index into the target_'s configs() or, when -1, the config
62  // values on the target itself.
63  int cur_index_;
64};
65
66template<typename T, class Writer>
67inline void ConfigValuesToStream(
68    const ConfigValues& values,
69    const std::vector<T>& (ConfigValues::* getter)() const,
70    const Writer& writer,
71    std::ostream& out) {
72  const std::vector<T>& v = (values.*getter)();
73  for (size_t i = 0; i < v.size(); i++)
74    writer(v[i], out);
75};
76
77// Writes a given config value that applies to a given target. This collects
78// all values from the target itself and all configs that apply, and writes
79// then in order.
80template<typename T, class Writer>
81inline void RecursiveTargetConfigToStream(
82    const Target* target,
83    const std::vector<T>& (ConfigValues::* getter)() const,
84    const Writer& writer,
85    std::ostream& out) {
86  for (ConfigValuesIterator iter(target); !iter.done(); iter.Next())
87    ConfigValuesToStream(iter.cur(), getter, writer, out);
88}
89
90// Writes the values out as strings with no transformation.
91void RecursiveTargetConfigStringsToStream(
92    const Target* target,
93    const std::vector<std::string>& (ConfigValues::* getter)() const,
94    const EscapeOptions& escape_options,
95    std::ostream& out);
96
97#endif  // TOOLS_GN_CONFIG_VALUES_EXTRACTORS_H_
98