Skip to content

Instantly share code, notes, and snippets.

@ming4883
Created March 2, 2019 06:03
Show Gist options
  • Save ming4883/1746d9569208c3d84e344511e84fe883 to your computer and use it in GitHub Desktop.
Save ming4883/1746d9569208c3d84e344511e84fe883 to your computer and use it in GitHub Desktop.
C++ Auto Register
#include <iostream>
//int a = 0;
template <typename T, T /*unnamed*/>
struct nnb_ForceInit { };
template<
class T
>
class Scriptable {
public:
struct nnb_Proxy {
nnb_Proxy() {
std::cout << "Proxy was executed! ID: " << T::id() << std::endl;
//a++;
}
};
static nnb_Proxy __nnb_proxy__;
typedef nnb_ForceInit<nnb_Proxy&, __nnb_proxy__> __nnb_typedef_dummy__;
} ;
template<
class T
>
typename Scriptable<T>::nnb_Proxy Scriptable<T>::__nnb_proxy__;
class Object : public Scriptable<Object> {
public:
static const char* id() {
return "[Object]";
}
};
class Image : public Scriptable<Image> {
public:
static const char* id() {
return "[Image]";
}
};
class Error : public Scriptable<Error> {
public:
static const char* id() {
return "[Error]";
}
} ;
int main() {
std::cout << "Done " << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment