Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/include/join/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <iostream>
#include <iomanip>
#include <sstream>
#include <utility>
#include <chrono>
#include <random>
#include <limits>
Expand Down Expand Up @@ -66,6 +67,16 @@ namespace join
};
}

/**
* @brief throw an exception out of the hot path.
* @param args exception constructor arguments.
*/
template <typename Exception, typename... Args>
[[noreturn]] __attribute__ ((cold, noinline)) void throwException (Args&&... args)
{
throw Exception (std::forward<Args> (args)...);
}

/**
* @brief swaps byte orders.
* @param val value to swap.
Expand Down
16 changes: 11 additions & 5 deletions core/include/join/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

// libjoin.
#include <join/traits.hpp>
#include <join/utils.hpp>

// C++.
#include <algorithm>
Expand All @@ -36,6 +37,7 @@

// C.
#include <cstddef>
#include <cstdint>

namespace join
{
Expand Down Expand Up @@ -505,7 +507,7 @@ namespace join
{
if (index () != find_index_v<T, Ts...>)
{
throw std::bad_cast ();
throwException<std::bad_cast> ();
}
return *reinterpret_cast<T*> (storage ());
}
Expand All @@ -520,7 +522,7 @@ namespace join
{
if (index () != find_index_v<T, Ts...>)
{
throw std::bad_cast ();
throwException<std::bad_cast> ();
}
return *reinterpret_cast<const T*> (storage ());
}
Expand All @@ -535,7 +537,7 @@ namespace join
{
if (index () != I)
{
throw std::bad_cast ();
throwException<std::bad_cast> ();
}
return *reinterpret_cast<std::tuple_element_t<I, std::tuple<Ts...>>*> (storage ());
}
Expand All @@ -550,7 +552,7 @@ namespace join
{
if (index () != I)
{
throw std::bad_cast ();
throwException<std::bad_cast> ();
}
return *reinterpret_cast<const std::tuple_element_t<I, std::tuple<Ts...>>*> (storage ());
}
Expand Down Expand Up @@ -621,6 +623,10 @@ namespace join
*/
constexpr std::size_t index () const noexcept
{
if (_which >= sizeof...(Ts))
{
__builtin_unreachable ();
}
return _which;
}

Expand Down Expand Up @@ -710,7 +716,7 @@ namespace join
alignas (Ts...) unsigned char _data[std::max ({sizeof (Ts)...})];

/// index of the alternative that is currently held by the variant.
std::size_t _which = 0;
uint32_t _which = 0;
};

/**
Expand Down
10 changes: 10 additions & 0 deletions core/tests/utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <gtest/gtest.h>

// C++.
#include <typeinfo>
#include <sstream>
#include <thread>

Expand Down Expand Up @@ -365,6 +366,15 @@ TEST (Utils, nextPow2)
static_assert (join::isPow2 (join::nextPow2 (300)), "nextPow2 must yield a power of two");
}

/**
* @brief test throwException.
*/
TEST (Utils, throwException)
{
EXPECT_THROW (join::throwException<std::bad_cast> (), std::bad_cast);
EXPECT_THROW (join::throwException<std::out_of_range> ("invalid key"), std::out_of_range);
}

/**
* @brief main function.
*/
Expand Down
15 changes: 3 additions & 12 deletions data/include/join/document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <join/sax.hpp>

// C++.
#include <string_view>
#include <algorithm>
#include <iterator>
#include <utility>
Expand Down Expand Up @@ -215,19 +216,9 @@ namespace join
* @param value string value to set.
* @return 0 on success, -1 otherwise.
*/
int setString (const std::string& value)
int setString (std::string_view value)
{
return emplaceValue (value);
}

/**
* @brief set string value.
* @param value string value to move.
* @return 0 on success, -1 otherwise.
*/
int setString (std::string&& value)
{
return emplaceValue (std::move (value));
return emplaceValue (std::string (value));
}

/**
Expand Down
17 changes: 12 additions & 5 deletions data/include/join/dtoa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ namespace join
}
else if ((0 < kk) && (kk <= 21))
{
memmove (&buffer[kk + 1], &buffer[kk], length - kk);
if ((length - kk <= 16) && (kk <= 7))
{
char tmp[16];
memcpy (tmp, &buffer[kk], 16);
memcpy (&buffer[kk + 1], tmp, 16);
}
else
{
memmove (&buffer[kk + 1], &buffer[kk], length - kk);
}
buffer[kk] = '.';
return &buffer[length + 1];
}
Expand Down Expand Up @@ -170,10 +179,8 @@ namespace join
delta *= 10;
unit *= 10;
char d = static_cast<char> (p2 >> -one._exponent);
if (d || length)
{
buffer[length++] = '0' + d;
}
buffer[length] = '0' + d;
length += (d || length);
p2 &= one._mantissa - 1;
--kappa;
if (p2 < delta)
Expand Down
Loading
Loading