Arion 1.0.2-alpha
A high-performance C++ framework for emulating executable binaries.
 
Loading...
Searching...
No Matches
type_utils.hpp
Go to the documentation of this file.
1#ifndef ARION_TYPE_UTILS_HPP
2#define ARION_TYPE_UTILS_HPP
3
5#include <cstdint>
6#include <memory>
7#include <string>
8
9#define COMMON_TYPE_PRIORITY 0
10#define OS_BASE_TYPE_PRIORITY 1
11#define OS_STRUCT_FACTORY_PRIORITY 2
12#define OS_STRUCT_TYPE_PRIORITY 3
13#define OS_VARIABLE_STRUCT_TYPE_PRIORITY 4
14
15namespace arion
16{
17
18class Arion;
19
20};
21
22namespace arion_type
23{
24
27{
28 private:
30 std::string name;
33
34 protected:
41
42 public:
44 virtual ~KernelType() = default;
56 virtual std::string str(std::shared_ptr<arion::Arion> arion, uint64_t val);
57};
58
60using InitFn = std::function<void()>;
63{
64 private:
66 std::vector<std::tuple<int, InitFn>> initializers;
68 bool initialized = false;
69
70 public:
76 {
77 static KernelTypeRegistry r;
78 return r;
79 }
80
86 void register_type(uint16_t priority, InitFn fn)
87 {
88 this->initializers.emplace_back(priority, fn);
89 }
90
95 {
96 this->initialized = true;
97 std::sort(this->initializers.begin(), this->initializers.end(),
98 [](auto &a, auto &b) { return std::get<0>(a) < std::get<0>(b); });
99
100 for (auto &[_, fn] : this->initializers)
101 fn();
102 }
103
109 {
110 return this->initialized;
111 }
112};
113
121#define ARION_REGISTER_KERNEL_TYPE(VAR, TYPE, PRIORITY) \
122 static_assert(std::is_same_v<decltype(VAR), std::shared_ptr<TYPE>>, \
123 "ARION_REGISTER_KERNEL_TYPE expects shared_ptr<TYPE>"); \
124 static struct KernelTypeRegistrar_##VAR \
125 { \
126 KernelTypeRegistrar_##VAR() \
127 { \
128 KernelTypeRegistry::instance().register_type(PRIORITY, [] { VAR = std::make_shared<TYPE>(); }); \
129 } \
130 } KernelTypeRegistrarInstance_##VAR;
131
133class FlagType : public KernelType
134{
135 private:
137 std::map<uint64_t, std::string> flag_map;
138
139 protected:
145 FlagType(std::string name, std::map<uint64_t, std::string> flag_map)
146 : KernelType(name, arion::LOG_COLOR::CYAN), flag_map(flag_map) {};
147
148 public:
155 std::string str(std::shared_ptr<arion::Arion> arion, uint64_t val) override;
156};
157
159class IntType : public KernelType
160{
161 public:
165 IntType() : KernelType("Int", arion::LOG_COLOR::MAGENTA) {};
166};
168extern std::shared_ptr<IntType> INT_TYPE;
169
172{
173 public:
177 RawStringType() : KernelType("Raw String", arion::LOG_COLOR::GREEN) {};
184 std::string str(std::shared_ptr<arion::Arion> arion, uint64_t val) override;
185};
187extern std::shared_ptr<RawStringType> RAW_STRING_TYPE;
188
189}; // namespace arion_type
190
191#endif // ARION_TYPE_UTILS_HPP
Class for data types represented as a set of bit flags or discrete symbolic constants.
Definition type_utils.hpp:134
std::map< uint64_t, std::string > flag_map
Map linking raw integer flag values to their string names (e.g., 0x1 to "MAP_SHARED").
Definition type_utils.hpp:137
FlagType(std::string name, std::map< uint64_t, std::string > flag_map)
Definition type_utils.hpp:145
std::string str(std::shared_ptr< arion::Arion > arion, uint64_t val) override
Concrete type representing a generic integer value.
Definition type_utils.hpp:160
IntType()
Definition type_utils.hpp:165
Singleton class responsible for managing the initialization order of all kernel type objects.
Definition type_utils.hpp:63
bool is_initialized()
Definition type_utils.hpp:108
void register_type(uint16_t priority, InitFn fn)
Definition type_utils.hpp:86
static KernelTypeRegistry & instance()
Definition type_utils.hpp:75
std::vector< std::tuple< int, InitFn > > initializers
List of initialization functions paired with their priority.
Definition type_utils.hpp:66
void init_types()
Definition type_utils.hpp:94
bool initialized
Flag indicating if initialization has been completed.
Definition type_utils.hpp:68
Abstract base class for all kernel-related data types used for visualization/logging.
Definition type_utils.hpp:27
arion::LOG_COLOR color
The log color associated with this data type for display.
Definition type_utils.hpp:32
std::string name
Display name of the data type (e.g., "File descriptor").
Definition type_utils.hpp:30
virtual std::string str(std::shared_ptr< arion::Arion > arion, uint64_t val)
KernelType(std::string name, arion::LOG_COLOR color=arion::LOG_COLOR::DEFAULT)
Definition type_utils.hpp:40
arion::LOG_COLOR get_color()
virtual ~KernelType()=default
Virtual destructor to allow polymorphic deletion.
Concrete type representing a memory address pointing to a null-terminated string.
Definition type_utils.hpp:172
RawStringType()
Definition type_utils.hpp:177
std::string str(std::shared_ptr< arion::Arion > arion, uint64_t val) override
Definition type_utils.hpp:23
std::shared_ptr< RawStringType > RAW_STRING_TYPE
Shared pointer to the singleton instance of RawStringType.
std::shared_ptr< IntType > INT_TYPE
Shared pointer to the singleton instance of IntType.
std::function< void()> InitFn
Function type used for initializing a kernel type.
Definition type_utils.hpp:60
Definition arch_x86-64.hpp:11
LOG_COLOR
These colors can be used for logging.
Definition logger.hpp:22
@ DEFAULT
Default foreground color.
Definition logger.hpp:23