Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compilation on Ubuntu 20.04 #210

Merged
merged 1 commit into from
Oct 19, 2024
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
3 changes: 3 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
ruby: ['3.0', '3.1', '3.2', '3.3']
include:
- os: ubuntu-20.04
ruby: '3.1'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
28 changes: 9 additions & 19 deletions include/rice/rice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,6 @@ namespace Rice::detail

#include <unordered_map>
#include <any>
#include <tuple>


namespace Rice::detail
Expand All @@ -1094,8 +1093,7 @@ namespace Rice::detail
Return_T lookup(VALUE klass, ID method_id);

private:
size_t key(VALUE klass, ID method_id);
std::unordered_multimap<size_t, std::tuple<VALUE, ID, std::any>> natives_ = {};
std::unordered_multimap<ID, std::pair<VALUE, std::any>> natives_ = {};
};
}

Expand All @@ -1111,34 +1109,26 @@ namespace Rice::detail

namespace Rice::detail
{
// Effective Java (2nd edition)
// https://stackoverflow.com/a/2634715
inline size_t NativeRegistry::key(VALUE klass, ID id)
{
uint32_t prime = 53;
return (prime + klass) * prime + id;
}

inline void NativeRegistry::add(VALUE klass, ID method_id, std::any callable)
{
if (rb_type(klass) == T_ICLASS)
{
klass = detail::protect(rb_class_of, klass);
}

auto range = this->natives_.equal_range(key(klass, method_id));
auto range = this->natives_.equal_range(method_id);
for (auto it = range.first; it != range.second; ++it)
{
const auto [k, m, d] = it->second;
const auto [k, d] = it->second;

if (k == klass && m == method_id)
if (k == klass)
{
std::get<2>(it->second) = callable;
std::get<1>(it->second) = callable;
return;
}
}

this->natives_.emplace(std::make_pair(key(klass, method_id), std::make_tuple(klass, method_id, callable)));
this->natives_.emplace(std::make_pair(method_id, std::make_pair(klass, callable)));
}

template <typename Return_T>
Expand All @@ -1162,12 +1152,12 @@ namespace Rice::detail
klass = detail::protect(rb_class_of, klass);
}

auto range = this->natives_.equal_range(key(klass, method_id));
auto range = this->natives_.equal_range(method_id);
for (auto it = range.first; it != range.second; ++it)
{
const auto [k, m, d] = it->second;
const auto [k, d] = it->second;

if (k == klass && m == method_id)
if (k == klass)
{
auto* ptr = std::any_cast<Return_T>(&d);
if (!ptr)
Expand Down
4 changes: 1 addition & 3 deletions rice/detail/NativeRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <unordered_map>
#include <any>
#include <tuple>

#include "ruby.hpp"

Expand All @@ -23,8 +22,7 @@ namespace Rice::detail
Return_T lookup(VALUE klass, ID method_id);

private:
size_t key(VALUE klass, ID method_id);
std::unordered_multimap<size_t, std::tuple<VALUE, ID, std::any>> natives_ = {};
std::unordered_multimap<ID, std::pair<VALUE, std::any>> natives_ = {};
};
}
#include "NativeRegistry.ipp"
Expand Down
24 changes: 8 additions & 16 deletions rice/detail/NativeRegistry.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,26 @@

namespace Rice::detail
{
// Effective Java (2nd edition)
// https://stackoverflow.com/a/2634715
inline size_t NativeRegistry::key(VALUE klass, ID id)
{
uint32_t prime = 53;
return (prime + klass) * prime + id;
}

inline void NativeRegistry::add(VALUE klass, ID method_id, std::any callable)
{
if (rb_type(klass) == T_ICLASS)
{
klass = detail::protect(rb_class_of, klass);
}

auto range = this->natives_.equal_range(key(klass, method_id));
auto range = this->natives_.equal_range(method_id);
for (auto it = range.first; it != range.second; ++it)
{
const auto [k, m, d] = it->second;
const auto [k, d] = it->second;

if (k == klass && m == method_id)
if (k == klass)
{
std::get<2>(it->second) = callable;
std::get<1>(it->second) = callable;
return;
}
}

this->natives_.emplace(std::make_pair(key(klass, method_id), std::make_tuple(klass, method_id, callable)));
this->natives_.emplace(std::make_pair(method_id, std::make_pair(klass, callable)));
}

template <typename Return_T>
Expand All @@ -61,12 +53,12 @@ namespace Rice::detail
klass = detail::protect(rb_class_of, klass);
}

auto range = this->natives_.equal_range(key(klass, method_id));
auto range = this->natives_.equal_range(method_id);
for (auto it = range.first; it != range.second; ++it)
{
const auto [k, m, d] = it->second;
const auto [k, d] = it->second;

if (k == klass && m == method_id)
if (k == klass)
{
auto* ptr = std::any_cast<Return_T>(&d);
if (!ptr)
Expand Down
Loading