Interface Documentation
Version: 2.-1 (devel)
factory.hh
Go to the documentation of this file.
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
18 #include <iostream>
19 #include <map>
20 
21 namespace flecsi {
22 namespace utils {
23 
28 template<typename RETURN, typename KEY, typename... Args>
29 class Factory_
30 {
31 public:
33  using createHandler = RETURN * (*)(Args... args);
34 
36  using key_t = KEY;
37 
39  using map_t = std::map<key_t, createHandler>;
40 
42  Factory_(const Factory_ & f) = delete;
43 
45  Factory_ & operator=(const Factory_ &) = delete;
46 
51  static Factory_ & instance() {
52  static Factory_ f;
53  return f;
54  } // instance
55 
64  bool registerType(const key_t key, const createHandler ch) {
65  return map_.insert(typename map_t::value_type(key, ch)).second;
66  } // registerType
67 
70 
73 
94  RETURN * create(const key_t key, Args &&... args) {
95  // lookup the create class
96  typename map_t::const_iterator ita = map_.find(key);
97 
98  // make sure that the id exists in the map
99  if(ita == map_.end()) {
100  std::cerr << "Error Unknown Type ID" << std::endl;
101  std::exit(1);
102  } // if
103 
104  // call the constructor method
105  return (ita->second(std::forward<Args>(args)...));
106  } // create
107 
108 private:
109  map_t map_;
110 
112  Factory_() {}
114  ~Factory_() {}
115 
116 }; // class Factory_
117 
118 } // namespace utils
119 } // namespace flecsi
RETURN * create(const key_t key, Args &&... args)
Definition: factory.hh:94
RETURN *(*)(Args... args) createHandler
Function pointer type for creation method.
Definition: factory.hh:33
KEY key_t
Map key type.
Definition: factory.hh:36
static Factory_ & instance()
Definition: factory.hh:51
bool registerType(const key_t key, const createHandler ch)
Definition: factory.hh:64
Factory_ & operator=(const Factory_ &)=delete
Assignment operator (hidden)
Factory_ provides a generic object factory class.
Definition: factory.hh:29
std::map< key_t, createHandler > map_t
Map type to associate ids and creation methods.
Definition: factory.hh:39
Definition: default_node.hh:20