Advertisement
JoshDreamland

Variant RTTI Sample

Oct 5th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. /** Copyright (C) 2014 Josh Ventura
  2. ***
  3. *** This file is a part of the ENIGMA Development Environment.
  4. ***
  5. *** ENIGMA is free software: you can redistribute it and/or modify it under the
  6. *** terms of the GNU General Public License as published by the Free Software
  7. *** Foundation, version 3 of the license or any later version.
  8. ***
  9. *** This application and its source code is distributed AS-IS, WITHOUT ANY
  10. *** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. *** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  12. *** details.
  13. ***
  14. *** You should have received a copy of the GNU General Public License along
  15. *** with this code. If not, see <http://www.gnu.org/licenses/>
  16. **/
  17.  
  18. #include <iostream>
  19.  
  20. // =====================================================================
  21. // == Up-front installation ============================================
  22. // =====================================================================
  23.  
  24. namespace enigma {
  25.   struct reference_index {
  26.     int id;
  27.     reference_index(int idx): id(idx) {}
  28.     enum { DNE = -1 };
  29.   };
  30.  
  31.   template<class T> struct derived_reference_index: reference_index {
  32.     derived_reference_index(): reference_index(-1) {}
  33.     derived_reference_index(int idx): reference_index(idx) {}
  34.   };
  35.  
  36.   namespace RTTI {
  37.     template<class T> int compiler_ids();
  38.   }
  39. }
  40.  
  41.  
  42.  
  43. // =====================================================================
  44. // == Declaring types ==================================================
  45. // =====================================================================
  46.  
  47. namespace enigma {
  48.   namespace RTTI {
  49.     struct window_t {};
  50.     struct button_t {};
  51.   }
  52. }
  53.  
  54. namespace enigma_user {
  55.   struct window_t:
  56.       enigma::derived_reference_index<enigma::RTTI::window_t> {
  57.     window_t(int id): derived_reference_index(id) {}
  58.   };
  59.   struct button_t:
  60.       enigma::derived_reference_index<enigma::RTTI::button_t> {
  61.     button_t(int id): derived_reference_index(id) {}
  62.   };
  63. }
  64.  
  65.  
  66.  
  67. // =====================================================================
  68. // == Variant Extensions (Approximate) =================================
  69. // =====================================================================
  70.  
  71. struct variant {
  72.   int rtti;
  73.  
  74.   variant(int x): /* ... */ rtti(-1) { /* ... */ }
  75.   variant(double x): /* ... */ rtti(-1) { /* ... */ }
  76.   // ...
  77.   template<class T> variant(const enigma::derived_reference_index<T>&):
  78.       rtti(enigma::RTTI::compiler_ids<T>()) { /* ... */ }
  79. };
  80.  
  81. // =====================================================================
  82. // == Compiler Codegen =================================================
  83. // =====================================================================
  84.  
  85. namespace enigma {
  86.   namespace RTTI {
  87.     enum variant_runtime_types {
  88.       VRT_WINDOW,  
  89.       VRT_BUTTON
  90.     };
  91.    
  92.     template<> int compiler_ids<window_t>() { return VRT_WINDOW; }
  93.     template<> int compiler_ids<button_t>() { return VRT_BUTTON; }
  94.   }
  95. }
  96.  
  97.  
  98. // =====================================================================
  99. // == Simple Test Code =================================================
  100. // =====================================================================
  101.  
  102. using namespace enigma_user;
  103. using namespace std;
  104.  
  105. int main() {
  106.   window_t wn = 20;
  107.   button_t bt = 50;
  108.   variant a(wn);
  109.   variant b(10);
  110.   variant c(10.5f);
  111.   variant d(bt);
  112.   cout << a.rtti << endl;
  113.   cout << b.rtti << endl;
  114.   cout << c.rtti << endl;
  115.   cout << d.rtti << endl;
  116. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement