1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
19 /** \file
20  * \ingroup freestyle
21  * \brief Identification system
22  */
23 
24 #ifdef WITH_CXX_GUARDEDALLOC
25 #  include "MEM_guardedalloc.h"
26 #endif
27 
28 namespace Freestyle {
29 
30 /*! Class used to tag any object by an id.
31  *  It is made of two unsigned integers.
32  */
33 class Id {
34  public:
35   typedef unsigned id_type;
36 
37   /*! Default constructor */
Id()38   Id()
39   {
40     _first = 0;
41     _second = 0;
42   }
43 
44   /*! Builds an Id from an integer.
45    *  The second number is set to 0.
46    */
Id(id_type id)47   Id(id_type id)
48   {
49     _first = id;
50     _second = 0;
51   }
52 
53   /*! Builds the Id from the two numbers */
Id(id_type ifirst,id_type isecond)54   Id(id_type ifirst, id_type isecond)
55   {
56     _first = ifirst;
57     _second = isecond;
58   }
59 
60   /*! Copy constructor */
Id(const Id & iBrother)61   Id(const Id &iBrother)
62   {
63     _first = iBrother._first;
64     _second = iBrother._second;
65   }
66 
67   /*! Operator= */
68   Id &operator=(const Id &iBrother)
69   {
70     _first = iBrother._first;
71     _second = iBrother._second;
72     return *this;
73   }
74 
75   /*! Returns the first Id number */
getFirst()76   id_type getFirst() const
77   {
78     return _first;
79   }
80 
81   /*! Returns the second Id number */
getSecond()82   id_type getSecond() const
83   {
84     return _second;
85   }
86 
87   /*! Sets the first number constituting the Id */
setFirst(id_type first)88   void setFirst(id_type first)
89   {
90     _first = first;
91   }
92 
93   /*! Sets the second number constituting the Id */
setSecond(id_type second)94   void setSecond(id_type second)
95   {
96     _second = second;
97   }
98 
99   /*! Operator== */
100   bool operator==(const Id &id) const
101   {
102     return ((_first == id._first) && (_second == id._second));
103   }
104 
105   /*! Operator!= */
106   bool operator!=(const Id &id) const
107   {
108     return !((*this) == id);
109   }
110 
111   /*! Operator< */
112   bool operator<(const Id &id) const
113   {
114     if (_first < id._first) {
115       return true;
116     }
117     if (_first == id._first && _second < id._second) {
118       return true;
119     }
120     return false;
121   }
122 
123  private:
124   id_type _first;
125   id_type _second;
126 
127 #ifdef WITH_CXX_GUARDEDALLOC
128   MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Id")
129 #endif
130 };
131 
132 // stream operator
133 inline std::ostream &operator<<(std::ostream &s, const Id &id)
134 {
135   s << "[" << id.getFirst() << ", " << id.getSecond() << "]";
136   return s;
137 }
138 
139 } /* namespace Freestyle */
140