Commit 3380f55b authored by GILLES Sebastien's avatar GILLES Sebastien

#1470 Introduce a facility to handle the separators properly (not yet in use).

parent d362e120
......@@ -12,12 +12,69 @@
# define MOREFEM_x_UTILITIES_x_CONTAINERS_x_INTERNAL_x_PRINT_HPP_
# include <tuple>
# include <string>
namespace MoReFEM::Internal::TupleNS
namespace MoReFEM::Internal::PrintNS
{
/*!
* \brief Facility to handle properly the interval issue (we want separator between elements but not at the
* end of the list.
*
* I recommend [this blog post](https://www.fluentcpp.com/2019/05/07/output-strings-separated-commas-cpp) which was
* a direct inspiration.
*/
template<class StringT = std::string>
class SeparatorFacility
{
public:
//! Convenient alias.
using self = SeparatorFacility<StringT>;
/*!
* \brief Constructor.
*
* \param[in] stream Stream onto which the content is used.
* \param[in] separator Type of separator to be used in the list.
*/
explicit SeparatorFacility(std::ostream& stream, StringT&& separator = ", ");
//! Destructor.
~SeparatorFacility() = default;
//! \copydoc doxygen_hide_copy_constructor
SeparatorFacility(const self& rhs) = delete;
//! \copydoc doxygen_hide_move_constructor
SeparatorFacility(self&& rhs) = delete;
//! \copydoc doxygen_hide_copy_affectation
SeparatorFacility& operator=(const self& rhs) = delete;
//! \copydoc doxygen_hide_move_affectation
SeparatorFacility& operator=(self&& rhs) = delete;
template<class T>
friend self& operator<<(self& facility, const T& value);
private:
//! The stream onto which content is printed.
std::ostream& stream_;
//! Separator to use between elements of the list.
StringT separator_;
//! True when we still have to print the first element.
bool is_first_ = true;
};
/*!
** \brief Facility to print elements of a tuple
**
......@@ -55,7 +112,7 @@ namespace MoReFEM::Internal::TupleNS
};
} // namespace MoReFEM::Internal::TupleNS
} // namespace MoReFEM::Internal::PrintNS
# include "Utilities/Containers/Internal/Print.hxx"
......
......@@ -12,10 +12,30 @@
# define MOREFEM_x_UTILITIES_x_CONTAINERS_x_INTERNAL_x_PRINT_HXX_
namespace MoReFEM::Internal::TupleNS
namespace MoReFEM::Internal::PrintNS
{
template<class StringT>
SeparatorFacility<StringT>::SeparatorFacility(std::ostream& stream, StringT&& separator)
: stream_(stream),
separator_(separator)
{ }
template<class StringT, class T>
SeparatorFacility<StringT>& operator<<(SeparatorFacility<StringT>& facility, const T& value)
{
if (facility.is_first_)
facility.is_first_ = false;
else
facility.stream_ << facility.separator_;
facility.stream_ << value;
return facility;
}
template<class StreamT, unsigned int Index, unsigned int Max, class TupleT>
template<typename StringT>
void PrintTupleHelper<StreamT, Index, Max, TupleT>
......@@ -40,7 +60,7 @@ namespace MoReFEM::Internal::TupleNS
}
} // namespace MoReFEM::Internal::TupleNS
} // namespace MoReFEM::Internal::PrintNS
#endif // MOREFEM_x_UTILITIES_x_CONTAINERS_x_INTERNAL_x_PRINT_HXX_
......@@ -252,7 +252,7 @@ namespace MoReFEM
{
stream << opener;
enum { size = std::tuple_size<TupleT>::value };
Internal::TupleNS::PrintTupleHelper<StreamT, 0, size, TupleT>::Print(stream, tuple, separator);
Internal::PrintNS::PrintTupleHelper<StreamT, 0, size, TupleT>::Print(stream, tuple, separator);
stream << closer;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment