diff --git a/Examples/RTTI/RTTI.xml b/Examples/RTTI/RTTI.xml new file mode 100644 index 00000000..fb42fe2d --- /dev/null +++ b/Examples/RTTI/RTTI.xml @@ -0,0 +1,109 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_abi.hpp b/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_abi.hpp new file mode 100644 index 00000000..ae254c13 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_abi.hpp @@ -0,0 +1,196 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++-Header file in order to allow an easy + use of RTTI + +Interface version: 1.0.0 + +*/ + +#ifndef __RTTI_HEADER_CPP +#define __RTTI_HEADER_CPP + +#ifdef __RTTI_EXPORTS +#ifdef _WIN32 +#define RTTI_DECLSPEC __declspec (dllexport) +#else // _WIN32 +#define RTTI_DECLSPEC __attribute__((visibility("default"))) +#endif // _WIN32 +#else // __RTTI_EXPORTS +#define RTTI_DECLSPEC +#endif // __RTTI_EXPORTS + +#include "rtti_types.hpp" + + +#ifdef __cplusplus +extern "C" { +#endif + +/************************************************************************************************************************* + Class definition for Base +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Animal +**************************************************************************************************************************/ + +/** +* Get the name of the animal +* +* @param[in] pAnimal - Animal instance. +* @param[in] nResultBufferSize - size of the buffer (including trailing 0) +* @param[out] pResultNeededChars - will be filled with the count of the written bytes, or needed buffer size. +* @param[out] pResultBuffer - buffer of , may be NULL +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_animal_name(RTTI_Animal pAnimal, const RTTI_uint32 nResultBufferSize, RTTI_uint32* pResultNeededChars, char * pResultBuffer); + +/************************************************************************************************************************* + Class definition for Mammal +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Reptile +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Giraffe +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Tiger +**************************************************************************************************************************/ + +/** +* Roar like a tiger +* +* @param[in] pTiger - Tiger instance. +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_tiger_roar(RTTI_Tiger pTiger); + +/************************************************************************************************************************* + Class definition for Snake +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Turtle +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for AnimalIterator +**************************************************************************************************************************/ + +/** +* Return next animal +* +* @param[in] pAnimalIterator - AnimalIterator instance. +* @param[out] pAnimal - +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_animaliterator_getnextanimal(RTTI_AnimalIterator pAnimalIterator, RTTI_Animal * pAnimal); + +/************************************************************************************************************************* + Class definition for Zoo +**************************************************************************************************************************/ + +/** +* Return an iterator over all zoo animals +* +* @param[in] pZoo - Zoo instance. +* @param[out] pIterator - +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_zoo_iterator(RTTI_Zoo pZoo, RTTI_AnimalIterator * pIterator); + +/************************************************************************************************************************* + Global functions +**************************************************************************************************************************/ + +/** +* retrieves the binary version of this library. +* +* @param[out] pMajor - returns the major version of this library +* @param[out] pMinor - returns the minor version of this library +* @param[out] pMicro - returns the micro version of this library +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_getversion(RTTI_uint32 * pMajor, RTTI_uint32 * pMinor, RTTI_uint32 * pMicro); + +/** +* Returns the last error recorded on this object +* +* @param[in] pInstance - Instance Handle +* @param[in] nErrorMessageBufferSize - size of the buffer (including trailing 0) +* @param[out] pErrorMessageNeededChars - will be filled with the count of the written bytes, or needed buffer size. +* @param[out] pErrorMessageBuffer - buffer of Message of the last error, may be NULL +* @param[out] pHasError - Is there a last error to query +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_getlasterror(RTTI_Base pInstance, const RTTI_uint32 nErrorMessageBufferSize, RTTI_uint32* pErrorMessageNeededChars, char * pErrorMessageBuffer, bool * pHasError); + +/** +* Releases shared ownership of an Instance +* +* @param[in] pInstance - Instance Handle +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_releaseinstance(RTTI_Base pInstance); + +/** +* Acquires shared ownership of an Instance +* +* @param[in] pInstance - Instance Handle +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_acquireinstance(RTTI_Base pInstance); + +/** +* Injects an imported component for usage within this component +* +* @param[in] pNameSpace - NameSpace of the injected component +* @param[in] pSymbolAddressMethod - Address of the SymbolAddressMethod of the injected component +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_injectcomponent(const char * pNameSpace, RTTI_pvoid pSymbolAddressMethod); + +/** +* Returns the address of the SymbolLookupMethod +* +* @param[out] pSymbolLookupMethod - Address of the SymbolAddressMethod +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_getsymbollookupmethod(RTTI_pvoid * pSymbolLookupMethod); + +/** +* Test whether an object implements a given interface +* +* @param[in] pObject - Instance Handle +* @param[in] nClassHashBufferSize - Number of elements in buffer +* @param[in] pClassHashBuffer - uint8 buffer of Hashed class name of the interface to test +* @param[out] pImplementsInterface - Will be set to true if pInstance implements the interface, false otherwise +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_implementsinterface(RTTI_Base pObject, RTTI_uint64 nClassHashBufferSize, const RTTI_uint8 * pClassHashBuffer, bool * pImplementsInterface); + +/** +* Create a new zoo with animals +* +* @param[out] pInstance - +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_createzoo(RTTI_Zoo * pInstance); + +#ifdef __cplusplus +} +#endif + +#endif // __RTTI_HEADER_CPP + diff --git a/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_dynamic.h b/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_dynamic.h new file mode 100644 index 00000000..859684e1 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_dynamic.h @@ -0,0 +1,199 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++-Header file in order to allow an easy + use of RTTI + +Interface version: 1.0.0 + +*/ + +#ifndef __RTTI_DYNAMICHEADER_CPPTYPES +#define __RTTI_DYNAMICHEADER_CPPTYPES + +#include "rtti_types.hpp" + + + +/************************************************************************************************************************* + Class definition for Base +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Animal +**************************************************************************************************************************/ + +/** +* Get the name of the animal +* +* @param[in] pAnimal - Animal instance. +* @param[in] nResultBufferSize - size of the buffer (including trailing 0) +* @param[out] pResultNeededChars - will be filled with the count of the written bytes, or needed buffer size. +* @param[out] pResultBuffer - buffer of , may be NULL +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTIAnimal_NamePtr) (RTTI_Animal pAnimal, const RTTI_uint32 nResultBufferSize, RTTI_uint32* pResultNeededChars, char * pResultBuffer); + +/************************************************************************************************************************* + Class definition for Mammal +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Reptile +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Giraffe +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Tiger +**************************************************************************************************************************/ + +/** +* Roar like a tiger +* +* @param[in] pTiger - Tiger instance. +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTITiger_RoarPtr) (RTTI_Tiger pTiger); + +/************************************************************************************************************************* + Class definition for Snake +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Turtle +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for AnimalIterator +**************************************************************************************************************************/ + +/** +* Return next animal +* +* @param[in] pAnimalIterator - AnimalIterator instance. +* @param[out] pAnimal - +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTIAnimalIterator_GetNextAnimalPtr) (RTTI_AnimalIterator pAnimalIterator, RTTI_Animal * pAnimal); + +/************************************************************************************************************************* + Class definition for Zoo +**************************************************************************************************************************/ + +/** +* Return an iterator over all zoo animals +* +* @param[in] pZoo - Zoo instance. +* @param[out] pIterator - +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTIZoo_IteratorPtr) (RTTI_Zoo pZoo, RTTI_AnimalIterator * pIterator); + +/************************************************************************************************************************* + Global functions +**************************************************************************************************************************/ + +/** +* retrieves the binary version of this library. +* +* @param[out] pMajor - returns the major version of this library +* @param[out] pMinor - returns the minor version of this library +* @param[out] pMicro - returns the micro version of this library +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTIGetVersionPtr) (RTTI_uint32 * pMajor, RTTI_uint32 * pMinor, RTTI_uint32 * pMicro); + +/** +* Returns the last error recorded on this object +* +* @param[in] pInstance - Instance Handle +* @param[in] nErrorMessageBufferSize - size of the buffer (including trailing 0) +* @param[out] pErrorMessageNeededChars - will be filled with the count of the written bytes, or needed buffer size. +* @param[out] pErrorMessageBuffer - buffer of Message of the last error, may be NULL +* @param[out] pHasError - Is there a last error to query +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTIGetLastErrorPtr) (RTTI_Base pInstance, const RTTI_uint32 nErrorMessageBufferSize, RTTI_uint32* pErrorMessageNeededChars, char * pErrorMessageBuffer, bool * pHasError); + +/** +* Releases shared ownership of an Instance +* +* @param[in] pInstance - Instance Handle +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTIReleaseInstancePtr) (RTTI_Base pInstance); + +/** +* Acquires shared ownership of an Instance +* +* @param[in] pInstance - Instance Handle +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTIAcquireInstancePtr) (RTTI_Base pInstance); + +/** +* Injects an imported component for usage within this component +* +* @param[in] pNameSpace - NameSpace of the injected component +* @param[in] pSymbolAddressMethod - Address of the SymbolAddressMethod of the injected component +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTIInjectComponentPtr) (const char * pNameSpace, RTTI_pvoid pSymbolAddressMethod); + +/** +* Returns the address of the SymbolLookupMethod +* +* @param[out] pSymbolLookupMethod - Address of the SymbolAddressMethod +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTIGetSymbolLookupMethodPtr) (RTTI_pvoid * pSymbolLookupMethod); + +/** +* Test whether an object implements a given interface +* +* @param[in] pObject - Instance Handle +* @param[in] nClassHashBufferSize - Number of elements in buffer +* @param[in] pClassHashBuffer - uint8 buffer of Hashed class name of the interface to test +* @param[out] pImplementsInterface - Will be set to true if pInstance implements the interface, false otherwise +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTIImplementsInterfacePtr) (RTTI_Base pObject, RTTI_uint64 nClassHashBufferSize, const RTTI_uint8 * pClassHashBuffer, bool * pImplementsInterface); + +/** +* Create a new zoo with animals +* +* @param[out] pInstance - +* @return error code or 0 (success) +*/ +typedef RTTIResult (*PRTTICreateZooPtr) (RTTI_Zoo * pInstance); + +/************************************************************************************************************************* + Function Table Structure +**************************************************************************************************************************/ + +typedef struct { + void * m_LibraryHandle; + PRTTIAnimal_NamePtr m_Animal_Name; + PRTTITiger_RoarPtr m_Tiger_Roar; + PRTTIAnimalIterator_GetNextAnimalPtr m_AnimalIterator_GetNextAnimal; + PRTTIZoo_IteratorPtr m_Zoo_Iterator; + PRTTIGetVersionPtr m_GetVersion; + PRTTIGetLastErrorPtr m_GetLastError; + PRTTIReleaseInstancePtr m_ReleaseInstance; + PRTTIAcquireInstancePtr m_AcquireInstance; + PRTTIInjectComponentPtr m_InjectComponent; + PRTTIGetSymbolLookupMethodPtr m_GetSymbolLookupMethod; + PRTTIImplementsInterfacePtr m_ImplementsInterface; + PRTTICreateZooPtr m_CreateZoo; +} sRTTIDynamicWrapperTable; + +#endif // __RTTI_DYNAMICHEADER_CPPTYPES + diff --git a/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_dynamic.hpp b/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_dynamic.hpp new file mode 100644 index 00000000..9606ce35 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_dynamic.hpp @@ -0,0 +1,1130 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++-Header file in order to allow an easy + use of RTTI + +Interface version: 1.0.0 + +*/ + +#ifndef __RTTI_CPPHEADER_DYNAMIC_CPP +#define __RTTI_CPPHEADER_DYNAMIC_CPP + +#include "rtti_types.hpp" +#include "rtti_dynamic.h" + + +#ifdef _WIN32 +#include +#else // _WIN32 +#include +#endif // _WIN32 +#include +#include +#include +#include +#include + +namespace RTTI { + +/************************************************************************************************************************* + Forward Declaration of all classes +**************************************************************************************************************************/ +class CWrapper; +class CBase; +class CAnimal; +class CMammal; +class CReptile; +class CGiraffe; +class CTiger; +class CSnake; +class CTurtle; +class CAnimalIterator; +class CZoo; + +/************************************************************************************************************************* + Declaration of deprecated class types +**************************************************************************************************************************/ +typedef CWrapper CRTTIWrapper; +typedef CBase CRTTIBase; +typedef CAnimal CRTTIAnimal; +typedef CMammal CRTTIMammal; +typedef CReptile CRTTIReptile; +typedef CGiraffe CRTTIGiraffe; +typedef CTiger CRTTITiger; +typedef CSnake CRTTISnake; +typedef CTurtle CRTTITurtle; +typedef CAnimalIterator CRTTIAnimalIterator; +typedef CZoo CRTTIZoo; + +/************************************************************************************************************************* + Declaration of shared pointer types +**************************************************************************************************************************/ +typedef std::shared_ptr PWrapper; +typedef std::shared_ptr PBase; +typedef std::shared_ptr PAnimal; +typedef std::shared_ptr PMammal; +typedef std::shared_ptr PReptile; +typedef std::shared_ptr PGiraffe; +typedef std::shared_ptr PTiger; +typedef std::shared_ptr PSnake; +typedef std::shared_ptr PTurtle; +typedef std::shared_ptr PAnimalIterator; +typedef std::shared_ptr PZoo; + +/************************************************************************************************************************* + Declaration of deprecated shared pointer types +**************************************************************************************************************************/ +typedef PWrapper PRTTIWrapper; +typedef PBase PRTTIBase; +typedef PAnimal PRTTIAnimal; +typedef PMammal PRTTIMammal; +typedef PReptile PRTTIReptile; +typedef PGiraffe PRTTIGiraffe; +typedef PTiger PRTTITiger; +typedef PSnake PRTTISnake; +typedef PTurtle PRTTITurtle; +typedef PAnimalIterator PRTTIAnimalIterator; +typedef PZoo PRTTIZoo; + +/************************************************************************************************************************* + rtti_cast Definition +**************************************************************************************************************************/ +template +inline std::shared_ptr rtti_cast(PBase obj); + +using RTTI_ClassHash = std::array; + +/************************************************************************************************************************* + classParam Definition +**************************************************************************************************************************/ + +template class classParam { +private: + const T* m_ptr; + +public: + classParam(const T* ptr) + : m_ptr (ptr) + { + } + + classParam(std::shared_ptr sharedPtr) + : m_ptr (sharedPtr.get()) + { + } + + RTTIHandle GetHandle() + { + if (m_ptr != nullptr) + return m_ptr->handle(); + return nullptr; + } +}; + +/************************************************************************************************************************* + Class ERTTIException +**************************************************************************************************************************/ +class ERTTIException : public std::exception { +protected: + /** + * Error code for the Exception. + */ + RTTIResult m_errorCode; + /** + * Error message for the Exception. + */ + std::string m_errorMessage; + +public: + /** + * Exception Constructor. + */ + ERTTIException(RTTIResult errorCode, const std::string & sErrorMessage) + : m_errorMessage("RTTI Error " + std::to_string(errorCode) + " (" + sErrorMessage + ")") + { + m_errorCode = errorCode; + } + + /** + * Returns error code + */ + RTTIResult getErrorCode() const noexcept + { + return m_errorCode; + } + + /** + * Returns error message + */ + const char* what() const noexcept + { + return m_errorMessage.c_str(); + } + +}; + +/************************************************************************************************************************* + Class CInputVector +**************************************************************************************************************************/ +template +class CInputVector { +private: + + const T* m_data; + size_t m_size; + +public: + + CInputVector( const std::vector& vec) + : m_data( vec.data() ), m_size( vec.size() ) + { + } + + CInputVector( const T* in_data, size_t in_size) + : m_data( in_data ), m_size(in_size ) + { + } + + const T* data() const + { + return m_data; + } + + size_t size() const + { + return m_size; + } + +}; + +// declare deprecated class name +template +using CRTTIInputVector = CInputVector; + +/************************************************************************************************************************* + Class CWrapper +**************************************************************************************************************************/ +class CWrapper { +public: + + CWrapper(void* pSymbolLookupMethod) + { + CheckError(nullptr, initWrapperTable(&m_WrapperTable)); + CheckError(nullptr, loadWrapperTableFromSymbolLookupMethod(&m_WrapperTable, pSymbolLookupMethod)); + + CheckError(nullptr, checkBinaryVersion()); + } + + CWrapper(const std::string &sFileName) + { + CheckError(nullptr, initWrapperTable(&m_WrapperTable)); + CheckError(nullptr, loadWrapperTable(&m_WrapperTable, sFileName.c_str())); + + CheckError(nullptr, checkBinaryVersion()); + } + + static PWrapper loadLibrary(const std::string &sFileName) + { + return std::make_shared(sFileName); + } + + static PWrapper loadLibraryFromSymbolLookupMethod(void* pSymbolLookupMethod) + { + return std::make_shared(pSymbolLookupMethod); + } + + ~CWrapper() + { + releaseWrapperTable(&m_WrapperTable); + } + + inline void CheckError(CBase * pBaseClass, RTTIResult nResult); + + inline void GetVersion(RTTI_uint32 & nMajor, RTTI_uint32 & nMinor, RTTI_uint32 & nMicro); + inline bool GetLastError(classParam pInstance, std::string & sErrorMessage); + inline void ReleaseInstance(classParam pInstance); + inline void AcquireInstance(classParam pInstance); + inline void InjectComponent(const std::string & sNameSpace, const RTTI_pvoid pSymbolAddressMethod); + inline RTTI_pvoid GetSymbolLookupMethod(); + inline bool ImplementsInterface(classParam pObject, const CInputVector & ClassHashBuffer); + inline PZoo CreateZoo(); + +private: + sRTTIDynamicWrapperTable m_WrapperTable; + + RTTIResult checkBinaryVersion() + { + RTTI_uint32 nMajor, nMinor, nMicro; + GetVersion(nMajor, nMinor, nMicro); + if ( (nMajor != RTTI_VERSION_MAJOR) || (nMinor < RTTI_VERSION_MINOR) ) { + return RTTI_ERROR_INCOMPATIBLEBINARYVERSION; + } + return RTTI_SUCCESS; + } + RTTIResult initWrapperTable(sRTTIDynamicWrapperTable * pWrapperTable); + RTTIResult releaseWrapperTable(sRTTIDynamicWrapperTable * pWrapperTable); + RTTIResult loadWrapperTable(sRTTIDynamicWrapperTable * pWrapperTable, const char * pLibraryFileName); + RTTIResult loadWrapperTableFromSymbolLookupMethod(sRTTIDynamicWrapperTable * pWrapperTable, void* pSymbolLookupMethod); + + friend class CBase; + friend class CAnimal; + friend class CMammal; + friend class CReptile; + friend class CGiraffe; + friend class CTiger; + friend class CSnake; + friend class CTurtle; + friend class CAnimalIterator; + friend class CZoo; + +}; + + +/************************************************************************************************************************* + Class CBase +**************************************************************************************************************************/ +class CBase { +public: + static inline const std::string &getClassName(); + static inline const RTTI_ClassHash &getClassHash(); + +protected: + /* Wrapper Object that created the class. */ + CWrapper * m_pWrapper; + /* Handle to Instance in library*/ + RTTIHandle m_pHandle; + + /* Checks for an Error code and raises Exceptions */ + void CheckError(RTTIResult nResult) + { + if (m_pWrapper != nullptr) + m_pWrapper->CheckError(this, nResult); + } +public: + /** + * CBase::CBase - Constructor for Base class. + */ + CBase(CWrapper * pWrapper, RTTIHandle pHandle) + : m_pWrapper(pWrapper), m_pHandle(pHandle) + { + } + + /** + * CBase::~CBase - Destructor for Base class. + */ + virtual ~CBase() + { + if (m_pWrapper != nullptr) + m_pWrapper->ReleaseInstance(this); + m_pWrapper = nullptr; + } + + /** + * CBase::handle - Returns handle to instance. + */ + RTTIHandle handle() const + { + return m_pHandle; + } + + /** + * CBase::wrapper - Returns wrapper instance. + */ + CWrapper * wrapper() const + { + return m_pWrapper; + } + + friend class CWrapper; +}; + +/************************************************************************************************************************* + Class CAnimal +**************************************************************************************************************************/ +class CAnimal : public CBase { +public: + static inline const std::string &getClassName(); + static inline const RTTI_ClassHash &getClassHash(); + + /** + * CAnimal::CAnimal - Constructor for Animal class. + */ + CAnimal(CWrapper* pWrapper, RTTIHandle pHandle) + : CBase(pWrapper, pHandle) + { + } + + inline std::string Name(); +}; + +/************************************************************************************************************************* + Class CMammal +**************************************************************************************************************************/ +class CMammal : public CAnimal { +public: + static inline const std::string &getClassName(); + static inline const RTTI_ClassHash &getClassHash(); + + /** + * CMammal::CMammal - Constructor for Mammal class. + */ + CMammal(CWrapper* pWrapper, RTTIHandle pHandle) + : CAnimal(pWrapper, pHandle) + { + } + +}; + +/************************************************************************************************************************* + Class CReptile +**************************************************************************************************************************/ +class CReptile : public CAnimal { +public: + static inline const std::string &getClassName(); + static inline const RTTI_ClassHash &getClassHash(); + + /** + * CReptile::CReptile - Constructor for Reptile class. + */ + CReptile(CWrapper* pWrapper, RTTIHandle pHandle) + : CAnimal(pWrapper, pHandle) + { + } + +}; + +/************************************************************************************************************************* + Class CGiraffe +**************************************************************************************************************************/ +class CGiraffe : public CMammal { +public: + static inline const std::string &getClassName(); + static inline const RTTI_ClassHash &getClassHash(); + + /** + * CGiraffe::CGiraffe - Constructor for Giraffe class. + */ + CGiraffe(CWrapper* pWrapper, RTTIHandle pHandle) + : CMammal(pWrapper, pHandle) + { + } + +}; + +/************************************************************************************************************************* + Class CTiger +**************************************************************************************************************************/ +class CTiger : public CMammal { +public: + static inline const std::string &getClassName(); + static inline const RTTI_ClassHash &getClassHash(); + + /** + * CTiger::CTiger - Constructor for Tiger class. + */ + CTiger(CWrapper* pWrapper, RTTIHandle pHandle) + : CMammal(pWrapper, pHandle) + { + } + + inline void Roar(); +}; + +/************************************************************************************************************************* + Class CSnake +**************************************************************************************************************************/ +class CSnake : public CReptile { +public: + static inline const std::string &getClassName(); + static inline const RTTI_ClassHash &getClassHash(); + + /** + * CSnake::CSnake - Constructor for Snake class. + */ + CSnake(CWrapper* pWrapper, RTTIHandle pHandle) + : CReptile(pWrapper, pHandle) + { + } + +}; + +/************************************************************************************************************************* + Class CTurtle +**************************************************************************************************************************/ +class CTurtle : public CReptile { +public: + static inline const std::string &getClassName(); + static inline const RTTI_ClassHash &getClassHash(); + + /** + * CTurtle::CTurtle - Constructor for Turtle class. + */ + CTurtle(CWrapper* pWrapper, RTTIHandle pHandle) + : CReptile(pWrapper, pHandle) + { + } + +}; + +/************************************************************************************************************************* + Class CAnimalIterator +**************************************************************************************************************************/ +class CAnimalIterator : public CBase { +public: + static inline const std::string &getClassName(); + static inline const RTTI_ClassHash &getClassHash(); + + /** + * CAnimalIterator::CAnimalIterator - Constructor for AnimalIterator class. + */ + CAnimalIterator(CWrapper* pWrapper, RTTIHandle pHandle) + : CBase(pWrapper, pHandle) + { + } + + inline PAnimal GetNextAnimal(); +}; + +/************************************************************************************************************************* + Class CZoo +**************************************************************************************************************************/ +class CZoo : public CBase { +public: + static inline const std::string &getClassName(); + static inline const RTTI_ClassHash &getClassHash(); + + /** + * CZoo::CZoo - Constructor for Zoo class. + */ + CZoo(CWrapper* pWrapper, RTTIHandle pHandle) + : CBase(pWrapper, pHandle) + { + } + + inline PAnimalIterator Iterator(); +}; + +/************************************************************************************************************************* + RTTI static getClassName implementations +**************************************************************************************************************************/ + + const std::string &CBase::getClassName() + { + static const std::string s_sClassName = "Base"; + return s_sClassName; + } + + const RTTI_ClassHash &CBase::getClassHash() + { + // MD5(Base): 095A1B43EFFEC73955E31E790438DE49 + static const RTTI_ClassHash s_sClassHash = { 0x09, 0x5A, 0x1B, 0x43, 0xEF, 0xFE, 0xC7, 0x39, 0x55, 0xE3, 0x1E, 0x79, 0x04, 0x38, 0xDE, 0x49, }; + return s_sClassHash; + } + + const std::string &CAnimal::getClassName() + { + static const std::string s_sClassName = "Animal"; + return s_sClassName; + } + + const RTTI_ClassHash &CAnimal::getClassHash() + { + // MD5(Animal): 161E7CE7BFDC89AB4B9F52C1D4C94212 + static const RTTI_ClassHash s_sClassHash = { 0x16, 0x1E, 0x7C, 0xE7, 0xBF, 0xDC, 0x89, 0xAB, 0x4B, 0x9F, 0x52, 0xC1, 0xD4, 0xC9, 0x42, 0x12, }; + return s_sClassHash; + } + + const std::string &CMammal::getClassName() + { + static const std::string s_sClassName = "Mammal"; + return s_sClassName; + } + + const RTTI_ClassHash &CMammal::getClassHash() + { + // MD5(Mammal): 37426113D129E79F548F4C90930FA697 + static const RTTI_ClassHash s_sClassHash = { 0x37, 0x42, 0x61, 0x13, 0xD1, 0x29, 0xE7, 0x9F, 0x54, 0x8F, 0x4C, 0x90, 0x93, 0x0F, 0xA6, 0x97, }; + return s_sClassHash; + } + + const std::string &CReptile::getClassName() + { + static const std::string s_sClassName = "Reptile"; + return s_sClassName; + } + + const RTTI_ClassHash &CReptile::getClassHash() + { + // MD5(Reptile): AA645186A4B5F3F27952C2FA5485FAB2 + static const RTTI_ClassHash s_sClassHash = { 0xAA, 0x64, 0x51, 0x86, 0xA4, 0xB5, 0xF3, 0xF2, 0x79, 0x52, 0xC2, 0xFA, 0x54, 0x85, 0xFA, 0xB2, }; + return s_sClassHash; + } + + const std::string &CGiraffe::getClassName() + { + static const std::string s_sClassName = "Giraffe"; + return s_sClassName; + } + + const RTTI_ClassHash &CGiraffe::getClassHash() + { + // MD5(Giraffe): 427DEBB81D265A0EDD8789F30B11BEB6 + static const RTTI_ClassHash s_sClassHash = { 0x42, 0x7D, 0xEB, 0xB8, 0x1D, 0x26, 0x5A, 0x0E, 0xDD, 0x87, 0x89, 0xF3, 0x0B, 0x11, 0xBE, 0xB6, }; + return s_sClassHash; + } + + const std::string &CTiger::getClassName() + { + static const std::string s_sClassName = "Tiger"; + return s_sClassName; + } + + const RTTI_ClassHash &CTiger::getClassHash() + { + // MD5(Tiger): 454C9843110686BF6F67CE5115B66617 + static const RTTI_ClassHash s_sClassHash = { 0x45, 0x4C, 0x98, 0x43, 0x11, 0x06, 0x86, 0xBF, 0x6F, 0x67, 0xCE, 0x51, 0x15, 0xB6, 0x66, 0x17, }; + return s_sClassHash; + } + + const std::string &CSnake::getClassName() + { + static const std::string s_sClassName = "Snake"; + return s_sClassName; + } + + const RTTI_ClassHash &CSnake::getClassHash() + { + // MD5(Snake): DFA90F1B4EB3AFFBD3B46AF34ED2477C + static const RTTI_ClassHash s_sClassHash = { 0xDF, 0xA9, 0x0F, 0x1B, 0x4E, 0xB3, 0xAF, 0xFB, 0xD3, 0xB4, 0x6A, 0xF3, 0x4E, 0xD2, 0x47, 0x7C, }; + return s_sClassHash; + } + + const std::string &CTurtle::getClassName() + { + static const std::string s_sClassName = "Turtle"; + return s_sClassName; + } + + const RTTI_ClassHash &CTurtle::getClassHash() + { + // MD5(Turtle): 06DEBA5908B007EB6F32D8D95F3F61B5 + static const RTTI_ClassHash s_sClassHash = { 0x06, 0xDE, 0xBA, 0x59, 0x08, 0xB0, 0x07, 0xEB, 0x6F, 0x32, 0xD8, 0xD9, 0x5F, 0x3F, 0x61, 0xB5, }; + return s_sClassHash; + } + + const std::string &CAnimalIterator::getClassName() + { + static const std::string s_sClassName = "AnimalIterator"; + return s_sClassName; + } + + const RTTI_ClassHash &CAnimalIterator::getClassHash() + { + // MD5(AnimalIterator): C2B36A84C6C032204E5C923C581071E7 + static const RTTI_ClassHash s_sClassHash = { 0xC2, 0xB3, 0x6A, 0x84, 0xC6, 0xC0, 0x32, 0x20, 0x4E, 0x5C, 0x92, 0x3C, 0x58, 0x10, 0x71, 0xE7, }; + return s_sClassHash; + } + + const std::string &CZoo::getClassName() + { + static const std::string s_sClassName = "Zoo"; + return s_sClassName; + } + + const RTTI_ClassHash &CZoo::getClassHash() + { + // MD5(Zoo): BFA888A354DB97C7CBEFB9D050B94CA3 + static const RTTI_ClassHash s_sClassHash = { 0xBF, 0xA8, 0x88, 0xA3, 0x54, 0xDB, 0x97, 0xC7, 0xCB, 0xEF, 0xB9, 0xD0, 0x50, 0xB9, 0x4C, 0xA3, }; + return s_sClassHash; + } + + +/************************************************************************************************************************* + RTTI cast implementation +**************************************************************************************************************************/ + + template + std::shared_ptr rtti_cast(PBase pObj) + { + static_assert(std::is_convertible::value, "T must be convertible to RTTI::CBase"); + + if (pObj) { + CWrapper *pWrapper = pObj->wrapper(); + const RTTI_ClassHash & ClassHash = T::getClassHash(); + CInputVector ClassHashBuffer(ClassHash.data(), ClassHash.size()); + if (pWrapper->ImplementsInterface(pObj.get(), ClassHashBuffer)) { + pWrapper->AcquireInstance(pObj); + return std::make_shared(pWrapper, pObj->handle()); + } + } + + return nullptr; + } + + /** + * CWrapper::GetVersion - retrieves the binary version of this library. + * @param[out] nMajor - returns the major version of this library + * @param[out] nMinor - returns the minor version of this library + * @param[out] nMicro - returns the micro version of this library + */ + inline void CWrapper::GetVersion(RTTI_uint32 & nMajor, RTTI_uint32 & nMinor, RTTI_uint32 & nMicro) + { + CheckError(nullptr,m_WrapperTable.m_GetVersion(&nMajor, &nMinor, &nMicro)); + } + + /** + * CWrapper::GetLastError - Returns the last error recorded on this object + * @param[in] pInstance - Instance Handle + * @param[out] sErrorMessage - Message of the last error + * @return Is there a last error to query + */ + inline bool CWrapper::GetLastError(classParam pInstance, std::string & sErrorMessage) + { + RTTIHandle hInstance = pInstance.GetHandle(); + RTTI_uint32 bytesNeededErrorMessage = 0; + RTTI_uint32 bytesWrittenErrorMessage = 0; + bool resultHasError = 0; + CheckError(nullptr,m_WrapperTable.m_GetLastError(hInstance, 0, &bytesNeededErrorMessage, nullptr, &resultHasError)); + std::vector bufferErrorMessage(bytesNeededErrorMessage); + CheckError(nullptr,m_WrapperTable.m_GetLastError(hInstance, bytesNeededErrorMessage, &bytesWrittenErrorMessage, &bufferErrorMessage[0], &resultHasError)); + sErrorMessage = std::string(&bufferErrorMessage[0]); + + return resultHasError; + } + + /** + * CWrapper::ReleaseInstance - Releases shared ownership of an Instance + * @param[in] pInstance - Instance Handle + */ + inline void CWrapper::ReleaseInstance(classParam pInstance) + { + RTTIHandle hInstance = pInstance.GetHandle(); + CheckError(nullptr,m_WrapperTable.m_ReleaseInstance(hInstance)); + } + + /** + * CWrapper::AcquireInstance - Acquires shared ownership of an Instance + * @param[in] pInstance - Instance Handle + */ + inline void CWrapper::AcquireInstance(classParam pInstance) + { + RTTIHandle hInstance = pInstance.GetHandle(); + CheckError(nullptr,m_WrapperTable.m_AcquireInstance(hInstance)); + } + + /** + * CWrapper::InjectComponent - Injects an imported component for usage within this component + * @param[in] sNameSpace - NameSpace of the injected component + * @param[in] pSymbolAddressMethod - Address of the SymbolAddressMethod of the injected component + */ + inline void CWrapper::InjectComponent(const std::string & sNameSpace, const RTTI_pvoid pSymbolAddressMethod) + { + CheckError(nullptr,m_WrapperTable.m_InjectComponent(sNameSpace.c_str(), pSymbolAddressMethod)); + + bool bNameSpaceFound = false; + if (!bNameSpaceFound) + throw ERTTIException(RTTI_ERROR_COULDNOTLOADLIBRARY, "Unknown namespace " + sNameSpace); + } + + /** + * CWrapper::GetSymbolLookupMethod - Returns the address of the SymbolLookupMethod + * @return Address of the SymbolAddressMethod + */ + inline RTTI_pvoid CWrapper::GetSymbolLookupMethod() + { + RTTI_pvoid resultSymbolLookupMethod = 0; + CheckError(nullptr,m_WrapperTable.m_GetSymbolLookupMethod(&resultSymbolLookupMethod)); + + return resultSymbolLookupMethod; + } + + /** + * CWrapper::ImplementsInterface - Test whether an object implements a given interface + * @param[in] pObject - Instance Handle + * @param[in] ClassHashBuffer - Hashed class name of the interface to test + * @return Will be set to true if pInstance implements the interface, false otherwise + */ + inline bool CWrapper::ImplementsInterface(classParam pObject, const CInputVector & ClassHashBuffer) + { + RTTIHandle hObject = pObject.GetHandle(); + bool resultImplementsInterface = 0; + CheckError(nullptr,m_WrapperTable.m_ImplementsInterface(hObject, (RTTI_uint64)ClassHashBuffer.size(), ClassHashBuffer.data(), &resultImplementsInterface)); + + return resultImplementsInterface; + } + + /** + * CWrapper::CreateZoo - Create a new zoo with animals + * @return + */ + inline PZoo CWrapper::CreateZoo() + { + RTTIHandle hInstance = nullptr; + CheckError(nullptr,m_WrapperTable.m_CreateZoo(&hInstance)); + + if (!hInstance) { + CheckError(nullptr,RTTI_ERROR_INVALIDPARAM); + } + return std::make_shared(this, hInstance); + } + + inline void CWrapper::CheckError(CBase * pBaseClass, RTTIResult nResult) + { + if (nResult != 0) { + std::string sErrorMessage; + if (pBaseClass != nullptr) { + GetLastError(pBaseClass, sErrorMessage); + } + throw ERTTIException(nResult, sErrorMessage); + } + } + + + inline RTTIResult CWrapper::initWrapperTable(sRTTIDynamicWrapperTable * pWrapperTable) + { + if (pWrapperTable == nullptr) + return RTTI_ERROR_INVALIDPARAM; + + pWrapperTable->m_LibraryHandle = nullptr; + pWrapperTable->m_Animal_Name = nullptr; + pWrapperTable->m_Tiger_Roar = nullptr; + pWrapperTable->m_AnimalIterator_GetNextAnimal = nullptr; + pWrapperTable->m_Zoo_Iterator = nullptr; + pWrapperTable->m_GetVersion = nullptr; + pWrapperTable->m_GetLastError = nullptr; + pWrapperTable->m_ReleaseInstance = nullptr; + pWrapperTable->m_AcquireInstance = nullptr; + pWrapperTable->m_InjectComponent = nullptr; + pWrapperTable->m_GetSymbolLookupMethod = nullptr; + pWrapperTable->m_ImplementsInterface = nullptr; + pWrapperTable->m_CreateZoo = nullptr; + + return RTTI_SUCCESS; + } + + inline RTTIResult CWrapper::releaseWrapperTable(sRTTIDynamicWrapperTable * pWrapperTable) + { + if (pWrapperTable == nullptr) + return RTTI_ERROR_INVALIDPARAM; + + if (pWrapperTable->m_LibraryHandle != nullptr) { + #ifdef _WIN32 + HMODULE hModule = (HMODULE) pWrapperTable->m_LibraryHandle; + FreeLibrary(hModule); + #else // _WIN32 + dlclose(pWrapperTable->m_LibraryHandle); + #endif // _WIN32 + return initWrapperTable(pWrapperTable); + } + + return RTTI_SUCCESS; + } + + inline RTTIResult CWrapper::loadWrapperTable(sRTTIDynamicWrapperTable * pWrapperTable, const char * pLibraryFileName) + { + if (pWrapperTable == nullptr) + return RTTI_ERROR_INVALIDPARAM; + if (pLibraryFileName == nullptr) + return RTTI_ERROR_INVALIDPARAM; + + #ifdef _WIN32 + // Convert filename to UTF16-string + int nLength = (int)strlen(pLibraryFileName); + int nBufferSize = nLength * 2 + 2; + std::vector wsLibraryFileName(nBufferSize); + int nResult = MultiByteToWideChar(CP_UTF8, 0, pLibraryFileName, nLength, &wsLibraryFileName[0], nBufferSize); + if (nResult == 0) + return RTTI_ERROR_COULDNOTLOADLIBRARY; + + HMODULE hLibrary = LoadLibraryW(wsLibraryFileName.data()); + if (hLibrary == 0) + return RTTI_ERROR_COULDNOTLOADLIBRARY; + #else // _WIN32 + void* hLibrary = dlopen(pLibraryFileName, RTLD_LAZY); + if (hLibrary == 0) + return RTTI_ERROR_COULDNOTLOADLIBRARY; + dlerror(); + #endif // _WIN32 + + #ifdef _WIN32 + pWrapperTable->m_Animal_Name = (PRTTIAnimal_NamePtr) GetProcAddress(hLibrary, "rtti_animal_name"); + #else // _WIN32 + pWrapperTable->m_Animal_Name = (PRTTIAnimal_NamePtr) dlsym(hLibrary, "rtti_animal_name"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_Animal_Name == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_Tiger_Roar = (PRTTITiger_RoarPtr) GetProcAddress(hLibrary, "rtti_tiger_roar"); + #else // _WIN32 + pWrapperTable->m_Tiger_Roar = (PRTTITiger_RoarPtr) dlsym(hLibrary, "rtti_tiger_roar"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_Tiger_Roar == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_AnimalIterator_GetNextAnimal = (PRTTIAnimalIterator_GetNextAnimalPtr) GetProcAddress(hLibrary, "rtti_animaliterator_getnextanimal"); + #else // _WIN32 + pWrapperTable->m_AnimalIterator_GetNextAnimal = (PRTTIAnimalIterator_GetNextAnimalPtr) dlsym(hLibrary, "rtti_animaliterator_getnextanimal"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_AnimalIterator_GetNextAnimal == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_Zoo_Iterator = (PRTTIZoo_IteratorPtr) GetProcAddress(hLibrary, "rtti_zoo_iterator"); + #else // _WIN32 + pWrapperTable->m_Zoo_Iterator = (PRTTIZoo_IteratorPtr) dlsym(hLibrary, "rtti_zoo_iterator"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_Zoo_Iterator == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_GetVersion = (PRTTIGetVersionPtr) GetProcAddress(hLibrary, "rtti_getversion"); + #else // _WIN32 + pWrapperTable->m_GetVersion = (PRTTIGetVersionPtr) dlsym(hLibrary, "rtti_getversion"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_GetVersion == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_GetLastError = (PRTTIGetLastErrorPtr) GetProcAddress(hLibrary, "rtti_getlasterror"); + #else // _WIN32 + pWrapperTable->m_GetLastError = (PRTTIGetLastErrorPtr) dlsym(hLibrary, "rtti_getlasterror"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_GetLastError == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_ReleaseInstance = (PRTTIReleaseInstancePtr) GetProcAddress(hLibrary, "rtti_releaseinstance"); + #else // _WIN32 + pWrapperTable->m_ReleaseInstance = (PRTTIReleaseInstancePtr) dlsym(hLibrary, "rtti_releaseinstance"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_ReleaseInstance == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_AcquireInstance = (PRTTIAcquireInstancePtr) GetProcAddress(hLibrary, "rtti_acquireinstance"); + #else // _WIN32 + pWrapperTable->m_AcquireInstance = (PRTTIAcquireInstancePtr) dlsym(hLibrary, "rtti_acquireinstance"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_AcquireInstance == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_InjectComponent = (PRTTIInjectComponentPtr) GetProcAddress(hLibrary, "rtti_injectcomponent"); + #else // _WIN32 + pWrapperTable->m_InjectComponent = (PRTTIInjectComponentPtr) dlsym(hLibrary, "rtti_injectcomponent"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_InjectComponent == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_GetSymbolLookupMethod = (PRTTIGetSymbolLookupMethodPtr) GetProcAddress(hLibrary, "rtti_getsymbollookupmethod"); + #else // _WIN32 + pWrapperTable->m_GetSymbolLookupMethod = (PRTTIGetSymbolLookupMethodPtr) dlsym(hLibrary, "rtti_getsymbollookupmethod"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_GetSymbolLookupMethod == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_ImplementsInterface = (PRTTIImplementsInterfacePtr) GetProcAddress(hLibrary, "rtti_implementsinterface"); + #else // _WIN32 + pWrapperTable->m_ImplementsInterface = (PRTTIImplementsInterfacePtr) dlsym(hLibrary, "rtti_implementsinterface"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_ImplementsInterface == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + #ifdef _WIN32 + pWrapperTable->m_CreateZoo = (PRTTICreateZooPtr) GetProcAddress(hLibrary, "rtti_createzoo"); + #else // _WIN32 + pWrapperTable->m_CreateZoo = (PRTTICreateZooPtr) dlsym(hLibrary, "rtti_createzoo"); + dlerror(); + #endif // _WIN32 + if (pWrapperTable->m_CreateZoo == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + pWrapperTable->m_LibraryHandle = hLibrary; + return RTTI_SUCCESS; + } + + inline RTTIResult CWrapper::loadWrapperTableFromSymbolLookupMethod(sRTTIDynamicWrapperTable * pWrapperTable, void* pSymbolLookupMethod) +{ + if (pWrapperTable == nullptr) + return RTTI_ERROR_INVALIDPARAM; + if (pSymbolLookupMethod == nullptr) + return RTTI_ERROR_INVALIDPARAM; + + typedef RTTIResult(*SymbolLookupType)(const char*, void**); + + SymbolLookupType pLookup = (SymbolLookupType)pSymbolLookupMethod; + + RTTIResult eLookupError = RTTI_SUCCESS; + eLookupError = (*pLookup)("rtti_animal_name", (void**)&(pWrapperTable->m_Animal_Name)); + if ( (eLookupError != 0) || (pWrapperTable->m_Animal_Name == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_tiger_roar", (void**)&(pWrapperTable->m_Tiger_Roar)); + if ( (eLookupError != 0) || (pWrapperTable->m_Tiger_Roar == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_animaliterator_getnextanimal", (void**)&(pWrapperTable->m_AnimalIterator_GetNextAnimal)); + if ( (eLookupError != 0) || (pWrapperTable->m_AnimalIterator_GetNextAnimal == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_zoo_iterator", (void**)&(pWrapperTable->m_Zoo_Iterator)); + if ( (eLookupError != 0) || (pWrapperTable->m_Zoo_Iterator == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_getversion", (void**)&(pWrapperTable->m_GetVersion)); + if ( (eLookupError != 0) || (pWrapperTable->m_GetVersion == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_getlasterror", (void**)&(pWrapperTable->m_GetLastError)); + if ( (eLookupError != 0) || (pWrapperTable->m_GetLastError == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_releaseinstance", (void**)&(pWrapperTable->m_ReleaseInstance)); + if ( (eLookupError != 0) || (pWrapperTable->m_ReleaseInstance == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_acquireinstance", (void**)&(pWrapperTable->m_AcquireInstance)); + if ( (eLookupError != 0) || (pWrapperTable->m_AcquireInstance == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_injectcomponent", (void**)&(pWrapperTable->m_InjectComponent)); + if ( (eLookupError != 0) || (pWrapperTable->m_InjectComponent == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_getsymbollookupmethod", (void**)&(pWrapperTable->m_GetSymbolLookupMethod)); + if ( (eLookupError != 0) || (pWrapperTable->m_GetSymbolLookupMethod == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_implementsinterface", (void**)&(pWrapperTable->m_ImplementsInterface)); + if ( (eLookupError != 0) || (pWrapperTable->m_ImplementsInterface == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + eLookupError = (*pLookup)("rtti_createzoo", (void**)&(pWrapperTable->m_CreateZoo)); + if ( (eLookupError != 0) || (pWrapperTable->m_CreateZoo == nullptr) ) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + + return RTTI_SUCCESS; +} + + + + /** + * Method definitions for class CBase + */ + + /** + * Method definitions for class CAnimal + */ + + /** + * CAnimal::Name - Get the name of the animal + * @return + */ + std::string CAnimal::Name() + { + RTTI_uint32 bytesNeededResult = 0; + RTTI_uint32 bytesWrittenResult = 0; + CheckError(m_pWrapper->m_WrapperTable.m_Animal_Name(m_pHandle, 0, &bytesNeededResult, nullptr)); + std::vector bufferResult(bytesNeededResult); + CheckError(m_pWrapper->m_WrapperTable.m_Animal_Name(m_pHandle, bytesNeededResult, &bytesWrittenResult, &bufferResult[0])); + + return std::string(&bufferResult[0]); + } + + /** + * Method definitions for class CMammal + */ + + /** + * Method definitions for class CReptile + */ + + /** + * Method definitions for class CGiraffe + */ + + /** + * Method definitions for class CTiger + */ + + /** + * CTiger::Roar - Roar like a tiger + */ + void CTiger::Roar() + { + CheckError(m_pWrapper->m_WrapperTable.m_Tiger_Roar(m_pHandle)); + } + + /** + * Method definitions for class CSnake + */ + + /** + * Method definitions for class CTurtle + */ + + /** + * Method definitions for class CAnimalIterator + */ + + /** + * CAnimalIterator::GetNextAnimal - Return next animal + * @return + */ + PAnimal CAnimalIterator::GetNextAnimal() + { + RTTIHandle hAnimal = nullptr; + CheckError(m_pWrapper->m_WrapperTable.m_AnimalIterator_GetNextAnimal(m_pHandle, &hAnimal)); + + if (hAnimal) { + return std::make_shared(m_pWrapper, hAnimal); + } else { + return nullptr; + } + } + + /** + * Method definitions for class CZoo + */ + + /** + * CZoo::Iterator - Return an iterator over all zoo animals + * @return + */ + PAnimalIterator CZoo::Iterator() + { + RTTIHandle hIterator = nullptr; + CheckError(m_pWrapper->m_WrapperTable.m_Zoo_Iterator(m_pHandle, &hIterator)); + + if (!hIterator) { + CheckError(RTTI_ERROR_INVALIDPARAM); + } + return std::make_shared(m_pWrapper, hIterator); + } + +} // namespace RTTI + +#endif // __RTTI_CPPHEADER_DYNAMIC_CPP + diff --git a/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_types.hpp b/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_types.hpp new file mode 100644 index 00000000..19fec9a2 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Bindings/CppDynamic/rtti_types.hpp @@ -0,0 +1,125 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++-Header file with basic types in +order to allow an easy use of RTTI + +Interface version: 1.0.0 + +*/ + +#ifndef __RTTI_TYPES_HEADER_CPP +#define __RTTI_TYPES_HEADER_CPP + + +/************************************************************************************************************************* + Scalar types definition +**************************************************************************************************************************/ + +#ifdef RTTI_USELEGACYINTEGERTYPES + +typedef unsigned char RTTI_uint8; +typedef unsigned short RTTI_uint16 ; +typedef unsigned int RTTI_uint32; +typedef unsigned long long RTTI_uint64; +typedef char RTTI_int8; +typedef short RTTI_int16; +typedef int RTTI_int32; +typedef long long RTTI_int64; + +#else // RTTI_USELEGACYINTEGERTYPES + +#include + +typedef uint8_t RTTI_uint8; +typedef uint16_t RTTI_uint16; +typedef uint32_t RTTI_uint32; +typedef uint64_t RTTI_uint64; +typedef int8_t RTTI_int8; +typedef int16_t RTTI_int16; +typedef int32_t RTTI_int32; +typedef int64_t RTTI_int64 ; + +#endif // RTTI_USELEGACYINTEGERTYPES + +typedef float RTTI_single; +typedef double RTTI_double; + +/************************************************************************************************************************* + General type definitions +**************************************************************************************************************************/ + +typedef RTTI_int32 RTTIResult; +typedef void * RTTIHandle; +typedef void * RTTI_pvoid; + +/************************************************************************************************************************* + Version for RTTI +**************************************************************************************************************************/ + +#define RTTI_VERSION_MAJOR 1 +#define RTTI_VERSION_MINOR 0 +#define RTTI_VERSION_MICRO 0 +#define RTTI_VERSION_PRERELEASEINFO "" +#define RTTI_VERSION_BUILDINFO "" + +/************************************************************************************************************************* + Error constants for RTTI +**************************************************************************************************************************/ + +#define RTTI_SUCCESS 0 +#define RTTI_ERROR_NOTIMPLEMENTED 1 +#define RTTI_ERROR_INVALIDPARAM 2 +#define RTTI_ERROR_INVALIDCAST 3 +#define RTTI_ERROR_BUFFERTOOSMALL 4 +#define RTTI_ERROR_GENERICEXCEPTION 5 +#define RTTI_ERROR_COULDNOTLOADLIBRARY 6 +#define RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT 7 +#define RTTI_ERROR_INCOMPATIBLEBINARYVERSION 8 + +/************************************************************************************************************************* + Error strings for RTTI +**************************************************************************************************************************/ + +inline const char * RTTI_GETERRORSTRING (RTTIResult nErrorCode) { + switch (nErrorCode) { + case RTTI_SUCCESS: return "no error"; + case RTTI_ERROR_NOTIMPLEMENTED: return "functionality not implemented"; + case RTTI_ERROR_INVALIDPARAM: return "an invalid parameter was passed"; + case RTTI_ERROR_INVALIDCAST: return "a type cast failed"; + case RTTI_ERROR_BUFFERTOOSMALL: return "a provided buffer is too small"; + case RTTI_ERROR_GENERICEXCEPTION: return "a generic exception occurred"; + case RTTI_ERROR_COULDNOTLOADLIBRARY: return "the library could not be loaded"; + case RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT: return "a required exported symbol could not be found in the library"; + case RTTI_ERROR_INCOMPATIBLEBINARYVERSION: return "the version of the binary interface does not match the bindings interface"; + default: return "unknown error"; + } +} + +/************************************************************************************************************************* + Declaration of handle classes +**************************************************************************************************************************/ + +typedef RTTIHandle RTTI_Base; +typedef RTTIHandle RTTI_Animal; +typedef RTTIHandle RTTI_Mammal; +typedef RTTIHandle RTTI_Reptile; +typedef RTTIHandle RTTI_Giraffe; +typedef RTTIHandle RTTI_Tiger; +typedef RTTIHandle RTTI_Snake; +typedef RTTIHandle RTTI_Turtle; +typedef RTTIHandle RTTI_AnimalIterator; +typedef RTTIHandle RTTI_Zoo; + +namespace RTTI { + +} // namespace RTTI; + +// define legacy C-names for enums, structs and function types + +#endif // __RTTI_TYPES_HEADER_CPP diff --git a/Examples/RTTI/RTTI_component/Bindings/Python/RTTI.py b/Examples/RTTI/RTTI_component/Bindings/Python/RTTI.py new file mode 100644 index 00000000..db3d6e2b --- /dev/null +++ b/Examples/RTTI/RTTI_component/Bindings/Python/RTTI.py @@ -0,0 +1,523 @@ +'''++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated Python file in order to allow an easy + use of RTTI + +Interface version: 1.0.0 + +''' + + +import ctypes +import platform +import enum +import os + +name = "rtti" + +'''Definition of domain specific exception +''' +class ERTTIException(Exception): + def __init__(self, code, message = ''): + self._code = code + self._message = message + + def __str__(self): + if self._message: + return 'RTTIException ' + str(self._code) + ': '+ str(self._message) + return 'RTTIException ' + str(self._code) + +'''Definition of binding API version +''' +class BindingVersion(enum.IntEnum): + MAJOR = 1 + MINOR = 0 + MICRO = 0 + +'''Definition Error Codes +''' +class ErrorCodes(enum.IntEnum): + SUCCESS = 0 + NOTIMPLEMENTED = 1 + INVALIDPARAM = 2 + INVALIDCAST = 3 + BUFFERTOOSMALL = 4 + GENERICEXCEPTION = 5 + COULDNOTLOADLIBRARY = 6 + COULDNOTFINDLIBRARYEXPORT = 7 + INCOMPATIBLEBINARYVERSION = 8 + +'''Definition of Function Table +''' +class FunctionTable: + rtti_getversion = None + rtti_getlasterror = None + rtti_releaseinstance = None + rtti_acquireinstance = None + rtti_injectcomponent = None + rtti_getsymbollookupmethod = None + rtti_implementsinterface = None + rtti_createzoo = None + rtti_animal_name = None + rtti_tiger_roar = None + rtti_animaliterator_getnextanimal = None + rtti_zoo_iterator = None + + +'''Wrapper Class Implementation +''' +class Wrapper: + + def __init__(self, libraryName = None, symbolLookupMethodAddress = None): + ending = '' + if platform.system() == 'Windows': + ending = 'dll' + elif platform.system() == 'Linux': + ending = 'so' + elif platform.system() == 'Darwin': + ending = 'dylib' + else: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY) + + if (not libraryName) and (not symbolLookupMethodAddress): + libraryName = os.path.join(os.path.dirname(os.path.realpath(__file__)),'rtti') + + if libraryName is not None: + path = libraryName + '.' + ending + try: + self.lib = ctypes.CDLL(path) + except Exception as e: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(e) + '| "'+path + '"' ) + + self._loadFunctionTable() + elif symbolLookupMethodAddress is not None: + self.lib = FunctionTable() + self._loadFunctionTableFromMethod(symbolLookupMethodAddress) + else: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(e)) + + self._checkBinaryVersion() + + def _loadFunctionTableFromMethod(self, symbolLookupMethodAddress): + try: + symbolLookupMethodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_char_p, ctypes.POINTER(ctypes.c_void_p)) + symbolLookupMethod = symbolLookupMethodType(int(symbolLookupMethodAddress)) + + methodAddress = ctypes.c_void_p() + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_getversion")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32)) + self.lib.rtti_getversion = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_getlasterror")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_void_p, ctypes.c_uint64, ctypes.POINTER(ctypes.c_uint64), ctypes.c_char_p, ctypes.POINTER(ctypes.c_bool)) + self.lib.rtti_getlasterror = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_releaseinstance")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_void_p) + self.lib.rtti_releaseinstance = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_acquireinstance")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_void_p) + self.lib.rtti_acquireinstance = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_injectcomponent")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_char_p, ctypes.c_void_p) + self.lib.rtti_injectcomponent = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_getsymbollookupmethod")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.POINTER(ctypes.c_void_p)) + self.lib.rtti_getsymbollookupmethod = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_implementsinterface")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_void_p, ctypes.c_uint64, ctypes.POINTER(ctypes.c_uint8), ctypes.POINTER(ctypes.c_bool)) + self.lib.rtti_implementsinterface = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_createzoo")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.POINTER(ctypes.c_void_p)) + self.lib.rtti_createzoo = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_animal_name")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_void_p, ctypes.c_uint64, ctypes.POINTER(ctypes.c_uint64), ctypes.c_char_p) + self.lib.rtti_animal_name = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_tiger_roar")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_void_p) + self.lib.rtti_tiger_roar = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_animaliterator_getnextanimal")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)) + self.lib.rtti_animaliterator_getnextanimal = methodType(int(methodAddress.value)) + + err = symbolLookupMethod(ctypes.c_char_p(str.encode("rtti_zoo_iterator")), methodAddress) + if err != 0: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, str(err)) + methodType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)) + self.lib.rtti_zoo_iterator = methodType(int(methodAddress.value)) + + except AttributeError as ae: + raise ERTTIException(ErrorCodes.COULDNOTFINDLIBRARYEXPORT, ae.args[0]) + + def _loadFunctionTable(self): + try: + self.lib.rtti_getversion.restype = ctypes.c_int32 + self.lib.rtti_getversion.argtypes = [ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32)] + + self.lib.rtti_getlasterror.restype = ctypes.c_int32 + self.lib.rtti_getlasterror.argtypes = [ctypes.c_void_p, ctypes.c_uint64, ctypes.POINTER(ctypes.c_uint64), ctypes.c_char_p, ctypes.POINTER(ctypes.c_bool)] + + self.lib.rtti_releaseinstance.restype = ctypes.c_int32 + self.lib.rtti_releaseinstance.argtypes = [ctypes.c_void_p] + + self.lib.rtti_acquireinstance.restype = ctypes.c_int32 + self.lib.rtti_acquireinstance.argtypes = [ctypes.c_void_p] + + self.lib.rtti_injectcomponent.restype = ctypes.c_int32 + self.lib.rtti_injectcomponent.argtypes = [ctypes.c_char_p, ctypes.c_void_p] + + self.lib.rtti_getsymbollookupmethod.restype = ctypes.c_int32 + self.lib.rtti_getsymbollookupmethod.argtypes = [ctypes.POINTER(ctypes.c_void_p)] + + self.lib.rtti_implementsinterface.restype = ctypes.c_int32 + self.lib.rtti_implementsinterface.argtypes = [ctypes.c_void_p, ctypes.c_uint64, ctypes.POINTER(ctypes.c_uint8), ctypes.POINTER(ctypes.c_bool)] + + self.lib.rtti_createzoo.restype = ctypes.c_int32 + self.lib.rtti_createzoo.argtypes = [ctypes.POINTER(ctypes.c_void_p)] + + self.lib.rtti_animal_name.restype = ctypes.c_int32 + self.lib.rtti_animal_name.argtypes = [ctypes.c_void_p, ctypes.c_uint64, ctypes.POINTER(ctypes.c_uint64), ctypes.c_char_p] + + self.lib.rtti_tiger_roar.restype = ctypes.c_int32 + self.lib.rtti_tiger_roar.argtypes = [ctypes.c_void_p] + + self.lib.rtti_animaliterator_getnextanimal.restype = ctypes.c_int32 + self.lib.rtti_animaliterator_getnextanimal.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)] + + self.lib.rtti_zoo_iterator.restype = ctypes.c_int32 + self.lib.rtti_zoo_iterator.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)] + + except AttributeError as ae: + raise ERTTIException(ErrorCodes.COULDNOTFINDLIBRARYEXPORT, ae.args[0]) + + def _checkBinaryVersion(self): + nMajor, nMinor, _ = self.GetVersion() + if (nMajor != BindingVersion.MAJOR) or (nMinor < BindingVersion.MINOR): + raise ERTTIException(ErrorCodes.INCOMPATIBLEBINARYVERSION) + + def checkError(self, instance, errorCode): + if errorCode != ErrorCodes.SUCCESS.value: + if instance: + if instance._wrapper != self: + raise ERTTIException(ErrorCodes.INVALIDCAST, 'invalid wrapper call') + message,_ = self.GetLastError(instance) + raise ERTTIException(errorCode, message) + + def GetVersion(self): + pMajor = ctypes.c_uint32() + pMinor = ctypes.c_uint32() + pMicro = ctypes.c_uint32() + self.checkError(None, self.lib.rtti_getversion(pMajor, pMinor, pMicro)) + + return pMajor.value, pMinor.value, pMicro.value + + def GetLastError(self, InstanceObject): + InstanceHandle = None + if InstanceObject: + InstanceHandle = InstanceObject._handle + else: + raise ERTTIException(ErrorCodes.INVALIDPARAM, 'Invalid return/output value') + nErrorMessageBufferSize = ctypes.c_uint64(0) + nErrorMessageNeededChars = ctypes.c_uint64(0) + pErrorMessageBuffer = ctypes.c_char_p(None) + pHasError = ctypes.c_bool() + self.checkError(None, self.lib.rtti_getlasterror(InstanceHandle, nErrorMessageBufferSize, nErrorMessageNeededChars, pErrorMessageBuffer, pHasError)) + nErrorMessageBufferSize = ctypes.c_uint64(nErrorMessageNeededChars.value) + pErrorMessageBuffer = (ctypes.c_char * (nErrorMessageNeededChars.value))() + self.checkError(None, self.lib.rtti_getlasterror(InstanceHandle, nErrorMessageBufferSize, nErrorMessageNeededChars, pErrorMessageBuffer, pHasError)) + + return pErrorMessageBuffer.value.decode(), pHasError.value + + def ReleaseInstance(self, InstanceObject): + InstanceHandle = None + if InstanceObject: + InstanceHandle = InstanceObject._handle + else: + raise ERTTIException(ErrorCodes.INVALIDPARAM, 'Invalid return/output value') + self.checkError(None, self.lib.rtti_releaseinstance(InstanceHandle)) + + + def AcquireInstance(self, InstanceObject): + InstanceHandle = None + if InstanceObject: + InstanceHandle = InstanceObject._handle + else: + raise ERTTIException(ErrorCodes.INVALIDPARAM, 'Invalid return/output value') + self.checkError(None, self.lib.rtti_acquireinstance(InstanceHandle)) + + + def InjectComponent(self, NameSpace, SymbolAddressMethod): + pNameSpace = ctypes.c_char_p(str.encode(NameSpace)) + pSymbolAddressMethod = ctypes.c_void_p(SymbolAddressMethod) + self.checkError(None, self.lib.rtti_injectcomponent(pNameSpace, pSymbolAddressMethod)) + + bNameSpaceFound = False + if not bNameSpaceFound: + raise ERTTIException(ErrorCodes.COULDNOTLOADLIBRARY, "Unknown namespace " + NameSpace) + + + def GetSymbolLookupMethod(self): + pSymbolLookupMethod = ctypes.c_void_p() + self.checkError(None, self.lib.rtti_getsymbollookupmethod(pSymbolLookupMethod)) + + return pSymbolLookupMethod.value + + def ImplementsInterface(self, ObjectObject, ClassHash): + ObjectHandle = None + if ObjectObject: + ObjectHandle = ObjectObject._handle + else: + raise ERTTIException(ErrorCodes.INVALIDPARAM, 'Invalid return/output value') + nClassHashCount = ctypes.c_uint64(len(ClassHash)) + pClassHashBuffer = (ctypes.c_uint8*len(ClassHash))(*ClassHash) + pImplementsInterface = ctypes.c_bool() + self.checkError(None, self.lib.rtti_implementsinterface(ObjectHandle, nClassHashCount, pClassHashBuffer, pImplementsInterface)) + + return pImplementsInterface.value + + def CreateZoo(self): + InstanceHandle = ctypes.c_void_p() + self.checkError(None, self.lib.rtti_createzoo(InstanceHandle)) + if InstanceHandle: + InstanceObject = Zoo(InstanceHandle, self) + else: + raise ERTTIException(ErrorCodes.INVALIDCAST, 'Invalid return/output value') + + return InstanceObject + + + +''' Class Implementation for Base +''' +class Base: + @staticmethod + def ClassName(): + return "Base" + + @staticmethod + def ClassHash(): + return bytearray(b'\x09\x5A\x1B\x43\xEF\xFE\xC7\x39\x55\xE3\x1E\x79\x04\x38\xDE\x49') + + @classmethod + def cast(cls, instance): + if instance and instance._wrapper.ImplementsInterface(instance, cls.ClassHash()): + instance._wrapper.AcquireInstance(instance) + return cls(instance._handle, instance._wrapper) + return None + + def __init__(self, handle, wrapper): + if not handle or not wrapper: + raise ERTTIException(ErrorCodes.INVALIDPARAM) + self._handle = handle + self._wrapper = wrapper + + def __del__(self): + self._wrapper.ReleaseInstance(self) + + +''' Class Implementation for Animal +''' +class Animal(Base): + @staticmethod + def ClassName(): + return "Animal" + + @staticmethod + def ClassHash(): + return bytearray(b'\x16\x1E\x7C\xE7\xBF\xDC\x89\xAB\x4B\x9F\x52\xC1\xD4\xC9\x42\x12') + + def __init__(self, handle, wrapper): + Base.__init__(self, handle, wrapper) + def Name(self): + nResultBufferSize = ctypes.c_uint64(0) + nResultNeededChars = ctypes.c_uint64(0) + pResultBuffer = ctypes.c_char_p(None) + self._wrapper.checkError(self, self._wrapper.lib.rtti_animal_name(self._handle, nResultBufferSize, nResultNeededChars, pResultBuffer)) + nResultBufferSize = ctypes.c_uint64(nResultNeededChars.value) + pResultBuffer = (ctypes.c_char * (nResultNeededChars.value))() + self._wrapper.checkError(self, self._wrapper.lib.rtti_animal_name(self._handle, nResultBufferSize, nResultNeededChars, pResultBuffer)) + + return pResultBuffer.value.decode() + + + +''' Class Implementation for Mammal +''' +class Mammal(Animal): + @staticmethod + def ClassName(): + return "Mammal" + + @staticmethod + def ClassHash(): + return bytearray(b'\x37\x42\x61\x13\xD1\x29\xE7\x9F\x54\x8F\x4C\x90\x93\x0F\xA6\x97') + + def __init__(self, handle, wrapper): + Animal.__init__(self, handle, wrapper) + + +''' Class Implementation for Reptile +''' +class Reptile(Animal): + @staticmethod + def ClassName(): + return "Reptile" + + @staticmethod + def ClassHash(): + return bytearray(b'\xAA\x64\x51\x86\xA4\xB5\xF3\xF2\x79\x52\xC2\xFA\x54\x85\xFA\xB2') + + def __init__(self, handle, wrapper): + Animal.__init__(self, handle, wrapper) + + +''' Class Implementation for Giraffe +''' +class Giraffe(Mammal): + @staticmethod + def ClassName(): + return "Giraffe" + + @staticmethod + def ClassHash(): + return bytearray(b'\x42\x7D\xEB\xB8\x1D\x26\x5A\x0E\xDD\x87\x89\xF3\x0B\x11\xBE\xB6') + + def __init__(self, handle, wrapper): + Mammal.__init__(self, handle, wrapper) + + +''' Class Implementation for Tiger +''' +class Tiger(Mammal): + @staticmethod + def ClassName(): + return "Tiger" + + @staticmethod + def ClassHash(): + return bytearray(b'\x45\x4C\x98\x43\x11\x06\x86\xBF\x6F\x67\xCE\x51\x15\xB6\x66\x17') + + def __init__(self, handle, wrapper): + Mammal.__init__(self, handle, wrapper) + def Roar(self): + self._wrapper.checkError(self, self._wrapper.lib.rtti_tiger_roar(self._handle)) + + + + +''' Class Implementation for Snake +''' +class Snake(Reptile): + @staticmethod + def ClassName(): + return "Snake" + + @staticmethod + def ClassHash(): + return bytearray(b'\xDF\xA9\x0F\x1B\x4E\xB3\xAF\xFB\xD3\xB4\x6A\xF3\x4E\xD2\x47\x7C') + + def __init__(self, handle, wrapper): + Reptile.__init__(self, handle, wrapper) + + +''' Class Implementation for Turtle +''' +class Turtle(Reptile): + @staticmethod + def ClassName(): + return "Turtle" + + @staticmethod + def ClassHash(): + return bytearray(b'\x06\xDE\xBA\x59\x08\xB0\x07\xEB\x6F\x32\xD8\xD9\x5F\x3F\x61\xB5') + + def __init__(self, handle, wrapper): + Reptile.__init__(self, handle, wrapper) + + +''' Class Implementation for AnimalIterator +''' +class AnimalIterator(Base): + @staticmethod + def ClassName(): + return "AnimalIterator" + + @staticmethod + def ClassHash(): + return bytearray(b'\xC2\xB3\x6A\x84\xC6\xC0\x32\x20\x4E\x5C\x92\x3C\x58\x10\x71\xE7') + + def __init__(self, handle, wrapper): + Base.__init__(self, handle, wrapper) + def GetNextAnimal(self): + AnimalHandle = ctypes.c_void_p() + self._wrapper.checkError(self, self._wrapper.lib.rtti_animaliterator_getnextanimal(self._handle, AnimalHandle)) + if AnimalHandle: + AnimalObject = Animal(AnimalHandle, self._wrapper) + else: + AnimalObject = None + + return AnimalObject + + + +''' Class Implementation for Zoo +''' +class Zoo(Base): + @staticmethod + def ClassName(): + return "Zoo" + + @staticmethod + def ClassHash(): + return bytearray(b'\xBF\xA8\x88\xA3\x54\xDB\x97\xC7\xCB\xEF\xB9\xD0\x50\xB9\x4C\xA3') + + def __init__(self, handle, wrapper): + Base.__init__(self, handle, wrapper) + def Iterator(self): + IteratorHandle = ctypes.c_void_p() + self._wrapper.checkError(self, self._wrapper.lib.rtti_zoo_iterator(self._handle, IteratorHandle)) + if IteratorHandle: + IteratorObject = AnimalIterator(IteratorHandle, self._wrapper) + else: + raise ERTTIException(ErrorCodes.INVALIDCAST, 'Invalid return/output value') + + return IteratorObject + + diff --git a/Examples/RTTI/RTTI_component/Examples/CppDynamic/CMakeLists.txt b/Examples/RTTI/RTTI_component/Examples/CppDynamic/CMakeLists.txt new file mode 100644 index 00000000..b739dd40 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Examples/CppDynamic/CMakeLists.txt @@ -0,0 +1,26 @@ +#[[++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated CMake Project that demonstrates the + usage of the Dynamic C++ bindings of RTTI + +Interface version: 1.0.0 + + +]] + +cmake_minimum_required(VERSION 3.5) + +set(CMAKE_CURRENT_BINDING_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../Bindings/CppDynamic) +project(RTTIExample_CPPDynamic) +set(CMAKE_CXX_STANDARD 11) +add_executable(RTTIExample_CPPDynamic "${CMAKE_CURRENT_SOURCE_DIR}/RTTI_example.cpp") +if (UNIX) + target_link_libraries(RTTIExample_CPPDynamic ${CMAKE_DL_LIBS}) +endif (UNIX) +target_include_directories(RTTIExample_CPPDynamic PRIVATE "${CMAKE_CURRENT_BINDING_DIR}") diff --git a/Examples/RTTI/RTTI_component/Examples/CppDynamic/RTTI_example.cpp b/Examples/RTTI/RTTI_component/Examples/CppDynamic/RTTI_example.cpp new file mode 100644 index 00000000..d5079a34 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Examples/CppDynamic/RTTI_example.cpp @@ -0,0 +1,50 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++ application that demonstrates the + usage of the Dynamic C++ bindings of RTTI + +Interface version: 1.0.0 + +*/ + +#include +#include "rtti_dynamic.hpp" + + +int main() +{ + try + { + std::string libpath = (""); // TODO: put the location of the RTTI-library file here. + auto wrapper = RTTI::CWrapper::loadLibrary(libpath + "/rtti."); // TODO: add correct suffix of the library + RTTI_uint32 nMajor, nMinor, nMicro; + wrapper->GetVersion(nMajor, nMinor, nMicro); + std::cout << "RTTI.Version = " << nMajor << "." << nMinor << "." << nMicro; + std::cout << std::endl; + + + auto zoo = wrapper->CreateZoo(); + auto iter = zoo->Iterator(); + + using namespace RTTI; + while (auto animal = iter->GetNextAnimal()) { + if (auto tiger = rtti_cast(animal)) { + tiger->Roar(); + } + std::cout << "Animal name: " << animal->Name() << std::endl; + } + } + catch (std::exception &e) + { + std::cout << e.what() << std::endl; + return 1; + } + return 0; +} + diff --git a/Examples/RTTI/RTTI_component/Examples/Python/RTTI_Example.py b/Examples/RTTI/RTTI_component/Examples/Python/RTTI_Example.py new file mode 100644 index 00000000..984534cf --- /dev/null +++ b/Examples/RTTI/RTTI_component/Examples/Python/RTTI_Example.py @@ -0,0 +1,43 @@ +'''++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated Python application that demonstrates the + usage of the Python bindings of RTTI + +Interface version: 1.0.0 + +''' + + +import os +import sys +sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "..", "Bindings", "Python")) +import RTTI + + +def main(): + libpath = '' # TODO add the location of the shared library binary here + wrapper = RTTI.Wrapper(libraryName = os.path.join(libpath, "rtti")) + + major, minor, micro = wrapper.GetVersion() + print("RTTI version: {:d}.{:d}.{:d}".format(major, minor, micro), end="") + print("") + + zoo = wrapper.CreateZoo() + iter = zoo.Iterator() + + while animal := iter.GetNextAnimal(): + if tiger := RTTI.Tiger.cast(animal): + tiger.Roar() + print("Animal name: " + animal.Name()) + +if __name__ == "__main__": + try: + main() + except RTTI.ERTTIException as e: + print(e) diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/CMakeLists.txt b/Examples/RTTI/RTTI_component/Implementations/Cpp/CMakeLists.txt new file mode 100644 index 00000000..d9db9617 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/CMakeLists.txt @@ -0,0 +1,46 @@ +#[[++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated CMakeLists file for the development of RTTI. + +Interface version: 1.0.0 + + +]] + +cmake_minimum_required(VERSION 3.5) + +### The implementation of the RTTI component +project(RTTI) + +set (CMAKE_CXX_STANDARD 11) + +# The location of autogenerated interfaces +set(CMAKE_CURRENT_AUTOGENERATED_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Interfaces) + +file(GLOB RTTI_SRC + ${CMAKE_CURRENT_SOURCE_DIR}/Stub/*.cpp +) +file(GLOB RTTI_HDR + ${CMAKE_CURRENT_SOURCE_DIR}/Stub/*.hpp +) +set(RTTI_SRC ${RTTI_SRC} ${RTTI_SRC} + ${CMAKE_CURRENT_AUTOGENERATED_DIR}/rtti_interfaceexception.cpp + ${CMAKE_CURRENT_AUTOGENERATED_DIR}/rtti_interfacewrapper.cpp +) + +add_library(rtti SHARED ${RTTI_SRC}) +# Do not prefix the binary's name with "lib" on Unix systems: +set_target_properties(rtti PROPERTIES PREFIX "" IMPORT_PREFIX "" ) +# The following two properties are crucial to reduce the number of undesirably exported symbols +set_target_properties(rtti PROPERTIES CXX_VISIBILITY_PRESET hidden) +set_target_properties(rtti PROPERTIES VISIBILITY_INLINES_HIDDEN ON) +# This makes sure symbols are exported +target_compile_options(rtti PRIVATE "-D__RTTI_EXPORTS") +target_include_directories(rtti PRIVATE ${CMAKE_CURRENT_AUTOGENERATED_DIR}) +target_include_directories(rtti PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Stub) diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_abi.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_abi.hpp new file mode 100644 index 00000000..ae254c13 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_abi.hpp @@ -0,0 +1,196 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++-Header file in order to allow an easy + use of RTTI + +Interface version: 1.0.0 + +*/ + +#ifndef __RTTI_HEADER_CPP +#define __RTTI_HEADER_CPP + +#ifdef __RTTI_EXPORTS +#ifdef _WIN32 +#define RTTI_DECLSPEC __declspec (dllexport) +#else // _WIN32 +#define RTTI_DECLSPEC __attribute__((visibility("default"))) +#endif // _WIN32 +#else // __RTTI_EXPORTS +#define RTTI_DECLSPEC +#endif // __RTTI_EXPORTS + +#include "rtti_types.hpp" + + +#ifdef __cplusplus +extern "C" { +#endif + +/************************************************************************************************************************* + Class definition for Base +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Animal +**************************************************************************************************************************/ + +/** +* Get the name of the animal +* +* @param[in] pAnimal - Animal instance. +* @param[in] nResultBufferSize - size of the buffer (including trailing 0) +* @param[out] pResultNeededChars - will be filled with the count of the written bytes, or needed buffer size. +* @param[out] pResultBuffer - buffer of , may be NULL +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_animal_name(RTTI_Animal pAnimal, const RTTI_uint32 nResultBufferSize, RTTI_uint32* pResultNeededChars, char * pResultBuffer); + +/************************************************************************************************************************* + Class definition for Mammal +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Reptile +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Giraffe +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Tiger +**************************************************************************************************************************/ + +/** +* Roar like a tiger +* +* @param[in] pTiger - Tiger instance. +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_tiger_roar(RTTI_Tiger pTiger); + +/************************************************************************************************************************* + Class definition for Snake +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for Turtle +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class definition for AnimalIterator +**************************************************************************************************************************/ + +/** +* Return next animal +* +* @param[in] pAnimalIterator - AnimalIterator instance. +* @param[out] pAnimal - +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_animaliterator_getnextanimal(RTTI_AnimalIterator pAnimalIterator, RTTI_Animal * pAnimal); + +/************************************************************************************************************************* + Class definition for Zoo +**************************************************************************************************************************/ + +/** +* Return an iterator over all zoo animals +* +* @param[in] pZoo - Zoo instance. +* @param[out] pIterator - +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_zoo_iterator(RTTI_Zoo pZoo, RTTI_AnimalIterator * pIterator); + +/************************************************************************************************************************* + Global functions +**************************************************************************************************************************/ + +/** +* retrieves the binary version of this library. +* +* @param[out] pMajor - returns the major version of this library +* @param[out] pMinor - returns the minor version of this library +* @param[out] pMicro - returns the micro version of this library +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_getversion(RTTI_uint32 * pMajor, RTTI_uint32 * pMinor, RTTI_uint32 * pMicro); + +/** +* Returns the last error recorded on this object +* +* @param[in] pInstance - Instance Handle +* @param[in] nErrorMessageBufferSize - size of the buffer (including trailing 0) +* @param[out] pErrorMessageNeededChars - will be filled with the count of the written bytes, or needed buffer size. +* @param[out] pErrorMessageBuffer - buffer of Message of the last error, may be NULL +* @param[out] pHasError - Is there a last error to query +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_getlasterror(RTTI_Base pInstance, const RTTI_uint32 nErrorMessageBufferSize, RTTI_uint32* pErrorMessageNeededChars, char * pErrorMessageBuffer, bool * pHasError); + +/** +* Releases shared ownership of an Instance +* +* @param[in] pInstance - Instance Handle +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_releaseinstance(RTTI_Base pInstance); + +/** +* Acquires shared ownership of an Instance +* +* @param[in] pInstance - Instance Handle +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_acquireinstance(RTTI_Base pInstance); + +/** +* Injects an imported component for usage within this component +* +* @param[in] pNameSpace - NameSpace of the injected component +* @param[in] pSymbolAddressMethod - Address of the SymbolAddressMethod of the injected component +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_injectcomponent(const char * pNameSpace, RTTI_pvoid pSymbolAddressMethod); + +/** +* Returns the address of the SymbolLookupMethod +* +* @param[out] pSymbolLookupMethod - Address of the SymbolAddressMethod +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_getsymbollookupmethod(RTTI_pvoid * pSymbolLookupMethod); + +/** +* Test whether an object implements a given interface +* +* @param[in] pObject - Instance Handle +* @param[in] nClassHashBufferSize - Number of elements in buffer +* @param[in] pClassHashBuffer - uint8 buffer of Hashed class name of the interface to test +* @param[out] pImplementsInterface - Will be set to true if pInstance implements the interface, false otherwise +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_implementsinterface(RTTI_Base pObject, RTTI_uint64 nClassHashBufferSize, const RTTI_uint8 * pClassHashBuffer, bool * pImplementsInterface); + +/** +* Create a new zoo with animals +* +* @param[out] pInstance - +* @return error code or 0 (success) +*/ +RTTI_DECLSPEC RTTIResult rtti_createzoo(RTTI_Zoo * pInstance); + +#ifdef __cplusplus +} +#endif + +#endif // __RTTI_HEADER_CPP + diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfaceexception.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfaceexception.cpp new file mode 100644 index 00000000..4db45b80 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfaceexception.cpp @@ -0,0 +1,45 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++ Implementation file with the basic internal + exception type in order to allow an easy use of RTTI + +Interface version: 1.0.0 + +*/ + + +#include + +#include "rtti_interfaceexception.hpp" + +/************************************************************************************************************************* + Class ERTTIInterfaceException +**************************************************************************************************************************/ +ERTTIInterfaceException::ERTTIInterfaceException(RTTIResult errorCode) + : m_errorMessage(RTTI_GETERRORSTRING (errorCode)) +{ + m_errorCode = errorCode; +} + +ERTTIInterfaceException::ERTTIInterfaceException(RTTIResult errorCode, std::string errorMessage) + : m_errorMessage(errorMessage + " (" + std::to_string (errorCode) + ")") +{ + m_errorCode = errorCode; +} + +RTTIResult ERTTIInterfaceException::getErrorCode () +{ + return m_errorCode; +} + +const char * ERTTIInterfaceException::what () const noexcept +{ + return m_errorMessage.c_str(); +} + diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfaceexception.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfaceexception.hpp new file mode 100644 index 00000000..a6f4795c --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfaceexception.hpp @@ -0,0 +1,60 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++ Header file with the basic internal + exception type in order to allow an easy use of RTTI + +Interface version: 1.0.0 + +*/ + +#ifndef __RTTI_INTERFACEEXCEPTION_HEADER +#define __RTTI_INTERFACEEXCEPTION_HEADER + +#include +#include +#include "rtti_types.hpp" + +/************************************************************************************************************************* + Class ERTTIInterfaceException +**************************************************************************************************************************/ + + +class ERTTIInterfaceException : public std::exception { +protected: + /** + * Error code for the Exception. + */ + RTTIResult m_errorCode; + /** + * Error message for the Exception. + */ + std::string m_errorMessage; + +public: + /** + * Exception Constructor. + */ + ERTTIInterfaceException(RTTIResult errorCode); + + /** + * Custom Exception Constructor. + */ + ERTTIInterfaceException(RTTIResult errorCode, std::string errorMessage); + + /** + * Returns error code + */ + RTTIResult getErrorCode(); + /** + * Returns error message + */ + const char* what() const noexcept override; +}; + +#endif // __RTTI_INTERFACEEXCEPTION_HEADER diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfaces.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfaces.hpp new file mode 100644 index 00000000..97b074aa --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfaces.hpp @@ -0,0 +1,392 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++ header file in order to allow easy +development of RTTI. The implementer of RTTI needs to +derive concrete classes from the abstract classes in this header. + +Interface version: 1.0.0 + +*/ + + +#ifndef __RTTI_CPPINTERFACES +#define __RTTI_CPPINTERFACES + +#include +#include + +#include "rtti_types.hpp" + + + +namespace RTTI { +namespace Impl { + +/** + Forward declarations of class interfaces +*/ +class IBase; +class IAnimal; +class IMammal; +class IReptile; +class IGiraffe; +class ITiger; +class ISnake; +class ITurtle; +class IAnimalIterator; +class IZoo; + + + +/************************************************************************************************************************* + Parameter Cache definitions +**************************************************************************************************************************/ + +class ParameterCache { + public: + virtual ~ParameterCache() {} +}; + +template class ParameterCache_1 : public ParameterCache { + private: + T1 m_param1; + public: + ParameterCache_1 (const T1 & param1) + : m_param1 (param1) + { + } + + void retrieveData (T1 & param1) + { + param1 = m_param1; + } +}; + +template class ParameterCache_2 : public ParameterCache { + private: + T1 m_param1; + T2 m_param2; + public: + ParameterCache_2 (const T1 & param1, const T2 & param2) + : m_param1 (param1), m_param2 (param2) + { + } + + void retrieveData (T1 & param1, T2 & param2) + { + param1 = m_param1; + param2 = m_param2; + } +}; + +template class ParameterCache_3 : public ParameterCache { + private: + T1 m_param1; + T2 m_param2; + T3 m_param3; + public: + ParameterCache_3 (const T1 & param1, const T2 & param2, const T3 & param3) + : m_param1 (param1), m_param2 (param2), m_param3 (param3) + { + } + + void retrieveData (T1 & param1, T2 & param2, T3 & param3) + { + param1 = m_param1; + param2 = m_param2; + param3 = m_param3; + } +}; + + +/************************************************************************************************************************* + Class interface for Base +**************************************************************************************************************************/ + +class IBase { +private: + std::unique_ptr m_ParameterCache; +public: + /** + * IBase::~IBase - virtual destructor of IBase + */ + virtual ~IBase() {}; + + /** + * IBase::ReleaseBaseClassInterface - Releases ownership of a base class interface. Deletes the reference, if necessary. + * @param[in] pIBase - The base class instance to release + */ + static void ReleaseBaseClassInterface(IBase* pIBase) + { + if (pIBase) { + pIBase->DecRefCount(); + } + }; + + /** + * IBase::AcquireBaseClassInterface - Acquires shared ownership of a base class interface. + * @param[in] pIBase - The base class instance to acquire + */ + static void AcquireBaseClassInterface(IBase* pIBase) + { + if (pIBase) { + pIBase->IncRefCount(); + } + }; + + + /** + * IBase::GetLastErrorMessage - Returns the last error registered of this class instance + * @param[out] sErrorMessage - Message of the last error registered + * @return Has an error been registered already + */ + virtual bool GetLastErrorMessage(std::string & sErrorMessage) = 0; + + /** + * IBase::ClearErrorMessages - Clears all registered messages of this class instance + */ + virtual void ClearErrorMessages() = 0; + + /** + * IBase::RegisterErrorMessage - Registers an error message with this class instance + * @param[in] sErrorMessage - Error message to register + */ + virtual void RegisterErrorMessage(const std::string & sErrorMessage) = 0; + + /** + * IBase::IncRefCount - Increases the reference count of a class instance + */ + virtual void IncRefCount() = 0; + + /** + * IBase::DecRefCount - Decreases the reference count of a class instance and free releases it, if the last reference has been removed + * @return Has the object been released + */ + virtual bool DecRefCount() = 0; + + /** + * IBase::_setCache - set parameter cache of object + */ + void _setCache(ParameterCache * pCache) + { + m_ParameterCache.reset(pCache); + } + + /** + * IBase::_getCache - returns parameter cache of object + */ + ParameterCache* _getCache() + { + return m_ParameterCache.get(); + } + +}; + + +/** + Definition of a shared pointer class for IBase +*/ +template +class IBaseSharedPtr : public std::shared_ptr +{ +public: + explicit IBaseSharedPtr(T* t = nullptr) + : std::shared_ptr(t, IBase::ReleaseBaseClassInterface) + { + t->IncRefCount(); + } + + // Reset function, as it also needs to properly set the deleter. + void reset(T* t = nullptr) + { + std::shared_ptr::reset(t, IBase::ReleaseBaseClassInterface); + } + + // Get-function that increases the Base class's reference count + T* getCoOwningPtr() + { + T* t = this->get(); + t->IncRefCount(); + return t; + } +}; + + +typedef IBaseSharedPtr PIBase; + + +/************************************************************************************************************************* + Class interface for Animal +**************************************************************************************************************************/ + +class IAnimal : public virtual IBase { +public: + /** + * IAnimal::Name - Get the name of the animal + * @return + */ + virtual std::string Name() = 0; + +}; + +typedef IBaseSharedPtr PIAnimal; + + +/************************************************************************************************************************* + Class interface for Mammal +**************************************************************************************************************************/ + +class IMammal : public virtual IAnimal { +public: +}; + +typedef IBaseSharedPtr PIMammal; + + +/************************************************************************************************************************* + Class interface for Reptile +**************************************************************************************************************************/ + +class IReptile : public virtual IAnimal { +public: +}; + +typedef IBaseSharedPtr PIReptile; + + +/************************************************************************************************************************* + Class interface for Giraffe +**************************************************************************************************************************/ + +class IGiraffe : public virtual IMammal { +public: +}; + +typedef IBaseSharedPtr PIGiraffe; + + +/************************************************************************************************************************* + Class interface for Tiger +**************************************************************************************************************************/ + +class ITiger : public virtual IMammal { +public: + /** + * ITiger::Roar - Roar like a tiger + */ + virtual void Roar() = 0; + +}; + +typedef IBaseSharedPtr PITiger; + + +/************************************************************************************************************************* + Class interface for Snake +**************************************************************************************************************************/ + +class ISnake : public virtual IReptile { +public: +}; + +typedef IBaseSharedPtr PISnake; + + +/************************************************************************************************************************* + Class interface for Turtle +**************************************************************************************************************************/ + +class ITurtle : public virtual IReptile { +public: +}; + +typedef IBaseSharedPtr PITurtle; + + +/************************************************************************************************************************* + Class interface for AnimalIterator +**************************************************************************************************************************/ + +class IAnimalIterator : public virtual IBase { +public: + /** + * IAnimalIterator::GetNextAnimal - Return next animal + * @return + */ + virtual IAnimal * GetNextAnimal() = 0; + +}; + +typedef IBaseSharedPtr PIAnimalIterator; + + +/************************************************************************************************************************* + Class interface for Zoo +**************************************************************************************************************************/ + +class IZoo : public virtual IBase { +public: + /** + * IZoo::Iterator - Return an iterator over all zoo animals + * @return + */ + virtual IAnimalIterator * Iterator() = 0; + +}; + +typedef IBaseSharedPtr PIZoo; + + +/************************************************************************************************************************* + Global functions declarations +**************************************************************************************************************************/ +class CWrapper { +public: + /** + * Irtti::GetVersion - retrieves the binary version of this library. + * @param[out] nMajor - returns the major version of this library + * @param[out] nMinor - returns the minor version of this library + * @param[out] nMicro - returns the micro version of this library + */ + static void GetVersion(RTTI_uint32 & nMajor, RTTI_uint32 & nMinor, RTTI_uint32 & nMicro); + + /** + * Irtti::GetLastError - Returns the last error recorded on this object + * @param[in] pInstance - Instance Handle + * @param[out] sErrorMessage - Message of the last error + * @return Is there a last error to query + */ + static bool GetLastError(IBase* pInstance, std::string & sErrorMessage); + + /** + * Irtti::ReleaseInstance - Releases shared ownership of an Instance + * @param[in] pInstance - Instance Handle + */ + static void ReleaseInstance(IBase* pInstance); + + /** + * Irtti::AcquireInstance - Acquires shared ownership of an Instance + * @param[in] pInstance - Instance Handle + */ + static void AcquireInstance(IBase* pInstance); + + /** + * Irtti::CreateZoo - Create a new zoo with animals + * @return + */ + static IZoo * CreateZoo(); + +}; + +RTTIResult RTTI_GetProcAddress (const char * pProcName, void ** ppProcAddress); + +} // namespace Impl +} // namespace RTTI + +#endif // __RTTI_CPPINTERFACES diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfacewrapper.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfacewrapper.cpp new file mode 100644 index 00000000..341e46ee --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_interfacewrapper.cpp @@ -0,0 +1,546 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++ implementation file in order to allow easy +development of RTTI. The functions in this file need to be implemented. It needs to be generated only once. + +Interface version: 1.0.0 + +*/ + +#include "rtti_abi.hpp" +#include "rtti_interfaces.hpp" +#include "rtti_interfaceexception.hpp" + +#include + +using namespace RTTI::Impl; + +RTTIResult handleRTTIException(IBase * pIBaseClass, ERTTIInterfaceException & Exception) +{ + RTTIResult errorCode = Exception.getErrorCode(); + + if (pIBaseClass != nullptr) + pIBaseClass->RegisterErrorMessage(Exception.what()); + + return errorCode; +} + +RTTIResult handleStdException(IBase * pIBaseClass, std::exception & Exception) +{ + RTTIResult errorCode = RTTI_ERROR_GENERICEXCEPTION; + + if (pIBaseClass != nullptr) + pIBaseClass->RegisterErrorMessage(Exception.what()); + + return errorCode; +} + +RTTIResult handleUnhandledException(IBase * pIBaseClass) +{ + RTTIResult errorCode = RTTI_ERROR_GENERICEXCEPTION; + + if (pIBaseClass != nullptr) + pIBaseClass->RegisterErrorMessage("Unhandled Exception"); + + return errorCode; +} + + + +/************************************************************************************************************************* + Class implementation for Base +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class implementation for Animal +**************************************************************************************************************************/ +RTTIResult rtti_animal_name(RTTI_Animal pAnimal, const RTTI_uint32 nResultBufferSize, RTTI_uint32* pResultNeededChars, char * pResultBuffer) +{ + IBase* pIBaseClass = (IBase *)pAnimal; + + try { + if ( (!pResultBuffer) && !(pResultNeededChars) ) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + std::string sResult(""); + IAnimal* pIAnimal = dynamic_cast(pIBaseClass); + if (!pIAnimal) + throw ERTTIInterfaceException(RTTI_ERROR_INVALIDCAST); + + bool isCacheCall = (pResultBuffer == nullptr); + if (isCacheCall) { + sResult = pIAnimal->Name(); + + pIAnimal->_setCache (new ParameterCache_1 (sResult)); + } + else { + auto cache = dynamic_cast*> (pIAnimal->_getCache ()); + if (cache == nullptr) + throw ERTTIInterfaceException(RTTI_ERROR_INVALIDCAST); + cache->retrieveData (sResult); + pIAnimal->_setCache (nullptr); + } + + if (pResultNeededChars) + *pResultNeededChars = (RTTI_uint32) (sResult.size()+1); + if (pResultBuffer) { + if (sResult.size() >= nResultBufferSize) + throw ERTTIInterfaceException (RTTI_ERROR_BUFFERTOOSMALL); + for (size_t iResult = 0; iResult < sResult.size(); iResult++) + pResultBuffer[iResult] = sResult[iResult]; + pResultBuffer[sResult.size()] = 0; + } + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + + +/************************************************************************************************************************* + Class implementation for Mammal +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class implementation for Reptile +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class implementation for Giraffe +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class implementation for Tiger +**************************************************************************************************************************/ +RTTIResult rtti_tiger_roar(RTTI_Tiger pTiger) +{ + IBase* pIBaseClass = (IBase *)pTiger; + + try { + ITiger* pITiger = dynamic_cast(pIBaseClass); + if (!pITiger) + throw ERTTIInterfaceException(RTTI_ERROR_INVALIDCAST); + + pITiger->Roar(); + + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + + +/************************************************************************************************************************* + Class implementation for Snake +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class implementation for Turtle +**************************************************************************************************************************/ + +/************************************************************************************************************************* + Class implementation for AnimalIterator +**************************************************************************************************************************/ +RTTIResult rtti_animaliterator_getnextanimal(RTTI_AnimalIterator pAnimalIterator, RTTI_Animal * pAnimal) +{ + IBase* pIBaseClass = (IBase *)pAnimalIterator; + + try { + if (pAnimal == nullptr) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + IBase* pBaseAnimal(nullptr); + IAnimalIterator* pIAnimalIterator = dynamic_cast(pIBaseClass); + if (!pIAnimalIterator) + throw ERTTIInterfaceException(RTTI_ERROR_INVALIDCAST); + + pBaseAnimal = pIAnimalIterator->GetNextAnimal(); + + *pAnimal = (IBase*)(pBaseAnimal); + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + + +/************************************************************************************************************************* + Class implementation for Zoo +**************************************************************************************************************************/ +RTTIResult rtti_zoo_iterator(RTTI_Zoo pZoo, RTTI_AnimalIterator * pIterator) +{ + IBase* pIBaseClass = (IBase *)pZoo; + + try { + if (pIterator == nullptr) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + IBase* pBaseIterator(nullptr); + IZoo* pIZoo = dynamic_cast(pIBaseClass); + if (!pIZoo) + throw ERTTIInterfaceException(RTTI_ERROR_INVALIDCAST); + + pBaseIterator = pIZoo->Iterator(); + + *pIterator = (IBase*)(pBaseIterator); + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + + + +/************************************************************************************************************************* + Function table lookup implementation +**************************************************************************************************************************/ + +RTTIResult RTTI::Impl::RTTI_GetProcAddress (const char * pProcName, void ** ppProcAddress) +{ + if (pProcName == nullptr) + return RTTI_ERROR_INVALIDPARAM; + if (ppProcAddress == nullptr) + return RTTI_ERROR_INVALIDPARAM; + *ppProcAddress = nullptr; + std::string sProcName (pProcName); + + if (sProcName == "rtti_animal_name") + *ppProcAddress = (void*) &rtti_animal_name; + if (sProcName == "rtti_tiger_roar") + *ppProcAddress = (void*) &rtti_tiger_roar; + if (sProcName == "rtti_animaliterator_getnextanimal") + *ppProcAddress = (void*) &rtti_animaliterator_getnextanimal; + if (sProcName == "rtti_zoo_iterator") + *ppProcAddress = (void*) &rtti_zoo_iterator; + if (sProcName == "rtti_getversion") + *ppProcAddress = (void*) &rtti_getversion; + if (sProcName == "rtti_getlasterror") + *ppProcAddress = (void*) &rtti_getlasterror; + if (sProcName == "rtti_releaseinstance") + *ppProcAddress = (void*) &rtti_releaseinstance; + if (sProcName == "rtti_acquireinstance") + *ppProcAddress = (void*) &rtti_acquireinstance; + if (sProcName == "rtti_injectcomponent") + *ppProcAddress = (void*) &rtti_injectcomponent; + if (sProcName == "rtti_getsymbollookupmethod") + *ppProcAddress = (void*) &rtti_getsymbollookupmethod; + if (sProcName == "rtti_implementsinterface") + *ppProcAddress = (void*) &rtti_implementsinterface; + if (sProcName == "rtti_createzoo") + *ppProcAddress = (void*) &rtti_createzoo; + + if (*ppProcAddress == nullptr) + return RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT; + return RTTI_SUCCESS; +} + +/************************************************************************************************************************* + Global functions implementation +**************************************************************************************************************************/ +RTTIResult rtti_getversion(RTTI_uint32 * pMajor, RTTI_uint32 * pMinor, RTTI_uint32 * pMicro) +{ + IBase* pIBaseClass = nullptr; + + try { + if (!pMajor) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + if (!pMinor) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + if (!pMicro) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + CWrapper::GetVersion(*pMajor, *pMinor, *pMicro); + + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + +RTTIResult rtti_getlasterror(RTTI_Base pInstance, const RTTI_uint32 nErrorMessageBufferSize, RTTI_uint32* pErrorMessageNeededChars, char * pErrorMessageBuffer, bool * pHasError) +{ + IBase* pIBaseClass = nullptr; + + try { + if ( (!pErrorMessageBuffer) && !(pErrorMessageNeededChars) ) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + if (pHasError == nullptr) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + IBase* pIBaseClassInstance = (IBase *)pInstance; + IBase* pIInstance = dynamic_cast(pIBaseClassInstance); + if (!pIInstance) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDCAST); + + std::string sErrorMessage(""); + *pHasError = CWrapper::GetLastError(pIInstance, sErrorMessage); + + if (pErrorMessageNeededChars) + *pErrorMessageNeededChars = (RTTI_uint32) (sErrorMessage.size()+1); + if (pErrorMessageBuffer) { + if (sErrorMessage.size() >= nErrorMessageBufferSize) + throw ERTTIInterfaceException (RTTI_ERROR_BUFFERTOOSMALL); + for (size_t iErrorMessage = 0; iErrorMessage < sErrorMessage.size(); iErrorMessage++) + pErrorMessageBuffer[iErrorMessage] = sErrorMessage[iErrorMessage]; + pErrorMessageBuffer[sErrorMessage.size()] = 0; + } + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + +RTTIResult rtti_releaseinstance(RTTI_Base pInstance) +{ + IBase* pIBaseClass = nullptr; + + try { + IBase* pIBaseClassInstance = (IBase *)pInstance; + IBase* pIInstance = dynamic_cast(pIBaseClassInstance); + if (!pIInstance) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDCAST); + + CWrapper::ReleaseInstance(pIInstance); + + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + +RTTIResult rtti_acquireinstance(RTTI_Base pInstance) +{ + IBase* pIBaseClass = nullptr; + + try { + IBase* pIBaseClassInstance = (IBase *)pInstance; + IBase* pIInstance = dynamic_cast(pIBaseClassInstance); + if (!pIInstance) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDCAST); + + CWrapper::AcquireInstance(pIInstance); + + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + +RTTIResult rtti_injectcomponent(const char * pNameSpace, RTTI_pvoid pSymbolAddressMethod) +{ + IBase* pIBaseClass = nullptr; + + try { + if (pNameSpace == nullptr) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + std::string sNameSpace(pNameSpace); + + bool bNameSpaceFound = false; + + + if (!bNameSpaceFound) + throw ERTTIInterfaceException(RTTI_ERROR_COULDNOTLOADLIBRARY); + + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + +RTTIResult rtti_getsymbollookupmethod(RTTI_pvoid * pSymbolLookupMethod) +{ + IBase* pIBaseClass = nullptr; + + try { + if (pSymbolLookupMethod == nullptr) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + *pSymbolLookupMethod = (void*)&RTTI::Impl::RTTI_GetProcAddress; + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + + +/************************************************************************************************************************* + Test whether an object implements a given interface +**************************************************************************************************************************/ + +RTTIResult rtti_implementsinterface(RTTI_Base pObject, RTTI_uint64 nClassHashBufferSize, const RTTI_uint8 * pClassHashBuffer, bool * pImplementsInterface) +{ + if (nClassHashBufferSize != 16) // Hash length must be as expected + return RTTI_ERROR_INVALIDPARAM; + + IBase* pIBaseClassInstance = (IBase *)pObject; + + switch(pClassHashBuffer[0]) { + case 0x06: + static const RTTI_uint8 s_TurtleHash[] = { 0x06, 0xDE, 0xBA, 0x59, 0x08, 0xB0, 0x07, 0xEB, 0x6F, 0x32, 0xD8, 0xD9, 0x5F, 0x3F, 0x61, 0xB5, }; + if (memcmp(pClassHashBuffer, s_TurtleHash, 16) == 0) { + *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr; + return RTTI_SUCCESS; + } + break; + case 0x09: + static const RTTI_uint8 s_BaseHash[] = { 0x09, 0x5A, 0x1B, 0x43, 0xEF, 0xFE, 0xC7, 0x39, 0x55, 0xE3, 0x1E, 0x79, 0x04, 0x38, 0xDE, 0x49, }; + if (memcmp(pClassHashBuffer, s_BaseHash, 16) == 0) { + *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr; + return RTTI_SUCCESS; + } + break; + case 0x16: + static const RTTI_uint8 s_AnimalHash[] = { 0x16, 0x1E, 0x7C, 0xE7, 0xBF, 0xDC, 0x89, 0xAB, 0x4B, 0x9F, 0x52, 0xC1, 0xD4, 0xC9, 0x42, 0x12, }; + if (memcmp(pClassHashBuffer, s_AnimalHash, 16) == 0) { + *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr; + return RTTI_SUCCESS; + } + break; + case 0x37: + static const RTTI_uint8 s_MammalHash[] = { 0x37, 0x42, 0x61, 0x13, 0xD1, 0x29, 0xE7, 0x9F, 0x54, 0x8F, 0x4C, 0x90, 0x93, 0x0F, 0xA6, 0x97, }; + if (memcmp(pClassHashBuffer, s_MammalHash, 16) == 0) { + *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr; + return RTTI_SUCCESS; + } + break; + case 0x42: + static const RTTI_uint8 s_GiraffeHash[] = { 0x42, 0x7D, 0xEB, 0xB8, 0x1D, 0x26, 0x5A, 0x0E, 0xDD, 0x87, 0x89, 0xF3, 0x0B, 0x11, 0xBE, 0xB6, }; + if (memcmp(pClassHashBuffer, s_GiraffeHash, 16) == 0) { + *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr; + return RTTI_SUCCESS; + } + break; + case 0x45: + static const RTTI_uint8 s_TigerHash[] = { 0x45, 0x4C, 0x98, 0x43, 0x11, 0x06, 0x86, 0xBF, 0x6F, 0x67, 0xCE, 0x51, 0x15, 0xB6, 0x66, 0x17, }; + if (memcmp(pClassHashBuffer, s_TigerHash, 16) == 0) { + *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr; + return RTTI_SUCCESS; + } + break; + case 0xAA: + static const RTTI_uint8 s_ReptileHash[] = { 0xAA, 0x64, 0x51, 0x86, 0xA4, 0xB5, 0xF3, 0xF2, 0x79, 0x52, 0xC2, 0xFA, 0x54, 0x85, 0xFA, 0xB2, }; + if (memcmp(pClassHashBuffer, s_ReptileHash, 16) == 0) { + *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr; + return RTTI_SUCCESS; + } + break; + case 0xBF: + static const RTTI_uint8 s_ZooHash[] = { 0xBF, 0xA8, 0x88, 0xA3, 0x54, 0xDB, 0x97, 0xC7, 0xCB, 0xEF, 0xB9, 0xD0, 0x50, 0xB9, 0x4C, 0xA3, }; + if (memcmp(pClassHashBuffer, s_ZooHash, 16) == 0) { + *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr; + return RTTI_SUCCESS; + } + break; + case 0xC2: + static const RTTI_uint8 s_AnimalIteratorHash[] = { 0xC2, 0xB3, 0x6A, 0x84, 0xC6, 0xC0, 0x32, 0x20, 0x4E, 0x5C, 0x92, 0x3C, 0x58, 0x10, 0x71, 0xE7, }; + if (memcmp(pClassHashBuffer, s_AnimalIteratorHash, 16) == 0) { + *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr; + return RTTI_SUCCESS; + } + break; + case 0xDF: + static const RTTI_uint8 s_SnakeHash[] = { 0xDF, 0xA9, 0x0F, 0x1B, 0x4E, 0xB3, 0xAF, 0xFB, 0xD3, 0xB4, 0x6A, 0xF3, 0x4E, 0xD2, 0x47, 0x7C, }; + if (memcmp(pClassHashBuffer, s_SnakeHash, 16) == 0) { + *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr; + return RTTI_SUCCESS; + } + break; + } + return RTTI_ERROR_INVALIDPARAM; +} + +RTTIResult rtti_createzoo(RTTI_Zoo * pInstance) +{ + IBase* pIBaseClass = nullptr; + + try { + if (pInstance == nullptr) + throw ERTTIInterfaceException (RTTI_ERROR_INVALIDPARAM); + IBase* pBaseInstance(nullptr); + pBaseInstance = CWrapper::CreateZoo(); + + *pInstance = (IBase*)(pBaseInstance); + return RTTI_SUCCESS; + } + catch (ERTTIInterfaceException & Exception) { + return handleRTTIException(pIBaseClass, Exception); + } + catch (std::exception & StdException) { + return handleStdException(pIBaseClass, StdException); + } + catch (...) { + return handleUnhandledException(pIBaseClass); + } +} + + diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_types.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_types.hpp new file mode 100644 index 00000000..19fec9a2 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Interfaces/rtti_types.hpp @@ -0,0 +1,125 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++-Header file with basic types in +order to allow an easy use of RTTI + +Interface version: 1.0.0 + +*/ + +#ifndef __RTTI_TYPES_HEADER_CPP +#define __RTTI_TYPES_HEADER_CPP + + +/************************************************************************************************************************* + Scalar types definition +**************************************************************************************************************************/ + +#ifdef RTTI_USELEGACYINTEGERTYPES + +typedef unsigned char RTTI_uint8; +typedef unsigned short RTTI_uint16 ; +typedef unsigned int RTTI_uint32; +typedef unsigned long long RTTI_uint64; +typedef char RTTI_int8; +typedef short RTTI_int16; +typedef int RTTI_int32; +typedef long long RTTI_int64; + +#else // RTTI_USELEGACYINTEGERTYPES + +#include + +typedef uint8_t RTTI_uint8; +typedef uint16_t RTTI_uint16; +typedef uint32_t RTTI_uint32; +typedef uint64_t RTTI_uint64; +typedef int8_t RTTI_int8; +typedef int16_t RTTI_int16; +typedef int32_t RTTI_int32; +typedef int64_t RTTI_int64 ; + +#endif // RTTI_USELEGACYINTEGERTYPES + +typedef float RTTI_single; +typedef double RTTI_double; + +/************************************************************************************************************************* + General type definitions +**************************************************************************************************************************/ + +typedef RTTI_int32 RTTIResult; +typedef void * RTTIHandle; +typedef void * RTTI_pvoid; + +/************************************************************************************************************************* + Version for RTTI +**************************************************************************************************************************/ + +#define RTTI_VERSION_MAJOR 1 +#define RTTI_VERSION_MINOR 0 +#define RTTI_VERSION_MICRO 0 +#define RTTI_VERSION_PRERELEASEINFO "" +#define RTTI_VERSION_BUILDINFO "" + +/************************************************************************************************************************* + Error constants for RTTI +**************************************************************************************************************************/ + +#define RTTI_SUCCESS 0 +#define RTTI_ERROR_NOTIMPLEMENTED 1 +#define RTTI_ERROR_INVALIDPARAM 2 +#define RTTI_ERROR_INVALIDCAST 3 +#define RTTI_ERROR_BUFFERTOOSMALL 4 +#define RTTI_ERROR_GENERICEXCEPTION 5 +#define RTTI_ERROR_COULDNOTLOADLIBRARY 6 +#define RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT 7 +#define RTTI_ERROR_INCOMPATIBLEBINARYVERSION 8 + +/************************************************************************************************************************* + Error strings for RTTI +**************************************************************************************************************************/ + +inline const char * RTTI_GETERRORSTRING (RTTIResult nErrorCode) { + switch (nErrorCode) { + case RTTI_SUCCESS: return "no error"; + case RTTI_ERROR_NOTIMPLEMENTED: return "functionality not implemented"; + case RTTI_ERROR_INVALIDPARAM: return "an invalid parameter was passed"; + case RTTI_ERROR_INVALIDCAST: return "a type cast failed"; + case RTTI_ERROR_BUFFERTOOSMALL: return "a provided buffer is too small"; + case RTTI_ERROR_GENERICEXCEPTION: return "a generic exception occurred"; + case RTTI_ERROR_COULDNOTLOADLIBRARY: return "the library could not be loaded"; + case RTTI_ERROR_COULDNOTFINDLIBRARYEXPORT: return "a required exported symbol could not be found in the library"; + case RTTI_ERROR_INCOMPATIBLEBINARYVERSION: return "the version of the binary interface does not match the bindings interface"; + default: return "unknown error"; + } +} + +/************************************************************************************************************************* + Declaration of handle classes +**************************************************************************************************************************/ + +typedef RTTIHandle RTTI_Base; +typedef RTTIHandle RTTI_Animal; +typedef RTTIHandle RTTI_Mammal; +typedef RTTIHandle RTTI_Reptile; +typedef RTTIHandle RTTI_Giraffe; +typedef RTTIHandle RTTI_Tiger; +typedef RTTIHandle RTTI_Snake; +typedef RTTIHandle RTTI_Turtle; +typedef RTTIHandle RTTI_AnimalIterator; +typedef RTTIHandle RTTI_Zoo; + +namespace RTTI { + +} // namespace RTTI; + +// define legacy C-names for enums, structs and function types + +#endif // __RTTI_TYPES_HEADER_CPP diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti.cpp new file mode 100644 index 00000000..133b8aee --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti.cpp @@ -0,0 +1,75 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +This file has been generated by the Automatic Component Toolkit (ACT) version 1.7.0-develop. + +Abstract: This is an autogenerated C++ implementation file in order to allow easy +development of RTTI. It needs to be generated only once. + +Interface version: 1.0.0 + +*/ + +#include "rtti_abi.hpp" +#include "rtti_interfaces.hpp" +#include "rtti_interfaceexception.hpp" + +#include "rtti_giraffe.hpp" +#include "rtti_snake.hpp" +#include "rtti_tiger.hpp" +#include "rtti_turtle.hpp" +#include "rtti_zoo.hpp" + +using namespace RTTI; +using namespace RTTI::Impl; + +void CWrapper::GetVersion(RTTI_uint32 & nMajor, RTTI_uint32 & nMinor, RTTI_uint32 & nMicro) +{ + nMajor = RTTI_VERSION_MAJOR; + nMinor = RTTI_VERSION_MINOR; + nMicro = RTTI_VERSION_MICRO; +} + +bool CWrapper::GetLastError(IBase* pInstance, std::string & sErrorMessage) +{ + if (pInstance) { + return pInstance->GetLastErrorMessage (sErrorMessage); + } else { + return false; + } +} + +void CWrapper::ReleaseInstance(IBase* pInstance) +{ + IBase::ReleaseBaseClassInterface(pInstance); +} + +void CWrapper::AcquireInstance(IBase* pInstance) +{ + IBase::AcquireBaseClassInterface(pInstance); +} + +IZoo * CWrapper::CreateZoo() +{ + auto zoo = new CZoo(); + + auto& animals = zoo->Animals(); + + animals.emplace_back(new CGiraffe("Gerald Giraffe")); + animals.emplace_back(new CTiger("Timmy Tiger")); + animals.emplace_back(new CTiger("Tony Tiger")); + animals.emplace_back(new CSnake("Sebastian Snake")); + animals.emplace_back(new CTurtle("Tobias Turtle")); + animals.emplace_back(new CTurtle("Theo Turtle")); + animals.emplace_back(new CTurtle("Tomás Turtle")); + animals.emplace_back(new CSnake("Slytherin Snake")); + animals.emplace_back(new CTiger("Travis Tiger")); + animals.emplace_back(new CGiraffe("Gary Giraffe")); + + return zoo; +} + + diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animal.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animal.cpp new file mode 100644 index 00000000..a77f4d28 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animal.cpp @@ -0,0 +1,36 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is a stub class definition of CAnimal + +*/ + +#include "rtti_animal.hpp" +#include "rtti_interfaceexception.hpp" + +// Include custom headers here. +#include + +using namespace RTTI::Impl; + +/************************************************************************************************************************* + Class definition of CAnimal +**************************************************************************************************************************/ +CAnimal::CAnimal() = default; + +CAnimal::CAnimal(std::string sName) + : m_sName(sName) +{} + +CAnimal::~CAnimal() +{ + std::cout << "Delete " << m_sName << std::endl; +} + +std::string CAnimal::Name() +{ + return m_sName; +} diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animal.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animal.hpp new file mode 100644 index 00000000..9c839d0e --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animal.hpp @@ -0,0 +1,70 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is the class declaration of CAnimal + +*/ + + +#ifndef __RTTI_ANIMAL +#define __RTTI_ANIMAL + +#include "rtti_interfaces.hpp" + +// Parent classes +#include "rtti_base.hpp" +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4250) +#endif + +// Include custom headers here. + + +namespace RTTI { +namespace Impl { + + +/************************************************************************************************************************* + Class declaration of CAnimal +**************************************************************************************************************************/ + +class CAnimal : public virtual IAnimal, public virtual CBase { +private: + + /** + * Put private members here. + */ + std::string m_sName; +protected: + + /** + * Put protected members here. + */ + CAnimal(); + ~CAnimal(); + explicit CAnimal(std::string sName); +public: + + /** + * Put additional public members here. They will not be visible in the external API. + */ + + + /** + * Public member functions to implement. + */ + std::string Name() override; + +}; + +} // namespace Impl +} // namespace RTTI + +#ifdef _MSC_VER +#pragma warning(pop) +#endif +#endif // __RTTI_ANIMAL diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animaliterator.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animaliterator.cpp new file mode 100644 index 00000000..f7cd5cc2 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animaliterator.cpp @@ -0,0 +1,37 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is a stub class definition of CAnimalIterator + +*/ + +#include "rtti_animaliterator.hpp" +#include "rtti_interfaceexception.hpp" + +// Include custom headers here. +#include "rtti_animal.hpp" + +using namespace RTTI::Impl; + +/************************************************************************************************************************* + Class definition of CAnimalIterator +**************************************************************************************************************************/ +CAnimalIterator::CAnimalIterator(std::vector::iterator begin, std::vector::iterator end) + : m_Current(begin) + , m_End(end) +{ +} + +IAnimal * CAnimalIterator::GetNextAnimal() +{ + if (m_Current != m_End) { + auto i = *(m_Current++); + i->IncRefCount(); + return i; + } else { + return nullptr; + } +} diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animaliterator.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animaliterator.hpp new file mode 100644 index 00000000..f7ec566b --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_animaliterator.hpp @@ -0,0 +1,71 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is the class declaration of CAnimalIterator + +*/ + + +#ifndef __RTTI_ANIMALITERATOR +#define __RTTI_ANIMALITERATOR + +#include "rtti_interfaces.hpp" + +// Parent classes +#include "rtti_base.hpp" +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4250) +#endif + +// Include custom headers here. +#include + +namespace RTTI { +namespace Impl { + +class CAnimal; + +/************************************************************************************************************************* + Class declaration of CAnimalIterator +**************************************************************************************************************************/ + +class CAnimalIterator : public virtual IAnimalIterator, public virtual CBase { +private: + + /** + * Put private members here. + */ + std::vector::iterator m_Current; + std::vector::iterator m_End; +protected: + + /** + * Put protected members here. + */ + +public: + + /** + * Put additional public members here. They will not be visible in the external API. + */ + CAnimalIterator(std::vector::iterator begin, std::vector::iterator end); + + /** + * Public member functions to implement. + */ + + IAnimal * GetNextAnimal() override; + +}; + +} // namespace Impl +} // namespace RTTI + +#ifdef _MSC_VER +#pragma warning(pop) +#endif +#endif // __RTTI_ANIMALITERATOR diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_base.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_base.cpp new file mode 100644 index 00000000..a0ab41f5 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_base.cpp @@ -0,0 +1,61 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is a stub class definition of CBase + +*/ + +#include "rtti_base.hpp" +#include "rtti_interfaceexception.hpp" + +// Include custom headers here. +#include + +using namespace RTTI::Impl; + +/************************************************************************************************************************* + Class definition of CBase +**************************************************************************************************************************/ + +bool CBase::GetLastErrorMessage(std::string & sErrorMessage) +{ + if (m_pLastError.get() != nullptr) { + sErrorMessage = *m_pLastError; + return true; + } else { + sErrorMessage = ""; + return false; + } +} + +void CBase::ClearErrorMessages() +{ + m_pLastError.reset(); +} + +void CBase::RegisterErrorMessage(const std::string & sErrorMessage) +{ + if (m_pLastError.get() == nullptr) { + m_pLastError.reset(new std::string()); + } + *m_pLastError = sErrorMessage; +} + +void CBase::IncRefCount() +{ + ++m_nReferenceCount; +} + +bool CBase::DecRefCount() +{ + m_nReferenceCount--; + if (!m_nReferenceCount) { + delete this; + return true; + } + return false; +} + diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_base.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_base.hpp new file mode 100644 index 00000000..d065e6e6 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_base.hpp @@ -0,0 +1,74 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is the class declaration of CBase + +*/ + + +#ifndef __RTTI_BASE +#define __RTTI_BASE + +#include "rtti_interfaces.hpp" +#include +#include +#include + + +// Include custom headers here. + + +namespace RTTI { +namespace Impl { + + +/************************************************************************************************************************* + Class declaration of CBase +**************************************************************************************************************************/ + +class CBase : public virtual IBase { +private: + + std::unique_ptr m_pLastError; + uint32_t m_nReferenceCount = 1; + + /** + * Put private members here. + */ + +protected: + + /** + * Put protected members here. + */ + +public: + + /** + * Put additional public members here. They will not be visible in the external API. + */ + + bool GetLastErrorMessage(std::string & sErrorMessage) override; + + void ClearErrorMessages() override; + + void RegisterErrorMessage(const std::string & sErrorMessage) override; + + void IncRefCount() override; + + bool DecRefCount() override; + + + /** + * Public member functions to implement. + */ + +}; + +} // namespace Impl +} // namespace RTTI + +#endif // __RTTI_BASE diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_giraffe.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_giraffe.cpp new file mode 100644 index 00000000..6c90106a --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_giraffe.cpp @@ -0,0 +1,25 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is a stub class definition of CGiraffe + +*/ + +#include "rtti_giraffe.hpp" +#include "rtti_interfaceexception.hpp" + +// Include custom headers here. + + +using namespace RTTI::Impl; + +/************************************************************************************************************************* + Class definition of CGiraffe +**************************************************************************************************************************/ +CGiraffe::CGiraffe(std::string sName) + : CAnimal(sName) +{ +} diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_giraffe.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_giraffe.hpp new file mode 100644 index 00000000..7ee1c17d --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_giraffe.hpp @@ -0,0 +1,67 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is the class declaration of CGiraffe + +*/ + + +#ifndef __RTTI_GIRAFFE +#define __RTTI_GIRAFFE + +#include "rtti_interfaces.hpp" + +// Parent classes +#include "rtti_mammal.hpp" +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4250) +#endif + +// Include custom headers here. + + +namespace RTTI { +namespace Impl { + + +/************************************************************************************************************************* + Class declaration of CGiraffe +**************************************************************************************************************************/ + +class CGiraffe : public virtual IGiraffe, public virtual CMammal { +private: + + /** + * Put private members here. + */ + +protected: + + /** + * Put protected members here. + */ + +public: + + /** + * Put additional public members here. They will not be visible in the external API. + */ + explicit CGiraffe(std::string sName); + + /** + * Public member functions to implement. + */ + +}; + +} // namespace Impl +} // namespace RTTI + +#ifdef _MSC_VER +#pragma warning(pop) +#endif +#endif // __RTTI_GIRAFFE diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_mammal.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_mammal.cpp new file mode 100644 index 00000000..a955239c --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_mammal.cpp @@ -0,0 +1,22 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is a stub class definition of CMammal + +*/ + +#include "rtti_mammal.hpp" +#include "rtti_interfaceexception.hpp" + +// Include custom headers here. + + +using namespace RTTI::Impl; + +/************************************************************************************************************************* + Class definition of CMammal +**************************************************************************************************************************/ + diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_mammal.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_mammal.hpp new file mode 100644 index 00000000..a99d4af4 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_mammal.hpp @@ -0,0 +1,67 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is the class declaration of CMammal + +*/ + + +#ifndef __RTTI_MAMMAL +#define __RTTI_MAMMAL + +#include "rtti_interfaces.hpp" + +// Parent classes +#include "rtti_animal.hpp" +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4250) +#endif + +// Include custom headers here. + + +namespace RTTI { +namespace Impl { + + +/************************************************************************************************************************* + Class declaration of CMammal +**************************************************************************************************************************/ + +class CMammal : public virtual IMammal, public virtual CAnimal { +private: + + /** + * Put private members here. + */ + +protected: + + /** + * Put protected members here. + */ + +public: + + /** + * Put additional public members here. They will not be visible in the external API. + */ + + + /** + * Public member functions to implement. + */ + +}; + +} // namespace Impl +} // namespace RTTI + +#ifdef _MSC_VER +#pragma warning(pop) +#endif +#endif // __RTTI_MAMMAL diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_reptile.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_reptile.cpp new file mode 100644 index 00000000..9ec7119a --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_reptile.cpp @@ -0,0 +1,22 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is a stub class definition of CReptile + +*/ + +#include "rtti_reptile.hpp" +#include "rtti_interfaceexception.hpp" + +// Include custom headers here. + + +using namespace RTTI::Impl; + +/************************************************************************************************************************* + Class definition of CReptile +**************************************************************************************************************************/ + diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_reptile.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_reptile.hpp new file mode 100644 index 00000000..83e7bc9d --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_reptile.hpp @@ -0,0 +1,67 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is the class declaration of CReptile + +*/ + + +#ifndef __RTTI_REPTILE +#define __RTTI_REPTILE + +#include "rtti_interfaces.hpp" + +// Parent classes +#include "rtti_animal.hpp" +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4250) +#endif + +// Include custom headers here. + + +namespace RTTI { +namespace Impl { + + +/************************************************************************************************************************* + Class declaration of CReptile +**************************************************************************************************************************/ + +class CReptile : public virtual IReptile, public virtual CAnimal { +private: + + /** + * Put private members here. + */ + +protected: + + /** + * Put protected members here. + */ + +public: + + /** + * Put additional public members here. They will not be visible in the external API. + */ + + + /** + * Public member functions to implement. + */ + +}; + +} // namespace Impl +} // namespace RTTI + +#ifdef _MSC_VER +#pragma warning(pop) +#endif +#endif // __RTTI_REPTILE diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_snake.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_snake.cpp new file mode 100644 index 00000000..261208b2 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_snake.cpp @@ -0,0 +1,26 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is a stub class definition of CSnake + +*/ + +#include "rtti_snake.hpp" +#include "rtti_interfaceexception.hpp" + +// Include custom headers here. + + +using namespace RTTI::Impl; + +/************************************************************************************************************************* + Class definition of CSnake +**************************************************************************************************************************/ +CSnake::CSnake(std::string sName) + : CAnimal(sName) +{ +} + diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_snake.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_snake.hpp new file mode 100644 index 00000000..d752f6cd --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_snake.hpp @@ -0,0 +1,67 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is the class declaration of CSnake + +*/ + + +#ifndef __RTTI_SNAKE +#define __RTTI_SNAKE + +#include "rtti_interfaces.hpp" + +// Parent classes +#include "rtti_reptile.hpp" +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4250) +#endif + +// Include custom headers here. + + +namespace RTTI { +namespace Impl { + + +/************************************************************************************************************************* + Class declaration of CSnake +**************************************************************************************************************************/ + +class CSnake : public virtual ISnake, public virtual CReptile { +private: + + /** + * Put private members here. + */ + +protected: + + /** + * Put protected members here. + */ + +public: + + /** + * Put additional public members here. They will not be visible in the external API. + */ + explicit CSnake(std::string sName); + + /** + * Public member functions to implement. + */ + +}; + +} // namespace Impl +} // namespace RTTI + +#ifdef _MSC_VER +#pragma warning(pop) +#endif +#endif // __RTTI_SNAKE diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_tiger.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_tiger.cpp new file mode 100644 index 00000000..a318e5aa --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_tiger.cpp @@ -0,0 +1,31 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is a stub class definition of CTiger + +*/ + +#include "rtti_tiger.hpp" +#include "rtti_interfaceexception.hpp" + +// Include custom headers here. +#include + +using namespace RTTI::Impl; + +/************************************************************************************************************************* + Class definition of CTiger +**************************************************************************************************************************/ +CTiger::CTiger(std::string sName) + : CAnimal(sName) +{ +} + +void CTiger::Roar() +{ + std::cout << "ROAAAAARRRRR!!" << std::endl; +} + diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_tiger.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_tiger.hpp new file mode 100644 index 00000000..d49e2692 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_tiger.hpp @@ -0,0 +1,69 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is the class declaration of CTiger + +*/ + + +#ifndef __RTTI_TIGER +#define __RTTI_TIGER + +#include "rtti_interfaces.hpp" + +// Parent classes +#include "rtti_mammal.hpp" +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4250) +#endif + +// Include custom headers here. + + +namespace RTTI { +namespace Impl { + + +/************************************************************************************************************************* + Class declaration of CTiger +**************************************************************************************************************************/ + +class CTiger : public virtual ITiger, public virtual CMammal { +private: + + /** + * Put private members here. + */ + +protected: + + /** + * Put protected members here. + */ + +public: + + /** + * Put additional public members here. They will not be visible in the external API. + */ + explicit CTiger(std::string sName); + + /** + * Public member functions to implement. + */ + + void Roar() override; + +}; + +} // namespace Impl +} // namespace RTTI + +#ifdef _MSC_VER +#pragma warning(pop) +#endif +#endif // __RTTI_TIGER diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_turtle.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_turtle.cpp new file mode 100644 index 00000000..bc14a6ad --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_turtle.cpp @@ -0,0 +1,25 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is a stub class definition of CTurtle + +*/ + +#include "rtti_turtle.hpp" +#include "rtti_interfaceexception.hpp" + +// Include custom headers here. + + +using namespace RTTI::Impl; + +/************************************************************************************************************************* + Class definition of CTurtle +**************************************************************************************************************************/ +CTurtle::CTurtle(std::string sName) + : CAnimal(sName) +{ +} diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_turtle.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_turtle.hpp new file mode 100644 index 00000000..dd2423c6 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_turtle.hpp @@ -0,0 +1,67 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is the class declaration of CTurtle + +*/ + + +#ifndef __RTTI_TURTLE +#define __RTTI_TURTLE + +#include "rtti_interfaces.hpp" + +// Parent classes +#include "rtti_reptile.hpp" +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4250) +#endif + +// Include custom headers here. + + +namespace RTTI { +namespace Impl { + + +/************************************************************************************************************************* + Class declaration of CTurtle +**************************************************************************************************************************/ + +class CTurtle : public virtual ITurtle, public virtual CReptile { +private: + + /** + * Put private members here. + */ + +protected: + + /** + * Put protected members here. + */ + +public: + + /** + * Put additional public members here. They will not be visible in the external API. + */ + explicit CTurtle(std::string sName); + + /** + * Public member functions to implement. + */ + +}; + +} // namespace Impl +} // namespace RTTI + +#ifdef _MSC_VER +#pragma warning(pop) +#endif +#endif // __RTTI_TURTLE diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_zoo.cpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_zoo.cpp new file mode 100644 index 00000000..6ea8fb50 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_zoo.cpp @@ -0,0 +1,39 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is a stub class definition of CZoo + +*/ + +#include "rtti_zoo.hpp" +#include "rtti_interfaceexception.hpp" + +// Include custom headers here. +#include "rtti_animal.hpp" +#include "rtti_animaliterator.hpp" + +using namespace RTTI::Impl; + +/************************************************************************************************************************* + Class definition of CZoo +**************************************************************************************************************************/ +CZoo::~CZoo() +{ + for(auto &&i: m_Animals) + { + i->DecRefCount(); + } +} + +std::vector &CZoo::Animals() +{ + return m_Animals; +} + +IAnimalIterator * CZoo::Iterator() +{ + return new CAnimalIterator(m_Animals.begin(), m_Animals.end()); +} diff --git a/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_zoo.hpp b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_zoo.hpp new file mode 100644 index 00000000..1e1af969 --- /dev/null +++ b/Examples/RTTI/RTTI_component/Implementations/Cpp/Stub/rtti_zoo.hpp @@ -0,0 +1,73 @@ +/*++ + +Copyright (C) 2020 ADSK + +All rights reserved. + +Abstract: This is the class declaration of CZoo + +*/ + + +#ifndef __RTTI_ZOO +#define __RTTI_ZOO + +#include "rtti_interfaces.hpp" + +// Parent classes +#include "rtti_base.hpp" +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4250) +#endif + +// Include custom headers here. +#include + +namespace RTTI { +namespace Impl { + +class CAnimal; +/************************************************************************************************************************* + Class declaration of CZoo +**************************************************************************************************************************/ + +class CZoo : public virtual IZoo, public virtual CBase { +private: + + /** + * Put private members here. + */ + std::vector m_Animals; + +protected: + + /** + * Put protected members here. + */ + +public: + + /** + * Put additional public members here. They will not be visible in the external API. + */ + ~CZoo(); + + std::vector& Animals(); + + + /** + * Public member functions to implement. + */ + + IAnimalIterator * Iterator() override; + +}; + +} // namespace Impl +} // namespace RTTI + +#ifdef _MSC_VER +#pragma warning(pop) +#endif +#endif // __RTTI_ZOO diff --git a/Examples/RTTI/RTTI_component/license.txt b/Examples/RTTI/RTTI_component/license.txt new file mode 100644 index 00000000..3eefc675 --- /dev/null +++ b/Examples/RTTI/RTTI_component/license.txt @@ -0,0 +1,5 @@ +Copyright (C) 2020 ADSK + +All rights reserved. + + diff --git a/Source/buildbindingccpp.go b/Source/buildbindingccpp.go index 0b513195..0ef36546 100644 --- a/Source/buildbindingccpp.go +++ b/Source/buildbindingccpp.go @@ -36,6 +36,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package main import ( + "crypto/md5" "fmt" "log" "path" @@ -898,7 +899,7 @@ func writeDynamicCppBaseClassMethods(component ComponentDefinition, baseClass Co w.Writeln(" return m_pWrapper;") w.Writeln(" }") - w.Writeln(" ") + w.Writeln("") w.Writeln(" friend class CWrapper;") return nil } @@ -1130,6 +1131,7 @@ func buildCppHeader(component ComponentDefinition, w LanguageWriter, NameSpace s w.Writeln("#else // _WIN32") w.Writeln("#include ") w.Writeln("#endif // _WIN32") + w.Writeln("#include ") w.Writeln("#include ") w.Writeln("#include ") w.Writeln("#include ") @@ -1142,6 +1144,14 @@ func buildCppHeader(component ComponentDefinition, w LanguageWriter, NameSpace s buildBindingCPPAllForwardDeclarations(component, w, NameSpace, cppClassPrefix, ClassIdentifier) + w.Writeln("/*************************************************************************************************************************") + w.Writeln(" %s_cast Definition", strings.ToLower(NameSpace)) + w.Writeln("**************************************************************************************************************************/") + w.Writeln("template ") + w.Writeln("inline std::shared_ptr %s_cast(PBase obj);", strings.ToLower(NameSpace)) + w.Writeln("") + w.Writeln("using %s_ClassHash = std::array<%s_uint8, %d>;", NameSpace, NameSpace, md5.Size) + w.Writeln("") w.Writeln("/*************************************************************************************************************************") w.Writeln(" classParam Definition") @@ -1400,6 +1410,8 @@ func buildCppHeader(component ComponentDefinition, w LanguageWriter, NameSpace s w.Writeln("**************************************************************************************************************************/") w.Writeln("class %s %s{", cppClassName, inheritanceSpecifier) w.Writeln("public:") + w.Writeln(" static inline const std::string &getClassName();") + w.Writeln(" static inline const %s_ClassHash &getClassHash();", NameSpace) w.Writeln(" ") if !component.isBaseClass(class) { w.Writeln(" /**") @@ -1429,6 +1441,64 @@ func buildCppHeader(component ComponentDefinition, w LanguageWriter, NameSpace s w.Writeln("};") } + w.Writeln("") + w.Writeln("/*************************************************************************************************************************") + w.Writeln(" RTTI static getClassName implementations") + w.Writeln("**************************************************************************************************************************/") + w.Writeln("") + + for i := 0; i < len(component.Classes); i++ { + class := component.Classes[i] + w.Writeln(" const std::string &C%s::getClassName()", class.ClassName) + w.Writeln(" {") + w.Writeln(" static const std::string s_sClassName = \"%s\";", class.ClassName) + w.Writeln(" return s_sClassName;") + w.Writeln(" }") + w.Writeln("") + + classHash := class.classHash() + + w.Writeln(" const %s_ClassHash &C%s::getClassHash()", NameSpace, class.ClassName) + w.Writeln(" {") + w.Writeln(" // MD5(%s): %X", class.ClassName, classHash) + + w.BeginLine() + w.Printf(" static const %s_ClassHash s_sClassHash = {", NameSpace) + for j := 0; j < len(classHash); j++ { + w.Printf(" 0x%02X,", classHash[j]) + } + w.Printf(" };") + w.EndLine() + w.Writeln(" return s_sClassHash;") + w.Writeln(" }") + w.Writeln("") + } + + w.Writeln("") + w.Writeln("/*************************************************************************************************************************") + w.Writeln(" RTTI cast implementation") + w.Writeln("**************************************************************************************************************************/") + w.Writeln("") + + w.Writeln(" template ") + w.Writeln(" std::shared_ptr %s_cast(PBase pObj)", strings.ToLower(NameSpace)) + w.Writeln(" {") + w.Writeln(" static_assert(std::is_convertible::value, \"T must be convertible to %s::CBase\");", NameSpace) + w.Writeln("") + w.Writeln(" if (pObj) {") + w.Writeln(" CWrapper *pWrapper = pObj->wrapper();") + w.Writeln(" const %s_ClassHash & ClassHash = T::getClassHash();", NameSpace) + w.Writeln(" CInputVector<%s_uint8> ClassHashBuffer(ClassHash.data(), ClassHash.size());", NameSpace) + w.Writeln(" if (pWrapper->%s(pObj.get(), ClassHashBuffer)) {", global.ImplementsInterfaceMethod) + w.Writeln(" pWrapper->%s(pObj);", global.AcquireMethod) + w.Writeln(" return std::make_shared(pWrapper, pObj->handle());") + w.Writeln(" }") + w.Writeln(" }") + w.Writeln("") + w.Writeln(" return nullptr;") + w.Writeln(" }") + + for j := 0; j < len(global.Methods); j++ { method := global.Methods[j] diff --git a/Source/buildbindingpython.go b/Source/buildbindingpython.go index 9eb7f0fd..11b40cf8 100644 --- a/Source/buildbindingpython.go +++ b/Source/buildbindingpython.go @@ -723,11 +723,19 @@ func generateCTypesParameter(param ComponentDefinitionParam, className string, m func writePythonClass(component ComponentDefinition, class ComponentDefinitionClass, w LanguageWriter, NameSpace string) error { pythonBaseClassName := fmt.Sprintf("%s", component.Global.BaseClassName) + + global := component.Global w.Writeln("''' Class Implementation for %s", class.ClassName) w.Writeln("'''") parentClass := "" + hash := class.classHash() + hashString := "" + for i := range hash { + hashString = hashString + fmt.Sprintf("\\x%02X", hash[i]) + } + if (!component.isBaseClass(class)) { if (class.ParentClass != "") { parentClass = fmt.Sprintf("%s", class.ParentClass) @@ -735,11 +743,36 @@ func writePythonClass(component ComponentDefinition, class ComponentDefinitionCl parentClass = pythonBaseClassName } w.Writeln("class %s(%s):", class.ClassName, parentClass) + w.Writeln(" @staticmethod") + w.Writeln(" def ClassName():") + w.Writeln(" return \"%s\"", class.ClassName) + w.Writeln(" ") + w.Writeln(" @staticmethod") + w.Writeln(" def ClassHash():") + w.Writeln(" return bytearray(b'%s')", hashString) + w.Writeln(" ") w.Writeln(" def __init__(self, handle, wrapper):") w.Writeln(" %s.__init__(self, handle, wrapper)", parentClass) } else { w.Writeln("class %s:", class.ClassName) + w.Writeln(" @staticmethod") + w.Writeln(" def ClassName():") + w.Writeln(" return \"%s\"", class.ClassName) + w.Writeln(" ") + w.Writeln(" @staticmethod") + w.Writeln(" def ClassHash():") + w.Writeln(" return bytearray(b'%s')", hashString) + w.Writeln(" ") + + w.Writeln(" @classmethod") + w.Writeln(" def cast(cls, instance):") + w.Writeln(" if instance and instance._wrapper.%s(instance, cls.ClassHash()):", global.ImplementsInterfaceMethod) + w.Writeln(" instance._wrapper.%s(instance)", global.AcquireMethod) + w.Writeln(" return cls(instance._handle, instance._wrapper)") + w.Writeln(" return None") + w.Writeln(" ") + w.Writeln(" def __init__(self, handle, wrapper):") w.Writeln(" if not handle or not wrapper:") w.Writeln(" raise E%sException(ErrorCodes.INVALIDPARAM)", NameSpace) diff --git a/Source/buildimplementationcpp.go b/Source/buildimplementationcpp.go index 71fae07f..706e8773 100644 --- a/Source/buildimplementationcpp.go +++ b/Source/buildimplementationcpp.go @@ -35,10 +35,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package main import ( + "crypto/md5" + "errors" "fmt" "log" - "errors" "path" + "sort" "strings" ) @@ -560,13 +562,13 @@ func buildCPPInterfaces(component ComponentDefinition, w LanguageWriter, NameSpa for j := 0; j < len(global.Methods); j++ { method := global.Methods[j] - // Omit Journal Method + // Omit special functions that are automatically implemented isSpecialFunction, err := CheckHeaderSpecialFunction(method, global); if err != nil { return err } if (isSpecialFunction == eSpecialMethodJournal) || (isSpecialFunction == eSpecialMethodInjection) || - (isSpecialFunction == eSpecialMethodSymbolLookup) { + (isSpecialFunction == eSpecialMethodSymbolLookup) || (isSpecialFunction == eSpecialMethodImplementsInterface ) { continue } @@ -618,13 +620,13 @@ func buildCPPGlobalStubFile(component ComponentDefinition, stubfile LanguageWrit thisMethodDefaultImpl := defaultImplementation - // Treat special functions + // Omit special functions that are automatically implemented isSpecialFunction, err := CheckHeaderSpecialFunction(method, component.Global); if err != nil { return err } if (isSpecialFunction == eSpecialMethodJournal) || (isSpecialFunction == eSpecialMethodInjection) || - (isSpecialFunction == eSpecialMethodSymbolLookup) { + (isSpecialFunction == eSpecialMethodSymbolLookup) || (isSpecialFunction == eSpecialMethodImplementsInterface) { continue } if (isSpecialFunction == eSpecialMethodVersion) { @@ -691,6 +693,80 @@ func buildCPPInterfaceWrapperMethods(component ComponentDefinition, class Compon return nil } +func writeCImplementsInterfaceMethod(component ComponentDefinition, method ComponentDefinitionMethod, w LanguageWriter, NameSpace string, IBaseClassName string) error { + cParams, err := GenerateCParameters(method, "", NameSpace) + if err != nil { + return err + } + + cparameters := "" + for _, cParam := range cParams { + if cparameters != "" { + cparameters = cparameters + ", " + } + cparameters = cparameters + cParam.ParamType + " " + cParam.ParamName + } + + CMethodName := fmt.Sprintf("%s_%s", strings.ToLower(NameSpace), strings.ToLower(method.MethodName)) + + classHashMap := make(map[string][]ComponentDefinitionClass) + + for i := 0; i < len(component.Classes); i++ { + class := component.Classes[i] + key := fmt.Sprintf("%02X", class.classHash()[0]) + classHashMap[key] = append(classHashMap[key], class) + } + + w.Writeln("") + w.Writeln("/*************************************************************************************************************************") + w.Writeln(" %s", method.MethodDescription) + w.Writeln("**************************************************************************************************************************/") + w.Writeln("") + + w.Writeln("%sResult %s(%s)", NameSpace, CMethodName, cparameters) + w.Writeln("{") + w.Writeln(" if (nClassHashBufferSize != %d) // Hash length must be as expected", md5.Size) + w.Writeln(" return RTTI_ERROR_INVALIDPARAM;") + w.Writeln("") + w.Writeln(" %s* pIBaseClassInstance = (%s *)pObject;", IBaseClassName, IBaseClassName) + w.Writeln("") + w.Writeln(" switch(pClassHashBuffer[0]) {") + + keys := make([]string, 0, len(classHashMap)) + for key := range classHashMap { + keys = append(keys, key) + } + sort.Strings(keys) + + for i := range keys { + w.Writeln(" case 0x%s:", keys[i]) + classes := classHashMap[keys[i]] + for j := range classes { + class := classes[j] + hash := class.classHash() + w.BeginLine() + w.Printf(" static const RTTI_uint8 s_%sHash[] = {", class.ClassName) + for j := 0; j < len(hash); j++ { + w.Printf(" 0x%02X,", hash[j]) + } + w.Printf(" };") + w.EndLine() + + w.Writeln(" if (memcmp(pClassHashBuffer, s_%sHash, 16) == 0) {", class.ClassName) + w.Writeln(" *pImplementsInterface = dynamic_cast(pIBaseClassInstance) != nullptr;", class.ClassName) + w.Writeln(" return RTTI_SUCCESS;") + w.Writeln(" }") + } + w.Writeln(" break;") + } + w.Writeln(" }") + w.Writeln(" return %s_ERROR_INVALIDPARAM;", strings.ToUpper(NameSpace)) + w.Writeln("}") + w.Writeln("") + + return nil +} + func buildCPPGetSymbolAddressMethod(component ComponentDefinition, w LanguageWriter, NameSpace string, NameSpaceImplementation string) error { w.Writeln("") @@ -719,8 +795,9 @@ func buildCPPGetSymbolAddressMethod(component ComponentDefinition, w LanguageWri method := global.Methods[j] procName := strings.ToLower(method.MethodName) - processfuncMap = append (processfuncMap, fmt.Sprintf("%s_%s", strings.ToLower(NameSpace), procName)); + processfuncMap = append (processfuncMap, fmt.Sprintf("%s_%s", strings.ToLower(NameSpace), procName)); } + w.Writeln("if (pProcName == nullptr)") w.Writeln(" return %s_ERROR_INVALIDPARAM;", strings.ToUpper(NameSpace)) @@ -850,7 +927,7 @@ func buildCPPInterfaceWrapper(component ComponentDefinition, w LanguageWriter, N method := global.Methods[j] // Check for special functions - isSpecialFunction, err := CheckHeaderSpecialFunction (method, global); + isSpecialFunction, err := CheckHeaderSpecialFunction(method, global); if err != nil { return err } @@ -861,6 +938,15 @@ func buildCPPInterfaceWrapper(component ComponentDefinition, w LanguageWriter, N doMethodJournal = false; } + if (isSpecialFunction == eSpecialMethodImplementsInterface) { + err = writeCImplementsInterfaceMethod(component, method, w, NameSpace, IBaseClassName) + if err != nil { + return err + } + continue + } + + // Write Static function implementation err = writeCImplementationMethod(component, method, w, BaseName, NameSpace, NameSpaceImplementation, ClassIdentifier, "Wrapper", component.Global.BaseClassName, true, doMethodJournal, isSpecialFunction, false) if err != nil { diff --git a/Source/componentdefinition.go b/Source/componentdefinition.go index d05477b5..6d354fd7 100644 --- a/Source/componentdefinition.go +++ b/Source/componentdefinition.go @@ -34,17 +34,18 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package main import ( - "strconv" - "fmt" - "errors" + "crypto/md5" "encoding/xml" - "regexp" - "strings" + "errors" + "fmt" + "io/ioutil" "log" "math" "os" - "io/ioutil" "path/filepath" + "regexp" + "strconv" + "strings" ) const ( @@ -58,6 +59,7 @@ const ( eSpecialMethodJournal = 7 eSpecialMethodPrerelease = 8 eSpecialMethodBuildinfo = 9 + eSpecialMethodImplementsInterface = 10 ) // ComponentDefinitionParam definition of a method parameter used in the component's API @@ -118,6 +120,7 @@ type ComponentDefinitionGlobal struct { BaseClassName string `xml:"baseclassname,attr"` StringOutBaseClassName string `xml:"stringoutclassname,attr"` ErrorMethod string `xml:"errormethod,attr"` + ImplementsInterfaceMethod string `xml:"implementsinterfacemethod,attr"` ReleaseMethod string `xml:"releasemethod,attr"` AcquireMethod string `xml:"acquiremethod,attr"` SymbolLookupMethod string `xml:"symbollookupmethod,attr"` @@ -513,8 +516,10 @@ func (component *ComponentDefinition) checkClasses() (error) { classLowerNameList := make(map[string]bool, 0) classNameIndex := make(map[string]int, 0) + classHashIndex := make(map[string]int, 0) for i := 0; i < len(classes); i++ { class := classes[i]; + hashString := fmt.Sprintf("%X", class.classHash()); if !nameIsValidIdentifier(class.ClassName) { return fmt.Errorf ("invalid class name \"%s\"", class.ClassName); } @@ -524,10 +529,15 @@ func (component *ComponentDefinition) checkClasses() (error) { if len(class.ClassDescription) > 0 && !descriptionIsValid(class.ClassDescription) { return fmt.Errorf ("invalid class description \"%s\" in class \"%s\"", class.ClassDescription, class.ClassName); } + collision, hashExists := classHashIndex[hashString] + if hashExists { + return fmt.Errorf ("hash collision for classes \"%s\" and \"%s\"", classes[collision].ClassName, class.ClassName); + } classLowerNameList[strings.ToLower(class.ClassName)] = true (*classNameList)[class.ClassName] = true classNameIndex[class.ClassName] = i + classHashIndex[hashString] = i } // Check parent class definitions @@ -1018,6 +1028,10 @@ func CheckHeaderSpecialFunction (method ComponentDefinitionMethod, global Compon return eSpecialMethodNone, errors.New ("No error method specified"); } + if (global.ImplementsInterfaceMethod == "") { + return eSpecialMethodNone, errors.New ("No implements inteface method specified"); + } + if (global.ReleaseMethod == global.JournalMethod) { return eSpecialMethodNone, errors.New ("Release method can not be the same as the Journal method"); } @@ -1126,6 +1140,21 @@ func CheckHeaderSpecialFunction (method ComponentDefinitionMethod, global Compon return eSpecialMethodError, nil; } + if (method.MethodName == global.ImplementsInterfaceMethod) { + if (len (method.Params) != 3) { + return eSpecialMethodNone, errors.New ("Implements Interface method does not match the expected function template"); + } + + if (method.Params[0].ParamType != "class") || (method.Params[0].ParamPass != "in") || + (method.Params[1].ParamType != "basicarray") || (method.Params[1].ParamClass != "uint8") || (method.Params[1].ParamPass != "in") || + (method.Params[2].ParamType != "bool") || (method.Params[2].ParamPass != "return") || + (method.Params[0].ParamClass != global.BaseClassName) { + return eSpecialMethodNone, errors.New ("Implements Interface method does not match the expected function template"); + } + + return eSpecialMethodImplementsInterface, nil; + } + if len(global.PrereleaseMethod)>0 && (global.PrereleaseMethod == global.BuildinfoMethod) { return eSpecialMethodNone, errors.New ("Prerelease method can not be the same as the buildinfo method"); } @@ -1161,7 +1190,6 @@ func CheckHeaderSpecialFunction (method ComponentDefinitionMethod, global Compon return eSpecialMethodNone, nil; } - // GetLastErrorMessageMethod returns the xml definition of the GetLastErrorMessage-method func GetLastErrorMessageMethod() (ComponentDefinitionMethod) { var method ComponentDefinitionMethod @@ -1368,3 +1396,8 @@ func (component *ComponentDefinition) countMaxOutParameters() (uint32) { return maxOutParameters; } +func (class *ComponentDefinitionClass) classHash() []byte { + hash := md5.New() + hash.Write([]byte(class.ClassName)) + return hash.Sum(nil) +}