MarlinMT  0.1.0
BookStore.cc
Go to the documentation of this file.
2 
3 // -- std includes
4 #include <filesystem>
5 #include <unordered_map>
6 
7 // -- MarlinBook includes
11 
12 class PathHash {
13 public:
14  std::size_t operator()(const std::filesystem::path& path) const {
15  return std::filesystem::hash_value(path);
16  }
17 };
18 
19 namespace marlinmt {
20  namespace book {
21 
22  std::shared_ptr< details::Entry >
23  BookStore::addEntry( const std::shared_ptr< EntryBase > &entry,
24  EntryKey key ) {
25  key.idx = _entries.size() ;
26 
27  if ( !_idToEntry
28  .insert(
29  std::make_pair( Identifier( key.path ), key.idx ) )
30  .second ) {
31  MARLIN_BOOK_THROW( "Object already exist. Use store.book to avoid this." ) ;
32  }
33  _entries.push_back( std::make_shared< details::Entry >( details::Entry( entry, key ) ) ) ;
34  return _entries.back() ;
35  }
36 
37  //--------------------------------------------------------------------------
38 
39  void BookStore::store(StoreWriter& writer) const {
40  writer.writeSelection(
41  Selection::find(
42  _entries.begin(),
43  _entries.end(),
45  );
46  }
47 
48  //--------------------------------------------------------------------------
49 
50  void BookStore::storeSelection(
51  StoreWriter& writer, const Selection& selection) const {
52  writer.writeSelection(selection);
53  }
54 
55  //--------------------------------------------------------------------------
56 
57  Selection BookStore::find( const Condition &cond ) const {
58  return Selection::find( _entries.cbegin(), _entries.cend(), cond ) ;
59  }
60 
61  //--------------------------------------------------------------------------
62 
63  WeakEntry BookStore::findFirst( const Condition &cond) const {
64  return Selection::findFirst(_entries.cbegin(), _entries.cend(), cond);
65  }
66 
67  //--------------------------------------------------------------------------
68 
69  void BookStore::remove( const EntryKey &key ) { get( key ).clear(); }
70 
71  //--------------------------------------------------------------------------
72 
73  void BookStore::remove( const Selection &selection ) {
74  for ( const WeakEntry &e : selection ) {
75  remove( e.key() ) ;
76  }
77  }
78 
79  //--------------------------------------------------------------------------
80 
81  void BookStore::clear() { _entries.resize( 0 ); }
82 
83  //--------------------------------------------------------------------------
84 
85  std::size_t BookStore::Identifier::Hash::
86  operator()( const Identifier &id ) const {
87  return std::filesystem::hash_value(id._path);
88  }
89 
90  //--------------------------------------------------------------------------
91 
92  std::filesystem::path BookStore::normalizeDirPath(const std::filesystem::path& path) {
93  if(!path.is_absolute() || path.has_filename()) {
94  MARLIN_BOOK_THROW( std::string("'")
95  + path.string()
96  + "' is not an absolute path to folder!");
97  }
98  return std::filesystem::absolute(path);
99  }
100 
101 
102  } // end namespace book
103 } // end namespace marlinmt
104 
Data selection to identify and manage an Entry.
Definition: EntryData.h:21
#define MARLIN_BOOK_THROW(message)
Definition: Types.h:10
std::filesystem::path path
virtual Entry path
Definition: EntryData.h:30
Wrapper for weak pointer to Entry.
Definition: Selection.h:29
void writeSelection(const Selection &sel)
Definition: StoreWriter.cc:48
std::size_t operator()(const std::filesystem::path &path) const
Definition: BookStore.cc:14
helper to create a Condition.
Definition: Condition.h:92
Contains references to entries.
Definition: Selection.h:75
holds references for identify an entry.
Definition: BookStore.h:103
std::size_t idx
unique number for Entry
Definition: EntryData.h:38
class to store and manage objects in BookStore.
Definition: Entry.h:124
wrapper class for an Entry filter function.
Definition: Condition.h:21