OpenMS  3.0.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-2022.
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 
38 
39 namespace OpenMS
40 {
41 
50  class OPENMS_DLLAPI UniqueIdInterface
51  {
52 public:
53 
58  enum
59  {
60  INVALID = 0
61  };
62 
67  inline static bool
68  isValid(UInt64 unique_id)
69  {
70  return unique_id != INVALID;
71  }
72 
75  unique_id_(UInt64(INVALID))
76  {
77  //do not use clearUniqueId(), as it will be slower and creates a warranted valgrind warning;
78  }
79 
81  UniqueIdInterface(const UniqueIdInterface & rhs) = default;
82 
84  UniqueIdInterface(UniqueIdInterface && rhs) = default;
85 
87  UniqueIdInterface & operator=(UniqueIdInterface const & rhs) = default;
88 
90  UniqueIdInterface& operator=(UniqueIdInterface&&) & = default;
91 
93  virtual ~UniqueIdInterface() = default;
94 
96  bool
97  operator==(UniqueIdInterface const & rhs) const
98  {
99  return unique_id_ == rhs.unique_id_;
100  }
101 
103  UInt64
104  getUniqueId() const
105  {
106  return unique_id_;
107  }
108 
110  Size
112  {
113  if (hasValidUniqueId())
114  {
115  unique_id_ = 0;
116  return 1;
117  }
118  else
119  return 0;
120  }
121 
122  void
124  {
125  std::swap(unique_id_, from.unique_id_);
126  return;
127  }
128 
130  Size
132  {
133  return isValid(unique_id_);
134  }
135 
137  Size
139  {
140  return !isValid(unique_id_);
141  }
142 
144  Size
145  setUniqueId();
146 
148  Size
149  ensureUniqueId();
150 
152  void
154  {
155  unique_id_ = rhs;
156  }
157 
165  void
166  setUniqueId(const String & rhs);
167 
168 protected:
171 
172  };
173 
174 } //namespace OpenMS
175 
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:131
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::UniqueIdInterface
A base class defining a common interface for all classes having a unique id.
Definition: UniqueIdInterface.h:50
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:111
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:138
OpenMS::UniqueIdInterface::UniqueIdInterface
UniqueIdInterface()
Default constructor - the unique id will be invalid
Definition: UniqueIdInterface.h:74
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
OpenMS::UniqueIdInterface::swap
void swap(UniqueIdInterface &from)
Definition: UniqueIdInterface.h:123
String.h
OpenMS::UniqueIdInterface::operator==
bool operator==(UniqueIdInterface const &rhs) const
Equality comparison operator - the unique ids must be equal (!)
Definition: UniqueIdInterface.h:97
OpenMS::UniqueIdInterface::isValid
static bool isValid(UInt64 unique_id)
Returns true if the unique_id is valid, false otherwise.
Definition: UniqueIdInterface.h:68
OpenMS::UniqueIdInterface::setUniqueId
void setUniqueId(UInt64 rhs)
Assigns the given unique id.
Definition: UniqueIdInterface.h:153
OpenMS::UniqueIdInterface::unique_id_
UInt64 unique_id_
the unique id
Definition: UniqueIdInterface.h:170
OpenMS::UniqueIdInterface::getUniqueId
UInt64 getUniqueId() const
Non-mutable access to unique id - returns the unique id.
Definition: UniqueIdInterface.h:104