CPN
Computational Process Networks
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
CPN::NodeLoader Class Reference

#include <NodeLoader.h>

+ Collaboration diagram for CPN::NodeLoader:

Public Member Functions

 NodeLoader ()
 
 ~NodeLoader ()
 
void LoadSharedLib (const std::string &libname)
 
void LoadSharedLib (const std::vector< std::string > &list)
 
void LoadNodeList (const std::string &filename)
 
void LoadNodeList (const std::vector< std::string > &list)
 
void SearchDirectory (const std::string &dirname)
 
void SearchDirectory (const std::vector< std::string > &dirnames)
 
NodeFactoryGetFactory (const std::string &nodename)
 
void RegisterFactory (shared_ptr< NodeFactory > factory)
 

Private Types

typedef std::map< std::string,
lt__handle * > 
LibMap
 
typedef std::map< std::string,
shared_ptr< NodeFactory > > 
FactoryMap
 
typedef std::map< std::string,
std::string > 
NodeLibMap
 

Private Member Functions

void InternalLoadLib (const std::string &lib)
 
void InternalLoad (const std::string &sym)
 

Private Attributes

LibMap libmap
 
FactoryMap factorymap
 
NodeLibMap nodelibmap
 

Detailed Description

This class encapsulates the necessary data and functions for loading up nodes.

Definition at line 45 of file NodeLoader.h.

Member Typedef Documentation

typedef std::map<std::string, shared_ptr<NodeFactory> > CPN::NodeLoader::FactoryMap
private

Definition at line 90 of file NodeLoader.h.

typedef std::map<std::string, lt__handle*> CPN::NodeLoader::LibMap
private

Definition at line 88 of file NodeLoader.h.

typedef std::map<std::string, std::string> CPN::NodeLoader::NodeLibMap
private

Definition at line 92 of file NodeLoader.h.

Constructor & Destructor Documentation

CPN::NodeLoader::NodeLoader ( )

Definition at line 40 of file NodeLoader.cc.

40  {
42  if (lt_dlinit() > 0) {
43  throw std::runtime_error(lt_dlerror());
44  }
45  }
static PthreadMutex lock
Definition: NodeLoader.cc:36
CPN::NodeLoader::~NodeLoader ( )

Definition at line 47 of file NodeLoader.cc.

References factorymap, and libmap.

47  {
49  factorymap.clear();
50  for (LibMap::iterator itr = libmap.begin(); itr != libmap.end(); ++itr) {
51  lt_dlclose(itr->second);
52  }
53  libmap.clear();
54  lt_dlexit();
55  }
FactoryMap factorymap
Definition: NodeLoader.h:91
static PthreadMutex lock
Definition: NodeLoader.cc:36

Member Function Documentation

NodeFactory * CPN::NodeLoader::GetFactory ( const std::string &  nodename)

Get the NodeFactory for the given node type

Parameters
nodenamethe node type
Returns
a node factory

Definition at line 99 of file NodeLoader.cc.

References ASSERT, factorymap, and InternalLoad().

99  {
101  FactoryMap::iterator entry = factorymap.find(nodetype);
102  if (entry == factorymap.end()) {
103  InternalLoad(nodetype);
104  entry = factorymap.find(nodetype);
105  ASSERT(entry != factorymap.end());
106  }
107  return entry->second.get();
108  }
FactoryMap factorymap
Definition: NodeLoader.h:91
void InternalLoad(const std::string &sym)
Definition: NodeLoader.cc:144
static PthreadMutex lock
Definition: NodeLoader.cc:36
#define ASSERT(exp,...)

+ Here is the call graph for this function:

void CPN::NodeLoader::InternalLoad ( const std::string &  sym)
private

Definition at line 144 of file NodeLoader.cc.

References CPN_DEFAULT_INIT_SYMBOL_STR, factorymap, InternalLoadLib(), and nodelibmap.

Referenced by GetFactory().

144  {
145  std::string sym = CPN_DEFAULT_INIT_SYMBOL_STR;
146  sym += name;
147  lt_dlhandle handle = 0;
148 
149  // Use a union to avoid having to cast
150  // from void pointer to function pointer explicitely.
151  // Doing so would violate strict aliasing.
152  union {
153  void *vptr;
154  CPNInitPrototype fn;
155  } init;
156 
157  handle = lt_dlopen(0);
158  if (!handle) {
159  throw std::runtime_error(lt_dlerror());
160  }
161  try {
162  lt_dlerror();
163  init.vptr = lt_dlsym(handle, sym.c_str());
164  const char *error = lt_dlerror();
165  if (error != 0) {
166  NodeLibMap::iterator itr = nodelibmap.find(sym);
167  if (itr == nodelibmap.end()) {
168  throw std::runtime_error("Unable to find node type " + name);
169  }
170  InternalLoadLib(itr->second);
171  lt_dlerror();
172  init.vptr = lt_dlsym(handle, sym.c_str());
173  error = lt_dlerror();
174  if (error != 0) {
175  throw std::runtime_error(error);
176  }
177  }
178  shared_ptr<NodeFactory> factory = init.fn();
179  factorymap.insert(std::make_pair(factory->GetName(), factory));
180  } catch (...) {
181  lt_dlclose(handle);
182  throw;
183  }
184  lt_dlclose(handle);
185  }
shared_ptr< NodeFactory >(* CPNInitPrototype)(void)
This is the prototype of the function that is called by the dynamic library loading facility...
Definition: NodeLoader.h:39
#define CPN_DEFAULT_INIT_SYMBOL_STR
Definition: NodeLoader.h:32
FactoryMap factorymap
Definition: NodeLoader.h:91
void InternalLoadLib(const std::string &lib)
Definition: NodeLoader.cc:116
NodeLibMap nodelibmap
Definition: NodeLoader.h:93

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::NodeLoader::InternalLoadLib ( const std::string &  lib)
private

Definition at line 116 of file NodeLoader.cc.

References libmap.

Referenced by InternalLoad(), and LoadSharedLib().

116  {
117  if (libmap.find(lib) == libmap.end()) {
118  struct advise_t {
119  advise_t() {
120  if (lt_dladvise_init(&advise) > 0) {
121  throw std::runtime_error(lt_dlerror());
122  }
123  }
124  ~advise_t() {
125  lt_dladvise_destroy(&advise);
126  }
127  void global() {
128  if (lt_dladvise_global(&advise) > 0) {
129  throw std::runtime_error(lt_dlerror());
130  }
131  }
132  lt_dladvise advise;
133  } advise;
134  advise.global();
135  lt_dlhandle handle = lt_dlopenadvise(lib.c_str(), advise.advise);
136  if (!handle) {
137  throw std::runtime_error(lt_dlerror());
138  } else {
139  libmap.insert(std::make_pair(lib, handle));
140  }
141  }
142  }

+ Here is the caller graph for this function:

void CPN::NodeLoader::LoadNodeList ( const std::string &  filename)

Loads up a node list. A node list contains information about what shared libraries need to be loaded to get a given node.

Parameters
filenamethe name of the nodelist file.

Definition at line 68 of file NodeLoader.cc.

References Loader(), and nodelibmap.

Referenced by CPN::Kernel::Kernel(), LoadNodeList(), and SearchDirectory().

68  {
69  Loader(filename, nodelibmap);
70  }
void Loader(const std::string &ldfile, std::map< std::string, std::string > &data)
Definition: NodeLoader.cc:314
NodeLibMap nodelibmap
Definition: NodeLoader.h:93

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::NodeLoader::LoadNodeList ( const std::vector< std::string > &  list)

Definition at line 72 of file NodeLoader.cc.

References LoadNodeList().

72  {
73  for (std::vector<std::string>::const_iterator i = list.begin(), e = list.end(); i != e; ++i) {
74  LoadNodeList(*i);
75  }
76  }
void LoadNodeList(const std::string &filename)
Definition: NodeLoader.cc:68

+ Here is the call graph for this function:

void CPN::NodeLoader::LoadSharedLib ( const std::string &  libname)

Loads a shared library for node init function searching.

Parameters
libnamethe shared library file name

Definition at line 57 of file NodeLoader.cc.

References InternalLoadLib().

Referenced by CPN::Kernel::Kernel(), and LoadSharedLib().

57  {
59  InternalLoadLib(libname);
60  }
void InternalLoadLib(const std::string &lib)
Definition: NodeLoader.cc:116
static PthreadMutex lock
Definition: NodeLoader.cc:36

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::NodeLoader::LoadSharedLib ( const std::vector< std::string > &  list)

Definition at line 62 of file NodeLoader.cc.

References LoadSharedLib().

62  {
63  for (std::vector<std::string>::const_iterator i = list.begin(), e = list.end(); i != e; ++i) {
64  LoadSharedLib(*i);
65  }
66  }
void LoadSharedLib(const std::string &libname)
Definition: NodeLoader.cc:57

+ Here is the call graph for this function:

void CPN::NodeLoader::RegisterFactory ( shared_ptr< NodeFactory factory)

Manually register a node factory with the NodeLoader.

Parameters
factorythe factory

Definition at line 110 of file NodeLoader.cc.

References factorymap.

110  {
112  std::string name = factory->GetName();
113  factorymap.insert(std::make_pair(name, factory));
114  }
FactoryMap factorymap
Definition: NodeLoader.h:91
static PthreadMutex lock
Definition: NodeLoader.cc:36
void CPN::NodeLoader::SearchDirectory ( const std::string &  dirname)

Search a directory for .nodelist files to add to the node lists

Parameters
dirnamethe directory to search

Definition at line 78 of file NodeLoader.cc.

References Directory::End(), and LoadNodeList().

Referenced by CPN::Kernel::Kernel(), and SearchDirectory().

78  {
79  std::string ext = ".nodelist";
80  for (Directory dir(dirname); !dir.End(); dir.Next()) {
81  if (dir.IsRegularFile()) {
82  std::string fname = dir.BaseName();
83  if (fname[0] == '.') { continue; }
84  if (fname.size() < ext.size()
85  || fname.substr(fname.size() - ext.size(), ext.size()) != ext) {
86  continue;
87  }
88  LoadNodeList(dir.FullName());
89  }
90  }
91  }
bool End() const
Definition: Directory.h:48
void LoadNodeList(const std::string &filename)
Definition: NodeLoader.cc:68

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::NodeLoader::SearchDirectory ( const std::vector< std::string > &  dirnames)

Definition at line 93 of file NodeLoader.cc.

References SearchDirectory().

93  {
94  for (std::vector<std::string>::const_iterator i = dirnames.begin(), e = dirnames.end(); i != e; ++i) {
95  SearchDirectory(*i);
96  }
97  }
void SearchDirectory(const std::string &dirname)
Definition: NodeLoader.cc:78

+ Here is the call graph for this function:

Member Data Documentation

FactoryMap CPN::NodeLoader::factorymap
private

Definition at line 91 of file NodeLoader.h.

Referenced by GetFactory(), InternalLoad(), RegisterFactory(), and ~NodeLoader().

LibMap CPN::NodeLoader::libmap
private

Definition at line 89 of file NodeLoader.h.

Referenced by InternalLoadLib(), and ~NodeLoader().

NodeLibMap CPN::NodeLoader::nodelibmap
private

Definition at line 93 of file NodeLoader.h.

Referenced by InternalLoad(), and LoadNodeList().


The documentation for this class was generated from the following files: