Victor
 All Data Structures Functions Variables Friends Pages
Identity.h
1 /* This file is part of Victor.
2 
3  Victor is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 3 of the License, or
6  (at your option) any later version.
7 
8  Victor is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with Victor. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 
18 #ifndef __IDENTITY_H__
19 #define __IDENTITY_H__
20 
21 // Includes:
22 
23 #include <iostream>
24 #include <string>
25 #include <Debug.h>
26 
27 
28 namespace Victor { namespace Biopool {
29 
30 
31 
32 
36  class Identity {
37  public:
38 
39 
40  Identity(const string& name = "", long number = 0);
41  Identity(const Identity& orig);
42 
43  /* OPERATORS */
44 
46  Identity& operator =(const Identity& orig);
47 
49  bool operator ==(const Identity& other) const;
50 
51  bool operator !=(const Identity& other) const {
52  return !((*this) == other);
53  };
54 
56  operator long() const;
57 
59  // operator string() const;
60 
62  operator const string&() const;
63 
65  void setName(string _name);
66 
68  void setNumber(long _number);
69 
70 
71 
72  /* FRIENDS */
73 
74  friend ostream& operator <<(ostream& os, const Identity& id);
75 
76  friend istream& operator >>(istream& is, Identity& id);
77 
78  private:
79  long number;
80  string name;
81 
82  static long counter;
83  };
84 
85  // ---------------------------------------------------------------------------
86  // Identity
87  // -----------------x-------------------x-------------------x-----------------
88 
89  inline
90  Identity::Identity(const string& s, long n) : name(s) {
91  if (n != 0) {
92  number = n;
93  } else {
94  number = ++counter;
95  }
96  }
97 
98  inline
99  Identity::Identity(const Identity& orig)
100  : name(orig.name) {
101  number = ++counter;
102  }
103 
104  inline
105  Identity&
107  name = orig.name;
108 
109  return *this;
110  }
111 
112  inline
113  bool
114  Identity::operator ==(const Identity& other) const {
115  return number == other.number;
116  }
117 
118  inline
119  Identity::operator long() const {
120  return number;
121  }
122 
123  inline
124  Identity::operator const string&() const {
125  return name;
126  }
127 
128  inline
129  void
130  Identity::setName(string _name) {
131  name = _name;
132  }
133 
134  inline
135  void
136  Identity::setNumber(long _number) {
137  number = _number;
138  if (_number >= counter)
139  counter = _number;
140  }
141 
142  // ---------------------------------------------------------------------------
143  // Friends
144  // -----------------x-------------------x-------------------x-----------------
145 
146  inline
147  ostream&
148  operator <<(ostream& os, const Identity& id) {
149  return os << "(Id " << id.name << " " << id.number << " ) ";
150  }
151 
152  inline
153  istream&
154  operator >>(istream& is, Identity& id) {
155  string tag;
156 
157  is >> tag;
158 
159  if (tag != "(Id") {
160  DEBUG_MSG("Identity::operator >> : bad input.");
161 
162  is.clear(ios::badbit);
163  return is;
164  }
165 
166  return is >> id.name >> id.number >> tag;
167  }
168 
169 }} // namespace
170 #endif /* __IDENTITY_H__ */
171 
void setNumber(long _number)
Definition: Identity.h:136
void setName(string _name)
Definition: Identity.h:130
bool operator==(const Identity &other) const
Definition: Identity.h:114
Implements object id. With a "number" a "name" and a "counter".
Definition: Identity.h:36
Identity & operator=(const Identity &orig)
Definition: Identity.h:106