EventSet.hpp 7.93 KB
Newer Older
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
/*
 * (C) 2018 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
#ifndef __HEPNOS_EVENT_SET_H
#define __HEPNOS_EVENT_SET_H

#include <memory>
#include <hepnos/Exception.hpp>
#include <hepnos/DataStore.hpp>
#include <hepnos/EventNumber.hpp>
#include <hepnos/RunNumber.hpp>
#include <hepnos/SubRunNumber.hpp>

namespace hepnos {

class EventSetImpl;

/**
 * @brief The EventSet class is a helper class to access Events
 * stored down in a particular DataSet or Run (bypassing
 * intermediate nesting levels).
 */
class EventSet {

    friend class DataSet;

    private:

    /**
     * @brief Implementation class (used for the Pimpl idiom).
     */
    std::shared_ptr<EventSetImpl> m_impl; /*!< Pointer to implementation. */

    /**
     * @brief Constructor.
     */
    EventSet(const std::shared_ptr<EventSetImpl>& impl);
    EventSet(std::shared_ptr<EventSetImpl>&& impl);

    public:

    typedef Event value_type;

    /**
     * @brief Copy-constructor.
     *
     * @param other EventSet to copy.
     */
    EventSet(const EventSet& other) = default;

    /**
     * @brief Move-constructor. Deleted.
     *
     * @param other EventSet to move.
     */
    EventSet(EventSet&& other) = default;

    /**
     * @brief Copy-assignment operator.
     *
     * @param other EventSet to copy.
     *
     * @return this.
     */
    EventSet& operator=(const EventSet& other) = default;

    /**
     * @brief Move-assignment operator. Deleted.
     *
     * @param other EventSet to move.
     *
     * @return this.
     */
    EventSet& operator=(EventSet&& other) = default;

    /**
     * @brief Destructor.
     */
    ~EventSet() = default;

    class const_iterator;
    class iterator;

    /**
     * @brief Get the DataStore to which this EventSet belongs.
     */
    DataStore datastore() const;

    /**
     * @brief Returns an iterator referring to the first Event
     * in this EventSet.
     *
     * @return an iterator referring to the first Event in this EventSet.
     */
    iterator begin();

    /**
     * @brief Returns an iterator referring to the end of the EventSet.
     * The EventSet pointed to by this iterator is not valid (that is,
     * `end()->valid()` returns `false`).
     *
     * @return an iterator referring to the end of the EventSet.
     */
    iterator end();

    /**
     * @brief Returns a const_iterator referring to the first Event
     * in this EventSet.
     *
     * @return an iterator referring to the first Event in this EventSet.
     */
    const_iterator begin() const;

    /**
     * @brief Returns a const_iterator referring to the end of the EventSet.
     * The EventSet pointed to by this iterator is not valid (that is,
     * `end()->valid()` returns `false`).
     *
     * @return a const_iterator referring to the end of the EventSet.
     */
    const_iterator end() const;

    /**
     * @brief Returns a const_iterator referring to the first Event
     * in this EventSet.
     *
     * @return a const_iterator referring to the first Event in this EventSet.
     */
    const_iterator cbegin() const;

    /**
     * @brief Returns a const_iterator referring to the end of the EventSet.
     * The EventSet pointed to by this iterator is not valid (that is,
     * `cend()->valid()` return `false`).
     *
     * @return a const_iterator referring to the end of the EventSet.
     */
    const_iterator cend() const;

};

class EventSet::const_iterator {

146 147
    friend class EventSet;

148 149 150 151 152 153 154 155
    protected:

    /**
     * @brief Implementation of the class (using Pimpl idiom)
     */
    class Impl;
    std::unique_ptr<Impl> m_impl; /*!< Pointer to implementation */

156 157
    const_iterator(std::unique_ptr<Impl>&& impl);

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
    public:
    /**
     * @brief Constructor. Creates a const_iterator pointing
     * to an invalid Event.
     */
    const_iterator();

    typedef const_iterator self_type;
    typedef Event value_type;
    typedef Event& reference;
    typedef Event* pointer;
    typedef int difference_type;
    typedef std::forward_iterator_tag iterator_category;

    /**
     * @brief Destructor. This destructor is virtual because
     * the iterator class inherits from const_iterator.
     */
    virtual ~const_iterator();

    /**
     * @brief Copy-constructor.
     *
     * @param other const_iterator to copy.
     */
    const_iterator(const const_iterator& other);

    /**
     * @brief Move-constructor.
     *
     * @param other const_iterator to move.
     */
    const_iterator(const_iterator&& other);

    /**
     * @brief Copy-assignment operator.
     *
     * @param other const_iterator to copy.
     *
     * @return this.
     */
    const_iterator& operator=(const const_iterator&);

    /**
     * @brief Move-assignment operator.
     *
     * @param other const_iterator to move.
     *
     * @return this.
     */
    const_iterator& operator=(const_iterator&&);

    /**
     * @brief Increments the const_iterator, returning
     * a copy of the iterator after incrementation.
     *
     * @return a copy of the iterator after incrementation.
     */
    self_type operator++();

    /**
     * @brief Increments the const_iterator, returning
     * a copy of the iterator before incrementation.
     *
     * @return a copy of the iterator after incrementation.
     */
    self_type operator++(int);

    /**
     * @brief Dereference operator. Returns a const reference
     * to the DataSet this const_iterator points to.
     *
     * @return a const reference to the DataSet this 
     *      const_iterator points to.
     */
    const reference operator*();

    /**
     * @brief Returns a const pointer to the DataSet this
     * const_iterator points to.
     *
     * @return a const pointer to the DataSet this 
     *      const_iterator points to.
     */
    const pointer operator->();

    /**
     * @brief Compares two const_iterators. The two const_iterators
     * are equal if they point to the same DataSet or if both
     * correspond to EventSet::cend().
     *
     * @param rhs const_iterator to compare with.
     *
     * @return true if the two const_iterators are equal, false otherwise.
     */
    bool operator==(const self_type& rhs) const;

    /**
     * @brief Compares two const_iterators.
     *
     * @param rhs const_iterator to compare with.
     *
     * @return true if the two const_iterators are different, false atherwise.
     */
    bool operator!=(const self_type& rhs) const;

};

class EventSet::iterator : public EventSet::const_iterator {

268 269 270 271 272 273
    friend class EventSet;

    private:

    iterator(std::unique_ptr<EventSet::const_iterator::Impl>&& impl);

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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    public:

    /**
     * @brief Constructor. Builds an iterator pointing to an
     * invalid Event.
     */
    iterator();

    typedef iterator self_type;
    typedef Event value_type;
    typedef Event& reference;
    typedef Event* pointer;
    typedef int difference_type;
    typedef std::forward_iterator_tag iterator_category;

    /**
     * @brief Destructor.
     */
    ~iterator();

    /**
     * @brief Copy constructor.
     *
     * @param other iterator to copy.
     */
    iterator(const iterator& other);

    /**
     * @brief Move constructor.
     *
     * @param other iterator to move.
     */
    iterator(iterator&& other);

    /**
     * @brief Copy-assignment operator.
     *
     * @param other iterator to copy.
     *
     * @return this.
     */
    iterator& operator=(const iterator& other);

    /**
     * @brief Move-assignment operator.
     *
     * @param other iterator to move.
     *
     * @return this.
     */
    iterator& operator=(iterator&& other);

    /**
     * @brief Dereference operator. Returns a reference
     * to the DataSet this iterator points to.
     *
     * @return A reference to the DataSet this iterator
     *      points to.
     */
    reference operator*();

    /**
     * @brief Returns a pointer to the DataSet this iterator
     * points to.
     *
     * @return A pointer to the DataSet this iterator points to.
     */
    pointer operator->();

};

}

#endif