RunSet.hpp 10.7 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 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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
#ifndef __HEPNOS_RUN_SET_H
#define __HEPNOS_RUN_SET_H

#include <memory>
#include <hepnos/Exception.hpp>
#include <hepnos/DataStore.hpp>
#include <hepnos/DataSet.hpp>
#include <hepnos/RunNumber.hpp>
#include <hepnos/Run.hpp>

namespace hepnos {

/**
 * @brief The RunSet class is a helper class to access Runs
 * stored in a particular DataSet.
 */
class RunSet {

    friend class DataSet::Impl;

    private:

    /**
     * @brief Constructor.
     *
     * @param ds DataSet to which this RunSet belongs.
     */
    RunSet(DataSet* ds);

    /**
     * @brief Implementation class (used for the Pimpl idiom).
     */
    class Impl;

    std::unique_ptr<Impl> m_impl; /*!< Pointer to implementation. */

    public:

    /**
     * @brief Copy-constructor. Deleted.
     *
     * @param other RunSet to copy.
     */
    RunSet(const RunSet& other) = delete;

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

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

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

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

    class const_iterator;
    class iterator;

    /**
     * @brief Searches this RunSet for a Run with 
     * the provided run number and returns an iterator to 
     * it if found, otherwise it returns an iterator pointing 
     * to RunSet::end().
     *
     * @param runNumber Run number of the Run to find.
     *
     * @return an iterator pointing to the Run if found,
     * RunSet::end() otherwise.
     */
    iterator find(const RunNumber& runNumber);

    /**
     * @brief Searches this RunSet for a Run with 
     * the provided run number and returns an const_iterator to 
     * it if found, otherwise it returns an iterator pointing 
     * to RunSet::cend().
     *
     * @param runNumber Run number of the Run to find.
     *
     * @return a const_iterator pointing to the Run if found,
     * RunSet::cend() otherwise.
     */
    const_iterator find(const RunNumber& runNumber) const;


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

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

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

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

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

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

    /**
     * @brief Returns an iterator pointing to the first Run in this
     * RunSet, whose number is equal or greater than lb.
     *
     * @param lb Run number to search for.
     *
     * @return An iterator to the the first Run in this RunSet 
     * whose number is equal or greater than, lb or RunSet::end() 
     * if such a Run does not exist.
     */
    iterator lower_bound(const RunNumber& lb);

    /**
     * @brief Returns a const_iterator pointing to the first Run in this
     * RunSet whose number is equal or greater than lb.
     *
     * @param lb Run number to search for.
     *
     * @return A const_iterator to the the first Run in this RunSet 
     * whose number is equal or greater than, lb or RunSet::end() 
     * if such a Run does not exist.
     */
    const_iterator lower_bound(const RunNumber& lb) const;

    /**
     * @brief Returns an iterator pointing to the first Run in the 
     * RunSet whose number is strictly greater than ub.
     *
     * @param ub Run number to search for.
     *
     * @return An iterator to the the first Run in this RunSet,
     * whose number is stricly greater than ub, or RunSet::end() if 
     * no such a Run exist.
     */
    iterator upper_bound(const RunNumber& ub);

    /**
     * @brief Returns a const_iterator pointing to the first Run in the 
     * RunSet whose number is strictly greater than ub.
     *
     * @param ub Run number to search for.
     *
     * @return A const_iterator to the the first Run in this RunSet,
     * whose number is stricly greater than ub, or RunSet::end() if 
     * no such a Run exist.
     */
    const_iterator upper_bound(const RunNumber& ub) const;
};

class RunSet::const_iterator {

    protected:

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

    public:
    /**
     * @brief Constructor. Creates a const_iterator pointing
     * to an invalid Run.
     */
    const_iterator();

    /**
     * @brief Constructor. Creates a const_iterator pointing
     * to a given Run. The Run may or may not be valid. 
     *
     * @param current Run to make the const_iterator point to.
     */
    const_iterator(const Run& current);

    /**
     * @brief Constructor. Creates a const_iterator pointing
     * to a given Run. The Run may or may not be valid. 
     *
     * @param current Run to make the const_iterator point to.
     */
    const_iterator(Run&& current);

    typedef const_iterator self_type;
    typedef Run value_type;
    typedef Run& reference;
    typedef Run* 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 RunSet::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 RunSet::iterator : public RunSet::const_iterator {

    public:

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

    /**
     * @brief Constructor. Builds an iterator pointing to
     * an existing Run. The Run may or may not be valid.
     *
     * @param current Run to point to.
     */
    iterator(const Run& current);

    /**
     * @brief Constructor. Builds an iterator pointing to
     * an existing Run. The Run may or may not be valid.
     *
     * @param current DataSet to point to.
     */
    iterator(Run&& current);

    typedef iterator self_type;
    typedef Run value_type;
    typedef Run& reference;
    typedef Run* 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