type::Primitive: add getValue(<default-val> method)

This commit is contained in:
lganzzzo 2021-10-29 02:33:04 +03:00
parent 446ad6710d
commit df653886b8
5 changed files with 50 additions and 1 deletions

View File

@ -38,9 +38,13 @@ Now it's much easier to use `oatpp::String` since `oatpp::String` is now wrapper
{
oatpp::String s1 = "Hello";
std::string s2 = s1; // s1 is used a l-value with a typecast operator
std::string s2 = s1; // implicit cast
}
{
oatpp::String s1 = nullptr;
std::string s2 = s1; // implicit cast from null-value trows runtime_error
}
{
oatpp::String s1 = "Hello";
@ -63,6 +67,14 @@ Now it's much easier to use `oatpp::String` since `oatpp::String` is now wrapper
OATPP_LOGD("TEST", "str='%s'", s3->c_str()); // prints 'Hello World'
}
{
oatpp::String s1 = nullptr;
oatpp::String s2 = "hello";
OATPP_ASSERT(s1.getValue("default") == "default")
OATPP_ASSERT(s2.getValue("default") == "hello")
}
```
## ConnectionPool::get() Timeout

View File

@ -79,6 +79,13 @@ bool String::equalsCI_ASCII(const char* other) {
return ciLabel == other;
}
std::string String::getValue(const std::string& defaultValue) {
if(m_ptr) {
return *m_ptr;
}
return defaultValue;
}
String operator + (const char* a, const String& b) {
data::stream::BufferOutputStream stream;
stream << a << b;

View File

@ -209,6 +209,13 @@ public:
*/
bool equalsCI_ASCII(const char* str);
/**
* Get underlying value.
* @param defaultValue - value to return in case stored value is `nullptr`.
* @return - value or `defaultValue` if stored value is `nullptr`.
*/
std::string getValue(const std::string& defaultValue);
template<typename T,
typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
>
@ -346,6 +353,13 @@ public:
return *this->m_ptr;
}
TValueType getValue(const TValueType& defaultValue) {
if(this->m_ptr) {
return *this->m_ptr;
}
return defaultValue;
}
};
/**

View File

@ -244,6 +244,14 @@ void PrimitiveTest::onRun() {
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "check default value");
oatpp::UInt8 s0;
oatpp::UInt8 s1 = 255;
OATPP_ASSERT(s0.getValue(128) == 128)
OATPP_ASSERT(s1.getValue(128) == 255)
}
}
}}}}}}

View File

@ -314,6 +314,14 @@ void StringTest::onRun() {
}
{
OATPP_LOGI(TAG, "check default value");
oatpp::String s0;
oatpp::String s1 = "hello";
OATPP_ASSERT(s0.getValue("def") == "def")
OATPP_ASSERT(s1.getValue("def") == "hello")
}
}
}}}}}}