Skip to content

Commit

Permalink
Merge pull request #210 from ankane/ubuntu-20-04-fix
Browse files Browse the repository at this point in the history
Fix compilation on Ubuntu 20.04
  • Loading branch information
jasonroelofs authored Oct 19, 2024
2 parents c7cfc2c + 317e9c2 commit 6b910e1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 38 deletions.
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

0 comments on commit 6b910e1

Please sign in to comment.