View

ViewBase

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
#ifndef DataFormats_Common_View_h
#define DataFormats_Common_View_h
// -*- C++ -*-
//
// Package:     Framework
// Class  :     View
//
/**\class edm::View<T>

Description: Provide access to the collected elements contained by any WrapperBase that is a sequence.

*/
//
// Original Author:
//         Created:  Mon Dec 18 09:48:30 CST 2006
//

#include "DataFormats/Common/interface/Ptr.h"
#include "DataFormats/Common/interface/RefToBase.h"
#include "DataFormats/Common/interface/IndirectHolder.h"
#include "DataFormats/Common/interface/RefHolder_.h"
#include "boost/iterator/indirect_iterator.hpp"

#include <vector>
#include <memory>
#include <algorithm>
#include <iterator>
#include <utility>
#include <cassert>

namespace edm {
  class EDProductGetter;

  //------------------------------------------------------------------
  // Class ViewBase
  //
  // ViewBase is an abstract base class. It exists only so that we
  // make invoke View<T> destructors polymorphically, and copy them
  // using clone().
  //
  //------------------------------------------------------------------

  class ViewBase {
  public:
    virtual ~ViewBase();
    std::unique_ptr<ViewBase> clone() const;

  protected:
    ViewBase() = default;
    ViewBase(ViewBase const&) = default;
    ViewBase(ViewBase&&) = default;
    ViewBase& operator=(ViewBase const&) = default;
    ViewBase& operator=(ViewBase&&) = default;
    virtual std::unique_ptr<ViewBase> doClone() const = 0;
    void swap(ViewBase&) {}  // Nothing to swap
  };

  //------------------------------------------------------------------
  /// Class template View<T>
  ///
  /// View<T> provides a way to allow reference to the elements (of
  /// type T) of some collection in an Event, without knowing about the
  /// type of the collection itself. For example, View<int> can refer
  /// to the ints in either a vector<int> or a list<int>, without the
  /// client code knowing about which type of container manages the
  /// ints.
  ///
  /// View<T> is not persistable.
  ///
  /// View<T> can be used to reference objects of any type that has T
  /// as a public base.
  ///
  //------------------------------------------------------------------

  template <typename T>
  class View : public ViewBase {
    typedef std::vector<T const*> seq_t;

  public:
    typedef T const* pointer;
    typedef T const* const_pointer;

    typedef T const& reference;
    typedef T const& const_reference;

    typedef T value_type;

    typedef boost::indirect_iterator<typename seq_t::const_iterator> const_iterator;

    // This should be a typedef to seq_t::size_type but because this type is used as a template
    // argument in a persistened class it must be stable for different architectures
    typedef unsigned int size_type;
    typedef typename seq_t::difference_type difference_type;

    typedef boost::indirect_iterator<typename seq_t::const_reverse_iterator> const_reverse_iterator;

    // Compiler-generated copy, and assignment each does the right
    // thing.

    View();

    // This function is dangerous, and should only be called from the
    // infrastructure code.
    View(std::vector<void const*> const& pointers, FillViewHelperVector const& helpers, EDProductGetter const* getter);

    void swap(View& other);

    size_type capacity() const;

    // Most non-const member functions not present.
    // No access to non-const contents provided.

    const_iterator begin() const;
    const_iterator end() const;

    const_reverse_iterator rbegin() const;
    const_reverse_iterator rend() const;

    size_type size() const;
    size_type max_size() const;
    bool empty() const;
    const_reference at(size_type pos) const;
    const_reference operator[](size_type pos) const;
    RefToBase<value_type> refAt(size_type i) const;
    Ptr<value_type> ptrAt(size_type i) const;
    std::vector<Ptr<value_type>> const& ptrs() const;

    const_reference front() const;
    const_reference back() const;

    // No erase, because erase is required to return an *iterator*,
    // not a *const_iterator*.

    // The following is for testing only.
    static void fill_from_range(T* first, T* last, View& output);

  private:
    seq_t items_;
    std::vector<Ptr<value_type>> vPtrs_;
    std::unique_ptr<ViewBase> doClone() const override;
  };

  // Associated free functions (same as for std::vector)
  template <typename T>
  bool operator==(View<T> const&, View<T> const&);
  template <typename T>
  bool operator!=(View<T> const&, View<T> const&);
  template <typename T>
  bool operator<(View<T> const&, View<T> const&);
  template <typename T>
  bool operator<=(View<T> const&, View<T> const&);
  template <typename T>
  bool operator>(View<T> const&, View<T> const&);
  template <typename T>
  bool operator>=(View<T> const&, View<T> const&);

  //------------------------------------------------------------------
  // Implementation of View<T>
  //------------------------------------------------------------------

  template <typename T>
  inline View<T>::View() : items_(), vPtrs_() {}

  template <typename T>
  View<T>::View(std::vector<void const*> const& pointers,
                FillViewHelperVector const& helpers,
                EDProductGetter const* getter)
      : items_(), vPtrs_() {
    size_type numElements = pointers.size();

    // If the two input vectors are not of the same size, there is a
    // logic error in the framework code that called this.
    // constructor.
    assert(numElements == helpers.size());

    items_.reserve(numElements);
    vPtrs_.reserve(numElements);
    for (std::vector<void const*>::size_type i = 0; i < pointers.size(); ++i) {
      void const* p = pointers[i];
      auto const& h = helpers[i];
      items_.push_back(static_cast<pointer>(p));
      if (nullptr != p) {
        vPtrs_.push_back(Ptr<T>(h.first, static_cast<T const*>(p), h.second));
      } else if (getter != nullptr) {
        vPtrs_.push_back(Ptr<T>(h.first, h.second, getter));
      } else {
        vPtrs_.push_back(Ptr<T>(h.first, nullptr, h.second));
      }
    }
  }

  template <typename T>
  inline void View<T>::swap(View& other) {
    this->ViewBase::swap(other);
    items_.swap(other.items_);
    vPtrs_.swap(other.vPtrs_);
  }

  template <typename T>
  inline typename View<T>::size_type View<T>::capacity() const {
    return items_.capacity();
  }

  template <typename T>
  inline typename View<T>::const_iterator View<T>::begin() const {
    return items_.begin();
  }

  template <typename T>
  inline typename View<T>::const_iterator View<T>::end() const {
    return items_.end();
  }

  template <typename T>
  inline typename View<T>::const_reverse_iterator View<T>::rbegin() const {
    return items_.rbegin();
  }

  template <typename T>
  inline typename View<T>::const_reverse_iterator View<T>::rend() const {
    return items_.rend();
  }

  template <typename T>
  inline typename View<T>::size_type View<T>::size() const {
    return items_.size();
  }

  template <typename T>
  inline typename View<T>::size_type View<T>::max_size() const {
    return items_.max_size();
  }

  template <typename T>
  inline bool View<T>::empty() const {
    return items_.empty();
  }

  template <typename T>
  inline typename View<T>::const_reference View<T>::at(size_type pos) const {
    return *items_.at(pos);
  }

  template <typename T>
  inline typename View<T>::const_reference View<T>::operator[](size_type pos) const {
    return *items_[pos];
  }

  template <typename T>
  inline RefToBase<T> View<T>::refAt(size_type i) const {
    //NOTE: considered creating a special BaseHolder for edm::Ptr.
    // But the IndirectHolder and RefHolder would still be needed
    // for other reasons. To reduce the number of dictionaries needed
    // we avoid using a more efficient BaseHolder.
    return RefToBase<T>(std::unique_ptr<reftobase::BaseHolder<T>>{new reftobase::IndirectHolder<T>{
        std::unique_ptr<reftobase::RefHolder<edm::Ptr<T>>>{new reftobase::RefHolder<Ptr<T>>{ptrAt(i)}}}});
  }

  template <typename T>
  inline Ptr<T> View<T>::ptrAt(size_type i) const {
    return vPtrs_[i];
  }

  template <typename T>
  inline std::vector<Ptr<T>> const& View<T>::ptrs() const {
    return vPtrs_;
  }

  template <typename T>
  inline typename View<T>::const_reference View<T>::front() const {
    return *items_.front();
  }

  template <typename T>
  inline typename View<T>::const_reference View<T>::back() const {
    return *items_.back();
  }

  // The following is for testing only.
  template <typename T>
  inline void View<T>::fill_from_range(T* first, T* last, View& output) {
    output.items_.resize(std::distance(first, last));
    for (typename View<T>::size_type i = 0; first != last; ++i, ++first)
      output.items_[i] = first;
  }

  template <typename T>
  std::unique_ptr<ViewBase> View<T>::doClone() const {
    return std::unique_ptr<ViewBase>{new View(*this)};
  }

  template <typename T>
  inline bool operator==(View<T> const& lhs, View<T> const& rhs) {
    return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
  }

  template <typename T>
  inline bool operator!=(View<T> const& lhs, View<T> const& rhs) {
    return !(lhs == rhs);
  }

  template <typename T>
  inline bool operator<(View<T> const& lhs, View<T> const& rhs) {
    return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  }

  template <typename T>
  inline bool operator<=(View<T> const& lhs, View<T> const& rhs) {
    return !(rhs < lhs);
  }

  template <typename T>
  inline bool operator>(View<T> const& lhs, View<T> const& rhs) {
    return rhs < lhs;
  }

  template <typename T>
  inline bool operator>=(View<T> const& lhs, View<T> const& rhs) {
    return !(lhs < rhs);
  }

  // Free swap function
  template <typename T>
  inline void swap(View<T>& lhs, View<T>& rhs) {
    lhs.swap(rhs);
  }
}  // namespace edm

#endif