| Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame^] | 1 | #pragma once | 
| 2 | |||||
| 3 | #include <stdexcept> | ||||
| 4 | |||||
| 5 | namespace utils | ||||
| 6 | { | ||||
| 7 | |||||
| 8 | template <class T, T first, T last> | ||||
| 9 | inline T toEnum(int x) | ||||
| 10 | { | ||||
| 11 | if (x < static_cast<decltype(x)>(first) || | ||||
| 12 | x > static_cast<decltype(x)>(last)) | ||||
| 13 | { | ||||
| 14 | throw std::out_of_range("Value is not in range of enum"); | ||||
| 15 | } | ||||
| 16 | return static_cast<T>(x); | ||||
| 17 | } | ||||
| 18 | } // namespace utils | ||||