Skip to content

Commit eacf7d4

Browse files
authored
Merge pull request #638 from UWB-Biocomputing/issue-270-handle-nullptr-from-factory
[ISSUE 270] Handle Nullptr From Factory
2 parents df94fc5 + 7775ca3 commit eacf7d4

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Simulator/Core/Model.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
* @file Model.cpp
33
*
44
* @ingroup Simulator/Core
5-
*
5+
*
66
* @brief Implementation of Model for the graph-based networks.
77
*
88
* The network is composed of 3 superimposed 2-d arrays: vertices, edges, and
99
* summation points.
1010
*
1111
* Edges in the edge map are located at the coordinates of the vertex
1212
* from which they receive output. Each edge stores a pointer into a
13-
* summation point.
13+
* summation point.
1414
*/
1515

1616
#include "Model.h"
@@ -23,21 +23,42 @@
2323
/// Constructor
2424
Model::Model()
2525
{
26+
// Get a copy of the console logger to use in the case of errors
27+
log4cplus::Logger consoleLogger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("console"));
28+
2629
// Reference variable used to get class type from ParameterManager.
2730
string type;
2831

2932
// Create Layout class using type definition from configuration file.
3033
ParameterManager::getInstance().getStringByXpath("//LayoutParams/@class", type);
3134
layout_ = Factory<Layout>::getInstance().createType(type);
3235

36+
// If the factory returns an error (nullptr), exit
37+
if (layout_ == nullptr) {
38+
LOG4CPLUS_INFO(consoleLogger, "INVALID CLASS: " + type);
39+
exit(EXIT_FAILURE);
40+
}
41+
3342
// Create Connections class using type definition from configuration file.
3443
ParameterManager::getInstance().getStringByXpath("//ConnectionsParams/@class", type);
3544
connections_ = Factory<Connections>::getInstance().createType(type);
3645

46+
// If the factory returns an error (nullptr), exit
47+
if (connections_ == nullptr) {
48+
LOG4CPLUS_INFO(consoleLogger, "INVALID CLASS: " + type);
49+
exit(EXIT_FAILURE);
50+
}
51+
3752
// Create Recorder class using type definition from configuration file.
3853
ParameterManager::getInstance().getStringByXpath("//RecorderParams/@class", type);
3954
recorder_ = Factory<Recorder>::getInstance().createType(type);
4055

56+
// If the factory returns an error (nullptr), exit
57+
if (recorder_ == nullptr) {
58+
LOG4CPLUS_INFO(consoleLogger, "INVALID CLASS: " + type);
59+
exit(EXIT_FAILURE);
60+
}
61+
4162
// Get a copy of the file logger to use log4cplus macros
4263
fileLogger_ = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("file"));
4364
}

Simulator/Core/Simulator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ void Simulator::loadParameters()
9494
ParameterManager::getInstance().getStringByXpath("//RNGConfig/NoiseRNGSeed/@class", type);
9595
noiseRNG = Factory<MTRand>::getInstance().createType(type);
9696

97+
// If the factory returns an error (nullptr), exit
98+
if (noiseRNG == nullptr) {
99+
LOG4CPLUS_INFO(consoleLogger_, "INVALID CLASS: " + type);
100+
exit(EXIT_FAILURE);
101+
}
102+
97103
ParameterManager::getInstance().getLongByXpath("//RNGConfig/InitRNGSeed/text()", initRngSeed_);
98104
ParameterManager::getInstance().getLongByXpath("//RNGConfig/NoiseRNGSeed/text()", noiseRngSeed_);
99105
noiseRNG->seed(noiseRngSeed_);

Simulator/Layouts/Layout.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@
1919
/// Constructor
2020
Layout::Layout() : numEndogenouslyActiveNeurons_(0)
2121
{
22+
// Get a copy of the console logger to use in the case of errors
23+
log4cplus::Logger consoleLogger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("console"));
24+
2225
// Create Vertices/Neurons class using type definition in configuration file
2326
string type;
2427
ParameterManager::getInstance().getStringByXpath("//VerticesParams/@class", type);
2528
vertices_ = Factory<AllVertices>::getInstance().createType(type);
2629

30+
// If the factory returns an error (nullptr), exit
31+
if (vertices_ == nullptr) {
32+
LOG4CPLUS_INFO(consoleLogger, "INVALID CLASS: " + type);
33+
exit(EXIT_FAILURE);
34+
}
35+
2736
// Register loadParameters function as a loadParameters operation in the Operation Manager
2837
function<void()> loadParametersFunc = std::bind(&Layout::loadParameters, this);
2938
OperationManager::getInstance().registerOperation(Operations::op::loadParameters,

0 commit comments

Comments
 (0)