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

Retry fix for unsafe initialization in graph classes #612

Merged
merged 4 commits into from
Jul 31, 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
14 changes: 14 additions & 0 deletions Migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ Deprecated code produces compile-time warnings. These warning serve as
notification to users that their code should be upgraded. The next major
release will remove the deprecated code.

## Gazebo Math 8.X to 9.X

### Deprecations

1. **graph/Vertex.hh**
+ The `Vertex::NullVertex` static member is deprecated. Please use
`Vertex::NullVertex()` instead.
E.g.: https://github.com/gazebosim/gz-math/pull/606/files#diff-0c0220a7e72be70337975433eeddc3f5e072ade5cd80dfb1ac03da233c39c983L153-R153

1. **graph/Edge.hh**
+ The `Edge::NullEdge` static member is deprecated. Please use
`Vertex::NullEdge()` instead.
E.g.: https://github.com/gazebosim/gz-math/pull/606/files#diff-0c0220a7e72be70337975433eeddc3f5e072ade5cd80dfb1ac03da233c39c983L222-R222

## Gazebo Math 7.X to 8.X

### Breaking Changes
Expand Down
20 changes: 17 additions & 3 deletions include/gz/math/graph/Edge.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
#include <set>

#include <gz/math/config.hh>
#include "gz/math/graph/Vertex.hh"
#include <gz/math/graph/Vertex.hh>
#include <gz/utils/NeverDestroyed.hh>

namespace gz::math
{
Expand Down Expand Up @@ -203,7 +204,8 @@ namespace graph
class UndirectedEdge : public Edge<E>
{
/// \brief An invalid undirected edge.
public: static UndirectedEdge<E> NullEdge;
// Deprecated in favor of NullEdge().
public: static UndirectedEdge<E> GZ_DEPRECATED(8) NullEdge;

/// \brief Constructor.
/// \param[in] _vertices The vertices of the edge.
Expand Down Expand Up @@ -255,6 +257,7 @@ namespace graph
};

/// \brief An invalid undirected edge.
// Deprecated in favor of NullEdge().
template<typename E>
UndirectedEdge<E> UndirectedEdge<E>::NullEdge(
VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);
Expand All @@ -266,7 +269,8 @@ namespace graph
class DirectedEdge : public Edge<E>
{
/// \brief An invalid directed edge.
public: static DirectedEdge<E> NullEdge;
// Deprecated in favor of NullEdge().
public: static DirectedEdge<E> GZ_DEPRECATED(8) NullEdge;

/// \brief Constructor.
/// \param[in] _vertices The vertices of the edge.
Expand Down Expand Up @@ -330,9 +334,19 @@ namespace graph
};

/// \brief An invalid directed edge.
// Deprecated in favor of NullEdge().
template<typename E>
DirectedEdge<E> DirectedEdge<E>::NullEdge(
VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);

/// \brief An invalid edge.
template<typename E, typename EdgeType>
EdgeType &NullEdge()
{
static gz::utils::NeverDestroyed<EdgeType> e(
VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);
return e.Access();
}
} // namespace graph
} // namespace GZ_MATH_VERSION_NAMESPACE
} // namespace gz::math
Expand Down
20 changes: 10 additions & 10 deletions include/gz/math/graph/Graph.hh
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace graph
{
std::cerr << "[Graph::AddVertex()] The limit of vertices has been "
<< "reached. Ignoring vertex." << std::endl;
return Vertex<V>::NullVertex;
return NullVertex<V>();
}
}

Expand All @@ -161,7 +161,7 @@ namespace graph
{
std::cerr << "[Graph::AddVertex()] Repeated vertex [" << id << "]"
<< std::endl;
return Vertex<V>::NullVertex;
return NullVertex<V>();
}

// Link the vertex with an empty list of edges.
Expand Down Expand Up @@ -217,7 +217,7 @@ namespace graph
{
std::cerr << "[Graph::AddEdge()] The limit of edges has been reached. "
<< "Ignoring edge." << std::endl;
return EdgeType::NullEdge;
return NullEdge<E, EdgeType>();
}

EdgeType newEdge(_vertices, _data, _weight, id);
Expand All @@ -238,7 +238,7 @@ namespace graph
for (auto const &v : {edgeVertices.first, edgeVertices.second})
{
if (this->vertices.find(v) == this->vertices.end())
return EdgeType::NullEdge;
return NullEdge<E, EdgeType>();
}

// Link the new edge.
Expand Down Expand Up @@ -609,7 +609,7 @@ namespace graph
{
auto iter = this->vertices.find(_id);
if (iter == this->vertices.end())
return Vertex<V>::NullVertex;
return NullVertex<V>();

return iter->second;
}
Expand All @@ -622,7 +622,7 @@ namespace graph
{
auto iter = this->vertices.find(_id);
if (iter == this->vertices.end())
return Vertex<V>::NullVertex;
return NullVertex<V>();

return iter->second;
}
Expand All @@ -644,7 +644,7 @@ namespace graph

// Quit early if there is no adjacency entry
if (adjIt == this->adjList.end())
return EdgeType::NullEdge;
return NullEdge<E, EdgeType>();

// Loop over the edges in the source vertex's adjacency list
for (std::set<EdgeId>::const_iterator edgIt = adjIt->second.begin();
Expand All @@ -663,7 +663,7 @@ namespace graph
}
}

return EdgeType::NullEdge;
return NullEdge<E, EdgeType>();
}

/// \brief Get a reference to an edge using its Id.
Expand All @@ -674,7 +674,7 @@ namespace graph
{
auto iter = this->edges.find(_id);
if (iter == this->edges.end())
return EdgeType::NullEdge;
return NullEdge<E, EdgeType>();

return iter->second;
}
Expand All @@ -687,7 +687,7 @@ namespace graph
{
auto iter = this->edges.find(_id);
if (iter == this->edges.end())
return EdgeType::NullEdge;
return NullEdge<E, EdgeType>();

return iter->second;
}
Expand Down
15 changes: 13 additions & 2 deletions include/gz/math/graph/Vertex.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <gz/math/config.hh>
#include <gz/math/Helpers.hh>
#include <gz/utils/NeverDestroyed.hh>

namespace gz::math
{
Expand All @@ -43,7 +44,7 @@ namespace graph
using VertexId_P = std::pair<VertexId, VertexId>;

/// \brief Represents an invalid Id.
static const VertexId kNullId = MAX_UI64;
constexpr VertexId kNullId = MAX_UI64;

/// \brief A vertex of a graph. It stores user information, an optional name,
/// and keeps an internal unique Id. This class does not enforce to choose a
Expand All @@ -52,7 +53,8 @@ namespace graph
class Vertex
{
/// \brief An invalid vertex.
public: static Vertex<V> NullVertex;
// Deprecated in favor of NullVertex().
public: static Vertex<V> GZ_DEPRECATED(8) NullVertex;

/// \brief Constructor.
/// \param[in] _name Non-unique vertex name.
Expand Down Expand Up @@ -134,9 +136,18 @@ namespace graph
};

/// \brief An invalid vertex.
// Deprecated in favor of NullVertex().
template<typename V>
Vertex<V> Vertex<V>::NullVertex("__null__", V(), kNullId);

/// \brief An invalid vertex.
template<typename V>
Vertex<V> &NullVertex()
{
static gz::utils::NeverDestroyed<Vertex<V>> v("__null__", V(), kNullId);
return v.Access();
}

/// \def VertexRef_M
/// \brief Map of vertices. The key is the vertex Id. The value is a
/// reference to the vertex.
Expand Down