raiigraph
C++ RAII for igraph data structures
Loading...
Searching...
No Matches
RNGScope.hpp
Go to the documentation of this file.
1#ifndef RAIIGRAPH_RNG_SCOPE_HPP
2#define RAIIGRAPH_RNG_SCOPE_HPP
3
4#include "igraph.h"
5#include "error.hpp"
6
12namespace raiigraph {
13
22class RNGScope {
23public:
29 RNGScope(igraph_uint_t seed) : RNGScope(seed, &igraph_rngtype_pcg32) {}
30
37 RNGScope(igraph_uint_t seed, const igraph_rng_type_t* type) {
38 check_code(igraph_rng_init(&current, type));
39
40 auto errcode = igraph_rng_seed(&current, seed);
41 if (errcode != IGRAPH_SUCCESS) {
42 igraph_rng_destroy(&current);
43 throw IgraphError(errcode);
44 }
45
46 previous = igraph_rng_set_default(&current);
47 }
48
54 RNGScope(const igraph_rng_type_t* type) {
55 check_code(igraph_rng_init(&current, type));
56 previous = igraph_rng_set_default(&current);
57 }
58
62 RNGScope() : RNGScope(&igraph_rngtype_pcg32) {}
63
64public:
68 // We shouldn't be copying or moving an RNGscope object, as this defeats the logic provided by scoping.
69 RNGScope(const RNGScope&) = delete;
70 RNGScope& operator=(const RNGScope&) = delete;
71 RNGScope(RNGScope&&) = delete;
72 RNGScope& operator=(RNGScope&&) = delete;
73
74 ~RNGScope() {
75 igraph_rng_set_default(previous);
76 igraph_rng_destroy(&current);
77 }
82private:
83 igraph_rng_t* previous;
84 igraph_rng_t current;
85};
86
87}
88
89#endif
Error class for igraph-related errors.
Definition error.hpp:16
Control the igraph RNG via RAII.
Definition RNGScope.hpp:22
RNGScope(const igraph_rng_type_t *type)
Definition RNGScope.hpp:54
RNGScope()
Definition RNGScope.hpp:62
RNGScope(igraph_uint_t seed)
Definition RNGScope.hpp:29
RNGScope(igraph_uint_t seed, const igraph_rng_type_t *type)
Definition RNGScope.hpp:37
Error handling for raiigraph.
Utilities for manipulating igraph data structures in C++.
Definition error.hpp:11
void check_code(igraph_error_t code)
Definition error.hpp:39