99 lines
2.8 KiB
C++
99 lines
2.8 KiB
C++
#pragma once
|
|
#include <cstddef>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <variant>
|
|
#include <vector>
|
|
#include<fstream>
|
|
|
|
namespace ztl{
|
|
template<class ...Ts>
|
|
void logger(Ts&&...v){
|
|
#ifndef NDEBUG
|
|
std::cout<<"log: ";
|
|
(std::cout<<...<<v);
|
|
std::cout<<"\n";
|
|
#endif
|
|
}
|
|
|
|
template<class T>
|
|
std::string get_type_detail(){
|
|
return __PRETTY_FUNCTION__;
|
|
}
|
|
|
|
template<class T>
|
|
std::string get_T_name(){
|
|
std::string type_detail = get_type_detail<T>();
|
|
// logger(type_detail);
|
|
size_t start = type_detail.find("T = ");
|
|
if(start == std::string::npos) {
|
|
throw std::runtime_error("Failed to get type name");
|
|
}
|
|
size_t end = type_detail.find(';', start);
|
|
if(end == std::string::npos) {
|
|
end = type_detail.size()-1;
|
|
}
|
|
// logger(type_detail.substr(start + 4, end - start - 4));
|
|
return type_detail.substr(start + 4, end - start - 4);
|
|
}
|
|
|
|
template<class ...Ts>
|
|
struct overloaded:Ts...{using Ts::operator()...;};
|
|
|
|
template<class ...Ts>
|
|
overloaded(Ts...)->overloaded<Ts...>;
|
|
|
|
template<class ...Ts, class ...Fs>
|
|
auto match(std::variant<Ts...>& v, Fs&&... func) {
|
|
return std::visit(ztl::overloaded{std::forward<Fs>(func)...}, v);
|
|
}
|
|
template<class ...Ts, class ...Fs>
|
|
auto match(const std::variant<Ts...>& v, Fs&&... func) {
|
|
return std::visit(ztl::overloaded{std::forward<Fs>(func)...}, v);
|
|
}
|
|
template<class ...Ts, class ...Fs>
|
|
auto match(std::variant<Ts...>&& v, Fs&&... func) {
|
|
return std::visit(ztl::overloaded{std::forward<Fs>(func)...}, std::move(v));
|
|
}
|
|
struct Err:std::string{};
|
|
|
|
// template<class T>
|
|
// struct Ok{
|
|
// T data;
|
|
// };
|
|
|
|
template<class T>
|
|
struct Result:std::variant<T,Err>{
|
|
T unwrap(){
|
|
return match<T>(*this,[](const T&t){
|
|
return t;
|
|
},[](const Err &e){
|
|
throw std::runtime_error(e);
|
|
T t;
|
|
return t;
|
|
});
|
|
}
|
|
};
|
|
|
|
inline Result<std::string> get_argv(size_t idx){
|
|
if(idx>=__argc){
|
|
return Result<std::string>(Err("argv's index out of range"));
|
|
}else{
|
|
return Result<std::string>(std::string(__argv[idx]));
|
|
}
|
|
}
|
|
|
|
inline Result<std::string> get_string_from_file(const std::string &file_path){
|
|
std::ifstream file(file_path);
|
|
if (!file) {
|
|
return Result<std::string>(Err("Could not open file: " + file_path));
|
|
}
|
|
std::string content((std::istreambuf_iterator<char>(file)),
|
|
std::istreambuf_iterator<char>());
|
|
return Result<std::string>(content);
|
|
}
|
|
}
|
|
|