Ocean
Address4.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef FACEBOOK_NETWORK_ADDRESS4_H
9 #define FACEBOOK_NETWORK_ADDRESS4_H
10 
11 #include "ocean/network/Network.h"
12 #include "ocean/network/Data.h"
13 
14 namespace Ocean
15 {
16 
17 namespace Network
18 {
19 
20 /**
21  * This class wraps an address number with 32 bits.
22  * Internally, the address number is stored in big-endian order.
23  * @ingroup network
24  */
25 class OCEAN_NETWORK_EXPORT Address4 : public Data
26 {
27  public:
28 
29  /**
30  * Creates a new address object with zero as default value.
31  */
32  inline Address4() = default;
33 
34  /**
35  * Creates a new address object.
36  * @param address Address4 number in big-endian order
37  */
38  inline Address4(const uint32_t address);
39 
40  /**
41  * Creates a new address object by a given address number in big-endian, little-endian order or local platform.
42  * @param address Address4 number to be wrapped
43  * @param orderType Type of the order
44  */
45  inline Address4(const uint32_t address, const OrderType orderType);
46 
47  /**
48  * Creates a new address object.
49  * @param sub0 First sub address
50  * @param sub1 Second sub address
51  * @param sub2 Third sub address
52  * @param sub3 Fourth sub address
53  */
54  inline Address4(const uint8_t sub0, const uint8_t sub1, const uint8_t sub2, const uint8_t sub3);
55 
56  /**
57  * Returns the address number in little-endian order.
58  * @return Address4 number in little-endian order
59  */
60  inline uint32_t littleEndian() const;
61 
62  /**
63  * Returns the address number as a readable string.
64  * @return Readable address string
65  */
66  std::string readable() const;
67 
68  /**
69  * Returns whether this address hold a valid address.
70  * @return True, if so
71  */
72  inline bool isValid() const;
73 
74  /**
75  * Returns whether this address hold an invalid address (a zero address).
76  * @return True, if so
77  */
78  inline bool isNull() const;
79 
80  /**
81  * Returns the local host address.
82  * @return Local host address
83  */
84  static inline Address4 localHost();
85 
86  /**
87  * Returns whether two address objects are identical.
88  * @param address Right address object to compare
89  * @return True, if so
90  */
91  inline bool operator==(const Address4& address) const;
92 
93  /**
94  * Returns whether two address objects are not identical.
95  * @param address Right address object to compare
96  * @return True, if so
97  */
98  inline bool operator!=(const Address4& address) const;
99 
100  /**
101  * Casts the wrapped address number
102  * @return Wrapped address number in big-endian order
103  */
104  inline operator uint32_t() const;
105 
106  /**
107  * Hash function.
108  * @param address The address for which the hash will be determined
109  * @return The hash value
110  */
111  inline size_t operator()(const Address4& address) const;
112 
113  protected:
114 
115  /// Address4 number to wrap in big-endian order
116  uint32_t address_ = 0u;
117 };
118 
119 inline Address4::Address4(const uint32_t address) :
120  address_(address)
121 {
122  // nothing to do here
123 }
124 
125 inline Address4::Address4(const uint32_t address, const OrderType orderType)
126 {
127  switch (orderType)
128  {
129  case TYPE_BIG_ENDIAN:
130  address_ = address;
131  break;
132 
133  case TYPE_LITTLE_ENDIAN:
134  address_ = (address >> 24u) | ((address >> 8u) & 0xFF00u) | ((address << 8u) & 0xFF0000u) | (address << 24u);
135  break;
136 
137  case TYPE_READABLE:
138  address_ = toBigEndian(address);
139  break;
140  }
141 }
142 
143 inline Address4::Address4(const uint8_t sub0, const uint8_t sub1, const uint8_t sub2, const uint8_t sub3) :
144  address_(uint32_t(sub0) | (uint32_t(sub1) << 8u) | (uint32_t(sub2) << 16u) | (uint32_t(sub3) << 24u))
145 {
146  // nothing to do here
147 }
148 
149 inline unsigned int Address4::littleEndian() const
150 {
151  return (address_ >> 24u) | ((address_ >> 8u) & 0xFF00u) | ((address_ << 8u) & 0xFF0000u) | (address_ << 24u);
152 }
153 
154 inline bool Address4::isValid() const
155 {
156  return !isNull();
157 }
158 
159 inline bool Address4::isNull() const
160 {
161  return address_ == 0u;
162 }
163 
165 {
166  return Address4(127u, 0u, 0u, 1u);
167 }
168 
169 inline bool Address4::operator==(const Address4& address) const
170 {
171  return address_ == address.address_;
172 }
173 
174 inline bool Address4::operator!=(const Address4& address) const
175 {
176  return address_ != address.address_;
177 }
178 
179 inline Address4::operator uint32_t() const
180 {
181  return address_;
182 }
183 
184 inline size_t Address4::operator()(const Address4& address) const
185 {
186  return size_t(address.address_);
187 }
188 
189 }
190 
191 }
192 
193 #endif // FACEBOOK_NETWORK_ADDRESS4_H
This class wraps an address number with 32 bits.
Definition: Address4.h:26
uint32_t littleEndian() const
Returns the address number in little-endian order.
Definition: Address4.h:149
uint32_t address_
Address4 number to wrap in big-endian order.
Definition: Address4.h:116
Address4()=default
Creates a new address object with zero as default value.
std::string readable() const
Returns the address number as a readable string.
bool operator!=(const Address4 &address) const
Returns whether two address objects are not identical.
Definition: Address4.h:174
size_t operator()(const Address4 &address) const
Hash function.
Definition: Address4.h:184
bool isNull() const
Returns whether this address hold an invalid address (a zero address).
Definition: Address4.h:159
static Address4 localHost()
Returns the local host address.
Definition: Address4.h:164
bool operator==(const Address4 &address) const
Returns whether two address objects are identical.
Definition: Address4.h:169
bool isValid() const
Returns whether this address hold a valid address.
Definition: Address4.h:154
This class provides function handling different network data.
Definition: Data.h:24
static int toBigEndian(const int32_t value)
Returns a value in big-endian order given in local system bit order.
Definition: Data.h:107
OrderType
Definition of different order types.
Definition: Data.h:31
@ TYPE_BIG_ENDIAN
Big endian type.
Definition: Data.h:33
@ TYPE_LITTLE_ENDIAN
Little endian type.
Definition: Data.h:35
@ TYPE_READABLE
Readable endian type which is platform specific.
Definition: Data.h:37
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15