2
3
4
5
6
7
8
9
10
12#ifndef IPADDRESS_IP_ADDRESS_BASE_HPP
13#define IPADDRESS_IP_ADDRESS_BASE_HPP
25
26
27
28
35
36
37
38
39
47
48
49
50
51
52
53
54
58 using base_type =
typename Base::base_type;
59 using uint_type =
typename Base::uint_type;
62
63
64
65
70
71
72
73
78
79
80
81
82
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
106 for (size_t i = 0; i < Base::base_size && i < byte_count; ++i) {
112#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
115
116
117
118
119
120
121
136#if IPADDRESS_CPP_VERSION
>= 17
139
140
141
142
143
144
145
146
147
148
149
155
156
157
158
159
160
161
162
163
164
165
170#if __cpp_char8_t >= 201811L
173
174
175
176
177
178
179
180
181
182
190
191
192
193
194
195
196
197
198
199
200
206
207
208
209
210
211
212
213
214
215
216
222
223
224
225
226
227
228
229
230
231
237
238
239
240
241
242
243
244
245
246
251#if __cpp_char8_t >= 201811L
254
255
256
257
258
259
260
261
262
270
271
272
273
274
275
276
277
278
279
285
286
287
288
289
290
291
292
293
294
302
303
304
305
306
307
309 return parse_string(address);
313
314
315
316
317
318
320 return parse_string(address);
324
325
326
327
328
329
331 return parse_string(address);
335
336
337
338
339
340
342 return parse_string(address);
346
347
348
349
350
351
353 return parse_string(address, code);
357
358
359
360
361
362
364 return parse_string(address, code);
368
369
370
371
372
373
375 return parse_string(address, code);
379
380
381
382
383
384
386 return parse_string(address, code);
392
393
394
395
396
397
398
399
400
401
402 template <
typename T, size_t N>
404 #ifdef IPADDRESS_NO_EXCEPTIONS
405 error_code err = error_code::no_error;
406 auto str = make_fixed_string(address, err);
407 if (err != error_code::no_error) {
411 auto str = make_fixed_string(address);
413 return parse_string(str);
417
418
419
420
421
422
423
424
425
426
427
428 template <
typename T, size_t N>
430 auto str = make_fixed_string(address, code);
435
436
437
438
439
440
442 return Base::bytes().data();
446
447
448
449
450
451
455
456
457
458
459
460
464
465
466
467
468
469
473
474
475
476
480
481
482
483
484
485
489
490
491
492
493
497
498
499
500
501
502
503
504
505
506
508 const auto& b = Base::bytes();
509 for (size_t i = 0; i < Base::base_size; ++i) {
518
519
520
521
522
523
524
525
526
528 char res[Base::base_max_string_len + 1]{};
529 const auto len = Base::ip_to_chars(Base::bytes(), fmt, res);
530 return std::string(res, len);
534
535
536
537
538
539
540
541
542
544 return internal::string_converter<
wchar_t>::convert(to_string(fmt));
548
549
550
551
552
553
554
555
556
558 return internal::string_converter<
char16_t>::convert(to_string(fmt));
562
563
564
565
566
567
568
569
570
572 return internal::string_converter<
char32_t>::convert(to_string(fmt));
575#if __cpp_char8_t >= 201811L
578
579
580
581
582
583
584
585
586
594
595
596
597
598
599
601 Base::swap(*
this, ip);
605
606
607
608
609
610
611
613 return Base::hash(Base::bytes());
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
635 return Base::ip_reverse_pointer(Base::bytes());
639
640
641
642
643
644
645
646
648 return Base::to_uint();
652
653
654
655
656
657
658
659 template <
typename T>
661 return internal::string_converter<T>::convert(
to_string());
665
666
667
668
669
670
671
673 return Base::equals(*
this, rhs);
677
678
679
680
681
682
683
685 return !(*
this == rhs);
688#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
691
692
693
694
695
696
697
705
706
707
708
709
710
711
712
714 return Base::less(*
this, rhs);
718
719
720
721
722
723
724
725
731
732
733
734
735
736
737
738
740 return !(rhs < *
this);
744
745
746
747
748
749
750
751
753 return !(*
this < rhs);
762 template <
typename Str>
766 auto result = Base::ip_from_string(address.data(), address.data() + address.size(), code, value);
768 raise_error(code, value, address.data(), address.size());
773 template <
typename Str>
777 return Base::ip_from_string(address.data(), address.data() + address.size(), code, value);
781#ifndef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
785template <
typename Base,
typename TChar, size_t MaxLen>
788 raise_error(error_code::string_is_too_long, 0, address, size);
790 TChar str[MaxLen + 1] = {};
791 for (size_t i = 0; i < size && i < MaxLen; ++i) {
801#ifndef IPADDRESS_NO_OVERLOAD_STD
804 static const int i = std::ios_base::xalloc();
809IPADDRESS_FORCE_INLINE std::basic_ostream<T, std::char_traits<T>>& full(std::basic_ostream<T, std::char_traits<T>>& stream) {
815IPADDRESS_FORCE_INLINE std::basic_ostream<T, std::char_traits<T>>& compact(std::basic_ostream<T, std::char_traits<T>>& stream) {
821IPADDRESS_FORCE_INLINE std::basic_ostream<T, std::char_traits<T>>& compressed(std::basic_ostream<T, std::char_traits<T>>& stream) {
830#ifndef IPADDRESS_NO_OVERLOAD_STD
848 return ip.to_string();
853 return ip.to_wstring();
863 auto str = ip.to_string(fmt);
864 if (stream.flags() & ios_base::uppercase) {
865 auto end = std::find(str.cbegin(), str.cend(),
'%');
866 std::transform(str.cbegin(), end, str.begin(), [](
char c){
867 return std::toupper(c);
875 std::basic_string<T, std::char_traits<T>, std::allocator<T>> str;
880 stream.setstate(std::ios_base::failbit);
A template base class for IP address representations.
Definition ip-address-base.hpp:56
constexpr inline ip_address_base() noexcept
Default constructor.
Definition ip-address-base.hpp:66
inline std::u16string to_u16string(format fmt=format::compressed) const
Converts the IP address to a string representation.
Definition ip-address-base.hpp:557
constexpr inline const uint8_t * data() const noexcept
Retrieves the raw data representing the IP address in network byte order (big-endian).
Definition ip-address-base.hpp:441
constexpr inline ip_address_base(const base_type &bytes) noexcept
Constructs an IP address object from a byte array.
Definition ip-address-base.hpp:74
static constexpr inline ip_address_base parse(const T(&address)[N])
Parses an IP address from a character array.
Definition ip-address-base.hpp:403
constexpr inline bool operator>=(const ip_address_base &rhs) const noexcept
Checks if one IP address is greater than or equal to another.
Definition ip-address-base.hpp:752
constexpr inline bool operator==(const ip_address_base &rhs) const noexcept
Checks if two IP addresses are equal.
Definition ip-address-base.hpp:672
inline std::string reverse_pointer() const
Generates a reverse DNS lookup pointer for the IP address.
Definition ip-address-base.hpp:634
constexpr inline bool is_multicast() const noexcept
Checks if the IP address is a multicast address.
Definition ip-networks.hpp:378
constexpr inline bool is_private() const noexcept
Checks if the IP address is a private address.
Definition ip-networks.hpp:368
constexpr inline bool is_reserved() const noexcept
Checks if the IP address is a reserved address.
Definition ip-networks.hpp:383
constexpr inline bool is_link_local() const noexcept
Checks if the IP address is a link-local address.
Definition ip-networks.hpp:393
inline operator std::basic_string< T, std::char_traits< T >, std::allocator< T > >() const
Converts the IP address to a string.
Definition ip-address-base.hpp:660
constexpr inline bool operator!=(const ip_address_base &rhs) const noexcept
Checks if two IP addresses are not equal.
Definition ip-address-base.hpp:684
constexpr inline bool is_unspecified() const noexcept
Checks if the IP address is unspecified.
Definition ip-address-base.hpp:507
static constexpr inline ip_address_base parse(const T(&address)[N], error_code &code) noexcept
Parses an IP address from a character array and reports errors through an error code.
Definition ip-address-base.hpp:429
constexpr inline void swap(ip_address_base &ip) noexcept
Swaps the contents of this IP address with another IP address.
Definition ip-address-base.hpp:600
constexpr inline bool operator>(const ip_address_base &rhs) const noexcept
Checks if one IP address is greater than another.
Definition ip-address-base.hpp:726
constexpr inline bool is_global() const noexcept
Checks if the IP address is a global address.
Definition ip-networks.hpp:373
static constexpr inline ip_address_base from_bytes(const base_type &bytes) noexcept
Static factory method to create an IP address object from a byte array.
Definition ip-address-base.hpp:83
constexpr inline size_t hash() const noexcept
Computes a hash value for the IP address.
Definition ip-address-base.hpp:612
inline std::wstring to_wstring(format fmt=format::compressed) const
Converts the IP address to a string representation.
Definition ip-address-base.hpp:543
constexpr inline bool operator<(const ip_address_base &rhs) const noexcept
Checks if one IP address is less than another.
Definition ip-address-base.hpp:713
inline std::u32string to_u32string(format fmt=format::compressed) const
Converts the IP address to a string representation.
Definition ip-address-base.hpp:571
static constexpr inline ip_address_base from_bytes(const uint8_t *bytes, size_t byte_count) noexcept
Static factory method to create an IP address object from a raw byte buffer.
Definition ip-address-base.hpp:104
constexpr inline bool is_loopback() const noexcept
Checks if the IP address is a loopback address.
Definition ip-networks.hpp:388
inline std::string to_string(format fmt=format::compressed) const
Converts the IP address to a string representation.
Definition ip-address-base.hpp:527
constexpr inline operator uint_type() const noexcept
Converts the IP address to an unsigned integer type.
Definition ip-address-base.hpp:647
constexpr inline bool operator<=(const ip_address_base &rhs) const noexcept
Checks if one IP address is less than or equal to another.
Definition ip-address-base.hpp:739
Template base class for representing a network of IP addresses.
Definition ip-network-base.hpp:32
#define IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS
Definition config.hpp:93
#define IPADDRESS_EXPORT
Definition config.hpp:42
#define IPADDRESS_NODISCARD
Definition config.hpp:98
#define IPADDRESS_FORCE_INLINE
Definition config.hpp:112
#define IPADDRESS_NAMESPACE
Definition config.hpp:38
#define IPADDRESS_NOEXCEPT
Definition config.hpp:89
#define IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS
Definition config.hpp:102
ip_version
Enumerates the IP address versions.
Definition ip-address-base.hpp:29
@ V4
IPv4 version identifier.
@ V6
IPv6 version identifier.
format
Enumerates the formatting options for IP address strings.
Definition ip-address-base.hpp:40
@ compressed
Compressed format with maximal omission of segments or octets.
@ compact
Compact format with possible omission of leading zeros.
@ full
Full format with no compression or omission.
error_code
Enumeration of error codes for IP address parsing and validation.
Definition errors.hpp:52
@ no_error
Indicates the absence of any errors.