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);
283 if (oldName == newName) {
284 g_log.
warning(
"Rename: The existing name matches the new name");
289 std::unique_lock<std::recursive_mutex> lock(m_mutex);
291 auto existingNameIter = datamap.find(oldName);
292 if (existingNameIter == datamap.end()) {
294 g_log.
warning(
" rename '" + oldName +
"' cannot be found");
298 auto existingNameObject = std::move(existingNameIter->second);
299 auto targetNameIter = datamap.find(newName);
302 if (targetNameIter != datamap.end()) {
303 auto targetNameObject = targetNameIter->second;
310 datamap.erase(existingNameIter);
312 if (targetNameIter != datamap.end()) {
313 targetNameIter->second = std::move(existingNameObject);
317 if (!(datamap.emplace(newName, std::move(existingNameObject)).second)) {
320 std::string
error =
" add : Unable to insert Data Object : '" + newName +
"'";
322 throw std::runtime_error(
error);
327 g_log.
debug(
"Data Object '" + oldName +
"' renamed to '" + newName +
"'");
336 std::lock_guard<std::recursive_mutex> lock(m_mutex);
340 g_log.
debug() <<
typeid(
this).
name() <<
" cleared.\n";
351 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
353 auto it = datamap.find(
name);
354 if (it != datamap.end()) {
364 return std::all_of(listOfNames.cbegin(), listOfNames.cend(),
365 [
this](
auto const &
name) { return this->doesExist(name); });
371 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
372 auto it = datamap.find(
name);
373 return it != datamap.end();
378 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
380 if (showingHiddenObjects()) {
381 return datamap.size();
383 return std::count_if(datamap.cbegin(), datamap.cend(),
384 [](
const auto &it) { return !isHiddenDataServiceObject(it.first); });
399 const std::string &contain =
"")
const {
401 std::vector<std::string> foundNames;
404 if (hiddenState == DataServiceHidden::Auto) {
405 if (showingHiddenObjects()) {
406 hiddenState = DataServiceHidden::Include;
408 hiddenState = DataServiceHidden::Exclude;
413 if (hiddenState == DataServiceHidden::Include) {
415 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
416 foundNames.reserve(datamap.size());
417 for (
const auto &item : datamap) {
418 if (contain.empty()) {
419 foundNames.emplace_back(item.first);
420 }
else if (item.first.find(contain) != std::string::npos) {
421 foundNames.emplace_back(item.first);
426 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
427 foundNames.reserve(datamap.size());
428 for (
const auto &item : datamap) {
429 if (!isHiddenDataServiceObject(item.first)) {
431 if (contain.empty()) {
432 foundNames.emplace_back(item.first);
433 }
else if (item.first.find(contain) != std::string::npos) {
434 foundNames.emplace_back(item.first);
442 if (sortState == DataServiceSort::Sorted) {
443 std::sort(foundNames.begin(), foundNames.end());
451 std::lock_guard<std::recursive_mutex> _lock(m_mutex);
453 const bool alwaysIncludeHidden = includeHidden == DataServiceHidden::Include;
454 const bool usingAuto = includeHidden == DataServiceHidden::Auto && showingHiddenObjects();
456 const bool showingHidden = alwaysIncludeHidden || usingAuto;
458 std::vector<std::shared_ptr<T>> objects;
459 objects.reserve(datamap.size());
460 for (
const auto &it : datamap) {
461 if (showingHidden || !isHiddenDataServiceObject(it.first)) {
462 objects.emplace_back(it.second);
473 auto showingHiddenFlag = ConfigService::Instance().getValue<
bool>(
"MantidOptions.InvisibleWorkspaces");
474 return showingHiddenFlag.value_or(
false);
495 const std::string
error =
"Add Data Object with empty name";
497 throw std::runtime_error(
error);
503 const std::string
error =
"Attempt to add empty shared pointer";
505 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