Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:14:58

0001 // lAger: General Purpose l/A-event Generator
0002 // Copyright (C) 2016-2021 Sylvester Joosten <sjoosten@anl.gov>
0003 // 
0004 // This file is part of lAger.
0005 // 
0006 // lAger is free software: you can redistribute it and/or modify
0007 // it under the terms of the GNU General Public License as published by
0008 // the Free Shoftware Foundation, either version 3 of the License, or
0009 // (at your option) any later version.
0010 // 
0011 // lAger is distributed in the hope that it will be useful,
0012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 // GNU General Public License for more details.
0015 // 
0016 // You should have received a copy of the GNU General Public License
0017 // along with lAger.  If not, see <https://www.gnu.org/licenses/>.
0018 // 
0019 
0020 #ifndef LAGER_CORE_STRINGIFY_LOADED
0021 #define LAGER_CORE_STRINGIFY_LOADED
0022 
0023 #include <boost/lexical_cast.hpp>
0024 #include <lager/core/type_traits.hh>
0025 #include <string>
0026 #include <type_traits>
0027 #include <utility>
0028 
0029 namespace lager {
0030 
0031 // implementation prototypes needed for stringify
0032 namespace stringify_impl {
0033 // standard accessor returns el untouched
0034 template <class RangeElement, class = typename std::enable_if<
0035                                   !is_container<RangeElement>::value>::type>
0036 RangeElement element_accessor(const RangeElement& el);
0037 // container accessor returns stringified version of container
0038 template <class RangeElement, class = typename std::enable_if<
0039                                   is_container<RangeElement>::value>::type,
0040           class = void>
0041 RangeElement element_accessor(const RangeElement& el);
0042 // pair element accessor returns a "<first>: <second>" string
0043 template <class U, class V>
0044 std::string element_accessor(const std::pair<U, V>& p);
0045 }
0046 } // ns lager
0047 
0048 // =============================================================================
0049 // stringify(range, delimiter, accessor):
0050 // return a stringified version of a range, with the elements separated by
0051 // <delimiter>. The elements are accessed by <acc> prior to string conversion.
0052 // Default accessor just returns the string, with specializations for STL
0053 // containers and std::pair, allowing for nesting.
0054 // =============================================================================
0055 namespace lager {
0056 template <class Range,
0057           class ElementAccessor = decltype(
0058               stringify_impl::element_accessor<typename Range::value_type>)>
0059 std::string
0060 stringify(const Range& r, const std::string& delimiter = ", ",
0061           ElementAccessor acc =
0062               stringify_impl::element_accessor<typename Range::value_type>);
0063 } // ns lager
0064 
0065 // =============================================================================
0066 // Implementation
0067 // =============================================================================
0068 namespace lager {
0069 namespace stringify_impl {
0070 template <class RangeElement, class>
0071 RangeElement element_accessor(const RangeElement& el) {
0072   return el;
0073 }
0074 template <class RangeElement, class, class>
0075 RangeElement element_accessor(const RangeElement& el) {
0076   return "(" + stringify(el) + ")";
0077 }
0078 template <class U, class V>
0079 std::string element_accessor(const std::pair<U, V>& p) {
0080   return (boost::lexical_cast<std::string>(element_accessor(p.first)) + ": " +
0081           boost::lexical_cast<std::string>(element_accessor(p.second)));
0082 }
0083 }
0084 
0085 template <class Range, class ElementAccessor>
0086 std::string stringify(const Range& r, const std::string& delimiter,
0087                       ElementAccessor acc) {
0088   std::string str{};
0089   for (const auto& el : r) {
0090     if (!str.empty()) {
0091       str += delimiter;
0092     }
0093     str += boost::lexical_cast<std::string>(acc(el));
0094   }
0095   return str;
0096 }
0097 } // ns lager
0098 
0099 #endif