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_LayerDeferred_HeaderFile
0015 #define _BRepGraph_LayerDeferred_HeaderFile
0016 
0017 #include <BRepGraph_ItemId.hxx>
0018 #include <BRepGraph_Layer.hxx>
0019 #include <NCollection_DataMap.hxx>
0020 #include <Standard_Assert.hxx>
0021 #include <TCollection_AsciiString.hxx>
0022 
0023 #include <limits>
0024 #include <new>
0025 #include <type_traits>
0026 #include <utility>
0027 
0028 //! Base layer for postponed graph item loading.
0029 //!
0030 //! The layer stores provider-neutral deferred representation records and owns the lock
0031 //! state through BRepGraph_LayerLock. Format-specific loaders, such as ODE or
0032 //! STEP, should derive from this class or use the same representation contract rather
0033 //! than storing deferred ownership in topology definitions.
0034 class BRepGraph_LayerDeferred : public BRepGraph_Layer
0035 {
0036 public:
0037   //! Constructor for generic deferred layers.
0038   Standard_EXPORT BRepGraph_LayerDeferred();
0039 
0040   //! Representation category.
0041   enum class RepresentationKind
0042   {
0043     Unknown,
0044     Geometry,
0045     Mesh,
0046     Topology,
0047     Assembly,
0048     Parametric
0049   };
0050 
0051   //! One postponed representation attached to a graph item.
0052   struct Representation
0053   {
0054     static constexpr uint32_t THE_INVALID_SOURCE_INDEX = std::numeric_limits<uint32_t>::max();
0055 
0056     RepresentationKind      Kind = RepresentationKind::Unknown;
0057     uint32_t                Role = 0;
0058     TCollection_AsciiString Name;
0059     uint32_t                SourceIndex = THE_INVALID_SOURCE_INDEX;
0060   };
0061 
0062   //! Deferred ownership entry for one graph item.
0063   struct Entry
0064   {
0065     //! Small fixed representation list. Deferred graph items have a bounded number of
0066     //! persisted representations, so avoid one heap allocation per item.
0067     class RepresentationStorage
0068     {
0069     public:
0070       static constexpr size_t THE_MAX_REPRESENTATIONS_PER_ITEM = 8;
0071 
0072       RepresentationStorage() = default;
0073 
0074       RepresentationStorage(const RepresentationStorage& theOther) { copyFrom(theOther); }
0075 
0076       RepresentationStorage(RepresentationStorage&& theOther) noexcept { moveFrom(theOther); }
0077 
0078       Standard_EXPORT RepresentationStorage& operator=(const RepresentationStorage& theOther);
0079       Standard_EXPORT RepresentationStorage& operator=(RepresentationStorage&& theOther) noexcept;
0080 
0081       ~RepresentationStorage() { Clear(); }
0082 
0083       [[nodiscard]] size_t Size() const { return mySize; }
0084 
0085       [[nodiscard]] bool IsEmpty() const { return mySize == 0; }
0086 
0087       [[nodiscard]] Standard_EXPORT bool ContainsKind(const RepresentationKind theKind) const;
0088 
0089       [[nodiscard]] const Representation& Value(const size_t theIndex) const
0090       {
0091         Standard_ASSERT_RAISE(theIndex < mySize, "Deferred representation index is out of range");
0092         return *representationPtr(theIndex);
0093       }
0094 
0095       [[nodiscard]] Representation& ChangeValue(const size_t theIndex)
0096       {
0097         Standard_ASSERT_RAISE(theIndex < mySize, "Deferred representation index is out of range");
0098         return *representationPtr(theIndex);
0099       }
0100 
0101       [[nodiscard]] const Representation& First() const
0102       {
0103         Standard_ASSERT_RAISE(mySize > 0, "Deferred representation list is empty");
0104         return *representationPtr(0);
0105       }
0106 
0107       Standard_EXPORT void Append(const Representation& theRepresentation);
0108 
0109       Standard_EXPORT void Clear();
0110 
0111     private:
0112       [[nodiscard]] const Representation* representationPtr(const size_t theIndex) const
0113       {
0114         return reinterpret_cast<const Representation*>(&myRepresentationStorage[theIndex]);
0115       }
0116 
0117       [[nodiscard]] Representation* representationPtr(const size_t theIndex)
0118       {
0119         return reinterpret_cast<Representation*>(&myRepresentationStorage[theIndex]);
0120       }
0121 
0122       Standard_EXPORT void copyFrom(const RepresentationStorage& theOther);
0123       Standard_EXPORT void moveFrom(RepresentationStorage& theOther);
0124 
0125       using RepresentationSlot =
0126         std::aligned_storage_t<sizeof(Representation), alignof(Representation)>;
0127 
0128       RepresentationSlot myRepresentationStorage[THE_MAX_REPRESENTATIONS_PER_ITEM];
0129       size_t             mySize = 0;
0130     };
0131 
0132     TCollection_AsciiString Provider;
0133     TCollection_AsciiString SourceKey;
0134     RepresentationStorage   Representations;
0135   };
0136 
0137   //! Return fixed layer type GUID.
0138   [[nodiscard]] Standard_EXPORT static const Standard_GUID& GetID();
0139 
0140   //! Return this layer type GUID.
0141   [[nodiscard]] Standard_EXPORT const Standard_GUID& ID() const override;
0142 
0143   //! Return deferred entry for an item, or null if none exists.
0144   [[nodiscard]] Standard_EXPORT const Entry* FindDeferred(const BRepGraph_ItemId theItem) const;
0145 
0146   //! Return deferred entry for a node, or null if none exists.
0147   [[nodiscard]] const Entry* FindDeferred(const BRepGraph_NodeId theNode) const
0148   {
0149     return FindDeferred(BRepGraph_ItemId(theNode));
0150   }
0151 
0152   //! Return deferred entry for a reference, or null if none exists.
0153   [[nodiscard]] const Entry* FindDeferred(const BRepGraph_RefId theRef) const
0154   {
0155     return FindDeferred(BRepGraph_ItemId(theRef));
0156   }
0157 
0158   //! Return true if an item has deferred representations.
0159   [[nodiscard]] Standard_EXPORT bool HasDeferred(const BRepGraph_ItemId theItem) const;
0160 
0161   //! Return true if a node has deferred representations.
0162   [[nodiscard]] bool HasDeferred(const BRepGraph_NodeId theNode) const
0163   {
0164     return HasDeferred(BRepGraph_ItemId(theNode));
0165   }
0166 
0167   //! Return true if a reference has deferred representations.
0168   [[nodiscard]] bool HasDeferred(const BRepGraph_RefId theRef) const
0169   {
0170     return HasDeferred(BRepGraph_ItemId(theRef));
0171   }
0172 
0173   //! Register one postponed representation and lock the item.
0174   Standard_EXPORT void RegisterDeferred(const BRepGraph_ItemId         theItem,
0175                                         const TCollection_AsciiString& theProvider,
0176                                         const TCollection_AsciiString& theSourceKey,
0177                                         const RepresentationKind       theRepresentationKind,
0178                                         const TCollection_AsciiString& theRepresentationName,
0179                                         const uint32_t                 theSourceIndex);
0180 
0181   //! Register postponed representations for one item and lock the item once.
0182   Standard_EXPORT void RegisterDeferredRepresentations(const BRepGraph_ItemId         theItem,
0183                                                        const TCollection_AsciiString& theProvider,
0184                                                        const TCollection_AsciiString& theSourceKey,
0185                                                        const Representation* theRepresentations,
0186                                                        const size_t          theNbRepresentations);
0187 
0188   //! Register postponed representations for a new item and lock it once.
0189   //!
0190   //! This is a trusted bulk-load fast path: the caller must ensure the item is valid,
0191   //! has no existing deferred entry, and `theRepresentations` contains no duplicates.
0192   Standard_EXPORT void RegisterDeferredRepresentationsDirect(
0193     const BRepGraph_ItemId         theItem,
0194     const TCollection_AsciiString& theProvider,
0195     const TCollection_AsciiString& theSourceKey,
0196     const Representation*          theRepresentations,
0197     const size_t                   theNbRepresentations);
0198 
0199   //! Register one postponed node representation and lock the node.
0200   void RegisterDeferred(const BRepGraph_NodeId         theNode,
0201                         const TCollection_AsciiString& theProvider,
0202                         const TCollection_AsciiString& theSourceKey,
0203                         const RepresentationKind       theRepresentationKind,
0204                         const TCollection_AsciiString& theRepresentationName,
0205                         const uint32_t                 theSourceIndex)
0206   {
0207     RegisterDeferred(BRepGraph_ItemId(theNode),
0208                      theProvider,
0209                      theSourceKey,
0210                      theRepresentationKind,
0211                      theRepresentationName,
0212                      theSourceIndex);
0213   }
0214 
0215   //! Register one postponed reference representation and lock the reference.
0216   void RegisterDeferred(const BRepGraph_RefId          theRef,
0217                         const TCollection_AsciiString& theProvider,
0218                         const TCollection_AsciiString& theSourceKey,
0219                         const RepresentationKind       theRepresentationKind,
0220                         const TCollection_AsciiString& theRepresentationName,
0221                         const uint32_t                 theSourceIndex)
0222   {
0223     RegisterDeferred(BRepGraph_ItemId(theRef),
0224                      theProvider,
0225                      theSourceKey,
0226                      theRepresentationKind,
0227                      theRepresentationName,
0228                      theSourceIndex);
0229   }
0230 
0231   //! Remove all deferred representations for an item and unlock it.
0232   Standard_EXPORT void UnregisterDeferred(const BRepGraph_ItemId theItem);
0233 
0234   //! Remove all deferred representations for a node and unlock it.
0235   void UnregisterDeferred(const BRepGraph_NodeId theNode)
0236   {
0237     UnregisterDeferred(BRepGraph_ItemId(theNode));
0238   }
0239 
0240   //! Remove all deferred representations for a reference and unlock it.
0241   void UnregisterDeferred(const BRepGraph_RefId theRef)
0242   {
0243     UnregisterDeferred(BRepGraph_ItemId(theRef));
0244   }
0245 
0246   //! Return true if at least one item has deferred representations.
0247   [[nodiscard]] bool HasDeferredItems() const { return myEntries.Extent() != 0; }
0248 
0249   //! Return first deferred entry with at least one representation of the requested kind, or null.
0250   [[nodiscard]] Standard_EXPORT const Entry* FindFirstDeferred(
0251     const RepresentationKind theKind,
0252     BRepGraph_ItemId*        theItem = nullptr) const;
0253 
0254   //! Reserve deferred and lock layer buckets for bulk registration.
0255   Standard_EXPORT void ReserveDeferredItems(const size_t theNbItems);
0256 
0257   //! Begin bulk deferred registration. Revision updates are postponed until EndBulkRegistration().
0258   Standard_EXPORT void BeginBulkRegistration();
0259 
0260   //! Finish bulk deferred registration and publish one revision update if anything changed.
0261   Standard_EXPORT void EndBulkRegistration();
0262 
0263   //! Visit deferred entries. Callback receives item id and an entry copy; returning false stops.
0264   template <typename VisitorT>
0265   void ForEachDeferred(VisitorT&& theVisitor) const
0266   {
0267     for (NCollection_DataMap<BRepGraph_ItemId, Entry>::Iterator anIt(myEntries); anIt.More();)
0268     {
0269       const BRepGraph_ItemId anItem  = anIt.Key();
0270       const Entry            anEntry = anIt.Value();
0271       anIt.Next();
0272       if (!theVisitor(anItem, anEntry))
0273       {
0274         break;
0275       }
0276     }
0277   }
0278 
0279   //! Visit deferred entries with at least one representation of the requested kind.
0280   //! Callback receives item id and an entry copy; returning false stops.
0281   template <typename VisitorT>
0282   void ForEachDeferred(const RepresentationKind theKind, VisitorT&& theVisitor) const
0283   {
0284     for (NCollection_DataMap<BRepGraph_ItemId, Entry>::Iterator anIt(myEntries); anIt.More();)
0285     {
0286       if (!anIt.Value().Representations.ContainsKind(theKind))
0287       {
0288         anIt.Next();
0289         continue;
0290       }
0291 
0292       const BRepGraph_ItemId anItem  = anIt.Key();
0293       const Entry            anEntry = anIt.Value();
0294       anIt.Next();
0295       if (!theVisitor(anItem, anEntry))
0296       {
0297         break;
0298       }
0299     }
0300   }
0301 
0302   Standard_EXPORT const TCollection_AsciiString& Name() const override;
0303   Standard_EXPORT void OnNodeRemoved(const BRepGraph_NodeId theNode) noexcept override;
0304   Standard_EXPORT void OnNodeReplaced(const BRepGraph_NodeId theOldNode,
0305                                       const BRepGraph_NodeId theNewNode) noexcept override;
0306   Standard_EXPORT void CopyTo(const BRepGraph_CopyRemap& theCopy) const override;
0307   Standard_EXPORT void OnRefRemoved(const BRepGraph_RefId theRef) noexcept override;
0308   Standard_EXPORT void InvalidateAll() noexcept override;
0309   Standard_EXPORT void Clear() noexcept override;
0310 
0311   DEFINE_STANDARD_RTTIEXT(BRepGraph_LayerDeferred, BRepGraph_Layer)
0312 
0313 private:
0314   void removeItem(const BRepGraph_ItemId theItem) noexcept;
0315   void lockItem(const BRepGraph_ItemId theItem);
0316   void unlockItem(const BRepGraph_ItemId theItem);
0317 
0318 private:
0319   NCollection_DataMap<BRepGraph_ItemId, Entry> myEntries;
0320   uint32_t                                     myBulkRegistrationDepth = 0;
0321   bool                                         myHasBulkChanges        = false;
0322 };
0323 
0324 #endif // _BRepGraph_LayerDeferred_HeaderFile