/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2018 Dominik Charousset                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#pragma once

#include <array>
#include <cstdint>
#include <string>

#include "caf/byte_address.hpp"
#include "caf/detail/comparable.hpp"
#include "caf/fwd.hpp"

namespace caf {

class ipv4_address : public byte_address<ipv4_address>,
                     detail::comparable<ipv4_address, ipv6_address> {
public:
  // -- constants --------------------------------------------------------------

  static constexpr size_t num_bytes = 4;

  // -- member types -----------------------------------------------------------

  using super = byte_address<ipv4_address>;

  using array_type = std::array<uint8_t, num_bytes>;

  // -- constructors, destructors, and assignment operators --------------------

  ipv4_address();

  ipv4_address(array_type bytes);

  // -- comparison -------------------------------------------------------------

  using super::compare;

  /// Returns a negative number if `*this < other`, zero if `*this == other`
  /// and a positive number if `*this > other`.
  int compare(const ipv6_address& other) const noexcept;

  // -- properties -------------------------------------------------------------

  /// Returns whether this is a loopback address.
  bool is_loopback() const noexcept;

  /// Returns whether this is a multicast address.
  bool is_multicast() const noexcept;

  /// Returns the bits of the IP address in a single integer.
  inline uint32_t bits() const noexcept {
    return bits_;
  }

  /// Returns the bytes of the IP address as array.
  inline array_type& bytes() noexcept {
    return bytes_;
  }

  /// Returns the bytes of the IP address as array.
  inline const array_type& bytes() const noexcept {
    return bytes_;
  }

  /// Alias for `bytes()`.
  inline array_type& data() noexcept {
    return bytes_;
  }

  /// Alias for `bytes()`.
  inline const array_type& data() const noexcept {
    return bytes_;
  }

  constexpr size_t size() const noexcept {
    return bytes_.size();
  }

  // -- inspection -------------------------------------------------------------

  template <class Inspector>
  friend typename Inspector::result_type inspect(Inspector& f, ipv4_address& x) {
    return f(x.bits_);
  }

private:
  // -- member variables -------------------------------------------------------

  union {
    uint32_t bits_;
    array_type bytes_;
  };

  // -- sanity checks ----------------------------------------------------------

  static_assert(sizeof(array_type) == sizeof(uint32_t),
                "array<char, 4> has a different size than uint32");
};

// -- related free functions ---------------------------------------------------

/// Returns a human-readable string representation of the address.
/// @relates ipv4_address
std::string to_string(const ipv4_address& x);

/// Tries to parse the content of `str` into `dest`.
/// @relates ipv4_address
error parse(string_view str, ipv4_address& dest);

} // namespace caf
