CPN
Computational Process Networks
NodeBase.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
25 #ifndef CPN_NODEBASE_H
26 #define CPN_NODEBASE_H
27 #pragma once
28 #include <cpn/common.h>
29 #include <cpn/NodeAttr.h>
30 #include <cpn/NodeFactory.h>
31 #include <cpn/bits/PseudoNode.h>
32 #include <cpn/utils/ParseBool.h>
33 #include <sstream>
34 #include <map>
35 #include <string>
36 #include <stdexcept>
37 
38 class Pthread;
39 
40 namespace CPN {
41 
42  namespace NodeBasePrivate {
43  template<typename T>
44  struct CoerceParam {
45  static T Coerce(const std::string& key, const std::string &param) {
46  std::istringstream iss(param);
47  T ret;
48  if (iss >> ret) {
49  return ret;
50  }
51  throw std::invalid_argument("Unable to convert parameter \"" + key + "\": \"" + param + "\"");
52  }
53  };
54  template<>
55  struct CoerceParam<bool> {
56  static bool Coerce(const std::string &, const std::string &param) {
57  return ParseBool(param);
58  }
59  };
60  }
61 
69  class CPN_API NodeBase : public PseudoNode {
70  public:
71  NodeBase(Kernel &ker, const NodeAttr &attr);
72 
73  virtual ~NodeBase();
74 
78  const std::string &GetTypeName() const { return type; }
79 
82  Kernel *GetKernel() { return &kernel; }
83 
84  std::string GetParam(const std::string &key) const;
85 
86  bool HasParam(const std::string &key) const;
87 
88  template<typename T>
89  T GetParam(const std::string &key) const {
90  return NodeBasePrivate::CoerceParam<T>::Coerce(key, GetParam(key));
91  }
92 
93  template<typename T>
94  T GetParam(const std::string &key, T def) const {
95  if (HasParam(key)) {
96  return GetParam<T>(key);
97  }
98  return def;
99  }
100 
104  void Start();
105 
106  void Shutdown();
107 
108  bool IsPurePseudo();
109 
111  void LogState();
112  protected:
113 
115  virtual void Process() = 0;
116 
118  private:
119  void* EntryPoint();
120 
121  const std::string type;
122  auto_ptr<Pthread> thread;
123  std::map<std::string, std::string> params;
124  };
125 
126 }
127 
128 #define CPN_DECLARE_NODE_FACTORY(type, klass) class type ## Factory : public CPN::NodeFactory {\
129  public: type ## Factory() : CPN::NodeFactory(#type) {}\
130  CPN::shared_ptr<CPN::NodeBase> Create(CPN::Kernel &ker, const CPN::NodeAttr &attr) { return CPN::shared_ptr<CPN::NodeBase>(new klass(ker, attr)); }};\
131 extern "C" CPN::shared_ptr<CPN::NodeFactory> cpninit ## type (void);\
132 CPN::shared_ptr<CPN::NodeFactory> cpninit ## type (void) { return CPN::shared_ptr<CPN::NodeFactory>(new type ## Factory); }
133 
134 #define CPN_DECLARE_NODE_AND_FACTORY(type,klass) class klass : public CPN::NodeBase { \
135  public : klass(CPN::Kernel &kern, const CPN::NodeAttr& attr) : CPN::NodeBase(kern,attr) {} \
136  private: void Process(); }; \
137 CPN_DECLARE_NODE_FACTORY(type,klass)
138 
139 #endif
Kernel * GetKernel()
Return a pointer to the kernel that his node is running under.
Definition: NodeBase.h:82
Attributes for a node.
Definition: NodeAttr.h:58
bool ParseBool(const std::string &str)
Parses string for a boolean value.
Definition: ParseBool.cc:28
The definition common to all nodes in the process network.
Definition: NodeBase.h:69
T GetParam(const std::string &key) const
Definition: NodeBase.h:89
Definition for node attributes.
const std::string & GetTypeName() const
Definition: NodeBase.h:78
const std::string type
Definition: NodeBase.h:121
static bool Coerce(const std::string &, const std::string &param)
Definition: NodeBase.h:56
T GetParam(const std::string &key, T def) const
Definition: NodeBase.h:94
std::map< std::string, std::string > params
Definition: NodeBase.h:123
#define CPN_API
Definition: common.h:36
static T Coerce(const std::string &key, const std::string &param)
Definition: NodeBase.h:45
auto_ptr< Pthread > thread
Definition: NodeBase.h:122
The Kernel declaration.
Definition: Kernel.h:55
Kernel & kernel
Definition: NodeBase.h:117
Definition of the NodeFactory.