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 ac25b38
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 49 deletions.
4 changes: 2 additions & 2 deletions doc/snippets/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ namespace Corrade { namespace Utility {
template<> struct ConfigurationValue<Foo> {
static std::string toString(const Foo& value, ConfigurationValueFlags flags) {
return
ConfigurationValue<int>::toString(value.a, flags) + ' ' +
ConfigurationValue<int>::toString(value.a, flags) + " " +
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
1 change: 1 addition & 0 deletions src/Corrade/Utility/Arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "Corrade/Containers/Array.h"
#include "Corrade/Containers/StringStl.h"
#include "Corrade/Utility/ConfigurationValue.h"
#include "Corrade/Utility/ConfigurationValueStl.h"
#include "Corrade/Utility/StlForwardVector.h"
#include "Corrade/Utility/Utility.h"
#include "Corrade/Utility/visibility.h"
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
55 changes: 55 additions & 0 deletions src/Corrade/Utility/ConfigurationValueStl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef Corrade_Utility_ConfigurationValueStl_h
#define Corrade_Utility_ConfigurationValueStl_h
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022
Vladimír Vondruš <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

/** @file
@brief STL @ref std::string compatibility for @ref Corrade::Utility::ConfigurationValue
*/

#include <string>
#include "Corrade/Utility/ConfigurationValue.h"
#include "Corrade/Utility/visibility.h"

namespace Corrade { namespace Utility {

/** @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(Containers::StringView value, ConfigurationValueFlags) {
return value;
}
static Containers::String toString(const std::string& value, ConfigurationValueFlags) {
return value;
}
#endif
};

}}

#endif
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 ac25b38

Please sign in to comment.