main
Brett 2025-04-11 14:44:45 -04:00
parent 78c219cc67
commit 1b4ad25bcf
4 changed files with 21 additions and 7 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake) include(cmake/color.cmake)
set(BLT_VERSION 5.2.41) set(BLT_VERSION 5.2.42)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -201,18 +201,26 @@ namespace blt::mem
static constexpr std::size_t make_storage_ones() static constexpr std::size_t make_storage_ones()
{ {
#ifdef __EMSCRIPTEN__
return ~static_cast<size_t>(0);
#else
std::size_t result = 0; std::size_t result = 0;
for (std::size_t i = START_BIT; i < END_BIT; i++) for (std::size_t i = START_BIT; i < END_BIT; i++)
result |= 1ul << i; result |= 1ul << i;
return result; return result;
#endif
} }
static constexpr std::size_t make_ptr_ones() static constexpr std::size_t make_ptr_ones()
{ {
#ifdef __EMSCRIPTEN__
return ~static_cast<size_t>(0);
#else
std::size_t result = 0; std::size_t result = 0;
for (std::size_t i = 0; i < START_BIT; i++) for (std::size_t i = 0; i < START_BIT; i++)
result |= 1ul << i; result |= 1ul << i;
return result; return result;
#endif
} }
bit_storage(): bits(0) bit_storage(): bits(0)
@ -263,16 +271,19 @@ namespace blt::mem
pointer_storage& bit(const std::size_t index, const bool b) noexcept pointer_storage& bit(const std::size_t index, const bool b) noexcept
{ {
#ifndef __EMSCRIPTEN__
if (index >= bit_storage::END_BIT) if (index >= bit_storage::END_BIT)
return *this; return *this;
ptr_bits &= ~(1ul << (bit_storage::START_BIT + index)); ptr_bits &= ~(1ul << (bit_storage::START_BIT + index));
ptr_bits |= (static_cast<std::uintptr_t>(b) << (bit_storage::START_BIT + index)); ptr_bits |= (static_cast<std::uintptr_t>(b) << (bit_storage::START_BIT + index));
#endif
return *this; return *this;
} }
template<typename T, std::enable_if_t<!std::is_same_v<T, bit_storage>, bool> = false> template<typename T, std::enable_if_t<!std::is_same_v<T, bit_storage>, bool> = false>
pointer_storage& storage(const T& type) pointer_storage& storage(const T& type)
{ {
#ifndef __EMSCRIPTEN__
static_assert(sizeof(T) <= sizeof(std::uintptr_t), "Type takes too many bits to be stored!"); static_assert(sizeof(T) <= sizeof(std::uintptr_t), "Type takes too many bits to be stored!");
static constexpr std::uintptr_t store_bits = (2 << (bit_storage::AVAILABLE_BITS - 1)) - 1; static constexpr std::uintptr_t store_bits = (2 << (bit_storage::AVAILABLE_BITS - 1)) - 1;
std::uintptr_t bit_store = 0; std::uintptr_t bit_store = 0;
@ -280,12 +291,15 @@ namespace blt::mem
std::memcpy(&bit_store, &type, sizeof(T)); std::memcpy(&bit_store, &type, sizeof(T));
store.bits |= bit_store & store_bits; store.bits |= bit_store & store_bits;
storage(store); storage(store);
#endif
return *this; return *this;
} }
pointer_storage& storage(const bit_storage bits) noexcept pointer_storage& storage(const bit_storage bits) noexcept
{ {
#ifndef __EMSCRIPTEN__
ptr_bits = ((ptr_bits & PTR_ALL_ONES) | (static_cast<std::uintptr_t>(bits.bits) << bit_storage::START_BIT)); ptr_bits = ((ptr_bits & PTR_ALL_ONES) | (static_cast<std::uintptr_t>(bits.bits) << bit_storage::START_BIT));
#endif
return *this; return *this;
} }

@ -1 +1 @@
Subproject commit 8a889d3699b3c09ade435641fb034427f3fd12b6 Subproject commit 154c63489e84d5569d3b466342a2ae8fd99e4734

View File

@ -313,7 +313,7 @@ namespace blt::argparse
break; break;
case action_t::COUNT: case action_t::COUNT:
set_nargs(0); set_nargs(0);
as_type<size_t>(); as_type<u64>();
break; break;
case action_t::EXTEND: case action_t::EXTEND:
set_nargs(nargs_t::ALL); set_nargs(nargs_t::ALL);
@ -1176,14 +1176,14 @@ namespace blt::argparse
argument_parser_t parser; argument_parser_t parser;
parser.add_flag("-a").set_action(action_t::STORE_TRUE); parser.add_flag("-a").set_action(action_t::STORE_TRUE);
parser.add_flag("+b").set_action(action_t::STORE_FALSE); parser.add_flag("+b").set_action(action_t::STORE_FALSE);
parser.add_flag("/c").as_type<int>().set_action(action_t::STORE); parser.add_flag("/c").as_type<u32>().set_action(action_t::STORE);
const std::vector<std::string> args = {"./program", "-a", "+b", "/c", "42"}; const std::vector<std::string> args = {"./program", "-a", "+b", "/c", "42"};
const auto parsed_args = parser.parse(args); const auto parsed_args = parser.parse(args);
BLT_ASSERT(parsed_args.get<bool>("-a") == true && "Flag '-a' should store `true`"); BLT_ASSERT(parsed_args.get<bool>("-a") == true && "Flag '-a' should store `true`");
BLT_ASSERT(parsed_args.get<bool>("+b") == false && "Flag '+b' should store `false`"); BLT_ASSERT(parsed_args.get<bool>("+b") == false && "Flag '+b' should store `false`");
BLT_ASSERT(parsed_args.get<int>("/c") == 42 && "Flag '/c' should store the value 42"); BLT_ASSERT(parsed_args.get<u32>("/c") == 42 && "Flag '/c' should store the value 42");
} }
// Test: Invalid flag prefixes // Test: Invalid flag prefixes
@ -1208,12 +1208,12 @@ namespace blt::argparse
void test_compound_flags() void test_compound_flags()
{ {
argument_parser_t parser; argument_parser_t parser;
parser.add_flag("-v").as_type<int>().set_action(action_t::COUNT); parser.add_flag("-v").set_action(action_t::COUNT);
const std::vector<std::string> args = {"./program", "-vvv"}; const std::vector<std::string> args = {"./program", "-vvv"};
const auto parsed_args = parser.parse(args); const auto parsed_args = parser.parse(args);
BLT_ASSERT(parsed_args.get<size_t>("-v") == 3 && "Flag '-v' should count occurrences in compound form"); BLT_ASSERT(parsed_args.get<u64>("-v") == 3 && "Flag '-v' should count occurrences in compound form");
} }
void test_combination_of_valid_and_invalid_flags() void test_combination_of_valid_and_invalid_flags()