All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DriverSet.h
Go to the documentation of this file.
1 /*
2  * DriverSet.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 DRIVERSET_H_
23 #define DRIVERSET_H_
24 
25 #include <HSEP/DriverLib.h>
26 #include <HSEP/Array.h>
27 
28 #define TYPENAME_LEN 256
29 
30 namespace HSEP {
31 
35  class BaseDriverSet {
36  string aFactoryName;
43  virtual void _addLibrary(const char* pTypeName,string& pFilePath, string& pFactoryName) = 0;
44  protected:
45  virtual void unload() = 0;
46  public:
47 
52  BaseDriverSet(string& pFactoryNameRef) {
53  aFactoryName = string(pFactoryNameRef); // take care with stolen references
54  } // constructor
55 
60  BaseDriverSet(const char* pFactoryNamePtr) {
61  aFactoryName = string(pFactoryNamePtr);
62  } // constructor
63 
64 
65  virtual ~BaseDriverSet() {
66  } // destructor
67 
79  bool load(string& pPathRef,string& pPrefixRef);
80 
81  }; // BaseDriverSet class
82 
89  template <class Type>
90  class DriverSet : public BaseDriverSet {
91  public:
95  typedef function<void ( string& pKey, DriverLibType* pValue)> ForEachWorker;
97  protected:
99 
100  void _addLibrary(const char* pTypeNamePtr,string& pFilePathRef,string& pFactoryNameRef) {
101 
102  DriverLibType* vLibraryPtr = new DriverLibType(pFilePathRef,pFactoryNameRef);
103  if (vLibraryPtr->valid()) {
104  aDriverLibCollection.put(pTypeNamePtr,vLibraryPtr);
105  } // if (vLibraryPtr->isValid())
106 
107  } // _addLibrary
108 
109  public:
120  DriverSet(string& pFactoryNameRef) : BaseDriverSet(pFactoryNameRef) {
121  }
122 
133  DriverSet(const char* pFactoryNamePtr) : BaseDriverSet(pFactoryNamePtr) {
134  }
135 
136  virtual ~DriverSet() {
137  //
138  // This is not necessary because the system
139  // releases all the resources automatcially
140  //
141  // unload();
142  }
143 
151  void unload() {
153  } // unload
154 
162  bool has(
163  string& pSetNameRef,
164  string& pItemNameRef
165  ) {
166  bool vResult = false;
167 
168  if (aDriverLibCollection.has(pSetNameRef)) {
169 
170  DriverLibType* vDriverLibPtr = aDriverLibCollection.ptr(pSetNameRef);
171 
172  if (nullptr != vDriverLibPtr) {
173  vResult = vDriverLibPtr->has(pItemNameRef);
174  }
175  }
176  return vResult;
177 
178  } // has
179 
187  Type* ptr(string& pSetNameRef,string& pItemNameRef) {
188 
189  Type* vItemPtr = nullptr;
190  DriverLibType* vDriverLibPtr;
191 
192  vDriverLibPtr = aDriverLibCollection.ptr(pSetNameRef);
193 
194  if (nullptr != vDriverLibPtr) {
195  vItemPtr = vDriverLibPtr->getItemPtr(pItemNameRef); // could return nullptr;
196  }
197  return vItemPtr;
198 
199  } // ptr
200 
208  Type* ptr(const char* pSetNamePtr, const char* pItemNamePtr) {
209  string vSetName(pSetNamePtr);
210  string vItemName(pItemNamePtr);
211  return ptr(vSetName,vItemName);
212  }
213 
214  void forEach(ForEachWorker pWorker) {
215  aDriverLibCollection.forEach(pWorker);
216  } // forEach
217 
222  void info(StringArray& pItems) {
223  aDriverLibCollection.forEach([&](string& xKey, DriverLibType* xDriverLib) {
224  pItems.push("Library ["+xKey+"] : "+xDriverLib->infoRef());
225  });
226  } // info
227 
228  }; // DriverSet
229 
230 } // HSEP namespace
231 
232 #endif /* DRIVERSET_H_ */