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/StreamFactory.hpp>
27 : #include <ers/Severity.hpp>
28 : #include <ers/ers.hpp>
29 : #include <ers/internal/Util.hpp>
30 : #include <ers/internal/NullStream.hpp>
31 : #include <ers/internal/PluginManager.hpp>
32 : #include <ers/internal/macro.hpp>
33 : #include <ers/internal/SingletonCreator.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 14178 : ers::StreamFactory::instance()
41 : {
42 : /**Singleton instance
43 : */
44 14178 : static ers::StreamFactory * instance = ers::SingletonCreator<ers::StreamFactory>::create();
45 :
46 14178 : 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 79 : ers::StreamFactory::create_out_stream( const std::string & format ) const
59 : {
60 79 : std::string key = format;
61 79 : std::string param;
62 79 : std::string::size_type start = format.find( '(' );
63 79 : if ( start != std::string::npos )
64 : {
65 0 : key = format.substr( 0, start );
66 0 : std::string::size_type end = format.find( ')', start );
67 0 : if ( end != std::string::npos )
68 0 : param = format.substr( start + 1, end - start - 1 );
69 : }
70 :
71 79 : OutFunctionMap::const_iterator it = m_out_factories.find( key );
72 :
73 79 : if( it != m_out_factories.end() )
74 : {
75 79 : try
76 : {
77 79 : return it->second( param );
78 : }
79 0 : catch( ers::Issue & issue )
80 : {
81 0 : ERS_INTERNAL_ERROR( issue )
82 0 : }
83 : }
84 : else
85 : {
86 0 : ERS_INTERNAL_ERROR( "Creator for the \"" << key << "\" stream is not found" )
87 : }
88 :
89 : return 0;
90 79 : }
91 :
92 : /** Builds a stream from a textual key
93 : * The key should have the format \c stream_name[(stream_parameters)]
94 : * For some streams parameters can be ommitted.
95 : * For instance to write to the error stream, the key is:
96 : * \c stderr
97 : * \param param parameter to be passed to the new stream constructor
98 : * \note the stream is allocated on the heap, it is the caller's responsibility to delete it.
99 : */
100 : ers::InputStream *
101 0 : ers::StreamFactory::create_in_stream( const std::string & stream,
102 : const std::string & param ) const
103 : {
104 0 : return create_in_stream( stream, { param } );
105 0 : }
106 :
107 : /** Builds a stream from a textual key
108 : * The key should have the format \c stream_name[(stream_parameters)]
109 : * For some streams parameters can be ommitted.
110 : * For instance to write to the error stream, the key is:
111 : * \c stderr
112 : * \param params parameters to be passed to the new stream constructor
113 : * \note the stream is allocated on the heap, it is the caller's responsibility to delete it.
114 : */
115 : ers::InputStream *
116 0 : ers::StreamFactory::create_in_stream(
117 : const std::string & stream,
118 : const std::initializer_list<std::string> & params ) const
119 : {
120 0 : InFunctionMap::const_iterator it = m_in_factories.find( stream );
121 :
122 0 : if( it != m_in_factories.end() )
123 : {
124 0 : try
125 : {
126 0 : return it->second( params );
127 : }
128 0 : catch( ers::Issue & issue )
129 : {
130 0 : throw ers::InvalidFormat( ERS_HERE, stream, issue );
131 0 : }
132 : }
133 :
134 0 : throw ers::InvalidFormat( ERS_HERE, stream );
135 : }
136 :
137 : /** Registers a factory function with the stream factory.
138 : * The callback is function that takes two parameters <ol>
139 : * <li>a string describing the protocol, for instance \c file </li>
140 : * <li>a string describing a uri, can be a path, a suffix or anything</li>
141 : * </ol>
142 : * The function should return a heap allocated stream, or null if it does not
143 : * understand the protocol.
144 : * \param name name of the stream type (human display only).
145 : * \param callback the creator function
146 : */
147 : void
148 0 : ers::StreamFactory::register_in_stream( const std::string & name, InputStreamCreator callback )
149 : {
150 0 : m_in_factories[name] = callback;
151 0 : }
152 :
153 : /** Registers a factory function with the stream factory.
154 : * The callback is function that takes two parameters <ol>
155 : * <li>a string describing the protocol, for instance \c file </li>
156 : * <li>a string describing a uri, can be a path, a suffix or anything</li>
157 : * </ol>
158 : * The function should return a heap allocated stream, or null if it does not
159 : * understand the protocol.
160 : * \param name name of the stream type (human display only).
161 : * \param callback the creator function
162 : */
163 : void
164 14099 : ers::StreamFactory::register_out_stream( const std::string & name, OutputStreamCreator callback )
165 : {
166 14099 : m_out_factories[name] = callback;
167 14099 : }
168 :
169 : std::ostream &
170 0 : ers::operator<<( std::ostream & out, const ers::StreamFactory & sf )
171 : {
172 0 : StreamFactory::OutFunctionMap::const_iterator oit = sf.m_out_factories.begin();
173 0 : for( ; oit != sf.m_out_factories.end(); ++oit )
174 : {
175 0 : out << oit->first << "\t\"" << oit->second << "\"" << std::endl;
176 : }
177 :
178 0 : StreamFactory::InFunctionMap::const_iterator iit = sf.m_in_factories.begin();
179 0 : for( ; iit != sf.m_in_factories.end(); ++iit )
180 : {
181 0 : out << iit->first << "\t\"" << iit->second << "\"" << std::endl;
182 : }
183 0 : return out;
184 : }
|