Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. template <typename T> struct General {};
  2. struct EnumSpecific : General<any_enum_type> {};
  3.  
  4. // A templated value holder:
  5. template <typename T>
  6. class Holder {
  7. public:
  8. Holder(T const& t) : _value(t) {};
  9. // generic methods
  10. void generics() {};
  11. // methods concerning the value:
  12. void set(T const& t /*, setInfo */) {
  13. // .. check for an actual change, notify buddies of the change..
  14. _value = t;
  15. };
  16. T value(/*readInfo*/) {
  17. // .. do stuff depending on how / why the value is read..
  18. return _value;
  19. };
  20. private:
  21. T _value;
  22. };
  23. // (in reality, all `generics` methods come from a parent, untemplated class)
  24.  
  25. // A generic process involving such `Holder`s:
  26. template <typename T>
  27. class General {
  28. public:
  29. typedef bool /* or anything */ KnownReturnTypes;
  30. General(Holder<T>* const a
  31. , Holder<T>* const b)
  32. : _a(a)
  33. , _b(b)
  34. {};
  35. void methods() {
  36. // Use common behavior of all `Holder`'s
  37. _a->generics();
  38. // .. or methods that rely on the actual values:
  39. KnownReturnTypes knr( valuedMethods() );
  40. if (knr) {} else {}
  41. // ...
  42. };
  43. // Use polymorphism to adapt to each situation..
  44. virtual KnownReturnTypes valuedMethods() = 0;
  45. protected:
  46. Holder<T>* _a;
  47. Holder<T>* _b;
  48. };
  49.  
  50. // Example of specialization for integral types (there might be others)
  51. class IntSpecific : General<int> {
  52. public:
  53. IntSpecific(Holder<int>* const a
  54. , Holder<int>* const b)
  55. : General<int>(a, b)
  56. {};
  57. // implement the valuedMethods:
  58. virtual KnownReturnTypes valuedMethods() {
  59. return _a->value() > _b->value(); // dummy
  60. }
  61. };
  62.  
  63. // Specialization for enum types:
  64. // * * * class EnumSpecific : General<any_enum_type> { // does not exist * *
  65. class EnumSpecific : General<int> {
  66. public:
  67. EnumSpecific( Holder<int>* const a
  68. , Holder<int>* const b)
  69. : General<int>(a, b)
  70. {};
  71. // only use properties and methods offered by an enum type:
  72. virtual KnownReturnTypes valuedMethods() {
  73. return _a->value() == _b->value(); // dummy
  74. }
  75. };
  76.  
  77. // One particular case
  78. typedef enum {One, Two, Three} Enum;
  79. typedef Holder<Enum> EnumHolder;
  80.  
  81.  
  82. int main() {
  83.  
  84. // Check that `IntSpecific` works fine.
  85. Holder<int>* i( new Holder<int>(3) );
  86. Holder<int>* j( new Holder<int>(5) );
  87. IntSpecific is(i, j); // ok.
  88.  
  89. // Try the `EnumSpecific`
  90. EnumHolder* a( new EnumHolder { One } );
  91. EnumHolder* b( new EnumHolder { Two } );
  92. EnumSpecific es(static_cast<Holder<int>*>(a) // invalid cast
  93. , static_cast<Holder<Enum>*>(b)); // unexpected type
  94. // This is because the compiler doesn't know enough about what
  95. // EnumSpecific actually *is*. How to tell him more about it?
  96.  
  97.  
  98. return EXIT_SUCCESS;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement