Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Cookie_Guard< GT > Class Template Reference

RAII guard that clears graph cookies on destruction. More...

#include <cookie_guard.H>

Collaboration diagram for Aleph::Cookie_Guard< GT >:
[legend]

Public Types

using Node = typename GT::Node
 
using Arc = typename GT::Arc
 
using Node_Deleter = std::function< void(Node *)>
 Signature for custom node cookie deleter.
 
using Arc_Deleter = std::function< void(Arc *)>
 Signature for custom arc cookie deleter.
 

Public Member Functions

 Cookie_Guard (const GT &g, bool _clear_nodes=true, bool _clear_arcs=true) noexcept
 Construct a cookie guard for the given graph.
 
 Cookie_Guard (const GT &g, Node_Deleter nd, Arc_Deleter ad=nullptr) noexcept
 Construct with custom deleters for cookie data.
 
 Cookie_Guard (const Cookie_Guard &)=delete
 Copying disabled.
 
Cookie_Guardoperator= (const Cookie_Guard &)=delete
 
 Cookie_Guard (Cookie_Guard &&other) noexcept
 Move construction transfers ownership.
 
Cookie_Guardoperator= (Cookie_Guard &&other) noexcept
 Move assignment transfers ownership.
 
void release () noexcept
 Release the guard without clearing cookies.
 
void clear_now ()
 Manually trigger cleanup now.
 
 ~Cookie_Guard ()
 Destructor - clears cookies if guard is still active.
 

Private Attributes

GTgraph
 
bool clear_nodes
 
bool clear_arcs
 
Node_Deleter node_deleter
 
Arc_Deleter arc_deleter
 
bool active
 

Detailed Description

template<class GT>
class Aleph::Cookie_Guard< GT >

RAII guard that clears graph cookies on destruction.

This class provides automatic cleanup of node and arc cookies when the guard goes out of scope. This is essential for exception safety in graph algorithms.

Template Parameters
GTGraph type (must have Node and Arc types with cookies).
Thread Safety
Not thread-safe. Each thread should use its own guard.
Author
Leandro Rabindranath León

Definition at line 119 of file cookie_guard.H.

Member Typedef Documentation

◆ Arc

Definition at line 123 of file cookie_guard.H.

◆ Arc_Deleter

template<class GT >
using Aleph::Cookie_Guard< GT >::Arc_Deleter = std::function<void(Arc*)>

Signature for custom arc cookie deleter.

Definition at line 129 of file cookie_guard.H.

◆ Node

Definition at line 122 of file cookie_guard.H.

◆ Node_Deleter

template<class GT >
using Aleph::Cookie_Guard< GT >::Node_Deleter = std::function<void(Node*)>

Signature for custom node cookie deleter.

Definition at line 126 of file cookie_guard.H.

Constructor & Destructor Documentation

◆ Cookie_Guard() [1/4]

template<class GT >
Aleph::Cookie_Guard< GT >::Cookie_Guard ( const GT g,
bool  _clear_nodes = true,
bool  _clear_arcs = true 
)
inlinenoexcept

Construct a cookie guard for the given graph.

Parameters
gGraph to guard (const_cast applied - cookies are mutable).
_clear_nodesWhether to clear node cookies (default: true).
_clear_arcsWhether to clear arc cookies (default: true).

Definition at line 146 of file cookie_guard.H.

◆ Cookie_Guard() [2/4]

template<class GT >
Aleph::Cookie_Guard< GT >::Cookie_Guard ( const GT g,
Node_Deleter  nd,
Arc_Deleter  ad = nullptr 
)
inlinenoexcept

Construct with custom deleters for cookie data.

Use this constructor when cookies point to dynamically allocated data that needs to be freed.

Parameters
gGraph to guard (const_cast applied - cookies are mutable).
ndNode deleter function (called for each node before clearing).
adArc deleter function (called for each arc before clearing).
Example
[](Node* p) { delete static_cast<MyNodeData*>(NODE_COOKIE(p)); },
[](Arc* a) { delete static_cast<MyArcData*>(ARC_COOKIE(a)); }
);
WeightedDigraph::Arc Arc
RAII guard that clears graph cookies on destruction.
typename GT::Node Node
#define ARC_COOKIE(p)
Return the arc cookie
#define NODE_COOKIE(p)
Return the node cookie
DynList< T > maps(const C &c, Op op)
Classic map operation.

Definition at line 168 of file cookie_guard.H.

◆ Cookie_Guard() [3/4]

Copying disabled.

◆ Cookie_Guard() [4/4]

template<class GT >
Aleph::Cookie_Guard< GT >::Cookie_Guard ( Cookie_Guard< GT > &&  other)
inlinenoexcept

Move construction transfers ownership.

Definition at line 178 of file cookie_guard.H.

References Aleph::maps().

◆ ~Cookie_Guard()

template<class GT >
Aleph::Cookie_Guard< GT >::~Cookie_Guard ( )
inline

Destructor - clears cookies if guard is still active.

Definition at line 248 of file cookie_guard.H.

References Aleph::Cookie_Guard< GT >::clear_now().

Referenced by Aleph::compute_cut_nodes().

Member Function Documentation

◆ clear_now()

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ release()

template<class GT >
void Aleph::Cookie_Guard< GT >::release ( )
inlinenoexcept

Release the guard without clearing cookies.

Call this if you want to keep the cookies after the guard goes out of scope (e.g., algorithm completed successfully and results are stored in cookies).

Definition at line 208 of file cookie_guard.H.

References Aleph::Cookie_Guard< GT >::active.

Referenced by Aleph::Cookie_Guard< GT >::operator=().

Member Data Documentation

◆ active

◆ arc_deleter

template<class GT >
Arc_Deleter Aleph::Cookie_Guard< GT >::arc_deleter
private

◆ clear_arcs

template<class GT >
bool Aleph::Cookie_Guard< GT >::clear_arcs
private

◆ clear_nodes

template<class GT >
bool Aleph::Cookie_Guard< GT >::clear_nodes
private

◆ graph

template<class GT >
GT& Aleph::Cookie_Guard< GT >::graph
private

Definition at line 132 of file cookie_guard.H.

Referenced by Aleph::Cookie_Guard< GT >::clear_now().

◆ node_deleter

template<class GT >
Node_Deleter Aleph::Cookie_Guard< GT >::node_deleter
private

The documentation for this class was generated from the following file: