Changed includes; Implemented std::pair and std::make_unsigned; changed usage of std::memcpy to memcpy

This commit is contained in:
Andreas Tsouchlos 2022-02-23 19:45:17 +01:00
parent 5ba1723295
commit ea722ec5bf
7 changed files with 221 additions and 137 deletions

View File

@ -57,7 +57,7 @@ constexpr inline void format_arg(char* dest, const char* arg) {
dest = dest + fmt_data.length - len;
if (!std::is_constant_evaluated()) {
std::memcpy(dest, arg, len);
memcpy(dest, arg, len);
return;
}
for (std::size_t i = 0; i < len; ++i) {

View File

@ -12,7 +12,7 @@
#include <stdint.h>
#include "std_lib.h"
#include "stdlib.h"
#include "utility.h"
@ -120,7 +120,7 @@ constexpr inline const char* digits2_base(size_t value) {
constexpr inline void copy2(char* dst, const char* src) {
if (!std::is_constant_evaluated()) {
std::memcpy(dst, src, 2);
memcpy(dst, src, 2);
return;
}
*dst++ = static_cast<char>(*src++);

View File

@ -1,127 +0,0 @@
#ifndef CONST_FMT_STD_LIB_H
#define CONST_FMT_STD_LIB_H
#ifndef CONST_FMT_NO_CPP_STD_LIB
#include <array>
#include <cstring>
#else
#include <stdint.h>
#include <string.h>
namespace std {
using size_t = uint16_t;
/*
*
* type_traits
*
*/
// clang-format off
template<typename T> struct remove_reference { using type = T; };
template<typename T> struct remove_reference<T&> { using type = T; };
template<typename T> struct remove_reference<T&&> { using type = T; };
template<typename T>
using remove_reference_t = typename std::remove_reference<T>::type;
// clang-format on
/*
*
* utility
*
*/
template <typename T>
std::remove_reference_t<T>&& move(T&& arg) noexcept {
return reinterpret_cast<std::remove_reference_t<T>&&>(arg);
}
template <typename T>
void swap(T& t1, T& t2) {
T temp = std::move(t1);
t1 = std::move(t2);
t2 = std::move(temp);
}
/*
*
* std::array
*
*/
// TODO: Is std::size_t really the best bet here?
template <typename data_t, std::size_t t_size>
class array {
public:
template<typename... args_t>
constexpr array(args_t... args) noexcept : m_data{args...} {
static_assert(sizeof...(args) == t_size, "Invalid number of arguments");
}
constexpr array() noexcept = default;
constexpr array(array&) = default;
constexpr array(array&&) = default;
constexpr array& operator=(array& other) = default;
constexpr array& operator=(array&& other) = default;
constexpr void swap(array<data_t, t_size>& other) noexcept {
for (int i = 0; i < t_size; ++i) {
using std::swap;
swap(m_data[i], other.m_data[i]);
}
}
constexpr data_t& operator[](std::size_t index) noexcept {
return m_data[index];
}
constexpr const data_t& operator[](std::size_t index) const noexcept {
return m_data[index];
}
using iterator = data_t*;
using const_iterator = const data_t*;
constexpr iterator begin() noexcept {
return &(m_data[0]);
}
constexpr iterator end() noexcept {
return (&(m_data[t_size - 1]) + 1);
}
constexpr const_iterator cbegin() const noexcept {
return &(m_data[0]);
}
constexpr const_iterator cend() const noexcept {
return (&(m_data[t_size - 1]) + 1);
}
data_t m_data[t_size];
};
} // namespace std
#endif
#endif // CONST_FMT_STD_LIB_H

209
const_fmt/stdlib.h Normal file
View File

@ -0,0 +1,209 @@
#ifndef CONST_FMT_STDLIB_H
#define CONST_FMT_STDLIB_H
/*
*
* Disclaimer: Probably very bad implementation of some features of the C++
* Standard Library. Not meant as a full-on stdlib implementation, only for
* usage in this project (Underlined by the fact, that in this case the
* namespace std is actually const_fmt::std)
*
*/
#ifndef CONST_FMT_NO_CPP_STDLIB
#include <array>
#include <cstring>
#else
#include <stdint.h>
#include <string.h>
namespace const_fmt { namespace std {
using size_t = uint16_t;
/*
*
* type_traits
*
*/
// clang-format off
template<typename T>
struct remove_reference {
using type = T;
};
template<typename T>
struct remove_reference<T &> {
using type = T;
};
template<typename T>
struct remove_reference<T &&> {
using type = T;
};
template<typename T>
using remove_reference_t = typename std::remove_reference<T>::type;
// clang-format on
constexpr inline bool is_constant_evaluated() noexcept {
return __builtin_is_constant_evaluated();
}
// clang-format off
template <typename type_t> struct make_unsigned { using type = type_t; };
template <> struct make_unsigned<signed char> { using type = char; };
template <> struct make_unsigned<unsigned short> { using type = unsigned short; };
template <> struct make_unsigned<signed int> { using type = unsigned int; };
template <> struct make_unsigned<signed long> { using type = unsigned long; };
template <> struct make_unsigned<signed long long> { using type = unsigned long long; };
// clang-format on
/*
*
* concepts
*
*/
//template <typename _Tp>
//concept integral = is_integral_v<_Tp>;
//
//template <typename _Tp>
//concept signed_integral = integral<_Tp> && is_signed_v<_Tp>;
//
//template <typename _Tp>
//concept unsigned_integral = integral<_Tp> && !signed_integral<_Tp>;
//
//template <typename _Tp>
//concept floating_point = is_floating_point_v<_Tp>;
/*
*
* algorithm
*
*/
template <typename input_t, typename output_t>
void copy(input_t* start, input_t* end, output_t* dest_start) {
memcpy(start, dest_start, end - start);
}
/*
*
* utility
*
*/
template <typename T>
std::remove_reference_t<T>&& move(T&& arg) noexcept {
return reinterpret_cast<std::remove_reference_t<T>&&>(arg);
}
template <typename T>
void swap(T& t1, T& t2) {
T temp = std::move(t1);
t1 = std::move(t2);
t2 = std::move(temp);
}
template <typename first_t, typename second_t>
struct pair {
first_t first;
second_t second;
};
/*
*
* array
*
*/
// TODO: Is std::size_t really the best bet here?
template <typename data_t, std::size_t t_size>
class array {
public:
template <typename... args_t>
constexpr array(args_t... args) noexcept : m_data{args...} {
static_assert(sizeof...(args) == t_size, "Invalid number of arguments");
}
constexpr array() noexcept = default;
constexpr array(array&) = default;
constexpr array(array&&) = default;
constexpr array& operator=(array& other) = default;
constexpr array& operator=(array&& other) = default;
constexpr void swap(array<data_t, t_size>& other) noexcept {
for (int i = 0; i < t_size; ++i) {
using std::swap;
swap(m_data[i], other.m_data[i]);
}
}
constexpr data_t& operator[](std::size_t index) noexcept {
return m_data[index];
}
constexpr const data_t& operator[](std::size_t index) const noexcept {
return m_data[index];
}
using iterator = data_t*;
using const_iterator = const data_t*;
constexpr iterator begin() noexcept {
return &(m_data[0]);
}
constexpr iterator end() noexcept {
return (&(m_data[t_size - 1]) + 1);
}
constexpr const_iterator cbegin() const noexcept {
return &(m_data[0]);
}
constexpr const_iterator cend() const noexcept {
return (&(m_data[t_size - 1]) + 1);
}
data_t m_data[t_size];
};
}} // namespace const_fmt::std
#endif
#endif // CONST_FMT_STDLIB_H

View File

@ -3,7 +3,7 @@
#include "std_lib.h"
#include "stdlib.h"
namespace const_fmt { namespace const_fmt_detail {

View File

@ -4,7 +4,7 @@
#include <string.h>
#include "std_lib.h"
#include "stdlib.h"
#include "types.h"

View File

@ -1,6 +1,8 @@
#define CONST_FMT_NO_CPP_STDLIB
#include <const_fmt/format.h>
#include <iostream>
#include <string_view>
//#include <iostream>
//#include <string_view>
using namespace const_fmt;
@ -9,9 +11,9 @@ int main() {
// Convert s (with a type of 'std::array<char, N>') into something
// writable to std::cout
std::string_view sv{&s[0], s.size()};
std::cout << sv << std::endl;
// std::string_view sv{&s[0], s.size()};
//
// std::cout << sv << std::endl;
return 0;
}