melder_info.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef _melder_info_h_
  2. #define _melder_info_h_
  3. /* melder_info.h
  4. *
  5. * Copyright (C) 1992-2018 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /*
  21. Give information to stdout (batch), or to an "Info" window (interactive), or to a diverted string.
  22. */
  23. namespace MelderInfo {
  24. using Proc = void (*) (conststring32 message);
  25. void _defaultProc (conststring32 message);
  26. extern Proc _p_currentProc;
  27. extern MelderString _foregroundBuffer, *_p_currentBuffer;
  28. };
  29. void MelderInfo_open (); // clear the Info window in the background
  30. void MelderInfo_close (); // drain the background info to the Info window, making sure there is a line break
  31. void MelderInfo_drain (); // drain the background info to the Info window, without adding any extra line break
  32. inline static void _recursiveTemplate_MelderInfo_write (const MelderArg& arg) {
  33. MelderConsole::write (arg._arg, false);
  34. }
  35. template <typename... Args>
  36. void _recursiveTemplate_MelderInfo_write (const MelderArg& first, Args... rest) {
  37. _recursiveTemplate_MelderInfo_write (first);
  38. _recursiveTemplate_MelderInfo_write (rest...);
  39. }
  40. template <typename... Args>
  41. void MelderInfo_write (const MelderArg& first, Args... rest) {
  42. MelderString_append (MelderInfo::_p_currentBuffer, first, rest...);
  43. if (MelderInfo::_p_currentProc == & MelderInfo::_defaultProc && MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer)
  44. _recursiveTemplate_MelderInfo_write (first, rest...);
  45. }
  46. template <typename... Args>
  47. void MelderInfo_writeLine (const MelderArg& first, Args... rest) {
  48. MelderString_append (MelderInfo::_p_currentBuffer, first, rest...);
  49. MelderString_appendCharacter (MelderInfo::_p_currentBuffer, U'\n');
  50. if (MelderInfo::_p_currentProc == & MelderInfo::_defaultProc && MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer) {
  51. _recursiveTemplate_MelderInfo_write (first, rest...);
  52. MelderConsole::write (U"\n", false);
  53. }
  54. }
  55. template <typename... Args>
  56. void Melder_information (const MelderArg& first, Args... rest) {
  57. MelderString_copy (MelderInfo::_p_currentBuffer, first, rest...);
  58. if (MelderInfo::_p_currentProc == & MelderInfo::_defaultProc && MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer)
  59. _recursiveTemplate_MelderInfo_write (first, rest...);
  60. MelderInfo_close ();
  61. }
  62. void Melder_informationReal (double value, conststring32 units); // %.17g or --undefined--; units may be null
  63. void Melder_divertInfo (MelderString *p_buffer); // nullptr = back to normal
  64. class autoMelderDivertInfo {
  65. public:
  66. autoMelderDivertInfo (MelderString *p_buffer) { Melder_divertInfo (p_buffer); }
  67. ~autoMelderDivertInfo () { Melder_divertInfo (nullptr); }
  68. };
  69. void Melder_clearInfo (); // clear the Info window
  70. conststring32 Melder_getInfo ();
  71. void Melder_setInformationProc (MelderInfo::Proc proc);
  72. /* End of file melder_info.h */
  73. #endif