AnimatLab  2
Test
StdClassFactory.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 
9 
10 namespace StdUtils
11 {
12 
20 {
21 }
22 
30 {}
31 
32 #ifdef WIN32
33 
43 IStdClassFactory *IStdClassFactory::LoadModule(std::string strModuleName, bool bThrowError)
44 {
45  TRACE_DEBUG("Loading Module: " + strModuleName);
46 
47  if(Std_IsBlank(strModuleName))
48  THROW_ERROR(Std_Err_lModuleNameIsBlank, Std_Err_strModuleNameIsBlank);
49 
50  HMODULE hMod = NULL;
51 
52  hMod = LoadLibrary(strModuleName.c_str());
53 
54  if(!hMod)
55  {
56  if(bThrowError)
57  {
58  int iError = GetLastError();
59  THROW_PARAM_ERROR(Std_Err_lModuleNotLoaded, Std_Err_strModuleNotLoaded, "Module", strModuleName + ", Last Error: " + STR(iError));
60  return NULL;
61  }
62  else
63  return NULL;
64  }
65 
66  GetClassFactory lpFactoryFunc = NULL;
67 
68  TRACE_DEBUG(" Gettting the classfactory pointer.");
69 
70  lpFactoryFunc = (GetClassFactory) GetProcAddress(hMod, "GetStdClassFactory");
71 
72  if(!lpFactoryFunc)
73  {
74  if(bThrowError)
75  THROW_PARAM_ERROR(Std_Err_lModuleProcNotLoaded, Std_Err_strModuleProcNotLoaded, "Module", strModuleName);
76  else
77  return NULL;
78  }
79 
80  TRACE_DEBUG("Finished Loading Module: " + strModuleName);
81  return lpFactoryFunc();
82 }
83 
84 #else
85 
95 IStdClassFactory *IStdClassFactory::LoadModule(std::string strModuleName, bool bThrowError)
96 {
97  TRACE_DEBUG("Loading Module: " + strModuleName);
98 
99  if(Std_IsBlank(strModuleName))
100  THROW_ERROR(Std_Err_lModuleNameIsBlank, Std_Err_strModuleNameIsBlank);
101 
102  //If the module name already has .so in it then do not modfiy it.
103  std::string strModRenamed = strModuleName;
104  if(Std_ToLower(strModuleName).find(".so") == -1)
105  {
106  strModRenamed = "lib" + Std_Replace(strModuleName, ".dll", ".so");
107  strModRenamed = Std_Replace(strModRenamed, "VC10", "vc10");
108  }
109 
110  void *hMod = NULL;
111 
112  hMod = dlopen(strModRenamed.c_str(), RTLD_LAZY);
113 
114  if(!hMod)
115  {
116  if(bThrowError)
117  {
118  std::string strError = strModRenamed + ", Last Error: " + dlerror();
119  THROW_PARAM_ERROR(Std_Err_lModuleNotLoaded, Std_Err_strModuleNotLoaded, "Module", strError);
120  }
121  else
122  return NULL;
123  }
124 
125  GetClassFactory lpFactoryFunc = NULL;
126 
127  TRACE_DEBUG(" Gettting the classfactory pointer.");
128 
129  lpFactoryFunc = (GetClassFactory) dlsym(hMod, "GetStdClassFactory");
130 
131  if(!lpFactoryFunc || dlerror() != NULL)
132  {
133  if(bThrowError)
134  THROW_PARAM_ERROR(Std_Err_lModuleProcNotLoaded, Std_Err_strModuleProcNotLoaded, "Module", strModRenamed + ", Last Error: " + dlerror());
135  else
136  return NULL;
137  }
138 
139  TRACE_DEBUG("Finished Loading Module: " + strModRenamed);
140  IStdClassFactory *lpFact = lpFactoryFunc();
141 
142  TRACE_DEBUG("Returning class factory: " + strModRenamed);
143  return lpFact;
144 }
145 
146 #endif
147 
148 
149 } //StdUtils
150 
151 
152 
153 
154 
Standard class factory.
virtual ~IStdClassFactory()
Destructor.
Namespace for the standard utility objects.
Definition: MarkupSTL.cpp:19
std::string Std_ToLower(std::string strVal)
Converts a string to lower case.
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
IStdClassFactory()
Default constructor.
std::string Std_Replace(std::string strVal, std::string strFind, std::string strReplace)
Replaces a substring with another string.
static IStdClassFactory * LoadModule(std::string strModuleName, bool bThrowError=true)
Loads a DLL module by name and attempts to call the GetStdClassFactory method to get a pointer to the...