Run.hpp 13.9 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5
/*
 * (C) 2018 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
6 7 8 9 10
#ifndef __HEPNOS_RUN_H
#define __HEPNOS_RUN_H

#include <memory>
#include <string>
Matthieu Dorier's avatar
Matthieu Dorier committed
11
#include <hepnos/KeyValueContainer.hpp>
12 13
#include <hepnos/DataStore.hpp>
#include <hepnos/RunNumber.hpp>
14 15
#include <hepnos/SubRun.hpp>
#include <hepnos/Exception.hpp>
16 17 18 19 20

namespace hepnos {

class RunSet;

Matthieu Dorier's avatar
Matthieu Dorier committed
21
class Run : public KeyValueContainer {
22 23 24 25 26 27 28 29 30

    private:

    friend class RunSet;
    friend class DataSet;

    class Impl;
    std::unique_ptr<Impl> m_impl;

31 32 33 34 35 36 37 38
    /**
     * @brief Constructor.
     *
     * @param datastore Pointer to the DataStore managing the underlying data.
     * @param level Level of nesting.
     * @param container Full name of the dataset containing the run.
     * @param run Run number.
     */
39 40 41 42
    Run(DataStore* datastore, uint8_t level, const std::string& container, const RunNumber& run);

    public:

43 44 45
    /**
     * @brief Default constructor. Creates a Run instance such that run.valid() is false.
     */
46 47
    Run();

48 49 50 51 52 53
    /**
     * @brief Copy constructor.
     *
     * @param other Run to copy.
     */
    Run(const Run& other);
54

55 56 57 58 59 60
    /**
     * @brief Move constructor.
     *
     * @param other Run to move.
     */
    Run(Run&& other);
61

62 63 64 65 66 67 68 69
    /**
     * @brief Copy-assignment operator.
     *
     * @param other Run to assign.
     *
     * @return Reference to this Run.
     */
    Run& operator=(const Run& other);
70

71 72 73 74 75 76 77 78
    /**
     * @brief Move-assignment operator.
     *
     * @param other Run to move from.
     *
     * @return Reference to this Run.
     */
    Run& operator=(Run&& other);
79

80 81 82
    /**
     * @brief Destructor.
     */
83 84
    ~Run();

85 86 87 88 89 90 91
    /**
     * @brief Returns the next Run in the same container,
     * sorted by run number. If no such run exists, a Run instance
     * such that Run::valid() returns false is returned.
     *
     * @return The next Run in the container.
     */
92 93
    Run next() const;

94 95 96 97 98 99 100
    /**
     * @brief Indicates whether this Run instance points to a valid
     * run in the underlying storage service.
     *
     * @return True if the Run instance points to a valid run in the
     * underlying service, false otherwise.
     */
101 102
    bool valid() const;

103 104 105 106 107 108 109 110
    /**
     * @brief Stores raw key/value data in this Run.
     *
     * @param key Key
     * @param buffer Value
     *
     * @return true if the key did not already exist, false otherwise.
     */
111 112
    bool storeRawData(const std::string& key, const std::vector<char>& buffer);

113 114 115 116 117 118 119 120
    /**
     * @brief Loads raw key/value data from this Run.
     *
     * @param key Key
     * @param buffer Buffer used to hold the value.
     *
     * @return true if the key exists, false otherwise.
     */
121 122
    bool loadRawData(const std::string& key, std::vector<char>& buffer) const;

123 124 125 126 127 128 129 130
    /**
     * @brief Compares this Run with another Run. The Runs must point to
     * the same run number within the same container.
     *
     * @param other Run instance to compare against.
     *
     * @return true if the Runs are the same, false otherwise.
     */
131 132
    bool operator==(const Run& other) const;

133 134 135 136 137 138 139
    /**
     * @brief Compares this Run with another Run.
     *
     * @param other Run instance to compare against.
     *
     * @return true if the Runs are different, false otherwise.
     */
140 141
    bool operator!=(const Run& other) const;

142 143 144 145 146 147
    /**
     * @brief Returns the run number of this Run. Note that if
     * the Run is not valid, this function will return 0.
     *
     * @return The run number.
     */
148 149
    const RunNumber& number() const;

150 151 152 153 154 155
    /**
     * @brief Returns the full name of the DataSet containing
     * this Run.
     *
     * @return the full name of the DataSet containing this Run.
     */
156
    const std::string& container() const;
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

    class const_iterator;
    class iterator;

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

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

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

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

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

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

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

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

    /**
     * @brief Returns an iterator pointing to the first SubRun in this
     * Run, whose SubRunNumber is not lower than lb.
     *
     * @param lb SubRunNumber lower bound to search for.
     *
     * @return An iterator to the first SubRun in this Run
     * whose whose SubRunNumber is not lower than lb, or Run::end() 
     * if all subrun numbers are lower.
     */
    iterator lower_bound(const SubRunNumber&);

    /**
     * @brief Returns a const_iterator pointing to the first SubRun in this
     * Run, whose SubRunNumber is not lower than lb.
     *
     * @param lb SubRunNumber lower bound to search for.
     *
     * @return A const_iterator to the first SubRun in this Run
     * whose whose SubRunNumber is not lower than lb, or Run::cend() 
     * if all subrun numbers are lower.
     */
    const_iterator lower_bound(const SubRunNumber&) const;

    /**
     * @brief Returns an iterator pointing to the first SubRun in the 
     * Run whose SubRunNumber is greater than ub.
     *
     * @param ub SubRunNumber upper bound to search for.
     *
     * @return An iterator to the first SubRun in this Run,
     * whose SubRunNumber is greater than ub, or Run::end() if 
     * no such SubRun exists.
     */
    iterator upper_bound(const SubRunNumber&);

    /**
     * @brief Returns a const_iterator pointing to the first SubRun in the 
     * Run whose SubRunNumber is greater than ub.
     *
     * @param ub SubRunNumber upper bound to search for.
     *
     * @return An const_iterator to the first SubRun in this Run,
     * whose SubRunNumber is greater than ub, or Run::cend() if 
     * no such SubRun exists.
     */
    const_iterator upper_bound(const SubRunNumber&) const;

    /**
285
     * @brief Accesses an existing ubun using the []
286 287 288 289 290 291 292 293
     * operator. If no run corresponds to the provided subrun number,
     * the function returns a SubRun instance r such that
     * r.valid() is false.
     *
     * @param subRunNumber Number of the subrun to retrieve.
     *
     * @return a SubRun corresponding to the provided subrun number.
     */
294
    SubRun operator[](const SubRunNumber& subRunNumber) const;
295 296 297 298 299 300 301 302 303 304 305 306

    /**
     * @brief Creates a SubRun within this Run, with the provided
     * SubRunNumber. If a SubRun with the same SubRunNumber already
     * exists, this method does not create a new SubRun but returns
     * a handle to the existing one instead.
     *
     * @param subRunNumber SubRunNumber of the SubRun to create.
     *
     * @return a handle to the created or existing SubRun.
     */
    SubRun createSubRun(const SubRunNumber& subRunNumber);
307 308
};

Matthieu Dorier's avatar
Matthieu Dorier committed
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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
class Run::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 SubRun.
     */
    const_iterator();

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

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

    typedef const_iterator self_type;
    typedef SubRun value_type;
    typedef SubRun& reference;
    typedef SubRun* 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 SubRun 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 SubRun this
     * const_iterator points to.
     *
     * @return a const pointer to the SubRun 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 SubRun or if both
     * correspond to Run::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 otherwise.
     */
    bool operator!=(const self_type& rhs) const;
};

class Run::iterator : public Run::const_iterator {

    public:

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

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

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

     typedef iterator self_type;
     typedef SubRun value_type;
     typedef SubRun& reference;
     typedef SubRun* 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 SubRun this iterator points to.
      *
      * @return A reference to the SubRun this iterator
      *      points to.
      */
     reference operator*();

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

534 535 536
}

#endif