CoolProp 7.2.0
An open-source fluid property and humid air property database
CachedElement.h
Go to the documentation of this file.
1/*
2 * CachedElement.h
3 *
4 * Created on: 21 Dec 2013
5 * Author: jowr
6 */
7
8#ifndef CACHEDELEMENT_H_
9#define CACHEDELEMENT_H_
10
11#include <algorithm>
12#include <array>
13#include "CoolPropTools.h"
14#include "DataStructures.h"
15#include "CPnumerics.h"
16
17namespace CoolProp {
18
36{
37
38 private:
39 bool is_cached = false;
40 double value;
41
42 public:
45 this->clear();
46 };
47
49 void _do_cache(double value) {
50 this->value = value;
51 this->is_cached = true;
52 }
53
55 void operator=(const double& value) {
56 _do_cache(value);
57 };
58
60 operator bool() {
61 return is_cached;
62 };
63
65 operator double() {
66 if (is_cached) {
67 return static_cast<double>(value);
68 } else {
69 throw std::exception();
70 }
71 }
72#ifndef COOLPROPDBL_MAPS_TO_DOUBLE
73 operator CoolPropDbl() {
74 if (is_cached) {
75 return value;
76 } else {
77 throw std::exception();
78 }
79 }
80#endif
82 void clear() {
83 is_cached = false;
84 this->value = _HUGE;
85 };
86 double& pt() {
87 return this->value;
88 }
89};
90
91template <typename NumType>
93{
94
95 private:
96 NumType& value;
97 bool& is_cached;
98
99 public:
100 // Constructor with value
101 CacheArrayElement(NumType& val, bool& is_cached) : value(val), is_cached(is_cached) {};
102
104 void _do_cache(double value) {
105 this->value = value;
106 this->is_cached = true;
107 }
108
110 void operator=(const double& value) {
111 _do_cache(value);
112 };
113
115 operator bool() {
116 return is_cached;
117 };
118
120 operator double() {
121 if (is_cached) {
122 return static_cast<double>(value);
123 } else {
124 throw std::exception();
125 }
126 }
127#ifndef COOLPROPDBL_MAPS_TO_DOUBLE
128 operator CoolPropDbl() {
129 if (is_cached) {
130 return value;
131 } else {
132 throw std::exception();
133 }
134 }
135#endif
137 void clear() {
138 is_cached = false;
139 this->value = _HUGE;
140 };
141 NumType& pt() {
142 return this->value;
143 }
144};
145
146template <int N>
148{
149
150 private:
151 std::size_t inext = 0;
152 std::array<double, N> m_values = create_filled_array<double, N>(_HUGE);
153 std::array<bool, N> m_cached = create_filled_array<bool, N>(false);
154
155 public:
156 void clear() {
157 memset(m_values.data(), 0, sizeof(m_values));
158 memset(m_cached.data(), false, sizeof(m_cached));
159 }
160 auto factory(std::size_t i) {
161 return CacheArrayElement<double>(m_values[i], m_cached[i]);
162 }
163 auto next() {
164 if (inext > N) {
165 throw ValueError("No more cache elements available");
166 }
167 auto el = factory(inext);
168 inext++;
169 return el;
170 }
171};
172
173} /* namespace CoolProp */
174#endif /* CACHEDELEMENT_H_ */