├── LICENSE
├── README.md
├── docs
├── index.rst
└── rrgen_logo.png
├── examples
└── examples.cc
└── include
└── rrgen.hpp
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Josh Schiavone
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # rrgen - A Header Only C++ Library For Storing Safe, Pseudo-randomly Generated Data Into Modern Containers
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | ## About
10 | This library was developed to combat insecure methods of storing random data into modern C++ containers. For example, old and clunky PRNGs. Thus, rrgen uses STL's distribution engines in order to efficiently and safely store a random number distribution into a given C++ container.
11 |
12 | ## Installation
13 | 1) ``git clone https://github.com/josh0xA/rrgen.git``
14 | 2) ``cd rrgen``
15 | 3) ``make``
16 | 4) Add ``include/rrgen.hpp`` to your project tree for access to the library classes and functions.
17 |
18 | ## Official Documentation
19 | *rrgen/docs/index.rst*
20 |
21 | ## Supported Containers
22 | 1) ``std::vector<>``
23 | 2) ``std::list<>``
24 | 3) ``std::array<>``
25 | 4) ``std::stack<>``
26 |
27 | ## Example Usages
28 | ```cpp
29 | #include "../include/rrgen.hpp"
30 | #include
31 |
32 | int main(void)
33 | {
34 | // Example usage for rrgen vector
35 | rrgen::rrand rrvec;
36 | rrvec.gen_rrvector(false, true, 0, 10);
37 | for (auto &i : rrvec.contents())
38 | {
39 | std::cout << i << " ";
40 | } // ^ the same as rrvec.show_contents()
41 |
42 | // Example usage for rrgen list (frontside insertion)
43 | rrgen::rrand rrlist;
44 | rrlist.gen_rrlist(false, true, "fside", 5, 25);
45 | std::cout << '\n'; rrlist.show_contents();
46 | std::cout << "Size: " << rrlist.contents().size() << '\n';
47 |
48 | // Example usage for rrgen array
49 | rrgen::rrand_array rrarr;
50 | rrarr.gen_rrarray(false, true, 5, 35);
51 | for (auto &i : rrarr.contents())
52 | {
53 | std::cout << i << " ";
54 | } // ^ the same as rrarr.show_contents()
55 |
56 | // Example usage for rrgen stack
57 | rrgen::rrand_stack rrstack;
58 | rrstack.gen_rrstack(false, true, 200, 1000);
59 | for (auto m = rrstack.xsize(); m > 0; m--)
60 | {
61 | std::cout << rrstack.grab_top() << " ";
62 | rrstack.pop_off();
63 | if (m == 1) { std::cout << '\n'; }
64 | }
65 | }
66 | ```
67 | Note: This is a transferred repository, from a completely unrelated project.
68 |
69 | ## License
70 | MIT License
71 | Copyright (c) Josh Schiavone
72 |
--------------------------------------------------------------------------------
/docs/index.rst:
--------------------------------------------------------------------------------
1 | rrgen - A Header Only C++ Library For Storing Safe, Randomly Generated Data Into Modern Containers
2 | ==================================================================================================
3 |
4 | About
5 | =====
6 | This library was developed to combat unsecure methods of storing random data into modern C++ containers.
7 |
8 | Documentation
9 | =============
10 | Namespace: rrgen
11 |
12 |
13 | Structures
14 |
15 | struct __modes__: Library structure for setting modes for list generations through std::random engines.
16 |
17 | Classes
18 | Class: rrand
19 |
20 | The rrand class provides tools for generating random data and storing it in vectors, lists, and arrays.
21 | Methods
22 |
23 | | gen_rrvector(bool numeric, bool minmax, rand_type __min = 0, rand_type __max = 100): Randomly generates data and stores it in a vector.
24 | | gen_rrlist(bool numeric, bool minmax, const std::string& direction, rand_type __min = 0, rand_type __max = 100): Randomly generates data and stores it in a list.
25 | | contents() const: Returns the data container.
26 | | show_contents() const: Prints the data stored in the container.
27 | | xsize() const: Returns the size of the container.
28 | | delete_contents(): Deletes all stored data in the container.
29 |
30 | Class: rrand_array
31 |
32 | The rrand_array class inherits from rrand and provides additional tools for generating random data in arrays.
33 | Methods
34 |
35 | | gen_rrarray(bool numeric, bool minmax, rand_type __min = 0, rand_type __max = 100): Generates random uniform data and stores values in an array.
36 | | contents() const: Returns the array container.
37 | | show_contents() const: Iterates array contents and outputs the stored data.
38 | | xsize() const: Returns the size of the array container.
39 |
40 | Class: rrand_stack
41 |
42 | The rrand_stack class inherits from rrand_array and provides additional tools for generating random data in stacks.
43 | Methods
44 |
45 | | gen_rrstack(bool numeric, bool minmax, rand_type __min = 0, rand_type __max = 100): Generates random uniform data and stores values in a stack.
46 | | contents() const: Returns the stack container.
47 | | is_empty() const noexcept: Returns true if the stack is empty.
48 | | grab_top() const: Returns the element at the top of the stack.
49 | | push_top(rand_type value): Pushes an element to the top of the stack.
50 | | xsize() const: Returns the size of the stack container.
51 | | pop_off(): Pops the top element off the stack.
52 |
53 | Namespace: exception
54 | Class: rrgen_except
55 |
56 | The rrgen_except class inherits from std::exception and is used to throw exceptions instead of using std::cerr.
57 | Methods
58 |
59 | what() const noexcept: Returns the error message.
--------------------------------------------------------------------------------
/docs/rrgen_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/josh0xA/rrgen/621b7b875fa30f4aea44c1788da856381fa7f6e3/docs/rrgen_logo.png
--------------------------------------------------------------------------------
/examples/examples.cc:
--------------------------------------------------------------------------------
1 | #include "../include/rrgen.hpp"
2 | #include
3 |
4 | int main(void)
5 | {
6 | // Example usage for rrgen vector
7 | rrgen::rrand rrvec;
8 | rrvec.gen_rrvector(false, true, 0, 10);
9 | for (auto &i : rrvec.contents())
10 | {
11 | std::cout << i << " ";
12 | } // ^ the same as rrvec.show_contents()
13 |
14 | // Example usage for rrgen list (frontside insertion)
15 | rrgen::rrand rrlist;
16 | rrlist.gen_rrlist(false, true, "fside", 5, 25);
17 | std::cout << '\n'; rrlist.show_contents();
18 | std::cout << "Size: " << rrlist.contents().size() << '\n';
19 |
20 | // Example usage for rrgen array
21 | rrgen::rrand_array rrarr;
22 | rrarr.gen_rrarray(false, true, 5, 35);
23 | for (auto &i : rrarr.contents())
24 | {
25 | std::cout << i << " ";
26 | } // ^ the same as rrarr.show_contents()
27 |
28 | // Example usage for rrgen stack
29 | rrgen::rrand_stack rrstack;
30 | rrstack.gen_rrstack(false, true, 200, 1000);
31 | for (auto m = rrstack.xsize(); m > 0; m--)
32 | {
33 | std::cout << rrstack.grab_top() << " ";
34 | rrstack.pop_off();
35 | if (m == 1) { std::cout << '\n'; }
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/include/rrgen.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2024 Josh Schiavone
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 |
25 | #ifndef RRGEN_HPP
26 | #define RRGEN_HPP
27 |
28 | #include
29 | #include
30 | #include
31 | #include
32 |
33 | #include
34 | #include
35 | #include
36 | #include
37 | #include
38 |
39 | #define RRGEN_ERROR_CODE_STANDARD -1
40 | #define RRGEN_SUCCESS_CODE_STANDARD 0
41 |
42 | /**
43 | * @brief debugging preprocessor - set a fatal type to a debug
44 | * value
45 | */
46 | #define RRGEN_SET_VALUE(k_value, s_value) ((k_value) = (s_value))
47 |
48 | typedef const char __cchar;
49 |
50 | namespace rrgen {
51 | /**
52 | * @brief For universal conditional use if true
53 | * @param bool, value to test
54 | * @return bool
55 | */
56 | inline bool rrgen_success_on_return_value(bool __svalue) {
57 |
58 | return (__svalue == true);
59 | }
60 |
61 | inline bool rrgen_error_on_return_value(bool __svalue) {
62 | /**
63 | * @brief For universal conditional use if false
64 | * @param bool, value to test
65 | * @return bool
66 | */
67 | return (__svalue == false);
68 | }
69 |
70 | typedef struct __modes__ {
71 | /**
72 | * @brief Library structure for setting modes for list generations
73 | * through std::random engines
74 | */
75 | __cchar* RRGEN_FRONTSIDE_MODE = "fside";
76 | __cchar* RRGEN_BACKSIDE_MODE = "bside";
77 | } modes, *pmodes;
78 |
79 | /**
80 | * @brief Nested template class rrand.
81 | * @class Usage for std::vector and std::list generations
82 | * @format rrand, std::size_t>
83 | */
84 | template class Arg, std::size_t __datasize>
85 | class rrand {
86 | protected:
87 | template
88 | std::enable_if_t, std::uniform_int_distribution>
89 | gen_random_data_numeric(const std::mt19937 __mtgenerator) {
90 | std::uniform_int_distribution distribution(
91 | std::numeric_limits::min(),
92 | std::numeric_limits::max());
93 | return distribution;
94 | }
95 |
96 | template
97 | std::enable_if_t, std::uniform_real_distribution>
98 | gen_random_data_numeric(const std::mt19937 __mtgenerator) {
99 | std::uniform_real_distribution distribution(
100 | std::numeric_limits::min(),
101 | std::numeric_limits::max());
102 | return distribution;
103 | }
104 |
105 | template
106 | std::enable_if_t, std::uniform_int_distribution>
107 | gen_random_data_mm(const std::mt19937 __mtgenerator, T __min, T __max) {
108 | std::uniform_int_distribution distribution(__min, __max);
109 | return distribution;
110 | }
111 |
112 | template
113 | std::enable_if_t, std::uniform_real_distribution>
114 | gen_random_data_mm(const std::mt19937 __mtgenerator, T __min, T __max) {
115 | std::uniform_real_distribution distribution(__min, __max);
116 | return distribution;
117 | }
118 |
119 | modes mds;
120 | public:
121 | /**
122 | * @brief Randomly generates data and stores the values into a vector
123 | * @param bool, must be true in order to generate data
124 | * @return Arg> - data container
125 | */
126 | Arg> gen_rrvector(bool numeric, bool minmax,
127 | rand_type __min = 0, rand_type __max = 100) {
128 | std::mt19937 __mtgenerator(__device());
129 | if (rrgen_success_on_return_value(numeric)) {
130 | auto dist = gen_random_data_numeric(__mtgenerator);
131 | for (auto elem = RRGEN_SUCCESS_CODE_STANDARD; elem < __datasize; ++elem) {
132 | struct_container.push_back(dist(__mtgenerator));
133 | }
134 | return struct_container;
135 | } else {
136 | if (rrgen_success_on_return_value(minmax)) {
137 | auto dist = gen_random_data_mm(__mtgenerator, __min, __max);
138 | for (auto elem = RRGEN_SUCCESS_CODE_STANDARD; elem < __datasize; ++elem) {
139 | struct_container.push_back(dist(__mtgenerator));
140 | }
141 | return struct_container;
142 | } else { return struct_container; }
143 | }
144 | }
145 | /**
146 | * @brief Randomly generates data and stores the values into a list
147 | * @param bool - must be true in order to generate data, std::string - push
148 | * direction
149 | * @return Arg> - data container
150 | */
151 | Arg> gen_rrlist(bool numeric, bool minmax,
152 | const std::string& direction, rand_type __min = 0, rand_type __max = 100) {
153 | std::mt19937 __mtgenerator(__device());
154 | if (rrgen_success_on_return_value(numeric)) {
155 | auto dist = gen_random_data_numeric(__mtgenerator);
156 | if (rrgen_success_on_return_value(direction.compare(mds.RRGEN_FRONTSIDE_MODE))) {
157 | for (auto elem = RRGEN_SUCCESS_CODE_STANDARD; elem < __datasize; ++elem) {
158 | struct_container.push_front(dist(__mtgenerator));
159 | }
160 | }
161 | if (rrgen_success_on_return_value(direction.compare(mds.RRGEN_BACKSIDE_MODE))) {
162 | for (auto elem = RRGEN_SUCCESS_CODE_STANDARD; elem < __datasize; ++elem) {
163 | struct_container.push_back(dist(__mtgenerator));
164 | }
165 | }
166 | return struct_container;
167 | } else {
168 | if (rrgen_success_on_return_value(minmax)) {
169 | auto dist = gen_random_data_mm(__mtgenerator, __min, __max);
170 | if (rrgen_success_on_return_value(direction.compare(mds.RRGEN_FRONTSIDE_MODE))) {
171 | for (auto elem = RRGEN_SUCCESS_CODE_STANDARD; elem < __datasize; ++elem) {
172 | struct_container.push_front(dist(__mtgenerator));
173 | }
174 | }
175 | if (rrgen_success_on_return_value(direction.compare(mds.RRGEN_BACKSIDE_MODE))) {
176 | for (auto elem = RRGEN_SUCCESS_CODE_STANDARD; elem < __datasize; ++elem) {
177 | struct_container.push_back(dist(__mtgenerator));
178 | }
179 | }
180 | return struct_container;
181 | } else { return struct_container; }
182 | }
183 | }
184 |
185 | Arg> contents() const {
186 | return struct_container;
187 | }
188 |
189 | rand_type show_contents() const {
190 | /**
191 | * @brief Prints the data stored in the container
192 | * @param void
193 | * @return typename (rand_type)
194 | */
195 | for (const rand_type& content : struct_container) {
196 | std::cout << content << " ";
197 | }
198 | }
199 |
200 | rand_type xsize() const {
201 | /**
202 | * @brief Returns the size of the container
203 | * @param void
204 | * @return typename (rand_type)
205 | */
206 | return struct_container.size();
207 | }
208 |
209 | void delete_contents() {
210 | /**
211 | * @brief Deletes all of the stored data in the container
212 | * @param void
213 | * @return void
214 | */
215 | struct_container.clear();
216 | }
217 |
218 | private:
219 | Arg> struct_container;
220 | std::random_device __device;
221 | }; // class: rrand
222 |
223 | /**
224 | * @brief Template class rrand_array
225 | * @class Usage for generating random uniform data for std::array
226 | * @format rrand_array
227 | */
228 | template
229 | class rrand_array {
230 | protected:
231 | /**
232 | * @brief Creates a uniform distribution of random numbers in the range of
233 | * std::numeric_limits::min() -> std::numeric_limits::max()
234 | * @param std::mt19937, uniform generation engine
235 | * @return std::unirom_int_distribution
236 | */
237 | template
238 | std::enable_if_t, std::uniform_int_distribution>
239 | gen_random_data(const std::mt19937 __mtgenerator) {
240 | std::uniform_int_distribution distribution(
241 | std::numeric_limits::min(),
242 | std::numeric_limits::max());
243 | return distribution;
244 | }
245 |
246 | template
247 | std::enable_if_t, std::uniform_real_distribution>
248 | gen_random_data(const std::mt19937 __mtgenerator) {
249 | std::uniform_real_distribution distribution(
250 | std::numeric_limits::min(),
251 | std::numeric_limits::max());
252 | return distribution;
253 | }
254 |
255 | template
256 | std::enable_if_t, std::uniform_int_distribution>
257 | gen_random_data_mm(const std::mt19937 __mtgenerator, T __min, T __max) {
258 | std::uniform_int_distribution distribution(__min, __max);
259 | return distribution;
260 | }
261 |
262 | template
263 | std::enable_if_t, std::uniform_real_distribution>
264 | gen_random_data_mm(const std::mt19937 __mtgenerator, T __min, T __max) {
265 | std::uniform_real_distribution distribution(__min, __max);
266 | return distribution;
267 | }
268 |
269 | public:
270 | /**
271 | * @brief Generates random uniform data and stores values into std::array
272 | * container
273 | * @param bool, must be true in order for generation to occur
274 | * @return std::array
275 | */
276 | std::array gen_rrarray(bool numeric, bool minmax, rand_type __min = 0, rand_type __max = 100) {
277 | std::mt19937 __mtgenerator(__device());
278 | if (rrgen_success_on_return_value(numeric)) {
279 | auto dist = gen_random_data(__mtgenerator);
280 | for (auto elem = RRGEN_SUCCESS_CODE_STANDARD; elem < __datasize; ++elem) {
281 | array_container.at(elem) = dist(__mtgenerator);
282 | }
283 | return array_container;
284 | } else {
285 | if (rrgen_success_on_return_value(minmax)) {
286 | auto dist = gen_random_data_mm(__mtgenerator, __min, __max);
287 | for (auto elem = RRGEN_SUCCESS_CODE_STANDARD; elem < __datasize; ++elem) {
288 | array_container.at(elem) = dist(__mtgenerator);
289 | }
290 | return array_container;
291 | } else { return array_container; }
292 | }
293 | }
294 |
295 | std::array contents() const {
296 | return array_container;
297 | }
298 |
299 | rand_type show_contents() const {
300 | /**
301 | * @brief Iterates array contents and outputs the data stored
302 | * @param void
303 | * @return typename (rand_type)
304 | */
305 | for (const auto& content : array_container) {
306 | std::cout << content << " ";
307 | }
308 | }
309 | /**
310 | * @brief Returns size of array container
311 | * @param void
312 | * @return typename (rand_tpe)
313 | */
314 | rand_type xsize() const { return array_container.size(); }
315 |
316 |
317 | private:
318 | std::random_device __device;
319 | std::array array_container;
320 | }; // class: rrand_array
321 |
322 | /**
323 | * @brief Inherited Template class rrand_stack
324 | * @class Usage for generating random uniform data for std::stack
325 | * @format rrand_stack
326 | */
327 | template
328 | class rrand_stack : protected rrand_array {
329 | public:
330 | std::stack gen_rrstack(bool numeric, bool minmax, rand_type __min = 0, rand_type __max = 100) {
331 | std::mt19937 __mtgenerator(__device());
332 | if (rrgen_success_on_return_value(numeric)) {
333 | auto dist = rrand_array::gen_random_data(__mtgenerator);
334 | for (auto elem = RRGEN_SUCCESS_CODE_STANDARD; elem < __datasize; ++elem) {
335 | stack_container.push(dist(__mtgenerator));
336 | }
337 | return stack_container;
338 | } else {
339 | if (rrgen_success_on_return_value(minmax)) {
340 | auto dist = rrand_array::gen_random_data_mm(__mtgenerator, __min, __max);
341 | for (auto elem = RRGEN_SUCCESS_CODE_STANDARD; elem < __datasize; ++elem) {
342 | stack_container.push(dist(__mtgenerator));
343 | }
344 | return stack_container;
345 | } else { return stack_container; }
346 | }
347 | }
348 |
349 | std::stack contents() const {
350 | return stack_container;
351 | }
352 |
353 | constexpr bool is_empty() const noexcept {
354 | /**
355 | * @brief returns whether or not the stack is empty
356 | * @param void
357 | * @return bool
358 | */
359 | return stack_container.empty();
360 | }
361 |
362 | rand_type grab_top() const {
363 | /**
364 | * @brief returns the element at the top of the stack
365 | * @param void
366 | * @return typename (rand_type)
367 | */
368 | return stack_container.top();
369 | }
370 |
371 | void push_top(rand_type value) {
372 | /**
373 | * @brief pushes argument to the top of the stack container
374 | * @param typename (rand_type) - element/value to push
375 | * @return void
376 | */
377 | stack_container.push(value);
378 | }
379 |
380 | rand_type xsize() const {
381 | /**
382 | * @brief returns the size of the stack container (# of elements)
383 | * @param void
384 | * @return typename (rand_type)
385 | */
386 | return stack_container.size();
387 | }
388 |
389 | void pop_off() {
390 | /**
391 | * @brief pops the top element off of the stack container
392 | * @param void
393 | * @return void
394 | */
395 | stack_container.pop();
396 | }
397 |
398 | private:
399 | std::random_device __device;
400 | std::stack stack_container;
401 | }; // class: rrand_stack
402 |
403 | namespace exception {
404 | /**
405 | * @brief rrgen exception class - inherits fron stdexcept
406 | * Used to throw exceptions rather than std::cerr
407 | */
408 | class rrgen_except : public std::exception {
409 | public:
410 | /**
411 | * @brief Constructor - sets error strings
412 | * @class rrgen_except
413 | */
414 | rrgen_except(const char* const message) : __errmessage__{message} {};
415 | /**
416 | * @brief returns the error message
417 | * @param void
418 | * @return const char*
419 | */
420 | const char* what() const noexcept { return __errmessage__; }
421 | private:
422 | const char* __errmessage__;
423 |
424 | }; // class: rrgen_except
425 |
426 | } // namespace: exception
427 |
428 |
429 | } // namespace: rrgen
430 |
431 |
432 | #endif
433 |
--------------------------------------------------------------------------------