EventAnalysis  1.3.0
DataStore.h
Go to the documentation of this file.
1 /*
2  * DataStore.h
3  *
4  * Created on: 03 Jan 2017
5  * Author: Nicola Mori
6  */
7 
10 #ifndef DATASTORE_H_
11 #define DATASTORE_H_
12 
13 #include "core/Info.h"
14 #include "core/ObjectMap.h"
15 #include "core/ObserverPtr.h"
16 #include "core/RetrievalResult.h"
17 #include "data/DataProvider.h"
18 #include "data/EventDataCache.h"
20 #include "data/ObjectCategory.h"
23 
24 #include <vector>
25 
26 namespace EA {
27 
28 class AlgoSequence;
29 
41 class DataStore : public Info, private ObjectMap {
42 public:
43  // Constructors/destructor
44 
50  DataStore(const std::string &name, ObjectCategory category);
51 
52  // ObjectMap interface
53 
64  template <class DataType>
65  void AddObject(const std::string &name, DataType &obj, const observer_ptr<ObjectProducer> &producer = nullptr);
66 
77  template <class DataType>
78  void AddObject(const std::string &name, observer_ptr<DataType> obj,
79  const observer_ptr<ObjectProducer> &producer = nullptr);
80 
91  void AddObject(const std::string &name, const char *obj, const observer_ptr<ObjectProducer> & = nullptr);
92 
103  template <class DataType>
104  void AddObject(const std::string &name, std::shared_ptr<DataType> obj,
105  const observer_ptr<ObjectProducer> &producer = nullptr);
106 
117  template <class DataType>
118  void AddObject(const std::string &name, std::unique_ptr<DataType> obj,
119  const observer_ptr<ObjectProducer> & = nullptr);
120 
128  void SetAlias(const std::string &objName, const std::string &objAlias);
129 
143  template <class DataType> observer_ptr<DataType> GetObject(const std::string &name);
144 
155  bool RemoveObject(const std::string &name, bool removeAliased = false);
156 
163  std::vector<std::string> GetObjects() const { return ObjectMap::GetObjects(); }
164 
170  bool IsPresent(const std::string &nameOrAlias) { return ObjectMap::IsPresent(nameOrAlias); }
171 
179  struct KnownObject {
180  std::string name;
181  std::set<std::string> aliases;
182 
190  bool operator==(const std::string &rhs) const { return name == rhs; };
191  };
192 
211  const std::vector<KnownObject> &GetKnownObjects();
212 
223  std::vector<std::string> GetAliases(const std::string &name = "") const { return ObjectMap::GetAliases(name); }
224 
232  bool Clean();
233 
240 
247  bool IsAlias(const std::string &objName) { return ObjectMap::IsAlias(objName); }
248 
256  const std::string &GetNameOfAliasedObject(const std::string &aliasName) {
257  return ObjectMap::GetNameOfAliasedObject(aliasName);
258  }
259 
272  ObjectWrapper GetObjectWrapper(const std::string &objName);
273 
274  // DataStore interface
285 
290  const std::vector<observer_ptr<DataProvider>> GetAttachedProviders() { return _providers; }
291 
299  void TrackRemovedObjects(bool track = true);
300 
305  bool IsTrackingRemovedObjects() { return (bool)_remObjTracker; }
306 
317  bool FreeRemoved(Memory::Status memStatus);
318 
329  bool SetAlgoProducedObjects(const AlgoSequence &seq);
330 
336  bool IsAlgoProducedObject(const std::string nameOrAlias);
337 
338 protected:
344  template <class DataType> ObjPtr<DataType> GetFromMap(const std::string &name);
345 
354  RetrievalResult GetFromProvider(const std::string &name, ObjectWrapper &wrapper, std::string &actualName,
355  observer_ptr<ObjectProducer> &provider);
356 
371  template <class DataType>
372  ObjPtr<DataType> CastAndWrap(const std::string &name, const std::string &actualName, ObjectWrapper &&wrapper,
374 
379  void AddKnownObjects(const ObjectProducer &producer);
380 
381 private:
382  std::vector<observer_ptr<DataProvider>> _providers;
384  std::vector<KnownObject> _knownObjects;
386 
387  std::unique_ptr<RemovedObjectsTracker> _remObjTracker;
388 
389  // The following two containers are kept ordered
390  std::list<std::string> _algoProdObjects;
391  std::list<std::string> _algoProdAliases;
392 };
393 
399 class OwningDataStore : public DataStore {
400 public:
401  OwningDataStore(const std::string &name, ObjectCategory category) : DataStore(name, category) {}
402 
403  template <class DataType> void AddObject(const std::string &name, std::shared_ptr<DataType> obj) {
404  DataStore::AddObject(name, obj);
405  }
406 
407  template <class DataType> void AddObject(const std::string &name, std::unique_ptr<DataType> &&obj) {
408  DataStore::AddObject(name, std::move(obj));
409  }
410 };
411 
413 public:
414  GlobalDataStore(const std::string &name) : OwningDataStore(name, ObjectCategory::GLOBAL) {}
415 };
416 
417 class PassDataStore : public DataStore {
418 public:
419  PassDataStore(const std::string &name) : DataStore(name, ObjectCategory::PASS) {}
420 };
421 
422 class EventDataStore : public DataStore {
423 public:
424  EventDataStore(const std::string &name) : DataStore(name, ObjectCategory::EVENT) {}
425 
434  bool AddEventCache(observer_ptr<EventDataCache> cache);
435 
448  template <class DataType> observer_ptr<DataType> GetObject(const std::string &name);
449 
450 private:
452 };
453 
454 } // namespace EA
455 
456 #include "data/DataStore.hpp"
457 
458 #endif /* DATASTORE_H_ */
bool AddProvider(observer_ptr< DataProvider > provider)
Adds a data provider in the data store.
Definition: DataStore.cpp:107
std::string name
Definition: DataStore.h:180
bool IsAlias(const std::string &objName)
Checks if an object name is actually an alias.
Definition: DataStore.h:247
Objects with values defined on a per-event basis.
Class describing a pointer to an object retrieved from an ObjectMap.
Definition: ObjPtr.h:31
bool IsPresent(const std::string &nameOrAlias) const
Checks if an object is present in the map.
Definition: ObjectMap.cpp:123
std::unique_ptr< RemovedObjectsTracker > _remObjTracker
Definition: DataStore.h:387
Definition: DataStore.h:417
bool CleanAliases()
Removes all the aliases from the map.
Definition: ObjectMap.cpp:138
bool CleanAliases()
Removes all the aliases from the store.
Definition: DataStore.h:239
bool SetAlgoProducedObjects(const AlgoSequence &seq)
Notify the data store about objects that are produced by the algorithms.
Definition: DataStore.cpp:61
void TrackRemovedObjects(bool track=true)
Enables the tracking of removed objects.
Definition: DataStore.cpp:134
EventDataStore(const std::string &name)
Definition: DataStore.h:424
bool Clean()
Removes all the objects and aliases from the store.
Definition: DataStore.cpp:97
std::list< std::string > _algoProdAliases
Definition: DataStore.h:391
bool _providersCheckedForProducedObjects
Definition: DataStore.h:385
OwningDataStore(const std::string &name, ObjectCategory category)
Definition: DataStore.h:401
Descriptor for a known object.
Definition: DataStore.h:179
observer_ptr< EventDataCache > _eventCache
Definition: DataStore.h:451
bool operator==(const std::string &rhs) const
All the aliases that might be used for the object.
Definition: DataStore.h:190
A smart pointer not owning the wrapped object.
Definition: ObserverPtr.h:28
GlobalDataStore(const std::string &name)
Definition: DataStore.h:414
std::vector< KnownObject > _knownObjects
Definition: DataStore.h:384
const std::vector< observer_ptr< DataProvider > > GetAttachedProviders()
Gets all the providers attached to the data store.
Definition: DataStore.h:290
const std::string & GetNameOfAliasedObject(const std::string &aliasName)
Gets the name of the object corresponding to an alias.
Definition: ObjectMap.cpp:70
bool IsPresent(const std::string &nameOrAlias)
Checks if an object is present in the map.
Definition: DataStore.h:170
std::set< std::string > aliases
The name of the object.
Definition: DataStore.h:181
Interface for retrieving informations about an object.
Definition: Info.h:18
bool IsAlgoProducedObject(const std::string nameOrAlias)
Checks if a given object is produced by an algorithm.
Definition: DataStore.cpp:89
RetrievalResult
Definition: RetrievalResult.h:16
bool IsAlias(const std::string &objName)
Checks if an object name is actually an alias.
Definition: ObjectMap.cpp:195
const std::vector< KnownObject > & GetKnownObjects()
Returns a list of all the objects that are known to the store.
Definition: DataStore.cpp:44
Algorithm made by multiple basic algorithms.
Definition: AlgoSequence.h:37
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
A map between strings and objects of ObjectWrapper kind.
Definition: ObjectMap.h:32
bool RemoveObject(const std::string &name, bool removeAliased=false)
Removes an object or an alias from the map.
Definition: DataStore.cpp:27
void SetAlias(const std::string &objName, const std::string &objAlias)
Set an alias for the given object.
Definition: DataStore.cpp:37
void AddKnownObjects(const ObjectProducer &producer)
Definition: DataStore.cpp:195
A data store owning all of the contained objects.
Definition: DataStore.h:399
ObjPtr< DataType > GetFromMap(const std::string &name)
Retrieves an object from the internal ObjectMap.
Definition: DataStore.hpp:108
Objects with values defined on a per-pass basis.
RetrievalResult GetFromProvider(const std::string &name, ObjectWrapper &wrapper, std::string &actualName, observer_ptr< ObjectProducer > &provider)
Retrieves an object from the registered providers.
Definition: DataStore.cpp:176
ObjectCategory
Category of objects.
Definition: ObjectCategory.h:17
Generic wrapper class.
Definition: ObjectWrapper.h:28
void AddObject(const std::string &name, DataType &obj, const observer_ptr< ObjectProducer > &producer=nullptr)
Adds an object to the map, storing also the producer.
Definition: DataStore.hpp:15
std::vector< std::string > GetObjects() const
Returns the names of the objects in the map.
Definition: ObjectMap.cpp:114
std::vector< std::string > GetObjects() const
Returns the names of the objects in the store.
Definition: DataStore.h:163
ObjPtr< DataType > CastAndWrap(const std::string &name, const std::string &actualName, ObjectWrapper &&wrapper, observer_ptr< ObjectProducer > provider)
Casts the wrapper to the template type, registers the object in the store and wraps it inside the ret...
Definition: DataStore.hpp:113
Definition: DataStore.h:422
PassDataStore(const std::string &name)
Definition: DataStore.h:419
std::vector< std::string > GetAliases(const std::string &name="") const
Returns the aliases of the objects in the store.
Definition: DataStore.h:223
std::vector< std::string > GetAliases(const std::string &name="") const
Returns the aliases of the objects in the map.
Definition: ObjectMap.cpp:184
std::list< std::string > _algoProdObjects
Definition: DataStore.h:390
Interface for a class which produced data objects.
Definition: ObjectProducer.h:29
DataStore(const std::string &name, ObjectCategory category)
Definition: DataStore.cpp:17
Status
Aliases for memory occupation levels.
Definition: Memory.h:23
bool IsTrackingRemovedObjects()
Checks whether the data store is tracking removed objects or not.
Definition: DataStore.h:305
std::vector< observer_ptr< DataProvider > > _providers
Definition: DataStore.h:382
Undefined value (for default initialization).
observer_ptr< DataType > GetObject(const std::string &name)
Provides a data object.
Definition: DataStore.hpp:67
void AddObject(const std::string &name, std::unique_ptr< DataType > &&obj)
Definition: DataStore.h:407
void AddObject(const std::string &name, std::shared_ptr< DataType > obj)
Definition: DataStore.h:403
Definition: DataStore.h:412
Data store class.
Definition: DataStore.h:41
ObjectWrapper GetObjectWrapper(const std::string &objName)
Provides a data object wrapper.
Definition: DataStore.cpp:142
bool FreeRemoved(Memory::Status memStatus)
Release the memory allocated for removed objects.
Definition: DataStore.cpp:54
const std::string & GetNameOfAliasedObject(const std::string &aliasName)
Gets the name of the object corresponding to an alias.
Definition: DataStore.h:256
ObjectCategory _category
Definition: DataStore.h:383