File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -509,6 +509,35 @@ class cached_mapt final
509
509
base_map.flush ();
510
510
}
511
511
512
+ /* ******************************************************************\
513
+
514
+ Function: cached_mapt::unload(const key_type &)
515
+
516
+ Inputs:
517
+ key: A key whose corresponding value should be unload.
518
+
519
+ Outputs:
520
+ True, if the referenced element was unload, and false otherwise.
521
+
522
+ Purpose:
523
+ Unloads the element referenced by the passed key in the cache to allow the
524
+ backing store to be externally manipulated.
525
+
526
+ Remarks:
527
+ This function will not unload a locked element.
528
+
529
+ \*******************************************************************/
530
+ bool unload (const key_type &key)
531
+ {
532
+ if (std::find (unloadable.cbegin (), unloadable.cend (), key)
533
+ == unloadable.cend ())
534
+ {
535
+ return false ;
536
+ }
537
+ base_map.set (key, remove_cache (key));
538
+ return true ;
539
+ }
540
+
512
541
private:
513
542
std::shared_ptr<mapped_type> get (
514
543
const key_type &key, std::function<mapped_type()> load_value) const
Original file line number Diff line number Diff line change @@ -122,6 +122,17 @@ void test_cached_map()
122
122
test_assert (map.cache_size () == 4 , " The unloaded element can be reloaded" );
123
123
test_assert (map.size () == 4 , " Map size is correct after element is reloaded" );
124
124
125
- // Now f4, f3 and f1 will be unlocked as they go out of scope
126
- // Now f4, f3 and f1 will be unloaded as map goes out of scope
125
+ test_assert (map.locked_count () == 4 ,
126
+ " All 4 elements must be locked before flush" );
127
+ test_assert (!map.unload (get_F_key (f2)),
128
+ " Flush must fail as the element 7 is locked" );
129
+ // Now we unlock elements 1 and 7 respectively.
130
+ f41 = nullptr ;
131
+ f22 = nullptr ;
132
+ test_assert (map.locked_count () == 2 ,
133
+ " After unlocking 1 and 7 there must remain only 2 locked" );
134
+ test_assert (map.unload (get_F_key (f2)),
135
+ " Flush of 7 must succeed as it was unlocked" );
136
+
137
+ // Now 5, 9 and will be unloaded as map goes out of scope
127
138
}
You can’t perform that action at this time.
0 commit comments