Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:19:35

0001 // Copyright (c) 2026 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _BRepGraph_LayerLock_HeaderFile
0015 #define _BRepGraph_LayerLock_HeaderFile
0016 
0017 #include <BRepGraph_ItemId.hxx>
0018 #include <BRepGraph_Layer.hxx>
0019 #include <NCollection_FlatDataMap.hxx>
0020 #include <TCollection_AsciiString.hxx>
0021 
0022 //! Owner metadata layer for owned BRepGraph items.
0023 //!
0024 //! Uses a root-based ownership model: only the highest owned item per group
0025 //! is stored in the map. All descendants receive the fast IsOwned bit-flag
0026 //! via automatic downward propagation. Owner lookup traverses upward to
0027 //! find the root entry.
0028 //!
0029 //! Overlapping roots are forbidden: SetOwner rejects if the item is already
0030 //! covered by an ancestor root with a different GUID.
0031 //!
0032 //! HasOwner() checks the IsOwned bit-flag (O(1)).
0033 //! FindOwnerId() traverses upward to find the root entry (O(depth)).
0034 class BRepGraph_LayerLock : public BRepGraph_Layer
0035 {
0036 public:
0037   //! Scoped permission for an owner layer to edit one item it owns.
0038   //!
0039   //! The scope traverses upward to find the root owner for GUID verification,
0040   //! then temporarily clears the fast owned bit on the specific item so existing
0041   //! editor mutation APIs can be reused by the owning layer.
0042   class ScopedOwnerEdit
0043   {
0044   public:
0045     Standard_EXPORT ScopedOwnerEdit(BRepGraph_LayerLock&   theLayer,
0046                                     const BRepGraph_ItemId theItem,
0047                                     const Standard_GUID&   theOwnerId);
0048     Standard_EXPORT ~ScopedOwnerEdit();
0049 
0050     ScopedOwnerEdit(const ScopedOwnerEdit&)            = delete;
0051     ScopedOwnerEdit& operator=(const ScopedOwnerEdit&) = delete;
0052 
0053     Standard_EXPORT                  ScopedOwnerEdit(ScopedOwnerEdit&& theOther) noexcept;
0054     Standard_EXPORT ScopedOwnerEdit& operator=(ScopedOwnerEdit&& theOther) noexcept;
0055 
0056   private:
0057     BRepGraph_LayerLock* myLayer = nullptr;
0058     BRepGraph_ItemId     myItem;
0059     bool                 myIsActive = false;
0060   };
0061 
0062   //! Create lock-owner storage.
0063   Standard_EXPORT BRepGraph_LayerLock();
0064 
0065   //! Return fixed layer type GUID.
0066   [[nodiscard]] Standard_EXPORT static const Standard_GUID& GetID();
0067 
0068   //! Return this layer type GUID.
0069   [[nodiscard]] Standard_EXPORT const Standard_GUID& ID() const override;
0070 
0071   //! Return owner ID for an item.
0072   //! Traverses upward for nodes/refs to find the root owner entry.
0073   //! @return true when the item has a resolved owner and @p theOwnerId was filled.
0074   [[nodiscard]] Standard_EXPORT bool FindOwnerId(const BRepGraph_ItemId theItem,
0075                                                  Standard_GUID&         theOwnerId) const;
0076 
0077   //! Return owner ID for a node.
0078   [[nodiscard]] bool FindOwnerId(const BRepGraph_NodeId theNode, Standard_GUID& theOwnerId) const
0079   {
0080     return FindOwnerId(BRepGraph_ItemId(theNode), theOwnerId);
0081   }
0082 
0083   //! Return owner ID for a reference.
0084   [[nodiscard]] bool FindOwnerId(const BRepGraph_RefId theRef, Standard_GUID& theOwnerId) const
0085   {
0086     return FindOwnerId(BRepGraph_ItemId(theRef), theOwnerId);
0087   }
0088 
0089   //! Return true if an item's IsOwned bit-flag is set.
0090   //! This is an O(1) check. Use FindOwnerId() to resolve the actual owner GUID.
0091   [[nodiscard]] Standard_EXPORT bool HasOwner(const BRepGraph_ItemId theItem) const;
0092 
0093   //! Return true if a node's IsOwned bit-flag is set.
0094   [[nodiscard]] bool HasOwner(const BRepGraph_NodeId theNode) const
0095   {
0096     return HasOwner(BRepGraph_ItemId(theNode));
0097   }
0098 
0099   //! Return true if a reference's IsOwned bit-flag is set.
0100   [[nodiscard]] bool HasOwner(const BRepGraph_RefId theRef) const
0101   {
0102     return HasOwner(BRepGraph_ItemId(theRef));
0103   }
0104 
0105   //! Register an owner ID and set the graph item's ownership flag.
0106   //! For nodes, propagates the IsOwned bit-flag to all descendants.
0107   //! Rejects if the item is already covered by an ancestor root with a different GUID.
0108   Standard_EXPORT void SetOwner(const BRepGraph_ItemId theItem, const Standard_GUID& theOwnerId);
0109 
0110   //! Register an owner ID and set the graph item's ownership flag.
0111   //! Returns true when owner storage changed. Revision update can be deferred by bulk callers.
0112   Standard_EXPORT bool SetOwner(const BRepGraph_ItemId theItem,
0113                                 const Standard_GUID&   theOwnerId,
0114                                 const bool             theToUpdateRevision);
0115 
0116   //! Register an owner ID and set the node ownership flag.
0117   void SetOwner(const BRepGraph_NodeId theNode, const Standard_GUID& theOwnerId)
0118   {
0119     SetOwner(BRepGraph_ItemId(theNode), theOwnerId);
0120   }
0121 
0122   //! Register an owner ID and set the reference ownership flag.
0123   void SetOwner(const BRepGraph_RefId theRef, const Standard_GUID& theOwnerId)
0124   {
0125     SetOwner(BRepGraph_ItemId(theRef), theOwnerId);
0126   }
0127 
0128   //! Remove an owner and clear the graph item's ownership flag.
0129   //! For node roots, clears the IsOwned bit-flag on all descendants.
0130   Standard_EXPORT void UnsetOwner(const BRepGraph_ItemId theItem);
0131 
0132   //! Remove an owner and clear the graph item's ownership flag if owner ID matches.
0133   Standard_EXPORT void UnsetOwner(const BRepGraph_ItemId theItem, const Standard_GUID& theOwnerId);
0134 
0135   //! Remove an owner and clear the node ownership flag.
0136   void UnsetOwner(const BRepGraph_NodeId theNode) { UnsetOwner(BRepGraph_ItemId(theNode)); }
0137 
0138   //! Remove an owner and clear the reference ownership flag.
0139   void UnsetOwner(const BRepGraph_RefId theRef) { UnsetOwner(BRepGraph_ItemId(theRef)); }
0140 
0141   //! Return true if at least one root entry exists.
0142   [[nodiscard]] bool HasOwners() const
0143   {
0144     return myNodeOwners.Extent() != 0 || myRefOwners.Extent() != 0;
0145   }
0146 
0147   //! Reserve owner map buckets for bulk registration.
0148   Standard_EXPORT void ReserveOwners(const size_t theNbOwners);
0149 
0150   //! Mark owner metadata changed after a bulk update.
0151   Standard_EXPORT void TouchOwners();
0152 
0153   Standard_EXPORT const TCollection_AsciiString& Name() const override;
0154   Standard_EXPORT void OnNodeRemoved(const BRepGraph_NodeId theNode) noexcept override;
0155   Standard_EXPORT void OnNodeReplaced(const BRepGraph_NodeId theOldNode,
0156                                       const BRepGraph_NodeId theNewNode) noexcept override;
0157   Standard_EXPORT void CopyTo(const BRepGraph_CopyRemap& theCopy) const override;
0158   Standard_EXPORT void OnRefRemoved(const BRepGraph_RefId theRef) noexcept override;
0159   Standard_EXPORT void InvalidateAll() noexcept override;
0160   Standard_EXPORT void Clear() noexcept override;
0161 
0162   DEFINE_STANDARD_RTTIEXT(BRepGraph_LayerLock, BRepGraph_Layer)
0163 
0164 private:
0165   //! Set or clear the IsOwned bit-flag on a single item.
0166   Standard_EXPORT void setItemOwned(const BRepGraph_ItemId theItem, const bool theIsOwned) const;
0167 
0168   //! Propagate the IsOwned bit-flag to all descendants of a root node.
0169   //! Walks nodes via ChildExplorer, refs via CurrentRef(), and reps via definition fields.
0170   void expandOwnership(const BRepGraph_NodeId theRoot, const bool theIsOwned);
0171 
0172   //! Rebuild fast IsOwned bit-flags from remaining root maps.
0173   void rebuildOwnedFlagsFromRoots();
0174 
0175   //! Find the root node entry that covers a given node.
0176   //! Checks direct map entry first, then traverses upward via ParentExplorer.
0177   [[nodiscard]] bool findRootNodeId(const BRepGraph_NodeId theNode,
0178                                     BRepGraph_ItemId&      theRootItem) const;
0179 
0180   //! Find the root entry that covers a given ref.
0181   //! Checks direct map entry first, then traverses via parent node.
0182   [[nodiscard]] bool findRootRefId(const BRepGraph_RefId theRef,
0183                                    BRepGraph_ItemId&     theRootItem) const;
0184 
0185   //! Get the parent node of a ref from its storage struct.
0186   [[nodiscard]] BRepGraph_NodeId parentNodeId(const BRepGraph_RefId theRef) const;
0187 
0188   //! Check if an item is already covered by an ancestor root.
0189   //! Returns true if an ancestor root exists. If so, fills theRootOwnerId.
0190   [[nodiscard]] bool isCoveredByAncestor(const BRepGraph_ItemId theItem,
0191                                          Standard_GUID&         theRootOwnerId) const;
0192 
0193   //! Remove a root entry from the appropriate map and clear its bit-flag.
0194   void removeRootEntry(const BRepGraph_ItemId theItem) noexcept;
0195 
0196   NCollection_FlatDataMap<BRepGraph_ItemId, Standard_GUID> myNodeOwners;
0197   NCollection_FlatDataMap<BRepGraph_ItemId, Standard_GUID> myRefOwners;
0198 };
0199 
0200 #endif // _BRepGraph_LayerLock_HeaderFile