OverSim
I3SubIdentifier.cc
Go to the documentation of this file.
1 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 //
8 // This program 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 this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 
25 #include "I3SubIdentifier.h"
26 
27 
29  type(Invalid)
30 {
31 }
32 
34 {
35  type = IPAddress;
36  ipAddress = address;
37  identifier.clear();
38 }
39 
41 {
42  type = Identifier;
43 
44  I3IPAddress zero; // initialized to zero
45  ipAddress = zero; // set to clear
46 
47  identifier = id;
48 }
49 
51 {
52  return type;
53 }
54 
56 {
57  if (type != IPAddress) {
58  // error!
59  }
60  return ipAddress;
61 }
62 
64 {
65  if (type != Identifier) {
66  // error!
67  };
68  return identifier;
69 }
70 
72 {
73  if (type != id.type) {
74  return type - id.type;
75  } else if (type == Identifier) {
76  return identifier.compareTo(id.identifier);
77  } else if (type == IPAddress) {
78  if (ipAddress.getIp() < id.ipAddress.getIp()) return -1;
79  else if (ipAddress.getIp() == id.ipAddress.getIp()) return ipAddress.getPort() - id.ipAddress.getPort();
80  else return 1;
81  }
82  return 0;
83 }
84 
86 // return sizeof(type) + (type == IPAddress ? ipAddress.length() : identifier.length());
87  return 1 + (type == IPAddress ? ipAddress.length() : identifier.length());
88 }
89 
90 std::ostream& operator<<(std::ostream& os, const I3SubIdentifier &s) {
92  os << s.identifier;
93  } else {
94  os << s.ipAddress;
95  }
96  return os;
97 }