/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2012-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file OutputFormatter.h14/// @author Daniel Krajzewicz15/// @author Michael Behrisch16/// @date 201217///18// Abstract base class for output formatters19/****************************************************************************/20#pragma once21#include <config.h>2223#include <string>24#include <vector>25#include <utils/xml/SUMOXMLDefinitions.h>262728// ===========================================================================29// class declarations30// ===========================================================================31class Boundary;32class Position;33class PositionVector;34class RGBColor;353637// ===========================================================================38// class definitions39// ===========================================================================40enum class OutputFormatterType {41XML,42#ifdef HAVE_PARQUET43PARQUET,44#endif45CSV46};4748/**49* @class OutputFormatter50* @brief Abstract base class for output formatters51*52* OutputFormatter format XML like output into the output stream.53* There are only two implementation at the moment, "normal" XML54* and binary XML.55*/56class OutputFormatter {57public:58/// @brief Constructor59OutputFormatter(OutputFormatterType t) : myType(t) { }6061/// @brief Destructor62virtual ~OutputFormatter() { }6364/** @brief Writes an XML header with optional configuration65*66* If something has been written (myXMLStack is not empty), nothing67* is written and false returned.68* The default implementation does nothing and returns false.69*70* @param[in] into The output stream to use71* @param[in] rootElement The root element to use72* @param[in] attrs Additional attributes to save within the rootElement73* @param[in] includeConfig whether the current config should be included as XML comment74* @return whether something has been written75*/76virtual bool writeXMLHeader(std::ostream& into, const std::string& rootElement,77const std::map<SumoXMLAttr, std::string>& attrs, bool writeMetadata,78bool includeConfig) {79UNUSED_PARAMETER(into);80UNUSED_PARAMETER(rootElement);81UNUSED_PARAMETER(attrs);82UNUSED_PARAMETER(writeMetadata);83UNUSED_PARAMETER(includeConfig);84return false;85}8687/** @brief Opens an XML tag88*89* An indentation, depending on the current xml-element-stack size, is written followed90* by the given xml element ("<" + xmlElement)91* The xml element is added to the stack, then.92*93* @param[in] into The output stream to use94* @param[in] xmlElement Name of element to open95* @return The OutputDevice for further processing96*/97virtual void openTag(std::ostream& into, const std::string& xmlElement) = 0;9899/** @brief Opens an XML tag100*101* Helper method which finds the correct string before calling openTag.102*103* @param[in] into The output stream to use104* @param[in] xmlElement Id of the element to open105*/106virtual void openTag(std::ostream& into, const SumoXMLTag& xmlElement) = 0;107108virtual void writeTime(std::ostream& into, const SumoXMLAttr attr, const SUMOTime val) = 0;109110/** @brief Closes the most recently opened tag and optinally add a comment111*112* @param[in] into The output stream to use113* @return Whether a further element existed in the stack and could be closed114* @todo it is not verified that the topmost element was closed115*/116virtual bool closeTag(std::ostream& into, const std::string& comment = "") = 0;117118/** @brief Writes a preformatted tag to the device but ensures that any119* pending tags are closed.120* This method is only implemented for XML output.121* @param[in] into The output stream to use122* @param[in] val The preformatted data123*/124virtual void writePreformattedTag(std::ostream& into, const std::string& val) {125UNUSED_PARAMETER(into);126UNUSED_PARAMETER(val);127throw ProcessError("The selected file format does not support preformatted tags.");128}129130/** @brief Writes some whitespace to format the output.131* This method is only implemented for XML output.132* @param[in] into The output stream to use133* @param[in] val The whitespace134*/135virtual void writePadding(std::ostream& into, const std::string& val) {136UNUSED_PARAMETER(into);137UNUSED_PARAMETER(val);138}139140/** @brief Returns whether a header has been written.141* Useful to detect whether a file is being used by multiple sources.142* @return Whether a header has been written143*/144virtual bool wroteHeader() const = 0;145146/** @brief Returns the type of formatter being used.147* @return the formatter type148*/149OutputFormatterType getType() {150return myType;151}152153/** @brief Set the expected attributes to write.154* This is used for tracking which attributes are expected in table like outputs.155* This should be not necessary but at least in the initial phase of implementing CSV and Parquet156* it helps a lot to track errors.157* @param[in] expected which attributes are to be written (at the deepest XML level)158* @param[in] depth the maximum XML hierarchy depth (excluding the root)159*/160virtual void setExpectedAttributes(const SumoXMLAttrMask& expected, const int depth = 2) {161UNUSED_PARAMETER(expected);162UNUSED_PARAMETER(depth);163}164165private:166/// @brief the type of formatter being used (XML, CSV, Parquet, etc.)167const OutputFormatterType myType;168};169170171