Skip to content

Instantly share code, notes, and snippets.

@dridk

dridk/orm.cpp

Created Sep 1, 2018
Embed
What would you like to do?
CRTP inheritance with static fields
#include <iostream>
#include <map>
#include <string>
#include <list>
using namespace std;
// =========== Classe de base: caché a l'utilisateur ==================================
template <typename T>
class Base
{
public:
void save() {
for (auto & i : metadatas)
cout<<i.first<<" "<<i.second<<" "<<endl;
};
list<T> all() {};
static map<string, int> metadatas;
};
#define REGISTER_FIELDS(ClassName,...) template<> map<string, int> Base<ClassName>::metadatas = {__VA_ARGS__};
// L'utilisateur créer des class ====================================================
class User: public Base<User>
{
};
REGISTER_FIELDS (User, {"AGE",32},{"TRUC",23})
class Comment: public Base<Comment>
{
};
REGISTER_FIELDS (Comment, {"POINT",12},{"NOTE",23})
int main(int argc, char **argv)
{
User user;
user.save();
Comment comm;
comm.save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.