- add sources.
[platform/framework/web/crosswalk.git] / src / tools / gn / source_dir.h
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_SOURCE_DIR_H_
6 #define TOOLS_GN_SOURCE_DIR_H_
7
8 #include <string>
9
10 #include "base/containers/hash_tables.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/strings/string_piece.h"
14
15 class SourceFile;
16
17 // Represents a directory within the source tree. Source dirs begin and end in
18 // slashes.
19 //
20 // If there is one slash at the beginning, it will mean a system-absolute file
21 // path. On Windows, absolute system paths will be of the form "/C:/foo/bar".
22 //
23 // Two slashes at the beginning indicate a path relative to the source root.
24 class SourceDir {
25  public:
26   SourceDir();
27   explicit SourceDir(const base::StringPiece& p);
28   ~SourceDir();
29
30   // Resolves a file or dir name relative to this source directory. Will return
31   // an empty SourceDir/File on error. Empty input is always an error (it's
32   // possible we should say ResolveRelativeDir vs. an empty string should be
33   // the source dir, but we require "." instead).
34   //
35   // If source_root is supplied, these functions will additionally handle the
36   // case where the input is a system-absolute but still inside the source
37   // tree. This is the case for some external tools.
38   SourceFile ResolveRelativeFile(
39       const base::StringPiece& p,
40       const base::StringPiece& source_root = base::StringPiece()) const;
41   SourceDir ResolveRelativeDir(
42       const base::StringPiece& p,
43       const base::StringPiece& source_root = base::StringPiece()) const;
44
45   // Resolves this source file relative to some given source root. Returns
46   // an empty file path on error.
47   base::FilePath Resolve(const base::FilePath& source_root) const;
48
49   bool is_null() const { return value_.empty(); }
50   const std::string& value() const { return value_; }
51
52   // Returns true if this path starts with a "//" which indicates a path
53   // from the source root.
54   bool is_source_absolute() const {
55     return value_.size() >= 2 && value_[0] == '/' && value_[1] == '/';
56   }
57
58   // Returns true if this path starts with a single slash which indicates a
59   // system-absolute path.
60   bool is_system_absolute() const {
61     return !is_source_absolute();
62   }
63
64   // Returns a source-absolute path starting with only one slash at the
65   // beginning (normally source-absolute paths start with two slashes to mark
66   // them as such). This is normally used when concatenating directories
67   // together.
68   //
69   // This function asserts that the directory is actually source-absolute. The
70   // return value points into our buffer.
71   base::StringPiece SourceAbsoluteWithOneSlash() const {
72     CHECK(is_source_absolute());
73     return base::StringPiece(&value_[1], value_.size() - 1);
74   }
75
76   void SwapInValue(std::string* v);
77
78   bool operator==(const SourceDir& other) const {
79     return value_ == other.value_;
80   }
81   bool operator!=(const SourceDir& other) const {
82     return !operator==(other);
83   }
84   bool operator<(const SourceDir& other) const {
85     return value_ < other.value_;
86   }
87
88  private:
89   friend class SourceFile;
90   std::string value_;
91
92   // Copy & assign supported.
93 };
94
95 namespace BASE_HASH_NAMESPACE {
96
97 #if defined(COMPILER_GCC)
98 template<> struct hash<SourceDir> {
99   std::size_t operator()(const SourceDir& v) const {
100     hash<std::string> h;
101     return h(v.value());
102   }
103 };
104 #elif defined(COMPILER_MSVC)
105 inline size_t hash_value(const SourceDir& v) {
106   return hash_value(v.value());
107 }
108 #endif  // COMPILER...
109
110 }  // namespace BASE_HASH_NAMESPACE
111
112 #endif  // TOOLS_GN_SOURCE_DIR_H_