-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implemented array_wrapper for type erasure * Added missing methods and members in value_ptr * Plugged array_wrapper * Removed class array_base * operator== workaround implementation to avoid OSX bug * Specialized basic_common_reference of nullable * Workaround OSX issue * Fixed iterator_tag of layouts
- Loading branch information
1 parent
ffc06fb
commit 2148ad2
Showing
25 changed files
with
635 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// Copyright 2024 Man Group Operations Limited | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or mplied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <variant> | ||
|
||
#include "sparrow/types/data_traits.hpp" | ||
#include "sparrow/utils/memory.hpp" | ||
|
||
namespace sparrow | ||
{ | ||
/** | ||
* Base class for array type erasure | ||
*/ | ||
class array_wrapper | ||
{ | ||
public: | ||
|
||
using wrapper_ptr = std::unique_ptr<array_wrapper>; | ||
|
||
virtual ~array_wrapper() = default; | ||
|
||
array_wrapper(array_wrapper&&) = delete; | ||
array_wrapper& operator=(const array_wrapper&) = delete; | ||
array_wrapper& operator=(array_wrapper&&) = delete; | ||
|
||
wrapper_ptr clone() const; | ||
|
||
enum data_type data_type() const; | ||
|
||
protected: | ||
|
||
array_wrapper(enum data_type dt); | ||
array_wrapper(const array_wrapper&) = default; | ||
|
||
private: | ||
|
||
enum data_type m_data_type; | ||
virtual wrapper_ptr clone_impl() const = 0; | ||
}; | ||
|
||
template <class T> | ||
class array_wrapper_impl : public array_wrapper | ||
{ | ||
public: | ||
|
||
array_wrapper_impl(T&& ar); | ||
array_wrapper_impl(T* ar); | ||
array_wrapper_impl(std::shared_ptr<T> ar); | ||
|
||
virtual ~array_wrapper_impl() = default; | ||
|
||
T& get_wrapped(); | ||
const T& get_wrapped() const; | ||
|
||
private: | ||
|
||
using wrapper_ptr = array_wrapper::wrapper_ptr; | ||
|
||
constexpr enum data_type get_data_type() const; | ||
|
||
array_wrapper_impl(const array_wrapper_impl&); | ||
wrapper_ptr clone_impl() const override; | ||
|
||
using storage_type = std::variant<value_ptr<T>, std::shared_ptr<T>, T*>; | ||
storage_type m_storage; | ||
T* p_array; | ||
}; | ||
|
||
template <class T> | ||
T& unwrap_array(array_wrapper&); | ||
|
||
template <class T> | ||
const T& unwrap_array(const array_wrapper&); | ||
|
||
/******************************** | ||
* array_wrapper implementation * | ||
********************************/ | ||
|
||
inline auto array_wrapper::clone() const -> wrapper_ptr | ||
{ | ||
return clone_impl(); | ||
} | ||
|
||
inline enum data_type array_wrapper::data_type() const | ||
{ | ||
return m_data_type; | ||
} | ||
|
||
inline array_wrapper::array_wrapper(enum data_type dt) | ||
: m_data_type(dt) | ||
{ | ||
} | ||
|
||
/************************************* | ||
* array_wrapper_impl implementation * | ||
*************************************/ | ||
|
||
template <class T> | ||
array_wrapper_impl<T>::array_wrapper_impl(T&& ar) | ||
: array_wrapper(this->get_data_type()) | ||
, m_storage(value_ptr<T>(std::move(ar))) | ||
, p_array(std::get<value_ptr<T>>(m_storage).get()) | ||
{ | ||
} | ||
|
||
template <class T> | ||
array_wrapper_impl<T>::array_wrapper_impl(T* ar) | ||
: array_wrapper(this->get_data_type()) | ||
, m_storage(ar) | ||
, p_array(ar) | ||
{ | ||
} | ||
|
||
template <class T> | ||
array_wrapper_impl<T>::array_wrapper_impl(std::shared_ptr<T> ar) | ||
: array_wrapper(this->get_data_type()) | ||
, m_storage(ar) | ||
, p_array(ar.get()) | ||
{ | ||
} | ||
|
||
template <class T> | ||
T& array_wrapper_impl<T>::get_wrapped() | ||
{ | ||
return *p_array; | ||
} | ||
|
||
template <class T> | ||
const T& array_wrapper_impl<T>::get_wrapped() const | ||
{ | ||
return *p_array; | ||
} | ||
|
||
template <class T> | ||
constexpr enum data_type array_wrapper_impl<T>::get_data_type() const | ||
{ | ||
return arrow_traits<typename T::inner_value_type>::type_id; | ||
} | ||
|
||
template <class T> | ||
array_wrapper_impl<T>::array_wrapper_impl(const array_wrapper_impl& rhs) | ||
: array_wrapper(rhs) | ||
, m_storage(rhs.m_storage) | ||
{ | ||
p_array = std::visit([](auto&& arg) | ||
{ | ||
using U = std::decay_t<decltype(arg)>; | ||
if constexpr (std::is_same_v<U, T*>) | ||
return arg; | ||
else | ||
return arg.get(); | ||
}, m_storage); | ||
} | ||
|
||
template <class T> | ||
auto array_wrapper_impl<T>::clone_impl() const -> wrapper_ptr | ||
{ | ||
return wrapper_ptr{new array_wrapper_impl<T>(*this)}; | ||
} | ||
|
||
template <class T> | ||
T& unwrap_array(array_wrapper& ar) | ||
{ | ||
return static_cast<array_wrapper_impl<T>&>(ar).get_wrapped(); | ||
} | ||
|
||
template <class T> | ||
const T& unwrap_array(const array_wrapper& ar) | ||
{ | ||
return static_cast<const array_wrapper_impl<T>&>(ar).get_wrapped(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.