Debian Codegraph @

src/Base/ProgramVersion.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 **************************************************************************/
23
24 #ifndef BASE_PROGRAM_VERSION_H
25 #define BASE_PROGRAM_VERSION_H
26
27 #include <algorithm>
28 #include <cstdint>
29 #include <string_view>
30 #include <FCGlobal.h>
31
32 namespace Base
33 {
34
35 enum class Version : std::uint8_t
36 {
37 v0_1x,
38 v0_16,
39 v0_17,
40 v0_18,
41 v0_19,
42 v0_20,
43 v0_21,
44 v0_22,
45 v1_0,
46 v1_1,
47 v1_x,
48 };
49
50 inline Version getVersion(std::string_view str)
51 {
52 // clang-format off
53 struct VersionItem
54 {
55 std::string_view name;
56 Version version;
57 };
58 static const std::initializer_list<VersionItem> items = {
59 {.name="0.16", .version=Version::v0_16},
60 {.name="0.17", .version=Version::v0_17},
61 {.name="0.18", .version=Version::v0_18},
62 {.name="0.19", .version=Version::v0_19},
63 {.name="0.20", .version=Version::v0_20},
64 {.name="0.21", .version=Version::v0_21},
65 {.name="0.22", .version=Version::v0_22},
66 {.name="1.0" , .version=Version::v1_0 },
67 {.name="1.1" , .version=Version::v1_1 },
68 };
69 // clang-format on
70 auto it = std::ranges::find_if(items, [str](const auto& item) {
71 return str.compare(0, item.name.size(), item.name) == 0;
72 });
73 if (it != items.end()) {
74 return it->version;
75 }
76 if (!str.empty() && str[0] == '0') {
77 return Version::v0_1x;
78 }
79 return Version::v1_x;
80 }
81
82 } // namespace Base
83
84 #endif // BASE_PROGRAM_VERSION_H