/** 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 **/ #include // ===================================================================== // == Up-front installation ============================================ // ===================================================================== namespace enigma { struct reference_index { int id; reference_index(int idx): id(idx) {} enum { DNE = -1 }; }; template struct derived_reference_index: reference_index { derived_reference_index(): reference_index(-1) {} derived_reference_index(int idx): reference_index(idx) {} }; namespace RTTI { template 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 { window_t(int id): derived_reference_index(id) {} }; struct button_t: enigma::derived_reference_index { 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 variant(const enigma::derived_reference_index&): rtti(enigma::RTTI::compiler_ids()) { /* ... */ } }; // ===================================================================== // == Compiler Codegen ================================================= // ===================================================================== namespace enigma { namespace RTTI { enum variant_runtime_types { VRT_WINDOW, VRT_BUTTON }; template<> int compiler_ids() { return VRT_WINDOW; } template<> int compiler_ids() { 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; };