test_namedfactory.cxx
Go to the documentation of this file.
2 #include "WireCellUtil/Testing.h"
3 #include "WireCellUtil/Logging.h"
5 
6 
7 using namespace WireCell;
8 
9 class ISomeComponent : public virtual WireCell::IComponent<ISomeComponent> {
11 public:
12  ISomeComponent() : l(Log::logger("SomeComponent")) {
13  l->debug("SomeComponent() at {:p}", (void*)this);
14  }
15  virtual ~ISomeComponent() {
16  l->debug("~SomeComponent()");
17  }
18  virtual void chirp() = 0;
19 };
20 
21 class SomeConcrete : public virtual ISomeComponent {
23 public:
24  SomeConcrete() : l(Log::logger("SomeConcrete")) {
25  l->debug("SomeConcrete() at {:p}", (void*)this);
26  }
27  virtual ~SomeConcrete() {
28  l->debug("~SomeConcrete()");
29  }
30  virtual void chirp() {
31  l->info("SomeConcrete::chirp() at {:p}", (void*)this);
32  }
33 };
34 
36 
37 using spdlog::warn;
38 using spdlog::info;
39 using spdlog::debug;
40 
41 int main(int argc, char* argv[])
42 {
43  Testing::log(argv[0]);
44  // for this test we cheat since the factory isn't in a shared library.
45  make_SomeConcrete_factory();
46 
47  auto ins = WireCell::Factory::lookup<ISomeComponent>("SomeConcrete");
48  AssertMsg(ins, "Failed to lookup 'SomeConcrete' with interface 'ISomeComponent'");
49  info("Got SomeConcrete @ {:p}", (void*)ins.get());
50  info("Got bogus @ {:p}", (void*)std::shared_ptr<ISomeComponent>().get());
51  ins->chirp();
52 
53  bool caught = false;
54  try {
55  auto should_fail = WireCell::Factory::lookup<ISomeComponent>("NothingNamedThis");
56  }
57  catch (WireCell::FactoryException& e) {
58  warn(errstr(e));
59  info("^^^ Successfully failed to lookup a nonexistent component");
60  caught=true;
61  }
62  AssertMsg(caught, "Failed to throw");
63  info("Should not hear anything from this failed find_tn");
64  std::shared_ptr<ISomeComponent> ptr
65  = WireCell::Factory::find_maybe_tn<ISomeComponent>("SomeConcrete:doesnotexist");
66  AssertMsg(ptr==nullptr, "Got non null for nonexistent named component");
67  info("Success. Next, 'find' should be successful");
68  std::shared_ptr<ISomeComponent> ptr2
69  = WireCell::Factory::find_maybe_tn<ISomeComponent>("SomeConcrete");
70  debug("Got ptr2 @ {:p}", (void*)ptr2.get());
71  AssertMsg(ptr2 != nullptr, "Got null for existing component");
72 // AssertMsg(false, "Got null for existing component");
73 }
74 
void info(const char *fmt, const Args &...args)
Definition: spdlog.h:189
void log(const char *argv0)
Definition: Testing.cxx:28
virtual ~ISomeComponent()
#define errstr(e)
Definition: Exceptions.h:26
Log::logptr_t l
const double e
void warn(const char *fmt, const Args &...args)
Definition: spdlog.h:195
logptr_t logger(std::string name)
Definition: Logging.cxx:71
std::shared_ptr< spdlog::logger > logptr_t
Definition: Logging.h:24
Definition: Main.h:22
const void * ptr(const T *p)
Definition: format.h:3138
int main(int argc, char *argv[])
#define AssertMsg
Definition: Testing.h:8
void debug(const char *fmt, const Args &...args)
Definition: spdlog.h:183
virtual ~SomeConcrete()
virtual void chirp()
#define WIRECELL_FACTORY(NAME, CONCRETE,...)
Definition: NamedFactory.h:332