All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DriverLib.h
Go to the documentation of this file.
1 /*
2  * Driver.h
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef DRIVERLIB_H_
23 #define DRIVERLIB_H_
24 
25 #include <HSEP/Library.h>
26 #include <HSEP/Dictionary.h>
27 
28 using namespace std;
29 using namespace HSEP;
30 
31 namespace HSEP {
32 
39  template<class Type>
40  class DriverLib : public Library {
41  string aFactoryFunctionName;
42  string aDriverInfo;
43  public:
45  typedef TypeDictionary* (*createTypeCatalog )();
46  typedef void (*destroyTypeCatalog)(TypeDictionary*);
47  typedef void (*driverInfo)(string&);
49  typedef function<void (string& pKey, Type* pValue)> ForEachWorker;
51  private:
52  TypeDictionary* aCatalogPtr;
53  public:
76  DriverLib(string& pPathRef,string& pFactoryFunctionNameRef) : Library("DriverLib",pPathRef) {
77 
78  aFactoryFunctionName = string(pFactoryFunctionNameRef); // take care with stolen references
79 
80  if (valid()) {
81  createTypeCatalog vCreateCatalogPtr;
82  string vCreateCatalogName("create"+aFactoryFunctionName);
83  vCreateCatalogPtr = (createTypeCatalog)getMethodPtr(vCreateCatalogName);
84 
85  if (nullptr) {
86  invalidate();
87  }
88  else {
89  aCatalogPtr = vCreateCatalogPtr();
90 
91  driverInfo vDriverInfoPtr;
92  string vDriverInfoName("driverInfo");
93 
94  vDriverInfoPtr = (driverInfo)getMethodPtr(vDriverInfoName);
95 
96  if (nullptr == vDriverInfoPtr) {
97  invalidate();
98  }
99  else {
100  vDriverInfoPtr(aDriverInfo);
101 
102  }
103 
104  }
105  }
106  } // constructor
107 
113 
114  if (valid()) {
115 
116  //aCatalogPtr->clear();
117 
118  destroyTypeCatalog vDestroyCatalogPtr;
119  string vDestroyCatalogName("destroy"+aFactoryFunctionName);
120  vDestroyCatalogPtr = (destroyTypeCatalog)getMethodPtr(vDestroyCatalogName);
121 
122  if (nullptr != vDestroyCatalogPtr) {
123  vDestroyCatalogPtr(aCatalogPtr);
124  }
125 
126  // vDestroyCatalogPtr instead of ... delete aCatalogPtr;
127  //
128  // This offers more flexibility to the library by itself, giving the opportunity
129  // to initialize and de-initialize additional resources.
130  //
131 
132  }
133 
134  } // destructor
135 
141 
142  TypeDictionary* vResultPtr = nullptr;
143  if (valid()) {
144  vResultPtr = aCatalogPtr;
145  }
146  return vResultPtr;
147  } // getCatalog
148 
153  string& infoRef() {
154  return aDriverInfo;
155  }
156 
162  Type* getItemPtr(string& pItemNameRef) {
163 
164  Type* vResultPtr = nullptr;
165  if (valid()) {
166  vResultPtr = aCatalogPtr->ptr(pItemNameRef);
167  }
168  return vResultPtr;
169 
170  } // getItem
171 
176  void forEach(ForEachWorker pWorker) {
177  if (valid()) {
178  aCatalogPtr->forEach(pWorker);
179  }
180  } // forEach
181 
188  bool has(string& pItemNameRef) {
189  return aCatalogPtr->has(pItemNameRef);
190  } // has
191 
192  }; // DriverLib class
193 
194 } // HSEP namespace
195 
196 #endif /* DRIVERLIB_H_ */