λ61
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 Tetex7
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19#ifndef L61_UTILS_HPP
20#define L61_UTILS_HPP
21#include <string>
22#include <functional>
23#include <type_traits>
24#include <vector>
25#include <memory>
26#include <format>
27
28namespace l61
29{
30 /*template<typename T>
31 concept LambdaCall = requires(T t) { //WIP
32 // Must be a class with an overloaded operator()
33 { &T::operator() } -> std::same_as<auto (T::*)(...) const>;
34 };*/
35 namespace meta
36 {
37 template<typename T>
38 concept CppObject = std::is_class_v<T>;
39 }
40
41 std::vector<char> get_file(const std::string& f_name);
42 std::string get_file_str(const std::string& f_name);
43 std::string execEx(const char* cmd);
44 std::string get_input(const std::string& prompt = "");
45
53 template<typename FunctionSignature, typename Lambda>
54 std::add_pointer_t<FunctionSignature> lambdaToFunPtr(const Lambda& lambda)
55 {
56 static_assert(std::is_convertible_v<Lambda, std::add_pointer_t<std::type_identity_t<FunctionSignature>>>, "Lambda must be non-capturing and convertible to a function pointer.");
57 return static_cast<std::add_pointer_t<FunctionSignature>>(lambda);
58 }
59
65 template<meta::CppObject T>
66 [[__gnu__::__always_inline__]]
67 constexpr void deconstruct(T& val)
68 {
69 static_assert(std::is_destructible_v<T>, "Type must be destructible");
70 static_assert(not std::is_trivially_destructible_v<T>);
71 val.~T();
72 }
73
74 template<typename T>
75 constexpr T runLambda(const std::function<T()>& lambda)
76 {
77 return std::forward<T>(lambda());
78 }
79
80 template<typename T>
81 std::string toAddressString(const T* ptr)
82 {
83 return std::format("{:p}", static_cast<const void*>(ptr));
84 }
85
86 template<typename T>
87 std::size_t getHash(const T& v)
88 {
89 return std::hash<T>{}(v);
90 }
91}
92
93#endif
Definition utils.hpp:38
Definition Object.hpp:34
constexpr void deconstruct(T &val)
Deconstructs an object only use when necessary.
Definition utils.hpp:67
std::string toAddressString(const T *ptr)
Definition utils.hpp:81
std::string execEx(const char *cmd)
Definition utils.cpp:49
std::vector< char > get_file(const std::string &f_name)
Definition utils.cpp:25
std::size_t getHash(const T &v)
Definition utils.hpp:87
constexpr T runLambda(const std::function< T()> &lambda)
Definition utils.hpp:75
std::string get_input(const std::string &prompt="")
Definition utils.cpp:71
std::add_pointer_t< FunctionSignature > lambdaToFunPtr(const Lambda &lambda)
Designed to help with C Apis that take function pointers.
Definition utils.hpp:54
std::string get_file_str(const std::string &f_name)
Definition utils.cpp:38