Loading [MathJax]/extensions/tex2jax.js
libTriton version 1.0 build 1599
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
coreUtils.hpp
Go to the documentation of this file.
1
2/*
3** Copyright (C) - Triton
4**
5** This program is under the terms of the Apache License 2.0.
6*/
7
8#ifndef TRITON_CORE_UTIL_H
9#define TRITON_CORE_UTIL_H
10
11#include <sstream>
12#include <string>
13#include <algorithm>
14#include <type_traits>
15
16#include <triton/config.hpp>
17#include <triton/dllexport.hpp>
19
20
21
23namespace triton {
30 namespace utils {
38 template<typename T>
39 inline std::string toString(const T& obj) {
40 std::stringstream ss;
41 ss << obj;
42 return ss.str();
43 }
44
46 TRITON_EXPORT void fromUintToBuffer(triton::uint80 value, triton::uint8* buffer);
47
49 TRITON_EXPORT void fromUintToBuffer(triton::uint128 value, triton::uint8* buffer);
50
52 TRITON_EXPORT void fromUintToBuffer(triton::uint256 value, triton::uint8* buffer);
53
55 TRITON_EXPORT void fromUintToBuffer(triton::uint512 value, triton::uint8* buffer);
56
58 template <typename T>
59 constexpr T cast(const triton::uint512& value) {
60 return static_cast<T>(value);
61 }
62
64 template <typename T>
65 constexpr T cast(const triton::uint80& value) {
66 return static_cast<T>(value);
67 }
68
70 template <typename T> T cast(const triton::uint8* buffer) {
71 // We want to always trigger the static assert when this function is defined (use with a type without specialization)
72 // so we have to set the value to false with dependency on T type.
73 static_assert(not std::is_same<T, T>::value, "cast have no implementation for this type");
74 return {};
75 }
76
77 template <> TRITON_EXPORT triton::uint80 cast(const triton::uint8* buffer);
78 template <> TRITON_EXPORT triton::uint128 cast(const triton::uint8* buffer);
79 template <> TRITON_EXPORT triton::uint256 cast(const triton::uint8* buffer);
80 template <> TRITON_EXPORT triton::uint512 cast(const triton::uint8* buffer);
81
82 template <> TRITON_EXPORT triton::uint80 cast(const triton::uint512& value);
83 template <> TRITON_EXPORT triton::uint512 cast(const triton::uint80& value);
84
85 template <typename T>
86 std::enable_if_t<std::is_unsigned_v<T>, T> byteswap(T value) {
87 std::array<std::byte, sizeof(value)> repr;
88 std::memcpy(&repr, &value, sizeof(value));
89 std::reverse(repr.begin(), repr.end());
90 T result;
91 std::memcpy(&result, &repr, sizeof(result));
92 return result;
93 }
94
96 };
98};
99
100#endif /* TRITON_CORE_UTIL_H */
std::uint8_t uint8
unisgned 8-bits
TRITON_EXPORT void fromUintToBuffer(triton::uint80 value, triton::uint8 *buffer)
Inject the value into the buffer. Make sure that the buffer contains at least 10 allocated bytes.
Definition coreUtils.cpp:16
constexpr T cast(const triton::uint512 &value)
Casts an triton::uint512 to T.
Definition coreUtils.hpp:59
std::string toString(const T &obj)
Converts an object to a string.
Definition coreUtils.hpp:39
The Triton namespace.