Refactor a base class containing parameter and name storage, and the ostream
authorbruceperens <bruceperens@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 17 Jan 2014 01:24:44 +0000 (01:24 +0000)
committerbruceperens <bruceperens@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 17 Jan 2014 01:24:44 +0000 (01:24 +0000)
inserter.

git-svn-id: https://svn.code.sf.net/p/freetel/code@1366 01035d8c-6547-0410-b346-abe4f91aad63

19 files changed:
freedv-server/CMakeLists.txt
freedv-server/source/audio_input.cpp
freedv-server/source/audio_output.cpp
freedv-server/source/audio_sink.cpp
freedv-server/source/blank_panel.cpp
freedv-server/source/codec.cpp
freedv-server/source/codec_noop.cpp
freedv-server/source/driver_manager.cpp
freedv-server/source/drivers.h
freedv-server/source/keying.cpp
freedv-server/source/keying_sink.cpp
freedv-server/source/modem.cpp
freedv-server/source/modem_noop.cpp
freedv-server/source/ptt_constant.cpp
freedv-server/source/ptt_input.cpp
freedv-server/source/text_constant.cpp
freedv-server/source/text_input.cpp
freedv-server/source/tone.cpp
freedv-server/source/user_interface.cpp

index a3d034e1e306a1011bbf2bfc42c87fcdb106276c..401ae5d8083a7708700f4ae62a8806eadadd45e8 100644 (file)
@@ -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
 )
 
index f157790f6f83965c36671fe5d772d1e8fd3e266f..15cd142a2a24fcd8256c26abba94b55cd1ff29f7 100644 (file)
@@ -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;
-  }
 }
index 4c8829d67dc1b773fb7ca2694b4d00ff6dc64c93..d7b0f44ddb4dd4dfa617914b94d9ef470ddcb3bb 100644 (file)
@@ -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;
-  }
 }
index 5540ccdef6d63f10334d3607413f4ab7c7f96d56..c1aeabc8fa7e6f9edff4a36a9e1aa2a959723fd6 100644 (file)
@@ -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)
   {
   }
 
index 06519a82b788be2f6dd940c606e4d588da94a21f..8a6941722a0f0331b7b346c2be4fc6620cd1f06c 100644 (file)
@@ -14,7 +14,7 @@ namespace FreeDV {
   };
 
   BlankPanel::BlankPanel(const char * parameter, Interfaces * interfaces)
-  : UserInterface(parameter, interfaces)
+  : UserInterface("blank-panel", parameter, interfaces)
   {
   }
 
index 6ec12a101aa8efefc5a4b78360bf7be6538a1a5f..161e90ac07c1447a9a045f0ede0e7d53a4a4417e 100644 (file)
@@ -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)
   {
   }
 
index 9204659246a576ddce26ef8e33a866974e7f4e84..954efe5fc2115b3de483d46dae4010efe518965a 100644 (file)
@@ -60,7 +60,7 @@ namespace FreeDV {
   };
 
   CodecNoOp::CodecNoOp(const char * parameters)
-  : Codec(parameters)
+  : Codec("no-op", parameters)
   {
   }
 
index f2305bd154f3b9f38119f6727acfbc840350dd8d..428e4da0cbd4d69b22a6453f0b8884bee037148c 100644 (file)
@@ -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.
index 53f368f494d2dd49d75b3c6110bc2d80ab010f29..340189b185934f30fba0565dd7db6b347ffc2800 100644 (file)
@@ -1,29 +1,79 @@
 /// FreeDV driver interface definitions.
 #include <cstdint>
+#include <iostream>
 
 
 /// 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 <iostream>
 #include <map>
 #include <string>
 
@@ -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.
index 641268add5320f487f09357acae48db42b1a4fa4..72569fd1fa35ec9ffdf0807a321af50748f4bda9 100644 (file)
@@ -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)
   {
   }
 
index 1ef543d92f67cf8d37df8e8da20f5bc75e161107..b24d54d503751890a66c6fb1b29e109b9fb673af 100644 (file)
@@ -18,7 +18,7 @@ namespace FreeDV {
   };
 
   KeyingSink::KeyingSink(const char * parameters)
-  : Keying(parameters)
+  : Keying("sink", parameters)
   {
   }
 
index a0a6bf905260bb7255eef952b04b4fcbc2c1cd1c..97ac63daf33519f28cd8f4ed9b5c009ee85adf46 100644 (file)
@@ -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)
   {
   }
 
index 37d6b5756f368288d740b05b4dae15d3abe926cf..1e5a6bacc62e9c9c0155465e877a1dc4ec8ba855 100644 (file)
@@ -14,7 +14,7 @@ namespace FreeDV {
   };
 
   ModemNoOp::ModemNoOp(const char * parameters)
-  : Modem(parameters)
+  : Modem("no-op", parameters)
   {
   }
 
index 5393fa6a9302520374d3a51ffc0ae0ec0fbeb247..7dfe1de787057b8517a6712c2b372060e86455a7 100644 (file)
@@ -13,7 +13,7 @@ namespace FreeDV {
   };
 
   PTTConstant::PTTConstant(const char * parameters)
-  : PTTInput(parameters)
+  : PTTInput("constant", parameters)
   {
   }
 
index a39fec8cded36e2c82e048c35fea6d49c078dce3..3f79a717e763da730ad2b2eda51436a4dc50ef0a 100644 (file)
@@ -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)
   {
index 04d40883af7c80b2ee12670acc3f781e9acec65e..02bb75f23357eae685c75f71ccc74c0756dbee2f 100644 (file)
@@ -13,7 +13,7 @@ namespace FreeDV {
   };
 
   TextConstant::TextConstant(const char * parameters)
-  : TextInput(parameters)
+  : TextInput("constant", parameters)
   {
   }
 
index 78438b101eebc1e0f0086e90794fd22e5a0d880e..740bebd1a3fbd4a2cadb16c04065d97548189d08 100644 (file)
@@ -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 *)
   {
index 8f50f9b897143ae987dbb476eba01676d79d737a..342d4434272760066a27a17526276eb9372b8101 100644 (file)
@@ -21,7 +21,7 @@ namespace FreeDV {
   };
 
   Tone::Tone(const char * parameters)
-  : AudioInput(parameters)
+  : AudioInput("tone", parameters)
   {
   }
 
index d85f42c53b676dd23e57d05ddd5f0c6ca4d42787..119d7295b6e8454aa09bcc602a23a41743b9e80d 100644 (file)
@@ -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)
   {
   }