Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_cpp_compat_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
45#include <iostream>
46#include <limits>
47#include <string>
48
49#include <ah-cpp-compat.H>
50
51using Aleph::expected;
53
54namespace
55{
56
57// A parse-like operation: turn a string into a positive int, or explain why
58// it could not. No exceptions cross the boundary — the error is a value.
59expected<int, std::string> parse_positive(const std::string &text)
60{
61 if (text.empty())
62 return unexpected<std::string>("empty input");
63
64 int value = 0;
65 for (char c : text)
66 {
67 if (c < '0' or c > '9')
68 return unexpected<std::string>("not a number: '" + text + "'");
69
70 int digit = c - '0';
71 constexpr int max_val = std::numeric_limits<int>::max();
72 if (value > (max_val - digit) / 10)
73 return unexpected<std::string>("overflow: '" + text + "'");
74
75 value = value * 10 + digit;
76 }
77
78 if (value == 0)
79 return unexpected<std::string>("must be > 0");
80
81 return value;
82}
83
84void report(const std::string &input)
85{
86 auto r = parse_positive(input);
87 std::cout << " parse_positive(\"" << input << "\") -> ";
88 if (r) // explicit operator bool
89 std::cout << "value " << *r << '\n';
90 else
91 std::cout << "error: " << r.error() << '\n';
92}
93
94} // namespace
95
96int main()
97{
98 std::cout << "C++23 library facilities detected on this toolchain:\n"
99 << " std::expected = " << ALEPH_HAS_STD_EXPECTED << '\n'
100 << " std::generator = " << ALEPH_HAS_STD_GENERATOR << '\n'
101 << " std::flat_map = " << ALEPH_HAS_STD_FLAT_MAP << '\n'
102 << " std::mdspan = " << ALEPH_HAS_STD_MDSPAN << "\n\n";
103
104 std::cout << "Basic value / error reporting:\n";
105 report("42");
106 report("7x");
107 report("0");
108 report("");
109
110 std::cout << "\nvalue_or (supply a fallback instead of branching):\n";
111 std::cout << " parse_positive(\"100\").value_or(-1) = "
112 << parse_positive("100").value_or(-1) << '\n';
113 std::cout << " parse_positive(\"bad\").value_or(-1) = "
114 << parse_positive("bad").value_or(-1) << '\n';
115
116 std::cout << "\nMonadic chaining (transform / and_then / or_else):\n";
117
118 // transform: apply a pure function to the value, propagate errors untouched.
119 auto squared = parse_positive("9").transform([](int v) { return v * v; });
120 std::cout << " \"9\" |> transform(square) = "
121 << (squared ? std::to_string(*squared) : squared.error()) << '\n';
122
123 // and_then: chain another fallible step.
124 auto chained = parse_positive("5").and_then(
125 [](int v) -> expected<int, std::string>
126 {
127 if (v > 1000)
128 return unexpected<std::string>("too large");
129 return v + 100;
130 });
131 std::cout << " \"5\" |> and_then(+100 if <=1000) = "
132 << (chained ? std::to_string(*chained) : chained.error()) << '\n';
133
134 // or_else: recover from an error with a default.
135 auto recovered = parse_positive("oops").or_else(
136 [](const std::string &) -> expected<int, std::string> { return 0; });
137 std::cout << " \"oops\" |> or_else(0) = "
138 << (recovered ? std::to_string(*recovered) : recovered.error())
139 << '\n';
140
141 return 0;
142}
C++20/23 compatibility layer: feature detection + Aleph:: polyfills.
#define ALEPH_HAS_STD_FLAT_MAP
#define ALEPH_HAS_STD_EXPECTED
#define ALEPH_HAS_STD_GENERATOR
#define ALEPH_HAS_STD_MDSPAN
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Minimal std::expected-style result type for C++20.
Wrapper for the error value used to initialize an expected.
gsl_rng * r