Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
CIDR.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_IO_Network_CIDR_h_
5#define _Stroika_Foundation_IO_Network_CIDR_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <compare>
10
14
15/**
16 * \file
17 *
18 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
19 *
20 * TODO:
21 */
22
24
25 using Characters::String;
26
27 /**
28 * \note <a href="Design-Overview.md#Comparisons">Comparisons</a>:
29 * static_assert (totally_ordered<CIDR>);
30 *
31 * Compare the significant bits of the CIDR.
32 */
33 class CIDR {
34 public:
35 /**
36 * For CIDR (const InternetAddress& internetAddress) CTOR
37 * \pre internetAddress.GetAddressFamily () is V4 or V6 (not unknown).
38 *
39 * For CIDR (const String& cidrNotation... CTOR)
40 * cidrNotation ends with "/NNN" where NNN are digits of an integer where the numberOfBits. This will
41 * throw an exception if the input is ill-formed (e.g. if number of bits too large for the address).
42 * And the internet-address part will be parsed as by InternetAddress{String} CTOR, and will also throw
43 * on bad format (basically must be valid numeric internet address format).
44 *
45 * This (string) API should always work with the output of CIDR::As<String> () - but will NOT in general work
46 * with the output of CIDR::ToString () (since the later is for display purposes).
47 */
48 CIDR () = delete;
49 CIDR (const CIDR& src) = default;
50 CIDR (CIDR&& src) = default;
51 CIDR (const InternetAddress& internetAddress, optional<unsigned int> significantBits = nullopt);
52 CIDR (const InternetAddress& internetAddress, unsigned int significantBits);
53 CIDR (const String& cidrNotation, InternetAddress::AddressFamily addressFamily = InternetAddress::AddressFamily::UNKNOWN);
54
55 public:
56 /**
57 */
58 nonvirtual CIDR& operator= (CIDR&& rhs) = default;
59 nonvirtual CIDR& operator= (const CIDR& rhs) = default;
60
61 public:
62 /**
63 */
64 nonvirtual InternetAddress GetBaseInternetAddress () const;
65
66 public:
67 /**
68 * Note - for IPv4 and class C, =24
69 */
70 nonvirtual unsigned int GetNumberOfSignificantBits () const;
71
72 public:
73 /**
74 * A CIDR designates a range of IP addresses, and GetRange returns that range.
75 *
76 * \par Example Usage
77 * \code
78 * CIDR cidr { "192.168.243.0/24" };
79 * InternetAddress lb = cidr.GetRange ().GetLowerBound ();
80 * InternetAddress ub = cidr.GetRange ().GetUpperBound ();
81 * unsigned int nAddresses = cird.GetRange ().GetNumberOfContainedPoints ();
82 * for (InternetAddress ia : cidr.GetRange ()) {
83 * print (ia);
84 * }
85 * \endcode
86 */
88
89 public:
90 /**
91 * Only specifically specialized variants are supported. As<T> supported variants include:
92 * As<String> ();
93 *
94 * \note As<String> () will always produce a numerical representation, whereas ToString () - will sometimes produce
95 * a textual shortcut, like "INADDR_ANY".
96 */
97 template <typename T>
98 nonvirtual T As () const;
99
100 public:
101 /**
102 */
103 nonvirtual strong_ordering operator<=> (const CIDR& rhs) const;
104
105 public:
106 /**
107 */
108 nonvirtual bool operator== (const CIDR& rhs) const;
109
110 public:
111 /**
112 * @see Characters::ToString ()
113 */
114 nonvirtual String ToString () const;
115
116 private:
117 InternetAddress fBaseAddress_;
118 unsigned int fSignificantBits_{}; // for IPv4 and class C, =24
119 };
120 static_assert (totally_ordered<CIDR>);
121
122}
123
124/*
125 ********************************************************************************
126 ***************************** Implementation Details ***************************
127 ********************************************************************************
128 */
129#include "CIDR.inl"
130
131#endif /*_Stroika_Foundation_IO_Network_CIDR_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual Traversal::DiscreteRange< InternetAddress > GetRange() const
Definition CIDR.cpp:73
nonvirtual String ToString() const
Definition CIDR.cpp:66
nonvirtual unsigned int GetNumberOfSignificantBits() const
Definition CIDR.inl:22
A DiscreteRange is a Range where the underlying endpoints are integral (discrete, not continuous); th...