b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
7fd59977 |
15 | |
69ff08ff |
16 | #include <Standard_Type.hxx> |
17 | #include <Standard_Mutex.hxx> |
18 | #include <Standard_Assert.hxx> |
7fd59977 |
19 | |
69ff08ff |
20 | #include <NCollection_DataMap.hxx> |
7fd59977 |
21 | |
f5f4ebd0 |
22 | IMPLEMENT_STANDARD_RTTIEXT(Standard_Type,Standard_Transient) |
23 | |
7fd59977 |
24 | //============================================================================ |
25 | |
69ff08ff |
26 | Standard_Boolean Standard_Type::SubType (const Handle(Standard_Type)& theOther) const |
7fd59977 |
27 | { |
69ff08ff |
28 | return ! theOther.IsNull() && (theOther == this || (! myParent.IsNull() && myParent->SubType (theOther))); |
7fd59977 |
29 | } |
30 | |
7fd59977 |
31 | //============================================================================ |
32 | |
69ff08ff |
33 | Standard_Boolean Standard_Type::SubType (const Standard_CString theName) const |
7fd59977 |
34 | { |
42a9dcfc |
35 | return theName != 0 && (IsEqual (myName, theName) || (! myParent.IsNull() && myParent->SubType (theName))); |
7fd59977 |
36 | } |
37 | |
69ff08ff |
38 | // ------------------------------------------------------------------ |
39 | // Print (me; s: in out OStream) returns OStream; |
40 | // ------------------------------------------------------------------ |
41 | void Standard_Type::Print (Standard_OStream& AStream) const |
7fd59977 |
42 | { |
69ff08ff |
43 | AStream << hex << (Standard_Address)this << " : " << dec << myName ; |
7fd59977 |
44 | } |
45 | |
46 | //============================================================================ |
69ff08ff |
47 | // Registry of types |
7fd59977 |
48 | //============================================================================ |
7fd59977 |
49 | |
69ff08ff |
50 | namespace { |
51 | // Value-based hasher for plain C string (char*) |
52 | struct CStringHasher |
53 | { |
54 | static Standard_Integer HashCode (const Standard_CString& theKey, const Standard_Integer Upper) |
55 | { |
56 | return ::HashCode (theKey, Upper); |
57 | } |
58 | static bool IsEqual (const Standard_CString& theKey1, const Standard_CString& theKey2) |
59 | { |
60 | return ! strcmp (theKey1, theKey2); |
61 | } |
62 | }; |
7fd59977 |
63 | |
69ff08ff |
64 | // Map of string to type |
65 | typedef NCollection_DataMap<Standard_CString, Standard_Type*, CStringHasher> registry_type; |
7fd59977 |
66 | |
69ff08ff |
67 | // Registry is made static in the function to ensure that it gets |
68 | // initialized by the time of first access |
69 | registry_type& GetRegistry() |
70 | { |
71 | static registry_type theRegistry; |
72 | return theRegistry; |
73 | } |
7fd59977 |
74 | } |
75 | |
e7195ab4 |
76 | Standard_Type* Standard_Type::Register (const char* theSystemName, const char* theName, |
77 | Standard_Size theSize, const Handle(Standard_Type)& theParent) |
7fd59977 |
78 | { |
69ff08ff |
79 | // Access to registry is protected by mutex; it should not happen often because |
80 | // instances are cached by Standard_Type::Instance() (one per binary module) |
81 | static Standard_Mutex theMutex; |
82 | Standard_Mutex::Sentry aSentry (theMutex); |
7fd59977 |
83 | |
69ff08ff |
84 | // return existing descriptor if already in the registry |
85 | registry_type& aRegistry = GetRegistry(); |
86 | Standard_Type* aType = 0; |
87 | if (aRegistry.Find (theSystemName, aType)) |
88 | return aType; |
7fd59977 |
89 | |
69ff08ff |
90 | // else create a new descriptor |
91 | aType = new Standard_Type (theSystemName, theName, theSize, theParent); |
7fd59977 |
92 | |
69ff08ff |
93 | // then add it to registry and return (the reference to the handle stored in the registry) |
94 | aRegistry.Bind (theSystemName, aType); |
7fd59977 |
95 | |
69ff08ff |
96 | // cout << "Registering " << theSystemName << ": " << aRegistry.Extent() << endl; |
7fd59977 |
97 | |
69ff08ff |
98 | return aType; |
7fd59977 |
99 | } |
100 | |
69ff08ff |
101 | Standard_Type::~Standard_Type () |
7fd59977 |
102 | { |
69ff08ff |
103 | // remove descriptor from the registry |
104 | registry_type& aRegistry = GetRegistry(); |
105 | Standard_ASSERT(aRegistry.UnBind (mySystemName), "Standard_Type::~Standard_Type() cannot find itself in registry",); |
7fd59977 |
106 | |
69ff08ff |
107 | // cout << "Unregistering " << mySystemName << ": " << aRegistry.Extent() << endl; |
7fd59977 |
108 | } |