Fix copyright lines for Bits.h and move BitsBenchmark.cpp
[folly.git] / folly / IPAddressException.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <exception>
20 #include <string>
21 #include <utility>
22
23 #include <folly/detail/IPAddress.h>
24
25 namespace folly {
26
27 /**
28  * Error codes for non-throwing interface of IPAddress family of functions.
29  */
30 enum class IPAddressFormatError { INVALID_IP, UNSUPPORTED_ADDR_FAMILY };
31
32 /**
33  * Exception for invalid IP addresses.
34  */
35 class IPAddressFormatException : public std::exception {
36  public:
37   explicit IPAddressFormatException(std::string msg) noexcept
38       : msg_(std::move(msg)) {}
39   IPAddressFormatException(const IPAddressFormatException&) = default;
40   IPAddressFormatException(IPAddressFormatException&&) = default;
41   IPAddressFormatException& operator=(const IPAddressFormatException&) =
42       default;
43   IPAddressFormatException& operator=(IPAddressFormatException&&) = default;
44
45   ~IPAddressFormatException() noexcept override {}
46   const char* what() const noexcept override {
47     return msg_.c_str();
48   }
49
50  private:
51   std::string msg_;
52 };
53
54 class InvalidAddressFamilyException : public IPAddressFormatException {
55  public:
56   explicit InvalidAddressFamilyException(std::string msg) noexcept
57       : IPAddressFormatException(std::move(msg)) {}
58   explicit InvalidAddressFamilyException(sa_family_t family) noexcept
59       : InvalidAddressFamilyException(
60             "Address family " + detail::familyNameStr(family) +
61             " is not AF_INET or AF_INET6") {}
62   InvalidAddressFamilyException(const InvalidAddressFamilyException&) = default;
63   InvalidAddressFamilyException(InvalidAddressFamilyException&&) = default;
64   InvalidAddressFamilyException& operator=(
65       const InvalidAddressFamilyException&) = default;
66   InvalidAddressFamilyException& operator=(InvalidAddressFamilyException&&) =
67       default;
68 };
69
70 } // namespace folly