aboutsummaryrefslogtreecommitdiff
path: root/build2/name
blob: 68fe9c7c4fd881a55cbf7bd11407ff4f9df9e68f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// file      : build2/name -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

// Note: include <build2/types> instead of this file directly.
//

#ifndef BUILD2_NAME
#define BUILD2_NAME

// We cannot include <build2/utility> since it includes <build2/types>.
//
#include <utility> // move()

namespace build2
{
  using std::move;

  // A name is what we operate on by default. Depending on the context,
  // it can be interpreted as a target or prerequisite name. A name
  // without a type and directory can be used to represent any text.
  // A name with directory and empty value represents a directory.
  // A name may also be project-qualified. If the project name is
  // empty, then it means the name is in a project other than our
  // own (e.g., it is installed).
  //
  // If pair is not '\0', then this name and the next in the list
  // form a pair.
  //
  struct name
  {
    const string* proj = nullptr; // Points to project_name_pool.
    dir_path dir;
    string type;
    string value;
    char pair = '\0'; // Pair character if first half of a pair. Can be used
                      // as bool.

    name () = default;

    explicit name (string v): value (move (v)) {}
    name& operator= (string v) {return *this = name (move (v));}

    explicit name (dir_path d): dir (move (d)) {}
    name& operator= (dir_path d) {return *this = name (move (d));}

    name (string t, string v): type (move (t)), value (move (v)) {}

    name (dir_path d, string t, string v)
        : dir (move (d)), type (move (t)), value (move (v)) {}

    // The first argument should be from project_name_pool.
    //
    name (const string* p, dir_path d, string t, string v)
        : proj (p), dir (move (d)), type (move (t)), value (move (v)) {}

    bool
    qualified () const {return proj != nullptr;}

    bool
    unqualified () const {return proj == nullptr;}

    bool
    typed () const {return !type.empty ();}

    bool
    untyped () const {return type.empty ();}

    bool
    empty () const {return dir.empty () && value.empty ();}

    // Note that strictly speaking the following tests should be orthogonal
    // to qualification. However, the vast majority of cases where we expect
    // a simple or directory name, we also expect it to be unqualified.
    //
    // Note also that empty name is simple but not a directory.
    //
    bool
    simple (bool ignore_qual = false) const
    {
      return (ignore_qual || unqualified ()) && untyped () && dir.empty ();
    }

    bool
    directory (bool ignore_qual = false) const
    {
      return (ignore_qual || unqualified ()) &&
        untyped () && !dir.empty () && value.empty ();
    }

    int
    compare (const name&) const;

    // The result is an unqualified, simple empty name.
    //
    void
    clear ();
  };

  inline bool
  operator== (const name& x, const name& y) {return x.compare (y) == 0;}

  inline bool
  operator!= (const name& x, const name& y) {return !(x == y);}

  inline bool
  operator< (const name& x, const name& y) {return x.compare (y) < 0;}

  ostream&
  operator<< (ostream&, const name&);

  // Vector of names.
  //
  using names = vector<name>;
  using names_view = vector_view<const name>;

  ostream&
  operator<< (ostream&, const names_view&);

  inline ostream&
  operator<< (ostream& os, const names& n) {return os << names_view (n);}
}

#include <build2/name.ixx>

#endif // BUILD2_NAME