forked from ngageoint/csm
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Plugin.cpp
229 lines (195 loc) · 6.08 KB
/
Plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//#############################################################################
//
// FILENAME: Plugin.cpp
//
// CLASSIFICATION: Unclassified
//
// DESCRIPTION:
//
// This file provides implementation for methods declared in the
// CSMPlugin class.
//
// SOFTWARE HISTORY:
// Date Author Comment
// ----------- ------ -------
// 17-Dec-2012 BAH Documentation updates.
//
// NOTES:
//
// Refer to Plugin.h for more information.
//
//#############################################################################
#define CSM_LIBRARY
#include "Plugin.h"
#include <algorithm>
#include <iostream>
#ifdef _WIN32 //exports the symbols to be used (KJR)
# include <windows.h>
# include "RasterGM.h"
# include "BytestreamIsd.h"
# include "NitfIsd.h"
#else
# include <pthread.h>
#endif
namespace csm
{
PluginList* Plugin::theList = NULL;
Plugin::Impl* Plugin::theImpl = NULL;
std::string Plugin::theDataDir("");
//*****************************************************************************
// Plugin::Impl
//*****************************************************************************
class Plugin::Impl
{
public:
Impl() : mutex() { initializeMutex(); }
void lockList(void);
// pre: The list is unlocked.
// post: The list has been locked.
void unlockList(void);
// pre: The list is locked.
// post: The list has been unlocked.
class Locker
{
public:
Locker(Impl* impl) : theImpl(impl) { if (theImpl) theImpl->lockList(); }
~Locker() { if (theImpl) theImpl->unlockList(); }
private:
Impl* theImpl;
};
private:
void initializeMutex();
// pre: None.
// post: The mutex has been initialized.
#ifdef _WIN32
typedef HANDLE Mutex;
#else
typedef pthread_mutex_t Mutex;
#endif
Mutex mutex;
};
//*****************************************************************************
// Plugin::Impl::initializeMutex
//*****************************************************************************
void Plugin::Impl::initializeMutex()
{
#ifdef _WIN32
mutex = CreateMutex(NULL, FALSE, NULL); // TBD: handle errors
#else
pthread_mutex_init(&mutex, NULL); // TBD: handle errors
#endif
}
//*****************************************************************************
// Plugin::Impl::lockList
//*****************************************************************************
void Plugin::Impl::lockList(void)
{
#ifdef _WIN32
WaitForSingleObject(mutex, INFINITE);
#else
pthread_mutex_lock(&mutex); // TBD: handle error returns
#endif
}
//*****************************************************************************
// Plugin::Impl::unlockList
//*****************************************************************************
void Plugin::Impl::unlockList(void)
{
#ifdef _WIN32
ReleaseMutex(mutex); // TBD: handle errors
#else
pthread_mutex_unlock(&mutex); // TBD: handle error returns
#endif
}
//*****************************************************************************
// Plugin::getList
//*****************************************************************************
const PluginList& Plugin::getList()
{
// create the list if it hasn't already been made
if (!theList) theList = new PluginList();
return *theList;
}
//*****************************************************************************
// Plugin::findPlugin
//*****************************************************************************
const Plugin* Plugin::findPlugin(const std:: string& pluginName,
WarningList* warnings)
{
Impl::Locker locker(impl());
const PluginList& plugins = getList();
for (PluginList::const_iterator i = plugins.begin();
i != plugins.end(); ++i)
{
if (pluginName == (*i)->getPluginName())
{
return *i;
}
}
// plugin not found
if (warnings)
{
warnings->push_back(Warning(Warning::DATA_NOT_AVAILABLE,
"No matching plugin found",
"Plugin::findPlugin"));
}
return NULL;
}
//*****************************************************************************
// Plugin::removePlugin
//*****************************************************************************
void Plugin::removePlugin(const std::string& pluginName,
WarningList* warnings)
{
const Plugin* pluginPtr = findPlugin(pluginName, warnings);
std::string myName("removePlugin");
if (pluginPtr != NULL)
{
Impl::Locker locker(impl());
// find and remove pointer-to-plugin from theList
PluginList::iterator pos = std::find(theList->begin(),
theList->end(),
pluginPtr);
if (theList->end() == pos)
{
throw Error(Error::BOUNDS,
"Plugin Name \"" + pluginName + "\" not found",
"Plugin::removePlugin");
}
theList->erase(pos);
}
// if not found, then there's nothing to remove
}
//*****************************************************************************
// Plugin::Plugin
//*****************************************************************************
Plugin::Plugin()
{
//---
// If the list of registered sensor model factories does not exist yet, then
// create it.
//---
if (!theList) theList = new PluginList();
//---
// If the list of registered sensor model factories exists now (i.e., no
// error occurred while creating it), then register the plugin factory in
// theList by adding a pointer to this list.
// The pointer points to the static instance of the derived sensor
// model plugin.
//---
if (theList)
{
Impl::Locker locker(impl());
theList->push_back(this);
}
}
//*****************************************************************************
// Plugin::impl
//*****************************************************************************
Plugin::Impl* Plugin::impl()
{
// make the singleton if it hasn't already been made
if (!theImpl) theImpl = new Impl();
return theImpl;
}
} // namespace csm