1d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// found in the LICENSE file.
4d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
5d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#ifndef TOOLS_GN_CONFIG_VALUES_EXTRACTORS_H_
6d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#define TOOLS_GN_CONFIG_VALUES_EXTRACTORS_H_
7d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
8d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include <ostream>
9d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include <string>
10d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include <vector>
11d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
12d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/config.h"
13d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/config_values.h"
14d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/target.h"
15d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)struct EscapeOptions;
173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Provides a way to iterate through all ConfigValues applying to a given
1958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// target. This is more complicated than normal because the target has a list
2058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// of configs applying to it, and also config values on the target itself.
2158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
2258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// This iterator allows one to iterate through all of these in a defined order
2358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// in one convenient loop. The order is defined to be the ConfigValues on the
2458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// target itself first, then the applying configs, in order.
2558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
2658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Example:
2758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//   for (ConfigValueIterator iter(target); !iter.done(); iter.Next())
2858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//     DoSomething(iter->cur());
2958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class ConfigValuesIterator {
3058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) public:
3158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  explicit ConfigValuesIterator(const Target* target)
3258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      : target_(target),
3358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        cur_index_(-1) {
3458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
3558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool done() const {
3758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return cur_index_ >= static_cast<int>(target_->configs().size());
3858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const ConfigValues& cur() const {
4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (cur_index_ == -1)
4258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      return target_->config_values();
430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return target_->configs()[cur_index_].ptr->config_values();
440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Returns the origin of who added this config, if any. This will alwsya be
470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // null for the config values of a target itself.
480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  const ParseNode* origin() const {
490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (cur_index_ == -1)
500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      return NULL;
510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return target_->configs()[cur_index_].origin;
5258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
5358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void Next() {
5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    cur_index_++;
5658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Returns the config holding the current config values, or NULL for those
5958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // config values associated with the target itself.
6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const Config* GetCurrentConfig() const {
6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (cur_index_ == -1)
6258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      return NULL;
630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return target_->configs()[cur_index_].ptr;
6458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
6558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) private:
6758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const Target* target_;
6858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Represents an index into the target_'s configs() or, when -1, the config
7058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // values on the target itself.
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  int cur_index_;
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)};
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
74d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochtemplate<typename T, class Writer>
75d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochinline void ConfigValuesToStream(
76d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    const ConfigValues& values,
77d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    const std::vector<T>& (ConfigValues::* getter)() const,
78d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    const Writer& writer,
79d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    std::ostream& out) {
80d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  const std::vector<T>& v = (values.*getter)();
81d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  for (size_t i = 0; i < v.size(); i++)
82d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    writer(v[i], out);
83d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch};
84d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
852385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch// Writes a given config value that applies to a given target. This collects
862385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch// all values from the target itself and all configs that apply, and writes
872385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch// then in order.
88d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochtemplate<typename T, class Writer>
89d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochinline void RecursiveTargetConfigToStream(
90d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    const Target* target,
91d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    const std::vector<T>& (ConfigValues::* getter)() const,
92d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    const Writer& writer,
93d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    std::ostream& out) {
9458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (ConfigValuesIterator iter(target); !iter.done(); iter.Next())
9558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ConfigValuesToStream(iter.cur(), getter, writer, out);
96d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch}
97d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
98d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Writes the values out as strings with no transformation.
99d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochvoid RecursiveTargetConfigStringsToStream(
100d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    const Target* target,
101d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    const std::vector<std::string>& (ConfigValues::* getter)() const,
1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const EscapeOptions& escape_options,
103d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    std::ostream& out);
104d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
105d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#endif  // TOOLS_GN_CONFIG_VALUES_EXTRACTORS_H_
106