From: bruceperens Date: Fri, 17 Jan 2014 01:24:44 +0000 (+0000) Subject: Refactor a base class containing parameter and name storage, and the ostream X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=5fc712b1d3f34090c2c92ad82d849f66297271a3;p=freetel-svn-tracking.git Refactor a base class containing parameter and name storage, and the ostream inserter. git-svn-id: https://svn.code.sf.net/p/freetel/code@1366 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/freedv-server/CMakeLists.txt b/freedv-server/CMakeLists.txt index a3d034e1..401ae5d8 100644 --- a/freedv-server/CMakeLists.txt +++ b/freedv-server/CMakeLists.txt @@ -90,6 +90,7 @@ set(Compile.sources source/audio_input.cpp source/audio_output.cpp source/audio_sink.cpp + source/base.cpp source/blank_panel.cpp source/codec.cpp source/codec_noop.cpp @@ -104,6 +105,7 @@ set(Compile.sources source/text_constant.cpp source/text_input.cpp source/tone.cpp + source/utility.cpp source/user_interface.cpp ) diff --git a/freedv-server/source/audio_input.cpp b/freedv-server/source/audio_input.cpp index f157790f..15cd142a 100644 --- a/freedv-server/source/audio_input.cpp +++ b/freedv-server/source/audio_input.cpp @@ -3,17 +3,12 @@ #include "drivers.h" namespace FreeDV { - AudioInput::AudioInput(const char * parameters) + AudioInput::AudioInput(const char * name, const char * parameters) + : Base(name, parameters) { } AudioInput::~AudioInput() { } - - bool const - AudioInput::captive() const - { - return false; - } } diff --git a/freedv-server/source/audio_output.cpp b/freedv-server/source/audio_output.cpp index 4c8829d6..d7b0f44d 100644 --- a/freedv-server/source/audio_output.cpp +++ b/freedv-server/source/audio_output.cpp @@ -3,17 +3,12 @@ #include "drivers.h" namespace FreeDV { - AudioOutput::AudioOutput(const char * parameters) + AudioOutput::AudioOutput(const char * name, const char * parameters) + : Base(name, parameters) { } AudioOutput::~AudioOutput() { } - - bool const - AudioOutput::captive() const - { - return false; - } } diff --git a/freedv-server/source/audio_sink.cpp b/freedv-server/source/audio_sink.cpp index 5540ccde..c1aeabc8 100644 --- a/freedv-server/source/audio_sink.cpp +++ b/freedv-server/source/audio_sink.cpp @@ -24,8 +24,8 @@ namespace FreeDV { write16(const int16_t * array, std::size_t length); }; - AudioSink::AudioSink(const char * parameters) - : AudioOutput(parameters) + AudioSink::AudioSink(const char * p) + : AudioOutput("sink", p) { } diff --git a/freedv-server/source/blank_panel.cpp b/freedv-server/source/blank_panel.cpp index 06519a82..8a694172 100644 --- a/freedv-server/source/blank_panel.cpp +++ b/freedv-server/source/blank_panel.cpp @@ -14,7 +14,7 @@ namespace FreeDV { }; BlankPanel::BlankPanel(const char * parameter, Interfaces * interfaces) - : UserInterface(parameter, interfaces) + : UserInterface("blank-panel", parameter, interfaces) { } diff --git a/freedv-server/source/codec.cpp b/freedv-server/source/codec.cpp index 6ec12a10..161e90ac 100644 --- a/freedv-server/source/codec.cpp +++ b/freedv-server/source/codec.cpp @@ -3,7 +3,8 @@ #include "drivers.h" namespace FreeDV { - Codec::Codec(const char * parameters) + Codec::Codec(const char * name, const char * parameters) + : Base(name, parameters) { } diff --git a/freedv-server/source/codec_noop.cpp b/freedv-server/source/codec_noop.cpp index 92046592..954efe5f 100644 --- a/freedv-server/source/codec_noop.cpp +++ b/freedv-server/source/codec_noop.cpp @@ -60,7 +60,7 @@ namespace FreeDV { }; CodecNoOp::CodecNoOp(const char * parameters) - : Codec(parameters) + : Codec("no-op", parameters) { } diff --git a/freedv-server/source/driver_manager.cpp b/freedv-server/source/driver_manager.cpp index f2305bd1..428e4da0 100644 --- a/freedv-server/source/driver_manager.cpp +++ b/freedv-server/source/driver_manager.cpp @@ -188,6 +188,12 @@ namespace FreeDV { user_interface_drivers[driver] = creator; } + std::ostream & + DriverManager::operator << (std::ostream & stream) const + { + return stream; + } + /// Automatic initializer for the driver manager. /// This has to be a function to get around the static initalization order /// problem. diff --git a/freedv-server/source/drivers.h b/freedv-server/source/drivers.h index 53f368f4..340189b1 100644 --- a/freedv-server/source/drivers.h +++ b/freedv-server/source/drivers.h @@ -1,29 +1,79 @@ /// FreeDV driver interface definitions. #include +#include /// Namespace used for all code in this program. namespace FreeDV { + /// Allocate memory and copy a string into it, so that it is permanently + /// stored. + /// \param s The string to be copied. + /// \return The new copy. It's the caller's responsibility to free this data, + /// or a memory leak will occurr. + char * copy_string(const char * s); + + class Base { + private: + /// The copy constructor is private to prevent it from being used. + /// \param that Not used. + Base(const Base & that); + + /// The assignment operator is private to prevent it from being used. + /// \param that Not used. + Base & operator = (const Base & that); + + protected: + /// The name of the driver. This must be the same as the name it + /// is registered under. It's expected to be from a per-class static + /// string and thus should not be deleted. + const char * const + name; + + /// The parameters to this instance of the driver. They are + /// copied here so that we can print them later in operator<<() . + /// the copy is deleted when in the ~Base() destructor. + const char * const + parameters; + + /// Constructor for the virtual base class. + /// \param _name Name of the driver. This is expected to be a single + /// constant static string per driver class. + /// \param _parameters Driver-specific configuration parameters. + Base(const char * _name, const char * _parameters); + + /// Destroy the base class. + virtual ~Base(); + + public: + /// Return true if the object is owned by a UserInterface object and + /// should not be destroyed separately. + /// The return value is invariant for a particular object (or possibly + /// class). + virtual bool const + captive() const; + + /// Write the driver information onto a stream, for debugging and + /// for dumping the configuration information. + /// \param stream A reference to an instance of ostream upon which the + /// object information is to be rendered. + /// \return A reference to the provided stream, meant for the + /// usual successive call paradigm of ostream operator << . + virtual std::ostream & + operator << (std::ostream &) const; + }; + /// Virtual base class for audio input drivers. - class AudioInput { + class AudioInput : public ::FreeDV::Base { protected: /// Create an AudioInput device instance. /// \param parameters Driver-specific configuration parameters. - AudioInput(const char * parameters); + AudioInput(const char * name, const char * parameters); /// Destroy an AudioInput device instance. virtual ~AudioInput() = 0; public: - /// Return true if this object is owned by a UserInterface object. - /// In that case we should not destroy it separately. - /// The default implementation always returns false. - /// \return True if this object is owned by a UserInterface object. - /// The return value is invariant for the particular object. - virtual bool const - captive() const; - /// Get the current audio level. /// \return The current audio level. /// The value is normalized to the range of 0.0 to 1.0. @@ -40,24 +90,16 @@ namespace FreeDV { }; /// Virtual base class for audio output drivers. - class AudioOutput { + class AudioOutput : public ::FreeDV::Base { protected: /// Create an AudioOutput device instance. /// \param parameters Driver-specific configuration parameters. - AudioOutput(const char * parameters); + AudioOutput(const char * name, const char * parameters); /// Destroy an AudioOutput device instance. virtual ~AudioOutput() = 0; public: - /// Return true if this object is owned by a UserInterface object. - /// In that case we should not destroy it separately. - /// The default implementation always returns false. - /// \return True if this object is owned by a UserInterface object. - /// The return value is invariant for the particular object. - virtual bool const - captive() const; - /// Get the current audio level. /// The value is normalized to the range of 0.0 to 1.0. virtual float level() = 0; @@ -72,11 +114,13 @@ namespace FreeDV { }; /// Virtual base class for codecs. - class Codec { + class Codec : public ::FreeDV::Base { protected: /// Create a codec instance. + /// \param name Name of the driver. This is expected to be a single + /// constant static string per driver class. /// \param parameters Driver-specific configuration parameters. - Codec(const char * parameters); + Codec(const char * name, const char * parameters); /// Destroy a codec instance. virtual ~Codec() = 0; @@ -126,7 +170,6 @@ namespace FreeDV { /// frame. virtual std::size_t const samples_per_frame() const = 0; - }; /// Event handler class, indirects the event handler of the particular GUI @@ -178,14 +221,6 @@ namespace FreeDV { /// Cause get_exit() to return true the next time it is called. inline void set_exit() { do_exit = true; } public: - /// Return true if this object is owned by a UserInterface object. - /// In that case we should not destroy it separately. - /// The default implementation always returns false. - /// \return True if this object is owned by a UserInterface object. - /// The return value is invariant for the particular object. - virtual bool const - captive() const; - /// Run the event loop. /// The default implementation iterates checking get_exit(), returning /// if its value is true and otherwise and calling iterate(). @@ -216,14 +251,21 @@ namespace FreeDV { /// loop handler. /// \param fd The file descriptor to be removed from monitoring. virtual void unmonitor(int fd) = 0; + + /// Write the driver information onto a stream, for debugging and + /// for dumping the configuration information. + /// \param stream A reference to an instance of ostream on which the + std::ostream & operator << (std::ostream &); }; /// Radio device keying driver. - class Keying { + class Keying : public ::FreeDV::Base { protected: - /// Create an radio keying device instance. + /// Create an radio keying output device instance. + /// \param name Name of the driver. This is expected to be a single + /// constant static string per driver class. /// \param parameters Driver-specific configuration parameters. - Keying(const char * parameters); + Keying(const char * name, const char * parameters); /// Destroy the radio keying device instance. virtual ~Keying() = 0; @@ -235,19 +277,19 @@ namespace FreeDV { }; /// Softmodem driver. - class Modem { + class Modem : public ::FreeDV::Base { protected: - /// Create a modem instance. + /// Create a softmodem device instance. + /// \param name Name of the driver. This is expected to be a single + /// constant static string per driver class. /// \param parameters Driver-specific configuration parameters. + Modem(const char * name, const char * parameters); - Modem(const char * parameters); virtual ~Modem() = 0; - - public: }; /// Push-to-talk input driver. - class PTTInput { + class PTTInput : public ::FreeDV::Base { private: /// Coroutine to be called when the sense of the push-to-talk switch /// changes. @@ -261,48 +303,33 @@ namespace FreeDV { void changed(bool value); /// Create a push-to-talk switch instance. + /// \param name Name of the driver. This is expected to be a single + /// constant static string per driver class. /// \param parameters Driver-specific configuration parameters. - PTTInput(const char * parameters); + PTTInput(const char * name, const char * parameters); virtual ~PTTInput() = 0; public: - /// Return true if this object is owned by a - /// UserInterface object. In that case we should - /// not destroy it separately. - /// The default implementation always returns false. - /// \return True if this object is owned by a UserInterface object. - /// The return value is invariant for the particular object. - virtual bool const - captive() const; - /// Set the function that will be called when the push-to-talk input /// changes its value. void set_callback(void (*value)(bool)); }; /// Driver for the text message source function. - class TextInput { + class TextInput : public ::FreeDV::Base { protected: /// The child class calls this member in its parent to set the text. void set(const char * text); /// Create a text source instance. + /// \param name Name of the driver. This is expected to be a single + /// constant static string per driver class. /// \param parameters Driver-specific configuration parameters. + TextInput(const char * name, const char * parameters); - TextInput(const char * parameters); virtual ~TextInput() = 0; - - public: - /// Return true if this object is owned by a - /// UserInterface object. In that case we should - /// not destroy it separately. - /// The default implementation always returns false. - /// \return True if this object is owned by a UserInterface object. - /// The return value is invariant for the particular object. - virtual bool const - captive() const; }; class Interfaces; @@ -314,18 +341,23 @@ namespace FreeDV { /// There must be inputs and callbacks for many things here. /// UserInterfaces may provide their own drivers for microphone, /// loudspeaker, TextInput, both forms of PTT, and EventHandler. - class UserInterface { + class UserInterface : public ::FreeDV::Base { protected: + /// The external Interfaces object. + Interfaces * + interfaces; + /// Create an instance of the UserInterface object. + /// \param name Name of the driver. This is expected to be a single + /// constant static string per driver class. /// \param parameters Driver-specific configuration parameters. /// \param interfaces An Interface object. The UserInterface object /// may set various fields of Interface to be its own captive driver /// objects, and they may change during operation if the user changes /// device driver parameters. - UserInterface(const char * parameters, Interfaces * interfaces); - virtual ~UserInterface() = 0; + UserInterface(const char * name, const char * parameters, Interfaces * _interfaces); - public: + virtual ~UserInterface() = 0; }; /// Structure used to pass all of the drivers. Can be modified from @@ -365,11 +397,16 @@ namespace FreeDV { AudioInput * receiver; /// The user interface driver. Used for GUIs. UserInterface * user_interface; + + /// Write the command-line flags necessary to configure the drivers as + /// they are presently configured to the stream. This is used to save + /// the configuration or debug the program. + /// \param stream A reference to an instance of ostream on which the + std::ostream & operator << (std::ostream &) const; }; } #ifndef NO_INITIALIZERS -#include #include #include @@ -465,6 +502,11 @@ namespace FreeDV { /// \param driver The name of the driver. /// \param creator The coroutine that will instantiate the driver. void register_user_interface(const char * driver, UserInterface * (*creator)(const char *, Interfaces *)); + + /// Print the available drivers to the argument stream. + /// \param stream A reference to an instance of ostream on which the + /// information is to be printed. + std::ostream & operator << (std::ostream &) const; }; /// Global reference to the driver manager. diff --git a/freedv-server/source/keying.cpp b/freedv-server/source/keying.cpp index 641268ad..72569fd1 100644 --- a/freedv-server/source/keying.cpp +++ b/freedv-server/source/keying.cpp @@ -3,7 +3,8 @@ #include "drivers.h" namespace FreeDV { - Keying::Keying(const char * parameters) + Keying::Keying(const char * name, const char * parameters) + : Base(name, parameters) { } diff --git a/freedv-server/source/keying_sink.cpp b/freedv-server/source/keying_sink.cpp index 1ef543d9..b24d54d5 100644 --- a/freedv-server/source/keying_sink.cpp +++ b/freedv-server/source/keying_sink.cpp @@ -18,7 +18,7 @@ namespace FreeDV { }; KeyingSink::KeyingSink(const char * parameters) - : Keying(parameters) + : Keying("sink", parameters) { } diff --git a/freedv-server/source/modem.cpp b/freedv-server/source/modem.cpp index a0a6bf90..97ac63da 100644 --- a/freedv-server/source/modem.cpp +++ b/freedv-server/source/modem.cpp @@ -3,7 +3,8 @@ #include "drivers.h" namespace FreeDV { - Modem::Modem(const char * parameters) + Modem::Modem(const char * name, const char * parameters) + : Base(name, parameters) { } diff --git a/freedv-server/source/modem_noop.cpp b/freedv-server/source/modem_noop.cpp index 37d6b575..1e5a6bac 100644 --- a/freedv-server/source/modem_noop.cpp +++ b/freedv-server/source/modem_noop.cpp @@ -14,7 +14,7 @@ namespace FreeDV { }; ModemNoOp::ModemNoOp(const char * parameters) - : Modem(parameters) + : Modem("no-op", parameters) { } diff --git a/freedv-server/source/ptt_constant.cpp b/freedv-server/source/ptt_constant.cpp index 5393fa6a..7dfe1de7 100644 --- a/freedv-server/source/ptt_constant.cpp +++ b/freedv-server/source/ptt_constant.cpp @@ -13,7 +13,7 @@ namespace FreeDV { }; PTTConstant::PTTConstant(const char * parameters) - : PTTInput(parameters) + : PTTInput("constant", parameters) { } diff --git a/freedv-server/source/ptt_input.cpp b/freedv-server/source/ptt_input.cpp index a39fec8c..3f79a717 100644 --- a/freedv-server/source/ptt_input.cpp +++ b/freedv-server/source/ptt_input.cpp @@ -3,7 +3,8 @@ #include "drivers.h" namespace FreeDV { - PTTInput::PTTInput(const char * parameters) + PTTInput::PTTInput(const char * name, const char * parameters) + : Base(name, parameters) { } @@ -11,12 +12,6 @@ namespace FreeDV { { } - bool const - PTTInput::captive() const - { - return false; - } - void PTTInput::changed(bool value) { diff --git a/freedv-server/source/text_constant.cpp b/freedv-server/source/text_constant.cpp index 04d40883..02bb75f2 100644 --- a/freedv-server/source/text_constant.cpp +++ b/freedv-server/source/text_constant.cpp @@ -13,7 +13,7 @@ namespace FreeDV { }; TextConstant::TextConstant(const char * parameters) - : TextInput(parameters) + : TextInput("constant", parameters) { } diff --git a/freedv-server/source/text_input.cpp b/freedv-server/source/text_input.cpp index 78438b10..740bebd1 100644 --- a/freedv-server/source/text_input.cpp +++ b/freedv-server/source/text_input.cpp @@ -3,7 +3,8 @@ #include "drivers.h" namespace FreeDV { - TextInput::TextInput(const char * parameters) + TextInput::TextInput(const char * name, const char * parameters) + : Base(name, parameters) { } @@ -11,12 +12,6 @@ namespace FreeDV { { } - bool const - TextInput::captive() const - { - return false; - } - void TextInput::set(const char *) { diff --git a/freedv-server/source/tone.cpp b/freedv-server/source/tone.cpp index 8f50f9b8..342d4434 100644 --- a/freedv-server/source/tone.cpp +++ b/freedv-server/source/tone.cpp @@ -21,7 +21,7 @@ namespace FreeDV { }; Tone::Tone(const char * parameters) - : AudioInput(parameters) + : AudioInput("tone", parameters) { } diff --git a/freedv-server/source/user_interface.cpp b/freedv-server/source/user_interface.cpp index d85f42c5..119d7295 100644 --- a/freedv-server/source/user_interface.cpp +++ b/freedv-server/source/user_interface.cpp @@ -3,7 +3,8 @@ #include "drivers.h" namespace FreeDV { - UserInterface::UserInterface(const char * parameters, Interfaces * interfaces) + UserInterface::UserInterface(const char * name, const char * parameters, Interfaces * interfaces) + : Base(name, parameters) { }