fastcgi++  3.1alpha
A C++ FastCGI/Web API
address.hpp
Go to the documentation of this file.
1 
9 /*******************************************************************************
10 * Copyright (C) 2018 Eddie Carle [eddie@isatec.ca] *
11 * *
12 * This file is part of fastcgi++. *
13 * *
14 * fastcgi++ is free software: you can redistribute it and/or modify it under *
15 * the terms of the GNU Lesser General Public License as published by the Free *
16 * Software Foundation, either version 3 of the License, or (at your option) *
17 * any later version. *
18 * *
19 * fastcgi++ is distributed in the hope that it will be useful, but WITHOUT ANY *
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
21 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for *
22 * more details. *
23 * *
24 * You should have received a copy of the GNU Lesser General Public License *
25 * along with fastcgi++. If not, see <http://www.gnu.org/licenses/>. *
26 *******************************************************************************/
27 
28 #ifndef FASTCGIPP_ADDRESS_HPP
29 #define FASTCGIPP_ADDRESS_HPP
30 
31 #include <array>
32 #include <algorithm>
33 #include <cstring>
34 
36 namespace Fastcgipp
37 {
39 
50  struct Address
51  {
53  static constexpr size_t size=16;
54 
56  std::array<unsigned char, size> m_data;
57 
59 
62  Address operator=(const unsigned char* data)
63  {
64  std::copy(data, data+m_data.size(), m_data.begin());
65  return *this;
66  }
67 
68  Address operator=(const Address& address)
69  {
70  std::copy(
71  address.m_data.begin(),
72  address.m_data.end(),
73  m_data.begin());
74  return *this;
75  }
76 
77  Address(const Address& address)
78  {
79  std::copy(
80  address.m_data.begin(),
81  address.m_data.end(),
82  m_data.begin());
83  }
84 
86  Address()
87  {
88  zero();
89  }
90 
92 
95  explicit Address(const unsigned char* data)
96  {
97  std::copy(data, data+m_data.size(), m_data.begin());
98  }
99 
101 
110  template<class charT> void assign(
111  const charT* start,
112  const charT* end);
113 
114  Address(const char* string)
115  {
116  assign(string, string+std::strlen(string));
117  }
118 
119  bool operator==(const Address& x) const
120  {
121  return std::equal(
122  m_data.cbegin(),
123  m_data.cend(),
124  x.m_data.cbegin());
125  }
126 
127  bool operator<(const Address& x) const
128  {
129  return std::memcmp(m_data.data(), x.m_data.data(), size)<0;
130  }
131 
133  operator bool() const;
134 
135  Address& operator&=(const Address& x);
136 
137  Address operator&(const Address& x) const
138  {
139  Address address(*this);
140  address &= x;
141 
142  return address;
143  }
144 
146  void zero()
147  {
148  m_data.fill(0);
149  }
150  };
151 
153 
157  template<class charT, class Traits>
158  std::basic_ostream<charT, Traits>& operator<<(
159  std::basic_ostream<charT, Traits>& os,
160  const Address& address);
161 
163 
168  template<class charT, class Traits>
169  std::basic_istream<charT, Traits>& operator>>(
170  std::basic_istream<charT, Traits>& is,
171  Address& address);
172 }
173 
174 #endif
Fastcgipp::operator<<
std::basic_ostream< charT, Traits > & operator<<(std::basic_ostream< charT, Traits > &os, const Address &address)
Address stream insertion operation.
Definition: address.cpp:179
Fastcgipp::Address::operator&
Address operator&(const Address &x) const
Definition: address.hpp:171
Fastcgipp::Address::size
static constexpr size_t size
This is the data length of the IPv6 address.
Definition: address.hpp:87
Fastcgipp::Address::operator&=
Address & operator&=(const Address &x)
Definition: address.cpp:36
Fastcgipp::Address::zero
void zero()
Set all bits to zero in IP address.
Definition: address.hpp:180
Fastcgipp::Address::assign
void assign(const charT *start, const charT *end)
Assign the IP address from a string of characters.
Definition: address.cpp:53
Fastcgipp::Address::m_data
std::array< unsigned char, size > m_data
Data representation of the IPv6 address.
Definition: address.hpp:90
Fastcgipp
Topmost namespace for the fastcgi++ library.
Definition: fcgistreambuf.cpp:34
Fastcgipp::Address::Address
Address()
Initializes an all zero address.
Definition: address.hpp:120
Fastcgipp::Address
Efficiently stores IPv6 addresses.
Definition: address.hpp:67
Fastcgipp::operator>>
std::basic_istream< charT, Traits > & operator>>(std::basic_istream< charT, Traits > &is, Address &address)
Address stream extractor operation.
Definition: address.cpp:361
Fastcgipp::Address::operator<
bool operator<(const Address &x) const
Definition: address.hpp:161
Fastcgipp::Address::operator=
Address operator=(const unsigned char *data)
Assign the IPv6 address from a data array.
Definition: address.hpp:96
Fastcgipp::Address::operator==
bool operator==(const Address &x) const
Definition: address.hpp:153