LCOV - code coverage report
Current view: top level - src/utils/common - ToString.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 125 129 96.9 %
Date: 2024-05-02 15:31:40 Functions: 88 97 90.7 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2002-2024 German Aerospace Center (DLR) and others.
       4             : // This program and the accompanying materials are made available under the
       5             : // terms of the Eclipse Public License 2.0 which is available at
       6             : // https://www.eclipse.org/legal/epl-2.0/
       7             : // This Source Code may also be made available under the following Secondary
       8             : // Licenses when the conditions for such availability set forth in the Eclipse
       9             : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10             : // or later which is available at
      11             : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12             : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13             : /****************************************************************************/
      14             : /// @file    ToString.h
      15             : /// @author  Christian Roessel
      16             : /// @author  Daniel Krajzewicz
      17             : /// @author  Jakob Erdmann
      18             : /// @author  Michael Behrisch
      19             : /// @date    Wed, 23 Sep 2002
      20             : ///
      21             : // -------------------
      22             : /****************************************************************************/
      23             : #pragma once
      24             : #include <config.h>
      25             : #include <sstream>
      26             : #include <string>
      27             : #include <iomanip>
      28             : #include <algorithm>
      29             : #include <list>
      30             : #include <utils/xml/SUMOXMLDefinitions.h>
      31             : #include <utils/common/SUMOVehicleClass.h>
      32             : #include <utils/common/Named.h>
      33             : #include <utils/distribution/Distribution_Parameterized.h>
      34             : #include <utils/vehicle/SUMOVTypeParameter.h>
      35             : #include "StdDefs.h"
      36             : 
      37             : 
      38             : // ===========================================================================
      39             : // class definitions
      40             : // ===========================================================================
      41             : /**
      42             :  * Template for conversions from origin format to string representation
      43             :  * (when supplied by c++/the stl)
      44             :  */
      45             : template <class T>
      46   171094918 : inline std::string toString(const T& t, std::streamsize accuracy = gPrecision) {
      47   171094918 :     std::ostringstream oss;
      48             :     oss.setf(std::ios::fixed, std::ios::floatfield);
      49   171094918 :     oss << std::setprecision(accuracy);
      50   118041978 :     oss << t;
      51   171094918 :     return oss.str();
      52   171094918 : }
      53             : 
      54             : 
      55             : template<typename T>
      56        1969 : inline std::string toHex(const T i, std::streamsize numDigits = 0) {
      57             :     // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
      58        1969 :     std::stringstream stream;
      59        3938 :     stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
      60        1969 :     return stream.str();
      61        1969 : }
      62             : 
      63             : 
      64             : inline std::string toString(const Named* obj, std::streamsize accuracy) {
      65             :     UNUSED_PARAMETER(accuracy);
      66             :     return Named::getIDSecure(obj);
      67             : }
      68             : 
      69             : 
      70             : template <>
      71    17457952 : inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
      72             :     UNUSED_PARAMETER(accuracy);
      73    17457952 :     return SUMOXMLDefinitions::Tags.getString(tag);
      74             : }
      75             : 
      76             : 
      77             : template <>
      78    86543108 : inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
      79             :     UNUSED_PARAMETER(accuracy);
      80    86543108 :     return SUMOXMLDefinitions::Attrs.getString(attr);
      81             : }
      82             : 
      83             : 
      84             : template <>
      85       92685 : inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
      86             :     UNUSED_PARAMETER(accuracy);
      87       92685 :     return SUMOXMLDefinitions::NodeTypes.getString(nodeType);
      88             : }
      89             : 
      90             : 
      91             : template <>
      92      133245 : inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
      93             :     UNUSED_PARAMETER(accuracy);
      94      133245 :     return SUMOXMLDefinitions::EdgeFunctions.getString(edgeFunc);
      95             : }
      96             : 
      97             : 
      98             : template <>
      99        1898 : inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
     100             :     UNUSED_PARAMETER(accuracy);
     101        1898 :     return SumoVehicleClassStrings.getString(vClass);
     102             : }
     103             : 
     104             : 
     105             : template <>
     106       97396 : inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
     107             :     UNUSED_PARAMETER(accuracy);
     108       97396 :     return SUMOXMLDefinitions::LaneSpreadFunctions.getString(lsf);
     109             : }
     110             : 
     111             : template <>
     112        9660 : inline std::string toString<ParkingType>(const ParkingType& pt, std::streamsize accuracy) {
     113             :     UNUSED_PARAMETER(accuracy);
     114        9660 :     return SUMOXMLDefinitions::ParkingTypes.getString(pt);
     115             : }
     116             : 
     117             : template <>
     118          31 : inline std::string toString<RightOfWay>(const RightOfWay& row, std::streamsize accuracy) {
     119             :     UNUSED_PARAMETER(accuracy);
     120          31 :     return SUMOXMLDefinitions::RightOfWayValues.getString(row);
     121             : }
     122             : 
     123             : template <>
     124         301 : inline std::string toString<FringeType>(const FringeType& fringeType, std::streamsize accuracy) {
     125             :     UNUSED_PARAMETER(accuracy);
     126         301 :     return SUMOXMLDefinitions::FringeTypeValues.getString(fringeType);
     127             : }
     128             : 
     129             : template <>
     130        1343 : inline std::string toString<PersonMode>(const PersonMode& personMode, std::streamsize accuracy) {
     131             :     UNUSED_PARAMETER(accuracy);
     132        1343 :     return SUMOXMLDefinitions::PersonModeValues.getString(personMode);
     133             : }
     134             : 
     135             : template <>
     136      245507 : inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
     137             :     UNUSED_PARAMETER(accuracy);
     138      245507 :     return SUMOXMLDefinitions::LinkStates.getString(linkState);
     139             : }
     140             : 
     141             : 
     142             : template <>
     143      397514 : inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
     144             :     UNUSED_PARAMETER(accuracy);
     145      397514 :     return SUMOXMLDefinitions::LinkDirections.getString(linkDir);
     146             : }
     147             : 
     148             : 
     149             : template <>
     150        2794 : inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
     151             :     UNUSED_PARAMETER(accuracy);
     152        2794 :     return SUMOXMLDefinitions::TrafficLightTypes.getString(type);
     153             : }
     154             : 
     155             : 
     156             : template <>
     157             : inline std::string toString<TrafficLightLayout>(const TrafficLightLayout& layout, std::streamsize accuracy) {
     158             :     UNUSED_PARAMETER(accuracy);
     159             :     return SUMOXMLDefinitions::TrafficLightLayouts.getString(layout);
     160             : }
     161             : 
     162             : 
     163             : template <>
     164          26 : inline std::string toString<InsertionCheck>(const InsertionCheck& check, std::streamsize accuracy) {
     165             :     UNUSED_PARAMETER(accuracy);
     166          26 :     return SUMOXMLDefinitions::InsertionChecks.getString(check);
     167             : }
     168             : 
     169             : 
     170             : template <>
     171        2721 : inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
     172             :     UNUSED_PARAMETER(accuracy);
     173        2721 :     return SUMOXMLDefinitions::LaneChangeModels.getString(model);
     174             : }
     175             : 
     176             : template <>
     177         103 : inline std::string toString<LatAlignmentDefinition>(const LatAlignmentDefinition& lad, std::streamsize accuracy) {
     178             :     UNUSED_PARAMETER(accuracy);
     179         103 :     switch (lad) {
     180             :         case LatAlignmentDefinition::RIGHT:
     181           0 :             return "right";
     182             :         case LatAlignmentDefinition::CENTER:
     183          92 :             return "center";
     184             :         case LatAlignmentDefinition::ARBITRARY:
     185          10 :             return "arbitrary";
     186             :         case LatAlignmentDefinition::NICE:
     187           0 :             return "nice";
     188             :         case LatAlignmentDefinition::COMPACT:
     189           1 :             return "compact";
     190             :         case LatAlignmentDefinition::LEFT:
     191           0 :             return "left";
     192             :         case LatAlignmentDefinition::GIVEN:
     193             :         case LatAlignmentDefinition::DEFAULT:
     194             :         default:
     195           0 :             return "";
     196             :     }
     197             : }
     198             : 
     199             : template <>
     200       14506 : inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
     201             :     UNUSED_PARAMETER(accuracy);
     202       14506 :     std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
     203             :     bool hadOne = false;
     204       14506 :     std::ostringstream oss;
     205      290120 :     for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
     206      275614 :         if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
     207       22632 :             if (hadOne) {
     208        8130 :                 oss << "|";
     209             :             } else {
     210             :                 hadOne = true;
     211             :             }
     212             :             oss << (*it);
     213             :         }
     214             :     }
     215       14506 :     return oss.str();
     216       14506 : }
     217             : 
     218             : template <>
     219             : inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
     220         699 :     return dist.toStr(accuracy);
     221             : }
     222             : 
     223             : template <typename V>
     224             : inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
     225     1133368 :     return toString<V>(v.begin(), v.end(), accuracy);
     226             : }
     227             : 
     228             : 
     229             : template <typename V>
     230     1134704 : inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
     231             :     UNUSED_PARAMETER(accuracy);
     232     1134704 :     std::ostringstream oss;
     233     6464981 :     for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
     234     5330277 :         if (it != b) {
     235     4196889 :             oss << " ";
     236             :         }
     237    10660554 :         oss << Named::getIDSecure(*it);
     238             :     }
     239     1134704 :     return oss.str();
     240     1134704 : }
     241             : 
     242             : template <typename V>
     243             : inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
     244           6 :     return toString<V>(v.begin(), v.end(), accuracy);
     245             : }
     246             : 
     247             : template <typename V>
     248           6 : inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
     249             :     UNUSED_PARAMETER(accuracy);
     250           6 :     std::ostringstream oss;
     251          14 :     for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
     252           8 :         if (it != b) {
     253           4 :             oss << " ";
     254             :         }
     255          16 :         oss << Named::getIDSecure(*it);
     256             :     }
     257           6 :     return oss.str();
     258           6 : }
     259             : 
     260             : 
     261             : 
     262             : //template <typename V>
     263             : //inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
     264             : //    return toString<V>(v.begin(), v.end(), accuracy);
     265             : //}
     266             : //
     267             : //
     268             : //template <typename V>
     269             : //inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
     270             : //    UNUSED_PARAMETER(accuracy);
     271             : //    std::ostringstream oss;
     272             : //    for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
     273             : //        if (it != b) {
     274             : //            oss << " ";
     275             : //        }
     276             : //        oss << Named::getIDSecure(*it);
     277             : //    }
     278             : //    return oss.str();
     279             : //}
     280             : 
     281             : 
     282             : template <typename T, typename T_BETWEEN>
     283    17830022 : inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
     284    17830022 :     std::ostringstream oss;
     285             :     bool connect = false;
     286    38145537 :     for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
     287    20315515 :         if (connect) {
     288     5084056 :             oss << toString(between, accuracy);
     289             :         } else {
     290             :             connect = true;
     291             :         }
     292    40631030 :         oss << toString(*it, accuracy);
     293             :     }
     294    17830022 :     return oss.str();
     295    17830022 : }
     296             : 
     297             : 
     298             : template <typename T, typename T_BETWEEN>
     299       10477 : inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
     300       10477 :     std::vector<T> sorted(v);
     301       10477 :     std::sort(sorted.begin(), sorted.end());
     302       20954 :     return joinToString(sorted, between, accuracy);
     303       10477 : }
     304             : 
     305             : 
     306             : template <typename T, typename T_BETWEEN>
     307          18 : inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
     308             :     std::vector<std::string> ids;
     309          72 :     for (T* n : ns) {
     310         108 :         ids.push_back(Named::getIDSecure(n));
     311             :     }
     312          36 :     return joinToStringSorting(ids, between);
     313          18 : }
     314             : 
     315             : 
     316             : template <typename T, typename C, typename T_BETWEEN>
     317        5637 : inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
     318             :     std::vector<std::string> ids;
     319       24241 :     for (T* n : ns) {
     320       37208 :         ids.push_back(Named::getIDSecure(n));
     321             :     }
     322       11274 :     return joinToString(ids, between);
     323        5637 : }
     324             : 
     325             : 
     326             : template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
     327             : inline std::string joinNamedToString(const std::map<KEY, VAL, ComparatorIdLess>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
     328             :     std::ostringstream oss;
     329             :     bool connect = false;
     330             :     for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
     331             :         if (connect) {
     332             :             oss << toString(between, accuracy);
     333             :         } else {
     334             :             connect = true;
     335             :         }
     336             :         oss << Named::getIDSecure(it->first) << between_keyval << toString(it->second, accuracy);
     337             :     }
     338             :     return oss.str();
     339             : }
     340             : 
     341             : 
     342             : template <typename V>
     343        4621 : inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
     344             :     UNUSED_PARAMETER(accuracy);
     345             :     std::vector<std::string> ids;
     346        9484 :     for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
     347        4863 :         ids.push_back((*it)->getID());
     348             :     }
     349        9242 :     return joinToStringSorting(ids, " ");
     350        4621 : }
     351             : 
     352             : 
     353             : template <>
     354             : inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
     355         149 :     return joinToString(v, " ", accuracy);
     356             : }
     357             : 
     358             : 
     359             : template <>
     360             : inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
     361         677 :     return joinToString(v, " ", accuracy);
     362             : }
     363             : 
     364             : 
     365             : template <>
     366             : inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
     367      408574 :     return joinToString(v, " ", accuracy);
     368             : }
     369             : 
     370             : 
     371             : template <typename V, typename W>
     372             : inline std::string toString(const std::vector<std::pair<V, W> >& v, std::streamsize accuracy = gPrecision, const std::string& between = ";", const std::string& between2 = ",") {
     373             :     std::ostringstream oss;
     374             :     oss << std::setprecision(accuracy);
     375             :     bool connect = false;
     376             :     for (auto it : v) {
     377             :         if (connect) {
     378             :             oss << toString(between, accuracy);
     379             :         } else {
     380             :             connect = true;
     381             :         }
     382             :         oss << toString(it.first) << between2 << toString(it.second);
     383             :     }
     384             :     return oss.str();
     385             : }
     386             : 
     387             : 
     388             : template <typename T, typename T_BETWEEN>
     389       32848 : inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
     390       32848 :     std::ostringstream oss;
     391             :     bool connect = false;
     392       67378 :     for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
     393       34530 :         if (connect) {
     394        3388 :             oss << toString(between, accuracy);
     395             :         } else {
     396             :             connect = true;
     397             :         }
     398       69060 :         oss << toString(*it, accuracy);
     399             :     }
     400       32848 :     return oss.str();
     401       32848 : }
     402             : 
     403             : 
     404             : template <>
     405             : inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
     406      200605 :     return joinToString(v, " ");
     407             : }
     408             : 
     409             : 
     410             : template <>
     411             : inline std::string toString(const std::set<std::string>& v, std::streamsize) {
     412        2059 :     return joinToString(v, " ");
     413             : }
     414             : 
     415             : 
     416             : template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
     417          11 : inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
     418          11 :     std::ostringstream oss;
     419             :     bool connect = false;
     420          33 :     for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
     421          22 :         if (connect) {
     422          22 :             oss << toString(between, accuracy);
     423             :         } else {
     424             :             connect = true;
     425             :         }
     426          66 :         oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
     427             :     }
     428          11 :     return oss.str();
     429          11 : }
     430             : 
     431             : 
     432             : template <>
     433             : inline std::string toString(const Parameterised::Map& v, std::streamsize) {
     434             :     return joinToString(v, ", ", ":");
     435             : }
     436             : 
     437             : template <>
     438        2319 : inline std::string toString(const MMVersion& v, std::streamsize) {
     439             :     // we only need higher accuracy on the minor version for hotfix releases
     440        4638 :     return toString(v.first) + "." + toString(v.second, 0);
     441             : }

Generated by: LCOV version 1.14