BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
IssueFactory.cxx
Go to the documentation of this file.
1/*
2 * IssueFactory.cxx
3 * ers
4 *
5 * Created by Matthias Wiesmann on 30.11.04.
6 * Copyright 2004 CERN. All rights reserved.
7 *
8 */
9
10#include "ers/IssueFactory.h"
11#include "ers/DefaultIssue.h"
12#include "ers/InvalidReferenceIssue.h"
13#include "ers/Issue.h"
14#include "ers/IssueFactoryIssue.h"
15#include "ers/Precondition.h"
16#include <cstdlib>
17#include <iostream>
18
20
22
23/** Finds the singleton instance of the factory.
24 * If the instance is not allocated yet, it is.
25 * \return a pointer to the singleton instance
26 */
27
29 if ( 0 == s_factory ) { s_factory = new IssueFactory(); } // creation needed
30 return s_factory;
31} // instance
32
35 std::cerr << *factory;
36} // print_registered_issues
37
38/** Register an issue type with the factory
39 * \param name the name that will be used to lookup new instances
40 * \param creator a pointer to the function used to create new instance for that particular
41 * type of function \return \c true if the name was already present in the table
42 */
43
44bool ers::IssueFactory::register_issue( const std::string& name,
45 CreateIssueCallback creator ) {
46 return m_factory_map.insert( CallbackMap::value_type( name, creator ) ).second;
47} // register_Issue
48
49/** Builds an issue out of the name it was registered with
50 * \param name the name used to indentify the class
51 * \return an newly allocated instance of type \c name or DefaultIssue
52 * \note If the requested type cannot be resolved an instance of type DefaultIssue
53 */
54
55ers::Issue* ers::IssueFactory::build( const std::string& name ) const {
56 CallbackMap::const_iterator i = m_factory_map.find( name );
57 if ( i == m_factory_map.end() )
58 {
59 // throw ERS_ISSUE_FACTORY_ERROR(name,"issue not registred") ;
60 return new DefaultIssue( name );
61 } // Not found
62 ers::Issue* issue = ( i->second )();
63 if ( 0 == issue )
64 { throw ERS_ISSUE_FACTORY_ERROR( name, "factory function returned null" ); } // issue null
65 return issue;
66} // build
67
68/** Builds an issue out of a name and a value table
69 * @param name the name used to indentify the class
70 * @param values the value-table containing the state for the Issue
71 * @return an newly allocated instance of this type or null if the name is not registered
72 */
73
74ers::Issue* ers::IssueFactory::build( const std::string& name,
75 const ers::string_map_type* values ) const {
76 Issue* i = build( name );
77 if ( i ) { i->set_values( *values ); } // if i
78 return i;
79} // build
80
81/** Clones an Issue.
82 * This method creates (using the other build methods) an new Issue with the same value table
83 * @param original the Issue to be cloned
84 * @return a new cloned Issued
85 * @note The problem of cause exception is not handled yet.
86 */
87
89 ERS_PRE_CHECK_PTR( original );
90 const std::string name = original->get_class_name();
91 const ers::string_map_type* values = original->get_value_table();
92 ERS_ASSERT( values != 0, "null value table for original" );
93 ers::Issue* clone_issue = build( name, values );
94 return clone_issue;
95} // build
96
97void ers::IssueFactory::write_to( std::ostream& stream ) const {
98 stream << "Issue factory - registered issues\n";
99 stream << "---------------------------------\n";
100 int i = 0;
101 for ( CallbackMap::const_iterator pos = m_factory_map.begin(); pos != m_factory_map.end();
102 ++pos )
103 {
104 std::string name = pos->first;
105 stream << i << ")\t" << name << std::endl;
106 i++;
107 } // for
108 stream << "---------------------------------\n";
109} //
110
111std::ostream& ers::operator<<( std::ostream& stream, const IssueFactory& factory ) {
112 factory.write_to( stream );
113 return stream;
114} // operator
#define ERS_ASSERT(expr,...)
#define ERS_ISSUE_FACTORY_ERROR(name, message)
bool register_issue(const std::string &name, CreateIssueCallback creator)
register an issue factory
void write_to(std::ostream &stream) const
writes description to stream
static void print_registered()
prints all registered issue types
static IssueFactory * instance()
method to access singleton
Issue * build(const std::string &name) const
build an empty issue out of a name
Root Issue class.
void set_values(const string_map_type &values)
sets the value table
const string_map_type * get_value_table() const
extract value table
virtual const char * get_class_name() const
Get key for class (used for serialisation).
efhlt::Interface * factory(void)
Definition factory.cxx:15
std::ostream & operator<<(std::ostream &, const Issue &)
std::map< std::string, std::string > string_map_type