-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptional.hpp
More file actions
97 lines (87 loc) · 3.35 KB
/
optional.hpp
File metadata and controls
97 lines (87 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once
#ifndef __CPPM_UTIL_OPTIONAL_HPP
#define __CPPM_UTIL_OPTIONAL_HPP
#include<optional>
#include<fmt/format.h>
#include<type_traits>
#include<string_view>
#include<iterator>
#include<algorithm>
#include<functional>
#include <memory>
template<typename U>
struct fmt::formatter<std::optional<U>> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const std::optional<U>& p, FormatContext& ctx) {
return format_to(ctx.out(), "{}", p.value());
}
};
namespace cppm::util
{
inline void panic(bool opt, const std::string& message) { if(!opt) { fmt::print(stderr, "{}", message); exit(1); } }
template<typename T>
T panic(std::optional<T> opt, const std::string& message) {
if(!opt) { fmt::print(stderr, "{}", message); exit(1); }
return *opt;
}
template<typename T>
std::shared_ptr<T> panic(std::shared_ptr<T> opt, const std::string& message) {
if(!opt) { fmt::print(stderr, "{}", message); exit(1); }
return opt;
}
}
//
//namespace cppm::toml {
// inline std::shared_ptr<cpptoml::table> get_table(std::shared_ptr<cpptoml::table> table,
// const std::string& name) {
// auto tb = table->get_table(name);
// if(!tb) tb = cpptoml::make_table();
// return tb;
// }
//
// inline std::shared_ptr<cpptoml::table_array> get_table_array(std::shared_ptr<cpptoml::table> table,
// const std::string& name,
// std::optional<std::function<void(std::shared_ptr<cpptoml::table>)>> func = std::nullopt) {
// auto tb = table->get_table_array(name);
// if(tb && func) std::for_each(tb->begin(), tb->end(), *func);
// return tb;
//
// }
// inline int get(std::shared_ptr<cpptoml::table> table, const std::string& name, int def_v) {
// return table->get_as<int>(name).value_or(def_v);
// }
//
// inline std::string get(std::shared_ptr<cpptoml::table> table, const std::string& name, std::string_view def_v) {
// return table->get_as<std::string>(name).value_or(std::string{def_v});
// }
//
// inline bool get_bool(std::shared_ptr<cpptoml::table> table, const std::string& name, bool def_v) {
// return table->get_as<bool>(name).value_or(def_v);
// }
//
// template<typename T>
// std::optional<T> get(std::shared_ptr<cpptoml::table> table, std::string_view name) {
// return table->get_as<T>(std::string{name});
// }
//
// template<typename T>
// std::optional<T> get(std::shared_ptr<cpptoml::table> table, const std::string& name) {
// return table->get_as<T>(std::string{name});
// }
//
// template<typename T = std::string>
// auto panic(std::shared_ptr<cpptoml::table> table, const std::string& name) {
// using namespace fmt::literals;
// return util::panic(get<T>(table, name), "need {} config"_format(name));
// }
//
// template<typename Iter>
// auto get_array(std::shared_ptr<cpptoml::table> table, const std::string& name, Iter& dest) {
// auto arr = table->get_array_of<std::string>(name);
// if(arr) { std::copy(arr->begin(), arr->end(), std::back_inserter(dest)); }
// return arr;
// }
//
//}
#endif