From: rao Date: Mon, 1 Nov 2010 18:53:25 +0000 (+0000) Subject: Add create and pattern_factory functions in parameter_handler. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd61550a4b64b1f0dfb0ebfc5e067c8a4c958afe;p=dealii-svn.git Add create and pattern_factory functions in parameter_handler. git-svn-id: https://svn.dealii.org/trunk@22579 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/include/deal.II/base/parameter_handler.h b/deal.II/include/deal.II/base/parameter_handler.h index d1acd7027a..d683eac337 100644 --- a/deal.II/include/deal.II/base/parameter_handler.h +++ b/deal.II/include/deal.II/base/parameter_handler.h @@ -53,6 +53,7 @@ class LogStream; */ namespace Patterns { + /** * Base class to declare common * interface. The purpose of this @@ -145,6 +146,12 @@ namespace Patterns virtual unsigned int memory_consumption () const; }; + /** + * Returns pointer to the correct + * derived class based on description. + */ + PatternBase * pattern_factory (const std::string& description); + /** * Test for the string being an * integer. If bounds are given @@ -251,6 +258,15 @@ namespace Patterns * function. */ virtual PatternBase * clone () const; + + /** + * Creates new object if the start + * of description matches description_init. + * Ownership of that object is + * transferred to the caller of this + * function. + */ + static Integer* create (const std::string& description); private: /** @@ -390,6 +406,15 @@ namespace Patterns * function. */ virtual PatternBase * clone () const; + + /** + * Creates new object if the start + * of description matches description_init. + * Ownership of that object is + * transferred to the caller of this + * function. + */ + static Double* create (const std::string& description); private: /** @@ -482,6 +507,15 @@ namespace Patterns * bytes) of this object. */ unsigned int memory_consumption () const; + + /** + * Creates new object if the start + * of description matches description_init. + * Ownership of that object is + * transferred to the caller of this + * function. + */ + static Selection* create (const std::string& description); private: /** @@ -573,6 +607,15 @@ namespace Patterns * function. */ virtual PatternBase * clone () const; + + /** + * Creates new object if the start + * of description matches description_init. + * Ownership of that object is + * transferred to the caller of this + * function. + */ + static List* create (const std::string& description); /** * Determine an estimate for @@ -679,6 +722,15 @@ namespace Patterns * function. */ virtual PatternBase * clone () const; + + /** + * Creates new object if the start + * of description matches description_init. + * Ownership of that object is + * transferred to the caller of this + * function. + */ + static MultipleSelection* create (const std::string& description); /** * Determine an estimate for @@ -729,6 +781,14 @@ namespace Patterns */ Bool (); + /** + * Return a description of + * the pattern that valid + * strings are expected to + * match. + */ + virtual std::string description () const; + /** * Return a copy of the * present object, which is @@ -739,6 +799,21 @@ namespace Patterns * function. */ virtual PatternBase * clone () const; + + /** + * Creates new object if the start + * of description matches description_init. + * Ownership of that object is + * transferred to the caller of this + * function. + */ + static Bool* create (const std::string& description); + + private: + /** + * Initial part of description + */ + static const char* description_init; }; /** @@ -783,7 +858,17 @@ namespace Patterns * function. */ virtual PatternBase * clone () const; - + + /** + * Creates new object if the start + * of description matches description_init. + * Ownership of that object is + * transferred to the caller of this + * function. + */ + static Anything* create (const std::string& description); + + private: /** * Initial part of description */ @@ -864,6 +949,15 @@ namespace Patterns * file type flag */ FileType file_type; + + /** + * Creates new object if the start + * of description matches description_init. + * Ownership of that object is + * transferred to the caller of this + * function. + */ + static FileName* create (const std::string& description); private: /** @@ -925,6 +1019,15 @@ namespace Patterns * function. */ virtual PatternBase * clone () const; + + /** + * Creates new object if the start + * of description matches description_init. + * Ownership of that object is + * transferred to the caller of this + * function. + */ + static DirectoryName* create (const std::string& description); private: /** diff --git a/deal.II/source/base/parameter_handler.cc b/deal.II/source/base/parameter_handler.cc index 5a87bb8bf0..009a7260fe 100644 --- a/deal.II/source/base/parameter_handler.cc +++ b/deal.II/source/base/parameter_handler.cc @@ -71,6 +71,53 @@ namespace Patterns + PatternBase* pattern_factory (const std::string& description) + { + PatternBase* p; + + p = Integer::create(description); + if (p != 0) + return p; + + p = Double::create(description); + if (p !=0 ) + return p; + + p = Selection::create(description); + if (p !=0 ) + return p; + + p = List::create(description); + if (p !=0 ) + return p; + + p = MultipleSelection::create(description); + if (p !=0 ) + return p; + + p = Bool::create(description); + if (p!=0 ) + return p; + + p = Anything::create(description); + if (p !=0 ) + return p; + + p = FileName::create(description); + if (p !=0 ) + return p; + + p = DirectoryName::create(description); + if (p!=0 ) + return p; + + Assert(false, ExcNotImplemented()); + + return 0; + } + + + PatternBase::~PatternBase () {} @@ -90,7 +137,7 @@ namespace Patterns return sizeof(*this) + 32; } - + const int Integer::min_int_value = #ifdef HAVE_STD_NUMERIC_LIMITS @@ -171,6 +218,38 @@ namespace Patterns + Integer* Integer::create (const std::string& description) + { + if(description.compare(0, std::strlen(description_init), description_init) == 0) + { + std::istringstream is(description); + + if(is.str().size() > strlen(description_init) + 1) + { +//TODO: verify that description matches the pattern "^\[Integer range \d+\.\.\.\d+\]$" + int lower_bound, upper_bound; + + is.ignore(strlen(description_init) + strlen(" range ")); + + if(!(is >> lower_bound)) + return new Integer(); + + is.ignore(strlen("...")); + + if(!(is >> upper_bound)) + return new Integer(); + + return new Integer(lower_bound, upper_bound); + } + else + return new Integer(); + } + else + return 0; + } + + + const double Double::min_double_value = #ifdef HAVE_STD_NUMERIC_LIMITS -std::numeric_limits::max(); @@ -252,6 +331,37 @@ namespace Patterns + Double* Double::create (const std::string& description) + { + if(description.compare(0, std::strlen(description_init), description_init) == 0) + { + std::istringstream is(description); + + if(is.str().size() > strlen(description_init) + 1) + { + double lower_bound, upper_bound; + + is.ignore(strlen(description_init) + strlen(" range ")); + + if(!(is >> lower_bound)) + return new Double(); + + is.ignore(strlen("...")); + + if(!(is >> upper_bound)) + return new Double(); + + return new Double(lower_bound, upper_bound); + } + else + return new Double(); + } + else + return 0; + } + + + const char* Selection::description_init = "[Selection"; @@ -319,6 +429,23 @@ namespace Patterns + Selection* Selection::create (const std::string& description) + { + if(description.compare(0, std::strlen(description_init), description_init) == 0) + { + std::string sequence(description); + + sequence.erase(0, std::strlen(description_init) + 1); + sequence.erase(sequence.length()-2, 2); + + return new Selection(sequence); + } + else + return 0; + } + + + const unsigned int List::max_int_value = #ifdef HAVE_STD_NUMERIC_LIMITS std::numeric_limits::max(); @@ -429,6 +556,37 @@ namespace Patterns + List* List::create (const std::string& description) + { + if(description.compare(0, std::strlen(description_init), description_init) == 0) + { + int min_elements, max_elements; + + std::istringstream is(description); + is.ignore(strlen(description_init) + strlen(" list of <")); + + std::auto_ptr new_description (new char[is.str().size() + 1]); + is.getline(&(*new_description), is.str().size(), '>'); + std::string str(&(*new_description)); + + std::auto_ptr base_pattern (pattern_factory(str)); + + is.ignore(strlen(" of length ")); + if(!(is >> min_elements)) + return new List(*base_pattern); + + is.ignore(strlen("...")); + if(!(is >> max_elements)) + return new List(*base_pattern); + + return new List(*base_pattern, min_elements, max_elements); + } + else + return 0; + } + + + const char* MultipleSelection::description_init = "[MultipleSelection"; @@ -539,6 +697,26 @@ namespace Patterns + MultipleSelection* MultipleSelection::create (const std::string& description) + { + if(description.compare(0, std::strlen(description_init), description_init) == 0) + { + std::string sequence(description); + + sequence.erase(0, std::strlen(description_init) + 1); + sequence.erase(sequence.length()-2, 2); + + return new MultipleSelection(sequence); + } + else + return 0; + } + + + + const char* Bool::description_init = "[Bool"; + + Bool::Bool () : Selection ("true|false") @@ -546,6 +724,18 @@ namespace Patterns + std::string Bool::description () const + { + std::ostringstream description; + + description << description_init + << "]"; + + return description.str(); + } + + + PatternBase * Bool::clone () const { @@ -554,6 +744,16 @@ namespace Patterns + Bool* Bool::create (const std::string& description) + { + if(description.compare(0, std::strlen(description_init), description_init) == 0) + return new Bool(); + else + return 0; + } + + + const char* Anything::description_init = "[Anything"; @@ -589,6 +789,16 @@ namespace Patterns + Anything* Anything::create (const std::string& description) + { + if(description.compare(0, std::strlen(description_init), description_init) == 0) + return new Anything(); + else + return 0; + } + + + const char* FileName::description_init = "[FileName"; @@ -629,6 +839,31 @@ namespace Patterns + FileName* FileName::create (const std::string& description) + { + if(description.compare(0, std::strlen(description_init), description_init) == 0) + { + std::istringstream is(description); + std::string file_type; + FileType type; + + is.ignore(strlen(description_init) + strlen(" (Type:")); + + is >> file_type; + + if (file_type == "input)]") + type = input; + else + type = output; + + return new FileName(type); + } + else + return 0; + } + + + const char* DirectoryName::description_init = "[DirectoryName"; @@ -661,6 +896,16 @@ namespace Patterns return new DirectoryName(); } + + + DirectoryName* DirectoryName::create (const std::string& description) + { + if(description.compare(0, std::strlen(description_init), description_init) == 0) + return new DirectoryName(); + else + return 0; + } + } // end namespace Patterns