Skip to content

Commit 6b7800b

Browse files
committed
Reduced the code duplication and switched to faster format function.
1 parent 48e31fe commit 6b7800b

File tree

2 files changed

+44
-75
lines changed

2 files changed

+44
-75
lines changed

src/lib/zswapobject/zswapobject.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,30 @@
1919

2020
#include "zswapobject/zswapobject.hpp"
2121

22-
bool ZSwapObject::CheckPercent(const std::string& Value) const
22+
void ZSwapObject::CheckValueBool(const std::string& Name, const std::string& Value) const
2323
{
24-
return !std::regex_match(Value, std::regex("^\\d{1,2}|100$"));
24+
if (!std::regex_match(Value, std::regex("^[YN]$"))) throw std::invalid_argument(std::format("The requested value for the \"{0}\" variable is incorrect (only Y or N are supported).", Name));
2525
}
2626

27-
bool ZSwapObject::CheckEnabled(const std::string& Value) const
27+
void ZSwapObject::CheckValueEmpty(const std::string& Name, const std::string& Value) const
2828
{
29-
return !std::regex_match(Value, std::regex("^[YN]$"));
29+
if (Value.empty()) throw std::invalid_argument(std::format("The requested value for the \"{0}\" variable is empty.", Name));
30+
}
31+
32+
void ZSwapObject::CheckValueRange(const std::string& Name, const std::string& Value) const
33+
{
34+
if (!std::regex_match(Value, std::regex("^\\d{1,2}|100$"))) throw std::invalid_argument(std::format("The requested value for the \"{0}\" variable is out of range [0..100].", Name));
3035
}
3136

3237
void ZSwapObject::WriteLogEntry(const std::string& Name, const std::string& Value) const
3338
{
34-
std::cout << std::vformat(ZSwapMessageLog, std::make_format_args(Name, Value)) << std::endl;
39+
std::cout << std::format("Writing a new value \"{1}\" to the \"{0}\" variable.", Name, Value) << std::endl;
3540
}
3641

3742
void ZSwapObject::WriteZSwapValue(const std::string& Name, const std::string& Value) const
3843
{
3944
const std::string FullPath = ZSwapModuleParametersPath + Name;
40-
if (!std::filesystem::exists(FullPath)) throw std::runtime_error(std::vformat(ZSwapErrorKernel, std::make_format_args(Name)));
45+
if (!std::filesystem::exists(FullPath)) throw std::runtime_error(std::format("Configuring the \"{0}\" variable is not possible on current kernel!", Name));
4146
std::ofstream ZSwapSysFs(FullPath);
4247
ZSwapSysFs << Value;
4348
ZSwapSysFs.close();
@@ -61,7 +66,7 @@ std::string ZSwapObject::GetZSwapEnabled() const
6166

6267
void ZSwapObject::SetZSwapEnabled(const std::string& Value) const
6368
{
64-
if (CheckEnabled(Value)) throw std::invalid_argument(std::vformat(ZSwapErrorBool, std::make_format_args(ZSwapEnabledName)));
69+
CheckValueBool(ZSwapEnabledName, Value);
6570
WriteZSwapValue(ZSwapEnabledName, Value);
6671
WriteLogEntry(ZSwapEnabledName, Value);
6772
}
@@ -73,7 +78,7 @@ std::string ZSwapObject::GetZSwapSameFilledPages() const
7378

7479
void ZSwapObject::SetZSwapSameFilledPages(const std::string& Value) const
7580
{
76-
if (CheckEnabled(Value)) throw std::invalid_argument(std::vformat(ZSwapErrorBool, std::make_format_args(ZSwapSameFilledPagesName)));
81+
CheckValueBool(ZSwapSameFilledPagesName, Value);
7782
WriteZSwapValue(ZSwapSameFilledPagesName, Value);
7883
WriteLogEntry(ZSwapSameFilledPagesName, Value);
7984
}
@@ -85,7 +90,7 @@ std::string ZSwapObject::GetZSwapMaxPoolPercent() const
8590

8691
void ZSwapObject::SetZSwapMaxPoolPercent(const std::string& Value) const
8792
{
88-
if (CheckPercent(Value)) throw std::invalid_argument(std::vformat(ZSwapErrorRange, std::make_format_args(ZSwapMaxPoolPercentName)));
93+
CheckValueRange(ZSwapMaxPoolPercentName, Value);
8994
WriteZSwapValue(ZSwapMaxPoolPercentName, Value);
9095
WriteLogEntry(ZSwapMaxPoolPercentName, Value);
9196
}
@@ -97,7 +102,7 @@ std::string ZSwapObject::GetZSwapCompressor() const
97102

98103
void ZSwapObject::SetZSwapCompressor(const std::string& Value) const
99104
{
100-
if (Value.empty()) throw std::invalid_argument(std::vformat(ZSwapErrorEmpty, std::make_format_args(ZSwapCompressorName)));
105+
CheckValueEmpty(ZSwapCompressorName, Value);
101106
WriteZSwapValue(ZSwapCompressorName, Value);
102107
WriteLogEntry(ZSwapCompressorName, Value);
103108
}
@@ -109,7 +114,7 @@ std::string ZSwapObject::GetZSwapZpool() const
109114

110115
void ZSwapObject::SetZSwapZpool(const std::string& Value) const
111116
{
112-
if (Value.empty()) throw std::invalid_argument(std::vformat(ZSwapErrorEmpty, std::make_format_args(ZSwapZpoolName)));
117+
CheckValueEmpty(ZSwapZpoolName, Value);
113118
WriteZSwapValue(ZSwapZpoolName, Value);
114119
WriteLogEntry(ZSwapZpoolName, Value);
115120
}
@@ -121,7 +126,7 @@ std::string ZSwapObject::GetZSwapAcceptThresholdPercent() const
121126

122127
void ZSwapObject::SetZSwapAcceptThresholdPercent(const std::string& Value) const
123128
{
124-
if (CheckPercent(Value)) throw std::invalid_argument(std::vformat(ZSwapErrorRange, std::make_format_args(ZSwapAcceptThresholdPercentName)));
129+
CheckValueRange(ZSwapAcceptThresholdPercentName, Value);
125130
WriteZSwapValue(ZSwapAcceptThresholdPercentName, Value);
126131
WriteLogEntry(ZSwapAcceptThresholdPercentName, Value);
127132
}
@@ -133,7 +138,7 @@ std::string ZSwapObject::GetZSwapNonSameFilledPages() const
133138

134139
void ZSwapObject::SetZSwapNonSameFilledPages(const std::string& Value) const
135140
{
136-
if (CheckEnabled(Value)) throw std::invalid_argument(std::vformat(ZSwapErrorBool, std::make_format_args(ZSwapNonSameFilledPagesName)));
141+
CheckValueBool(ZSwapNonSameFilledPagesName, Value);
137142
WriteZSwapValue(ZSwapNonSameFilledPagesName, Value);
138143
WriteLogEntry(ZSwapNonSameFilledPagesName, Value);
139144
}
@@ -145,7 +150,7 @@ std::string ZSwapObject::GetZSwapExclusiveLoads() const
145150

146151
void ZSwapObject::SetZSwapExclusiveLoads(const std::string& Value) const
147152
{
148-
if (CheckEnabled(Value)) throw std::invalid_argument(std::vformat(ZSwapErrorBool, std::make_format_args(ZSwapExclusiveLoadsName)));
153+
CheckValueBool(ZSwapExclusiveLoadsName, Value);
149154
WriteZSwapValue(ZSwapExclusiveLoadsName, Value);
150155
WriteLogEntry(ZSwapExclusiveLoadsName, Value);
151156
}
@@ -157,7 +162,7 @@ std::string ZSwapObject::GetZSwapShrinkerEnabled() const
157162

158163
void ZSwapObject::SetZSwapShrinkerEnabled(const std::string& Value) const
159164
{
160-
if (CheckEnabled(Value)) throw std::invalid_argument(std::vformat(ZSwapErrorBool, std::make_format_args(ZSwapShrinkerEnabledName)));
165+
CheckValueBool(ZSwapShrinkerEnabledName, Value);
161166
WriteZSwapValue(ZSwapShrinkerEnabledName, Value);
162167
WriteLogEntry(ZSwapShrinkerEnabledName, Value);
163168
}

src/lib/zswapobject/zswapobject.hpp

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class ZSwapObject
3434
/**
3535
* Sets the ZSwap enabled value.
3636
* @param Value New value.
37-
* @exception Raises an instance of std::invalid_argument if cannot set
38-
* the proposed value.
3937
*/
4038
void SetZSwapEnabled(const std::string&) const;
4139

@@ -48,8 +46,6 @@ class ZSwapObject
4846
/**
4947
* Sets the same filled pages enabled value.
5048
* @param Value New value.
51-
* @exception Raises an instance of std::invalid_argument if cannot set
52-
* the proposed value.
5349
*/
5450
void SetZSwapSameFilledPages(const std::string&) const;
5551

@@ -62,8 +58,6 @@ class ZSwapObject
6258
/**
6359
* Sets the maximum pool percentage value.
6460
* @param Value New value.
65-
* @exception Raises an instance of std::invalid_argument if cannot set
66-
* the proposed value.
6761
*/
6862
void SetZSwapMaxPoolPercent(const std::string&) const;
6963

@@ -76,8 +70,6 @@ class ZSwapObject
7670
/**
7771
* Sets the compression algorithm name.
7872
* @param Value New value.
79-
* @exception Raises an instance of std::invalid_argument if cannot set
80-
* the proposed value.
8173
*/
8274
void SetZSwapCompressor(const std::string&) const;
8375

@@ -90,8 +82,6 @@ class ZSwapObject
9082
/**
9183
* Sets the kernel's zpool type.
9284
* @param Value New value.
93-
* @exception Raises an instance of std::invalid_argument if cannot set
94-
* the proposed value.
9585
*/
9686
void SetZSwapZpool(const std::string&) const;
9787

@@ -104,8 +94,6 @@ class ZSwapObject
10494
/**
10595
* Sets the accept threshold percentage value.
10696
* @param Value New value.
107-
* @exception Raises an instance of std::invalid_argument if cannot set
108-
* the proposed value.
10997
*/
11098
void SetZSwapAcceptThresholdPercent(const std::string&) const;
11199

@@ -118,8 +106,6 @@ class ZSwapObject
118106
/**
119107
* Sets the non same filled pages enabled value.
120108
* @param Value New value.
121-
* @exception Raises an instance of std::invalid_argument if cannot set
122-
* the proposed value.
123109
*/
124110
void SetZSwapNonSameFilledPages(const std::string&) const;
125111

@@ -132,8 +118,6 @@ class ZSwapObject
132118
/**
133119
* Sets the exclusive loads enabled value.
134120
* @param Value New value.
135-
* @exception Raises an instance of std::invalid_argument if cannot set
136-
* the proposed value.
137121
*/
138122
void SetZSwapExclusiveLoads(const std::string&) const;
139123

@@ -146,8 +130,6 @@ class ZSwapObject
146130
/**
147131
* Sets the shrinker enabled value.
148132
* @param Value New value.
149-
* @exception Raises an instance of std::invalid_argument if cannot set
150-
* the proposed value.
151133
*/
152134
void SetZSwapShrinkerEnabled(const std::string&) const;
153135

@@ -157,31 +139,6 @@ class ZSwapObject
157139
*/
158140
bool IsAvailable() const;
159141
private:
160-
/**
161-
* Stores the ZSwap log message template.
162-
*/
163-
const std::string ZSwapMessageLog = "Writing a new value \"{1}\" to the \"{0}\" variable.";
164-
165-
/**
166-
* Stores the ZSwap error message template for Y or N values.
167-
*/
168-
const std::string ZSwapErrorBool = "The requested value for the \"{0}\" variable is incorrect (only Y or N are supported).";
169-
170-
/**
171-
* Stores the ZSwap error message template for range values.
172-
*/
173-
const std::string ZSwapErrorRange = "The requested value for the \"{0}\" variable is out of range [0..100].";
174-
175-
/**
176-
* Stores the ZSwap error message template for empty values.
177-
*/
178-
const std::string ZSwapErrorEmpty = "The requested value for the \"{0}\" variable is empty.";
179-
180-
/**
181-
* Stores the ZSwap error message template for incorrect kernel version.
182-
*/
183-
const std::string ZSwapErrorKernel = "Configuring the \"{0}\" variable is not possible on current kernel!";
184-
185142
/**
186143
* Stores the ZSwap kernel module parameters path.
187144
*/
@@ -242,36 +199,43 @@ class ZSwapObject
242199
/**
243200
* Reads the value of the ZSwap kernel module by the specified name.
244201
* @param Name Value name.
245-
* @exception Raises an instance of std::invalid_argument if cannot set
246-
* the proposed value.
247-
* @returns Value.
202+
* @returns Value or N/A if the variable is not supported.
248203
*/
249204
std::string ReadZSwapValue(const std::string&) const;
250205

251206
/**
252207
* Prints the log entry to the standard output.
253-
* @param Name Parameter name.
254-
* @param Value Parameter value.
208+
* @param Name Variable name.
209+
* @param Value Variable value.
255210
*/
256211
void WriteLogEntry(const std::string&, const std::string&) const;
257212

258213
/**
259-
* Checks if the value is in the [0..100] range.
260-
* @param Value Value to check.
261-
* @returns Check results.
262-
* @retval true If the value is outside the [0..100] range.
263-
* @retval false Otherwise.
214+
* Checks if the value is Y or N.
215+
* @param Name Variable name.
216+
* @param Value Variable value to check.
217+
* @exception Raises an instance of std::invalid_argument if the
218+
* value does not match the criteria.
264219
*/
265-
bool CheckPercent(const std::string&) const;
220+
void CheckValueBool(const std::string&, const std::string&) const;
266221

267222
/**
268-
* Checks if the value is Y or N.
269-
* @param Value Value to check.
270-
* @returns Check results.
271-
* @retval true If the value does not meet the specified criteria.
272-
* @retval false Otherwise.
223+
* Checks if the value is empty.
224+
* @param Name Variable name.
225+
* @param Value Variable value to check.
226+
* @exception Raises an instance of std::invalid_argument if the
227+
* value does not match the criteria.
228+
*/
229+
void CheckValueEmpty(const std::string&, const std::string&) const;
230+
231+
/**
232+
* Checks if the value is in the [0..100] range.
233+
* @param Name Variable name.
234+
* @param Value Variable value to check.
235+
* @exception Raises an instance of std::invalid_argument if the
236+
* value does not match the criteria.
273237
*/
274-
bool CheckEnabled(const std::string&) const;
238+
void CheckValueRange(const std::string&, const std::string&) const;
275239
};
276240

277241
#endif // ZSWAPOBJECT_HPP

0 commit comments

Comments
 (0)