18#include <Poco/Notification.h>
19#include <Poco/NotificationCenter.h>
23#define strcasecmp _stricmp
42 return strcasecmp(lhs.c_str(),
rhs.c_str()) < 0;
62 using svc_it =
typename svcmap::iterator;
88 const std::shared_ptr<T> &
object()
const {
return m_object; }
117 const std::shared_ptr<T> &
newObject()
const {
return m_newObject; }
118 const std::shared_ptr<T> &
oldObject()
const {
return m_oldObject; }
189 virtual void add(
const std::string &
name,
const std::shared_ptr<T> &Tobject) {
190 checkForEmptyName(
name);
191 checkForNullPointer(Tobject);
193 bool success =
false;
196 std::lock_guard<std::recursive_mutex> lock(m_mutex);
201 success = datamap.insert(std::make_pair(
name, Tobject)).second;
204 std::string
error =
" add : Unable to insert Data Object : '" +
name +
"'";
206 throw std::runtime_error(
error);
208 g_log.
debug() <<
"Add Data Object " <<
name <<
" successful\n";
222 checkForNullPointer(Tobject);
225 std::unique_lock<std::recursive_mutex> lock(m_mutex);
228 auto it = datamap.find(
name);
229 if (it != datamap.end()) {
231 g_log.
debug(
"Data Object '" +
name +
"' replaced in data service.\n");
236 it->second = Tobject;
243 DataService::add(
name, Tobject);
252 std::unique_lock<std::recursive_mutex> lock(m_mutex);
254 auto it = datamap.find(
name);
255 if (it == datamap.end()) {
257 g_log.
debug(
" remove '" +
name +
"' cannot be found");
263 auto data = std::move(it->second);
271 g_log.
debug(
"Data Object '" +
name +
"' deleted from data service.");
280 void rename(
const std::string &oldName,
const std::string &newName) {
281 checkForEmptyName(newName);
282 bool caseInsensitiveMatch =
false;
284 if (!strcasecmp(oldName.c_str(), newName.c_str())) {
285 if (oldName == newName) {
286 g_log.
warning(
"Rename: The existing name matches the new name");
289 caseInsensitiveMatch =
true;
292 std::unique_lock<std::recursive_mutex> lock(m_mutex);
294 auto existingNameIter = datamap.find(oldName);
295 if (existingNameIter == datamap.end()) {
297 g_log.
warning(
" rename '" + oldName +
"' cannot be found");
301 auto existingNameObject = std::move(existingNameIter->second);
302 auto targetNameIter = caseInsensitiveMatch ? datamap.end() : datamap.find(newName);
305 if (targetNameIter != datamap.end()) {
306 auto targetNameObject = targetNameIter->second;
313 datamap.erase(existingNameIter);
315 if (targetNameIter != datamap.end()) {
316 targetNameIter->second = std::move(existingNameObject);
320 if (!(datamap.emplace(newName, std::move(existingNameObject)).second)) {
323 std::string
error =
" add : Unable to insert Data Object : '" + newName +
"'";
325 throw std::runtime_error(
error);
330 g_log.
debug(
"Data Object '" + oldName +
"' renamed to '" + newName +
"'");
339 std::lock_guard<std::recursive_mutex> lock(m_mutex);
343 g_log.
debug() <<
typeid(
this).
name() <<
" cleared.\n";
354 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
356 auto it = datamap.find(
name);
357 if (it != datamap.end()) {
367 return std::all_of(listOfNames.cbegin(), listOfNames.cend(),
368 [
this](
auto const &
name) { return this->doesExist(name); });
374 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
375 auto it = datamap.find(
name);
376 return it != datamap.end();
381 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
383 if (showingHiddenObjects()) {
384 return datamap.size();
386 return std::count_if(datamap.cbegin(), datamap.cend(),
387 [](
const auto &it) { return !isHiddenDataServiceObject(it.first); });
402 const std::string &contain =
"")
const {
404 std::vector<std::string> foundNames;
407 if (hiddenState == DataServiceHidden::Auto) {
408 if (showingHiddenObjects()) {
409 hiddenState = DataServiceHidden::Include;
411 hiddenState = DataServiceHidden::Exclude;
416 if (hiddenState == DataServiceHidden::Include) {
418 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
419 foundNames.reserve(datamap.size());
420 for (
const auto &item : datamap) {
421 if (contain.empty()) {
422 foundNames.emplace_back(item.first);
423 }
else if (item.first.find(contain) != std::string::npos) {
424 foundNames.emplace_back(item.first);
429 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
430 foundNames.reserve(datamap.size());
431 for (
const auto &item : datamap) {
432 if (!isHiddenDataServiceObject(item.first)) {
434 if (contain.empty()) {
435 foundNames.emplace_back(item.first);
436 }
else if (item.first.find(contain) != std::string::npos) {
437 foundNames.emplace_back(item.first);
445 if (sortState == DataServiceSort::Sorted) {
446 std::sort(foundNames.begin(), foundNames.end());
454 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
456 const bool alwaysIncludeHidden = includeHidden == DataServiceHidden::Include;
457 const bool usingAuto = includeHidden == DataServiceHidden::Auto && showingHiddenObjects();
459 const bool showingHidden = alwaysIncludeHidden || usingAuto;
461 std::vector<std::shared_ptr<T>> objects;
462 objects.reserve(datamap.size());
463 for (
const auto &it : datamap) {
464 if (showingHidden || !isHiddenDataServiceObject(it.first)) {
465 objects.emplace_back(it.second);
476 auto showingHiddenFlag = ConfigService::Instance().getValue<
bool>(
"MantidOptions.InvisibleWorkspaces");
477 return showingHiddenFlag.value_or(
false);
498 const std::string
error =
"Add Data Object with empty name";
500 throw std::runtime_error(
error);
506 const std::string
error =
"Attempt to add empty shared pointer";
508 throw std::runtime_error(
error);
const std::vector< double > & rhs
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
double obj
the value of the quadratic function
AddNotification is sent after an new object is added to the data service.
AddNotification(const std::string &name, const std::shared_ptr< T > &obj)
Constructor.
AfterReplaceNotification is sent after an object is replaced in the addOrReplace() function.
AfterReplaceNotification(const std::string &name, const std::shared_ptr< T > &newObj)
Constructor.
BeforeReplaceNotification is sent before an object is replaced in the addOrReplace() function.
std::shared_ptr< T > m_oldObject
const std::shared_ptr< T > & oldObject() const
BeforeReplaceNotification(const std::string &name, const std::shared_ptr< T > &obj, const std::shared_ptr< T > &newObj)
Constructor.
const std::shared_ptr< T > & newObject() const
Returns the pointer to the new object.
std::shared_ptr< T > m_newObject
shared pointer to the object
Clear notification is sent when the service is cleared.
ClearNotification()
Constructor.
Base class for DataService notifications that also stores a pointer to the object.
DataServiceNotification(const std::string &name, const std::shared_ptr< T > &obj)
Constructor.
const std::shared_ptr< T > & object() const
Returns the const pointer to the object concerned or 0 if it is a general notification.
std::shared_ptr< T > m_object
shared pointer to the object
Class for named object notifications.
NamedObjectNotification(const std::string &name)
std::string m_name
object's name
const std::string & objectName() const
Returns the name of the object.
PostDeleteNotification is sent after an object is deleted from the data service.
PostDeleteNotification(const std::string &name)
Constructor.
PreDeleteNotification is sent before an object is deleted from the data service.
PreDeleteNotification(const std::string &name, const std::shared_ptr< T > &obj)
Constructor.
Rename notification is sent when the rename method is called.
std::string m_newName
New object name.
const std::string & newObjectName() const
New name for the object.
RenameNotification(const std::string &name, const std::string &newName)
Constructor.
DataService stores instances of a given type.
virtual void add(const std::string &name, const std::shared_ptr< T > &Tobject)
Add an object to the service.
static std::string prefixToHide()
bool doesExist(const std::string &name) const
Check to see if a data object exists in the store.
void checkForNullPointer(const std::shared_ptr< T > &Tobject)
void rename(const std::string &oldName, const std::string &newName)
Rename an object within the service.
std::vector< std::string > getObjectNames(DataServiceSort sortState=DataServiceSort::Unsorted, DataServiceHidden hiddenState=DataServiceHidden::Auto, const std::string &contain="") const
Returns a vector of strings containing all object names in the ADS.
Logger g_log
Logger for this DataService.
svcmap datamap
Map of objects in the data service.
const std::string svcName
DataService name.
virtual void shutdown()
Prepare for shutdown.
std::shared_ptr< T > retrieve(const std::string &name) const
Get a shared pointer to a stored data object.
void checkForEmptyName(const std::string &name)
DataService & operator=(const DataService &)=delete
Deleted copy assignment operator.
size_t size() const
Return the number of objects stored by the data service.
std::map< std::string, std::shared_ptr< T >, CaseInsensitiveCmp > svcmap
Typedef for the map holding the names of and pointers to the data objects.
static bool showingHiddenObjects()
virtual ~DataService()=default
std::recursive_mutex m_mutex
Recursive mutex to avoid simultaneous access or notifications.
typename svcmap::const_iterator svc_constit
Const iterator for the data store map.
bool doAllWsExist(const std::vector< std::string > &listOfNames)
Checks all elements within the specified vector exist in the ADS.
virtual void addOrReplace(const std::string &name, const std::shared_ptr< T > &Tobject)
Add or replace an object to the service.
typename svcmap::iterator svc_it
Iterator for the data store map.
Poco::NotificationCenter notificationCenter
Sends notifications to observers.
DataService(const std::string &name)
Protected constructor (singleton)
std::vector< std::shared_ptr< T > > getObjects(DataServiceHidden includeHidden=DataServiceHidden::Auto) const
Get a vector of the pointers to the data objects stored by the service.
void remove(const std::string &name)
Remove an object from the service.
static bool isHiddenDataServiceObject(const std::string &name)
void clear()
Empty the service.
DataService(const DataService &)=delete
Deleted copy constructor.
Exception for when an item is not found in a collection.
The Logger class is in charge of the publishing messages from the framework through various channels.
void debug(const std::string &msg)
Logs at debug level.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
DataServiceSort
Flag for whether to sort items before returning.
DataServiceHidden
Flag for whether to include hidden items when returning, Auto queries the class to determine this beh...
Helper class which provides the Collimation Length for SANS instruments.
bool operator()(const std::string &lhs, const std::string &rhs) const