mirror of
https://github.com/oatpp/oatpp.git
synced 2024-11-21 03:14:51 +08:00
type::Primitive: add getValue(<default-val> method)
This commit is contained in:
parent
446ad6710d
commit
df653886b8
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}}}}}}
|
@ -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")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}}}}}}
|
||||
|
Loading…
Reference in New Issue
Block a user