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.
5 #ifndef TOOLS_GN_SOURCE_FILE_H_
6 #define TOOLS_GN_SOURCE_FILE_H_
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"
17 // Represents a file within the source tree. Always begins in a slash, never
23 // Takes a known absolute source file. Always begins in a slash.
24 explicit SourceFile(const base::StringPiece
& p
);
28 bool is_null() const { return value_
.empty(); }
29 const std::string
& value() const { return value_
; }
31 // Returns everythign after the last slash.
32 std::string
GetName() const;
33 SourceDir
GetDir() const;
35 // Resolves this source file relative to some given source root. Returns
36 // an empty file path on error.
37 base::FilePath
Resolve(const base::FilePath
& source_root
) const;
39 // Returns true if this file starts with a "//" which indicates a path
40 // from the source root.
41 bool is_source_absolute() const {
42 return value_
.size() >= 2 && value_
[0] == '/' && value_
[1] == '/';
45 // Returns true if this file starts with a single slash which indicates a
46 // system-absolute path.
47 bool is_system_absolute() const {
48 return !is_source_absolute();
51 // Returns a source-absolute path starting with only one slash at the
52 // beginning (normally source-absolute paths start with two slashes to mark
53 // them as such). This is normally used when concatenating names together.
55 // This function asserts that the file is actually source-absolute. The
56 // return value points into our buffer.
57 base::StringPiece
SourceAbsoluteWithOneSlash() const {
58 CHECK(is_source_absolute());
59 return base::StringPiece(&value_
[1], value_
.size() - 1);
62 bool operator==(const SourceFile
& other
) const {
63 return value_
== other
.value_
;
65 bool operator!=(const SourceFile
& other
) const {
66 return !operator==(other
);
68 bool operator<(const SourceFile
& other
) const {
69 return value_
< other
.value_
;
73 friend class SourceDir
;
77 // Copy & assign supported.
80 namespace BASE_HASH_NAMESPACE
{
82 #if defined(COMPILER_GCC)
83 template<> struct hash
<SourceFile
> {
84 std::size_t operator()(const SourceFile
& v
) const {
89 #elif defined(COMPILER_MSVC)
90 inline size_t hash_value(const SourceFile
& v
) {
91 return hash_value(v
.value());
95 } // namespace BASE_HASH_NAMESPACE
97 #endif // TOOLS_GN_SOURCE_FILE_H_