HandleBase

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#ifndef DataFormats_Common_interface_HandleBase_h
#define DataFormats_Common_interface_HandleBase_h

/*----------------------------------------------------------------------
  
Handle: Non-owning "smart pointer" for reference to products and
their provenances.

This is a very preliminary version, and lacks safety features and
elegance.

If the pointed-to product or provenance is destroyed, use of the
Handle becomes undefined. There is no way to query the Handle to
discover if this has happened.

Handles can have:
  -- Product and Provenance pointers both null;
  -- Both pointers valid

To check validity, one can use the isValid() function.

If failedToGet() returns true then the requested data is not available
If failedToGet() returns false but isValid() is also false then no attempt 
  to get data has occurred

----------------------------------------------------------------------*/

#include <algorithm>
#include <cassert>
#include <memory>

#include "DataFormats/Provenance/interface/ProductID.h"
#include "DataFormats/Provenance/interface/ProvenanceFwd.h"
#include "DataFormats/Common/interface/HandleExceptionFactory.h"

namespace cms {
  class Exception;
}

namespace edm {
  class HandleBase {
  public:
    HandleBase() : product_(nullptr), prov_(nullptr) {}

    HandleBase(void const* prod, Provenance const* prov) : product_(prod), prov_(prov) {
      assert(prod);
      assert(prov);
    }

    ///Used when the attempt to get the data failed
    HandleBase(std::shared_ptr<HandleExceptionFactory const>&& iWhyFailed)
        : product_(nullptr), prov_(nullptr), whyFailedFactory_(iWhyFailed) {}

    void clear() {
      product_ = nullptr;
      prov_ = nullptr;
      whyFailedFactory_.reset();
    }

    void swap(HandleBase& other) {
      std::swap(product_, other.product_);
      std::swap(prov_, other.prov_);
      std::swap(whyFailedFactory_, other.whyFailedFactory_);
    }

    bool isValid() const { return product_ && prov_; }

    bool failedToGet() const { return bool(whyFailedFactory_); }

    Provenance const* provenance() const { return prov_; }

    ProductID id() const;

    std::shared_ptr<cms::Exception> whyFailed() const {
      if (whyFailedFactory_.get()) {
        return whyFailedFactory_->make();
      }
      return std::shared_ptr<cms::Exception>();
    }

    std::shared_ptr<HandleExceptionFactory const> const& whyFailedFactory() const { return whyFailedFactory_; }

    explicit operator bool() const { return isValid(); }

    bool operator!() const { return not isValid(); }

  protected:
    void const* productStorage() const;

  private:
    void const* product_;
    Provenance const* prov_;
    std::shared_ptr<HandleExceptionFactory const> whyFailedFactory_;
  };

  // Free swap function
  inline void swap(HandleBase& a, HandleBase& b) { a.swap(b); }

}  // namespace edm

#endif  // DataFormats_Common_interface_HandleBase_h