OpenMS  2.6.0
UniqueIdInterface.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2020.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
39 
40 namespace OpenMS
41 {
42 
51  class OPENMS_DLLAPI UniqueIdInterface
52  {
53 public:
54 
59  enum
60  {
61  INVALID = 0
62  };
63 
68  inline static bool
69  isValid(UInt64 unique_id)
70  {
71  return unique_id != INVALID;
72  }
73 
76  unique_id_(UInt64(INVALID))
77  {
78  //do not use clearUniqueId(), as it will be slower and creates a warranted valgrind warning;
79  }
80 
82  UniqueIdInterface(const UniqueIdInterface & rhs) = default;
83 
85  UniqueIdInterface(UniqueIdInterface && rhs) = default;
86 
88  UniqueIdInterface & operator=(UniqueIdInterface const & rhs) = default;
89 
91  UniqueIdInterface& operator=(UniqueIdInterface&&) & = default;
92 
94  virtual ~UniqueIdInterface() = default;
95 
97  bool
98  operator==(UniqueIdInterface const & rhs) const
99  {
100  return unique_id_ == rhs.unique_id_;
101  }
102 
104  UInt64
105  getUniqueId() const
106  {
107  return unique_id_;
108  }
109 
111  Size
113  {
114  if (hasValidUniqueId())
115  {
116  unique_id_ = 0;
117  return 1;
118  }
119  else
120  return 0;
121  }
122 
123  void
125  {
126  std::swap(unique_id_, from.unique_id_);
127  return;
128  }
129 
131  Size
133  {
134  return isValid(unique_id_);
135  }
136 
138  Size
140  {
141  return !isValid(unique_id_);
142  }
143 
145  Size
147  {
148  unique_id_ = UniqueIdGenerator::getUniqueId();
149  return 1;
150  }
151 
153  Size
155  {
156  if (!hasValidUniqueId())
157  {
158  unique_id_ = UniqueIdGenerator::getUniqueId();
159  return 1;
160  }
161  else
162  return 0;
163  }
164 
166  void
168  {
169  unique_id_ = rhs;
170  }
171 
179  void
180  setUniqueId(const String & rhs);
181 
182 protected:
185 
186  };
187 
188 } //namespace OpenMS
189 
OpenMS::UniqueIdInterface::ensureUniqueId
Size ensureUniqueId()
Assigns a valid unique id, but only if the present one is invalid. Returns 1 if the unique id was cha...
Definition: UniqueIdInterface.h:154
OpenMS::UInt64
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
OpenMS::UniqueIdInterface::hasValidUniqueId
Size hasValidUniqueId() const
Returns whether the unique id is valid. Returns 1 if the unique id is valid, 0 otherwise.
Definition: UniqueIdInterface.h:132
OpenMS::String
A more convenient string class.
Definition: String.h:59
OpenMS::UniqueIdInterface
A base class defining a common interface for all classes having a unique id.
Definition: UniqueIdInterface.h:51
OpenMS::UniqueIdInterface::clearUniqueId
Size clearUniqueId()
Clear the unique id. The new unique id will be invalid. Returns 1 if the unique id was changed,...
Definition: UniqueIdInterface.h:112
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::UniqueIdInterface::hasInvalidUniqueId
Size hasInvalidUniqueId() const
Returns whether the unique id is invalid. Returns 1 if the unique id is invalid, 0 otherwise.
Definition: UniqueIdInterface.h:139
OpenMS::UniqueIdInterface::UniqueIdInterface
UniqueIdInterface()
Default constructor - the unique id will be invalid
Definition: UniqueIdInterface.h:75
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
UniqueIdGenerator.h
OpenMS::UniqueIdInterface::setUniqueId
Size setUniqueId()
Assigns a new, valid unique id. Always returns 1.
Definition: UniqueIdInterface.h:146
OpenMS::UniqueIdGenerator::getUniqueId
static UInt64 getUniqueId()
Returns a new unique id.
OpenMS::UniqueIdInterface::swap
void swap(UniqueIdInterface &from)
Definition: UniqueIdInterface.h:124
String.h
OpenMS::UniqueIdInterface::operator==
bool operator==(UniqueIdInterface const &rhs) const
Equality comparison operator - the unique ids must be equal (!)
Definition: UniqueIdInterface.h:98
OpenMS::UniqueIdInterface::isValid
static bool isValid(UInt64 unique_id)
Returns true if the unique_id is valid, false otherwise.
Definition: UniqueIdInterface.h:69
OpenMS::UniqueIdInterface::setUniqueId
void setUniqueId(UInt64 rhs)
Assigns the given unique id.
Definition: UniqueIdInterface.h:167
OpenMS::UniqueIdInterface::unique_id_
UInt64 unique_id_
the unique id
Definition: UniqueIdInterface.h:184
OpenMS::UniqueIdInterface::getUniqueId
UInt64 getUniqueId() const
Non-mutable access to unique id - returns the unique id.
Definition: UniqueIdInterface.h:105