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_LayerRegistry_HeaderFile
0015 #define _BRepGraph_LayerRegistry_HeaderFile
0016 
0017 #include <BRepGraph.hxx>
0018 #include <BRepGraph_Layer.hxx>
0019 #include <BRepGraph_RefId.hxx>
0020 #include <NCollection_DataMap.hxx>
0021 #include <NCollection_FlatDataMap.hxx>
0022 #include <NCollection_LinearVector.hxx>
0023 #include <Standard_DefineAlloc.hxx>
0024 #include <Standard_GUID.hxx>
0025 
0026 #include <functional>
0027 #include <mutex>
0028 #include <shared_mutex>
0029 #include <atomic>
0030 
0031 //! @brief Dense GUID-keyed runtime registry of graph layers.
0032 //!
0033 //! Stores registered layers in a compact vector for O(1) slot access and a
0034 //! GUID-to-slot map for O(1) lookup by stable public identity.
0035 class BRepGraph_LayerRegistry
0036 {
0037 public:
0038   DEFINE_STANDARD_ALLOC
0039 
0040   Standard_EXPORT BRepGraph_LayerRegistry();
0041 
0042   BRepGraph_LayerRegistry(const BRepGraph_LayerRegistry&)            = delete;
0043   BRepGraph_LayerRegistry& operator=(const BRepGraph_LayerRegistry&) = delete;
0044 
0045   Standard_EXPORT BRepGraph_LayerRegistry(BRepGraph_LayerRegistry&& theOther) noexcept;
0046   Standard_EXPORT BRepGraph_LayerRegistry& operator=(BRepGraph_LayerRegistry&& theOther) noexcept;
0047 
0048   //! Register a layer. Replaces an existing layer with the same GUID.
0049   //! @return slot index in the internal dense vector.
0050   Standard_EXPORT uint32_t RegisterLayer(const occ::handle<BRepGraph_Layer>& theLayer);
0051 
0052   //! Remove a layer by GUID.
0053   Standard_EXPORT void UnregisterLayer(const Standard_GUID& theGUID);
0054 
0055   //! Find a layer by GUID. Returns null handle if not found.
0056   [[nodiscard]] Standard_EXPORT occ::handle<BRepGraph_Layer> FindLayer(
0057     const Standard_GUID& theGUID) const;
0058 
0059   //! Typed convenience lookup by layer GUID.
0060   template <typename T>
0061   [[nodiscard]] occ::handle<T> FindLayer() const
0062   {
0063     return Find<T>();
0064   }
0065 
0066   //! Typed lookup by layer GUID.
0067   template <typename T>
0068   [[nodiscard]] occ::handle<T> Find() const
0069   {
0070     return occ::down_cast<T>(FindLayer(T::GetID()));
0071   }
0072 
0073   //! Return an existing layer or create and register a default one.
0074   //! Template convenience wrapper: extracts GUID and calls ensureLayer.
0075   template <typename T>
0076   [[nodiscard]] occ::handle<T> Ensure()
0077   {
0078     return occ::down_cast<T>(
0079       ensureLayer(T::GetID(), []() -> occ::handle<BRepGraph_Layer> { return new T(); }));
0080   }
0081 
0082   //! Return current slot for a GUID.
0083   [[nodiscard]] Standard_EXPORT bool FindSlot(const Standard_GUID& theGUID,
0084                                               uint32_t&            theSlot) const;
0085 
0086   //! Return layer by slot index, or null handle if the slot is out of range.
0087   [[nodiscard]] Standard_EXPORT occ::handle<BRepGraph_Layer> Layer(uint32_t theSlot) const;
0088 
0089   //! Number of registered layers.
0090   [[nodiscard]] uint32_t NbLayers() const
0091   {
0092     std::shared_lock<std::shared_mutex> aLock(myMutex);
0093     return static_cast<uint32_t>(myLayers.Size());
0094   }
0095 
0096   //! True if any registered layer subscribes to node modification events.
0097   [[nodiscard]] bool HasModificationSubscribers() const
0098   {
0099     return mySubscribedKindsMask.load(std::memory_order_acquire) != 0;
0100   }
0101 
0102   //! Bitwise OR of all registered layer node subscription masks.
0103   [[nodiscard]] int SubscribedKindsMask() const
0104   {
0105     return static_cast<int>(mySubscribedKindsMask.load(std::memory_order_acquire));
0106   }
0107 
0108   //! Dispatch OnNodeRemoved to all registered layers.
0109   Standard_EXPORT void DispatchOnNodeRemoved(const BRepGraph_NodeId theNode) noexcept;
0110 
0111   //! Dispatch generic item removal to all registered layers.
0112   Standard_EXPORT void DispatchOnItemRemoved(const BRepGraph_ItemId theItem) noexcept;
0113 
0114   //! Dispatch OnNodeReplaced to all registered layers.
0115   Standard_EXPORT void DispatchOnNodeReplaced(const BRepGraph_NodeId theOldNode,
0116                                               const BRepGraph_NodeId theNewNode) noexcept;
0117 
0118   //! Dispatch OnNodeModified to subscribed layers.
0119   Standard_EXPORT void DispatchNodeModified(const BRepGraph_NodeId theNode) noexcept;
0120 
0121   //! Dispatch generic item modification through the matching typed subscription path.
0122   Standard_EXPORT void DispatchItemModified(const BRepGraph_ItemId theItem) noexcept;
0123 
0124   //! Dispatch OnNodesModified to subscribed layers.
0125   Standard_EXPORT void DispatchNodesModified(
0126     const NCollection_Array1<BRepGraph_NodeId>& theModifiedNodes,
0127     const int                                   theModifiedKindsMask) noexcept;
0128 
0129   //! Ask every registered source layer to copy itself into the target graph.
0130   //! For Mode::Compact, layers are unregistered first and CopyTo creates fresh instances.
0131   //! @param[in] theTargetGraph target graph to receive layer data
0132   //! @param[in] theItemRemap   source -> target item id mapping
0133   //! @param[in] theMode        Copy or Compact semantics
0134   Standard_EXPORT void CopyLayersTo(
0135     BRepGraph&                                                         theTargetGraph,
0136     const NCollection_FlatDataMap<BRepGraph_ItemId, BRepGraph_ItemId>& theItemRemap,
0137     const BRepGraph_CopyRemap::Mode                                    theMode) const;
0138 
0139   //! Ask every registered source layer to copy itself using identity mapping.
0140   //! Source item ids are the same as target item ids (full identity copy).
0141   //! @param[in] theTargetGraph target graph to receive layer data
0142   //! @param[in] theMappingKind identity or explicit mapping
0143   //! @param[in] theMode        Copy or Compact semantics
0144   Standard_EXPORT void CopyLayersTo(BRepGraph&                       theTargetGraph,
0145                                     BRepGraph_CopyRemap::MappingKind theMappingKind,
0146                                     BRepGraph_CopyRemap::Mode        theMode) const;
0147 
0148   //! True if any registered layer subscribes to reference modification events.
0149   [[nodiscard]] bool HasRefModificationSubscribers() const
0150   {
0151     return mySubscribedRefKindsMask.load(std::memory_order_acquire) != 0;
0152   }
0153 
0154   //! Bitwise OR of all registered layer reference subscription masks.
0155   [[nodiscard]] int SubscribedRefKindsMask() const
0156   {
0157     return static_cast<int>(mySubscribedRefKindsMask.load(std::memory_order_acquire));
0158   }
0159 
0160   //! Dispatch OnRefRemoved to all registered layers (unconditional - not filtered).
0161   Standard_EXPORT void DispatchOnRefRemoved(const BRepGraph_RefId theRef) noexcept;
0162 
0163   //! Dispatch OnRefModified to subscribed layers (immediate mode).
0164   Standard_EXPORT void DispatchRefModified(const BRepGraph_RefId theRef) noexcept;
0165 
0166   //! Dispatch OnRefsModified to subscribed layers (deferred/batch mode).
0167   Standard_EXPORT void DispatchRefsModified(
0168     const NCollection_Array1<BRepGraph_RefId>& theModifiedRefs,
0169     const int                                  theModifiedRefKindsMask) noexcept;
0170 
0171   //! Clear all registered layer data without unregistering services.
0172   Standard_EXPORT void ClearAll() noexcept;
0173 
0174   //! Invalidate all registered layer data.
0175   Standard_EXPORT void InvalidateAll() noexcept;
0176 
0177 private:
0178   friend class ::BRepGraph;
0179   friend struct ::BRepGraph_Data;
0180 
0181   //! Attach this registry to graph owner. Propagates context to registered layers.
0182   Standard_EXPORT void Attach(BRepGraph* theGraph) noexcept;
0183 
0184   //! Clear the graph data binding.
0185   Standard_EXPORT void Detach() noexcept;
0186 
0187   [[nodiscard]] Standard_EXPORT occ::handle<BRepGraph_Layer> findLayerLocked(
0188     const Standard_GUID& theGUID) const;
0189 
0190   //! Return an existing layer or create and register a default one.
0191   //! Uses double-checked locking: shared lock for fast path (layer exists),
0192   //! exclusive lock only for creation (rare, first-call only).
0193   [[nodiscard]] Standard_EXPORT occ::handle<BRepGraph_Layer> ensureLayer(
0194     const Standard_GUID&                                 theGUID,
0195     const std::function<occ::handle<BRepGraph_Layer>()>& theFactory);
0196 
0197   [[nodiscard]] Standard_EXPORT occ::handle<BRepGraph_Layer> layerAt(uint32_t theSlot) const;
0198 
0199   Standard_EXPORT uint32_t registerLayerLocked(const occ::handle<BRepGraph_Layer>& theLayer);
0200 
0201   Standard_EXPORT void recomputeSubscribedKindsMask();
0202   Standard_EXPORT void detachAllLocked() noexcept;
0203 
0204 private:
0205   NCollection_LinearVector<occ::handle<BRepGraph_Layer>> myLayers;
0206   NCollection_DataMap<Standard_GUID, uint32_t>           myGuidToSlot;
0207   std::atomic<uint32_t>                                  mySubscribedKindsMask{0};
0208   std::atomic<uint32_t>                                  mySubscribedRefKindsMask{0};
0209   BRepGraph*                                             myGraph = nullptr;
0210   mutable std::shared_mutex                              myMutex;
0211 };
0212 
0213 #endif // _BRepGraph_LayerRegistry_HeaderFile