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
20class RNGScope {
21public:
27 RNGScope(igraph_uint_t seed) : RNGScope(seed, &igraph_rngtype_pcg32) {}
28
35 RNGScope(igraph_uint_t seed, const igraph_rng_type_t* type) {
36 check_code(igraph_rng_init(&current, type));
37
38 auto errcode = igraph_rng_seed(&current, seed);
39 if (errcode != IGRAPH_SUCCESS) {
40 igraph_rng_destroy(&current);
41 throw IgraphError(errcode);
42 }
43
44 previous = *(igraph_rng_default());
45 igraph_rng_set_default(&current);
46 }
47
53 RNGScope(const igraph_rng_type_t* type) {
54 check_code(igraph_rng_init(&current, type));
55 previous = *(igraph_rng_default());
56 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:20
RNGScope(const igraph_rng_type_t *type)
Definition RNGScope.hpp:53
RNGScope()
Definition RNGScope.hpp:62
RNGScope(igraph_uint_t seed)
Definition RNGScope.hpp:27
RNGScope(igraph_uint_t seed, const igraph_rng_type_t *type)
Definition RNGScope.hpp:35
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:34