Commit 25c05210 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Reimplemented Config::ConfigDescription and Config::ParameterContainer using...

Reimplemented Config::ConfigDescription and Config::ParameterContainer using std::vector instead of TNL::List

This is much safer and simpler because it solves many problems due to
cyclic inclusion of TNL headers.
parent c29579e4
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@
   #include <mpi-ext.h>
#endif

#include <unistd.h>  // getpid

#ifdef HAVE_CUDA
    #include <TNL/Devices/Cuda.h>

+4 −5
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@

#pragma once

#include <TNL/Config/ConfigEntryBase.h>

namespace TNL {
namespace Config {

@@ -17,16 +19,13 @@ struct ConfigDelimiter : public ConfigEntryBase
{
   ConfigDelimiter( const String& delimiter )
   : ConfigEntryBase( "", delimiter, false )
   {
   };
   {}

   bool isDelimiter() const { return true; };

   String getEntryType() const { return ""; };

   String getUIEntryType() const { return ""; };
   
   //~ConfigDelimiter(){};
};

} //namespace Config
+36 −46
Original line number Diff line number Diff line
@@ -10,31 +10,23 @@

#include <fstream>
#include <iomanip>
#include <string>

#include <TNL/Config/ConfigDescription.h>
#include <TNL/Config/ParameterContainer.h>

namespace TNL {
namespace Config {

ConfigDescription :: ConfigDescription()
: currentEntry( 0 )
{
}

ConfigDescription :: ~ConfigDescription()
{
   entries.DeepEraseAll();
}

void ConfigDescription::printUsage( const char* program_name ) const
void
ConfigDescription::
printUsage( const char* program_name ) const
{
   std::cout << "Usage of: " << program_name << std::endl << std::endl;
   int i, j;
   //const int group_num = groups. getSize();
   const int entries_num = entries. getSize();
   const int entries_num = entries.size();
   int max_name_length( 0 );
   int max_type_length( 0 );
   for( j = 0; j < entries_num; j ++ )
   for( int j = 0; j < entries_num; j++ )
      if( ! entries[ j ]->isDelimiter() )
      {
         max_name_length = std::max( max_name_length,
@@ -44,7 +36,7 @@ void ConfigDescription::printUsage( const char* program_name ) const
      }
   max_name_length += 2; // this is for '--'

   for( j = 0; j < entries_num; j ++ )
   for( int j = 0; j < entries_num; j++ )
   {
      if( entries[ j ]->isDelimiter() )
      {
@@ -81,11 +73,12 @@ void ConfigDescription::printUsage( const char* program_name ) const
   std::cout << std::endl;
}

void Config::ConfigDescription :: addMissingEntries( Config::ParameterContainer& parameter_container ) const
void
ConfigDescription::
addMissingEntries( Config::ParameterContainer& parameter_container ) const
{
   int i;
   const int size = entries.getSize();
   for( i = 0; i < size; i ++ )
   const int size = entries.size();
   for( int i = 0; i < size; i++ )
   {
      const char* entry_name = entries[ i ]->name.getString();
      if( entries[ i ]->hasDefaultValue &&
@@ -93,57 +86,54 @@ void Config::ConfigDescription :: addMissingEntries( Config::ParameterContainer&
      {
         if( entries[ i ]->getEntryType() == "String" )
         {
            parameter_container. addParameter< String >(
               entry_name,
               ( ( ConfigEntry< String >* ) entries[ i ] ) -> defaultValue );
            ConfigEntry< String >& entry = dynamic_cast< ConfigEntry< String >& >( *entries[ i ] );
            parameter_container.addParameter< String >( entry_name, entry.defaultValue );
            continue;
         }
         if( entries[ i ]->getEntryType() == "bool" )
         {
            parameter_container. addParameter< bool >(
               entry_name,
               ( ( ConfigEntry< bool >* ) entries[ i ] ) -> defaultValue );
            ConfigEntry< bool >& entry = dynamic_cast< ConfigEntry< bool >& >( *entries[ i ] );
            parameter_container.addParameter< bool >( entry_name, entry.defaultValue );
            continue;
         }
         if( entries[ i ]->getEntryType() == "int" )
         {
            parameter_container. addParameter< int >(
               entry_name,
               ( ( ConfigEntry< int >* ) entries[ i ] ) -> defaultValue );
            ConfigEntry< int >& entry = dynamic_cast< ConfigEntry< int >& >( *entries[ i ] );
            parameter_container.addParameter< int >( entry_name, entry.defaultValue );
            continue;
         }
         if( entries[ i ]->getEntryType() == "double" )
         {
            parameter_container. addParameter< double >(
               entry_name,
               ( ( ConfigEntry< double >* ) entries[ i ] ) -> defaultValue );
            ConfigEntry< double >& entry = dynamic_cast< ConfigEntry< double >& >( *entries[ i ] );
            parameter_container.addParameter< double >( entry_name, entry.defaultValue );
            continue;
         }
      }
   }
}

bool Config::ConfigDescription :: checkMissingEntries( Config::ParameterContainer& parameter_container,
bool
Config::ConfigDescription::
checkMissingEntries( Config::ParameterContainer& parameter_container,
                     bool printUsage,
                     const char* programName ) const
{
   int i;
   const int size = entries. getSize();
   Containers::List< String > missingParameters;
   for( i = 0; i < size; i ++ )
   const int size = entries.size();
   std::vector< std::string > missingParameters;
   for( int i = 0; i < size; i++ )
   {
      const char* entry_name = entries[ i ] -> name.getString();
      if( entries[ i ] -> required &&
          ! parameter_container.checkParameter( entry_name ) )
         missingParameters.Append( entry_name );
         missingParameters.push_back( entry_name );
   }
   if( missingParameters.getSize() != 0 )
   if( missingParameters.size() > 0 )
   {
      std::cerr << "Some mandatory parameters are misssing. They are listed at the end. " << std::endl;
      if( printUsage )
         this->printUsage( programName );
      std::cerr << "Add the following missing parameters to the command line: " << std::endl << "   ";
      for( int i = 0; i < missingParameters.getSize(); i++ )
      for( int i = 0; i < (int) missingParameters.size(); i++ )
         std::cerr << "--" << missingParameters[ i ] << " ... ";
      std::cerr << std::endl;
      return false;
+66 −67
Original line number Diff line number Diff line
@@ -10,8 +10,23 @@

#pragma once

#include <vector>
#include <memory>

// std::make_unique does not exist until C++14
// https://stackoverflow.com/a/9657991
#if __cplusplus < 201402L
namespace std {
   template<typename T, typename ...Args>
   std::unique_ptr<T> make_unique( Args&& ...args )
   {
      return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
   }
}
#endif

#include <TNL/Assert.h>
#include <TNL/String.h>
#include <TNL/Containers/List.h>
#include <TNL/param-types.h>
#include <TNL/Config/ConfigEntryType.h>
#include <TNL/Config/ConfigEntry.h>
@@ -26,12 +41,6 @@ class ParameterContainer;
class ConfigDescription
{
public:

   /**
    * \brief Basic constructor.
    */
   ConfigDescription();

   /**
    * \brief Adds new entry to the configuration description.
    *
@@ -43,8 +52,8 @@ class ConfigDescription
   void addEntry( const String& name,
                  const String& description )
   {
      currentEntry = new ConfigEntry< EntryType >( name, description, false );
      entries.Append( currentEntry );
      entries.push_back( std::make_unique< ConfigEntry< EntryType > >( name, description, false ) );
      currentEntry = entries.back().get();
   }

   /**
@@ -58,8 +67,8 @@ class ConfigDescription
   void addRequiredEntry( const String& name,
                          const String& description )
   {
      currentEntry = new ConfigEntry< EntryType >( name, description, true );
      entries.Append( currentEntry );
      entries.push_back( std::make_unique< ConfigEntry< EntryType > >( name, description, true ) );
      currentEntry = entries.back().get();
   }

   /**
@@ -75,11 +84,8 @@ class ConfigDescription
                  const String& description,
                  const EntryType& defaultValue )
   {
      currentEntry = new ConfigEntry< EntryType >( name,
                                                   description,
                                                   false,
                                                   defaultValue );
      entries. Append( currentEntry );
      entries.push_back( std::make_unique< ConfigEntry< EntryType > >( name, description, false, defaultValue ) );
      currentEntry = entries.back().get();
   }

   /**
@@ -93,8 +99,8 @@ class ConfigDescription
   void addList( const String& name,
                 const String& description )
   {
      currentEntry = new ConfigEntryList< EntryType >( name, description, false );
      entries.Append( currentEntry );
      entries.push_back( std::make_unique< ConfigEntryList< EntryType > >( name, description, false ) );
      currentEntry = entries.back().get();
   }

   /**
@@ -108,8 +114,8 @@ class ConfigDescription
   void addRequiredList( const String& name,
                         const String& description )
   {
      currentEntry = new ConfigEntryList< EntryType >( name, description, true );
      entries.Append( currentEntry );
      entries.push_back( std::make_unique< ConfigEntryList< EntryType > >( name, description, true ) );
      currentEntry = entries.back().get();
   }

   /**
@@ -125,11 +131,8 @@ class ConfigDescription
                 const String& description,
                 const EntryType& defaultValue )
   {
      currentEntry = new ConfigEntryList< EntryType >( name,
                                                          description,
                                                          false,
                                                          defaultValue );
      entries. Append( currentEntry );
      entries.push_back( std::make_unique< ConfigEntryList< EntryType > >( name, description, false, defaultValue ) );
      currentEntry = entries.back().get();
   }

   /**
@@ -142,8 +145,9 @@ class ConfigDescription
   template< typename EntryType >
   void addEntryEnum( const EntryType& entryEnum )
   {
      TNL_ASSERT( this->currentEntry,);
      ( ( ConfigEntry< EntryType >* ) currentEntry )->getEnumValues().Append( entryEnum );
      TNL_ASSERT_TRUE( this->currentEntry, "there is no current entry" );
      ConfigEntry< EntryType >& entry = dynamic_cast< ConfigEntry< EntryType >& >( *currentEntry );
      entry.getEnumValues().push_back( entryEnum );
   }

   /**
@@ -154,8 +158,9 @@ class ConfigDescription
    */
   void addEntryEnum( const char* entryEnum )
   {
      TNL_ASSERT( this->currentEntry,);
      ( ( ConfigEntry< String >* ) currentEntry )->getEnumValues().Append( String( entryEnum ) );
      TNL_ASSERT_TRUE( this->currentEntry, "there is no current entry" );
      ConfigEntry< String >& entry = dynamic_cast< ConfigEntry< String >& >( *currentEntry );
      entry.getEnumValues().push_back( String( entryEnum ) );
   }

   /**
@@ -165,8 +170,8 @@ class ConfigDescription
    */
   void addDelimiter( const String& delimiter )
   {
      entries.Append( new ConfigDelimiter( delimiter ) );
      currentEntry = 0;
      entries.push_back( std::make_unique< ConfigDelimiter >( delimiter ) );
      currentEntry = nullptr;
   }

   /**
@@ -176,10 +181,11 @@ class ConfigDescription
    */
   const ConfigEntryBase* getEntry( const String& name ) const
   {
      for( int i = 0; i < entries.getSize(); i++ )
      const int entries_num = entries.size();
      for( int i = 0; i < entries_num; i++ )
         if( entries[ i ]->name == name )
            return entries[ i ];
      return NULL;
            return entries[ i ].get();
      return nullptr;
   }


@@ -187,32 +193,34 @@ class ConfigDescription
   //const String getEntryType( const char* name ) const;

   //! Returns zero pointer if there is no default value
   template< class T > const T* getDefaultValue( const String& name ) const
   template< class T >
   const T* getDefaultValue( const String& name ) const
   {
      int i;
      const int entries_num = entries. getSize();
      for( i = 0; i < entries_num; i ++ )
         if( entries[ i ] -> name == name )
         {
            if( entries[ i ] -> hasDefaultValue )
               return ( ( ConfigEntry< T > * ) entries[ i ] ) -> default_value;
            else return NULL;
      const int entries_num = entries.size();
      for( int i = 0; i < entries_num; i++ )
         if( entries[ i ]->name == name ) {
            if( entries[ i ]->hasDefaultValue ) {
               const ConfigEntry< T >& entry = dynamic_cast< ConfigEntry< T >& >( *entries[ i ] );
               return entry->default_value;
            }
            return nullptr;
         }
      std::cerr << "Asking for the default value of unknown parameter." << std::endl;
      return NULL;
      return nullptr;
   }
 
   //! Returns zero pointer if there is no default value
   template< class T > T* getDefaultValue( const String& name )
   {
      int i;
      const int entries_num = entries. getSize();
      for( i = 0; i < entries_num; i ++ )
         if( entries[ i ] -> name == name )
   template< class T >
   T* getDefaultValue( const String& name )
   {
            if( entries[ i ] -> hasDefaultValue )
               return ( ( ConfigEntry< T > * ) entries[ i ] ) -> default_value;
            else return NULL;
      const int entries_num = entries.size();
      for( int i = 0; i < entries_num; i++ )
         if( entries[ i ] -> name == name ) {
            if( entries[ i ] -> hasDefaultValue ) {
               ConfigEntry< T >& entry = dynamic_cast< ConfigEntry< T >& >( *entries[ i ] );
               return entry->default_value;
            }
            return nullptr;
         }
      std::cerr << "Asking for the default value of unknown parameter." << std::endl;
      return NULL;
@@ -243,19 +251,10 @@ class ConfigDescription

   //bool parseConfigDescription( const char* file_name );

   /**
    * \brief Basic destructor.
    */
   ~ConfigDescription();

protected:

   Containers::List< ConfigEntryBase* > entries;

   ConfigEntryBase* currentEntry;

   std::vector< std::unique_ptr< ConfigEntryBase > > entries;
   ConfigEntryBase* currentEntry = nullptr;
};

} //namespace Config
} //namespace TNL
+32 −33
Original line number Diff line number Diff line
@@ -10,8 +10,9 @@

#pragma once

#include <vector>

#include <TNL/Config/ConfigEntryBase.h>
#include <TNL/Containers/List.h>

namespace TNL {
namespace Config {
@@ -21,7 +22,7 @@ struct ConfigEntry : public ConfigEntryBase
{
   EntryType defaultValue;

   Containers::List< EntryType > enumValues;
   std::vector< EntryType > enumValues;

   public:

@@ -62,14 +63,14 @@ struct ConfigEntry : public ConfigEntryBase
      return convertToString( defaultValue );
   };

   Containers::List< EntryType >& getEnumValues()
   std::vector< EntryType >& getEnumValues()
   {
      return this->enumValues;
   }

   bool hasEnumValues() const
   {
      if( enumValues.getSize() != 0 )
      if( enumValues.size() > 0 )
         return true;
      return false;
   }
@@ -78,7 +79,7 @@ struct ConfigEntry : public ConfigEntryBase
   {
      std::cout << "- Can be:           ";
      int i;
      for( i = 0; i < enumValues.getSize() - 1; i++ )
      for( i = 0; i < (int) enumValues.size() - 1; i++ )
         std::cout << enumValues[ i ] << ", ";
      std::cout << enumValues[ i ];
      std::cout << " ";
@@ -86,10 +87,10 @@ struct ConfigEntry : public ConfigEntryBase

   bool checkValue( const EntryType& value ) const
   {
      if( this->enumValues.getSize() != 0 )
      if( this->enumValues.size() > 0 )
      {
         bool found( false );
         for( int i = 0; i < this->enumValues.getSize(); i++ )
         for( int i = 0; i < (int) this->enumValues.size(); i++ )
            if( value == this->enumValues[ i ] )
            {
               found = true;
@@ -104,9 +105,7 @@ struct ConfigEntry : public ConfigEntryBase
         }
      }
      return true;
   };
   
   //~ConfigEntry(){};
   }
};

} // namespace Config
Loading