mirror of
https://gitlab.com/square-game-liberation-front/F.E.I.S.git
synced 2025-02-23 21:43:18 +01:00
27 lines
513 B
C++
27 lines
513 B
C++
#pragma once
|
|
|
|
#include <optional>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
template<class A, class B>
|
|
B apply_or(std::optional<A> opt, B(*func)(A), B b) {
|
|
if (opt.has_value()) {
|
|
return func(*opt);
|
|
} else {
|
|
return b;
|
|
}
|
|
}
|
|
|
|
template<class A>
|
|
std::string stringify_or(std::optional<A> opt, std::string fallback) {
|
|
return apply_or(
|
|
opt,
|
|
[](const A& a){
|
|
std::stringstream ss;
|
|
ss << a;
|
|
return ss.str();
|
|
},
|
|
fallback
|
|
);
|
|
} |