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_ItemId_HeaderFile
0015 #define _BRepGraph_ItemId_HeaderFile
0016 
0017 #include <BRepGraph_NodeId.hxx>
0018 #include <BRepGraph_RefId.hxx>
0019 #include <Standard_Assert.hxx>
0020 #include <Standard_HashUtils.hxx>
0021 #include <Standard_TypeDef.hxx>
0022 
0023 #include <cstdint>
0024 #include <functional>
0025 #include <limits>
0026 
0027 //! Generic BRepGraph item identifier covering definitions and references.
0028 //! Use-records are NOT included - they are session-local, not graph identity.
0029 class BRepGraph_ItemId
0030 {
0031 public:
0032   //! Addressed graph item domain.
0033   enum class Domain : uint8_t
0034   {
0035     None,
0036     Node,
0037     Reference
0038   };
0039 
0040   //! Construct an invalid item id.
0041   BRepGraph_ItemId() = default;
0042 
0043   //! Construct a node item id.
0044   BRepGraph_ItemId(const BRepGraph_NodeId theNode)
0045   {
0046     if (theNode.IsValid())
0047     {
0048       myDomain = Domain::Node;
0049       myIndex  = theNode.Index;
0050       myKind   = static_cast<uint8_t>(theNode.NodeKind);
0051     }
0052   }
0053 
0054   //! Construct a reference item id.
0055   BRepGraph_ItemId(const BRepGraph_RefId theRef)
0056   {
0057     if (theRef.IsValid())
0058     {
0059       myDomain = Domain::Reference;
0060       myIndex  = theRef.Index;
0061       myKind   = static_cast<uint8_t>(theRef.RefKind);
0062     }
0063   }
0064 
0065   //! Return true if this item addresses a graph object.
0066   [[nodiscard]] bool IsValid() const noexcept
0067   {
0068     if (myIndex == THE_INVALID_INDEX)
0069     {
0070       return false;
0071     }
0072 
0073     switch (myDomain)
0074     {
0075       case Domain::Node:
0076         return BRepGraph_NodeId::IsValidKind(static_cast<BRepGraph_NodeId::Kind>(myKind));
0077       case Domain::Reference:
0078         return BRepGraph_RefId::IsValidKind(static_cast<BRepGraph_RefId::Kind>(myKind));
0079       case Domain::None:
0080         return false;
0081     }
0082     return false;
0083   }
0084 
0085   //! Return the addressed domain.
0086   [[nodiscard]] Domain ItemDomain() const noexcept { return myDomain; }
0087 
0088   //! Return true if this item addresses a definition node.
0089   [[nodiscard]] bool IsNode() const noexcept { return myDomain == Domain::Node; }
0090 
0091   //! Return true if this item addresses a reference entry.
0092   [[nodiscard]] bool IsReference() const noexcept { return myDomain == Domain::Reference; }
0093 
0094   //! Convert to node id. Returns invalid id for non-node items.
0095   [[nodiscard]] BRepGraph_NodeId NodeId() const noexcept
0096   {
0097     return IsNode() && BRepGraph_NodeId::IsValidKind(static_cast<BRepGraph_NodeId::Kind>(myKind))
0098              ? BRepGraph_NodeId(static_cast<BRepGraph_NodeId::Kind>(myKind), myIndex)
0099              : BRepGraph_NodeId();
0100   }
0101 
0102   //! Convert to reference id. Returns invalid id for non-reference items.
0103   [[nodiscard]] BRepGraph_RefId RefId() const noexcept
0104   {
0105     return IsReference() && BRepGraph_RefId::IsValidKind(static_cast<BRepGraph_RefId::Kind>(myKind))
0106              ? BRepGraph_RefId(static_cast<BRepGraph_RefId::Kind>(myKind), myIndex)
0107              : BRepGraph_RefId();
0108   }
0109 
0110   //! Return node kind. Valid only when IsNode() is true.
0111   [[nodiscard]] BRepGraph_NodeId::Kind NodeKind() const noexcept
0112   {
0113     Standard_ASSERT_RETURN(
0114       IsNode() && BRepGraph_NodeId::IsValidKind(static_cast<BRepGraph_NodeId::Kind>(myKind)),
0115       "BRepGraph_ItemId::NodeKind(): item is not a valid node",
0116       BRepGraph_NodeId::Kind::Solid);
0117     return static_cast<BRepGraph_NodeId::Kind>(myKind);
0118   }
0119 
0120   //! Return reference kind. Valid only when IsReference() is true.
0121   [[nodiscard]] BRepGraph_RefId::Kind RefKind() const noexcept
0122   {
0123     Standard_ASSERT_RETURN(
0124       IsReference() && BRepGraph_RefId::IsValidKind(static_cast<BRepGraph_RefId::Kind>(myKind)),
0125       "BRepGraph_ItemId::RefKind(): item is not a valid reference",
0126       BRepGraph_RefId::Kind::Shell);
0127     return static_cast<BRepGraph_RefId::Kind>(myKind);
0128   }
0129 
0130   //! Return item kind encoded in its own domain enum space.
0131   [[nodiscard]] uint8_t RawKind() const noexcept { return myKind; }
0132 
0133   //! Return item kind encoded in its own domain enum space.
0134   [[nodiscard]] uint8_t Kind() const noexcept { return RawKind(); }
0135 
0136   //! Return item per-kind index.
0137   [[nodiscard]] uint32_t Index() const noexcept { return myIndex; }
0138 
0139   friend bool operator==(const BRepGraph_ItemId& theLeft, const BRepGraph_ItemId& theRight) noexcept
0140   {
0141     return theLeft.myDomain == theRight.myDomain && theLeft.myKind == theRight.myKind
0142            && theLeft.myIndex == theRight.myIndex;
0143   }
0144 
0145   friend bool operator!=(const BRepGraph_ItemId& theLeft, const BRepGraph_ItemId& theRight) noexcept
0146   {
0147     return !(theLeft == theRight);
0148   }
0149 
0150 private:
0151   static constexpr uint32_t THE_INVALID_INDEX = std::numeric_limits<uint32_t>::max();
0152 
0153   Domain   myDomain = Domain::None;
0154   uint32_t myIndex  = THE_INVALID_INDEX;
0155   uint8_t  myKind   = 0;
0156 };
0157 
0158 //! std::hash specialization for BRepGraph_ItemId.
0159 template <>
0160 struct std::hash<BRepGraph_ItemId>
0161 {
0162   size_t operator()(const BRepGraph_ItemId& theId) const noexcept
0163   {
0164     size_t aCombination[3];
0165     aCombination[0] = opencascade::hash(static_cast<uint32_t>(theId.ItemDomain()));
0166     aCombination[1] = opencascade::hash(static_cast<uint32_t>(theId.RawKind()));
0167     aCombination[2] = opencascade::hash(theId.Index());
0168     return opencascade::hashBytes(aCombination, sizeof(aCombination));
0169   }
0170 };
0171 
0172 #endif // _BRepGraph_ItemId_HeaderFile