dasmig-core 1.2.0
Shared random engine, distributions and data tiers for the dasmig generator libraries
Loading...
Searching...
No Matches
sex.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2020-2026 Diego Dasso Migotto
3
4#ifndef DASMIG_CORE_SEX_HPP
5#define DASMIG_CORE_SEX_HPP
6
7#include <cstdint>
8#include <optional>
9#include <string_view>
10
11/// @file sex.hpp
12/// @brief The sex enum shared by every generator library, and its text form.
13/// @author Diego Dasso Migotto (diegomigotto at hotmail dot com)
14
15// Libraries released before the core define their own copy of the enum behind
16// this guard; defining it here makes them skip it when included together.
17#ifndef DASMIG_SEX_DEFINED
18#define DASMIG_SEX_DEFINED
19
20namespace dasmig
21{
22
23/// @brief Biological sex.
24enum class sex : std::uint8_t
25{
26 male, ///< Male.
27 female ///< Female.
28};
29
30} // namespace dasmig
31
32#endif // DASMIG_SEX_DEFINED
33
34namespace dasmig::core
35{
36
37/// @brief The lower-case name of a sex, as written in data files and JSON.
38/// @param s Sex.
39/// @return `"male"` or `"female"`; `"unknown"` for a value outside the enum.
40[[nodiscard]] constexpr std::string_view sex_name(sex s) noexcept
41{
42 switch (s)
43 {
44 case sex::male: return "male";
45 case sex::female: return "female";
46 }
47 return "unknown";
48}
49
50/// @brief Reads the name written by sex_name().
51///
52/// Exact and case-sensitive: only `"male"` and `"female"` are accepted.
53///
54/// @param text Text to read.
55/// @return The sex, or an empty optional for any other text.
56[[nodiscard]] constexpr std::optional<sex> parse_sex(std::string_view text) noexcept
57{
58 if (text == "male") return sex::male;
59 if (text == "female") return sex::female;
60 return std::nullopt;
61}
62
63} // namespace dasmig::core
64
65#endif // DASMIG_CORE_SEX_HPP
constexpr std::optional< sex > parse_sex(std::string_view text) noexcept
Reads the name written by sex_name().
Definition sex.hpp:56
sex
Biological sex.
Definition sex.hpp:25
@ female
Female.
constexpr std::string_view sex_name(sex s) noexcept
The lower-case name of a sex, as written in data files and JSON.
Definition sex.hpp:40