PandA-2024.02
strong_typedef.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
41 #ifndef STRONG_TYPEDEF_HPP
42 #define STRONG_TYPEDEF_HPP
43 
44 #ifdef NDEBUG
45 #define STRONG_TYPEDEF(original_type, new_type) typedef original_type new_type
46 #define STRONG_TYPEDEF_FORWARD_DECL(original_type, new_type) typedef original_type new_type
47 
48 template <typename Dest, typename Source>
49 inline Dest from_strongtype_cast(Source source)
50 {
51  return static_cast<Dest>(source);
52 }
53 
54 #if 0
55 template<typename Dest, typename Source>
56 inline Dest to_strongtype_cast(Source source)
57 {
58  return static_cast<Dest>(source);
59 }
60 #endif
61 
62 #else
63 #include <boost/functional/hash/hash.hpp>
64 template <typename Dest, typename Source>
65 inline Dest from_strongtype_cast(Source source)
66 {
67  return static_cast<Dest>(source.GetContent());
68 }
69 
70 #if 0
71 template<typename Dest, typename Source>
72 inline Dest to_strongtype_cast(Source source)
73 {
74  return Dest(source);
75 }
76 #endif
77 
78 #define STRONG_TYPEDEF_FORWARD_DECL(OriginalType, NewType) class NewType
79 #define STRONG_TYPEDEF(OriginalType, NewType) \
81  class NewType \
82  { \
83  private: \
84  /*The actual value of the object*/ \
85  OriginalType content; \
86  \
87  public: \
88  /* Explicit constructor */ \
89  explicit NewType(const OriginalType _content) : content(_content) \
90  { \
91  } \
92  \
93  friend std::ostream& operator<<(std::ostream& os, const NewType element); \
94  \
95  inline OriginalType GetContent() const \
96  { \
97  return content; \
98  } \
99  \
100  /* Overloading of -- */ \
101  NewType& operator--() \
102  { \
103  content--; \
104  return *this; \
105  } \
106  \
107  /* Overloading of ++ */ \
108  NewType& operator++() \
109  { \
110  content++; \
111  return *this; \
112  } \
113  \
114  /* Overloading of ++ */ \
115  NewType operator++(int) \
116  { \
117  content++; \
118  return NewType(content - 1); \
119  } \
120  \
121  /* Overloading of + */ \
122  NewType operator+(const NewType& other) const \
123  { \
124  return NewType(content + other.content); \
125  } \
126  NewType operator+(const OriginalType& other) const \
127  { \
128  return NewType(content + other); \
129  } \
130  \
131  /* Overloading of += */ \
132  NewType operator+=(const NewType& other) \
133  { \
134  content += other.content; \
135  return *this; \
136  } \
137  NewType operator+=(const OriginalType& other) \
138  { \
139  content += other; \
140  return *this; \
141  } \
142  \
143  /* Overloading of - */ \
144  NewType operator-(const NewType& other) const \
145  { \
146  return NewType(content - other.content); \
147  } \
148  NewType operator-(const OriginalType& other) const \
149  { \
150  return NewType(content - other); \
151  } \
152  \
153  /* Overloading of - */ \
154  NewType operator-() const \
155  { \
156  return NewType(-content); \
157  } \
158  \
159  /* Overloading of * */ \
160  NewType operator*(const NewType& other) const \
161  { \
162  return NewType(content * other.content); \
163  } \
164  NewType operator*(const OriginalType& other) const \
165  { \
166  return NewType(content * other); \
167  } \
168  \
169  /* Overloading of < */ \
170  bool operator<(const NewType& other) const \
171  { \
172  return content < other.content; \
173  } \
174  bool operator<(const OriginalType& other) const \
175  { \
176  return content < other; \
177  } \
178  \
179  /* Overloading of <= */ \
180  bool operator<=(const NewType& other) const \
181  { \
182  return content <= other.content; \
183  } \
184  \
185  /* Overloading of > */ \
186  bool operator>(const NewType& other) const \
187  { \
188  return content > other.content; \
189  } \
190  bool operator>(const OriginalType& other) const \
191  { \
192  return content > other; \
193  } \
194  \
195  /* Overloading of >= */ \
196  bool operator>=(const NewType& other) const \
197  { \
198  return content >= other.content; \
199  } \
200  \
201  /* Overloading of == */ \
202  bool operator==(const NewType& other) const \
203  { \
204  return content == other.content; \
205  } \
206  bool operator==(const OriginalType& other) const \
207  { \
208  return content == other; \
209  } \
210  \
211  /* Overloading of != */ \
212  bool operator!=(const NewType& other) const \
213  { \
214  return content != other.content; \
215  } \
216  bool operator!=(const OriginalType& other) const \
217  { \
218  return content != other; \
219  } \
220  }; \
221  inline std::ostream& operator<<(std::ostream& os, const NewType element) \
222  { \
223  os << element.content; \
224  return os; \
225  } \
226  \
227  namespace std \
228  { \
229  template <> \
230  struct hash<NewType> : public unary_function<NewType, size_t> \
231  { \
232  size_t operator()(NewType var) const \
233  { \
234  hash<int> hasher; \
235  return hasher(static_cast<int>(var.GetContent())); \
236  } \
237  }; \
238  } \
239  /* Workaround for ;;*/ \
240  class NewType
241 
242 #endif
243 
244 #define UINT_STRONG_TYPEDEF(new_type) STRONG_TYPEDEF(unsigned int, new_type)
245 #define UINT_STRONG_TYPEDEF_FORWARD_DECL(new_type) STRONG_TYPEDEF_FORWARD_DECL(unsigned int, new_type)
246 
247 #endif
Dest from_strongtype_cast(Source source)

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13