Skip to content

Commit

Permalink
Merge pull request #346 from NWChemEx/mac_debug
Browse files Browse the repository at this point in the history
Mac debug
  • Loading branch information
jwaldrop107 authored May 3, 2024
2 parents 60d8e95 + 551bb2f commit 5900642
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
9 changes: 6 additions & 3 deletions include/pluginplay/any/detail_/any_field_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

#pragma once
#include <any>
#include <boost/any.hpp>
#include <exception>
#include <memory>
#include <ostream>
Expand Down Expand Up @@ -43,6 +43,9 @@ class AnyFieldBase {
/// A read-only reference to a Python object
using const_python_reference = const python_value&;

/// The type used to store the value
using value_type = boost::any;

/** @brief Polymorphic copy
*
* This method returns a pointer to a newly allocated AnyFieldBase instance
Expand Down Expand Up @@ -307,7 +310,7 @@ class AnyFieldBase {
protected:
/// AnyWrappers are always created via AnyResultWrapper/AnyInputWrapper
///@{
AnyFieldBase(std::any da_any) : m_value_(std::move(da_any)) {}
AnyFieldBase(value_type da_any) : m_value_(std::move(da_any)) {}
AnyFieldBase(const AnyFieldBase& other) = delete;
///@}

Expand Down Expand Up @@ -351,7 +354,7 @@ class AnyFieldBase {
void error_if_not_convertible_() const;

/// The type-erased value
std::any m_value_;
value_type m_value_;
};

} // namespace pluginplay::any::detail_
Expand Down
20 changes: 10 additions & 10 deletions include/pluginplay/any/detail_/any_field_base.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ T AnyFieldBase::cast() {
using clean_type = std::decay_t<T>;

if(storing_python_object()) {
auto& py_obj = std::any_cast<python_reference>(m_value_);
auto& py_obj = boost::any_cast<python_reference>(m_value_);
return py_obj.unwrap<T>();
}

Expand All @@ -39,12 +39,12 @@ T AnyFieldBase::cast() {
if(storing_const_reference()) {
// This is the only possible way we use a reference wrapper
using ref_type = std::reference_wrapper<const clean_type>;
return std::any_cast<ref_type>(m_value_).get();
return boost::any_cast<ref_type>(m_value_).get();
}
} // else: assert_convertible will catch by_mutable_ref and
// storing_const_ref

return std::any_cast<T>(m_value_);
return boost::any_cast<T>(m_value_);
}

template<typename T>
Expand All @@ -54,7 +54,7 @@ T AnyFieldBase::cast() const {
using clean_type = std::decay_t<T>;

if(storing_python_object()) {
const auto& py_obj = std::any_cast<const_python_reference>(m_value_);
const auto& py_obj = boost::any_cast<const_python_reference>(m_value_);
return py_obj.unwrap<T>();
}

Expand All @@ -68,9 +68,9 @@ T AnyFieldBase::cast() const {

if(storing_const_reference()) {
using ref_type = std::reference_wrapper<const clean_type>;
return std::any_cast<ref_type>(m_value_).get();
return boost::any_cast<ref_type>(m_value_).get();
}
return std::any_cast<T>(m_value_);
return boost::any_cast<T>(m_value_);
}

template<typename T>
Expand All @@ -92,7 +92,7 @@ bool AnyFieldBase::is_convertible() noexcept {

// Getting here means it's stored by mutable value and we can return it
// however the user wants (as long as the object's actually that type...)
return std::any_cast<clean_type>(&m_value_) != nullptr;
return boost::any_cast<clean_type>(&m_value_) != nullptr;
}

template<typename T>
Expand All @@ -101,7 +101,7 @@ bool AnyFieldBase::is_convertible() const noexcept {

// Trying to convert a Python object to a C++ object
if(storing_python_object()) {
const auto& py_obj = std::any_cast<const_python_reference>(m_value_);
const auto& py_obj = boost::any_cast<const_python_reference>(m_value_);
return py_obj.is_convertible<T>();
}

Expand All @@ -125,10 +125,10 @@ bool AnyFieldBase::is_convertible() const noexcept {
// before comparing
if(storing_const_reference()) {
using ref_type = std::reference_wrapper<const clean_type>;
return std::any_cast<ref_type>(&m_value_) != nullptr;
return boost::any_cast<ref_type>(&m_value_) != nullptr;
}
// Otherwise just compare them
return std::any_cast<clean_type>(&m_value_) != nullptr;
return boost::any_cast<clean_type>(&m_value_) != nullptr;
} else {
return false;
}
Expand Down
13 changes: 8 additions & 5 deletions include/pluginplay/any/detail_/any_field_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ class AnyFieldWrapper : public AnyFieldBase {
/// Type used to wrap Python objects
using typename base_type::python_value;

/// This is the type of the object actually in the std::any
/// Type used to store values
using typename base_type::value_type;

/// This is the type of the object actually in the any
using wrapped_type = std::conditional_t<wrap_const_ref_v, ref_wrapper_t, T>;

/** @brief Makes an AnyFieldWrapper by wrapping the provided value.
Expand All @@ -93,11 +96,11 @@ class AnyFieldWrapper : public AnyFieldBase {
* alive for the lifetime of the AnyFieldWrapper.
*
* N.B. if @p U is not an implicityly convertible to type @p T you will get
* a compiler error saying there is no valid std::any ctor for @p U.
* a compiler error saying there is no valid any ctor for @p U.
*
* @param[in] value2wrap The object being passed as an input.
*
* @throw ??? If wrapping @p value2wrap in a std::any throws. Same throw
* @throw ??? If wrapping @p value2wrap in an any throws. Same throw
* guarantee.
*/
template<typename U,
Expand Down Expand Up @@ -145,9 +148,9 @@ class AnyFieldWrapper : public AnyFieldBase {
/// Implements storing_python_object
bool storing_python_object_() const noexcept override;

/// Code factorization for wrapping an object of type @p U in a std::any
/// Code factorization for wrapping an object of type @p U in an any
template<typename U>
std::any wrap_value_(U&& value2wrap) const;
value_type wrap_value_(U&& value2wrap) const;
};

} // namespace pluginplay::any::detail_
Expand Down
5 changes: 3 additions & 2 deletions include/pluginplay/any/detail_/any_field_wrapper.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ bool ANY_FIELD_WRAPPER::storing_python_object_() const noexcept {

TEMPLATE_PARAMS
template<typename U>
std::any ANY_FIELD_WRAPPER::wrap_value_(U&& value2wrap) const {
return std::make_any<wrapped_type>(std::forward<U>(value2wrap));
typename ANY_FIELD_WRAPPER::value_type ANY_FIELD_WRAPPER::wrap_value_(
U&& value2wrap) const {
return boost::any(std::move(wrapped_type(std::forward<U>(value2wrap))));
}

#undef ANY_FIELD_WRAPPER
Expand Down
36 changes: 19 additions & 17 deletions include/pluginplay/python/python_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

#pragma once
#include <any>
#include <boost/any.hpp>
#include <stdexcept>
#ifdef BUILD_PYBIND11
#include <memory>
Expand All @@ -30,14 +30,14 @@ namespace pluginplay::python {
* the box, we just need a few redirections. The PythonWrapper class wraps a
* pybind11 Python object and gives it the expected API.
*
* @note We actually hold the pybind11 object in a std::any. This is because
* @note We actually hold the pybind11 object in an any. This is because
* Pybind11 has hidden symbols by default an trying to directly hold the
* Pybind11 object leads to compiler warnings.
*/
class PythonWrapper {
public:
/// How we hold the object internally
using value_type = std::any;
using value_type = boost::any;

/// The type of a Python object, as seen from C++
using py_object_type = pybind11::object;
Expand All @@ -56,11 +56,10 @@ class PythonWrapper {
*
* @param[in] py_value The Python object to wrap
*
* @throw ??? if making the internal std::any fails. Strong throw
* @throw ??? if making the internal any fails. Strong throw
* guarantee.
*/
explicit PythonWrapper(py_object_type py_value) :
m_value_(std::make_any<py_object_type>(py_value)) {}
explicit PythonWrapper(py_object_type py_value) : m_value_(py_value) {}

/** @brief Determines whether or not the Python object *this was
* constructed with actually holds a value.
Expand Down Expand Up @@ -179,15 +178,15 @@ class PythonWrapper {
}

private:
/// Code factorization for unwrapping the mutable std::any
py_reference unwrap_() { return std::any_cast<py_reference>(m_value_); }
/// Code factorization for unwrapping the mutable any
py_reference unwrap_() { return boost::any_cast<py_reference>(m_value_); }

/// Code factorization for unwrapping the read-only std::any
/// Code factorization for unwrapping the read-only any
const_py_reference unwrap_() const {
return std::any_cast<const_py_reference>(m_value_);
return boost::any_cast<const_py_reference>(m_value_);
}

/// The actual Python object, held in a std::any
/// The actual Python object, held in an any
value_type m_value_;

/// A buffer for holding the newly created C++ object
Expand Down Expand Up @@ -306,11 +305,11 @@ T PythonWrapper::unwrap() {
}
// User wants the converted C++ value by reference or const reference
else {
if(!m_buffer_.has_value()) {
if(m_buffer_.empty()) {
auto cxx_value = py_value.cast<clean_type>();
m_buffer_ = std::make_any<clean_type>(std::move(cxx_value));
m_buffer_ = std::move(cxx_value);
}
return std::any_cast<T>(m_buffer_);
return boost::any_cast<T>(m_buffer_);
}
}

Expand Down Expand Up @@ -340,6 +339,9 @@ namespace pluginplay::python {
*/
class PythonWrapper {
public:
/// How we hold the object internally
using value_type = boost::any;

PythonWrapper() { error_(); }

template<typename T>
Expand All @@ -350,13 +352,13 @@ class PythonWrapper {
template<typename T>
T unwrap() {
error_();
return std::any_cast<T>(m_value_); // Won't actually get here...
return boost::any_cast<T>(m_value_); // Won't actually get here...
}

template<typename T>
T unwrap() const {
error_();
return std::any_cast<T>(m_value_); // Won't actually get here...
return boost::any_cast<T>(m_value_); // Won't actually get here...
}

template<typename T>
Expand All @@ -369,7 +371,7 @@ class PythonWrapper {
bool operator!=(const PythonWrapper& rhs) { return false; }

private:
std::any m_value_;
value_type m_value_;
void error_() const {
throw std::runtime_error("PluginPlay was not configured with Pybind11 "
"support");
Expand Down

0 comments on commit 5900642

Please sign in to comment.