*** NEW CODING POLICY ***
[ardour.git] / libs / pbd / pbd / transmitter.h
1 /*
2     Copyright (C) 1998-99 Paul Barton-Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __libmisc_transmitter_h__
21 #define __libmisc_transmitter_h__
22
23 #include <sstream>
24 #include <iostream>
25
26 #include <sigc++/sigc++.h>
27
28 using std::cout;
29 using std::cerr;
30 using std::endl;
31
32 class Transmitter : public std::stringstream
33
34 {
35   public:
36         enum Channel {
37                 Info,
38                 Error,
39                 Warning,
40                 Fatal,
41                 Throw
42         };
43
44         Transmitter (Channel);
45
46         sigc::signal<void,Channel, const char *> &sender() { 
47                 return *send;
48         }
49
50         bool does_not_return ();
51
52   protected:
53         virtual void deliver ();
54         friend std::ostream& endmsg (std::ostream &);
55
56   private:
57         Channel channel;
58         sigc::signal<void, Channel, const char *> *send;
59
60         sigc::signal<void, Channel, const char *> info;
61         sigc::signal<void, Channel, const char *> warning;
62         sigc::signal<void, Channel, const char *> error;
63         sigc::signal<void, Channel, const char *> fatal;
64 };
65
66 /* for EGCS 2.91.66, if this function is not compiled within the same
67    compilation unit as the one where a ThrownError is thrown, then 
68    nothing will catch the error. This is a pretty small function, so
69    inlining it here seems like a reasonable workaround.
70 */
71
72 inline std::ostream &
73 endmsg (std::ostream &ostr)
74
75 {
76         Transmitter *t;
77
78         /* There is a serious bug in the Cygnus/GCC libstdc++ library:
79            cout is not actually an ostream, but a trick was played
80            to make the compiler think that it is. This will cause
81            the dynamic_cast<> to fail with SEGV. So, first check to
82            see if ostr == cout, and handle it specially.
83         */
84
85         if (&ostr == &cout) {
86                 cout << endl;
87                 return ostr;
88         } else if (&ostr == &cerr) {
89                 cerr << endl;
90                 return ostr;
91         }
92
93         if ((t = dynamic_cast<Transmitter *> (&ostr)) != 0) {
94                 t->deliver ();
95         } else {
96                 /* hmm. not a Transmitter, so just put a newline on
97                    it and assume that that will be enough.
98                 */
99                 
100                 ostr << endl;
101         }
102
103         return ostr;
104 }
105
106
107 extern "C" { void pbd_c_error (const char *); }
108
109 #endif // __libmisc_transmitter_h__