#ifndef __JTRIGGER__JPREPROCESSOR__ #define __JTRIGGER__JPREPROCESSOR__ #include #include #include #include #include "JLang/JException.hh" #include "JLang/JPredicate.hh" #include "JLang/JVectorize.hh" /** * \file * * Auxiliaries for pre-processing of hits. * \author mdejong */ namespace JTRIGGER {} namespace JPP { using namespace JTRIGGER; } namespace JTRIGGER { using JLANG::JTypeInformationException; /** * Auxiliary class for specifying the way of pre-processing of hits. */ struct JPreprocessor : public std::string { /** * Preprocessing options. */ enum JOption_t { none_t = 0, //!< no pre-processing join_t, //!< join consecutive hits according match criterion filter_t, //!< filter consecutive hits according match criterion remove_t //!< remove consecutive hits according match criterion }; typedef std::pair pair_type; typedef std::vector data_type; /** * Default constructor. */ JPreprocessor() : std::string(get_options()[0].first) {} /** * Type conversion operator. * * \return option */ operator JOption_t () const { return JPreprocessor::getOption(*this); } /** * Get option. * * \param option option * \return option */ static JOption_t getOption(const JPreprocessor& option) { using namespace std; using namespace JPP; data_type::const_iterator i = find_if(get_options().begin(), get_options().end(), make_predicate(&pair_type::first, JPreprocessor(option))); if (i != get_options().end()) { return i->second; } else { THROW(JTypeInformationException, "Invalid option <" << option << ">"); } }; /** * Get option. * * \param option option * \return option */ static JPreprocessor getOption(const JOption_t option) { using namespace std; using namespace JPP; data_type::const_iterator i = find_if(get_options().begin(), get_options().end(), make_predicate(&pair_type::second, option)); if (i != get_options().end()) { return i->first; } else { THROW(JTypeInformationException, "Invalid option <" << option << ">"); } }; /** * Get options. * * \return list of options */ static std::vector getOptions() { return JLANG::make_array(get_options().begin(), get_options().end(), &pair_type::first); } /** * Get options. * * \param option option * \return list of options */ static std::vector getOptions(const JOption_t option) { std::vector buffer(1, getOption(option)); for (const auto& i : get_options()) { if (i.second != option) { buffer.push_back(i.first); } } return buffer; } protected: static const char ENUM_EXTENSION = '_'; //!< Extension of enumeration names. /** * Constructor. * * \param option option */ JPreprocessor(const std::string& option) : std::string(option) { using namespace std; const string::size_type pos = this->rfind(ENUM_EXTENSION); if (pos != string::npos) { this->erase(pos); } } /** * Get paired options. * * \return list of paired options */ static const data_type& get_options() { static data_type buffer; if (buffer.empty()) { #define MAKE_ENTRY(A) std::make_pair(JPreprocessor(#A), A) buffer.push_back(MAKE_ENTRY(none_t)); buffer.push_back(MAKE_ENTRY(join_t)); buffer.push_back(MAKE_ENTRY(filter_t)); buffer.push_back(MAKE_ENTRY(remove_t)); #undef MAKE_ENTRY } return buffer; } }; } #endif