From b43c009f77a2964882c9d8d88df195ef906338bc Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Tue, 19 Jul 2016 14:29:40 +0200 Subject: No longer uses exceptions for control flow. --- binparse/output.hpp | 70 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/binparse/output.hpp b/binparse/output.hpp index d942fca..4d17175 100644 --- a/binparse/output.hpp +++ b/binparse/output.hpp @@ -49,14 +49,23 @@ template std::ostream& operator<<(std::ostream& os, std::vector const& vec); template -std::ostream& operator<<(std::ostream& os, std::map const& map) { - try { - indent scoped(os); - os << std::endl; - for(auto&& entry : map) { - os << entry.first << ": " << entry.second << std::endl; - } - } catch(std::bad_cast&) { +std::ostream& operator<<(otreestream& os, std::map const& map) { + indent scoped(os); + os << std::endl; + for(auto&& entry : map) { + os << entry.first << ": " << entry.second << std::endl; + } + + return os; +} + +template +std::ostream& operator<<(std::ostream& os, std::map const& map) { + auto* tree_os = dynamic_cast(&os); + + if(tree_os) { + *tree_os << map; + } else { os << std::endl; for(auto&& entry : map) { os << entry.first << ": " << entry.second << std::endl; @@ -66,25 +75,32 @@ std::ostream& operator<<(std::ostream& os, std::map const& map) { return os; } +template +std::ostream& operator<<(otreestream& os, std::vector const& vec) { + indent scoped(os); + os << std::endl; + for(auto&& entry : vec) { + os << entry << std::endl; + } + + return os; +} + template std::ostream& operator<<(std::ostream& os, std::vector const& vec) { - try { - indent scoped(os); - os << std::endl; - for(auto&& entry : vec) { - os << entry << std::endl; - } - } catch(std::bad_cast&) { + auto* tree_os = dynamic_cast(&os); + if(tree_os) { + *tree_os << vec; + } else { os << std::endl; for(auto&& entry : vec) { os << entry << std::endl; } } + return os; } - - template std::ostream& output_impl(std::ostream& os, T const&, indices<>) { return os; @@ -98,15 +114,25 @@ std::ostream& output_impl(std::ostream& os, T const& t, indices) } +template ::value>::type*> +std::ostream& operator<<(otreestream& os, T const& t) { + typedef typename build_indices::value>::type indices; + + indent scoped(os); + os << std::endl; + output_impl(os, t, indices{}); + + return os; +} + template ::value>::type*> std::ostream& operator<<(std::ostream& os, T const& t) { typedef typename build_indices::value>::type indices; - try { - indent scoped(os); - os << std::endl; - output_impl(os, t, indices{}); - } catch(std::bad_cast&) { + auto* tree_os = dynamic_cast(&os); + if(tree_os) { + *tree_os << t; + } else { os << std::endl; output_impl(os, t, indices{}); } -- cgit v1.2.3-70-g09d2