]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/HostAddress_p.h
7542b6720f0d400a7ce5db8f024e2e428806f903
[bamtools.git] / src / api / internal / io / HostAddress_p.h
1 #ifndef HOSTADDRESS_P_H
2 #define HOSTADDRESS_P_H
3
4 #include "api/api_global.h"
5 #include <cstring>
6 #include <string>
7
8 namespace BamTools {
9 namespace Internal {
10
11 struct IPv6Address {
12
13     // ctor
14     inline IPv6Address(void) { memset(&data, 0, sizeof(uint8_t)*16); }
15
16     // data access (no bounds checking)
17     inline uint8_t& operator[](size_t index)       { return data[index]; }
18     inline uint8_t  operator[](size_t index) const { return data[index]; }
19
20     // data
21     uint8_t data[16];
22 };
23
24 class HostAddress {
25
26     // enums
27     public:
28         enum NetworkProtocol { UnknownNetworkProtocol = -1
29                              , IPv4Protocol = 0
30                              , IPv6Protocol
31                              };
32
33     // ctors & dtor
34     public:
35         HostAddress(void);
36         explicit HostAddress(const uint32_t ip4Address);
37         explicit HostAddress(const uint8_t* ip6Address);
38         explicit HostAddress(const IPv6Address& ip6Address);
39         explicit HostAddress(const std::string& address);
40         HostAddress(const HostAddress& other);
41         ~HostAddress(void);
42
43     // HostAddress interface
44     public:
45         void Clear(void);
46         bool HasIPAddress(void) const; // returns whether string address could be converted to IP address
47         bool IsNull(void) const;
48
49         uint32_t    GetIPv4Address(void) const;
50         IPv6Address GetIPv6Address(void) const;
51         std::string GetIPString(void) const;
52         HostAddress::NetworkProtocol GetProtocol(void) const;
53
54         void SetAddress(const uint32_t ip4Address);
55         void SetAddress(const uint8_t* ip6Address);
56         void SetAddress(const IPv6Address& ip6Address);
57         void SetAddress(const std::string& address);
58
59     // HostAddress comparison operators
60     public:
61         bool operator==(const HostAddress& other) const;
62         bool operator!=(const HostAddress& other) const { return !( operator==(other) ); }
63         bool operator<(const HostAddress& other) const;
64
65     // internal methods
66     private:
67         bool ParseAddress(void);
68
69     // data members
70     private:
71         HostAddress::NetworkProtocol m_protocol;
72         uint32_t    m_ip4Address;
73         IPv6Address m_ip6Address;
74         std::string m_ipString;
75         bool        m_hasIpAddress; // true until string passed in, then signifies whether string was an IP
76 };
77
78 } // namespace Internal
79 } // namespace BamTools
80
81 #endif // HOSTADDRESS_P_H