File indexing completed on 2026-09-28 09:19:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _BRepGraph_MutGuard_HeaderFile
0015 #define _BRepGraph_MutGuard_HeaderFile
0016
0017 #include <BRepGraph.hxx>
0018 #include <BRepGraphInc_Definition.hxx>
0019 #include <BRepGraphInc_Reference.hxx>
0020 #include <BRepGraphInc_Storage.hxx>
0021 #include <Standard_ProgramError.hxx>
0022
0023 #include <type_traits>
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 template <typename T>
0051 class BRepGraph_MutGuard
0052 {
0053 static_assert(std::is_base_of_v<BRepGraphInc::BaseDef, T>
0054 || std::is_base_of_v<BRepGraphInc::BaseRef, T>,
0055 "BRepGraph_MutGuard<T>: T must derive from BaseDef or BaseRef");
0056
0057 using TypeId = typename T::TypeId;
0058
0059
0060 void notify() noexcept
0061 {
0062 try
0063 {
0064 if constexpr (std::is_base_of_v<BRepGraphInc::BaseDef, T>)
0065 {
0066 myGraph->markModified(myId, *myEntity);
0067 }
0068 else if constexpr (std::is_base_of_v<BRepGraphInc::BaseRef, T>)
0069 {
0070 myGraph->markRefModified(myId);
0071 }
0072 }
0073 catch (...)
0074 {
0075 }
0076 }
0077
0078 public:
0079
0080
0081
0082
0083
0084
0085
0086 BRepGraph_MutGuard(BRepGraph& theGraph,
0087 BRepGraphInc_Storage& theStorage,
0088 T* theEntity,
0089 const TypeId theId)
0090 : myGraph(&theGraph),
0091 myStorage(&theStorage),
0092 myEntity(theEntity),
0093 myId(theId),
0094 myDirty(false)
0095 {
0096 Standard_ProgramError_Raise_if(myStorage->IsGuarded(BRepGraph_ItemId(myId)),
0097 "BRepGraph_MutGuard: guard already active on this item");
0098 myStorage->SetGuarded(BRepGraph_ItemId(myId));
0099 }
0100
0101
0102
0103
0104
0105 ~BRepGraph_MutGuard()
0106 {
0107 if (myEntity != nullptr)
0108 {
0109 myStorage->ClearGuarded(BRepGraph_ItemId(myId));
0110 if (myDirty)
0111 {
0112 notify();
0113 }
0114 }
0115 }
0116
0117
0118
0119 BRepGraph_MutGuard(BRepGraph_MutGuard&& theOther) noexcept
0120 : myGraph(theOther.myGraph),
0121 myStorage(theOther.myStorage),
0122 myEntity(theOther.myEntity),
0123 myId(theOther.myId),
0124 myDirty(theOther.myDirty)
0125 {
0126 theOther.myEntity = nullptr;
0127 theOther.myDirty = false;
0128 theOther.myGraph = nullptr;
0129 theOther.myStorage = nullptr;
0130 }
0131
0132
0133
0134 BRepGraph_MutGuard& operator=(BRepGraph_MutGuard&& theOther) noexcept
0135 {
0136 if (this != &theOther)
0137 {
0138 if (myEntity != nullptr)
0139 {
0140 myStorage->ClearGuarded(BRepGraph_ItemId(myId));
0141 if (myDirty)
0142 {
0143 notify();
0144 }
0145 }
0146 myGraph = theOther.myGraph;
0147 myStorage = theOther.myStorage;
0148 myEntity = theOther.myEntity;
0149 myId = theOther.myId;
0150 myDirty = theOther.myDirty;
0151 theOther.myEntity = nullptr;
0152 theOther.myDirty = false;
0153 theOther.myGraph = nullptr;
0154 theOther.myStorage = nullptr;
0155 }
0156 return *this;
0157 }
0158
0159 BRepGraph_MutGuard(const BRepGraph_MutGuard&) = delete;
0160 BRepGraph_MutGuard& operator=(const BRepGraph_MutGuard&) = delete;
0161
0162
0163
0164 [[nodiscard]] explicit operator bool() const noexcept { return myEntity != nullptr; }
0165
0166
0167
0168 [[nodiscard]] const T* operator->() const
0169 {
0170 Standard_ProgramError_Raise_if(
0171 myEntity == nullptr,
0172 "BRepGraph_MutGuard::operator->(): guard is empty or moved-from");
0173 return myEntity;
0174 }
0175
0176
0177 [[nodiscard]] const T& operator*() const
0178 {
0179 Standard_ProgramError_Raise_if(myEntity == nullptr,
0180 "BRepGraph_MutGuard::operator*(): guard is empty or moved-from");
0181 return *myEntity;
0182 }
0183
0184
0185 [[nodiscard]] TypeId Id() const noexcept { return myId; }
0186
0187
0188 [[nodiscard]] BRepGraph& Graph() const { return *myGraph; }
0189
0190
0191
0192
0193 void MarkDirty() noexcept { myDirty = true; }
0194
0195
0196 [[nodiscard]] bool IsDirty() const noexcept { return myDirty; }
0197
0198
0199
0200 [[nodiscard]] T& Internal() noexcept
0201 {
0202 myDirty = true;
0203 return *myEntity;
0204 }
0205
0206 private:
0207 BRepGraph* myGraph;
0208 BRepGraphInc_Storage* myStorage;
0209 T* myEntity;
0210 TypeId myId;
0211 bool myDirty;
0212 };
0213
0214 #endif