TPIE

v1.1rc1-6-g0c97303
factory_base.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2011, 2012, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef __TPIE_PIPELINING_FACTORY_BASE_H__
21 #define __TPIE_PIPELINING_FACTORY_BASE_H__
22 
23 // XXX remove when init_segment is removed
24 #include <tpie/backtrace.h>
25 
26 namespace tpie {
27 
28 namespace pipelining {
29 
31 public:
32  virtual void init_node(node & r) = 0;
33  virtual ~factory_init_hook() {
34  }
35 };
36 
37 class factory_base {
38 public:
39  factory_base() : m_amount(0), m_set(false) {
40  }
41 
42  inline void memory(double amount) {
43  m_amount = amount;
44  m_set = true;
45  }
46 
47  inline double memory() const {
48  return m_amount;
49  }
50 
57  m_hooks.push_back(hook);
58  }
59 
63  void copy_hooks_to(factory_base & other) const {
64  for (size_t i = 0; i < m_hooks.size(); ++i) {
65  other.m_hooks.push_back(m_hooks[i]);
66  }
67  }
68 
69  inline void init_segment(node & r) const {
70  log_fatal() << "init_segment has been renamed to init_node" << std::endl;
72  init_node(r);
73  }
74 
75  inline void init_node(node & r) const {
76  if (m_set) r.set_memory_fraction(memory());
77  if (!m_name.empty()) {
78  r.set_name(m_name, m_namePriority);
79  }
80  if (!m_breadcrumbs.empty()) {
81  r.set_breadcrumb(m_breadcrumbs);
82  }
83  for (size_t i = 0; i < m_hooks.size(); ++i) {
84  m_hooks[i]->init_node(r);
85  }
86  }
87 
88  void init_sub_node(node & r) const {
89  if (m_set) r.set_memory_fraction(memory());
90  if (m_breadcrumbs.empty()) {
91  if (m_name.empty()) {
92  // no op
93  } else {
94  r.set_breadcrumb(m_name);
95  }
96  } else {
97  if (m_name.empty()) {
98  r.set_breadcrumb(m_breadcrumbs);
99  } else {
100  r.set_breadcrumb(m_breadcrumbs + " | " + m_name);
101  }
102  }
103  for (size_t i = 0; i < m_hooks.size(); ++i) {
104  m_hooks[i]->init_node(r);
105  }
106  }
107 
108  inline void name(const std::string & n, priority_type p) {
109  m_name = n;
110  m_namePriority = p;
111  }
112 
113  inline void push_breadcrumb(const std::string & n) {
114  if (m_breadcrumbs.empty()) m_breadcrumbs = n;
115  else m_breadcrumbs = n + " | " + m_breadcrumbs;
116  }
117 
118 private:
119  double m_amount;
120  bool m_set;
121  std::string m_name;
122  std::string m_breadcrumbs;
123  priority_type m_namePriority;
124  std::vector<factory_init_hook *> m_hooks;
125 };
126 
127 } // namespace pipelining
128 
129 } // namespace tpie
130 
131 #endif // __TPIE_PIPELINING_FACTORY_BASE_H__
void hook_initialization(factory_init_hook *hook)
Add a node initialization hook.
Definition: factory_base.h:56
Linux TPIE debugging helpers.
void copy_hooks_to(factory_base &other) const
Copy the hooks that have been added to this factory to another.
Definition: factory_base.h:63
Base class of all nodes.
Definition: node.h:58
logstream & log_fatal()
Return logstream for writing fatal log messages.
Definition: tpie_log.h:99
void backtrace(std::ostream &out, int depth=1024)
Output a function call backtrace for debugging.