CatalogueItor.hpp 3.14 KB
Newer Older
1
/*
2
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2015-2021 CERN
4
5
6
7
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
13
 *
14
15
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18
19
 */

#pragma once

20
#include "catalogue/CatalogueItorImpl.hpp"
21
#include "common/exception/Exception.hpp"
22
23
24
25

namespace cta {
namespace catalogue {

26
/**
27
28
 * A wrapper around an object that iterators over a list of items retrieved
 * from the CTA catalogue.
29
30
31
 *
 * This wrapper permits the user of the Catalogue API to use different
 * iterator implementations whilst only using a single iterator type.
32
 */
33
34
template <typename Item>
class CatalogueItor {
35
36
public:

37
  typedef CatalogueItorImpl<Item> Impl;
38

39
40
41
  /**
   * Constructor.
   */
42
43
44
  CatalogueItor():
    m_impl(nullptr) {
  }
45
46
47
48
49
50

  /**
   * Constructor.
   *
   * @param impl The object actually implementing this iterator.
   */
51
52
53
54
55
56
  CatalogueItor(Impl *const impl):
    m_impl(impl) {
    if(nullptr == impl) {
      throw exception::Exception(std::string(__FUNCTION__) + " failed: Pointer to implementation object is null");
    }
  }
57
58
59
60

  /**
   * Deletion of copy constructor.
   */
61
  CatalogueItor(const CatalogueItor &) = delete;
62
63
64
65
66
67

  /**
   * Move constructor.
   *
   * @param other The other object to be moved.
   */
68
69
70
71
  CatalogueItor(CatalogueItor &&other):
    m_impl(other.m_impl) {
    other.m_impl = nullptr;
  }
72

73
74
75
  /**
   * Destructor.
   */
76
77
78
  ~CatalogueItor() {
    delete m_impl;
  }
79
80
81
82

  /**
   * Deletion of copy assignment.
   */
83
  CatalogueItor &operator=(const CatalogueItor &) = delete;
84
85
86
87

  /**
   * Move assignment.
   */
88
89
90
91
92
93
94
95
96
97
98
  CatalogueItor &operator=(CatalogueItor &&rhs) {
    // Protect against self assignment
    if(this != &rhs) {
      // Avoid memory leak
      delete m_impl;

      m_impl = rhs.m_impl;
      rhs.m_impl = nullptr;
    }
    return *this;
  }
99
100

  /**
101
   * Returns true if a call to next would return another item.
102
   */
103
104
105
106
107
108
  bool hasMore() const {
    if(nullptr == m_impl) {
      throw exception::Exception(std::string(__FUNCTION__) + " failed: This iterator is invalid");
    }
    return m_impl->hasMore();
  }
109
110

  /**
111
   * Returns the next item or throws an exception if there isn't one.
112
   */
113
114
115
116
117
118
  Item next() {
    if(nullptr == m_impl) {
      throw exception::Exception(std::string(__FUNCTION__) + " failed: This iterator is invalid");
    }
    return m_impl->next();
  }
119
120
121
122
123
124

private:

  /**
   * The object actually implementing this iterator.
   */
125
  Impl *m_impl;
126

127
}; // class CatalogueItor
128
129
130

} // namespace catalogue
} // namespace cta