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. template< typename T>
  30. using isSpace2 = typename std::enable_if < std::is_same<T, Space2>::value && std::is_same<Space, Space2>::value >::type;
  31.  
  32. template< typename T>
  33. using isSpace3 = typename std::enable_if < std::is_same<T, Space3>::value && std::is_same<Space, Space3>::value >::type;
  34.  
  35. template<class T, class Enable = void>
  36. struct ParticleData;
  37.  
  38. template<class T>
  39. struct ParticleData<T, isSpace2<T>>
  40. {
  41. float orientation;
  42. float invInertia;
  43. ParticleData()
  44. {
  45. orientation = 2.0f ;
  46. invInertia = 3.0f;
  47. }
  48. };
  49.  
  50. template<class T>
  51. struct ParticleData<T, isSpace3<T>>
  52. {
  53. float test;
  54. ParticleData()
  55. {
  56. test = 100500.0f;
  57. }
  58. };
  59.  
  60. struct Particle : public ParticleData<Space>
  61. {
  62. float someGenericStuff;
  63. };
  64.  
  65.  
  66. template<class T>
  67. void DumpParticle(size_t particleIndex, isSpace2<T>* val = 0)
  68. {
  69. printf("2: %f %f\n", particles[particleIndex].orientation, particles[particleIndex].invInertia );
  70. }
  71.  
  72. template<class T>
  73. void DumpParticle(size_t particleIndex, isSpace3<T>* val = 0)
  74. {
  75. printf("3: %f\n", particles[particleIndex].test);
  76. }
  77.  
  78. void DumpParticle(size_t particleIndex)
  79. {
  80. DumpParticle<Space>( particleIndex);
  81. }
  82.  
  83. void DumpParticles()
  84. {
  85. for (size_t particleIndex = 0; particleIndex < particles.size(); particleIndex++)
  86. {
  87. DumpParticle(particleIndex);
  88. }
  89. }
  90. ParticleSystem()
  91. {
  92. particles.push_back(Particle());
  93. }
  94. std::vector<Particle> particles;
  95. };
  96.  
  97.  
  98.  
  99. int main()
  100. {
  101. ParticleSystem<Space3, float> p3;
  102. ParticleSystem<Space2, char> p2;
  103. p3.DumpParticles();
  104. p2.DumpParticles();
  105. return 0;
  106. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
3: 100500.000000
2: 2.000000 3.000000