Back to home page

EIC code displayed by LXR

 
 

    


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

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_Cache_HeaderFile
0015 #define _BRepGraph_Cache_HeaderFile
0016 
0017 #include <BRepGraph_ItemId.hxx>
0018 #include <BRepGraph_NodeId.hxx>
0019 #include <BRepGraph_RefId.hxx>
0020 #include <Standard_GUID.hxx>
0021 #include <Standard_Handle.hxx>
0022 #include <Standard_Transient.hxx>
0023 #include <TCollection_AsciiString.hxx>
0024 
0025 #include <cstdint>
0026 
0027 class BRepGraph;
0028 class BRepGraph_CacheRegistry;
0029 class BRepGraph_CopyRemap;
0030 
0031 //! @brief Lightweight owner-bound base for transient graph cache services.
0032 //!
0033 //! A cache service stores typed, recomputable, graph-local data such as
0034 //! bounding boxes, UV bounds, or display-resolution results. The registry owns
0035 //! only service identity and lifetime binding; concrete caches own their own
0036 //! typed storage and validate freshness lazily via graph generation counters.
0037 class BRepGraph_Cache : public Standard_Transient
0038 {
0039 public:
0040   //! Cache service identity, unique within a graph registry.
0041   [[nodiscard]] virtual const Standard_GUID& ID() const = 0;
0042 
0043   //! Cache service display name.
0044   [[nodiscard]] virtual const TCollection_AsciiString& Name() const = 0;
0045 
0046   //! Clear all transient data owned by this cache.
0047   Standard_EXPORT virtual void Clear() noexcept;
0048 
0049   //! Copy fresh, remappable cache data into the target graph described by the remap.
0050   //! Default implementation copies nothing.
0051   Standard_EXPORT virtual void CopyFreshTo(const BRepGraph_CopyRemap& theCopy) const;
0052 
0053   DEFINE_STANDARD_RTTIEXT(BRepGraph_Cache, Standard_Transient)
0054 
0055 protected:
0056   //! @brief Value base for node-derived cache entries.
0057   //!
0058   //! Concrete cache services inherit this from their private entry structs and
0059   //! store entries by value in their own typed storage. The base records the
0060   //! node identity plus the generation counter used to validate freshness.
0061   class NodeEntry
0062   {
0063   public:
0064     //! Reset to an unbound stale state.
0065     Standard_EXPORT void Reset() noexcept;
0066 
0067     //! Bind this entry to the current OwnGen of a node.
0068     //! @return false when the cache is detached or the node is inactive
0069     [[nodiscard]] Standard_EXPORT bool BindOwnGen(const BRepGraph_Cache& theCache,
0070                                                   const BRepGraph_NodeId theNode) noexcept;
0071 
0072     //! Bind this entry to the current SubtreeGen of a node.
0073     //! @return false when the cache is detached or the node is inactive
0074     [[nodiscard]] Standard_EXPORT bool BindSubtreeGen(const BRepGraph_Cache& theCache,
0075                                                       const BRepGraph_NodeId theNode) noexcept;
0076 
0077     //! Validate against the current OwnGen of the same node.
0078     [[nodiscard]] Standard_EXPORT bool IsFreshOwn(const BRepGraph_Cache& theCache,
0079                                                   const BRepGraph_NodeId theNode) const noexcept;
0080 
0081     //! Validate against the current SubtreeGen of the same node.
0082     [[nodiscard]] Standard_EXPORT bool IsFreshSubtree(
0083       const BRepGraph_Cache& theCache,
0084       const BRepGraph_NodeId theNode) const noexcept;
0085 
0086     //! Validate against the generation kind captured at bind time.
0087     [[nodiscard]] Standard_EXPORT bool IsFresh(const BRepGraph_Cache& theCache) const noexcept;
0088 
0089     //! True if the entry was successfully bound to a node generation.
0090     [[nodiscard]] bool IsBound() const noexcept { return myKind != GenKind::None; }
0091 
0092     //! Node identity captured at bind time.
0093     [[nodiscard]] BRepGraph_NodeId Node() const noexcept { return myNode; }
0094 
0095   private:
0096     enum class GenKind : uint8_t
0097     {
0098       None,
0099       Own,
0100       Subtree
0101     };
0102 
0103     [[nodiscard]] Standard_EXPORT bool bind(const BRepGraph_Cache& theCache,
0104                                             const BRepGraph_NodeId theNode,
0105                                             const GenKind          theKind) noexcept;
0106 
0107     [[nodiscard]] Standard_EXPORT bool isFresh(const BRepGraph_Cache& theCache,
0108                                                const BRepGraph_NodeId theNode,
0109                                                const GenKind          theKind) const noexcept;
0110 
0111     BRepGraph_NodeId myNode;
0112     uint32_t         myGeneration = 0;
0113     GenKind          myKind       = GenKind::None;
0114   };
0115 
0116   //! @brief Value base for reference-derived cache entries.
0117   class RefEntry
0118   {
0119   public:
0120     //! Reset to an unbound stale state.
0121     Standard_EXPORT void Reset() noexcept;
0122 
0123     //! Bind this entry to the current OwnGen of a reference.
0124     //! @return false when the cache is detached or the reference is inactive
0125     [[nodiscard]] Standard_EXPORT bool BindOwnGen(const BRepGraph_Cache& theCache,
0126                                                   const BRepGraph_RefId  theRef) noexcept;
0127 
0128     //! Validate against the current OwnGen of the same reference.
0129     [[nodiscard]] Standard_EXPORT bool IsFreshOwn(const BRepGraph_Cache& theCache,
0130                                                   const BRepGraph_RefId  theRef) const noexcept;
0131 
0132     //! Validate against the reference captured at bind time.
0133     [[nodiscard]] Standard_EXPORT bool IsFresh(const BRepGraph_Cache& theCache) const noexcept;
0134 
0135     //! True if the entry was successfully bound to a reference generation.
0136     [[nodiscard]] bool IsBound() const noexcept { return myIsBound; }
0137 
0138     //! Reference identity captured at bind time.
0139     [[nodiscard]] BRepGraph_RefId Ref() const noexcept { return myRef; }
0140 
0141   private:
0142     BRepGraph_RefId myRef;
0143     uint32_t        myGeneration = 0;
0144     bool            myIsBound    = false;
0145   };
0146 
0147   //! @brief Value base for item-derived cache entries.
0148   class ItemEntry
0149   {
0150   public:
0151     //! Reset to an unbound stale state.
0152     Standard_EXPORT void Reset() noexcept;
0153 
0154     //! Bind this entry to the current OwnGen of a graph item.
0155     //! @return false when the cache is detached or the item is inactive
0156     [[nodiscard]] Standard_EXPORT bool BindOwnGen(const BRepGraph_Cache& theCache,
0157                                                   const BRepGraph_ItemId theItem) noexcept;
0158 
0159     //! Validate against the current OwnGen of the same item.
0160     [[nodiscard]] Standard_EXPORT bool IsFreshOwn(const BRepGraph_Cache& theCache,
0161                                                   const BRepGraph_ItemId theItem) const noexcept;
0162 
0163     //! Validate against the item captured at bind time.
0164     [[nodiscard]] Standard_EXPORT bool IsFresh(const BRepGraph_Cache& theCache) const noexcept;
0165 
0166     //! True if the entry was successfully bound to an item generation.
0167     [[nodiscard]] bool IsBound() const noexcept { return myIsBound; }
0168 
0169     //! Item identity captured at bind time.
0170     [[nodiscard]] BRepGraph_ItemId Item() const noexcept { return myItem; }
0171 
0172   private:
0173     BRepGraph_ItemId myItem;
0174     uint32_t         myGeneration = 0;
0175     bool             myIsBound    = false;
0176   };
0177 
0178   Standard_EXPORT BRepGraph_Cache();
0179 
0180   //! True while this cache is registered in a live graph registry.
0181   [[nodiscard]] bool IsAttached() const noexcept { return myGraph != nullptr; }
0182 
0183   //! Attached graph for read-only cache services. Raises Standard_ProgramError if detached.
0184   [[nodiscard]] Standard_EXPORT const BRepGraph& Graph() const;
0185 
0186   //! Attached mutable graph for graph-owned cache services. Returns null if detached.
0187   [[nodiscard]] BRepGraph* AttachedGraph() const noexcept { return myGraph; }
0188 
0189   //! Called after the cache is attached to a graph registry.
0190   Standard_EXPORT virtual void OnAttached() noexcept;
0191 
0192   //! Called before the cache is detached from a graph registry.
0193   Standard_EXPORT virtual void OnDetached() noexcept;
0194 
0195   //! Return current SubtreeGen for an active node.
0196   [[nodiscard]] Standard_EXPORT bool NodeSubtreeGen(const BRepGraph_NodeId theNode,
0197                                                     uint32_t&              theGen) const noexcept;
0198 
0199   //! Return current OwnGen for an active node.
0200   [[nodiscard]] Standard_EXPORT bool NodeOwnGen(const BRepGraph_NodeId theNode,
0201                                                 uint32_t&              theGen) const noexcept;
0202 
0203   //! Return current OwnGen for an active reference.
0204   [[nodiscard]] Standard_EXPORT bool RefOwnGen(const BRepGraph_RefId theRef,
0205                                                uint32_t&             theGen) const noexcept;
0206 
0207   //! Return current OwnGen for an active graph item.
0208   [[nodiscard]] Standard_EXPORT bool ItemOwnGen(const BRepGraph_ItemId theItem,
0209                                                 uint32_t&              theGen) const noexcept;
0210 
0211   //! Resolve an active reference to its active child node.
0212   [[nodiscard]] Standard_EXPORT bool ResolveActiveRefChild(
0213     const BRepGraph_RefId theRef,
0214     BRepGraph_NodeId&     theNode) const noexcept;
0215 
0216   //! Resolve an active face reference to its active face node.
0217   [[nodiscard]] Standard_EXPORT bool ResolveActiveFaceRef(const BRepGraph_FaceRefId theRef,
0218                                                           BRepGraph_FaceId& theFace) const noexcept;
0219 
0220 private:
0221   friend class ::BRepGraph_CacheRegistry;
0222 
0223   Standard_EXPORT void attachGraph(BRepGraph* theGraph) noexcept;
0224   Standard_EXPORT void rebindGraph(BRepGraph* theGraph) noexcept;
0225   Standard_EXPORT void detachGraph() noexcept;
0226 
0227   BRepGraph* myGraph = nullptr;
0228 };
0229 
0230 #endif // _BRepGraph_Cache_HeaderFile