OpenMS  2.4.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-2018.
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 
83  unique_id_(rhs.unique_id_)
84  {
85  }
86 
90  {
91  unique_id_ = rhs.unique_id_;
92  return *this;
93  }
94 
97  {
98  }
99 
101  bool
102  operator==(UniqueIdInterface const & rhs) const
103  {
104  return unique_id_ == rhs.unique_id_;
105  }
106 
108  UInt64
109  getUniqueId() const
110  {
111  return unique_id_;
112  }
113 
115  Size
117  {
118  if (hasValidUniqueId())
119  {
120  unique_id_ = 0;
121  return 1;
122  }
123  else
124  return 0;
125  }
126 
127  void
129  {
130  std::swap(unique_id_, from.unique_id_);
131  return;
132  }
133 
135  Size
137  {
138  return isValid(unique_id_);
139  }
140 
142  Size
144  {
145  return !isValid(unique_id_);
146  }
147 
149  Size
151  {
152  unique_id_ = UniqueIdGenerator::getUniqueId();
153  return 1;
154  }
155 
157  Size
159  {
160  if (!hasValidUniqueId())
161  {
162  unique_id_ = UniqueIdGenerator::getUniqueId();
163  return 1;
164  }
165  else
166  return 0;
167  }
168 
170  void
172  {
173  unique_id_ = rhs;
174  }
175 
183  void
184  setUniqueId(const String & rhs);
185 
186 protected:
189 
190  };
191 
192 } //namespace OpenMS
193 
A more convenient string class.
Definition: String.h:57
Size setUniqueId()
Assigns a new, valid unique id. Always returns 1.
Definition: UniqueIdInterface.h:150
UniqueIdInterface()
Default constructor - the unique id will be invalid
Definition: UniqueIdInterface.h:75
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
Size clearUniqueId()
Clear the unique id. The new unique id will be invalid. Returns 1 if the unique id was changed...
Definition: UniqueIdInterface.h:116
Size hasInvalidUniqueId() const
Returns whether the unique id is invalid. Returns 1 if the unique id is invalid, 0 otherwise...
Definition: UniqueIdInterface.h:143
static bool isValid(UInt64 unique_id)
Returns true if the unique_id is valid, false otherwise.
Definition: UniqueIdInterface.h:69
void setUniqueId(UInt64 rhs)
Assigns the given unique id.
Definition: UniqueIdInterface.h:171
static UInt64 getUniqueId()
Returns a new unique id.
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
UniqueIdInterface(const UniqueIdInterface &rhs)
Copy constructor - copies the unique id.
Definition: UniqueIdInterface.h:82
A base class defining a common interface for all classes having a unique id.
Definition: UniqueIdInterface.h:51
bool operator==(UniqueIdInterface const &rhs) const
Equality comparison operator - the unique ids must be equal (!)
Definition: UniqueIdInterface.h:102
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
UInt64 unique_id_
the unique id
Definition: UniqueIdInterface.h:188
virtual ~UniqueIdInterface()
Destructor.
Definition: UniqueIdInterface.h:96
void swap(UniqueIdInterface &from)
Definition: UniqueIdInterface.h:128
UniqueIdInterface & operator=(UniqueIdInterface const &rhs)
Assignment operator - copies the unique id.
Definition: UniqueIdInterface.h:89
Size hasValidUniqueId() const
Returns whether the unique id is valid. Returns 1 if the unique id is valid, 0 otherwise.
Definition: UniqueIdInterface.h:136
UInt64 getUniqueId() const
Non-mutable access to unique id - returns the unique id.
Definition: UniqueIdInterface.h:109
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:158