fork download
  1.  
  2. #include <stdio.h>
  3. #include <vector>
  4. #include <type_traits>
  5. using namespace std;
  6. struct Space2
  7. {
  8. struct Vector
  9. {
  10.  
  11. };
  12.  
  13. };
  14. struct Space3
  15. {
  16. struct Vector
  17. {
  18.  
  19. };
  20.  
  21. };
  22.  
  23.  
  24.  
  25.  
  26. template<typename Space, typename AwesomeParam>
  27. struct ParticleSystem
  28. {
  29.  
  30. #define isSpace2 typename std::enable_if < std::is_same<T, Space2>::value>::type
  31. #define isSpace3 typename std::enable_if < std::is_same<T, Space3>::value>::type
  32.  
  33. template<class T, class Enable = void>
  34. struct ParticleData;
  35.  
  36. template<class T>
  37. struct ParticleData<T, isSpace2>
  38. {
  39. float orientation;
  40. float invInertia;
  41. ParticleData()
  42. {
  43. orientation = 2.0f ;
  44. invInertia = 3.0f;
  45. }
  46. };
  47.  
  48. template<class T>
  49. struct ParticleData<T, isSpace3>
  50. {
  51. float test;
  52. ParticleData()
  53. {
  54. test = 100500.0f;
  55. }
  56. };
  57.  
  58. struct Particle : public ParticleData<Space>
  59. {
  60. float someGenericStuff;
  61. };
  62.  
  63.  
  64. template<class T>
  65. void DumpParticle(size_t particleIndex, isSpace2* val = 0)
  66. {
  67. printf("2: %f %f\n", particles[particleIndex].orientation, particles[particleIndex].invInertia );
  68. }
  69.  
  70. template<class T>
  71. void DumpParticle(size_t particleIndex, isSpace3* val = 0)
  72. {
  73. printf("3: %f\n", particles[particleIndex].test);
  74. }
  75.  
  76. void DumpParticle(size_t particleIndex)
  77. {
  78. DumpParticle<Space>( particleIndex);
  79. }
  80.  
  81. void DumpParticles()
  82. {
  83. for (size_t particleIndex = 0; particleIndex < particles.size(); particleIndex++)
  84. {
  85. DumpParticle(particleIndex);
  86. }
  87. }
  88. ParticleSystem()
  89. {
  90. particles.push_back(Particle());
  91. }
  92. std::vector<Particle> particles;
  93. };
  94.  
  95.  
  96.  
  97. int main()
  98. {
  99. ParticleSystem<Space3, float> p3;
  100. ParticleSystem<Space2, char> p2;
  101. p3.DumpParticles();
  102. p2.DumpParticles();
  103. return 0;
  104. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
3: 100500.000000
2: 2.000000 3.000000