- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
#include <iostream>
#include <cstring>
const char tag[] = "Secret tag!";
const size_t tagSize = sizeof(tag);
const size_t nameSize = 32;
template<class T>
struct Value
{
Value(const char* name, const T& data) :
data(data)
{
std::strncpy(this->tag, ::tag, tagSize);
std::strncpy(this->name, name, nameSize);
}
char tag[tagSize];
char name[nameSize];
T data;
};
int getStackDir()
{
char a;
char b;
return &b > &a ? 1 : -1;
}
template<class T>
T getValue(const char* name)
{
static const size_t stackSize = 1024 * 1024;
const int stackDir = getStackDir();
char begin;
for(char* p = &begin, *end = &begin - stackSize * stackDir; p != end; p -= stackDir)
{
Value<T>* value = (Value<T>*)p;
if(std::strcmp(value->tag, tag) != 0) continue;
if(std::strcmp(value->name, name) != 0) continue;
return value->data;
}
return T();
}
#define SET(type, name, value) Value<type> name(#name, value)
#define GET(type, name) getValue<type>(#name)
//-----------------------------------------------------------
void test()
{
std::cout << GET(int, x) << std::endl;
}
int main()
{
SET(int, x, 13);
test();
}