Skip to content

Commit

Permalink
Squashed 'externals/coda-oss/' changes from bf7ae4a71..8dee6f7a6
Browse files Browse the repository at this point in the history
8dee6f7a6 revert toString() changes (#728)

git-subtree-dir: externals/coda-oss
git-subtree-split: 8dee6f7a68d560f8bb3469f2d0375bb65e2f532f
  • Loading branch information
Dan Smith committed Aug 29, 2023
1 parent 6c23d5d commit d982f60
Showing 1 changed file with 12 additions and 112 deletions.
124 changes: 12 additions & 112 deletions modules/c++/str/include/str/Convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define CODA_OSS_str_Convert_h_INCLUDED_

#include <cerrno>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
Expand All @@ -33,7 +34,6 @@
#include <sstream>
#include <string>
#include <typeinfo>
#include <type_traits>

#include "config/Exports.h"
#include "coda_oss/string.h"
Expand All @@ -52,112 +52,17 @@ template <typename T> int getPrecision(const std::complex<T>&);
template <typename T> int getPrecision(const types::ComplexInteger<T>&);
#endif

namespace details
{
// Templating (and then specializing) toString() creates all kinds of weird name-look
// problems; avoid trying to work-around all that by just not doing it.
//
// The preferred approach is to make a a toString() free function.
template <typename T>
inline std::string default_toString(const T& value)
{
// Use operator<<() to generate a string value; this may not be quite
// 100% kosher, but it's been long-standing practice in this codebase.
//
// Note that std::to_string() doesn't necessarily generate the same
// output as writing to std::cout; see
// https://en.cppreference.com/w/cpp/string/basic_string/to_string
std::ostringstream buf;
buf.precision(getPrecision(value));
buf << std::boolalpha << value;
return buf.str();
}

// https://stackoverflow.com/a/73594999/19912380
template<int N> struct priority : priority<N - 1> {};
template<> struct priority<0> {};

template<typename T>
inline auto toString_imp(const T& obj, priority<2>) -> decltype(obj.toString(), std::string())
{
return obj.toString(); // member-function
}

template<typename T>
inline auto toString_imp(const T& obj, priority<1>) -> decltype(toString(obj), std::string())
{
return toString(obj); // free function
}

template<typename T>
inline auto toString_imp(const T& obj, priority<0>) -> decltype(default_toString(obj), std::string())
{
return details::default_toString(obj); // our default utility which uses operator<<()
}

// In order, try to call 1) obj.toString() (highest priority), 2) toString(obj),
// and finally 3) toString_(obj) (lowest priority).
template<typename T>
inline auto toString_(const T& obj) -> decltype(toString_imp(obj, priority<2>{}), std::string())
{
return details::toString_imp(obj, priority<2>{});
}
}
// Note that std::to_string() doesn't necessarily generate the same output as writing
// to std::cout; see https://en.cppreference.com/w/cpp/string/basic_string/to_string
template <typename T>
inline std::string toString(const T& value) // no dectype() noise here, leave that in details::toString_()
{
// This breaks the Windows-CMake build on GitHub (when building as an "external" in NITRO)
// ... different compilers or compile-options?
//return details::toString_(value);

return details::default_toString(value);
}

// C++11 has a bunch of overloads, do the same.
// https://en.cppreference.com/w/cpp/string/basic_string/to_string
inline std::string toString(int value)
{
return details::default_toString(value);
}
inline std::string toString(long value)
std::string toString(const T& value)
{
return details::default_toString(value);
}
inline std::string toString(long long value)
{
return details::default_toString(value);
}
inline std::string toString(unsigned value)
{
return details::default_toString(value);
}
inline std::string toString(unsigned long value)
{
return details::default_toString(value);
}
inline std::string toString(unsigned long long value)
{
return details::default_toString(value);
}
inline std::string toString(float value)
{
return details::default_toString(value);
}
inline std::string toString(double value)
{
return details::default_toString(value);
}
inline std::string toString(long double value)
{
return details::default_toString(value);
std::ostringstream buf;
buf.precision(getPrecision(value));
buf << std::boolalpha << value;
return buf.str();
}

// C++ doesn't have these ...
// https://en.cppreference.com/w/cpp/string/basic_string/to_string
inline std::string toString(bool value)
{
return details::default_toString(value);
}
inline std::string toString(uint8_t value)
{
return toString(gsl::narrow<unsigned int>(value));
Expand Down Expand Up @@ -213,19 +118,13 @@ template <typename T>
inline std::string toString(const coda_oss::optional<T>& value)
{
// TODO: handle empty/NULL optional?
return details::default_toString(value.value());
return toString(value.value());
}

template <typename T>
template<typename T>
inline std::string toString(const T& real, const T& imag)
{
return details::default_toString(std::complex<T>(real, imag));
}

template <typename T>
inline std::string toString(const T* ptr)
{
return details::default_toString(ptr);
return toString(std::complex<T>(real, imag));
}

CODA_OSS_API std::wstring toWString(const std::string&); // platform determines Windows-1252 or UTF-8 input and output encoding
Expand All @@ -234,6 +133,7 @@ CODA_OSS_API std::wstring toWString(const str::W1252string&); // platform determ

CODA_OSS_API coda_oss::u8string u8FromWString(const std::wstring&); // platform determines UTF16 or UTF-32 input


template <typename T>
T toType(const std::string& s)
{
Expand Down

0 comments on commit d982f60

Please sign in to comment.