// // ******************************************************************** // * License and Disclaimer * // * * // * The Geant4 software is copyright of the Copyright Holders of * // * the Geant4 Collaboration. It is provided under the terms and * // * conditions of the Geant4 Software License, included in the file * // * LICENSE and available at http://cern.ch/geant4/license . These * // * include a list of copyright holders. * // * * // * Neither the authors of this software system, nor their employing * // * institutes,nor the agencies providing financial support for this * // * work make any representation or warranty, express or implied, * // * regarding this software system or assume any liability for its * // * use. Please see the license in the file LICENSE and URL above * // * for the full disclaimer and the limitation of liability. * // * * // * This code implementation is the result of the scientific and * // * technical work of the GEANT4 collaboration. * // * By using, copying, modifying or distributing the software (or * // * any work based on the software) you agree to acknowledge its * // * use in resulting scientific publications, and indicate your * // * acceptance of all terms of the Geant4 Software license. * // ******************************************************************** // // $Id: G4AttValueFilterT.hh,v 1.3 2006/12/13 15:50:00 gunter Exp $ // GEANT4 tag $Name: $ // // Templated class for G4AttValue filters. // // Jane Tinslay, September 2006 // #ifndef G4ATTVALUEFILTERT_HH #define G4ATTVALUEFILTERT_HH #include "G4VAttValueFilter.hh" #include "G4ConversionFatalError.hh" #include "G4ConversionUtils.hh" namespace { // Helper classes template class IsEqual{ public: IsEqual(const T& value): fValue(value) {}; bool operator()(const std::pair& myPair) const { return myPair.second == fValue; } private: T fValue; }; template class InInterval{ public: InInterval(const T& value): fValue(value) {}; bool operator()(const std::pair >& myPair) const { T min = myPair.second.first; T max = myPair.second.second; return ((fValue > min || fValue == min) && (fValue < max)); } private: T fValue; }; } template class G4AttValueFilterT : public ConversionErrorPolicy, public G4VAttValueFilter { public: // Constructor G4AttValueFilterT(); // Destructor virtual ~G4AttValueFilterT(); // Filter methods G4bool Accept(const G4AttValue& attVal) const; G4bool GetValidElement(const G4AttValue& input, G4String& interval) const; // Print configuration virtual void PrintAll(std::ostream& ostr) const; // Reset virtual void Reset(); void LoadIntervalElement(const G4String& input); void LoadSingleValueElement(const G4String& input); private: typedef std::pair Pair; typedef typename std::map IntervalMap; typedef std::map SingleValueMap; // Data members IntervalMap fIntervalMap; SingleValueMap fSingleValueMap; }; template G4AttValueFilterT::G4AttValueFilterT() {} template G4AttValueFilterT::~G4AttValueFilterT() {} template G4bool G4AttValueFilterT::GetValidElement(const G4AttValue& attValue, G4String& element) const { T value; G4String input = attValue.GetValue(); if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); typename SingleValueMap::const_iterator iterValues = std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual(value)); if (iterValues != fSingleValueMap.end()) { element = iterValues->first; return true; } typename IntervalMap::const_iterator iterIntervals = std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval(value)); if (iterIntervals != fIntervalMap.end()) { element = iterIntervals->first; return true; } return false; } template G4bool G4AttValueFilterT::Accept(const G4AttValue& attValue) const { T value; G4String input = attValue.GetValue(); if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); typename SingleValueMap::const_iterator iterValues = std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual(value)); if (iterValues != fSingleValueMap.end()) return true; typename IntervalMap::const_iterator iterIntervals = std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval(value)); if (iterIntervals != fIntervalMap.end()) return true; return false; } template void G4AttValueFilterT::LoadIntervalElement(const G4String& input) { T min; T max; if (!G4ConversionUtils::Convert(input, min, max)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); std::pair myPair(min, max); fIntervalMap[input] = myPair; } template void G4AttValueFilterT::LoadSingleValueElement(const G4String& input) { T output; if (!G4ConversionUtils::Convert(input, output)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); fSingleValueMap[input] = output; } template void G4AttValueFilterT::PrintAll(std::ostream& ostr) const { ostr<<"Printing data for filter: "<second.first<<" : "<second.second<second< void G4AttValueFilterT::Reset() { fIntervalMap.clear(); fSingleValueMap.clear(); } #endif