The OdRxObject class expands object comparison. The comparing can be an exact equivalence of instances, equivalence of classes, comparison of contents, or equivalence of instances according to a defined meaning. For example, the xClsA class and the xClsB class are derived from the OdRxObject class and have the following definition:
class xClsA : public OdRxObjectImpl<OdRxObject>
{
public:
OdUInt32 parameter;
ODRX_DECLARE_MEMBERS(xClsA);
bool isEqualTo(const OdRxObject* pOther) const;
OdRx::Ordering comparedTo(const OdRxObject* pOther) const;
xClsA() {};
~xClsA() {};
};
class xClsB : public OdRxObjectImpl<OdRxObject>
{
public:
OdUInt32 parameter;
ODRX_DECLARE_MEMBERS(xClsB);
xClsB() {};
~xClsB() {};
};
ODRX_DEFINE_MEMBERS(xClsA, OdRxObject, NEWOBJ_CONSTR);
ODRX_DEFINE_MEMBERS(xClsB, OdRxObject, NEWOBJ_CONSTR);
bool xClsA::isEqualTo(const OdRxObject* pOther) const
{
return ((pOther->isA()->name().compare(L"xClsB") == 0) &&
((this->parameter & 0xFFFF) == (((xClsB*)pOther)->parameter & 0xFFFF)))
}
OdRx::Ordering xClsA::comparedTo(const OdRxObject* pOther) const
{
return (pOther->isA()->name().compare(L"xClsB") == 0) &&
((this->parameter < ((xClsB*)pOther)->parameter) ? OdRx::kLessThan :
((this->parameter > ((xClsB*)pOther)->parameter) ? OdRx::kGreaterThan : OdRx::kEqual));
}
The xClsA and xClsB classes must be registered in the program using the rxInit() static method before comparison. If a class is not registered, the RTTI methods generate the eNotApplicable exception. The testing function has the following implementation:
#include "OdaCommon.h"
#include "OdToolKit.h"
#include "..\..\Teigha.dwg\Extensions\ExServices\ExHostAppServices.h"
#include "..\..\Teigha.dwg\Extensions\ExServices\ExSystemServices.h"
class MyApp : public ExSystemServices
{
protected:
ODRX_USING_HEAP_OPERATORS(ExSystemServices);
public:
MyApp() {};
};
// Here, past the definition of classes described above
void main()
{
OdStaticRxObject<MyApp> svcs;
odInitialize(&svcs);
xClsA::rxInit();
xClsB::rxInit();
OdRxObjectPtr pObj1 = xClsA::createObject();
OdRxObjectPtr pObj2 = xClsB::createObject();
OdRxObjectPtr pObj3 = xClsA::createObject();
OdRxObjectPtr pObj4 = xClsB::createObject();
OdRxObjectPtr pObj5 = pObj1.get();
// Here, past a one of cpp-codes described below
xClsB::rxUninit();
xClsA::rxUninit();
odUninitialize();
}
The OdRxObject class provides the ability to compare two Rx instances:
Use the (==) and (!=) operators for raw or smart pointers to the instances being compared. If the pointers store the same address, it is the same instance and the result of the (==) operator returns True; the (!=) operator returns False. For example:
odPrintConsoleString(L"\n(Obj1==Obj3) => %s", (pObj1 == pObj3) ? L"true" : L"false"); // 0 (false)
odPrintConsoleString(L"\n(Obj1!=Obj3) => %s", (pObj1 != pObj3) ? L"true" : L"false"); // 1 (true)
odPrintConsoleString(L"\n(Obj1==Obj5) => %s", (pObj1 == pObj5) ? L"true" : L"false"); // 1 (true)
odPrintConsoleString(L"\n(Obj1!=Obj5) => %s", (pObj1 != pObj5) ? L"true" : L"false"); // 0 (false)
Use the isA() method that returns the pointer to the class description instance and compare the returned addresses or class names. If the pointers store the same address or the class description instances store the same class name, these objects belong to the same class. For example:
odPrintConsoleString(L"\nClass(Obj1==Obj2) => %s", (pObj1->isA() == pObj2->isA()) ? L"true" : L"false"); // 0 (false)
odPrintConsoleString(L"\nClass(Obj1!=Obj2) => %s", (pObj1->isA() != pObj2->isA()) ? L"true" : L"false"); // 1 (true)
odPrintConsoleString(L"\nClass(Obj1==Obj3) => %s", (pObj1->isA() == pObj3->isA()) ? L"true" : L"false"); // 1 (true)
odPrintConsoleString(L"\nClass(Obj1!=Obj3) => %s", (pObj1->isA() != pObj3->isA()) ? L"true" : L"false"); // 0 (false)
Use the isEqualTo() method that returns True when an instance is the equivalent of a specified instance in a defined meaning. The isEqualTo() method is a virtual method that can be redefined in the derived class. In the default implementation, this method compares whether two objects are the same instance (the first way). For example, an instance of the xClsA class is equivalent to an instance of the xClsB class when the 0 to 15 bits of their parameters are equal:
pObj1->parameter = 0x150A0846;
pObj2->parameter = 0x230E0846;
pObj4->parameter = 0x150A0684;
odPrintConsoleString(L"\n(Obj1 Eqv Obj2) => %s", (pObj1->isEqualTo(pObj2)) ? L"true" : L"false"); // 1 (true)
odPrintConsoleString(L"\n(Obj1 Eqv Obj4) => %s", (pObj1->isEqualTo(pObj4)) ? L"true" : L"false"); // 0 (false)
Use the comparedTo() method that returns kLessThan when the current instance is less than a specified instance, kGreaterThan when the current instance is greater than a specified instance, or kEqual when the two instances are equal. For example, an instance of the xClsA class is less than, more than, or equal to an instance of the xClsB class when their parameters are less, more, or equal:
pObj1->parameter = 20;
pObj2->parameter = 30;
pObj4->parameter = 10;
odPrintConsoleString(L"\n(Obj1 %s Obj2)", ((pObj1->comparedTo(pObj2) == OdRx::kLessThan) ? L"<" : L"<=")); // (Obj1 < Obj2)
odPrintConsoleString(L"\n(Obj1 %s Obj4)", ((pObj1->comparedTo(pObj4) == OdRx::kGreateThan) ? L">" : L">=")); // (Obj1 > Obj4)
Comparing the Equivalence of Rx Objects
Comparing the Value of Rx Objects
Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.
|