Back to home page

EIC code displayed by LXR

 
 

    


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

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_VersionStamp_HeaderFile
0015 #define _BRepGraph_VersionStamp_HeaderFile
0016 
0017 #include <BRepGraph_ItemUID.hxx>
0018 #include <BRepGraph_RefUID.hxx>
0019 #include <BRepGraph_UID.hxx>
0020 #include <Standard_GUID.hxx>
0021 
0022 #include <cstdint>
0023 
0024 //! @brief Snapshot of a graph item identity and its freshness generation.
0025 //!
0026 //! Combines a persistent node or reference UID with OwnGen (own-data mutation counter)
0027 //! and graph Generation (BRepGraph::Clear() cycle). It is intended for custom cache and
0028 //! layer freshness checks, not as a separate topology identity model.
0029 //!
0030 //! Usage pattern:
0031 //! @code
0032 //!   BRepGraph_VersionStamp aStamp = aGraph.UIDs().StampOf(aFaceId);
0033 //!   // ... later, after mutations ...
0034 //!   if (aGraph.UIDs().IsStale(aStamp))
0035 //!     recomputeDerivedData();
0036 //! @endcode
0037 //!
0038 struct BRepGraph_VersionStamp
0039 {
0040   //! Identity domain encoded in this stamp.
0041   enum class Domain : uint8_t
0042   {
0043     None      = 0,
0044     Node      = 1,
0045     Reference = 2
0046   };
0047 
0048   BRepGraph_UID    myNodeUID;     //!< Definition-node identity for node-domain stamps.
0049   BRepGraph_RefUID myRefUID;      //!< Reference-entry identity for reference-domain stamps.
0050   uint32_t         myMutationGen; //!< OwnGen counter at snapshot time.
0051   uint32_t         myGeneration;  //!< Graph BRepGraph::Clear() generation at snapshot time.
0052   Domain           myDomain;      //!< Active identity domain.
0053 
0054   //! Default constructor. Creates an invalid stamp (invalid UID, zero counters).
0055   BRepGraph_VersionStamp()
0056       : myMutationGen(0),
0057         myGeneration(0),
0058         myDomain(Domain::None)
0059   {
0060   }
0061 
0062   //! Construct a node-domain stamp from components.
0063   //! @param[in] theUID         persistent definition-node identity
0064   //! @param[in] theMutationGen OwnGen counter (own-data mutation counter)
0065   //! @param[in] theGeneration  graph BRepGraph::Clear() generation
0066   BRepGraph_VersionStamp(const BRepGraph_UID& theUID,
0067                          const uint32_t       theMutationGen,
0068                          const uint32_t       theGeneration)
0069       : myNodeUID(theUID),
0070         myMutationGen(theMutationGen),
0071         myGeneration(theGeneration),
0072         myDomain(Domain::Node)
0073   {
0074   }
0075 
0076   //! Construct a reference-domain stamp from components.
0077   //! @param[in] theRefUID      persistent reference identity
0078   //! @param[in] theMutationGen OwnGen counter (own-data mutation counter)
0079   //! @param[in] theGeneration  graph BRepGraph::Clear() generation
0080   BRepGraph_VersionStamp(const BRepGraph_RefUID& theRefUID,
0081                          const uint32_t          theMutationGen,
0082                          const uint32_t          theGeneration)
0083       : myRefUID(theRefUID),
0084         myMutationGen(theMutationGen),
0085         myGeneration(theGeneration),
0086         myDomain(Domain::Reference)
0087   {
0088   }
0089 
0090   //! Check if the stamp has a valid identity in its domain.
0091   [[nodiscard]] bool IsValid() const
0092   {
0093     if (myDomain == Domain::Node)
0094     {
0095       return myNodeUID.IsValid();
0096     }
0097     if (myDomain == Domain::Reference)
0098     {
0099       return myRefUID.IsValid();
0100     }
0101     return myNodeUID.IsValid() || myRefUID.IsValid();
0102   }
0103 
0104   //! True when this is a definition-node-domain stamp.
0105   [[nodiscard]] bool IsNodeStamp() const
0106   {
0107     if (myDomain == Domain::Node)
0108     {
0109       return myNodeUID.IsValid();
0110     }
0111     return myDomain == Domain::None && myNodeUID.IsValid() && !myRefUID.IsValid();
0112   }
0113 
0114   //! True when this is a reference-domain stamp.
0115   [[nodiscard]] bool IsRefStamp() const
0116   {
0117     if (myDomain == Domain::Reference)
0118     {
0119       return myRefUID.IsValid();
0120     }
0121     return myDomain == Domain::None && myRefUID.IsValid() && !myNodeUID.IsValid();
0122   }
0123 
0124   //! Return the active generic item identity.
0125   [[nodiscard]] BRepGraph_ItemUID ItemUID() const
0126   {
0127     if (myDomain == Domain::Node)
0128     {
0129       return BRepGraph_ItemUID::Node(myNodeUID.Kind, myNodeUID.Counter);
0130     }
0131     if (myDomain == Domain::Reference)
0132     {
0133       return BRepGraph_ItemUID::Reference(myRefUID.Kind, myRefUID.Counter);
0134     }
0135     return BRepGraph_ItemUID();
0136   }
0137 
0138   //! Full equality: same domain, UID, OwnGen, and Generation.
0139   //! Two invalid stamps are equal.
0140   Standard_EXPORT bool operator==(const BRepGraph_VersionStamp& theOther) const;
0141 
0142   bool operator!=(const BRepGraph_VersionStamp& theOther) const { return !(*this == theOther); }
0143 
0144   //! Check if two stamps refer to the same graph item regardless of version.
0145   //! Compares active UID only, ignoring OwnGen and Generation.
0146   //! @param[in] theOther stamp to compare with
0147   //! @return true if both stamps have the same domain and UID
0148   [[nodiscard]] Standard_EXPORT bool IsSameItem(const BRepGraph_VersionStamp& theOther) const;
0149 
0150   //! Derive a deterministic Standard_GUID from this stamp.
0151   //! The graph GUID is incorporated into the hash, making per-node GUIDs
0152   //! globally unique across different graph instances.
0153   //! One-way: cannot reconstruct stamp fields from the resulting GUID.
0154   //! @param[in] theGraphGUID the owning graph's identity GUID
0155   //! @return deterministic Standard_GUID derived from stamp + graph GUID
0156   [[nodiscard]] Standard_EXPORT Standard_GUID ToGUID(const Standard_GUID& theGraphGUID) const;
0157 
0158   //! Compute hash value consistent with operator==.
0159   //! @return hash combining active UID, domain, OwnGen, and Generation
0160   [[nodiscard]] Standard_EXPORT size_t HashValue() const;
0161 };
0162 
0163 //! std::hash specialization for NCollection_DefaultHasher support.
0164 template <>
0165 struct std::hash<BRepGraph_VersionStamp>
0166 {
0167   size_t operator()(const BRepGraph_VersionStamp& theStamp) const noexcept
0168   {
0169     return theStamp.HashValue();
0170   }
0171 };
0172 
0173 #endif // _BRepGraph_VersionStamp_HeaderFile