config_values_extractors.h revision 2385ea399aae016c0806a4f9ef3c9cfe3d2a39df
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
16template<typename T, class Writer>
17inline void ConfigValuesToStream(
18    const ConfigValues& values,
19    const std::vector<T>& (ConfigValues::* getter)() const,
20    const Writer& writer,
21    std::ostream& out) {
22  const std::vector<T>& v = (values.*getter)();
23  for (size_t i = 0; i < v.size(); i++)
24    writer(v[i], out);
25};
26
27// Writes a given config value that applies to a given target. This collects
28// all values from the target itself and all configs that apply, and writes
29// then in order.
30template<typename T, class Writer>
31inline void RecursiveTargetConfigToStream(
32    const Target* target,
33    const std::vector<T>& (ConfigValues::* getter)() const,
34    const Writer& writer,
35    std::ostream& out) {
36  // Note: if you make any changes to this, also change the writer in the
37  // implementation of the "desc" command.
38
39  // First write the values from the config itself.
40  ConfigValuesToStream(target->config_values(), getter, writer, out);
41
42  // Then write the configs in order.
43  for (size_t i = 0; i < target->configs().size(); i++) {
44    ConfigValuesToStream(target->configs()[i]->config_values(), getter,
45                         writer, out);
46  }
47}
48
49// Writes the values out as strings with no transformation.
50void RecursiveTargetConfigStringsToStream(
51    const Target* target,
52    const std::vector<std::string>& (ConfigValues::* getter)() const,
53    std::ostream& out);
54
55#endif  // TOOLS_GN_CONFIG_VALUES_EXTRACTORS_H_
56