Skip to content

Commit f896f89

Browse files
Split RandomNew to create an init function
Signed-off-by: Martin Melik Merkumians <martin.melik@gmail.com>
1 parent af7ac04 commit f896f89

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

source/src/utils/random.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@ Random* RandomNew(SetSeed set_seed_function,
1313
GetNextUInt32 get_next_uint32_function) {
1414
Random* random = malloc(sizeof(Random));
1515
assert(random != NULL && "Failed to allocate Random struct");
16+
RandomInit(random, set_seed_function, get_next_uint32_function);
17+
return random;
18+
}
19+
20+
void RandomInit(Random* const random,
21+
SetSeed set_seed_function,
22+
GetNextUInt32 get_next_uint32_function) {
23+
assert(random != NULL && "Cannot initialize NULL Random struct");
1624
// The next line is fine, just false positive from cpplint due to formatting
1725
*random = (Random){ .set_seed = set_seed_function,
1826
.get_next_uint32 = get_next_uint32_function }; // NOLINT
19-
return random;
2027
}
2128

2229
void RandomDelete(Random** random) {

source/src/utils/random.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,21 @@ typedef struct Random {
2525
get_next_uint32; ///< Function pointer to GetNextUInt32 function
2626
} Random;
2727

28+
/// @brief Creates a new Random object
29+
/// @param set_seed_function
30+
/// @param get_next_uint32_function
31+
/// @return Pointer to newly created Random object
2832
Random* RandomNew(SetSeed set_seed_function,
2933
GetNextUInt32 get_next_uint32_function);
3034

35+
/// @brief Initializes a Random object
36+
/// @param random Pointer to Random object to initialize
37+
/// @param set_seed_function Used set seed function
38+
/// @param get_next_uint32_function used get next uint32 function
39+
void RandomInit(Random* const random,
40+
SetSeed set_seed_function,
41+
GetNextUInt32 get_next_uint32_function);
42+
3143
void RandomDelete(Random** random);
3244

3345
#endif /* UTILS_RANDOM_H_ */

0 commit comments

Comments
 (0)