Core class for runtime type information in byeol language. More...
#include <type.hpp>
Public Member Functions | |
| virtual nbool | operator== (const me &rhs) const |
| Checks if two type objects represent the same type. | |
| nbool | operator!= (const me &rhs) const |
| virtual nbool | isTemplate () const =0 |
| virtual nbool | isAbstract () const =0 |
| virtual const std::string & | getName () const |
| virtual void * | make () const =0 |
| create an instance to be refered this type. | |
| template<typename T > | |
| T * | makeAs () const |
| virtual ncnt | size () const =0 |
| virtual nbool | init () |
| Initializes type metadata and constructs class hierarchy. | |
| virtual nbool | rel () |
| virtual const type & | getSuper () const =0 |
| virtual const nbool & | isInit () const =0 |
| const types & | getLeafs () const |
| Returns all most derived classes (leaf nodes) from this class. | |
| const types & | getSubs () const |
| const types & | getSupers () const |
| virtual nbool | isSuper (const type &it) const |
| Checks if this type is a super class of the given type. | |
| nbool | isSuper (const type *it) const BY_SIDE_FUNC(isSuper) |
| template<typename T > | |
| nbool | isSuper () const |
| nbool | isSub (const type &it) const |
| Checks if this type is a sub class of the given type. | |
| nbool | isSub (const type *it) const BY_SIDE_FUNC(isSub) |
| template<typename T > | |
| nbool | isSub () const |
| const type & | getStatic () const BY_CONST_FUNC(_getStatic()) virtual const nchar *getMetaTypeName() const |
| Get meta type name for efficient type checking. | |
Protected Member Functions | |
| virtual types & | _getSubs ()=0 |
| virtual types & | _getSupers ()=0 |
| virtual type & | _getStatic () const =0 |
| void | _setInit (nbool newState) |
| virtual void | _onAddSubClass (const me &subClass) |
| Hook method called when a new subclass is registered with this type. | |
| virtual types ** | _onGetLeafs () const =0 |
| void | _setLeafs (types *newLeafs) const |
| virtual const std::string & | _getNativeName () const |
Core class for runtime type information in byeol language.
The central class of the meta module. Provides fundamental type information APIs.
Performance note: isSuper() and isSub() are more efficient than dynamic_cast. While dynamic_cast typically loops through vtables, the meta module uses a tier algorithm that compares tier values and char* addresses for O(1) type checking.
These are typically handled automatically via BY_INIT_META macro and rarely need direct invocation.
Type information like isTemplate(), isAbstract(), and getName() is filled by ttypeBase<T> through metaprogramming. The purpose of type::init() is to construct the class hierarchy.
The hierarchy is built using the constraint that "all classes must define super as a typedef". With super defined for all classes, ttype<super>().init() can be called, enabling recursive class hierarchy construction:
Type objects are initialized via init() calls, but manually calling init() for every class would be inefficient. The BY_INIT_META macro solves this by using BY_INITIATOR to execute init() before main() via static object initialization with lambda functions.
The constraint is that each class declaration must include BY_INIT_META(MyClass). These meta DSL-style macros are executed through the BY macro convention, and core module adds additional meta DSL macros, so use BY(CLASS()) or BY(ADT()) instead of calling BY_INIT_META directly.
While type provides substantial type information, language implementations like byeol may need additional information like parameters or return types. You might think to inherit from type, but since the user entry point must always be ttype<T>, and you cannot modify ttype<T> code from modules depending on meta, inheritance isn't possible. Instead, inject custom meta types.
The core code is in ttypeBase<T>:
tmetaTypeDef returns T::metaType if it exists, otherwise returns type. ttype inherits from ttypeBase, which inherits from tmetaTypeDef<T>::is. When calling ttype<T>, if class T defines typedef metaType MyType, the ttype<T> object will be based on MyType.
This feature is actually used in the core module to inject ntype. See ntype for details.
Hook method called when a new subclass is registered with this type.
Derived type classes can override this method to perform custom actions (e.g., updating internal structures or caches) whenever a new subClass is added to its hierarchy.
| subClass | The new subclass being added. |
| const types & by::type::getLeafs | ( | ) | const |
Returns all most derived classes (leaf nodes) from this class.
Get meta type name for efficient type checking.
This returns metaTypename. metaTypename can be used like 'dynamic_cast<yourType>'. as you may know, c++'s dynamic_cast is slow. because normally compilers tries to loop in order to figure out which type is fit to given your type parameter. 'meta' library, however, uses 'tier' algorithm and it's O(1), so it's faster.
you can use your own metaType to represent more data on type class. for instance, 'core' module uses 'ntype' custom type class. but in that case, when you compare custom type class, you must compare extended data to 'rhs' variable to base type class, 'type'.
so how can you know that 'type' is actually instance of your derived custom type class in 'tier' algorithm? please don't think about 'dynamic_cast'. it'll vanish our effectiveness to use 'tier' algorithm. that's why I make 'getMetaTypeName()' func.
Initializes type metadata and constructs class hierarchy.
Checks if this type is a sub class of the given type.
Checks if this type is a super class of the given type.
create an instance to be refered this type.
Implemented in by::ttypeBase< void, type >.
Checks if two type objects represent the same type.
This comparison typically checks if the underlying type information (e.g., name, template status) is identical.
| rhs | The other type object to compare with. |