Skip to content

Commit 1d73529

Browse files
IP: Comment cleanup and code rearrangement for clarity.
1 parent 1c22483 commit 1d73529

File tree

2 files changed

+89
-58
lines changed

2 files changed

+89
-58
lines changed

code/include/swoc/IPSrv.h

+88-57
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,29 @@ class IP4Srv {
8585
*/
8686
bool load(swoc::TextView text);
8787

88-
/** Change the address.
88+
/** Assign an IPv4 address.
8989
*
9090
* @param addr Address to assign.
9191
* @return @a this
9292
*/
9393
self_type &assign(IP4Addr const &addr);
9494

95-
/** Change the host_order_port.
95+
/** Assign a port.
9696
*
97-
* @param port Port to assign.
97+
* @param port Port to assign (host order)
9898
* @return @a this.
9999
*/
100100
self_type &assign(in_port_t port);
101101

102-
/** Change the address and port.
102+
/** Assign an address and port.
103103
*
104104
* @param addr Address to assign.
105-
* @param port Port to assign.
105+
* @param port Port to assign (host order).
106106
* @return @a this
107107
*/
108108
self_type &assign(IP4Addr const &addr, in_port_t port);
109109

110-
/** Change the address and port.
110+
/** Assign an address and port from an IPv4 socket address.
111111
*
112112
* @param s A socket address.
113113
* @return @a this
@@ -116,7 +116,7 @@ class IP4Srv {
116116

117117
protected:
118118
IP4Addr _addr; ///< Address.
119-
in_port_t _port = 0; ///< Port.
119+
in_port_t _port = 0; ///< Port [host order].
120120
};
121121

122122
/// An IPv6 address and host_order_port, modeled on an SRV type for DNS.
@@ -193,17 +193,17 @@ class IP6Srv {
193193
*/
194194
self_type &assign(IP6Addr const &addr);
195195

196-
/** Change the host_order_port.
196+
/** Assign a port.
197197
*
198-
* @param port Port to assign.
198+
* @param port Port [host order].
199199
* @return @a this.
200200
*/
201201
self_type &assign(in_port_t port);
202202

203-
/** Change the address and host_order_port.
203+
/** Assign an address and port.
204204
*
205-
* @param addr Address to assign.
206-
* @param port Port to assign.
205+
* @param addr Address.
206+
* @param port Port [host order].
207207
* @return @a this
208208
*/
209209
self_type &assign(IP6Addr const &addr, in_port_t port);
@@ -217,7 +217,7 @@ class IP6Srv {
217217

218218
protected:
219219
IP6Addr _addr; ///< Address.
220-
in_port_t _port = 0; ///< Port.
220+
in_port_t _port = 0; ///< Port [host order]
221221
};
222222

223223
/// An IP address and host_order_port, modeled on an SRV type for DNS.
@@ -228,11 +228,17 @@ class IPSrv {
228228
public:
229229
IPSrv() = default; ///< Default constructor.
230230
explicit IPSrv(IP4Addr addr, in_port_t port = 0) : _srv(IP4Srv{addr, port}), _family(addr.family()) {}
231+
/// Construct for IPv6 address and port.
231232
explicit IPSrv(IP6Addr addr, in_port_t port = 0) : _srv(IP6Srv{addr, port}), _family(addr.family()) {}
233+
/// Construct from generic address and port.
232234
explicit IPSrv(IPAddr addr, in_port_t port = 0);
235+
/// Construct from socket address.
233236
explicit IPSrv(sockaddr const *sa);
237+
/// Construct IPv4 service from socket address.
234238
explicit IPSrv(sockaddr_in const *s);
239+
/// Construct IPv6 service from socket address.
235240
explicit IPSrv(sockaddr_in6 const *s);
241+
/// Construct from Endpoint.
236242
explicit IPSrv(IPEndpoint const &ep);
237243

238244
/** Construct from a string.
@@ -259,21 +265,17 @@ class IPSrv {
259265
/// @return The protocol of the current value.
260266
constexpr sa_family_t family() const;
261267

268+
/// @return @c true if this is a valid service, @c false if not.
269+
bool is_valid() const;
262270
/// @return @c true if the data is IPv4, @c false if not.
263271
bool is_ip4() const;
264272
/// @return @c true if hte data is IPv6, @c false if not.
265273
bool is_ip6() const;
266274

267275
/// @return The IPv4 data.
268-
IP4Srv const &
269-
ip4() const {
270-
return _srv._ip4;
271-
}
276+
IP4Srv const & ip4() const;
272277
/// @return The IPv6 data.
273-
IP6Srv const &
274-
ip6() const {
275-
return _srv._ip6;
276-
}
278+
IP6Srv const & ip6() const;
277279

278280
/** Change the address.
279281
*
@@ -289,88 +291,91 @@ class IPSrv {
289291
*/
290292
self_type &assign(IP6Addr const &addr);
291293

292-
/** Change the address.
294+
/** Assign an address.
293295
*
294-
* @param addr Address to assign.
296+
* @param addr Address.
295297
* @return @a this
296298
*
297-
* If @a addr isn't valid then no assignment is made.
299+
* If @a addr isn't valid then no assignment is made, otherwise the family is changed to that of
300+
* @a addr.
298301
*/
299302
self_type &assign(IPAddr const &addr);
300303

301-
/** Change the host_order_port.
304+
/** Assign port.
302305
*
303-
* @param port Port in host order.
306+
* @param port Port [host order].
304307
* @return @a this.
305308
*/
306309
self_type &assign(in_port_t port);
307310

308-
/** Change the address and port.
311+
/** Assign an IPv4 address and port.
309312
*
310-
* @param addr Address to assign.
311-
* @param port Port to assign.
313+
* @param addr Address.
314+
* @param port Port [host order].
312315
* @return @a this
313316
*/
314317
self_type &assign(IP4Addr const &addr, in_port_t port);
315318

316-
/** Change the address and port.
319+
/** Assign an IPv6 address and port.
317320
*
318-
* @param addr Address to assign.
319-
* @param port Port to assign.
321+
* @param addr Address.
322+
* @param port Port [host order].
320323
* @return @a this
321324
*/
322325
self_type &assign(IP6Addr const &addr, in_port_t port);
323326

324-
/** Change the address and port.
327+
/** Assogm address amd [prt/
325328
*
326329
* @param sa Socket address.
327330
* @return @a this
331+
*
332+
* The assignment is ignored if @a sa is not a valid IP family, otherwise the family is changed
333+
* to that of @a sa.
328334
*/
329335
self_type &assign(sockaddr const *sa);
330336

331-
/** Change the address and port.
337+
/** Assign an IPv4 address and port.
332338
*
333339
* @param s Socket address.
334340
* @return @a this
335341
*/
336342
self_type &assign(sockaddr_in const *s);
337343

338-
/** Change the address and port.
344+
/** Assign an IPv6 address and port.
339345
*
340346
* @param s Socket address.
341347
* @return @a this
342348
*/
343349
self_type &assign(sockaddr_in6 const *s);
344350

345-
/** Change the address and host_order_port.
351+
/** Assign an address and port.
346352
*
347-
* @param addr Address to assign.
348-
* @param port Port to assign.
353+
* @param addr Address.
354+
* @param port Port [host order].
349355
* @return @a this
350356
*
351-
* If @a addr isn't valid then no assignment is made.
357+
* If @a addr isn't valid then no assignment is made, otherwise the family is changed to match
358+
* @a addr.
352359
*/
353360
self_type &assign(IPAddr const &addr, in_port_t port);
354361

362+
/// Copy assignment.
355363
self_type &operator=(self_type const &that) = default;
364+
/// Assign from IPv4.
356365
self_type &operator=(IP4Srv const &that);
366+
/// Assign from IPv6.
357367
self_type &operator=(IP6Srv const &that);
358-
self_type &
359-
operator=(sockaddr const *sa) {
360-
return this->assign(sa);
361-
}
362-
self_type &
363-
operator=(sockaddr_in const *s) {
364-
return this->assign(s);
365-
}
366-
self_type &
367-
operator=(sockaddr_in6 const *s) {
368-
return this->assign(s);
369-
}
368+
/// Assign from generic socket address.
369+
self_type & operator=(sockaddr const *sa);
370+
/// Assign from IPv4 socket address.
371+
self_type & operator=(sockaddr_in const *s);
372+
/// Assign from IPv6 socket address.
373+
self_type & operator=(sockaddr_in6 const *s);
370374

371375
protected:
372376
/// Family specialized data.
373377
union data {
378+
std::monostate _nil; ///< Nil / invalid state.
374379
IP4Srv _ip4; ///< IPv4 address (host)
375380
IP6Srv _ip6; ///< IPv6 address (host)
376381

@@ -555,19 +560,42 @@ inline IPAddr
555560
IPSrv::addr() const {
556561
return _srv.addr(_family);
557562
}
563+
558564
inline constexpr sa_family_t
559565
IPSrv::family() const {
560566
return _family;
561567
}
568+
569+
inline bool IPSrv::is_valid() const { return AF_INET == _family || AF_INET6 == _family; }
570+
562571
inline bool
563572
IPSrv::is_ip4() const {
564573
return _family == AF_INET;
565574
}
575+
566576
inline bool
567577
IPSrv::is_ip6() const {
568578
return _family == AF_INET6;
569579
}
570580

581+
inline IP4Srv const& IPSrv::ip4() const {
582+
return _srv._ip4;
583+
}
584+
585+
inline IP6Srv const& IPSrv::ip6() const {
586+
return _srv._ip6;
587+
}
588+
589+
inline constexpr in_port_t
590+
IPSrv::host_order_port() const {
591+
return _srv.port(_family);
592+
}
593+
594+
inline in_port_t
595+
IPSrv::network_order_port() const {
596+
return ntohs(_srv.port(_family));
597+
}
598+
571599
inline auto
572600
IPSrv::assign(IP6Addr const &addr) -> self_type & {
573601
_srv._ip6.assign(addr, this->host_order_port());
@@ -656,14 +684,16 @@ IPSrv::assign(sockaddr_in6 const *s) -> self_type & {
656684
return *this;
657685
}
658686

659-
inline constexpr in_port_t
660-
IPSrv::host_order_port() const {
661-
return _srv.port(_family);
687+
inline IPSrv::self_type & IPSrv::operator=(sockaddr const *sa) {
688+
return this->assign(sa);
662689
}
663690

664-
inline in_port_t
665-
IPSrv::network_order_port() const {
666-
return ntohs(_srv.port(_family));
691+
inline IPSrv::self_type & IPSrv::operator=(sockaddr_in const *s) {
692+
return this->assign(s);
693+
}
694+
695+
inline IPSrv::self_type & IPSrv::operator=(sockaddr_in6 const *s) {
696+
return this->assign(s);
667697
}
668698

669699
inline IPAddr
@@ -675,6 +705,7 @@ constexpr inline in_port_t
675705
IPSrv::data::port(sa_family_t f) const {
676706
return (f == AF_INET) ? _ip4.host_order_port() : (f == AF_INET6) ? _ip6.host_order_port() : 0;
677707
}
708+
678709
// --- Independent comparisons.
679710

680711
inline bool

unit_tests/test_TextView.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ TEST_CASE("TextView Conversions", "[libswoc][TextView]") {
545545
x.assign("-9223372036854775809");
546546
CHECK(svtoi(x) == IMIN);
547547

548-
// floating point is never exact, so "good enough" is all that is measureable. This checks the
548+
// floating point is never exact, so "good enough" is all that iisnts measureable. This checks the
549549
// value is within one epsilon (minimum change possible) of the compiler generated value.
550550
auto fcmp = [](double lhs, double rhs) {
551551
double tolerance = std::max({1.0, std::fabs(lhs), std::fabs(rhs)}) * std::numeric_limits<double>::epsilon();

0 commit comments

Comments
 (0)