code_junkie

Static member variable in template, with multiple dlls

Nov 13th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #pragma data_seg(".JOE")
  2. HWND hWndServer = NULL;
  3. HHOOK hook = NULL;
  4. #pragma data_seg()
  5. #pragma comment(linker, "/section:.JOE,rws")
  6.  
  7. template <class T> class Foo;
  8. template<> class Foo<int> {};
  9.  
  10. __declspec(dllexport) int Foo<int>::StaticMember = 0;
  11.  
  12. map<string,void*> dllexport the_map; // instantiate this once in a single DLL
  13.  
  14. T *set_the_global(T *candidate) {
  15. map<string,void*>::iterator r = the_map.find(string(typeid(the_class<T>).name()));
  16. if(r == the_map.end()) {
  17. the_map[string(typeid(the_class<T>).name())] = (void*)candidate;
  18. return candidate; // new class: use it as global storage location
  19. } else {
  20. return (T*)(r->second); // class already has global storage location
  21. }
  22. }
  23.  
  24. template <class T> class the_class {
  25. virtual void something(); // so RTTI exists
  26. static T *the_global; // use this! always points to the same object
  27. static T one_per_dll; // only used in initialisation
  28. };
  29. template<class T> the_class<T>::one_per_dll;
  30. template<class T> the_class<T>::the_global = set_the_global(&the_class<T>::one_per_dll)
Add Comment
Please, Sign In to add comment