Line data Source code
1 : /*
2 : * DUNE DAQ modification notice:
3 : * This file has been modified from the original ATLAS ers source for the DUNE DAQ project.
4 : * Fork baseline commit: 8267df82a4f6fe6bf02c4014923eba19eddc4614 (2020-04-14).
5 : * Renamed since fork: yes (from src/StreamFactory.cxx to src/StreamFactory.cpp).
6 : *
7 : * Original copyright:
8 : * Copyright (C) 2001-2020 CERN for the benefit of the ATLAS collaboration.
9 : * Licensed under the Apache License, Version 2.0.
10 : */
11 :
12 : /*
13 : * StreamFactory.cxx
14 : * ERS
15 : *
16 : * Created by Serguei Kolos on 21.01.05.
17 : * Modified by Serguei Kolos on 21.11.05.
18 : * Copyright 2005 CERN. All rights reserved.
19 : *
20 : */
21 :
22 : #include <iostream>
23 :
24 : #include <ers/Issue.hpp>
25 : #include <ers/OutputStream.hpp>
26 : #include <ers/Severity.hpp>
27 : #include <ers/StreamFactory.hpp>
28 : #include <ers/ers.hpp>
29 : #include <ers/internal/NullStream.hpp>
30 : #include <ers/internal/PluginManager.hpp>
31 : #include <ers/internal/SingletonCreator.hpp>
32 : #include <ers/internal/Util.hpp>
33 : #include <ers/internal/macro.hpp>
34 :
35 : /** This method returns the singleton instance.
36 : * It should be used for every operation on the factory.
37 : * \return a reference to the singleton instance
38 : */
39 : ers::StreamFactory&
40 15028 : ers::StreamFactory::instance()
41 : {
42 : /**Singleton instance
43 : */
44 15028 : static ers::StreamFactory* instance = ers::SingletonCreator<ers::StreamFactory>::create();
45 :
46 15028 : return *instance;
47 : } // instance
48 :
49 : /** Builds a stream from a textual key
50 : * The key should have the format \c stream_name[(stream_parameters)]
51 : * For some streams parameters can be ommitted.
52 : * For instance to write to the error stream, the key is:
53 : * \c stderr
54 : * \param format the format, which describes new stream
55 : * \note the stream is allocated on the heap, it is the caller's responsibility to delete it.
56 : */
57 : ers::OutputStream*
58 82 : ers::StreamFactory::create_out_stream(const std::string& format) const
59 : {
60 82 : std::string key = format;
61 82 : std::string param;
62 82 : std::string::size_type start = format.find('(');
63 82 : if (start != std::string::npos) {
64 0 : key = format.substr(0, start);
65 0 : std::string::size_type end = format.find(')', start);
66 0 : if (end != std::string::npos)
67 0 : param = format.substr(start + 1, end - start - 1);
68 : }
69 :
70 82 : OutFunctionMap::const_iterator it = m_out_factories.find(key);
71 :
72 82 : if (it != m_out_factories.end()) {
73 82 : try {
74 82 : return it->second(param);
75 0 : } catch (ers::Issue& issue) {
76 0 : ERS_INTERNAL_ERROR(issue)
77 0 : }
78 : } else {
79 0 : ERS_INTERNAL_ERROR("Creator for the \"" << key << "\" stream is not found")
80 : }
81 :
82 : return 0;
83 82 : }
84 :
85 : /** Builds a stream from a textual key
86 : * The key should have the format \c stream_name[(stream_parameters)]
87 : * For some streams parameters can be ommitted.
88 : * For instance to write to the error stream, the key is:
89 : * \c stderr
90 : * \param param parameter to be passed to the new stream constructor
91 : * \note the stream is allocated on the heap, it is the caller's responsibility to delete it.
92 : */
93 : ers::InputStream*
94 0 : ers::StreamFactory::create_in_stream(const std::string& stream, const std::string& param) const
95 : {
96 0 : return create_in_stream(stream, { param });
97 0 : }
98 :
99 : /** Builds a stream from a textual key
100 : * The key should have the format \c stream_name[(stream_parameters)]
101 : * For some streams parameters can be ommitted.
102 : * For instance to write to the error stream, the key is:
103 : * \c stderr
104 : * \param params parameters to be passed to the new stream constructor
105 : * \note the stream is allocated on the heap, it is the caller's responsibility to delete it.
106 : */
107 : ers::InputStream*
108 0 : ers::StreamFactory::create_in_stream(const std::string& stream, const std::initializer_list<std::string>& params) const
109 : {
110 0 : InFunctionMap::const_iterator it = m_in_factories.find(stream);
111 :
112 0 : if (it != m_in_factories.end()) {
113 0 : try {
114 0 : return it->second(params);
115 0 : } catch (ers::Issue& issue) {
116 0 : throw ers::InvalidFormat(ERS_HERE, stream, issue);
117 0 : }
118 : }
119 :
120 0 : throw ers::InvalidFormat(ERS_HERE, stream);
121 : }
122 :
123 : /** Registers a factory function with the stream factory.
124 : * The callback is function that takes two parameters <ol>
125 : * <li>a string describing the protocol, for instance \c file </li>
126 : * <li>a string describing a uri, can be a path, a suffix or anything</li>
127 : * </ol>
128 : * The function should return a heap allocated stream, or null if it does not
129 : * understand the protocol.
130 : * \param name name of the stream type (human display only).
131 : * \param callback the creator function
132 : */
133 : void
134 0 : ers::StreamFactory::register_in_stream(const std::string& name, InputStreamCreator callback)
135 : {
136 0 : m_in_factories[name] = callback;
137 0 : }
138 :
139 : /** Registers a factory function with the stream factory.
140 : * The callback is function that takes two parameters <ol>
141 : * <li>a string describing the protocol, for instance \c file </li>
142 : * <li>a string describing a uri, can be a path, a suffix or anything</li>
143 : * </ol>
144 : * The function should return a heap allocated stream, or null if it does not
145 : * understand the protocol.
146 : * \param name name of the stream type (human display only).
147 : * \param callback the creator function
148 : */
149 : void
150 14946 : ers::StreamFactory::register_out_stream(const std::string& name, OutputStreamCreator callback)
151 : {
152 14946 : m_out_factories[name] = callback;
153 14946 : }
154 :
155 : std::ostream&
156 0 : ers::operator<<(std::ostream& out, const ers::StreamFactory& sf)
157 : {
158 0 : StreamFactory::OutFunctionMap::const_iterator oit = sf.m_out_factories.begin();
159 0 : for (; oit != sf.m_out_factories.end(); ++oit) {
160 0 : out << oit->first << "\t\"" << oit->second << "\"" << std::endl;
161 : }
162 :
163 0 : StreamFactory::InFunctionMap::const_iterator iit = sf.m_in_factories.begin();
164 0 : for (; iit != sf.m_in_factories.end(); ++iit) {
165 0 : out << iit->first << "\t\"" << iit->second << "\"" << std::endl;
166 : }
167 0 : return out;
168 : }
|