print_string.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**************************************************************************/
  2. /* print_string.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/variant/variant.h"
  32. extern void (*_print_func)(String);
  33. typedef void (*PrintHandlerFunc)(void *, const String &p_string, bool p_error, bool p_rich);
  34. struct PrintHandlerList {
  35. PrintHandlerFunc printfunc = nullptr;
  36. void *userdata = nullptr;
  37. PrintHandlerList *next = nullptr;
  38. PrintHandlerList() {}
  39. };
  40. String stringify_variants(const Variant &p_var);
  41. template <typename... Args>
  42. String stringify_variants(const Variant &p_var, Args... p_args) {
  43. return p_var.operator String() + " " + stringify_variants(p_args...);
  44. }
  45. void add_print_handler(PrintHandlerList *p_handler);
  46. void remove_print_handler(const PrintHandlerList *p_handler);
  47. extern void __print_line(const String &p_string);
  48. extern void __print_line_rich(const String &p_string);
  49. extern void print_error(const String &p_string);
  50. extern bool is_print_verbose_enabled();
  51. // This version avoids processing the text to be printed until it actually has to be printed, saving some CPU usage.
  52. #define print_verbose(m_text) \
  53. { \
  54. if (is_print_verbose_enabled()) { \
  55. print_line(m_text); \
  56. } \
  57. }
  58. inline void print_line(const Variant &v) {
  59. __print_line(stringify_variants(v));
  60. }
  61. inline void print_line_rich(const Variant &v) {
  62. __print_line_rich(stringify_variants(v));
  63. }
  64. template <typename... Args>
  65. void print_line(const Variant &p_var, Args... p_args) {
  66. __print_line(stringify_variants(p_var, p_args...));
  67. }
  68. template <typename... Args>
  69. void print_line_rich(const Variant &p_var, Args... p_args) {
  70. __print_line_rich(stringify_variants(p_var, p_args...));
  71. }