Skip to content

Commit

Permalink
Foundation Classes - Reorganize of OCCT RTTI #215
Browse files Browse the repository at this point in the history
Implement functionality to avoid mutex operation and keeping unload data Making Standard_Type own specialized handle to avoid sharing and atomic operations

TODO:
Simplify the base type for type. Create extended type type. Simple will have ONLY reference to std::type_info and parent type. Extended will have a name and size of the object.
  • Loading branch information
dpasukhi committed Dec 29, 2024
1 parent 33c22d1 commit ad3f42d
Show file tree
Hide file tree
Showing 41 changed files with 234 additions and 355 deletions.
2 changes: 1 addition & 1 deletion src/BOPTest/BOPTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void BOPTest::ReportAlerts(const Handle(Message_Report)& theReport)
for (int iGravity = 0; iGravity < 2; iGravity++)
{
// report shapes for the same type of alert together
NCollection_Map<Handle(Standard_Transient)> aPassedTypes;
NCollection_Map<Handle(Standard_Type)> aPassedTypes;
const Message_ListOfAlert& aList = theReport->GetAlerts (anAlertTypes[iGravity]);
for (Message_ListOfAlert::Iterator aIt (aList); aIt.More(); aIt.Next())
{
Expand Down
4 changes: 2 additions & 2 deletions src/BinLDrivers/BinLDrivers_DocumentStorageDriver.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ private:

BinObjMgt_Persistent myPAtt;
TDF_LabelList myEmptyLabels;
TColStd_MapOfTransient myMapUnsupported;
TColStd_IndexedMapOfTransient myTypesMap;
NCollection_Map<Handle(Standard_Type)> myMapUnsupported;
NCollection_IndexedMap<Handle(Standard_Type)> myTypesMap;
BinLDrivers_VectorOfDocumentSection mySections;
TCollection_ExtendedString myFileName;
//! Sizes of labels and some attributes that will be stored in the second pass
Expand Down
7 changes: 4 additions & 3 deletions src/BinMDF/BinMDF_ADriverTable.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ const Handle(Standard_Type)& BinMDF_ADriverTable::AddDerivedDriver (Standard_CSt
//=======================================================================

void BinMDF_ADriverTable::AssignIds
(const TColStd_IndexedMapOfTransient& theTypes)
(const NCollection_IndexedMap<Handle(Standard_Type)>& theTypes)
{
myMapId.Clear();
Standard_Integer i;
for (i=1; i <= theTypes.Extent(); i++) {
Handle(Standard_Type) aType (Handle(Standard_Type)::DownCast (theTypes(i)));
const Handle(Standard_Type)& aType = theTypes(i);
if (myMap.IsBound (aType)) {
myMapId.Bind (aType, i);
}
Expand Down Expand Up @@ -141,7 +141,8 @@ void BinMDF_ADriverTable::AssignIds
{
if (!myMapId.IsBound2 (aStrId.Value()))
{
if (Handle(Standard_Type) anAdded = AddDerivedDriver (aStrId.Key().ToCString()))
Handle(Standard_Type) anAdded = AddDerivedDriver(aStrId.Key().ToCString());
if (!anAdded.IsNull())
{
myMapId.Bind (anAdded, aStrId.Value());
}
Expand Down
2 changes: 1 addition & 1 deletion src/BinMDF/BinMDF_ADriverTable.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public:
//! Assigns the IDs to the drivers of the given Types.
//! It uses indices in the map as IDs.
//! Useful in storage procedure.
Standard_EXPORT void AssignIds (const TColStd_IndexedMapOfTransient& theTypes);
Standard_EXPORT void AssignIds (const NCollection_IndexedMap<Handle(Standard_Type)>& theTypes);

//! Assigns the IDs to the drivers of the given Type Names;
//! It uses indices in the sequence as IDs.
Expand Down
1 change: 1 addition & 0 deletions src/BinObjMgt/BinObjMgt_Position.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define _BinObjMgt_Position_HeaderFile

#include <Standard_Type.hxx>
#include <Standard_Transient.hxx>

class BinObjMgt_Position;
DEFINE_STANDARD_HANDLE (BinObjMgt_Position, Standard_Transient)
Expand Down
7 changes: 2 additions & 5 deletions src/BndLib/BndLib_Add2dCurve.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -842,12 +842,9 @@ Standard_Boolean BndLib_Box2dCurve::IsTypeBase
(const Handle(Geom2d_Curve)& aC2D,
GeomAbs_CurveType& aTypeB)
{
Standard_Boolean bRet;
Handle(Standard_Type) aType;
Standard_Boolean bRet=Standard_True;
//
bRet=Standard_True;
//
aType=aC2D->DynamicType();
const Handle(Standard_Type)& aType = aC2D->DynamicType();
if (aType==STANDARD_TYPE(Geom2d_Line)) {
aTypeB=GeomAbs_Line;
}
Expand Down
1 change: 1 addition & 0 deletions src/CDF/CDF_MetaDataDriver.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define _CDF_MetaDataDriver_HeaderFile

#include <Standard_Type.hxx>
#include <Standard_Transient.hxx>

class CDM_MetaData;
class TCollection_ExtendedString;
Expand Down
1 change: 1 addition & 0 deletions src/Express/Express.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <Standard_Boolean.hxx>
#include <Standard_Type.hxx>
#include <Standard_OStream.hxx>
#include <Standard_Handle.hxx>

class Express_Schema;
class TCollection_AsciiString;
Expand Down
1 change: 1 addition & 0 deletions src/Express/Express_Field.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define _Express_Field_HeaderFile

#include <Standard_Type.hxx>
#include <Standard_Transient.hxx>

class Express_Type;
class TCollection_HAsciiString;
Expand Down
1 change: 1 addition & 0 deletions src/Express/Express_Type.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define _Express_Type_HeaderFile

#include <Standard_Type.hxx>
#include <Standard_Transient.hxx>

class TCollection_AsciiString;

Expand Down
2 changes: 1 addition & 1 deletion src/Geom2dAdaptor/Geom2dAdaptor_Curve.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void Geom2dAdaptor_Curve::load(const Handle(Geom2d_Curve)& C,
myNestedEvaluator.Nullify();
myBSplineCurve.Nullify();

Handle(Standard_Type) TheType = C->DynamicType();
const Handle(Standard_Type)& TheType = C->DynamicType();
if ( TheType == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
Load(Handle(Geom2d_TrimmedCurve)::DownCast (C)->BasisCurve(),
UFirst,ULast);
Expand Down
4 changes: 1 addition & 3 deletions src/IFSelect/IFSelect_SignAncestor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ Standard_Boolean IFSelect_SignAncestor::Matches(const Handle(Standard_Transient)
const TCollection_AsciiString& text,
const Standard_Boolean /*exact*/) const
{
if (ent.IsNull()) return Standard_False;
DeclareAndCast(Standard_Type,atype,ent);
if (atype.IsNull()) atype = ent->DynamicType();
Handle(Standard_Type) atype = ent->DynamicType();
return atype->SubType(text.ToCString());
}

3 changes: 1 addition & 2 deletions src/IFSelect/IFSelect_SignType.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ static Standard_CString nulsign = "";
const Handle(Interface_InterfaceModel)& /*model*/) const
{
if (ent.IsNull()) return nulsign;
DeclareAndCast(Standard_Type,atype,ent);
if (atype.IsNull()) atype = ent->DynamicType();
Handle(Standard_Type) atype = ent->DynamicType();
Standard_CString tn = atype->Name();
if (!thenopk) return tn;
for (int i = 0; tn[i] != '\0'; i ++) {
Expand Down
2 changes: 1 addition & 1 deletion src/Interface/Interface_FileReaderTool.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ Standard_Boolean Interface_FileReaderTool::RecognizeByLib(const Standard_Integer
}
if (CN <= 0 || proto.IsNull()) return Standard_False;
// Se recaler dans GeneralLib : Creation de l entite vide
Handle(Standard_Type) typrot = proto->DynamicType();
const Handle(Standard_Type)& typrot = proto->DynamicType();
for (glib.Start(); glib.More(); glib.Next()) {
proto = glib.Protocol();
if (proto.IsNull()) continue;
Expand Down
4 changes: 2 additions & 2 deletions src/Interface/Interface_GTool.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ private:
Handle(Interface_Protocol) theproto;
Handle(Interface_SignType) thesign;
Interface_GeneralLib thelib;
Interface_DataMapOfTransientInteger thentnum;
TColStd_IndexedDataMapOfTransientTransient thentmod;
NCollection_DataMap<Handle(Standard_Type),Standard_Integer> thentnum;
NCollection_IndexedDataMap<Handle(Standard_Type),Handle(Standard_Transient)> thentmod;


};
Expand Down
1 change: 1 addition & 0 deletions src/Message/Message_Alert.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define _Message_Alert_HeaderFile

#include <Standard_Type.hxx>
#include <Standard_Transient.hxx>

DEFINE_STANDARD_HANDLE(Message_Alert, Standard_Transient)

Expand Down
6 changes: 1 addition & 5 deletions src/Message/Message_Algorithm.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,9 @@ void Message_Algorithm::SendStatusMessages (const Message_ExecStatus& theStatus,

// find message, prefixed by class type name, iterating by base classes if necessary
TCollection_AsciiString aMsgName;
for (Handle(Standard_Type) aType = DynamicType(); ! aType.IsNull(); aType = aType->Parent())
{
Handle(Standard_Type) aType = DynamicType();
aMsgName = aType->Name();
aMsgName += aSuffix;
if (Message_MsgFile::HasMsg(aMsgName))
break;
}

// create a message
Message_Msg aMsg ( aMsgName );
Expand Down
1 change: 1 addition & 0 deletions src/OpenGl/OpenGl_Element.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define OpenGl_Element_HeaderFile

#include <Standard_Type.hxx>
#include <Standard_Handle.hxx>

class Graphic3d_FrameStatsDataTmp;
class OpenGl_Workspace;
Expand Down
1 change: 1 addition & 0 deletions src/OpenGl/OpenGl_Resource.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define OpenGl_Resource_HeaderFile

#include <Standard_Type.hxx>
#include <Standard_Transient.hxx>

class OpenGl_Context;

Expand Down
3 changes: 1 addition & 2 deletions src/STEPSelections/STEPSelections_SelectDerived.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ Standard_Boolean STEPSelections_SelectDerived::Matches(const Handle(Standard_Tra

Standard_Boolean plex = module->IsComplex(CN);
if (!plex) {
DeclareAndCast(Standard_Type,atype,ent);
if (atype.IsNull()) atype = ent->DynamicType();
Handle(Standard_Type) atype = ent->DynamicType();
return atype->SubType(checker);
} else {
TColStd_SequenceOfAsciiString list;
Expand Down
1 change: 1 addition & 0 deletions src/SelectMgr/SelectMgr.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define _SelectMgr_HeaderFile

#include <Standard_Type.hxx>
#include <Standard_Handle.hxx>

class Graphic3d_Structure;
class Graphic3d_TransformPers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ Standard_Boolean ShapeConstruct_ProjectCurveOnSurface::PerformByProjLib(Handle(G
Handle(Geom_Curve) cIso;
Standard_Real t1, t2;

Handle(Standard_Type) sType = mySurf->Surface()->DynamicType();
const Handle(Standard_Type)& sType = mySurf->Surface()->DynamicType();
Standard_Boolean isAnalytic = Standard_True;
if (sType == STANDARD_TYPE(Geom_BezierSurface) || sType == STANDARD_TYPE(Geom_BSplineSurface)) isAnalytic = Standard_False;
Standard_Real uf, ul, vf, vl;
Expand Down
2 changes: 2 additions & 0 deletions src/Standard/Standard_ArrayStreamBuffer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include <Standard_ArrayStreamBuffer.hxx>

#include <cstring>

// =======================================================================
// function : Standard_ArrayStreamBuffer
// purpose :
Expand Down
2 changes: 1 addition & 1 deletion src/Standard/Standard_DefineHandle.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
//! Since OCCT 7.0, relevant macros are provided by Standard_Type.hxx and Standard_Handle.hxx.

#include <Standard_Type.hxx>
#include <Standard_Handle.hxx>

class Standard_Transient;
class Standard_Persistent;
class Standard_Type;

// Obsolete macros kept for compatibility
#define IMPLEMENT_DOWNCAST(C1,BC)
Expand Down
1 change: 1 addition & 0 deletions src/Standard/Standard_Persistent.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define _Standard_Persistent_HeaderFile

#include <Standard_Type.hxx>
#include <Standard_Transient.hxx>

//! Root of "persistent" classes, a legacy support of
//! object oriented databases, now outdated.
Expand Down
6 changes: 4 additions & 2 deletions src/Standard/Standard_Transient.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
#include <Standard_CString.hxx>
#include <Standard_ProgramError.hxx>

const Handle(Standard_Type)& Standard_Transient::get_type_descriptor ()
const Handle(Standard_Type)& Standard_Transient::get_type_descriptor()
{
return opencascade::type_instance<Standard_Transient>::get();
static const opencascade::handle<Standard_Type> THE_TYPE_INSTANCE(new Standard_Type(typeid(Standard_Transient), get_type_name(),
sizeof(Standard_Transient), nullptr));
return THE_TYPE_INSTANCE;
}

//
Expand Down
2 changes: 2 additions & 0 deletions src/Standard/Standard_Transient.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,6 @@ private:
//! Definition of Handle_Standard_Transient as typedef for compatibility
typedef opencascade::handle<Standard_Transient> Handle_Standard_Transient;

#include <Standard_Handle.hxx>

#endif
97 changes: 22 additions & 75 deletions src/Standard/Standard_Type.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 1998-1999 Matra Datavision
// Copyright (c) 1999-2014 OPEN CASCADE SAS
// Copyright (c) 1999-2024 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
Expand All @@ -12,40 +12,37 @@
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.


#include <Standard_Type.hxx>
#include <Standard_Mutex.hxx>
#include <Standard_Assert.hxx>

#include <unordered_map>

IMPLEMENT_STANDARD_RTTIEXT(Standard_Type,Standard_Transient)

//============================================================================

Standard_Type::Standard_Type (const std::type_info& theInfo,
const char* theName,
Standard_Size theSize,
const Handle(Standard_Type)& theParent) :
myInfo(theInfo),
myName(theName),
mySize(theSize),
myParent(theParent)
{
}

//============================================================================

Standard_Boolean Standard_Type::SubType (const Handle(Standard_Type)& theOther) const
{
return ! theOther.IsNull() && (theOther == this || (! myParent.IsNull() && myParent->SubType (theOther)));
const Standard_Type* aTypeIter = this;
while (aTypeIter != nullptr)
{
if (*theOther == *aTypeIter)
{
return true;
}
aTypeIter = aTypeIter->Parent();
}
return false;
}

//============================================================================

Standard_Boolean Standard_Type::SubType (const Standard_CString theName) const
{
return theName != 0 && (IsEqual (myName, theName) || (! myParent.IsNull() && myParent->SubType (theName)));
const Standard_Type* aTypeIter = this;
while (aTypeIter != nullptr)
{
if (IsEqual(theName, aTypeIter->Name()))
{
return true;
}
aTypeIter = aTypeIter->Parent();
}
return false;
}

// ------------------------------------------------------------------
Expand All @@ -54,54 +51,4 @@ Standard_Boolean Standard_Type::SubType (const Standard_CString theName) const
void Standard_Type::Print (Standard_OStream& AStream) const
{
AStream << std::hex << (Standard_Address)this << " : " << std::dec << myName ;
}

//============================================================================
// Registry of types
//============================================================================

namespace {
// Map of string to type
typedef std::unordered_map<std::type_index, Standard_Type*> registry_type;

// Registry is made static in the function to ensure that it gets
// initialized by the time of first access
registry_type& GetRegistry()
{
static registry_type theRegistry;
return theRegistry;
}

// To initialize theRegistry map as soon as possible to be destroyed the latest
Handle(Standard_Type) theType = STANDARD_TYPE(Standard_Transient);
}

Standard_Type* Standard_Type::Register (const std::type_info& theInfo, const char* theName,
Standard_Size theSize, const Handle(Standard_Type)& theParent)
{
// Access to registry is protected by mutex; it should not happen often because
// instances are cached by Standard_Type::Instance() (one per binary module)
static Standard_Mutex theMutex;
Standard_Mutex::Sentry aSentry (theMutex);

// return existing descriptor if already in the registry
registry_type& aRegistry = GetRegistry();
Standard_Type* aType = 0;
auto anIter = aRegistry.find(theInfo);
if (anIter != aRegistry.end())
return anIter->second;

// else create a new descriptor
aType = new Standard_Type (theInfo, theName, theSize, theParent);

// then add it to registry and return (the reference to the handle stored in the registry)
aRegistry.emplace(theInfo, aType);
return aType;
}

Standard_Type::~Standard_Type ()
{
// remove descriptor from the registry
registry_type& aRegistry = GetRegistry();
Standard_ASSERT(aRegistry.erase(myInfo) > 0, "Standard_Type::~Standard_Type() cannot find itself in registry",);
}
}
Loading

0 comments on commit ad3f42d

Please sign in to comment.