DataStore.hpp 11.9 KB
Newer Older
1 2 3
#ifndef __HEPNOS_DATA_STORE_H
#define __HEPNOS_DATA_STORE_H

4
#include <vector>
5 6 7 8 9 10 11
#include <string>
#include <memory>

namespace hepnos {

class DataSet;

Matthieu Dorier's avatar
Matthieu Dorier committed
12 13 14 15
/**
 * The DataStore class is the main handle referencing an HEPnOS service.
 * It provides functionalities to navigate DataSets.
 */
16 17
class DataStore {

18 19
    friend class DataSet;

20 21
    public:

Matthieu Dorier's avatar
Matthieu Dorier committed
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
    /**
     * @brief Constructor. Initializes the DataStore using a YAML
     * configuration file (this file is generated by the HEPnOS
     * service when started.
     *
     * @param configFile Path to a YAML configuration file.
     */
    DataStore(const std::string& configFile);

    /**
     * @brief Copy constructor is deleted.
     */
    DataStore(const DataStore&) = delete;

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

    /**
     * @brief Copy-assignment operator. Deleted.
     */
    DataStore& operator=(const DataStore&) = delete;

    /**
     * @brief Move-assignment operator.
     *
     * @param other DataStore to move from.
     *
     * @return This DataStore.
     */
    DataStore& operator=(DataStore&& other);
56
    
Matthieu Dorier's avatar
Matthieu Dorier committed
57 58 59 60 61 62
    /**
     * @brief Destructor.
     */
    ~DataStore();

    /**
63
     * @brief Accesses an existing DataSet using the []
Matthieu Dorier's avatar
Matthieu Dorier committed
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
     * operator. If no DataSet correspond to the provided name,
     * the function returns a DataSet instance d such that
     * d.valid() is false.
     *
     * @param datasetName Name of the DataSet to retrieve.
     *
     * @return a DataSet corresponding to the provided name.
     */
    DataSet operator[](const std::string& datasetName) const;

    /**
     * @brief iterator class to navigate DataSets.
     * This iterator is a forward iterator. DataSets are sorted
     * alphabetically inside the DataStore.
     */
    class iterator;

    /**
     * @brief const_iterator class to navigate DataSets.
     * This iterator is a forward iterator. DataSets are sorted
     * alphabetically inside the DataStore.
     */
    class const_iterator;

    /**
     * @brief Searches the DataStore for an DataSet with 
     * the provided name and returns an iterator to it if found,
     * otherwise it returns an iterator to DataStore::end().
     *
     * @param datasetName Name of the DataSet to find.
     *
     * @return an iterator pointing to the DataSet if found,
     * DataStore::end() otherwise.
     */
    iterator find(const std::string& datasetName);

    /**
     * @brief Searches the DataStore for an DataSet with 
     * the provided name and returns a const_iterator to it 
     * if found, otherwise it returns an iterator to DataStore::end().
     *
     * @param datasetName Name of the DataSet to find.
     *
     * @return a const_iterator pointing to the DataSet if found,
     * DataStore::cend() otherwise.
     */
    const_iterator find(const std::string& datasetName) const;

    /**
     * @brief Returns an iterator referring to the first DataSet
     * in the DataStore.
     *
     * @return an iterator referring to the first DataSet in the DataStore.
     */
    iterator begin();

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

    /**
     * @brief Returns a const_iterator referring to the first DataSet
     * in the DataStore.
     *
     * @return a const_iterator referring to the first DataSet in the DataStore.
     */
    const_iterator cbegin() const;

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

    /**
     * @brief Returns an iterator pointing to the first DataSet in the 
     * DataStore whose name is not considered to go before lb 
     * (i.e., either it is equal or goes after, alphabetically).
     *
     * @param lb DataSet name to search for.
     *
     * @return An iterator to the the first DataSet in the DataStore 
     * whose name is not considered to go before lb, or DataStore::end() 
     * if all keys are considered to go before it.
     */
    iterator lower_bound(const std::string& lb);

    /**
     * @brief Returns a const_iterator pointing to the first DataSet in the 
     * DataStore whose name is not considered to go before lb 
     * (i.e., either it is equal or goes after, alphabetically).
     *
     * @param lb DataSet name to search for.
     *
     * @return A const_iterator to the the first DataSet in the DataStore 
     * whose name is not considered to go before lb, or DataStore::cend() 
     * if all DataSet names are considered to go before it.
     */
    const_iterator lower_bound(const std::string& lb) const;

    /**
     * @brief Returns an iterator pointing to the first DataSet in the 
     * DataStore whose key is considered to go after ub.
     *
     * @param ub DataSet name to search for.
     *
     * @return An iterator to the the first DataSet in the DataStore 
     * whose name is considered to go after ub, or DataStore::end() if 
     * no DataSet names are considered to go after it.
     */
    iterator upper_bound(const std::string& ub);

    /**
     * @brief Returns a const_iterator pointing to the first DataSet in the 
     * DataStore whose key is considered to go after ub.
     *
     * @param ub DataSet name to search for.
     *
     * @return A const_iterator to the the first DataSet in the DataStore 
     * whose name is considered to go after ub, or DataStore::end() if 
     * no DataSet names are considered to go after it.
     */
    const_iterator upper_bound(const std::string& ub) const;

    /**
     * @brief Creates a dataset with a given name inside the
     * DataStore. This name must not have the '/' and '#' characters.
     * A DataSet object pointing to the created dataset is returned.
     * If a dataset with this name already exists in the DataStore, 
     * it is not created, but a DataSet object pointing to the 
     * existing one is returned instead.
     *
     * @param name Name of DataSet.
     *
     * @return A DataSet instance pointing to the created dataset.
     */
    DataSet createDataSet(const std::string& name);
209 210 211

    private:

Matthieu Dorier's avatar
Matthieu Dorier committed
212 213 214 215 216
    /**
     * @brief Implementation of the class (using Pimpl idiom)
     */
    class Impl;
    std::unique_ptr<Impl> m_impl; /*!< Pointer to implementation */
217 218 219 220 221 222
};

class DataStore::const_iterator {

    protected:

Matthieu Dorier's avatar
Matthieu Dorier committed
223 224 225 226 227 228
    /**
     * @brief Implementation of the class (using Pimpl idiom)
     */
    class Impl;
    std::unique_ptr<Impl> m_impl; /*!< Pointer to implementation */

229
    public:
Matthieu Dorier's avatar
Matthieu Dorier committed
230 231 232
    /**
     * @brief Constructor. Creates a const_iterator pointing
     * to an invalid DataSet.
233 234 235 236 237 238
     */
    const_iterator();

    /**
     * @brief Constructor. Creates a const_iterator pointing
     * to a given DataSet. The DataSet may or may not be valid. 
Matthieu Dorier's avatar
Matthieu Dorier committed
239
     *
240
     * @param current DataSet to make the const_iterator point to.
Matthieu Dorier's avatar
Matthieu Dorier committed
241
     */
242
    const_iterator(const DataSet& current);
Matthieu Dorier's avatar
Matthieu Dorier committed
243 244 245 246 247 248 249

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

Matthieu Dorier's avatar
Matthieu Dorier committed
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
    typedef const_iterator self_type;
    typedef DataSet value_type;
    typedef DataSet& reference;
    typedef DataSet* 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 DataStore::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;
350 351 352 353
};

class DataStore::iterator : public DataStore::const_iterator {

354
    public:
355

Matthieu Dorier's avatar
Matthieu Dorier committed
356 357 358
    /**
     * @brief Constructor. Builds an iterator pointing to an
     * invalid DataSet.
359 360 361 362 363 364 365
     */
    iterator();

    /**
     * @brief Constructor. Builds an iterator pointing to
     * an existing DataSet. The DataSet may or may not be
     * valid.
Matthieu Dorier's avatar
Matthieu Dorier committed
366
     *
367
     * @param current DataSet to point to.
Matthieu Dorier's avatar
Matthieu Dorier committed
368
     */
369
    iterator(const DataSet& current);
Matthieu Dorier's avatar
Matthieu Dorier committed
370 371 372 373 374 375 376 377

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

Matthieu Dorier's avatar
Matthieu Dorier committed
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
    typedef iterator self_type;
    typedef DataSet value_type;
    typedef DataSet& reference;
    typedef DataSet* 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->();
440 441 442 443 444
};

}

#endif