Embedded Template Library 1.0
Loading...
Searching...
No Matches
type_def.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2016 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_TYPE_DEF_INCLUDED
32#define ETL_TYPE_DEF_INCLUDED
33
34#include "platform.h"
35#include "type_traits.h"
36
37namespace etl
38{
39 #define ETL_TYPEDEF(T, name) class name##_tag; typedef etl::type_def<name##_tag, T> name
40 #define ETL_USING(name, T) class name##_tag; typedef etl::type_def<name##_tag, T> name
41
42 //*************************************************************************
55 //*************************************************************************
56 template <typename TIdType, typename TValue>
58 {
59 public:
60
61 typedef TValue type;
62 typedef TValue value_type;
63 typedef TIdType id_type;
64
65 //*********************************************************************
66 ETL_CONSTEXPR type_def() ETL_NOEXCEPT
67 : value(TValue())
68 {
69 }
70
71 //*********************************************************************
72#if ETL_USING_CPP11
74#else
75 template <typename T>
76#endif
77 ETL_CONSTEXPR type_def(T value_) ETL_NOEXCEPT
78 : value(value_)
79 {
80 }
81
82 //*********************************************************************
83#if ETL_USING_CPP11
84 ETL_CONSTEXPR type_def(const type_def& other) = default;
85#endif
86
87 //*********************************************************************
88 ETL_CONSTEXPR operator TValue() const ETL_NOEXCEPT
89 {
90 return value;
91 }
92
93 //*********************************************************************
94 ETL_CONSTEXPR14 type_def& operator ++() ETL_NOEXCEPT
95 {
96 ++value;
97 return *this;
98 }
99
100 //*********************************************************************
101 ETL_CONSTEXPR14 type_def operator ++(int) ETL_NOEXCEPT
102 {
103 type_def temp(*this);
104 type_def::operator ++();
105 return temp;
106 }
107
108 //*********************************************************************
109 ETL_CONSTEXPR14 type_def& operator --() ETL_NOEXCEPT
110 {
111 --value;
112 return *this;
113 }
114
115 //*********************************************************************
116 ETL_CONSTEXPR14 type_def operator --(int) ETL_NOEXCEPT
117 {
118 type_def temp(*this);
119 type_def::operator --();
120 return temp;
121 }
122
123 //*********************************************************************
124 template <typename T>
125 ETL_CONSTEXPR14
126#if ETL_USING_CPP11
128#else
129 type_def&
130#endif
131 operator +=(T rhs) ETL_NOEXCEPT
132 {
133 value += rhs;
134 return *this;
135 }
136
137 //*********************************************************************
138 ETL_CONSTEXPR14
139 type_def& operator +=(const type_def& rhs) ETL_NOEXCEPT
140 {
141 value += rhs.value;
142 return *this;
143 }
144
145 //*********************************************************************
146 template <typename T>
147 ETL_CONSTEXPR14
148#if ETL_USING_CPP11
150#else
151 type_def&
152#endif
153 operator -=(T rhs) ETL_NOEXCEPT
154 {
155 value -= rhs;
156 return *this;
157 }
158
159 //*********************************************************************
160 ETL_CONSTEXPR14 type_def& operator -=(const type_def& rhs) ETL_NOEXCEPT
161 {
162 value -= rhs.value;
163 return *this;
164 }
165
166 //*********************************************************************
167 template <typename T>
168 ETL_CONSTEXPR14
169#if ETL_USING_CPP11
171#else
172 type_def&
173#endif
174 operator *=(T rhs) ETL_NOEXCEPT
175 {
176 value *= rhs;
177 return *this;
178 }
179
180 //*********************************************************************
181 ETL_CONSTEXPR14 type_def& operator *=(const type_def& rhs) ETL_NOEXCEPT
182 {
183 value *= rhs.value;
184 return *this;
185 }
186
187 //*********************************************************************
188 template <typename T>
189 ETL_CONSTEXPR14
190#if ETL_USING_CPP11
192#else
193 type_def&
194#endif
195 operator /=(T rhs) ETL_NOEXCEPT
196 {
197 value /= rhs;
198 return *this;
199 }
200
201 //*********************************************************************
202 ETL_CONSTEXPR14 type_def& operator /=(const type_def& rhs) ETL_NOEXCEPT
203 {
204 value /= rhs.value;
205 return *this;
206 }
207
208 //*********************************************************************
209 template <typename T>
210 ETL_CONSTEXPR14
211#if ETL_USING_CPP11
213#else
214 type_def&
215#endif
216 operator %=(T rhs) ETL_NOEXCEPT
217 {
218 value %= rhs;
219 return *this;
220 }
221
222 //*********************************************************************
223 ETL_CONSTEXPR14 type_def& operator %=(const type_def& rhs) ETL_NOEXCEPT
224 {
225 value %= rhs.value;
226 return *this;
227 }
228
229 //*********************************************************************
230 template <typename T>
231 ETL_CONSTEXPR14
232#if ETL_USING_CPP11
234#else
235 type_def&
236#endif
237 operator &=(T rhs) ETL_NOEXCEPT
238 {
239 value &= rhs;
240 return *this;
241 }
242
243 //*********************************************************************
244 ETL_CONSTEXPR14 type_def& operator &=(const type_def& rhs) ETL_NOEXCEPT
245 {
246 value &= rhs.value;
247 return *this;
248 }
249
250 //*********************************************************************
251 template <typename T>
252 ETL_CONSTEXPR14
253#if ETL_USING_CPP11
255#else
256 type_def&
257#endif
258 operator |=(T rhs) ETL_NOEXCEPT
259 {
260 value |= rhs;
261 return *this;
262 }
263
264 //*********************************************************************
265 ETL_CONSTEXPR14 type_def& operator |=(const type_def& rhs) ETL_NOEXCEPT
266 {
267 value |= rhs.value;
268 return *this;
269 }
270
271 //*********************************************************************
272 template <typename T>
273 ETL_CONSTEXPR14
274#if ETL_USING_CPP11
276#else
277 type_def&
278#endif
279 operator ^=(T rhs) ETL_NOEXCEPT
280 {
281 value ^= rhs;
282 return *this;
283 }
284
285 //*********************************************************************
286 ETL_CONSTEXPR14 type_def& operator ^=(const type_def& rhs) ETL_NOEXCEPT
287 {
288 value ^= rhs.value;
289 return *this;
290 }
291
292 //*********************************************************************
293 ETL_CONSTEXPR14 type_def& operator <<=(int rhs) ETL_NOEXCEPT
294 {
295 value <<= rhs;
296 return *this;
297 }
298
299 //*********************************************************************
300 ETL_CONSTEXPR14 type_def& operator >>=(int rhs) ETL_NOEXCEPT
301 {
302 value >>= rhs;
303 return *this;
304 }
305
306 //*********************************************************************
307#if ETL_USING_CPP11
308 ETL_CONSTEXPR14 type_def& operator =(const type_def& rhs) = default;
309#endif
310
311 //*********************************************************************
312 TValue& get() ETL_NOEXCEPT
313 {
314 return value;
315 }
316
317 //*********************************************************************
318 ETL_CONSTEXPR const TValue& get() const ETL_NOEXCEPT
319 {
320 return value;
321 }
322
323 //*********************************************************************
324 // + operator
325 //*********************************************************************
326 template <typename T>
327 friend ETL_CONSTEXPR
328#if ETL_USING_CPP11
330#else
332#endif
333 operator +(const type_def& lhs, T rhs) ETL_NOEXCEPT
334 {
335 return type_def(lhs.value + rhs);
336 }
337
338 //*********************************************************************
339 template <typename T>
340 friend ETL_CONSTEXPR type_def operator +(T lhs, const type_def& rhs) ETL_NOEXCEPT
341 {
342 return type_def(lhs + rhs.value);
343 }
344
345 //*********************************************************************
346 friend ETL_CONSTEXPR type_def operator +(const type_def& lhs, const type_def& rhs)
347 {
348 return type_def(lhs.value + rhs.value);
349 }
350
351 //*********************************************************************
352 // - operator
353 //*********************************************************************
354 template <typename T>
355 friend ETL_CONSTEXPR
356#if ETL_USING_CPP11
358#else
360#endif
361 operator -(const type_def& lhs, T rhs) ETL_NOEXCEPT
362 {
363 return type_def(lhs.value - rhs);
364 }
365
366 //*********************************************************************
367 template <typename T>
368 friend ETL_CONSTEXPR
369#if ETL_USING_CPP11
371#else
373#endif
374 operator -(T lhs, const type_def& rhs) ETL_NOEXCEPT
375 {
376 return type_def(lhs - rhs.value);
377 }
378
379 //*********************************************************************
380 friend ETL_CONSTEXPR type_def operator -(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
381 {
382 return type_def(lhs.value - rhs.value);
383 }
384
385 //*********************************************************************
386 // * operator
387 //*********************************************************************
388 template <typename T>
389 friend ETL_CONSTEXPR
390#if ETL_USING_CPP11
392#else
394#endif
395 operator *(const type_def& lhs, T rhs) ETL_NOEXCEPT
396 {
397 return type_def(lhs.value * rhs);
398 }
399
400 //*********************************************************************
401 template <typename T>
402 friend ETL_CONSTEXPR
403#if ETL_USING_CPP11
405#else
407#endif
408 operator *(T lhs, const type_def& rhs) ETL_NOEXCEPT
409 {
410 return type_def(lhs * rhs.value);
411 }
412
413 //*********************************************************************
414 friend ETL_CONSTEXPR type_def operator *(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
415 {
416 return type_def(lhs.value * rhs.value);
417 }
418
419 //*********************************************************************
420 // / operator
421 //*********************************************************************
422 template <typename T>
423 friend ETL_CONSTEXPR
424#if ETL_USING_CPP11
426#else
428#endif
429 operator /(const type_def& lhs, T rhs) ETL_NOEXCEPT
430 {
431 return type_def(lhs.value / rhs);
432 }
433
434 //*********************************************************************
435 template <typename T>
436 friend ETL_CONSTEXPR
437#if ETL_USING_CPP11
439#else
441#endif
442 operator /(T lhs, const type_def& rhs) ETL_NOEXCEPT
443 {
444 return type_def(lhs / rhs.value);
445 }
446
447 //*********************************************************************
448 friend ETL_CONSTEXPR type_def operator /(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
449 {
450 return type_def(lhs.value / rhs.value);
451 }
452
453 //*********************************************************************
454 template <typename T>
455 friend ETL_CONSTEXPR
456#if ETL_USING_CPP11
458#else
460#endif
461 operator %(const type_def& lhs, T rhs) ETL_NOEXCEPT
462 {
463 return type_def(lhs.value % rhs);
464 }
465
466 //*********************************************************************
467 // % operator
468 //*********************************************************************
469 template <typename T>
470 friend ETL_CONSTEXPR
471#if ETL_USING_CPP11
473#else
475#endif
476 operator %(T lhs, const type_def& rhs) ETL_NOEXCEPT
477 {
478 return type_def(lhs % rhs.value);
479 }
480
481 //*********************************************************************
482 friend ETL_CONSTEXPR type_def operator %(const type_def& lhs, const type_def& rhs)
483 {
484 return type_def(lhs.value % rhs.value);
485 }
486
487 //*********************************************************************
488 // & operator
489 //*********************************************************************
490 template <typename T>
491 friend ETL_CONSTEXPR
492#if ETL_USING_CPP11
494#else
496#endif
497 operator &(const type_def& lhs, T rhs) ETL_NOEXCEPT
498 {
499 return type_def(lhs.value & rhs);
500 }
501
502 //*********************************************************************
503 template <typename T>
504 friend ETL_CONSTEXPR
505#if ETL_USING_CPP11
507#else
509#endif
510 operator &(T lhs, const type_def& rhs) ETL_NOEXCEPT
511 {
512 return type_def(lhs & rhs.value);
513 }
514
515 //*********************************************************************
516 friend ETL_CONSTEXPR type_def operator &(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
517 {
518 return type_def(lhs.value & rhs.value);
519 }
520
521 //*********************************************************************
522 // | operator
523 //*********************************************************************
524 template <typename T>
525 friend ETL_CONSTEXPR
526#if ETL_USING_CPP11
528#else
530#endif
531 operator |(const type_def& lhs, T rhs) ETL_NOEXCEPT
532 {
533 return type_def(lhs.value | rhs);
534 }
535
536 //*********************************************************************
537 template <typename T>
538 friend ETL_CONSTEXPR
539#if ETL_USING_CPP11
541#else
543#endif
544 operator |(T lhs, const type_def& rhs) ETL_NOEXCEPT
545 {
546 return type_def(lhs | rhs.value);
547 }
548
549 //*********************************************************************
550 friend ETL_CONSTEXPR type_def operator |(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
551 {
552 return type_def(lhs.value | rhs.value);
553 }
554
555 //*********************************************************************
556 // ^ operator
557 //*********************************************************************
558 template <typename T>
559 friend ETL_CONSTEXPR
560#if ETL_USING_CPP11
562#else
564#endif
565 operator ^(const type_def& lhs, T rhs) ETL_NOEXCEPT
566 {
567 return type_def(lhs.value ^ rhs);
568 }
569
570 //*********************************************************************
571 template <typename T>
572 friend ETL_CONSTEXPR
573#if ETL_USING_CPP11
575#else
577#endif
578 operator ^(T lhs, const type_def& rhs) ETL_NOEXCEPT
579 {
580 return type_def(lhs ^ rhs.value);
581 }
582
583 //*********************************************************************
584 friend ETL_CONSTEXPR type_def operator ^(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
585 {
586 return type_def(lhs.value ^ rhs.value);
587 }
588
589 //*********************************************************************
590 // << operator
591 //*********************************************************************
592 friend ETL_CONSTEXPR type_def operator <<(const type_def& lhs, int rhs) ETL_NOEXCEPT
593 {
594 return type_def(lhs.value << rhs);
595 }
596
597 //*********************************************************************
598 // >> operator
599 //*********************************************************************
600 friend ETL_CONSTEXPR type_def operator >>(const type_def& lhs, int rhs) ETL_NOEXCEPT
601 {
602 return type_def(lhs.value >> rhs);
603 }
604
605 //*********************************************************************
606 // < operator
607 //*********************************************************************
608 template <typename T>
609 friend ETL_CONSTEXPR
610#if ETL_USING_CPP11
612#else
613 bool
614#endif
615 operator <(const type_def& lhs, T rhs) ETL_NOEXCEPT
616 {
617 return lhs.value < rhs;
618 }
619
620 //*********************************************************************
621 template <typename T>
622 friend ETL_CONSTEXPR
623#if ETL_USING_CPP11
625#else
626 bool
627#endif
628 operator <(T lhs, const type_def& rhs) ETL_NOEXCEPT
629 {
630 return lhs < rhs.value;
631 }
632
633 //*********************************************************************
634 friend ETL_CONSTEXPR bool operator <(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
635 {
636 return lhs.value < rhs.value;
637 }
638
639 //*********************************************************************
640 // <= operator
641 //*********************************************************************
642 template <typename T>
643 friend ETL_CONSTEXPR
644#if ETL_USING_CPP11
646#else
647 bool
648#endif
649 operator <=(const type_def& lhs, T rhs) ETL_NOEXCEPT
650 {
651 return lhs.value <= rhs;
652 }
653
654 //*********************************************************************
655 template <typename T>
656 friend ETL_CONSTEXPR
657#if ETL_USING_CPP11
659#else
660 bool
661#endif
662 operator <=(T lhs, const type_def& rhs) ETL_NOEXCEPT
663 {
664 return lhs <= rhs.value;
665 }
666
667 //*********************************************************************
668 friend ETL_CONSTEXPR bool operator <=(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
669 {
670 return lhs.value <= rhs.value;
671 }
672
673 //*********************************************************************
674 // > operator
675 //*********************************************************************
676 template <typename T>
677 friend ETL_CONSTEXPR
678#if ETL_USING_CPP11
680#else
681 bool
682#endif
683 operator >(const type_def& lhs, T rhs) ETL_NOEXCEPT
684 {
685 return lhs.value > rhs;
686 }
687
688 //*********************************************************************
689 template <typename T>
690 friend ETL_CONSTEXPR
691#if ETL_USING_CPP11
693#else
694 bool
695#endif
696 operator >(T lhs, const type_def& rhs) ETL_NOEXCEPT
697 {
698 return lhs > rhs.value;
699 }
700
701 //*********************************************************************
702 friend ETL_CONSTEXPR bool operator >(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
703 {
704 return lhs.value > rhs.value;
705 }
706
707 //*********************************************************************
708 // >= operator
709 //*********************************************************************
710 template <typename T>
711 friend ETL_CONSTEXPR
712#if ETL_USING_CPP11
714#else
715 bool
716#endif
717 operator >=(const type_def& lhs, T rhs) ETL_NOEXCEPT
718 {
719 return lhs.value >= rhs;
720 }
721
722 //*********************************************************************
723 template <typename T>
724 friend ETL_CONSTEXPR
725#if ETL_USING_CPP11
727#else
728 bool
729#endif
730 operator >=(T lhs, const type_def& rhs) ETL_NOEXCEPT
731 {
732 return lhs >= rhs.value;
733 }
734
735 //*********************************************************************
736 friend ETL_CONSTEXPR bool operator >=(const type_def& lhs, const type_def& rhs)
737 {
738 return lhs.value >= rhs.value;
739 }
740
741 //*********************************************************************
742 // == operator
743 //*********************************************************************
744 template <typename T>
745 friend ETL_CONSTEXPR
746#if ETL_USING_CPP11
748#else
749 bool
750#endif
751 operator ==(const type_def& lhs, T rhs) ETL_NOEXCEPT
752 {
753 return lhs.value == rhs;
754 }
755
756 //*********************************************************************
757 template <typename T>
758 friend ETL_CONSTEXPR
759#if ETL_USING_CPP11
761#else
762 bool
763#endif
764 operator ==(T lhs, const type_def& rhs)
765 {
766 return lhs == rhs.value;
767 }
768
769 //*********************************************************************
770 friend ETL_CONSTEXPR bool operator ==(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
771 {
772 return lhs.value == rhs.value;
773 }
774
775 //*********************************************************************
776 // != operator
777 //*********************************************************************
778 template <typename T>
779 friend ETL_CONSTEXPR
780#if ETL_USING_CPP11
782#else
783 bool
784#endif
785 operator !=(const type_def& lhs, T rhs) ETL_NOEXCEPT
786 {
787 return lhs.value != rhs;
788 }
789
790 //*********************************************************************
791 template <typename T>
792 friend ETL_CONSTEXPR
793#if ETL_USING_CPP11
795#else
796 bool
797#endif
798 operator !=(T lhs, const type_def& rhs) ETL_NOEXCEPT
799 {
800 return lhs != rhs.value;
801 }
802
803 //*********************************************************************
804 friend ETL_CONSTEXPR bool operator !=(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
805 {
806 return lhs.value != rhs.value;
807 }
808
809 private:
810
811 TValue value;
812 };
813}
814
815#endif
Definition type_def.h:58
enable_if
Definition type_traits_generator.h:1230
bitset_ext
Definition absolute.h:38
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:314