// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Core/Collections/Array.h"
#include "MTypes.h"
///
/// Contains information about a single managed class.
///
class FLAXENGINE_API MClass
{
private:
#if USE_MONO
MonoClass* _monoClass;
void* _attrInfo = nullptr;
#endif
const MAssembly* _assembly;
MString _fullname;
Array _methods;
Array _fields;
Array _properties;
Array _attributes;
Array _events;
MVisibility _visibility;
int32 _hasCachedProperties : 1;
int32 _hasCachedFields : 1;
int32 _hasCachedMethods : 1;
int32 _hasCachedAttributes : 1;
int32 _hasCachedEvents : 1;
int32 _isStatic : 1;
int32 _isSealed : 1;
int32 _isAbstract : 1;
int32 _isInterface : 1;
public:
#if USE_MONO
///
/// Initializes a new instance of the class.
///
/// The parent assembly.
/// The Mono class.
/// The fullname.
MClass(const MAssembly* parentAssembly, MonoClass* monoClass, const MString& fullname);
#endif
///
/// Finalizes an instance of the class.
///
~MClass();
public:
///
/// Gets the parent assembly.
///
const MAssembly* GetAssembly() const
{
return _assembly;
}
///
/// Gets the full name of the class (namespace and typename).
///
FORCE_INLINE const MString& GetFullName() const
{
return _fullname;
}
#if USE_MONO
///
/// Gets the Mono class handle.
///
FORCE_INLINE MonoClass* GetNative() const
{
return _monoClass;
}
#endif
///
/// Gets class visibility
///
FORCE_INLINE MVisibility GetVisibility() const
{
return _visibility;
}
///
/// Gets if class is static
///
FORCE_INLINE bool IsStatic() const
{
return _isStatic != 0;
}
///
/// Gets if class is abstract
///
FORCE_INLINE bool IsAbstract() const
{
return _isAbstract != 0;
}
///
/// Gets if class is sealed
///
FORCE_INLINE bool IsSealed() const
{
return _isSealed != 0;
}
///
/// Gets if class is interface
///
FORCE_INLINE bool IsInterface() const
{
return _isInterface != 0;
}
///
/// Gets if class is generic
///
bool IsGeneric() const;
///
/// Gets the class type.
///
MType GetType() const;
///
/// Returns the base class of this class. Null if this class has no base.
///
MClass* GetBaseClass() const;
///
/// Checks if this class is a sub class of the specified class (including any derived types).
///
/// The class.
/// True if this class is a sub class of the specified class.
bool IsSubClassOf(const MClass* klass) const;
#if USE_MONO
///
/// Checks if this class is a sub class of the specified class (including any derived types).
///
/// The Mono class.
/// True if this class is a sub class of the specified class.
bool IsSubClassOf(MonoClass* monoClass) const;
#endif
///
/// Checks is the provided object instance of this class' type.
///
/// The object to check.
/// True if object is an instance the this class.
bool IsInstanceOfType(MObject* object) const;
///
/// Returns the size of an instance of this class, in bytes.
///
uint32 GetInstanceSize() const;
public:
///
/// Returns an object referencing a method with the specified name and number of parameters. Optionally checks the base classes.
///
/// The method name.
/// The method parameters count.
/// True if check base classes when searching for the given method.
/// The method or null if failed to find it.
MMethod* FindMethod(const char* name, int32 numParams, bool checkBaseClasses);
///
/// Returns an object referencing a method with the specified name and number of parameters.
///
/// If the type contains more than one method of the given name and parameters count the returned value can be non-deterministic (one of the matching methods).
/// The method name.
/// The method parameters count.
/// The method or null if failed to get it.
MMethod* GetMethod(const char* name, int32 numParams = 0);
///
/// Returns all methods belonging to this class.
///
///
/// Be aware this will not include the methods of any base classes.
///
/// The list of methods.
const Array& GetMethods();
///
/// Returns an object referencing a field with the specified name.
///
///
/// Does not query base class fields.
/// Returns null if field cannot be found.
///
/// The field name.
/// The field or null if failed.
MField* GetField(const char* name);
///
/// Returns all fields belonging to this class.
///
///
/// Be aware this will not include the fields of any base classes.
///
/// The list of fields.
const Array& GetFields();
///
/// Returns an object referencing a event with the specified name.
///
/// The event name.
/// The event object.
MEvent* GetEvent(const char* name);
///
/// Returns all events belonging to this class.
///
/// The list of events.
const Array& GetEvents();
///
/// Returns an object referencing a property with the specified name.
///
///
/// Does not query base class properties.
/// Returns null if property cannot be found.
///
/// The property name.
/// The property.
MProperty* GetProperty(const char* name);
///
/// Returns all properties belonging to this class.
///
///
/// Be aware this will not include the properties of any base classes.
///
/// The list of properties.
const Array& GetProperties();
public:
///
/// Creates a new instance of this class and constructs it.
///
/// The created managed object.
MObject* CreateInstance() const;
///
/// Creates a new instance of this class and then constructs it using the constructor with the specified number of parameters.
///
/// The array containing pointers to constructor parameters. Array length must be equal to number of parameters.
/// The number of parameters the constructor accepts.
/// The created managed object.
MObject* CreateInstance(void** params, uint32 numParams);
public:
///
/// Checks if class has an attribute of the specified type.
///
/// The attribute class to check.
/// True if has attribute of that class type, otherwise false.
bool HasAttribute(MClass* monoClass);
///
/// Checks if class has an attribute of any type.
///
/// True if has any custom attribute, otherwise false.
bool HasAttribute();
///
/// Returns an instance of an attribute of the specified type. Returns null if the class doesn't have such an attribute.
///
/// The attribute class to take.
/// The attribute object.
MObject* GetAttribute(MClass* monoClass);
///
/// Returns an instance of all attributes connected with given class. Returns null if the class doesn't have any attributes.
///
/// The array of attribute objects.
const Array& GetAttributes();
};