Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
cookie_guard.H File Reference

RAII guards for graph node/arc cookies. More...

#include <vector>
#include <utility>
#include <tpl_graph.H>
Include dependency graph for cookie_guard.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Aleph::Cookie_Guard< GT >
 RAII guard that clears graph cookies on destruction. More...
 
class  Aleph::Cookie_Saver< GT >
 RAII guard that saves and restores graph cookies. More...
 
class  Aleph::Scope_Guard< GT, Cleanup >
 Generic RAII scope guard for cleanup operations on graphs. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 

Functions

template<class GT , class Func >
auto Aleph::with_clean_cookies (GT &g, Func &&func) -> decltype(func())
 Convenience function to run an algorithm with automatic cookie cleanup.
 
template<class GT , class Func >
auto Aleph::with_saved_cookies (GT &g, Func &&func) -> decltype(func())
 Convenience function to run an algorithm preserving existing cookies.
 
template<class GT , class Cleanup >
 Aleph::Scope_Guard (const GT &, Cleanup) -> Scope_Guard< GT, Cleanup >
 Deduction guide for Scope_Guard.
 

Detailed Description

RAII guards for graph node/arc cookies.

This file provides RAII-style guards for managing cookies in graph algorithms. Cookies are temporary void* fields in nodes and arcs used by algorithms to store intermediate data.

Two main utilities are provided:

  • Cookie_Guard: Automatically clears all cookies when the guard goes out of scope. Useful for exception safety - ensures cookies are cleaned even if an exception is thrown.
  • Cookie_Saver: Saves current cookie values and restores them when the guard goes out of scope. Useful when an algorithm needs to temporarily use cookies without destroying data from a previous algorithm.
Example - Cookie_Guard (cleanup on exit)
template <class GT>
void my_algorithm(GT & g)
{
Cookie_Guard<GT> guard(g); // will clear cookies on exit
// Use cookies for algorithm...
for_each_node(g, [](auto * p) {
NODE_COOKIE(p) = new MyData();
});
// ... algorithm logic that might throw ...
} // guard destructor clears all cookies (and optionally deletes)
#define NODE_COOKIE(p)
Return the node cookie
void for_each_node(const GT &g, std::function< void(typename GT::Node *)> operation, SN sn=SN())
Traverse all the nodes of graph filtering some ones according to a condition and executing an operati...
Definition tpl_graph.H:1238
Example - Cookie_Saver (preserve and restore)
template <class GT>
void nested_algorithm(GT & g)
{
// Another algorithm already stored data in cookies
Cookie_Saver<GT> saver(g); // saves current cookies
// Now we can use cookies freely
for_each_node(g, [](auto * p) {
NODE_COOKIE(p) = new TempData();
});
// ... do work ...
// Clean up our temporary data
for_each_node(g, [](auto * p) {
delete static_cast<TempData*>(NODE_COOKIE(p));
});
} // saver destructor restores original cookies
Author
Leandro Rabindranath León

Definition in file cookie_guard.H.