aboutsummaryrefslogtreecommitdiff
path: root/libbutl/standard-version.mxx
blob: fa74e0132d408d261937dc7eaa69edf3978e3222 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// file      : libbutl/standard-version.mxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules
#pragma once
#endif

// C includes.

#ifndef __cpp_lib_modules
#include <string>
#include <cstdint> // uint*_t
#include <cstddef> // size_t
#include <ostream>
#endif

// Other includes.

#ifdef __cpp_modules
export module butl.standard_version;
#ifdef __cpp_lib_modules
import std.core;
import std.io;
#endif
import butl.optional;
#else
#include <libbutl/optional.mxx>
#endif

#include <libbutl/export.hxx>

// FreeBSD defines these macros in its <sys/types.h>.
//
#ifdef major
#  undef major
#endif

#ifdef minor
#  undef minor
#endif

LIBBUTL_MODEXPORT namespace butl
{
  // The build2 "standard version" (specific, earliest and stub):
  //
  // [+<epoch>-]<maj>.<min>.<patch>[-(a|b).<num>[.<snapsn>[.<snapid>]]][+<rev>]
  // [+<epoch>-]<maj>.<min>.<patch>-
  // 0[+<revision>]
  //
  struct LIBBUTL_SYMEXPORT standard_version
  {
    // Invariants:
    //
    // 1. allow_earliest
    //    ? (E == 1) || (snapshot_sn == 0)
    //    : (E == 0) == (snapshot_sn == 0)
    //
    // 2. version != 0 || allow_stub && epoch == 0 && snapshot_sn == 0
    //
    // 3. snapshot_sn != latest_sn && snapshot_sn != 0 || snapshot_id.empty ()
    //
    static const std::uint64_t latest_sn = std::uint64_t (~0);

    std::uint16_t epoch        = 0;  // 0 if not specified.
    std::uint64_t version      = 0;  // AAABBBCCCDDDE
    std::uint64_t snapshot_sn  = 0;  // 0 if not specifed, latest_sn if 'z'.
    std::string   snapshot_id;       // Empty if not specified.
    std::uint16_t revision     = 0;  // 0 if not specified.

    std::uint16_t major () const noexcept;
    std::uint16_t minor () const noexcept;
    std::uint16_t patch () const noexcept;

    // Note: 0 is ambiguous (-a.0.z).
    //
    std::uint16_t pre_release () const noexcept;

    // Note: return empty if the corresponding component is unspecified.
    //
    std::string string () const;             // Package version.
    std::string string_project () const;     // Project version (no epoch/rev).
    std::string string_project_id () const;  // Project version id (no snapsn).
    std::string string_version () const;     // Version only (no snapshot).
    std::string string_pre_release () const; // Pre-release part only (a.1).
    std::string string_snapshot () const;    // Snapshot part only (1234.1f23).

    bool empty () const noexcept {return version == 0;}

    bool alpha () const noexcept;
    bool beta () const noexcept;
    bool snapshot () const noexcept {return snapshot_sn != 0;}

    // Represented by DDDE in version being 0001 and snapshot_sn being 0.
    //
    // Note that the earliest version is a final alpha pre-release.
    //
    bool
    earliest () const noexcept;

    bool
    stub () const noexcept {return version == std::uint64_t (~0);}

    // Comparison of empty or stub versions doesn't make sense.
    //
    int
    compare (const standard_version& v) const noexcept
    {
      if (epoch != v.epoch)
        return epoch < v.epoch ? -1 : 1;

      if (version != v.version)
        return version < v.version ? -1 : 1;

      if (snapshot_sn != v.snapshot_sn)
        return snapshot_sn < v.snapshot_sn ? -1 : 1;

      if (revision != v.revision)
        return revision < v.revision ? -1 : 1;

      return 0;
    }

    // Parse the version. Throw std::invalid_argument if the format is not
    // recognizable or components are invalid.
    //
    enum flags
    {
      none           = 0,
      allow_earliest = 0x01, // Allow <major>.<minor>.<patch>- form.
      allow_stub     = 0x02  // Allow 0[+<revision>] form.
    };

    explicit
    standard_version (const std::string&, flags = none);

    explicit
    standard_version (std::uint64_t version, flags = none);

    standard_version (std::uint64_t version,
                      const std::string& snapshot,
                      flags = none);

    standard_version (std::uint16_t epoch,
                      std::uint64_t version,
                      const std::string& snapshot,
                      std::uint16_t revision,
                      flags = none);

    standard_version (std::uint16_t epoch,
                      std::uint64_t version,
                      std::uint64_t snapshot_sn,
                      std::string snapshot_id,
                      std::uint16_t revision,
                      flags = none);

    // Create empty version.
    //
    standard_version () {} // = default; @@ MOD VC
  };

  // Try to parse a string as a standard version returning nullopt if invalid.
  //
  LIBBUTL_SYMEXPORT optional<standard_version>
  parse_standard_version (const std::string&,
                          standard_version::flags = standard_version::none);

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

  inline bool
  operator> (const standard_version& x, const standard_version& y) noexcept
  {
    return x.compare (y) > 0;
  }

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

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

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

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

  inline std::ostream&
  operator<< (std::ostream& o, const standard_version& x)
  {
    return o << x.string ();
  }

  inline standard_version::flags
  operator& (standard_version::flags, standard_version::flags);

  inline standard_version::flags
  operator| (standard_version::flags, standard_version::flags);

  inline standard_version::flags
  operator&= (standard_version::flags&, standard_version::flags);

  inline standard_version::flags
  operator|= (standard_version::flags&, standard_version::flags);

  // The build2 "standard version" constraint:
  //
  // ('==' | '>' | '<' | '>=' | '<=') <version>
  // ('^' | '~') <version>
  // ('(' | '[') <version> <version> (')' | ']')
  //
  struct LIBBUTL_SYMEXPORT standard_version_constraint
  {
    butl::optional<standard_version> min_version;
    butl::optional<standard_version> max_version;
    bool min_open;
    bool max_open;

    // Parse the version constraint. Throw std::invalid_argument on error.
    //
    explicit
    standard_version_constraint (const std::string&);

    // Throw std::invalid_argument if the specified version range is invalid.
    //
    standard_version_constraint (
      butl::optional<standard_version> min_version, bool min_open,
      butl::optional<standard_version> max_version, bool max_open);

    explicit
    standard_version_constraint (const standard_version& v)
        : standard_version_constraint (v, false, v, false) {}

    standard_version_constraint () = default;

    std::string
    string () const;

    bool
    empty () const noexcept {return !min_version && !max_version;}

    bool
    satisfies (const standard_version&) const noexcept;
  };

  inline bool
  operator== (const standard_version_constraint& x,
              const standard_version_constraint& y)
  {
    return x.min_version == y.min_version && x.max_version == y.max_version &&
      x.min_open == y.min_open && x.max_open == y.max_open;
  }

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

  inline std::ostream&
  operator<< (std::ostream& o, const standard_version_constraint& x)
  {
    return o << x.string ();
  }
}

#include <libbutl/standard-version.ixx>