Commit 89539a5b authored by Pawel Sznajder's avatar Pawel Sznajder
Browse files

Add `std::string` based constructor to allow set type by string of

characters (and not cast from integer) #24
parent cc22ab32
...@@ -21,53 +21,74 @@ namespace NumA { ...@@ -21,53 +21,74 @@ namespace NumA {
*/ */
class IntegratorType1D { class IntegratorType1D {
public: public:
/**
* Type of integration. /**
*/ * Type of integration.
enum Type { */
UNDEFINED = 0, TRAPEZOIDAL, TRAPEZOIDALLOG, GaussLegendre, DEXP, GK21_ADAPTIVE enum Type {
}; UNDEFINED = 0, //!< Undefined type.
TRAPEZOIDAL = 1, //!< Trapezoidal method with a fixed number of equidistant steps.
/** TRAPEZOIDALLOG = 2, //!< Trapezoidal method with a fixed number with equidistantly distributed steps in [ln(min), ln(max)] range.
* Default constructor. GaussLegendre = 3, //!< Gauss-Legendre method with a fixed number of steps.
*/ DEXP = 4, //!< Adaptive double exponential method.
IntegratorType1D(); GK21_ADAPTIVE = 5 //!< Adaptive Gauss-Kronrod method with 21 point rules.
};
IntegratorType1D(Type type);
/**
/** * Default constructor.
* Default destructor. */
*/ IntegratorType1D();
virtual ~IntegratorType1D();
/**
/** * Assignment constructor.
* Operator that casts back into an enum. * @param type Type to be assigned.
*/ */
operator Type() const; IntegratorType1D(Type type);
/** /**
* Returns a formatted string displaying the type. * Assignment constructor trying to match integration type from given string. If unable to match set IntegratorType1D::UNDEFINED.
* @return std::string * @param integratorType1DString String to be matched.
*/ */
std::string toString(); IntegratorType1D(const std::string& integratorType1DString);
/** /**
* Returns a short name for the type. * Default destructor.
* @return std::string */
*/ virtual ~IntegratorType1D();
std::string getShortName();
/**
/** * Operator that casts back into an enum.
* Returns the wrapped enum. */
* @return IntegratorType1D::Type operator Type() const;
*/
IntegratorType1D::Type getType() const; /**
* Returns a formatted string displaying the type.
void setType(Type type); * @return std::string
*/
std::string toString();
/**
* Returns a short name for the type.
* @return std::string
*/
std::string getShortName();
/**
* Returns the wrapped enum.
* @return IntegratorType1D::Type
*/
IntegratorType1D::Type getType() const;
/**
* Set the type of integration.
* @param type Integration type to be set.
*/
void setType(Type type);
private: private:
IntegratorType1D::Type m_type; ///< Type of integration (enum). IntegratorType1D::Type m_type; ///< Type of integration (enum).
}; };
} /* namespace NumA */ } /* namespace NumA */
......
#include "../../../../include/NumA/integration/one_dimension/IntegratorType1D.h" #include "../../../../include/NumA/integration/one_dimension/IntegratorType1D.h"
#include <ElementaryUtils/logger/LoggerManager.h>
#include <ElementaryUtils/string_utils/Formatter.h>
#include <ElementaryUtils/string_utils/StringUtils.h>
namespace NumA { namespace NumA {
IntegratorType1D::IntegratorType1D() : IntegratorType1D::IntegratorType1D() :
m_type(IntegratorType1D::UNDEFINED) { m_type(IntegratorType1D::UNDEFINED) {
} }
IntegratorType1D::IntegratorType1D(Type type) : IntegratorType1D::IntegratorType1D(Type type) :
m_type(type) { m_type(type) {
}
IntegratorType1D::IntegratorType1D(const std::string& integratorType1DString) {
if (ElemUtils::StringUtils::equals(integratorType1DString, "TRAPEZOIDAL")) {
m_type = IntegratorType1D::TRAPEZOIDAL;
} else if (ElemUtils::StringUtils::equals(integratorType1DString,
"TRAPEZOIDALLOG")) {
m_type = IntegratorType1D::TRAPEZOIDALLOG;
} else if (ElemUtils::StringUtils::equals(integratorType1DString,
"GaussLegendre")) {
  • Beware here, it's not the same string as in line 56.

  • Nice catch Nabil!

    Maybe we can change the type and all strings to GL, it is annoying that only this one is written with lowercases.

Please register or sign in to reply
m_type = IntegratorType1D::GaussLegendre;
} else if (ElemUtils::StringUtils::equals(integratorType1DString, "DEXP")) {
m_type = IntegratorType1D::DEXP;
} else if (ElemUtils::StringUtils::equals(integratorType1DString,
"GK21_ADAPTIVE")) {
m_type = IntegratorType1D::GK21_ADAPTIVE;
} else {
ElemUtils::LoggerManager::getInstance()->warn("IntegratorType1D", __func__,
ElemUtils::Formatter()
<< "Cannot found IntegratorType1D for string = "
<< integratorType1DString);
}
} }
IntegratorType1D::~IntegratorType1D() { IntegratorType1D::~IntegratorType1D() {
} }
IntegratorType1D::operator IntegratorType1D::Type() const { IntegratorType1D::operator IntegratorType1D::Type() const {
return m_type; return m_type;
} }
std::string IntegratorType1D::toString() { std::string IntegratorType1D::toString() {
switch (m_type) { switch (m_type) {
case TRAPEZOIDAL: case TRAPEZOIDAL:
return "TRAPEZOIDAL"; return "TRAPEZOIDAL";
break; break;
case TRAPEZOIDALLOG: case TRAPEZOIDALLOG:
return "TRAPEZOIDALLOG"; return "TRAPEZOIDALLOG";
break; break;
case GaussLegendre: case GaussLegendre:
return "Gauss-Legendre"; return "Gauss-Legendre";
break; break;
case DEXP: case DEXP:
return "DEXP"; return "DEXP";
break; break;
case GK21_ADAPTIVE: case GK21_ADAPTIVE:
return "GK21_ADAPTIVE"; return "GK21_ADAPTIVE";
break; break;
default: default:
return "UNDEFINED"; return "UNDEFINED";
} }
} }
std::string IntegratorType1D::getShortName() { std::string IntegratorType1D::getShortName() {
switch (m_type) { switch (m_type) {
case TRAPEZOIDAL: case TRAPEZOIDAL:
return "TRAPEZOIDAL"; return "TRAPEZOIDAL";
break; break;
case TRAPEZOIDALLOG: case TRAPEZOIDALLOG:
return "TRAPEZOIDALLOG"; return "TRAPEZOIDALLOG";
break; break;
case GaussLegendre: case GaussLegendre:
return "GL"; return "GL";
break; break;
case DEXP: case DEXP:
return "DEXP"; return "DEXP";
break; break;
case GK21_ADAPTIVE: case GK21_ADAPTIVE:
return "GK21_ADAPTIVE"; return "GK21_ADAPTIVE";
break; break;
default: default:
return "UNDEFINED"; return "UNDEFINED";
} }
} }
IntegratorType1D::Type IntegratorType1D::getType() const { IntegratorType1D::Type IntegratorType1D::getType() const {
return m_type; return m_type;
} }
void IntegratorType1D::setType(Type type) { void IntegratorType1D::setType(Type type) {
m_type = type; m_type = type;
} }
} /* namespace NumA */ } /* namespace NumA */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment