CoolProp 6.8.1dev
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
37{
38
39private:
40 bool is_cached = false;
41 double value;
42
43public:
46 this->clear();
47 };
48
50 void _do_cache(double value) {
51 this->value = value;
52 this->is_cached = true;
53 }
54
56 void operator=(const double& value) {
57 _do_cache(value);
58 };
59
61 operator bool() {
62 return is_cached;
63 };
64
66 operator double() {
67 if (is_cached) {
68 return static_cast<double>(value);
69 } else {
70 throw std::exception();
71 }
72 }
73#ifndef COOLPROPDBL_MAPS_TO_DOUBLE
74 operator CoolPropDbl() {
75 if (is_cached) {
76 return value;
77 } else {
78 throw std::exception();
79 }
80 }
81#endif
83 void clear() {
84 is_cached = false;
85 this->value = _HUGE;
86 };
87 double& pt() {
88 return this->value;
89 }
90};
91
92template<typename NumType>
94{
95
96private:
97 NumType& value;
98 bool& is_cached;
99
100public:
101
102 // Constructor with value
103 CacheArrayElement(NumType& val, bool& is_cached) : value(val), is_cached(is_cached) {};
104
106 void _do_cache(double value) {
107 this->value = value;
108 this->is_cached = true;
109 }
110
112 void operator=(const double& value) {
113 _do_cache(value);
114 };
115
117 operator bool() {
118 return is_cached;
119 };
120
122 operator double() {
123 if (is_cached) {
124 return static_cast<double>(value);
125 } else {
126 throw std::exception();
127 }
128 }
129#ifndef COOLPROPDBL_MAPS_TO_DOUBLE
130 operator CoolPropDbl() {
131 if (is_cached) {
132 return value;
133 } else {
134 throw std::exception();
135 }
136 }
137#endif
139 void clear() {
140 is_cached = false;
141 this->value = _HUGE;
142 };
143 NumType& pt() {
144 return this->value;
145 }
146};
147
148
149template<int N>
151
152private:
153
154 std::size_t inext = 0;
155 std::array<double, N> m_values = create_filled_array<double, N>(_HUGE);
156 std::array<bool, N> m_cached = create_filled_array<bool, N>(false);
157
158 public:
159 void clear(){
160 memset(m_values.data(), 0, sizeof(m_values));
161 memset(m_cached.data(), false, sizeof(m_cached));
162 }
163 auto factory(std::size_t i){
164 return CacheArrayElement<double>(m_values[i], m_cached[i]);
165 }
166 auto next(){
167 if (inext > N){
168 throw ValueError("No more cache elements available");
169 }
170 auto el = factory(inext);
171 inext++;
172 return el;
173 }
174};
175
176} /* namespace CoolProp */
177#endif /* CACHEDELEMENT_H_ */