SmartTester  2.0.0
Smart Tester is unit testing and regression testing framework used for testing SmartCGMS system.
EntityUtils.h
1 //
2 // Author: markovd@students.zcu.cz
3 //
4 
5 #ifndef SMARTTESTER_ENTITYUTILS_H
6 #define SMARTTESTER_ENTITYUTILS_H
7 
8 namespace tester {
9  template<typename T, typename... Args>
10  HRESULT constructEntity(CDynamic_Library& library, const std::string& symbolName, Args... args) {
11  if (!library.Is_Loaded()) {
12  Logger::getInstance().error(L"Library is not loaded! Cannot construct entity.");
13  return E_FAIL;
14  }
15 
16  Logger::getInstance().debug(L"Creating entity with " + Widen_String(symbolName) + L" factory.");
17 
18  auto entityFactory = library.Resolve<T>(symbolName.c_str());
19  if (!entityFactory) {
20  Logger::getInstance().error(L"Error while loading " + Widen_String(symbolName) + L" factory!");
21  return E_FAIL;
22  }
23 
24  HRESULT creationResult = entityFactory(args...);
25 
26  if (!Succeeded(creationResult)) {
27  return E_FAIL;
28  }
29 
30  return S_OK;
31  }
32 
33  template<typename T, typename D>
34  HRESULT getEntityDescriptors(CDynamic_Library& library, const std::string& symbolName, D **begin, D **end) {
35  if (!library.Is_Loaded()) {
36  Logger::getInstance().error(L"Library is not loaded! Cannot get entity descriptors.");
37  return E_FAIL;
38  }
39 
40  auto descriptorsCreator = library.Resolve<T>(symbolName.c_str());
41  if (!descriptorsCreator) {
42  Logger::getInstance().error(L"Error while loading " + Widen_String(symbolName) + L" descriptors factory!");
43  return E_FAIL;
44  }
45 
46  return descriptorsCreator(begin, end);
47  }
48 
49  template<typename T, typename D>
50  D* getEntityDescriptor(CDynamic_Library& library, const GUID& guid, const std::string& symbolName) {
51  D *begin, *end;
52 
53  HRESULT result = getEntityDescriptors<T>(library, symbolName, &begin, &end);
54  if (!Succeeded(result)) {
55  Logger::getInstance().error(L"Error while acquiring entity descriptors!");
56  return nullptr;
57  }
58 
59  while (begin != end) {
60  if (begin->id == guid) {
61  return begin;
62  }
63  begin++;
64  }
65 
66  return nullptr;
67  }
68 }
69 
70 #endif //SMARTTESTER_ENTITYUTILS_H