aboutsummaryrefslogtreecommitdiff
path: root/libbutl/standard-version.ixx
blob: 6142586fddd8b75b61ff16de82270536ce987067 (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
// file      : libbutl/standard-version.ixx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

namespace butl
{
  inline std::uint16_t standard_version::
  major () const noexcept
  {
    std::uint64_t e (version % 10);
    std::uint64_t v (version / 10);
    std::uint64_t ab (v % 1000);
    if (ab != 0 || e == 1)
      v += 1000 - ab;

    return static_cast<std::uint16_t> (v / 1000000000 % 1000);
  }

  inline std::uint16_t standard_version::
  minor () const noexcept
  {
    std::uint64_t e (version % 10);
    std::uint64_t v (version / 10);
    std::uint64_t ab (v % 1000);
    if (ab != 0 || e == 1)
      v += 1000 - ab;

    return static_cast<std::uint16_t> (v / 1000000 % 1000);
  }

  inline std::uint16_t standard_version::
  patch () const noexcept
  {
    std::uint64_t e (version % 10);
    std::uint64_t v (version / 10);
    std::uint64_t ab (v % 1000);
    if (ab != 0 || e == 1)
      v += 1000 - ab;

    return static_cast<std::uint16_t> (v / 1000 % 1000);
  }

  inline bool standard_version::
  release () const noexcept
  {
    return version % 10000 == 0;
  }

  inline optional<std::uint16_t> standard_version::
  pre_release () const noexcept
  {
    return release () || stub ()
      ? nullopt
      : optional<std::uint16_t> (
          static_cast<std::uint16_t> (version / 10 % 1000));
  }

  inline optional<std::uint16_t> standard_version::
  alpha () const noexcept
  {
    optional<std::uint16_t> pr (pre_release ());
    return pr && *pr < 500 ? pr : nullopt;
  }

  inline optional<std::uint16_t> standard_version::
  beta () const noexcept
  {
    optional<std::uint16_t> pr (pre_release ());
    return pr && *pr >= 500 ? optional<std::uint16_t> (*pr - 500) : nullopt;
  }

  inline bool standard_version::
  final () const noexcept
  {
    return release () || !(snapshot () || stub ());
  }

  inline bool standard_version::
  earliest () const noexcept
  {
    return version % 10000 == 1 && !snapshot () && !stub ();
  }

  inline bool standard_version::
  latest_snapshot () const noexcept
  {
    return snapshot () && snapshot_sn == latest_sn;
  }

  // Note: in the following constructors we subtract one from AAABBBCCC if
  // DDDE is not zero (see standard-version.hxx for details).
  //
  inline standard_version::
  standard_version (std::uint16_t ep,
                    std::uint16_t mj,
                    std::uint16_t mi,
                    std::uint16_t pa,
                    std::uint16_t pr,
                    std::uint16_t rv)
      : standard_version (ep,
                          //  AAABBBCCCDDDE
                          (mj * 10000000000ULL +
                           mi *    10000000ULL +
                           pa *       10000ULL +
                           pr *          10ULL -
                           (pr != 0 ? 10000ULL : 0ULL)),
                          "" /* snapshot */,
                          rv)
  {
  }

  inline standard_version::
  standard_version (std::uint16_t ep,
                    std::uint16_t mj,
                    std::uint16_t mi,
                    std::uint16_t pa,
                    std::uint16_t pr,
                    std::uint64_t sn,
                    std::string si,
                    std::uint16_t rv)
      : standard_version (ep,
                          //  AAABBBCCCDDDE
                          (mj * 10000000000ULL                +
                           mi *    10000000ULL                +
                           pa *       10000ULL                +
                           pr *          10ULL                +
                                          1ULL /* snapshot */ -
                                      10000ULL),
                          sn,
                          si,
                          rv)
  {
  }

  inline standard_version::flags
  operator& (standard_version::flags x, standard_version::flags y)
  {
    return x &= y;
  }

  inline standard_version::flags
  operator| (standard_version::flags x, standard_version::flags y)
  {
    return x |= y;
  }

  inline standard_version::flags
  operator&= (standard_version::flags& x, standard_version::flags y)
  {
    return x = static_cast<standard_version::flags> (
      static_cast<std::uint16_t> (x) &
      static_cast<std::uint16_t> (y));
  }

  inline standard_version::flags
  operator|= (standard_version::flags& x, standard_version::flags y)
  {
    return x = static_cast<standard_version::flags> (
      static_cast<std::uint16_t> (x) |
      static_cast<std::uint16_t> (y));
  }
}