-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvariadic_storage.cpp
More file actions
99 lines (79 loc) · 2.4 KB
/
variadic_storage.cpp
File metadata and controls
99 lines (79 loc) · 2.4 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <string>
#include <vector>
#include <utility>
#include <iostream>
class SimpleStorage
{
public:
using SimpleElement_t = std::pair<std::string, std::string>;
using SimpleStorage_t = std::vector<SimpleElement_t>;
SimpleStorage() = default;
// Interface
void addProperty( std::string propertyName )
{
createPair( propertyName );
}
void addNumber( std::string propertyName, double number )
{
createPair( propertyName, number );
}
void addString( std::string propertyName, std::string content )
{
createPair( propertyName, content );
}
void addPair( std::string propertyName, int contentNumber, std::string subContent )
{
createPair( propertyName, contentNumber, subContent );
}
friend std::ostream& operator<<( std::ostream& out, const SimpleStorage& obj )
{
for ( auto element : obj.m_storage )
{
out << element.first << " " << element.second << "\n";
}
return out;
}
private:
// Intermediate collector
template<typename... Args>
void createPair( std::string propertyName, Args&&... args )
{
// Place for the preprocessing logic
SimpleElement_t newPair( propertyName, "" );
// Call redirection
storeElement( std::move( newPair ), std::forward<Args>( args )... );
}
// Internal implementations
void storeElement( SimpleElement_t newPair )
{
newPair.second = "Empty";
m_storage.emplace_back( newPair );
}
void storeElement( SimpleElement_t newPair, double number )
{
newPair.second = std::to_string( number );
m_storage.emplace_back( newPair );
}
void storeElement( SimpleElement_t newPair, std::string content )
{
newPair.second = content;
m_storage.emplace_back( newPair );
}
void storeElement( SimpleElement_t newPair, int contentNumber, std::string subContent )
{
newPair.second = std::to_string( contentNumber ) + ": " + subContent;
m_storage.emplace_back( newPair );
}
private:
SimpleStorage_t m_storage;
};
int main()
{
SimpleStorage storage;
storage.addProperty( "someEmptyProperty" );
storage.addNumber( "numericalProperty", 5.5 );
storage.addString( "textProperty", "Some string" );
storage.addPair( "complexProperty", 3, "Complex string" );
std::cout << storage;
return 0;
}