@@ -19,6 +19,7 @@ Interface version: 1.0.0
19
19
#include " calculator_types.hpp"
20
20
#include " calculator_dynamic.h"
21
21
22
+
22
23
#ifdef _WIN32
23
24
#include < windows.h>
24
25
#else // _WIN32
@@ -150,6 +151,14 @@ using CCalculatorInputVector = CInputVector<T>;
150
151
class CWrapper {
151
152
public:
152
153
154
+ CWrapper (void * pSymbolLookupMethod)
155
+ {
156
+ CheckError (nullptr , initWrapperTable (&m_WrapperTable));
157
+ CheckError (nullptr , loadWrapperTableFromSymbolLookupMethod (&m_WrapperTable, pSymbolLookupMethod));
158
+
159
+ CheckError (nullptr , checkBinaryVersion ());
160
+ }
161
+
153
162
CWrapper (const std::string &sFileName )
154
163
{
155
164
CheckError (nullptr , initWrapperTable (&m_WrapperTable));
@@ -163,6 +172,11 @@ class CWrapper {
163
172
return std::make_shared<CWrapper>(sFileName );
164
173
}
165
174
175
+ static PWrapper loadLibraryFromSymbolLookupMethod (void * pSymbolLookupMethod)
176
+ {
177
+ return std::make_shared<CWrapper>(pSymbolLookupMethod);
178
+ }
179
+
166
180
~CWrapper ()
167
181
{
168
182
releaseWrapperTable (&m_WrapperTable);
@@ -173,6 +187,7 @@ class CWrapper {
173
187
inline void GetVersion (Calculator_uint32 & nMajor, Calculator_uint32 & nMinor, Calculator_uint32 & nMicro);
174
188
inline bool GetLastError (CBase * pInstance, std::string & sErrorMessage );
175
189
inline void ReleaseInstance (CBase * pInstance);
190
+ inline void AcquireInstance (CBase * pInstance);
176
191
inline PVariable CreateVariable (const Calculator_double dInitialValue);
177
192
inline PCalculator CreateCalculator ();
178
193
@@ -191,6 +206,7 @@ class CWrapper {
191
206
CalculatorResult initWrapperTable (sCalculatorDynamicWrapperTable * pWrapperTable);
192
207
CalculatorResult releaseWrapperTable (sCalculatorDynamicWrapperTable * pWrapperTable);
193
208
CalculatorResult loadWrapperTable (sCalculatorDynamicWrapperTable * pWrapperTable, const char * pLibraryFileName);
209
+ CalculatorResult loadWrapperTableFromSymbolLookupMethod (sCalculatorDynamicWrapperTable * pWrapperTable, void * pSymbolLookupMethod);
194
210
195
211
friend class CBase ;
196
212
friend class CVariable ;
@@ -217,7 +233,7 @@ class CBase {
217
233
if (m_pWrapper != nullptr )
218
234
m_pWrapper->CheckError (this , nResult);
219
235
}
220
-
236
+ public:
221
237
/* *
222
238
* CBase::CBase - Constructor for Base class.
223
239
*/
@@ -236,7 +252,6 @@ class CBase {
236
252
m_pWrapper = nullptr ;
237
253
}
238
254
239
- public:
240
255
/* *
241
256
* CBase::GetHandle - Returns handle to instance.
242
257
*/
@@ -317,11 +332,12 @@ class CCalculator : public CBase {
317
332
std::vector<char > bufferErrorMessage (bytesNeededErrorMessage);
318
333
CheckError (nullptr ,m_WrapperTable.m_GetLastError (hInstance, bytesNeededErrorMessage, &bytesWrittenErrorMessage, &bufferErrorMessage[0 ], &resultHasError));
319
334
sErrorMessage = std::string (&bufferErrorMessage[0 ]);
335
+
320
336
return resultHasError;
321
337
}
322
338
323
339
/* *
324
- * CWrapper::ReleaseInstance - Releases the memory of an Instance
340
+ * CWrapper::ReleaseInstance - Releases shared ownership of an Instance
325
341
* @param[in] pInstance - Instance Handle
326
342
*/
327
343
inline void CWrapper::ReleaseInstance (CBase * pInstance)
@@ -333,6 +349,19 @@ class CCalculator : public CBase {
333
349
CheckError (nullptr ,m_WrapperTable.m_ReleaseInstance (hInstance));
334
350
}
335
351
352
+ /* *
353
+ * CWrapper::AcquireInstance - Acquires shared ownership of an Instance
354
+ * @param[in] pInstance - Instance Handle
355
+ */
356
+ inline void CWrapper::AcquireInstance (CBase * pInstance)
357
+ {
358
+ CalculatorHandle hInstance = nullptr ;
359
+ if (pInstance != nullptr ) {
360
+ hInstance = pInstance->GetHandle ();
361
+ };
362
+ CheckError (nullptr ,m_WrapperTable.m_AcquireInstance (hInstance));
363
+ }
364
+
336
365
/* *
337
366
* CWrapper::CreateVariable - Creates a new Variable instance
338
367
* @param[in] dInitialValue - Initial value of the new Variable
@@ -342,6 +371,10 @@ class CCalculator : public CBase {
342
371
{
343
372
CalculatorHandle hInstance = nullptr ;
344
373
CheckError (nullptr ,m_WrapperTable.m_CreateVariable (dInitialValue, &hInstance));
374
+
375
+ if (!hInstance) {
376
+ CheckError (nullptr ,CALCULATOR_ERROR_INVALIDPARAM);
377
+ }
345
378
return std::make_shared<CVariable>(this , hInstance);
346
379
}
347
380
@@ -353,6 +386,10 @@ class CCalculator : public CBase {
353
386
{
354
387
CalculatorHandle hInstance = nullptr ;
355
388
CheckError (nullptr ,m_WrapperTable.m_CreateCalculator (&hInstance));
389
+
390
+ if (!hInstance) {
391
+ CheckError (nullptr ,CALCULATOR_ERROR_INVALIDPARAM);
392
+ }
356
393
return std::make_shared<CCalculator>(this , hInstance);
357
394
}
358
395
@@ -384,6 +421,7 @@ class CCalculator : public CBase {
384
421
pWrapperTable->m_GetVersion = nullptr ;
385
422
pWrapperTable->m_GetLastError = nullptr ;
386
423
pWrapperTable->m_ReleaseInstance = nullptr ;
424
+ pWrapperTable->m_AcquireInstance = nullptr ;
387
425
pWrapperTable->m_CreateVariable = nullptr ;
388
426
pWrapperTable->m_CreateCalculator = nullptr ;
389
427
@@ -524,6 +562,15 @@ class CCalculator : public CBase {
524
562
if (pWrapperTable->m_ReleaseInstance == nullptr )
525
563
return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
526
564
565
+ #ifdef _WIN32
566
+ pWrapperTable->m_AcquireInstance = (PCalculatorAcquireInstancePtr) GetProcAddress (hLibrary, " calculator_acquireinstance" );
567
+ #else // _WIN32
568
+ pWrapperTable->m_AcquireInstance = (PCalculatorAcquireInstancePtr) dlsym (hLibrary, " calculator_acquireinstance" );
569
+ dlerror ();
570
+ #endif // _WIN32
571
+ if (pWrapperTable->m_AcquireInstance == nullptr )
572
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
573
+
527
574
#ifdef _WIN32
528
575
pWrapperTable->m_CreateVariable = (PCalculatorCreateVariablePtr) GetProcAddress (hLibrary, " calculator_createvariable" );
529
576
#else // _WIN32
@@ -545,6 +592,74 @@ class CCalculator : public CBase {
545
592
pWrapperTable->m_LibraryHandle = hLibrary;
546
593
return CALCULATOR_SUCCESS;
547
594
}
595
+
596
+ inline CalculatorResult CWrapper::loadWrapperTableFromSymbolLookupMethod (sCalculatorDynamicWrapperTable * pWrapperTable, void * pSymbolLookupMethod)
597
+ {
598
+ if (pWrapperTable == nullptr )
599
+ return CALCULATOR_ERROR_INVALIDPARAM;
600
+ if (pSymbolLookupMethod == nullptr )
601
+ return CALCULATOR_ERROR_INVALIDPARAM;
602
+
603
+ typedef CalculatorResult (*SymbolLookupType)(const char *, void **);
604
+
605
+ SymbolLookupType pLookup = (SymbolLookupType)pSymbolLookupMethod;
606
+
607
+ CalculatorResult eLookupError = CALCULATOR_SUCCESS;
608
+ eLookupError = (*pLookup)(" calculator_variable_getvalue" , (void **)&(pWrapperTable->m_Variable_GetValue ));
609
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_Variable_GetValue == nullptr ) )
610
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
611
+
612
+ eLookupError = (*pLookup)(" calculator_variable_setvalue" , (void **)&(pWrapperTable->m_Variable_SetValue ));
613
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_Variable_SetValue == nullptr ) )
614
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
615
+
616
+ eLookupError = (*pLookup)(" calculator_calculator_enlistvariable" , (void **)&(pWrapperTable->m_Calculator_EnlistVariable ));
617
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_Calculator_EnlistVariable == nullptr ) )
618
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
619
+
620
+ eLookupError = (*pLookup)(" calculator_calculator_getenlistedvariable" , (void **)&(pWrapperTable->m_Calculator_GetEnlistedVariable ));
621
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_Calculator_GetEnlistedVariable == nullptr ) )
622
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
623
+
624
+ eLookupError = (*pLookup)(" calculator_calculator_clearvariables" , (void **)&(pWrapperTable->m_Calculator_ClearVariables ));
625
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_Calculator_ClearVariables == nullptr ) )
626
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
627
+
628
+ eLookupError = (*pLookup)(" calculator_calculator_multiply" , (void **)&(pWrapperTable->m_Calculator_Multiply ));
629
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_Calculator_Multiply == nullptr ) )
630
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
631
+
632
+ eLookupError = (*pLookup)(" calculator_calculator_add" , (void **)&(pWrapperTable->m_Calculator_Add ));
633
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_Calculator_Add == nullptr ) )
634
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
635
+
636
+ eLookupError = (*pLookup)(" calculator_getversion" , (void **)&(pWrapperTable->m_GetVersion ));
637
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_GetVersion == nullptr ) )
638
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
639
+
640
+ eLookupError = (*pLookup)(" calculator_getlasterror" , (void **)&(pWrapperTable->m_GetLastError ));
641
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_GetLastError == nullptr ) )
642
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
643
+
644
+ eLookupError = (*pLookup)(" calculator_releaseinstance" , (void **)&(pWrapperTable->m_ReleaseInstance ));
645
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_ReleaseInstance == nullptr ) )
646
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
647
+
648
+ eLookupError = (*pLookup)(" calculator_acquireinstance" , (void **)&(pWrapperTable->m_AcquireInstance ));
649
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_AcquireInstance == nullptr ) )
650
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
651
+
652
+ eLookupError = (*pLookup)(" calculator_createvariable" , (void **)&(pWrapperTable->m_CreateVariable ));
653
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_CreateVariable == nullptr ) )
654
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
655
+
656
+ eLookupError = (*pLookup)(" calculator_createcalculator" , (void **)&(pWrapperTable->m_CreateCalculator ));
657
+ if ( (eLookupError != 0 ) || (pWrapperTable->m_CreateCalculator == nullptr ) )
658
+ return CALCULATOR_ERROR_COULDNOTFINDLIBRARYEXPORT;
659
+
660
+ return CALCULATOR_SUCCESS;
661
+ }
662
+
548
663
549
664
550
665
/* *
@@ -563,6 +678,7 @@ class CCalculator : public CBase {
563
678
{
564
679
Calculator_double resultValue = 0 ;
565
680
CheckError (m_pWrapper->m_WrapperTable .m_Variable_GetValue (m_pHandle, &resultValue));
681
+
566
682
return resultValue;
567
683
}
568
684
@@ -601,6 +717,10 @@ class CCalculator : public CBase {
601
717
{
602
718
CalculatorHandle hVariable = nullptr ;
603
719
CheckError (m_pWrapper->m_WrapperTable .m_Calculator_GetEnlistedVariable (m_pHandle, nIndex, &hVariable));
720
+
721
+ if (!hVariable) {
722
+ CheckError (CALCULATOR_ERROR_INVALIDPARAM);
723
+ }
604
724
return std::make_shared<CVariable>(m_pWrapper, hVariable);
605
725
}
606
726
@@ -620,6 +740,10 @@ class CCalculator : public CBase {
620
740
{
621
741
CalculatorHandle hInstance = nullptr ;
622
742
CheckError (m_pWrapper->m_WrapperTable .m_Calculator_Multiply (m_pHandle, &hInstance));
743
+
744
+ if (!hInstance) {
745
+ CheckError (CALCULATOR_ERROR_INVALIDPARAM);
746
+ }
623
747
return std::make_shared<CVariable>(m_pWrapper, hInstance);
624
748
}
625
749
@@ -631,6 +755,10 @@ class CCalculator : public CBase {
631
755
{
632
756
CalculatorHandle hInstance = nullptr ;
633
757
CheckError (m_pWrapper->m_WrapperTable .m_Calculator_Add (m_pHandle, &hInstance));
758
+
759
+ if (!hInstance) {
760
+ CheckError (CALCULATOR_ERROR_INVALIDPARAM);
761
+ }
634
762
return std::make_shared<CVariable>(m_pWrapper, hInstance);
635
763
}
636
764
0 commit comments