Home | History | Annotate | Download | only in gn
      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_FILE_TEMPLATE_H_
      6 #define TOOLS_GN_FILE_TEMPLATE_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/containers/stack_container.h"
     10 #include "tools/gn/err.h"
     11 #include "tools/gn/value.h"
     12 
     13 class ParseNode;
     14 
     15 class FileTemplate {
     16  public:
     17   struct Subrange {
     18     enum Type {
     19       LITERAL = 0,
     20       SOURCE,
     21       NAME_PART,
     22       NUM_TYPES  // Must be last
     23     };
     24     Subrange(Type t, const std::string& l = std::string())
     25         : type(t),
     26           literal(l) {
     27     }
     28 
     29     Type type;
     30 
     31     // When type_ == LITERAL, this specifies the literal.
     32     std::string literal;
     33   };
     34 
     35   // Constructs a template from the given value. On error, the err will be
     36   // set. In this case you should not use this object.
     37   FileTemplate(const Value& t, Err* err);
     38   FileTemplate(const std::vector<std::string>& t);
     39   ~FileTemplate();
     40 
     41   // Applies this template to the given list of sources, appending all
     42   // results to the given dest list. The sources must be a list for the
     43   // one that takes a value as an input, otherwise the given error will be set.
     44   void Apply(const Value& sources,
     45              const ParseNode* origin,
     46              std::vector<Value>* dest,
     47              Err* err) const;
     48   void ApplyString(const std::string& input,
     49                    std::vector<std::string>* output) const;
     50 
     51   // Known template types.
     52   static const char kSource[];
     53   static const char kSourceNamePart[];
     54 
     55  private:
     56   typedef base::StackVector<Subrange, 8> Template;
     57   typedef base::StackVector<Template, 8> TemplateVector;
     58 
     59   void ParseInput(const Value& value, Err* err);
     60 
     61   // Parses a template string and adds it to the templates_ list.
     62   void ParseOneTemplateString(const std::string& str);
     63 
     64   TemplateVector templates_;
     65 
     66   // The corresponding value is set to true if the given subrange type is
     67   // required. This allows us to precompute these types whem applying them
     68   // to a given source file.
     69   bool types_required_[Subrange::NUM_TYPES];
     70 
     71   DISALLOW_COPY_AND_ASSIGN(FileTemplate);
     72 };
     73 
     74 #endif  // TOOLS_GN_FILE_TEMPLATE_H_
     75