1
0
Fork 0

🍻 get rid of all specialised container output code 🍻 (#8798)

This commit is contained in:
Kaveh Vahedipour 2019-04-18 17:50:35 +02:00 committed by Jan
parent 425b40d9a2
commit fe65959a05
2 changed files with 86 additions and 103 deletions

View File

@ -279,3 +279,8 @@ void TRI_FlushDebugging(char const* file, int line, char const* message) {
Logger::flush();
Logger::shutdown();
}
template<> char const conpar<true>::open = '{';
template<> char const conpar<true>::close = '}';
template<> char const conpar<false>::open = '[';
template<> char const conpar<false>::close = ']';

View File

@ -19,6 +19,7 @@
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Jan Steemann
/// @author Kaveh Vahedipour
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGODB_BASICS_DEBUGGING_H
@ -99,6 +100,67 @@ void TRI_LogBacktrace();
void TRI_FlushDebugging();
void TRI_FlushDebugging(char const* file, int line, char const* message);
////////////////////////////////////////////////////////////////////////////////
/// @brief container traits
////////////////////////////////////////////////////////////////////////////////
namespace container_traits {
using tc = char[2];
template<typename T> struct is_container {
static tc& test(...);
template <typename U>
static char test(U&&, decltype(std::begin(std::declval<U>()))* = 0);
static constexpr bool value = sizeof(test(std::declval<T>())) == 1;
};
template < typename T > struct is_associative {
static tc& test(...) ;
template < typename U >
static char test(U&&, typename U::key_type* = 0) ;
static constexpr bool value = sizeof( test( std::declval<T>() ) ) == 1 ;
};
}
template < typename T > struct is_container :
std::conditional<(container_traits::is_container<T>::value || std::is_array<T>::value)
&& !std::is_same<char *, typename std::decay<T>::type>::value
&& !std::is_same<char const*, typename std::decay<T>::type>::value
&& !std::is_same<unsigned char *, typename std::decay<T>::type>::value
&& !std::is_same<unsigned char const*, typename std::decay<T>::type>::value
&& !std::is_same<T, std::string>::value
&& !std::is_same<T, const std::string>::value, std::true_type, std::false_type >::type {};
template < typename T > struct is_associative :
std::conditional< container_traits::is_container<T>::value && container_traits::is_associative<T>::value,
std::true_type, std::false_type >::type {};
////////////////////////////////////////////////////////////////////////////////
/// @brief no std::enable_if_t in c++11
////////////////////////////////////////////////////////////////////////////////
#if __cplusplus <= 201103L
namespace std {
template< bool B, class T = void >
using enable_if_t = typename std::enable_if<B,T>::type;
}
#endif
////////////////////////////////////////////////////////////////////////////////
/// @brief forward declaration for pair output below
////////////////////////////////////////////////////////////////////////////////
template<typename T>
std::enable_if_t<is_container<T>::value, std::ostream&>
operator<< (std::ostream& o, T const& t);
////////////////////////////////////////////////////////////////////////////////
/// @brief dump pair contents to an ostream
////////////////////////////////////////////////////////////////////////////////
@ -113,115 +175,31 @@ std::ostream& operator<<(std::ostream& stream, std::pair<T1, T2> const& obj) {
/// @brief dump vector contents to an ostream
////////////////////////////////////////////////////////////////////////////////
template <typename T>
std::ostream& operator<<(std::ostream& stream, std::vector<T> const& data) {
bool first = true;
template<bool b>
struct conpar {
static char const open;
static char const close;
};
stream << "[";
for (auto const& it : data) {
template< bool B, class T = void >
using enable_if_t = typename std::enable_if<B,T>::type;
template<typename T>
enable_if_t<is_container<T>::value, std::ostream&>
operator<< (std::ostream& o, T const& t) {
o << conpar<is_associative<T>::value>::open;
bool first = true;
for (auto const& i : t) {
if (first) {
stream << " ";
o << " ";
first = false;
} else {
stream << ", ";
o << ", ";
}
stream << it;
o << i ;
}
stream << " ]";
return stream;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief dump deque contents to an ostream
////////////////////////////////////////////////////////////////////////////////
template <typename T>
std::ostream& operator<<(std::ostream& stream, std::deque<T> const& data) {
bool first = true;
stream << "[";
for (auto const& it : data) {
if (first) {
stream << " ";
first = false;
} else {
stream << ", ";
}
stream << it;
}
stream << " ]";
return stream;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief dump unordered_set contents to an ostream
////////////////////////////////////////////////////////////////////////////////
template <typename T>
std::ostream& operator<<(std::ostream& stream, std::unordered_set<T> const& data) {
bool first = true;
stream << "{";
for (auto const& it : data) {
if (first) {
stream << " ";
first = false;
} else {
stream << ", ";
}
stream << it;
}
stream << " }";
return stream;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief dump unordered_map contents to an ostream
////////////////////////////////////////////////////////////////////////////////
template <typename K, typename V>
std::ostream& operator<<(std::ostream& stream, std::unordered_map<K, V> const& data) {
bool first = true;
stream << "{";
for (auto const& it : data) {
if (first) {
stream << " ";
first = false;
} else {
stream << ", ";
}
stream << it.first << ": " << it.second;
}
stream << " }";
return stream;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief dump map contents to an ostream
////////////////////////////////////////////////////////////////////////////////
template <typename K, typename V>
std::ostream& operator<<(std::ostream& stream, std::map<K, V> const& data) {
bool first = true;
stream << "{";
for (auto const& it : data) {
if (first) {
stream << " ";
first = false;
} else {
stream << ", ";
}
stream << it.first << ": " << it.second;
}
stream << " }";
return stream;
o << " " << conpar<is_associative<T>::value>::close;
return o;
}
#endif