Skip to content

Draft: Use CxxUtils::span to reduce const_cast in TrkGeometry

Hey @ssnyder

this is not to be merged per se, as is at least. As I include the full CxxUtils::span but wanted to see if the "trick" can work

So on top of the span we can do now.

 /** Return all objects of the Array non-const T*/
  const std::vector<T*>& arrayObjects()
  {
    createArrayCache();
    return (*m_arrayObjects);
  }

  /** Return all objects of the Array const T*/
  using const_ptr_span = CxxUtils::span<const T* const>;
  const_ptr_span arrayObjects() const
  {
    createArrayCache();
    return (&*m_arrayObjects.begin(), &*m_arrayObjects.end());
  }

The end product of all of these (span , plus overloads)

  // get a reference surface
  -  const std::vector<const Trk::Surface*>& surfaces =
        m_surfaceArray->arrayObjects();
   + CxxUtils::span<Trk::Surface const * const> surfaces =  std::as_const(*m_surfaceArray).arrayObjects();


  if (m_surfaceArray) {
   - const std::vector<const Trk::Surface*>& surfaces =
        m_surfaceArray->arrayObjects();
   + CxxUtils::span<Trk::Surface const * const > surfaces = std::as_const(*m_surfaceArray).arrayObjects();

  // set the subsurface representation, usually already owned by DetElement
  if (m_surfaceArray) {
 -  const std::vector<const Trk::Surface*>& surfaces =
        m_surfaceArray->arrayObjects();
 -  for (const auto *sIter : surfaces) {
 +  const std::vector<Trk::Surface*>& surfaces = m_surfaceArray->arrayObjects();
 +  for (auto *sIter : surfaces) {
      if (sIter && (*sIter).owner() == Trk::noOwn) {
 -      const_cast<Trk::Surface&>((*sIter)).setOwner(Trk::TGOwn);
 +     (*sIter).setOwner(Trk::TGOwn);

The 2 first are from const methods the last one is non-const one . Of course one will need to do the Volume/Geometry also like the Layer.

Let me ping @sroe . Anyhow needs to go over a bit of code still but seems there is a chance to get rid of quite a few const_cast in TrkGeometry

Edited by Christos Anastopoulos

Merge request reports