#ifndef __JLANG__JREFERENCE__ #define __JLANG__JREFERENCE__ #include "JLang/JPointer.hh" #include "JLang/JException.hh" /** * \author mdejong */ namespace JLANG {} namespace JPP { using namespace JLANG; } namespace JLANG { /** * The template JReference class can be used to reference an object. */ template class JReference { public: /** * Constructor * * \param object object */ JReference(JClass_t& object) : __object(object) {} /** * Get reference to object. * * \return reference to object */ const JClass_t& getReference() const { return __object; } /** * Get reference to object. * * \return reference to object */ JClass_t& getReference() { return __object; } /** * Check validity of reference. * * \return true */ bool is_valid() const { return true; } /** * Smart pointer. * * \return pointer to object */ const JClass_t* operator->() const { return &__object; } /** * Smart pointer. * * \return pointer to object */ JClass_t* operator->() { return &__object; } /** * Dereference operator. * * \return reference to object */ const JClass_t& operator*() const { return __object; } /** * Dereference operator. * * \return reference to object */ JClass_t& operator*() { return __object; } protected: JClass_t& __object; }; /** * Template specialisation of JReference class for const data type. */ template class JReference { public: /** * Constructor * * \param object object */ JReference(const JClass_t& object) : __object(object) {} /** * Constructor * * \param reference reference */ JReference(const JReference& reference) : __object(*reference) {} /** * Get reference to object. * * \return reference to object */ const JClass_t& getReference() const { return __object; } /** * Check validity of reference. * * \return true */ bool is_valid() const { return true; } /** * Smart pointer. * * \return pointer to object */ const JClass_t* operator->() const { return &__object; } /** * Dereference operator. * * \return reference to object */ const JClass_t& operator*() const { return __object; } protected: const JClass_t& __object; }; /** * Template specialisation of JReference class for pointer type. */ template class JReference { public: /** * Constructor * * \param pointer pointer */ JReference(JClass_t* const& pointer) : __pointer(pointer) {} /** * Constructor * * \param pointer pointer object */ JReference(const JPointer& pointer) : __pointer(pointer.getReference()) {} /** * Get reference to object. * * \return reference to object */ const JClass_t& getReference() const { return *__pointer; } /** * Check validity of reference. * * \return true if pointer not null; else false */ bool is_valid() const { return (__pointer != NULL); } /** * Smart pointer. * * \return pointer to object */ const JClass_t* operator->() const { if (__pointer == NULL) THROW(JNullPointerException, "JReference::operator->()"); else return __pointer; } /** * Dereference operator. * * \return reference to object */ const JClass_t& operator*() const { if (__pointer == NULL) THROW(JNullPointerException, "JReference::operator*()"); else return *__pointer; } protected: JClass_t* const& __pointer; }; } #endif