Skip to content

Commit

Permalink
Utility: use Corrade String and StringView in ConfigurationValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Amiard committed Aug 10, 2022
1 parent 6386a97 commit 0b68fd3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 48 deletions.
2 changes: 1 addition & 1 deletion doc/snippets/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ template<> struct ConfigurationValue<Foo> {
ConfigurationValue<int>::toString(value.b, flags);
}

static Foo fromString(const std::string& stringValue, ConfigurationValueFlags flags) {
static Foo fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) {
std::istringstream i{stringValue};
std::string a, b;

Expand Down
35 changes: 14 additions & 21 deletions src/Corrade/Utility/ConfigurationValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@

namespace Corrade { namespace Utility {

Containers::StringView ConfigurationValue<Containers::StringView>::fromString(const std::string& value, ConfigurationValueFlags) {
Containers::StringView ConfigurationValue<Containers::StringView>::fromString(Containers::StringView value, ConfigurationValueFlags) {
return value;
}
std::string ConfigurationValue<Containers::StringView>::toString(const Containers::StringView value, ConfigurationValueFlags) {
Containers::String ConfigurationValue<Containers::StringView>::toString(const Containers::StringView value, ConfigurationValueFlags) {
return value;
}

Containers::String ConfigurationValue<Containers::String>::fromString(const std::string& value, ConfigurationValueFlags) {
Containers::String ConfigurationValue<Containers::String>::fromString(Containers::StringView value, ConfigurationValueFlags) {
return value;
}
std::string ConfigurationValue<Containers::String>::toString(const Containers::String& value, ConfigurationValueFlags) {
Containers::String ConfigurationValue<Containers::String>::toString(const Containers::String& value, ConfigurationValueFlags) {
return value;
}

namespace Implementation {
template<class T> std::string IntegerConfigurationValue<T>::toString(const T& value, ConfigurationValueFlags flags) {
template<class T> Containers::String IntegerConfigurationValue<T>::toString(const T& value, ConfigurationValueFlags flags) {
std::ostringstream stream;

/* Hexadecimal / octal values */
Expand All @@ -65,8 +65,8 @@ namespace Implementation {
return stream.str();
}

template<class T> T IntegerConfigurationValue<T>::fromString(const std::string& stringValue, ConfigurationValueFlags flags) {
if(stringValue.empty()) return T{};
template<class T> T IntegerConfigurationValue<T>::fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) {
if(stringValue.isEmpty()) return T{};

std::istringstream stream{stringValue};

Expand All @@ -93,7 +93,7 @@ namespace Implementation {
template struct IntegerConfigurationValue<long long>;
template struct IntegerConfigurationValue<unsigned long long>;

template<class T> std::string FloatConfigurationValue<T>::toString(const T& value, ConfigurationValueFlags flags) {
template<class T> Containers::String FloatConfigurationValue<T>::toString(const T& value, ConfigurationValueFlags flags) {
std::ostringstream stream;

/* Scientific notation */
Expand All @@ -107,8 +107,8 @@ namespace Implementation {
return stream.str();
}

template<class T> T FloatConfigurationValue<T>::fromString(const std::string& stringValue, ConfigurationValueFlags flags) {
if(stringValue.empty()) return T{};
template<class T> T FloatConfigurationValue<T>::fromString(Containers::StringView stringValue, ConfigurationValueFlags flags) {
if(stringValue.isEmpty()) return T{};

std::istringstream stream{stringValue};

Expand All @@ -130,24 +130,17 @@ namespace Implementation {
}

#ifndef DOXYGEN_GENERATING_OUTPUT
std::string ConfigurationValue<std::string>::fromString(const std::string& value, ConfigurationValueFlags) {
return value;
}
std::string ConfigurationValue<std::string>::toString(const std::string& value, ConfigurationValueFlags) {
return value;
}

bool ConfigurationValue<bool>::fromString(const std::string& value, ConfigurationValueFlags) {
bool ConfigurationValue<bool>::fromString(Containers::StringView value, ConfigurationValueFlags) {
return value == "1" || value == "yes" || value == "y" || value == "true";
}
std::string ConfigurationValue<bool>::toString(const bool value, ConfigurationValueFlags) {
Containers::String ConfigurationValue<bool>::toString(const bool value, ConfigurationValueFlags) {
return value ? "true" : "false";
}

char32_t ConfigurationValue<char32_t>::fromString(const std::string& value, ConfigurationValueFlags) {
char32_t ConfigurationValue<char32_t>::fromString(Containers::StringView value, ConfigurationValueFlags) {
return char32_t(ConfigurationValue<unsigned long long>::fromString(value, ConfigurationValueFlag::Hex|ConfigurationValueFlag::Uppercase));
}
std::string ConfigurationValue<char32_t>::toString(const char32_t value, ConfigurationValueFlags) {
Containers::String ConfigurationValue<char32_t>::toString(const char32_t value, ConfigurationValueFlags) {
return ConfigurationValue<unsigned long long>::toString(value, ConfigurationValueFlag::Hex|ConfigurationValueFlag::Uppercase);
}
#endif
Expand Down
39 changes: 14 additions & 25 deletions src/Corrade/Utility/ConfigurationValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/

#include "Corrade/Containers/EnumSet.h"
#include "Corrade/Utility/StlForwardString.h"
#include "Corrade/Utility/visibility.h"

namespace Corrade { namespace Utility {
Expand Down Expand Up @@ -92,15 +91,15 @@ template<class T> struct ConfigurationValue {
* @param flags Flags
* @return Value as string
*/
static std::string toString(const T& value, ConfigurationValueFlags flags);
static Containers::String toString(const T& value, ConfigurationValueFlags flags);

/**
* @brief Convert value from string
* @param stringValue Value as string
* @param flags Flags
* @return Value
*/
static T fromString(const std::string& stringValue, ConfigurationValueFlags flags);
static T fromString(Containers::StringView stringValue, ConfigurationValueFlags flags);
#endif
};

Expand All @@ -115,8 +114,8 @@ template<> struct CORRADE_UTILITY_EXPORT ConfigurationValue<Containers::StringVi
ConfigurationValue() = delete;

#ifndef DOXYGEN_GENERATING_OUTPUT
static Containers::StringView fromString(const std::string& value, ConfigurationValueFlags flags);
static std::string toString(Containers::StringView value, ConfigurationValueFlags flags);
static Containers::StringView fromString(Containers::StringView value, ConfigurationValueFlags flags);
static Containers::String toString(Containers::StringView value, ConfigurationValueFlags flags);
#endif
};

Expand All @@ -131,23 +130,23 @@ template<> struct CORRADE_UTILITY_EXPORT ConfigurationValue<Containers::String>
ConfigurationValue() = delete;

#ifndef DOXYGEN_GENERATING_OUTPUT
static Containers::String fromString(const std::string& value, ConfigurationValueFlags flags);
static std::string toString(const Containers::String& value, ConfigurationValueFlags flags);
static Containers::String fromString(Containers::StringView value, ConfigurationValueFlags flags);
static Containers::String toString(const Containers::String& value, ConfigurationValueFlags flags);
#endif
};

namespace Implementation {
template<class T> struct CORRADE_UTILITY_EXPORT IntegerConfigurationValue {
IntegerConfigurationValue() = delete;

static std::string toString(const T& value, ConfigurationValueFlags flags);
static T fromString(const std::string& stringValue, ConfigurationValueFlags flags);
static Containers::String toString(const T& value, ConfigurationValueFlags flags);
static T fromString(Containers::StringView stringValue, ConfigurationValueFlags flags);
};
template<class T> struct CORRADE_UTILITY_EXPORT FloatConfigurationValue {
FloatConfigurationValue() = delete;

static std::string toString(const T& value, ConfigurationValueFlags flags);
static T fromString(const std::string& stringValue, ConfigurationValueFlags flags);
static Containers::String toString(const T& value, ConfigurationValueFlags flags);
static T fromString(Containers::StringView stringValue, ConfigurationValueFlags flags);
};
}

Expand Down Expand Up @@ -233,16 +232,6 @@ digits on platforms with 80-bit @cpp long double @ce and 15 digits on platforms
*/
template<> struct ConfigurationValue<long double>: public Implementation::FloatConfigurationValue<long double> {};

/** @brief Configuration value parser and writer for @ref std::string type */
template<> struct CORRADE_UTILITY_EXPORT ConfigurationValue<std::string> {
ConfigurationValue() = delete;

#ifndef DOXYGEN_GENERATING_OUTPUT
static std::string fromString(const std::string& value, ConfigurationValueFlags flags);
static std::string toString(const std::string& value, ConfigurationValueFlags flags);
#endif
};

/**
@brief Configuration value parser and writer for `bool` type
Expand All @@ -253,8 +242,8 @@ template<> struct CORRADE_UTILITY_EXPORT ConfigurationValue<bool> {
ConfigurationValue() = delete;

#ifndef DOXYGEN_GENERATING_OUTPUT
static bool fromString(const std::string& value, ConfigurationValueFlags flags);
static std::string toString(bool value, ConfigurationValueFlags flags);
static bool fromString(Containers::StringView value, ConfigurationValueFlags flags);
static Containers::String toString(bool value, ConfigurationValueFlags flags);
#endif
};

Expand All @@ -267,8 +256,8 @@ template<> struct CORRADE_UTILITY_EXPORT ConfigurationValue<char32_t> {
ConfigurationValue() = delete;

#ifndef DOXYGEN_GENERATING_OUTPUT
static char32_t fromString(const std::string& value, ConfigurationValueFlags);
static std::string toString(char32_t value, ConfigurationValueFlags);
static char32_t fromString(Containers::StringView value, ConfigurationValueFlags);
static Containers::String toString(char32_t value, ConfigurationValueFlags);
#endif
};

Expand Down
2 changes: 1 addition & 1 deletion src/Corrade/Utility/Test/ConfigurationValueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ template<> struct ConfigurationValue<NoDefaultConstructor> {
static std::string toString(NoDefaultConstructor value, ConfigurationValueFlags) {
return std::string(value.a, 'a');
}
static NoDefaultConstructor fromString(const std::string& stringValue, ConfigurationValueFlags) {
static NoDefaultConstructor fromString(Containers::StringView stringValue, ConfigurationValueFlags) {
return NoDefaultConstructor{stringValue.size()};
}
};
Expand Down

0 comments on commit 0b68fd3

Please # to comment.