lazy construction of FileAggregator
authorTom Henderson <tomh@tomh.org>
Sun, 04 Aug 2013 21:04:36 -0700
changeset 9916 461e9afec7ec
parent 9915 4e4a6c8ec55e
child 9917 1a3bd67be133
lazy construction of FileAggregator
src/stats/helper/file-helper.cc
src/stats/helper/file-helper.h
--- a/src/stats/helper/file-helper.cc	Sun Aug 04 07:23:21 2013 -0700
+++ b/src/stats/helper/file-helper.cc	Sun Aug 04 21:04:36 2013 -0700
@@ -35,16 +35,15 @@
 FileHelper::FileHelper ()
   : m_aggregator        (0),
     m_fileProbeCount    (0),
+    m_fileType (FileAggregator::SPACE_SEPARATED),
+    m_outputFileNameWithoutExtension ("file-helper"),
     m_hasHeadingBeenSet (false)
 {
   NS_LOG_FUNCTION (this);
 
-  std::string outputFileNameWithoutExtension = "file-helper";
-  enum FileAggregator::FileType fileType = FileAggregator::SPACE_SEPARATED;
-
-  // Use these default values for the real constructor.
-  FileHelper (outputFileNameWithoutExtension, fileType);
-}
+   // Note that this does not construct an aggregator. It will be
+   // constructed later when needed.
+}   
 
 FileHelper::FileHelper (const std::string &outputFileNameWithoutExtension,
                         enum FileAggregator::FileType fileType)
@@ -56,12 +55,7 @@
 {
   NS_LOG_FUNCTION (this);
 
-  // Create the aggregator.
-  std::string outputFileName = m_outputFileNameWithoutExtension + ".txt";
-  m_aggregator = CreateObject<FileAggregator> (outputFileName, m_fileType);
-
-  // Enable logging of data for the aggregator.
-  m_aggregator->Enable ();
+  ConstructAggregator ();
 }
 
 FileHelper::~FileHelper ()
@@ -86,12 +80,7 @@
                    " may be destroyed if no references remain.");
     }
 
-  // Create the aggregator.
-  std::string outputFileName = m_outputFileNameWithoutExtension + ".txt";
-  m_aggregator = CreateObject<FileAggregator> (outputFileName, fileType);
-
-  // Enable logging of data for the aggregator.
-  m_aggregator->Enable ();
+  ConstructAggregator ();
 }
 
 void
@@ -252,7 +241,7 @@
   // already constructed in the map.
   if (onlyOneAggregator)
     {
-      m_aggregatorMap[aggregatorName] = m_aggregator;
+      m_aggregatorMap[aggregatorName] = GetAggregator ();
       return;
     }
 
@@ -303,87 +292,106 @@
 }
 
 Ptr<FileAggregator>
-FileHelper::GetAggregator () const
+FileHelper::GetAggregator ()
 {
+  NS_LOG_FUNCTION (this);
+
+  // Do a lazy construction of the aggregator if it hasn't already
+  // been constructed.
+  if (!m_aggregator)
+    {
+      ConstructAggregator();
+    }
   return m_aggregator;
 }
 
 void
+FileHelper::ConstructAggregator ()
+{
+  NS_LOG_FUNCTION (this);
+ 
+  // Create the aggregator.
+  m_aggregator = CreateObject<FileAggregator> (m_outputFileNameWithoutExtension);
+  // Enable the aggregator.
+  m_aggregator->Enable ();
+}
+
+void
 FileHelper::SetHeading (const std::string &heading)
 {
   m_hasHeadingBeenSet = true;
   m_heading = heading;
-  m_aggregator->SetHeading (heading);
+  GetAggregator ()->SetHeading (heading);
 }
 
 void
 FileHelper::Set1dFormat (const std::string &format)
 {
   m_1dFormat = format;
-  m_aggregator->Set1dFormat (format);
+  GetAggregator ()->Set1dFormat (format);
 }
 
 void
 FileHelper::Set2dFormat (const std::string &format)
 {
   m_2dFormat = format;
-  m_aggregator->Set2dFormat (format);
+  GetAggregator ()->Set2dFormat (format);
 }
 
 void
 FileHelper::Set3dFormat (const std::string &format)
 {
   m_3dFormat = format;
-  m_aggregator->Set3dFormat (format);
+  GetAggregator ()->Set3dFormat (format);
 }
 
 void
 FileHelper::Set4dFormat (const std::string &format)
 {
   m_4dFormat = format;
-  m_aggregator->Set4dFormat (format);
+  GetAggregator ()->Set4dFormat (format);
 }
 
 void
 FileHelper::Set5dFormat (const std::string &format)
 {
   m_5dFormat = format;
-  m_aggregator->Set5dFormat (format);
+  GetAggregator ()->Set5dFormat (format);
 }
 
 void
 FileHelper::Set6dFormat (const std::string &format)
 {
   m_6dFormat = format;
-  m_aggregator->Set6dFormat (format);
+  GetAggregator ()->Set6dFormat (format);
 }
 
 void
 FileHelper::Set7dFormat (const std::string &format)
 {
   m_7dFormat = format;
-  m_aggregator->Set7dFormat (format);
+  GetAggregator ()->Set7dFormat (format);
 }
 
 void
 FileHelper::Set8dFormat (const std::string &format)
 {
   m_8dFormat = format;
-  m_aggregator->Set8dFormat (format);
+  GetAggregator ()->Set8dFormat (format);
 }
 
 void
 FileHelper::Set9dFormat (const std::string &format)
 {
   m_9dFormat = format;
-  m_aggregator->Set9dFormat (format);
+  GetAggregator ()->Set9dFormat (format);
 }
 
 void
 FileHelper::Set10dFormat (const std::string &format)
 {
   m_10dFormat = format;
-  m_aggregator->Set10dFormat (format);
+  GetAggregator ()->Set10dFormat (format);
 }
 
 void
--- a/src/stats/helper/file-helper.h	Sun Aug 04 07:23:21 2013 -0700
+++ b/src/stats/helper/file-helper.h	Sun Aug 04 21:04:36 2013 -0700
@@ -39,7 +39,7 @@
 public:
   /**
    * Constructs a file helper that will create a space separated file
-   * named "file-helper.txt".
+   * named "file-helper.txt" unless it is later configured otherwise.
    */
   FileHelper ();
 
@@ -139,7 +139,7 @@
   /**
    * \brief Gets the aggregator.
    */
-  Ptr<FileAggregator> GetAggregator () const;
+  Ptr<FileAggregator> GetAggregator ();
 
   /**
    * \param heading the heading string.
@@ -253,6 +253,11 @@
                                  const std::string &outputFileNameWithoutExtension,
                                  bool onlyOneAggregator);
 
+  /**
+   * \brief Constructs the aggregator.
+   */
+  void ConstructAggregator ();
+
   /// Used to create the probes and collectors as they are added.
   ObjectFactory m_factory;
 
@@ -277,7 +282,7 @@
 
   /// The name of the output file to created without its extension.
   std::string m_outputFileNameWithoutExtension;
-
+  
   /// Indicates if the heading line for the file has been set.
   bool m_hasHeadingBeenSet;