[libc-commits] [libc] [libc] Template the writing mode for the writer class (PR #111559)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Tue Mar 11 10:50:25 PDT 2025


================
@@ -21,12 +21,23 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace printf_core {
 
-struct WriteBuffer {
-  enum class WriteMode {
-    FILL_BUFF_AND_DROP_OVERFLOW,
-    FLUSH_TO_STREAM,
-    RESIZE_AND_FILL_BUFF,
-  };
+enum class WriteMode {
+  FILL_BUFF_AND_DROP_OVERFLOW,
+  FLUSH_TO_STREAM,
+  RESIZE_AND_FILL_BUFF,
+};
+
+// Helper to omit the template argument if we are using runtime dispatch and
+// avoid multiple copies of the converter functions.
+template <WriteMode write_mode> struct Mode {
+#ifdef LIBC_COPT_PRINTF_RUNTIME_DISPATCH
+  static constexpr WriteMode value = WriteMode::FILL_BUFF_AND_DROP_OVERFLOW;
+#else
+  static constexpr WriteMode value = write_mode;
+#endif
+};
----------------
michaelrj-google wrote:

Would it make more sense to have a `WriteMode` of `DYNAMIC_DISPATCH`, then use that in `overflow_write` in place of the `#ifdef`? That would also make this less confusing.

https://github.com/llvm/llvm-project/pull/111559


More information about the libc-commits mailing list