Repository navigation
Expand file tree
/
Copy pathCachedElement.h
More file actions
82 lines (69 loc) · 1.68 KB
/
Copy pathCachedElement.h
File metadata and controls
82 lines (69 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* CachedElement.h
*
* Created on: 21 Dec 2013
* Author: jowr
*/
#ifndef CACHEDELEMENT_H_
#define CACHEDELEMENT_H_
#include "CoolPropTools.h"
#include "DataStructures.h"
namespace CoolProp {
/*!
A class that contains the magic to cache a value.
Includes an "=" assignment operator and casting to boolean
so you can do something like::
double CoolPropStateClassSI::d3phir_dTau3(double tau, double delta){
if (cache.d3phir_dTau3) {
return cache.d3phir_dTau3;
} else {
cache.d3phir_dTau3 = pFluid->d3phir_dTau3(tau,delta);
return cache.d3phir_dTau3;
}
};
*/
class CachedElement {
private:
bool is_cached;
long double value;
public:
/// Default constructor
CachedElement() {
this->clear();
};
/// Function to carry out the caching
void _do_cache(double value)
{
this->value = value;
this->is_cached = true;
}
/// Assignment operator - sets the value and sets the flag
void operator=(const double& value) {
_do_cache(value);
};
/// Cast to boolean, for checking if cached
operator bool() {return is_cached;};
/// Cast to double, for returning value
operator double() {
if (is_cached) {return static_cast<double>(value); }
else {
throw std::exception();
}
}
operator long double() {
if (is_cached) {return value; }
else {
throw std::exception();
}
}
/// Clear the flag and the value
void clear() {
is_cached = false;
this->value = _HUGE;
};
long double &pt(){
return this->value;
}
};
} /* namespace CoolProp */
#endif /* CACHEDELEMENT_H_ */