HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
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 "../strings.hpp"
9#include <string>
10#include <stdexcept>
11
12namespace hi::inline v1 {
13
14inline std::optional<std::string> _application_name = std::nullopt;
15inline std::optional<std::string> _application_slug = std::nullopt;
16inline std::optional<std::string> _application_vendor = std::nullopt;
17inline std::optional<semantic_version> _application_version = std::nullopt;
18
19[[nodiscard]] inline std::string const& get_application_name()
20{
21 if (_application_name) {
22 return *_application_name;
23 } else {
24 throw std::logic_error("set_application_name() should be called at application startup.");
25 }
26}
27
28[[nodiscard]] inline std::string const& get_application_slug()
29{
30 if (_application_slug) {
31 return *_application_slug;
32 } else {
33 throw std::logic_error("set_application_name() should be called at application startup.");
34 }
35}
36
37[[nodiscard]] inline std::string const& get_application_vendor()
38{
39 if (_application_vendor) {
40 return *_application_vendor;
41 } else {
42 throw std::logic_error("set_application_vendor() should be called at application startup.");
43 }
44}
45
46[[nodiscard]] inline semantic_version const& get_application_version()
47{
48 if (_application_version) {
49 return *_application_version;
50 } else {
51 throw std::logic_error("set_application_version() should be called at application startup.");
52 }
53}
54
55inline void set_application_name(std::string_view name, std::string_view slug)
56{
57 if (name.empty()) {
58 throw std::invalid_argument("application name must not be empty.");
59 }
60 if (name.contains('/') or name.contains('\\')) {
61 throw std::invalid_argument("application name must not contain a slash or backslash.");
62 }
63 if (slug.empty()) {
64 throw std::invalid_argument("application slug must not be empty.");
65 }
66 if (not is_slug(slug)) {
67 throw std::invalid_argument("application slug must contain only 'a'-'z' '0'-'9' and '-' characters.");
68 }
69
70 _application_name = name;
71 _application_slug = slug;
72}
73
74inline void set_application_name(std::string_view name)
75{
76 return set_application_name(name, make_slug(name));
77}
78
79inline void set_application_vendor(std::string_view name)
80{
81 if (name.empty()) {
82 throw std::invalid_argument("vendor name must not be empty.");
83 }
84 if (name.contains('/') or name.contains('\\')) {
85 throw std::invalid_argument("vendor name must not contain a slash or backslash.");
86 }
87
88 _application_vendor = name;
89}
90
91inline void set_application_version(semantic_version version) noexcept
92{
93 _application_version = version;
94}
95
96inline void set_application_version(int major, int minor = 0, int patch = 0) noexcept
97{
98 return set_application_version(semantic_version{major, minor, patch});
99}
100
101} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:13
constexpr std::string make_slug(std::string_view str) noexcept
Create a slug from a string.
Definition strings.hpp:251