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_VALUE_EXTRACTORS_H_
6#define TOOLS_GN_VALUE_EXTRACTORS_H_
7
8#include <string>
9#include <vector>
10
11#include "tools/gn/target.h"
12#include "tools/gn/value.h"
13
14class BuildSettings;
15class Err;
16class Label;
17class SourceDir;
18class SourceFile;
19
20// Sets the error and returns false on failure.
21template<typename T, class Converter>
22bool ListValueExtractor(const Value& value, std::vector<T>* dest,
23                        Err* err,
24                        const Converter& converter) {
25  if (!value.VerifyTypeIs(Value::LIST, err))
26    return false;
27  const std::vector<Value>& input_list = value.list_value();
28  dest->resize(input_list.size());
29  for (size_t i = 0; i < input_list.size(); i++) {
30    if (!converter(input_list[i], &(*dest)[i], err))
31      return false;
32  }
33  return true;
34}
35
36// On failure, returns false and sets the error.
37bool ExtractListOfStringValues(const Value& value,
38                               std::vector<std::string>* dest,
39                               Err* err);
40
41// Looks for a list of source files relative to a given current dir.
42bool ExtractListOfRelativeFiles(const BuildSettings* build_settings,
43                                const Value& value,
44                                const SourceDir& current_dir,
45                                std::vector<SourceFile>* files,
46                                Err* err);
47
48// Looks for a list of source directories relative to a given current dir.
49bool ExtractListOfRelativeDirs(const BuildSettings* build_settings,
50                               const Value& value,
51                               const SourceDir& current_dir,
52                               std::vector<SourceDir>* dest,
53                               Err* err);
54
55// Extracts the list of labels and their origins to the given vector. Only the
56// labels are filled in, the ptr for each pair in the vector will be null.
57bool ExtractListOfLabels(const Value& value,
58                         const SourceDir& current_dir,
59                         const Label& current_toolchain,
60                         LabelConfigVector* dest,
61                         Err* err);
62bool ExtractListOfLabels(const Value& value,
63                         const SourceDir& current_dir,
64                         const Label& current_toolchain,
65                         LabelTargetVector* dest,
66                         Err* err);
67
68bool ExtractRelativeFile(const BuildSettings* build_settings,
69                         const Value& value,
70                         const SourceDir& current_dir,
71                         SourceFile* file,
72                         Err* err);
73
74#endif  // TOOLS_GN_VALUE_EXTRACTORS_H_
75