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

Replace boost::variant with std::variant in sdf #2718

Merged
merged 1 commit into from
Nov 1, 2023
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
8 changes: 4 additions & 4 deletions pxr/usd/sdf/namespaceEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
#include "pxr/base/tf/stringUtils.h"

#include <boost/ptr_container/ptr_set.hpp>
#include <boost/variant.hpp>

#include <memory>
#include <ostream>
#include <variant>

PXR_NAMESPACE_OPEN_SCOPE

Expand Down Expand Up @@ -91,7 +91,7 @@ class SdfNamespaceEdit_Namespace {
// A key for a _Node. _RootKey is for the root, SdfPath is for attribute
// connections and relationship targets, and TfToken for prim and property
// children.
typedef boost::variant<_RootKey, TfToken, SdfPath> _Key;
using _Key = std::variant<_RootKey, TfToken, SdfPath>;

struct _TargetKey {
_TargetKey(const SdfPath& path) : key(path) { }
Expand Down Expand Up @@ -161,7 +161,7 @@ class SdfNamespaceEdit_Namespace {
// Test if the node was removed. This returns true for key nodes.
bool IsRemoved() const
{
return !_parent && _key.which() != 0;
return !_parent && _key.index() != 0;
}

// Remove the node from its parent. After this call returns \c true
Expand Down Expand Up @@ -567,7 +567,7 @@ SdfNamespaceEdit_Namespace::_FixBackpointers(
for (_BackpointerMap::iterator j = i; j != n; ++j) {
for (auto node : j->second) {
node->SetKey(
boost::get<SdfPath>(node->GetKey()).
std::get<SdfPath>(node->GetKey()).
ReplacePrefix(currentPath, newPath, !fixTargetPaths));
}
}
Expand Down
45 changes: 24 additions & 21 deletions pxr/usd/sdf/parserHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
#include "pxr/base/vt/value.h"

#include <boost/numeric/conversion/cast.hpp>
#include <boost/variant.hpp>
#include <boost/variant/get.hpp>

#include <functional>
#include <limits>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>

PXR_NAMESPACE_OPEN_SCOPE
Expand All @@ -46,8 +47,8 @@ bool Sdf_BoolFromString(const std::string &, bool *parseOk);
namespace Sdf_ParserHelpers {

// Internal variant type.
typedef boost::variant<uint64_t, int64_t, double,
std::string, TfToken, SdfAssetPath> _Variant;
using _Variant = std::variant<uint64_t, int64_t, double,
std::string, TfToken, SdfAssetPath>;

////////////////////////////////////////////////////////////////////////
// Utilities that implement the Sdf_ParserHelpers::Value::Get<T>() method. The
Expand All @@ -61,7 +62,7 @@ struct _GetImpl
{
typedef const T &ResultType;
static const T &Visit(_Variant const &variant) {
return boost::get<T>(variant);
return std::get<T>(variant);
}
};

Expand All @@ -72,12 +73,11 @@ struct _GetImpl
template <class T>
struct _GetImpl<
T, std::enable_if_t<std::is_integral<T>::value>>
: public boost::static_visitor<T>
{
typedef T ResultType;

T Visit(_Variant const &variant) {
return boost::apply_visitor(*this, variant);
return std::visit(*this, variant);
}

// Fallback case: throw bad_get.
Expand Down Expand Up @@ -114,12 +114,11 @@ struct _GetImpl<
template <class T>
struct _GetImpl<
T, std::enable_if_t<std::is_floating_point<T>::value>>
: public boost::static_visitor<T>
{
typedef T ResultType;

T Visit(_Variant const &variant) {
return boost::apply_visitor(*this, variant);
return std::visit(*this, variant);
}

// Fallback case: throw bad_get.
Expand Down Expand Up @@ -167,22 +166,22 @@ struct _GetImpl<SdfAssetPath>
typedef SdfAssetPath ResultType;

SdfAssetPath Visit(_Variant const &variant) {
if (std::string const *str = boost::get<std::string>(&variant))
if (std::string const *str = std::get_if<std::string>(&variant))
return SdfAssetPath(*str);
return boost::get<SdfAssetPath>(variant);
return std::get<SdfAssetPath>(variant);
}
};

// Get a bool. Numbers are considered true if nonzero, false otherwise.
// Strings and tokens get parsed via Sdf_BoolFromString. Otherwise throw
// bad_get.
template <>
struct _GetImpl<bool> : public boost::static_visitor<bool>
struct _GetImpl<bool>
{
typedef bool ResultType;

bool Visit(_Variant const &variant) {
return boost::apply_visitor(*this, variant);
return std::visit(*this, variant);
}

// Parse string via Sdf_BoolFromString.
Expand Down Expand Up @@ -270,33 +269,37 @@ struct Value
// boost::bad_get.
template <class T>
typename _GetImpl<T>::ResultType Get() const {
return _GetImpl<T>().Visit(_variant);
try {
return _GetImpl<T>().Visit(_variant);
} catch (std::bad_variant_access& e) {
throw boost::bad_get();
}
}

// Hopefully short-lived API that applies an external visitor to the held
// variant type.
template <class Visitor>
typename Visitor::result_type
auto
ApplyVisitor(const Visitor &visitor) {
return boost::apply_visitor(visitor, _variant);
return std::visit(visitor, _variant);
}

template <class Visitor>
typename Visitor::result_type
auto
ApplyVisitor(Visitor &visitor) {
return boost::apply_visitor(visitor, _variant);
return std::visit(visitor, _variant);
}

template <class Visitor>
typename Visitor::result_type
auto
ApplyVisitor(const Visitor &visitor) const {
return _variant.apply_visitor(visitor);
return std::visit(visitor, _variant);
}

template <class Visitor>
typename Visitor::result_type
auto
ApplyVisitor(Visitor &visitor) const {
return _variant.apply_visitor(visitor);
return std::visit(visitor, _variant);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion pxr/usd/sdf/parserValueContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

PXR_NAMESPACE_OPEN_SCOPE

struct Sdf_ToStringVisitor : boost::static_visitor<std::string>
struct Sdf_ToStringVisitor
{
template <typename T>
std::string operator () (const T &value)
Expand Down
Loading