-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcstdargHomeMade.h
74 lines (66 loc) · 2.49 KB
/
cstdargHomeMade.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
/*
Exploring the Functionality of cstdarg through Custom Class: Ellipsis
*/
class Ellipsis {
/*
To simulate the behavior of va_list, we create an object of the Ellipsis class.
*/
private:
void* Ellipsis_ptr;
// Store the address of the first ellipsis parameter of our function.
public:
template <typename noneEllipsisType>
void setEllipsis(noneEllipsisType& last_nonEllipsis) {
/*
Mimicking va_start(): determine ellipsis elements based on the last non-Ellipsis element's type.
*/
void* address = &last_nonEllipsis;
Ellipsis_ptr = static_cast<double*>(address) + 1;
/*
The core of our class: calculating the address of the first ellipsis element.
The ellipsis parameters are stored with gaps equal to the size of the largest type.
*/
}
// Fetch the variable with the type that corresponds to the ellipsis.
//
// In this method, the user provides a sample type to help the system deduce the type.
template<typename EllipsisType>
EllipsisType next_arg(EllipsisType sample) {
/*
Iterating through our array of parameters.
We ask the user for the ellipsis type and extract it from the array.
*/
EllipsisType value = *(static_cast<EllipsisType*>(Ellipsis_ptr));
/*
Reading the value of our ellipsis element from the array.
*/
Ellipsis_ptr = static_cast<double*>(Ellipsis_ptr) + 1;
/*
Moving to the next ellipsis element for the next call.
To restart, recall the setEllipsis() function.
*/
return value;
}
// This version allows you to provide the value type to be read within <>
template<typename EllipsisType>
EllipsisType next_arg() {
/*
Similar to the previous method, we traverse the array of parameters.
User provides the ellipsis type and we extract it from the array.
*/
EllipsisType value = *(static_cast<EllipsisType*>(Ellipsis_ptr));
/*
Reading the value of our ellipsis element from the array.
*/
Ellipsis_ptr = static_cast<double*>(Ellipsis_ptr) + 1;
/*
Moving to the next ellipsis element for the next call.
To restart, recall the setEllipsis() function.
*/
return value;
}
};
/*
Copyright © SkillFulElectro LLC.
*/