Remove dynamic exceptions
[anna.git] / include / anna / comm / INetAddress.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_comm_INetAddress_hpp
10 #define anna_comm_INetAddress_hpp
11
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16
17 #include <string>
18
19 #include <anna/core/RuntimeException.hpp>
20
21 namespace anna {
22
23 namespace xml {
24 class Node;
25 }
26
27 namespace comm {
28
29 class Device;
30
31 /**
32    Network address abstraction.
33 */
34 class INetAddress {
35 public:
36   /**
37      Constructor.
38   */
39   INetAddress() : a_device(NULL), a_port(-1)  {}
40
41   /**
42      Constructor.
43      \param device Instance for device (address).
44      \param port Port number.
45   */
46   INetAddress(const Device* device, const int port = -1)  : a_device(device),  a_port(port) {;}
47
48   /**
49      Copy constructor.
50      \param other Source network address.
51   */
52   INetAddress(const INetAddress& other) : a_device(other.a_device) , a_port(other.a_port) {;}
53
54   /**
55      Returns the device (address) associated to this instance.
56      \param exceptionWhenNull Exception is launched in case the device returned is NULL.
57      \return Device (address) associated to this instance.
58   */
59   const Device* getDevice(const bool exceptionWhenNull = true) const noexcept(false);
60
61   /**
62      Returns the port associated to this instance.
63      \return Port associated to this instance.
64   */
65   int getPort() const { return a_port; }
66
67   /**
68      Sets the address for this instance.
69      \param device Address provided.
70   */
71   void setAddress(const Device* device)   { a_device = device;  }
72
73   /**
74      Sets the port for this instance.
75      \param port Port provided.
76   */
77   void setPort(const int port) { a_port = port; }
78
79   /**
80      Copy operator.
81      \param right Source address to be copied.
82   */
83   INetAddress& operator = (const INetAddress& right) { a_device = right.a_device; a_port = right.a_port; return *this; }
84
85   /**
86     Comparison operator.
87     \param right Source address to be compared.
88     @return \em true when address provided is equal to this \em false in other case.
89   */
90   bool operator == (const INetAddress& right) const { return a_device == right.a_device && a_port == right.a_port; }
91
92   /**
93      Returns the initialized state for this network address.
94      @return \em true when initialized, \em false when not.
95   */
96   bool isNull() const { return (a_device == NULL || a_port == -1);  }
97
98   /**
99      Clear the content for this instance.
100   */
101   void clear() { a_device = NULL; a_port = -1; }
102
103   /**
104      Returns string with relevant information for this instance.
105      @return string with relevant information for this instance.
106   */
107   std::string asString() const ;
108
109   /**
110      Returns string with low-level format relevant information for this instance.
111      @return string with low-level format relevant information for this instance.
112   */
113   std::string serialize() const ;
114
115   /**
116      Returns XML document with relevant information for this instance.
117      \param parent XML node from which created data will depend on.
118      @return XML document with relevant information for this instance.
119   */
120   xml::Node* asXML(xml::Node* parent) const noexcept(false);
121
122 private:
123   const Device* a_device;
124   int a_port;
125 };
126
127 }
128 }
129
130 #endif
131
132
133