/src/quantlib/ql/utilities/null.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
5 | | Copyright (C) 2003, 2004 StatPro Italia srl |
6 | | Copyright (C) 2010 Kakhkhor Abdijalilov |
7 | | |
8 | | This file is part of QuantLib, a free-software/open-source library |
9 | | for financial quantitative analysts and developers - http://quantlib.org/ |
10 | | |
11 | | QuantLib is free software: you can redistribute it and/or modify it |
12 | | under the terms of the QuantLib license. You should have received a |
13 | | copy of the license along with this program; if not, please email |
14 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
15 | | <https://www.quantlib.org/license.shtml>. |
16 | | |
17 | | This program is distributed in the hope that it will be useful, but WITHOUT |
18 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
19 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
20 | | */ |
21 | | |
22 | | /*! \file null.hpp |
23 | | \brief null values |
24 | | */ |
25 | | |
26 | | #ifndef quantlib_null_hpp |
27 | | #define quantlib_null_hpp |
28 | | |
29 | | #include <ql/types.hpp> |
30 | | #include <type_traits> |
31 | | #include <limits> |
32 | | |
33 | | namespace QuantLib { |
34 | | |
35 | | #ifdef QL_NULL_AS_FUNCTIONS |
36 | | |
37 | | //! template function providing a null value for a given type. |
38 | | template <typename T> |
39 | | constexpr T Null() { |
40 | | if constexpr (std::is_floating_point_v<T>) { |
41 | | // a specific, unlikely value that should fit into any Real |
42 | | return (std::numeric_limits<float>::max)(); |
43 | | } else if constexpr (std::is_integral_v<T>) { |
44 | | // this should fit into any Integer |
45 | | return (std::numeric_limits<int>::max)(); |
46 | | } else { |
47 | | return T(); |
48 | | } |
49 | | } |
50 | | |
51 | | #else |
52 | | |
53 | | //! template class providing a null value for a given type. |
54 | | template <class Type> |
55 | | class Null; |
56 | | |
57 | | // default implementation for built-in types |
58 | | template <typename T> |
59 | | class Null { |
60 | | public: |
61 | | constexpr Null() = default; |
62 | 77.0M | constexpr operator T() const { |
63 | 77.0M | if constexpr (std::is_floating_point_v<T>) { |
64 | | // a specific, unlikely value that should fit into any Real |
65 | 77.0M | return (std::numeric_limits<float>::max)(); |
66 | 77.0M | } else if constexpr (std::is_integral_v<T>) { |
67 | | // this should fit into any Integer |
68 | 2.91k | return (std::numeric_limits<int>::max)(); |
69 | 2.91k | } else { |
70 | 0 | return T(); |
71 | 0 | } |
72 | 77.0M | } QuantLib::Null<double>::operator double() const Line | Count | Source | 62 | 77.0M | constexpr operator T() const { | 63 | 77.0M | if constexpr (std::is_floating_point_v<T>) { | 64 | | // a specific, unlikely value that should fit into any Real | 65 | 77.0M | return (std::numeric_limits<float>::max)(); | 66 | | } else if constexpr (std::is_integral_v<T>) { | 67 | | // this should fit into any Integer | 68 | | return (std::numeric_limits<int>::max)(); | 69 | | } else { | 70 | | return T(); | 71 | | } | 72 | 77.0M | } |
QuantLib::Null<unsigned int>::operator unsigned int() const Line | Count | Source | 62 | 2.91k | constexpr operator T() const { | 63 | | if constexpr (std::is_floating_point_v<T>) { | 64 | | // a specific, unlikely value that should fit into any Real | 65 | | return (std::numeric_limits<float>::max)(); | 66 | 2.91k | } else if constexpr (std::is_integral_v<T>) { | 67 | | // this should fit into any Integer | 68 | 2.91k | return (std::numeric_limits<int>::max)(); | 69 | | } else { | 70 | | return T(); | 71 | | } | 72 | 2.91k | } |
Unexecuted instantiation: QuantLib::Null<unsigned long>::operator unsigned long() const Unexecuted instantiation: QuantLib::Null<QuantLib::Date>::operator QuantLib::Date() const Unexecuted instantiation: QuantLib::Null<int>::operator int() const Unexecuted instantiation: QuantLib::Null<QuantLib::IntervalPrice>::operator QuantLib::IntervalPrice() const |
73 | | }; |
74 | | |
75 | | #endif |
76 | | |
77 | | } |
78 | | |
79 | | |
80 | | #endif |