]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
libtextstyle: Add is_instance_of_* predicates.
authorBruno Haible <bruno@clisp.org>
Mon, 14 Dec 2020 22:31:41 +0000 (23:31 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 14 Dec 2020 22:31:41 +0000 (23:31 +0100)
* libtextstyle/gnulib-local/lib/fd-ostream.oo.h (is_instance_of_fd_ostream): New
declaration.
* libtextstyle/gnulib-local/lib/fd-ostream.oo.c (is_instance_of_fd_ostream): New
function.
* libtextstyle/gnulib-local/lib/file-ostream.oo.h (is_instance_of_file_ostream):
New declaration.
* libtextstyle/gnulib-local/lib/file-ostream.oo.c (is_instance_of_file_ostream):
New function.
* libtextstyle/gnulib-local/lib/html-ostream.oo.h (is_instance_of_html_ostream):
New declaration.
* libtextstyle/gnulib-local/lib/html-ostream.oo.c (is_instance_of_html_ostream):
New function.
* libtextstyle/gnulib-local/lib/html-styled-ostream.oo.h
(is_instance_of_html_styled_ostream): New declaration.
* libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c
(is_instance_of_html_styled_ostream): New function.
* libtextstyle/gnulib-local/lib/iconv-ostream.oo.h
(is_instance_of_iconv_ostream): New declaration.
* libtextstyle/gnulib-local/lib/iconv-ostream.oo.c
(is_instance_of_iconv_ostream): New function.
* libtextstyle/gnulib-local/lib/memory-ostream.oo.h
(is_instance_of_memory_ostream): New declaration.
* libtextstyle/gnulib-local/lib/memory-ostream.oo.c
(is_instance_of_memory_ostream): New function.
* libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.h
(is_instance_of_noop_styled_ostream): New declaration.
* libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c
(is_instance_of_noop_styled_ostream): New function.
* libtextstyle/gnulib-local/lib/styled-ostream.oo.h
(is_instance_of_styled_ostream): New declaration.
* libtextstyle/gnulib-local/lib/styled-ostream.oo.c
(is_instance_of_styled_ostream): New function.
* libtextstyle/gnulib-local/lib/term-ostream.oo.h (is_instance_of_term_ostream):
New declaration.
* libtextstyle/gnulib-local/lib/term-ostream.oo.c (is_instance_of_term_ostream):
New function.
* libtextstyle/gnulib-local/lib/term-styled-ostream.oo.h
(is_instance_of_term_styled_ostream): New declaration.
* libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c
(is_instance_of_term_styled_ostream): New function.
* libtextstyle/lib/textstyle.h (is_instance_of_styled_ostream,
is_instance_of_file_ostream, is_instance_of_fd_ostream,
is_instance_of_term_ostream, is_instance_of_memory_ostream,
is_instance_of_iconv_ostream, is_instance_of_html_ostream,
is_instance_of_term_styled_ostream, is_instance_of_html_styled_ostream,
is_instance_of_noop_styled_ostream): New declarations.
* libtextstyle/tests/test-instanceof.c: New file.
* libtextstyle/tests/Makefile.am: Arrange to compile it.
* libtextstyle/doc/libtextstyle.texi (The output stream hierarchy): Mention the
is_instance_of_* predicates.
* libtextstyle/NEWS: Mention the new functions.

26 files changed:
libtextstyle/.gitignore
libtextstyle/NEWS
libtextstyle/doc/libtextstyle.texi
libtextstyle/gnulib-local/lib/fd-ostream.oo.c
libtextstyle/gnulib-local/lib/fd-ostream.oo.h
libtextstyle/gnulib-local/lib/file-ostream.oo.c
libtextstyle/gnulib-local/lib/file-ostream.oo.h
libtextstyle/gnulib-local/lib/html-ostream.oo.c
libtextstyle/gnulib-local/lib/html-ostream.oo.h
libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c
libtextstyle/gnulib-local/lib/html-styled-ostream.oo.h
libtextstyle/gnulib-local/lib/iconv-ostream.oo.c
libtextstyle/gnulib-local/lib/iconv-ostream.oo.h
libtextstyle/gnulib-local/lib/memory-ostream.oo.c
libtextstyle/gnulib-local/lib/memory-ostream.oo.h
libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c
libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.h
libtextstyle/gnulib-local/lib/styled-ostream.oo.c
libtextstyle/gnulib-local/lib/styled-ostream.oo.h
libtextstyle/gnulib-local/lib/term-ostream.oo.c
libtextstyle/gnulib-local/lib/term-ostream.oo.h
libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c
libtextstyle/gnulib-local/lib/term-styled-ostream.oo.h
libtextstyle/lib/textstyle.h
libtextstyle/tests/Makefile.am
libtextstyle/tests/test-instanceof.c [new file with mode: 0644]

index a2e030de92a05c041cb8e9332a76cfa5b63eba32..9666781ba79c065fe9ad0a390d5db7bd79c182ed 100644 (file)
 /lib/xvasprintf.h
 /tests/
 !/tests/Makefile.am
+!/tests/test-instanceof.c
 
 # Files brought in by "automake --add-missing --copy":
 /build-aux/compile
index 125a1307ad7995f0a9732546de79309d9f27f987..953c7ab54901207b256336715fc82f9ea57f5e67 100644 (file)
@@ -1,3 +1,17 @@
+New in 0.21.1:
+* Added type test functions:
+       TYPE                             FUNCTION
+  styled_ostream_t          is_instance_of_styled_ostream
+  file_ostream_t            is_instance_of_file_ostream
+  fd_ostream_t              is_instance_of_fd_ostream
+  term_ostream_t            is_instance_of_term_ostream
+  memory_ostream_t          is_instance_of_memory_ostream
+  iconv_ostream_t           is_instance_of_iconv_ostream
+  html_ostream_t            is_instance_of_html_ostream
+  term_styled_ostream_t     is_instance_of_term_styled_ostream
+  html_styled_ostream_t     is_instance_of_html_styled_ostream
+  noop_styled_ostream_t     is_instance_of_noop_styled_ostream
+
 New in 0.21:
 * Added support for emitting hyperlinks.
   New functions:
index 1dd344152bca3fe37970c1f802614e72dfe9faf1..82860aa296bbe2eebf862ef64893e7262de36294 100644 (file)
@@ -36,7 +36,7 @@
 This manual provides documentation for the GNU @code{libtextstyle} library.
 
 @copying
-Copyright (C) 2018-2019 Free Software Foundation, Inc.
+Copyright (C) 2018-2020 Free Software Foundation, Inc.
 
 This manual is free documentation.  It is dually licensed under the
 GNU FDL and the GNU GPL.  This means that you can redistribute this
@@ -68,7 +68,7 @@ A copy of the license is at @url{https://www.gnu.org/licenses/gpl.html}.
 @page
 @vskip 0pt plus 1filll
 @c @insertcopying
-Copyright (C) 2018-2019 Free Software Foundation, Inc.
+Copyright (C) 2018-2020 Free Software Foundation, Inc.
 
 This manual is free documentation.  It is dually licensed under the
 GNU FDL and the GNU GPL.  This means that you can redistribute this
@@ -824,7 +824,9 @@ Every @samp{some_ostream_t} object can be converted to an
 @item
 The opposite conversion, from @samp{ostream_t} to @samp{some_ostream_t},
 can also be performed, provided that the object is actually an instance
-of @samp{some_ostream_t}.
+of @samp{some_ostream_t}.  You can test whether an object is an instance
+of @samp{some_ostream_t} by invoking the method
+@samp{bool is_instance_of_some_ostream (ostream_t stream)}.
 @item
 Every method @samp{ostream_@var{foobar}} exists also as a method
 @samp{some_ostream_@var{foobar}} with compatible argument types and a
index cabe2a07ce00a8b8942f2aaa0ea4c1f85cfff662..f5aa13495819138781272f3c98527ae177062c58 100644 (file)
@@ -199,3 +199,11 @@ fd_ostream_create (int fd, const char *filename, bool buffered)
 
   return stream;
 }
+
+/* Instanceof test.  */
+
+bool
+is_instance_of_fd_ostream (ostream_t stream)
+{
+  return IS_INSTANCE (stream, ostream, fd_ostream);
+}
index 57e0f82306174cdbfe3add8b5ce784696bb1c8bf..71b82100e76f57ac84053ac789a558f301f67590 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream referring to a file descriptor.
-   Copyright (C) 2006 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -41,6 +41,10 @@ extern fd_ostream_t fd_ostream_create (int fd, const char *filename,
                                        bool buffered);
 
 
+/* Test whether a given output stream is a fd_ostream.  */
+extern bool is_instance_of_fd_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
index 12565b04cb053688fea9932988ad3818ca08ee3c..0937191b60a5c09a03baef6b5281812eb38598f3 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream referring to an stdio FILE.
-   Copyright (C) 2006, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -106,3 +106,11 @@ file_ostream_create (FILE *fp)
 
   return stream;
 }
+
+/* Instanceof test.  */
+
+bool
+is_instance_of_file_ostream (ostream_t stream)
+{
+  return IS_INSTANCE (stream, ostream, file_ostream);
+}
index 98bd29cd92e11f018ee1258923ad06130eba3fe3..cbc7da611fc6adba11fb9cb65264c37e47fed48d 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream referring to an stdio FILE.
-   Copyright (C) 2006 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -18,6 +18,7 @@
 #ifndef _FILE_OSTREAM_H
 #define _FILE_OSTREAM_H
 
+#include <stdbool.h>
 #include <stdio.h>
 
 #include "ostream.h"
@@ -39,6 +40,10 @@ extern "C" {
 extern file_ostream_t file_ostream_create (FILE *fp);
 
 
+/* Test whether a given output stream is a file_ostream.  */
+extern bool is_instance_of_file_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
index 3c6bfbbc9cb0a135ddd064ea9a2c9d3ac0a13822..9a613e33c0ebc68a7afb49e530564584df11fb51 100644 (file)
@@ -436,3 +436,11 @@ html_ostream_create (ostream_t destination)
 
   return stream;
 }
+
+/* Instanceof test.  */
+
+bool
+is_instance_of_html_ostream (ostream_t stream)
+{
+  return IS_INSTANCE (stream, ostream, html_ostream);
+}
index 93456d4769a2b27928172f6ba7de3e2d18fd89c6..987fbb2719547681e16ffd92d9e18b228547f6b2 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream that produces HTML output.
-   Copyright (C) 2006, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -18,6 +18,8 @@
 #ifndef _HTML_OSTREAM_H
 #define _HTML_OSTREAM_H
 
+#include <stdbool.h>
+
 #include "ostream.h"
 
 
@@ -63,6 +65,10 @@ extern "C" {
 extern html_ostream_t html_ostream_create (ostream_t destination);
 
 
+/* Test whether a given output stream is a html_ostream.  */
+extern bool is_instance_of_html_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
index 945396499241376d237cd71edb64ac6e81ac3bff..a78740ab605db8841899387bf6c8331dee6c0390 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream for CSS styled text, producing HTML output.
-   Copyright (C) 2006-2007, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006-2007, 2019-2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -190,3 +190,11 @@ html_styled_ostream_create (ostream_t destination, const char *css_filename)
 
   return stream;
 }
+
+/* Instanceof test.  */
+
+bool
+is_instance_of_html_styled_ostream (ostream_t stream)
+{
+  return IS_INSTANCE (stream, ostream, html_styled_ostream);
+}
index 14a36f2c03a1c7dd5823875359989e8344392e41..74fe376ad57edc4f698b46e23f2d81f1fe7f3e43 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream for CSS styled text, producing HTML output.
-   Copyright (C) 2006 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -18,6 +18,8 @@
 #ifndef _HTML_STYLED_OSTREAM_H
 #define _HTML_STYLED_OSTREAM_H
 
+#include <stdbool.h>
+
 #include "styled-ostream.h"
 
 
@@ -41,6 +43,10 @@ extern html_styled_ostream_t
                                    const char *css_filename);
 
 
+/* Test whether a given output stream is a html_styled_ostream.  */
+extern bool is_instance_of_html_styled_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
index 1df6b18cc85be79219c40933d0d93dbf55b0f4de..3e9f7a59e0db25ad867ce5e0f6ef8828986cc7a7 100644 (file)
@@ -222,6 +222,14 @@ iconv_ostream_create (const char *from_encoding, const char *to_encoding,
   return stream;
 }
 
+/* Instanceof test.  */
+
+bool
+is_instance_of_iconv_ostream (ostream_t stream)
+{
+  return IS_INSTANCE (stream, ostream, iconv_ostream);
+}
+
 #else
 
 static void
@@ -242,4 +250,12 @@ iconv_ostream::free (iconv_ostream_t stream)
   abort ();
 }
 
+/* Instanceof test.  */
+
+bool
+is_instance_of_iconv_ostream (ostream_t stream)
+{
+  return false;
+}
+
 #endif /* HAVE_ICONV */
index 7a9baa546cc1f7b65c26a0b85581e54fb2c74dd8..a2a87057307af0e295cfb82215f0c7118f59fe69 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream that converts the output to another encoding.
-   Copyright (C) 2006 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -21,6 +21,8 @@
 /* Note that this stream does not provide accurate error messages with line
    and column number when the conversion fails.  */
 
+#include <stdbool.h>
+
 #include "ostream.h"
 
 
@@ -46,6 +48,10 @@ extern iconv_ostream_t iconv_ostream_create (const char *from_encoding,
 #endif /* HAVE_ICONV */
 
 
+/* Test whether a given output stream is an iconv_ostream.  */
+extern bool is_instance_of_iconv_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
index 4c12b655ecfcb3e2ff7e099b8a296b0ef3e49e7c..a5f73126bd1320f9f86317227ee85dacf0343095 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream that accumulates the output in memory.
-   Copyright (C) 2006-2007, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006-2007, 2019-2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -99,3 +99,11 @@ memory_ostream_create (void)
 
   return stream;
 }
+
+/* Instanceof test.  */
+
+bool
+is_instance_of_memory_ostream (ostream_t stream)
+{
+  return IS_INSTANCE (stream, ostream, memory_ostream);
+}
index 625557d64ae6923ac0361fd7cb73910664e5e615..384e0e8230005a1eae104bcb992d23bc2253a918 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream that accumulates the output in memory.
-   Copyright (C) 2006, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -18,6 +18,7 @@
 #ifndef _MEMORY_OSTREAM_H
 #define _MEMORY_OSTREAM_H
 
+#include <stdbool.h>
 #include <stddef.h>
 
 #include "ostream.h"
@@ -41,6 +42,10 @@ extern "C" {
 extern memory_ostream_t memory_ostream_create (void);
 
 
+/* Test whether a given output stream is a memory_ostream.  */
+extern bool is_instance_of_memory_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
index fb824b9057ad295dabd0a47ea952d8968dacbdb7..7ef0f5d64d3a3e7a77d1a320e4cc39a64784dfb1 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream with no-op styling.
-   Copyright (C) 2006, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2019.
 
    This program is free software: you can redistribute it and/or modify
@@ -121,3 +121,11 @@ noop_styled_ostream_create (ostream_t destination, bool pass_ownership)
 
   return stream;
 }
+
+/* Instanceof test.  */
+
+bool
+is_instance_of_noop_styled_ostream (ostream_t stream)
+{
+  return IS_INSTANCE (stream, ostream, noop_styled_ostream);
+}
index fa64e1c87c41ed889aaf1b21320accd50ef483dc..4a57e54626d2fa21542f9191f9e142bc46461452 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream with no-op styling.
-   Copyright (C) 2006, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2019.
 
    This program is free software: you can redistribute it and/or modify
@@ -44,6 +44,10 @@ extern noop_styled_ostream_t
        noop_styled_ostream_create (ostream_t destination, bool pass_ownership);
 
 
+/* Test whether a given output stream is a noop_styled_ostream.  */
+extern bool is_instance_of_noop_styled_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
index e87eea9eeec7844099ab46d58795b5ea2aaaa825..b06553d055ed7c08f4c5e2a6714d00f280869461 100644 (file)
@@ -1,5 +1,5 @@
 /* Abstract output stream for CSS styled text.
-   Copyright (C) 2006 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -25,3 +25,11 @@ struct styled_ostream : struct ostream
 {
 fields:
 };
+
+/* Instanceof test.  */
+
+bool
+is_instance_of_styled_ostream (ostream_t stream)
+{
+  return IS_INSTANCE (stream, ostream, styled_ostream);
+}
index cee0e64ea0c4f837f11fed411fe683c34f47fa60..f90f8bfbab1257140704496f947bc5abcb67f249 100644 (file)
@@ -1,5 +1,5 @@
 /* Abstract output stream for CSS styled text.
-   Copyright (C) 2006, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -18,6 +18,8 @@
 #ifndef _STYLED_OSTREAM_H
 #define _STYLED_OSTREAM_H
 
+#include <stdbool.h>
+
 #include "ostream.h"
 
 
@@ -54,5 +56,17 @@ methods:
   void flush_to_current_style (styled_ostream_t stream);
 };
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* Test whether a given output stream is a styled_ostream.  */
+extern bool is_instance_of_styled_ostream (ostream_t stream);
+
+
+#ifdef __cplusplus
+}
+#endif
 
 #endif /* _STYLED_OSTREAM_H */
index 36d84d31c8db1d9e91d889afa0d9d97244bbfa98..02e873eac3d5ec0c5dac02ca86e4f30c6bc628d0 100644 (file)
@@ -2740,3 +2740,11 @@ term_ostream_create (int fd, const char *filename, ttyctl_t tty_control)
 
   return stream;
 }
+
+/* Instanceof test.  */
+
+bool
+is_instance_of_term_ostream (ostream_t stream)
+{
+  return IS_INSTANCE (stream, ostream, term_ostream);
+}
index 8e830978776a8bf71c69324011344b3a72393da2..949649f045077e5bdfe222faf15fc0feff283d6a 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream for attributed text, producing ANSI escape sequences.
-   Copyright (C) 2006, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -18,6 +18,8 @@
 #ifndef _TERM_OSTREAM_H
 #define _TERM_OSTREAM_H
 
+#include <stdbool.h>
+
 #include "ostream.h"
 
 
@@ -123,6 +125,10 @@ extern term_ostream_t
        term_ostream_create (int fd, const char *filename, ttyctl_t tty_control);
 
 
+/* Test whether a given output stream is a term_ostream.  */
+extern bool is_instance_of_term_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
index 2ff978f6678d55a88ed26f83138549c3985a3023..3ed00d1d6d8c490e67f7c470ff30f58d6998f09a 100644 (file)
@@ -667,3 +667,11 @@ term_styled_ostream_create (int fd, const char *filename, ttyctl_t tty_control,
 
   return stream;
 }
+
+/* Instanceof test.  */
+
+bool
+is_instance_of_term_styled_ostream (ostream_t stream)
+{
+  return IS_INSTANCE (stream, ostream, term_styled_ostream);
+}
index 5918f28705731605a40170231c45e93f84548c67..59f441ec0211cb6983c157d841d7e184e2d88316 100644 (file)
@@ -1,5 +1,5 @@
 /* Output stream for CSS styled text, producing ANSI escape sequences.
-   Copyright (C) 2006, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -18,6 +18,8 @@
 #ifndef _TERM_STYLED_OSTREAM_H
 #define _TERM_STYLED_OSTREAM_H
 
+#include <stdbool.h>
+
 #include "styled-ostream.h"
 #include "term-ostream.h"
 
@@ -45,6 +47,10 @@ extern term_styled_ostream_t
                                    const char *css_filename);
 
 
+/* Test whether a given output stream is a term_styled_ostream.  */
+extern bool is_instance_of_term_styled_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
index c1b381db7f544ddaddf6d42bbe534296a41de2d9..2cf56b1c54ad6d5bc008fb80a0e9d49dbc579c55 100644 (file)
@@ -1,5 +1,5 @@
 /* Public API of the libtextstyle library.
-   Copyright (C) 2006-2007, 2019 Free Software Foundation, Inc.
+   Copyright (C) 2006-2007, 2019-2020 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -123,6 +123,19 @@ extern void styled_ostream_flush_to_current_style (styled_ostream_t stream);
 }
 #endif
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* Test whether a given output stream is a styled_ostream.  */
+extern bool is_instance_of_styled_ostream (ostream_t stream);
+
+
+#ifdef __cplusplus
+}
+#endif
+
 /* -------------------------- From file-ostream.h -------------------------- */
 
 /* file_ostream_t is a subtype of ostream_t.  */
@@ -149,6 +162,10 @@ extern "C" {
 extern file_ostream_t file_ostream_create (FILE *fp);
 
 
+/* Test whether a given output stream is a file_ostream.  */
+extern bool is_instance_of_file_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
@@ -181,6 +198,10 @@ extern fd_ostream_t fd_ostream_create (int fd, const char *filename,
                                        bool buffered);
 
 
+/* Test whether a given output stream is a fd_ostream.  */
+extern bool is_instance_of_fd_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
@@ -294,6 +315,10 @@ extern term_ostream_t
        term_ostream_create (int fd, const char *filename, ttyctl_t tty_control);
 
 
+/* Test whether a given output stream is a term_ostream.  */
+extern bool is_instance_of_term_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
@@ -324,6 +349,10 @@ extern "C" {
 extern memory_ostream_t memory_ostream_create (void);
 
 
+/* Test whether a given output stream is a memory_ostream.  */
+extern bool is_instance_of_memory_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
@@ -358,6 +387,10 @@ extern iconv_ostream_t iconv_ostream_create (const char *from_encoding,
                                              ostream_t destination);
 
 
+/* Test whether a given output stream is an iconv_ostream.  */
+extern bool is_instance_of_iconv_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
@@ -406,6 +439,10 @@ extern "C" {
 extern html_ostream_t html_ostream_create (ostream_t destination);
 
 
+/* Test whether a given output stream is a html_ostream.  */
+extern bool is_instance_of_html_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
@@ -449,6 +486,10 @@ extern term_styled_ostream_t
                                    const char *css_filename);
 
 
+/* Test whether a given output stream is a term_styled_ostream.  */
+extern bool is_instance_of_term_styled_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
@@ -489,6 +530,10 @@ extern html_styled_ostream_t
                                    const char *css_filename);
 
 
+/* Test whether a given output stream is a html_styled_ostream.  */
+extern bool is_instance_of_html_styled_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
@@ -530,6 +575,10 @@ extern noop_styled_ostream_t
        noop_styled_ostream_create (ostream_t destination, bool pass_ownership);
 
 
+/* Test whether a given output stream is a noop_styled_ostream.  */
+extern bool is_instance_of_noop_styled_ostream (ostream_t stream);
+
+
 #ifdef __cplusplus
 }
 #endif
index 852d0b6f2b665fdc8461bba28d62a10d705ba43f..dd3ad6f90830c7ea58548a96f62ff6777b4bd693 100644 (file)
@@ -1,5 +1,5 @@
 ## Makefile for the tests subdirectory of GNU libtextstyle.
-## Copyright (C) 2009-2019 Free Software Foundation, Inc.
+## Copyright (C) 2009-2020 Free Software Foundation, Inc.
 ##
 ## This program is free software: you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
@@ -24,6 +24,11 @@ AUTOMAKE_OPTIONS += 1.13 subdir-objects color-tests
 # Ensure that ../lib/config.h is seen before ../config.h.
 DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir)/lib -I$(top_builddir)
 
+TESTS += test-instanceof
+check_PROGRAMS += test-instanceof
+EXTRA_DIST += test-instanceof.c
+AM_CPPFLAGS += -DSRCDIR=\"$(srcdir)/\"
+
 
 # For debugging memory leaks and memory allocation bugs.
 # You should build with --disable-shared when using valgrind.
diff --git a/libtextstyle/tests/test-instanceof.c b/libtextstyle/tests/test-instanceof.c
new file mode 100644 (file)
index 0000000..3d8a117
--- /dev/null
@@ -0,0 +1,141 @@
+/* Test for the is_instance_of_* functions.  */
+
+#include <config.h>
+
+#include "textstyle.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ASSERT(x) if (!(x)) abort ()
+
+int
+main ()
+{
+  ostream_t stream1 = file_ostream_create (stdout);
+  ostream_t stream2 = fd_ostream_create (1, "(stdout)", false);
+  ostream_t stream3 = term_ostream_create (1, "(stdout)", TTYCTL_AUTO);
+  ostream_t stream4 = memory_ostream_create ();
+  ostream_t stream5 = html_ostream_create (stream1);
+  ostream_t sstream1 =
+    term_styled_ostream_create (1, "(stdout)", TTYCTL_AUTO,
+                                SRCDIR "../adhoc-tests/hello-default.css");
+  ostream_t sstream2 =
+    html_styled_ostream_create (stream5,
+                                SRCDIR "../adhoc-tests/hello-default.css");
+  ostream_t sstream3 = noop_styled_ostream_create (stream1, false);
+
+  if (stream1 != NULL)
+    {
+      ASSERT (! is_instance_of_styled_ostream (stream1));
+      ASSERT (is_instance_of_file_ostream (stream1));
+      ASSERT (! is_instance_of_fd_ostream (stream1));
+      ASSERT (! is_instance_of_term_ostream (stream1));
+      ASSERT (! is_instance_of_memory_ostream (stream1));
+      ASSERT (! is_instance_of_iconv_ostream (stream1));
+      ASSERT (! is_instance_of_html_ostream (stream1));
+      ASSERT (! is_instance_of_term_styled_ostream (stream1));
+      ASSERT (! is_instance_of_html_styled_ostream (stream1));
+      ASSERT (! is_instance_of_noop_styled_ostream (stream1));
+    }
+
+  if (stream2 != NULL)
+    {
+      ASSERT (! is_instance_of_styled_ostream (stream2));
+      ASSERT (! is_instance_of_file_ostream (stream2));
+      ASSERT (is_instance_of_fd_ostream (stream2));
+      ASSERT (! is_instance_of_term_ostream (stream2));
+      ASSERT (! is_instance_of_memory_ostream (stream2));
+      ASSERT (! is_instance_of_iconv_ostream (stream2));
+      ASSERT (! is_instance_of_html_ostream (stream2));
+      ASSERT (! is_instance_of_term_styled_ostream (stream2));
+      ASSERT (! is_instance_of_html_styled_ostream (stream2));
+      ASSERT (! is_instance_of_noop_styled_ostream (stream2));
+    }
+
+  if (stream3 != NULL)
+    {
+      ASSERT (! is_instance_of_styled_ostream (stream3));
+      ASSERT (! is_instance_of_file_ostream (stream3));
+      ASSERT (! is_instance_of_fd_ostream (stream3));
+      ASSERT (is_instance_of_term_ostream (stream3));
+      ASSERT (! is_instance_of_memory_ostream (stream3));
+      ASSERT (! is_instance_of_iconv_ostream (stream3));
+      ASSERT (! is_instance_of_html_ostream (stream3));
+      ASSERT (! is_instance_of_term_styled_ostream (stream3));
+      ASSERT (! is_instance_of_html_styled_ostream (stream3));
+      ASSERT (! is_instance_of_noop_styled_ostream (stream3));
+    }
+
+  if (stream4 != NULL)
+    {
+      ASSERT (! is_instance_of_styled_ostream (stream4));
+      ASSERT (! is_instance_of_file_ostream (stream4));
+      ASSERT (! is_instance_of_fd_ostream (stream4));
+      ASSERT (! is_instance_of_term_ostream (stream4));
+      ASSERT (is_instance_of_memory_ostream (stream4));
+      ASSERT (! is_instance_of_iconv_ostream (stream4));
+      ASSERT (! is_instance_of_html_ostream (stream4));
+      ASSERT (! is_instance_of_term_styled_ostream (stream4));
+      ASSERT (! is_instance_of_html_styled_ostream (stream4));
+      ASSERT (! is_instance_of_noop_styled_ostream (stream4));
+    }
+
+  if (stream5 != NULL)
+    {
+      ASSERT (! is_instance_of_styled_ostream (stream5));
+      ASSERT (! is_instance_of_file_ostream (stream5));
+      ASSERT (! is_instance_of_fd_ostream (stream5));
+      ASSERT (! is_instance_of_term_ostream (stream5));
+      ASSERT (! is_instance_of_memory_ostream (stream5));
+      ASSERT (! is_instance_of_iconv_ostream (stream5));
+      ASSERT (is_instance_of_html_ostream (stream5));
+      ASSERT (! is_instance_of_term_styled_ostream (stream5));
+      ASSERT (! is_instance_of_html_styled_ostream (stream5));
+      ASSERT (! is_instance_of_noop_styled_ostream (stream5));
+    }
+
+  if (sstream1 != NULL)
+    {
+      ASSERT (is_instance_of_styled_ostream (sstream1));
+      ASSERT (! is_instance_of_file_ostream (sstream1));
+      ASSERT (! is_instance_of_fd_ostream (sstream1));
+      ASSERT (! is_instance_of_term_ostream (sstream1));
+      ASSERT (! is_instance_of_memory_ostream (sstream1));
+      ASSERT (! is_instance_of_iconv_ostream (sstream1));
+      ASSERT (! is_instance_of_html_ostream (sstream1));
+      ASSERT (is_instance_of_term_styled_ostream (sstream1));
+      ASSERT (! is_instance_of_html_styled_ostream (sstream1));
+      ASSERT (! is_instance_of_noop_styled_ostream (sstream1));
+    }
+
+  if (sstream2 != NULL)
+    {
+      ASSERT (is_instance_of_styled_ostream (sstream2));
+      ASSERT (! is_instance_of_file_ostream (sstream2));
+      ASSERT (! is_instance_of_fd_ostream (sstream2));
+      ASSERT (! is_instance_of_term_ostream (sstream2));
+      ASSERT (! is_instance_of_memory_ostream (sstream2));
+      ASSERT (! is_instance_of_iconv_ostream (sstream2));
+      ASSERT (! is_instance_of_html_ostream (sstream2));
+      ASSERT (! is_instance_of_term_styled_ostream (sstream2));
+      ASSERT (is_instance_of_html_styled_ostream (sstream2));
+      ASSERT (! is_instance_of_noop_styled_ostream (sstream2));
+    }
+
+  if (sstream3 != NULL)
+    {
+      ASSERT (is_instance_of_styled_ostream (sstream3));
+      ASSERT (! is_instance_of_file_ostream (sstream3));
+      ASSERT (! is_instance_of_fd_ostream (sstream3));
+      ASSERT (! is_instance_of_term_ostream (sstream3));
+      ASSERT (! is_instance_of_memory_ostream (sstream3));
+      ASSERT (! is_instance_of_iconv_ostream (sstream3));
+      ASSERT (! is_instance_of_html_ostream (sstream3));
+      ASSERT (! is_instance_of_term_styled_ostream (sstream3));
+      ASSERT (! is_instance_of_html_styled_ostream (sstream3));
+      ASSERT (is_instance_of_noop_styled_ostream (sstream3));
+    }
+
+  return 0;
+}