GCC Code Coverage Report


Directory: ./
File: src/iguana/services/ConcurrentParam.cc
Date: 2025-01-05 09:03:17
Exec Total Coverage
Lines: 25 35 71.4%
Functions: 15 66 22.7%
Branches: 6 22 27.3%

Line Branch Exec Source
1 #include "ConcurrentParam.h"
2
3 namespace iguana {
4
5 // ==================================================================================
6 // Constructors
7 // ==================================================================================
8
9 template <typename T>
10 34 ConcurrentParam<T>::ConcurrentParam(std::string const& model)
11 {
12
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
68 if(GlobalConcurrencyModel() != model)
13 throw std::runtime_error(
14 "attempted to construct a ConcurrentParam with model '" +
15 model + "', but GlobalConcurrencyModel is '" + GlobalConcurrencyModel() + "'");
16 34 }
17
18 template <typename T>
19
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
18 SingleThreadParam<T>::SingleThreadParam() : ConcurrentParam<T>("single")
20 {
21 11 this->m_needs_hashing = false;
22 11 }
23
24 template <typename T>
25
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
46 MemoizedParam<T>::MemoizedParam() : ConcurrentParam<T>("memoize")
26 {
27 23 this->m_needs_hashing = true;
28 23 }
29
30 // ==================================================================================
31 // Load methods
32 // ==================================================================================
33
34 template <typename T>
35 4820 T const SingleThreadParam<T>::Load(concurrent_key_t const key) const
36 {
37 4820 return m_value;
38 }
39
40 template <typename T>
41
1/2
✓ Branch 0 taken 837 times.
✗ Branch 1 not taken.
837 T const MemoizedParam<T>::Load(concurrent_key_t const key) const
42 {
43
1/2
✓ Branch 0 taken 837 times.
✗ Branch 1 not taken.
837 if(auto it{m_container.find(key)}; it != m_container.end())
44 837 return it->second;
45 throw std::runtime_error("MemoizedParam::Load failed to find the parameter");
46 }
47
48 // ==================================================================================
49 // Save methods
50 // ==================================================================================
51
52 template <typename T>
53 11 void SingleThreadParam<T>::Save(T const& value, concurrent_key_t const key)
54 {
55 11 this->m_empty = false;
56 11 m_value = value;
57 11 }
58
59 template <typename T>
60 22 void MemoizedParam<T>::Save(T const& value, concurrent_key_t const key)
61 {
62 22 std::lock_guard<std::mutex> const lock(this->m_mutex);
63
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 this->m_empty = false;
64 22 m_container.insert({key, value});
65 22 }
66
67 // ==================================================================================
68 // HasKey methods
69 // ==================================================================================
70
71 template <typename T>
72 bool SingleThreadParam<T>::HasKey(concurrent_key_t const key) const
73 {
74 throw std::runtime_error("do not call ConcurrentParam::HasKey when model is 'single'");
75 }
76
77 template <typename T>
78 4005 bool MemoizedParam<T>::HasKey(concurrent_key_t const key) const
79 {
80 4005 return m_container.find(key) != m_container.end();
81 }
82
83 // ==================================================================================
84 // GetSize() methods
85 // ==================================================================================
86 template <typename T>
87 std::size_t SingleThreadParam<T>::GetSize() const
88 {
89 return 1;
90 }
91
92 template <typename T>
93 std::size_t MemoizedParam<T>::GetSize() const
94 {
95 return m_container.size();
96 }
97
98 // ==================================================================================
99 // template specializations
100 // ==================================================================================
101
102 template class ConcurrentParam<int>;
103 template class ConcurrentParam<double>;
104 template class ConcurrentParam<std::string>;
105 template class ConcurrentParam<std::vector<int>>;
106 template class ConcurrentParam<std::vector<double>>;
107 template class ConcurrentParam<std::vector<std::string>>;
108
109 template class SingleThreadParam<int>;
110 template class SingleThreadParam<double>;
111 template class SingleThreadParam<std::string>;
112 template class SingleThreadParam<std::vector<int>>;
113 template class SingleThreadParam<std::vector<double>>;
114 template class SingleThreadParam<std::vector<std::string>>;
115
116 template class MemoizedParam<int>;
117 template class MemoizedParam<double>;
118 template class MemoizedParam<std::string>;
119 template class MemoizedParam<std::vector<int>>;
120 template class MemoizedParam<std::vector<double>>;
121 template class MemoizedParam<std::vector<std::string>>;
122
123 }
124