Bash++
Bash++ compiler internal documentation
BashVersion.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 Andrew S. Rightenburg
3 * Bash++: Bash with classes
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7#pragma once
8
9#include <cstdint>
10#include <string>
11#include <stdexcept>
12#include <limits>
13
24 uint16_t major = 5;
25 uint16_t minor = 2;
26
27 constexpr operator uint32_t() const {
28 return (static_cast<uint32_t>(major) << 16) | static_cast<uint32_t>(minor);
29 }
30
31 constexpr bool operator>=(const BashVersion& other) const {
32 return static_cast<uint32_t>(*this) >= static_cast<uint32_t>(other);
33 }
34
35 constexpr bool operator<=(const BashVersion& other) const {
36 return static_cast<uint32_t>(*this) <= static_cast<uint32_t>(other);
37 }
38
39 constexpr bool operator>(const BashVersion& other) const {
40 return static_cast<uint32_t>(*this) > static_cast<uint32_t>(other);
41 }
42
43 constexpr bool operator<(const BashVersion& other) const {
44 return static_cast<uint32_t>(*this) < static_cast<uint32_t>(other);
45 }
46
47 constexpr bool operator==(const BashVersion& other) const {
48 return static_cast<uint32_t>(*this) == static_cast<uint32_t>(other);
49 }
50
51 constexpr bool operator!=(const BashVersion& other) const {
52 return static_cast<uint32_t>(*this) != static_cast<uint32_t>(other);
53 }
54
55 constexpr std::string to_string() const {
56 return std::to_string(major) + "." + std::to_string(minor);
57 }
58
59 constexpr operator std::string() const {
60 return to_string();
61 }
62
63 constexpr BashVersion() = default;
64 constexpr ~BashVersion() = default;
65 constexpr BashVersion(const BashVersion&) = default;
66 constexpr BashVersion& operator=(const BashVersion&) = default;
67 constexpr BashVersion(BashVersion&&) = default;
68 constexpr BashVersion& operator=(BashVersion&&) = default;
69
70 constexpr BashVersion(uint16_t major, uint16_t minor) : major(major), minor(minor) {}
71
80 explicit constexpr BashVersion(std::string_view version_string) {
81 uint16_t* current = &major;
82 uint16_t value = 0;
83
84 for (const auto& c : version_string) {
85 switch (c) {
86 case '0' ... '9':
87 // Verify we don't overflow the major or minor version
88 if (value > (std::numeric_limits<uint16_t>::max() - (c - '0')) / 10)
89 throw std::invalid_argument("Invalid Bash version: " + std::string(version_string));
90 value = (value * 10) + (c - '0');
91 break;
92 case '.':
93 if (current == &minor)
94 throw std::invalid_argument("Invalid Bash version: " + std::string(version_string));
95 *current = value;
96 current = &minor;
97 value = 0;
98 break;
99 default:
100 throw std::invalid_argument("Invalid Bash version: " + std::string(version_string));
101 }
102 }
103 *current = value;
104
105 // If we never encountered a dot, the entire string was the major version
106 if (current == &major) minor = 0;
107 }
108};
Represents a Bash version to target for code generation.
Definition BashVersion.h:23
constexpr BashVersion & operator=(const BashVersion &)=default
uint16_t minor
Definition BashVersion.h:25
constexpr BashVersion(BashVersion &&)=default
uint16_t major
Definition BashVersion.h:24
constexpr bool operator<=(const BashVersion &other) const
Definition BashVersion.h:35
constexpr ~BashVersion()=default
constexpr std::string to_string() const
Definition BashVersion.h:55
constexpr bool operator==(const BashVersion &other) const
Definition BashVersion.h:47
constexpr BashVersion & operator=(BashVersion &&)=default
constexpr bool operator<(const BashVersion &other) const
Definition BashVersion.h:43
constexpr bool operator!=(const BashVersion &other) const
Definition BashVersion.h:51
constexpr BashVersion(uint16_t major, uint16_t minor)
Definition BashVersion.h:70
constexpr bool operator>(const BashVersion &other) const
Definition BashVersion.h:39
constexpr BashVersion()=default
constexpr BashVersion(std::string_view version_string)
Constructs a BashVersion from a string in the format "major.minor" (e.g., "5.2")
Definition BashVersion.h:80
constexpr BashVersion(const BashVersion &)=default
constexpr bool operator>=(const BashVersion &other) const
Definition BashVersion.h:31