JoshDreamland icon

Variant RTTI Sample

JoshDreamland | PRO | 10/05/14 05:48:33 PM UTC | 0 ⭐ | 557 👁️ | Never ⏰ | []
C++ |

3.49 KB

|

None

|

0 👍

/

0 👎

/** Copyright (C) 2014 Josh Ventura
***
*** This file is a part of the ENIGMA Development Environment.
***
*** ENIGMA is free software: you can redistribute it and/or modify it under the
*** terms of the GNU General Public License as published by the Free Software
*** Foundation, version 3 of the license or any later version.
***
*** This application and its source code is distributed AS-IS, WITHOUT ANY
*** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
*** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
*** details.
***
*** You should have received a copy of the GNU General Public License along
*** with this code. If not, see <http://www.gnu.org/licenses/>
**/
 
#include <iostream>
 
// =====================================================================
// == Up-front installation ============================================
// =====================================================================
 
namespace enigma {
  struct reference_index {
    int id;
    reference_index(int idx): id(idx) {}
    enum { DNE = -1 };
  };
 
  template<class T> struct derived_reference_index: reference_index {
    derived_reference_index(): reference_index(-1) {}
    derived_reference_index(int idx): reference_index(idx) {}
  };
 
  namespace RTTI {
    template<class T> int compiler_ids();
  }
}
 
 
 
// =====================================================================
// == Declaring types ==================================================
// =====================================================================
 
namespace enigma {
  namespace RTTI {
    struct window_t {};
    struct button_t {};
  }
}
 
namespace enigma_user {
  struct window_t:
      enigma::derived_reference_index<enigma::RTTI::window_t> {
    window_t(int id): derived_reference_index(id) {}
  };
  struct button_t:
      enigma::derived_reference_index<enigma::RTTI::button_t> {
    button_t(int id): derived_reference_index(id) {}
  };
}
 
 
 
// =====================================================================
// == Variant Extensions (Approximate) =================================
// =====================================================================
 
struct variant {
  int rtti;
  
  variant(int x): /* ... */ rtti(-1) { /* ... */ }
  variant(double x): /* ... */ rtti(-1) { /* ... */ }
  // ...
  template<class T> variant(const enigma::derived_reference_index<T>&):
      rtti(enigma::RTTI::compiler_ids<T>()) { /* ... */ }
};
 
// =====================================================================
// == Compiler Codegen =================================================
// =====================================================================
 
namespace enigma {
  namespace RTTI {
    enum variant_runtime_types {
      VRT_WINDOW,  
      VRT_BUTTON 
    };
    
    template<> int compiler_ids<window_t>() { return VRT_WINDOW; }
    template<> int compiler_ids<button_t>() { return VRT_BUTTON; }
  }
}
 
 
// =====================================================================
// == Simple Test Code =================================================
// =====================================================================
 
using namespace enigma_user;
using namespace std;
 
int main() {
  window_t wn = 20;
  button_t bt = 50;
  variant a(wn);
  variant b(10);
  variant c(10.5f);
  variant d(bt);
  cout << a.rtti << endl;
  cout << b.rtti << endl;
  cout << c.rtti << endl;
  cout << d.rtti << endl;
};

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎