Skip to content

Instantly share code, notes, and snippets.

@xueliu
Last active July 27, 2020 11:51
Show Gist options
  • Save xueliu/19fdadedf2fe2161eed878c173628cc4 to your computer and use it in GitHub Desktop.
Save xueliu/19fdadedf2fe2161eed878c173628cc4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <array>
#include <functional>
using namespace std;
class NodeManager {
private:
int hasFlags(int x) const
{
return 0;
};
int setFlags(int x)
{
return 0;
};
int removeFlags(int x)
{
return 0;
};
private:
#define NODE_LIST(NODE) \
NODE(Node_1) \
NODE(Node_2) \
NODE(Node_3) \
NODE(Node_4) \
NODE(Node_5) \
NODE(Node_6) \
NODE(Node_7)
#define DEFINE_ENUM(ENUM) ENUM,
enum Node {
NODE_LIST(DEFINE_ENUM)
Total
};
#undef DEFINE_ENUM
#define DEFINE_STRING(STRING) #STRING,
public:
const std::array< std::string, Node::Total > Node_String = {
{
NODE_LIST(DEFINE_STRING)
}
};
#undef DEFINE_STRING
#define NODE_ACCESSOR(NODE) \
bool is##NODE() const { \
return hasFlags(1 << NODE); \
} \
void set##NODE() { \
setFlags(1 << NODE); \
} \
void setNot##NODE() { \
removeFlags(1 << NODE); \
} \
void Cb##NODE(int x) { \
std::cout << x << std::endl; \
}
NODE_LIST(NODE_ACCESSOR)
#undef NODE_ACCESSOR
#define NODE_CALLBACK(CALLBACK) std::bind(&NodeManager::Cb##CALLBACK, this, std::placeholders::_1),
public:
const std::array<std::function<void(int)>, Node::Total> Node_Callback = {
{
NODE_LIST(NODE_CALLBACK)
}
};
#undef NODE_CALLBACK
};
int main()
{
NodeManager a;
int i = 0;
for(string str : a.Node_String)
{
std::cout << str << std::endl;
a.Node_Callback[i](i);
i++;
}
std::cout << "End" <<std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment