HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
application_metadata.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "semantic_version.hpp"
8#include "../algorithm/module.hpp"
9#include "../macros.hpp"
10#include <string>
11#include <stdexcept>
12
13hi_export_module(hikogui.metadata.metadata_application);
14
15hi_export namespace hi::inline v1 {
16
17inline std::optional<std::string> _application_name = std::nullopt;
18inline std::optional<std::string> _application_slug = std::nullopt;
19inline std::optional<std::string> _application_vendor = std::nullopt;
20inline std::optional<semantic_version> _application_version = std::nullopt;
21
22[[nodiscard]] inline std::string const& get_application_name()
23{
24 if (_application_name) {
25 return *_application_name;
26 } else {
27 throw std::logic_error("set_application_name() should be called at application startup.");
28 }
29}
30
31[[nodiscard]] inline std::string const& get_application_slug()
32{
33 if (_application_slug) {
34 return *_application_slug;
35 } else {
36 throw std::logic_error("set_application_name() should be called at application startup.");
37 }
38}
39
40[[nodiscard]] inline std::string const& get_application_vendor()
41{
42 if (_application_vendor) {
43 return *_application_vendor;
44 } else {
45 throw std::logic_error("set_application_vendor() should be called at application startup.");
46 }
47}
48
49[[nodiscard]] inline semantic_version const& get_application_version()
50{
51 if (_application_version) {
52 return *_application_version;
53 } else {
54 throw std::logic_error("set_application_version() should be called at application startup.");
55 }
56}
57
58inline void set_application_name(std::string_view name, std::string_view slug)
59{
60 if (name.empty()) {
61 throw std::invalid_argument("application name must not be empty.");
62 }
63 if (name.contains('/') or name.contains('\\')) {
64 throw std::invalid_argument("application name must not contain a slash or backslash.");
65 }
66 if (slug.empty()) {
67 throw std::invalid_argument("application slug must not be empty.");
68 }
69 if (not is_slug(slug)) {
70 throw std::invalid_argument("application slug must contain only 'a'-'z' '0'-'9' and '-' characters.");
71 }
72
73 _application_name = name;
74 _application_slug = slug;
75}
76
77inline void set_application_name(std::string_view name)
78{
79 return set_application_name(name, make_slug(name));
80}
81
82inline void set_application_vendor(std::string_view name)
83{
84 if (name.empty()) {
85 throw std::invalid_argument("vendor name must not be empty.");
86 }
87 if (name.contains('/') or name.contains('\\')) {
88 throw std::invalid_argument("vendor name must not contain a slash or backslash.");
89 }
90
91 _application_vendor = name;
92}
93
94inline void set_application_version(semantic_version version) noexcept
95{
96 _application_version = version;
97}
98
99inline void set_application_version(int major, int minor = 0, int patch = 0) noexcept
100{
101 return set_application_version(semantic_version{major, minor, patch});
102}
103
104} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr std::string make_slug(std::string_view str) noexcept
Create a slug from a string.
Definition strings.hpp:254
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377