identifiable_factory.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2013 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/Util/units/identifiable_factory.h
10
11#ifndef __identifiable_factory_h
12#define __identifiable_factory_h
13
30
31template<typename T, typename C>
32T* identifiable_factory (C& ptrs, std::string name)
33{
34
35#ifdef _DEBUG
36 std::cerr << "identifiable_factory name=" << name << std::endl;
37#endif
38
39 std::string message;
40
41 if (name == "help")
42 message += "\n\n" "Options:" "\n\n";
43
44#ifdef _DEBUG
45 std::cerr << "identifiable_factory ptrs=" << (void*) &ptrs << std::endl;
46#endif
47
48 for (auto ptr=ptrs.begin(); ptr != ptrs.end(); ptr++)
49 {
50#ifdef _DEBUG
51 std::cerr << "ptr=" << (void*) (*ptr) << " id=" << (*ptr)->get_identity() << std::endl;
52#endif
53
54 if (name == "help")
55 {
56 message += (*ptr)->get_identity() +"\t"+ (*ptr)->get_description() +"\n";
57 }
58 else if ((*ptr)->identify(name))
59 {
60#ifdef _DEBUG
61 std::cerr << "identifiable_factory calling clone" << std::endl;
62#endif
63 return (*ptr)->clone ();
64 }
65 }
66
67 if (name == "help")
68 throw Error (HelpMessage, "identifiable_factory", message);
69
70 throw Error (InvalidState, "identifiable_factory",
71 "no instance named '" + name + "'");
72}
73
74#endif
75
A convenient exception handling class.
Definition Error.h:54

Generated using doxygen 1.14.0