BOSS 7.0.5
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 <iostream>
11#include <cstdlib>
12#include "ers/IssueFactory.h"
13#include "ers/Issue.h"
14#include "ers/DefaultIssue.h"
15#include "ers/Precondition.h"
16#include "ers/IssueFactoryIssue.h"
17#include "ers/InvalidReferenceIssue.h"
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) {
30 s_factory = new IssueFactory();
31 } // creation needed
32 return s_factory ;
33} // instance
34
36 IssueFactory *factory = instance() ;
37 std::cerr << *factory;
38} //print_registered_issues
39
40
41/** Register an issue type with the factory
42 * \param name the name that will be used to lookup new instances
43 * \param creator a pointer to the function used to create new instance for that particular type of function
44 * \return \c true if the name was already present in the table
45 */
46
47bool ers::IssueFactory::register_issue(const std::string &name, CreateIssueCallback creator) {
48 return m_factory_map.insert(CallbackMap::value_type(name,creator)).second;
49} // register_Issue
50
51/** Builds an issue out of the name it was registered with
52 * \param name the name used to indentify the class
53 * \return an newly allocated instance of type \c name or DefaultIssue
54 * \note If the requested type cannot be resolved an instance of type DefaultIssue
55 */
56
57ers::Issue *ers::IssueFactory::build(const std::string &name) const {
58 CallbackMap::const_iterator i = m_factory_map.find(name);
59 if (i == m_factory_map.end()) {
60 // throw ERS_ISSUE_FACTORY_ERROR(name,"issue not registred") ;
61 return new DefaultIssue(name);
62 } // Not found
63 ers::Issue *issue = (i->second)() ;
64 if (0==issue) {
65 throw ERS_ISSUE_FACTORY_ERROR(name,"factory function returned null");
66 } // issue null
67 return issue ;
68} // build
69
70/** Builds an issue out of a name and a value table
71 * @param name the name used to indentify the class
72 * @param values the value-table containing the state for the Issue
73 * @return an newly allocated instance of this type or null if the name is not registered
74 */
75
76ers::Issue *ers::IssueFactory::build(const std::string &name, const ers::string_map_type *values) const {
77 Issue *i = build(name);
78 if (i) {
79 i->set_values(*values);
80 } // if i
81 return i ;
82} // build
83
84/** Clones an Issue.
85 * This method creates (using the other build methods) an new Issue with the same value table
86 * @param original the Issue to be cloned
87 * @return a new cloned Issued
88 * @note The problem of cause exception is not handled yet.
89 */
90
92 ERS_PRE_CHECK_PTR(original);
93 const std::string name = original->get_class_name() ;
94 const ers::string_map_type *values = original->get_value_table();
95 ERS_ASSERT(values!=0,"null value table for original");
96 ers::Issue *clone_issue = build(name,values);
97 return clone_issue ;
98}// build
99
100void ers::IssueFactory::write_to(std::ostream& stream) const {
101 stream << "Issue factory - registered issues\n" ;
102 stream << "---------------------------------\n" ;
103 int i = 0 ;
104 for(CallbackMap::const_iterator pos=m_factory_map.begin();pos!=m_factory_map.end();++pos) {
105 std::string name = pos->first;
106 stream << i << ")\t" << name << std::endl;
107 i++ ;
108 } // for
109 stream << "---------------------------------\n" ;
110} //
111
112std::ostream& ers::operator<<(std::ostream& stream, const IssueFactory& factory) {
113 factory.write_to(stream);
114 return stream ;
115} // operator
116
117
118
efhlt::Interface * factory(void)
Definition: factory.cxx:17
#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
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)
std::ostream & operator<<(std::ostream &, const Issue &)
std::map< std::string, std::string > string_map_type