diff options
Diffstat (limited to 'src/support/dom/document_cache.cc')
-rw-r--r-- | src/support/dom/document_cache.cc | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/src/support/dom/document_cache.cc b/src/support/dom/document_cache.cc index 1573af8..c4c57d3 100644 --- a/src/support/dom/document_cache.cc +++ b/src/support/dom/document_cache.cc @@ -5,23 +5,33 @@ namespace InputXSLT { DomDocumentCache::DomDocumentCache(): + write_mutex_(), map_() { } -DomDocumentCache::item* DomDocumentCache::get(const std::string& key) { +DomDocumentCache::optional_item DomDocumentCache::get(const std::string& key) { auto itemIter = this->map_.find(key); if ( itemIter == this->map_.end() ) { - auto result = this->map_.emplace( - std::make_pair(key, std::unique_ptr<item>(new item("content"))) - ); - - if ( result.second ) { - return (*(result.first)).second.get(); - } else { - throw std::out_of_range("failed to instantiate DomDocumentCache"); - } + return optional_item(false, nullptr); } else { - return (*itemIter).second.get(); + return optional_item(true, (*itemIter).second.get()); + } +} + +DomDocumentCache::optional_item DomDocumentCache::create( + const std::string& key, + xercesc::DOMDocument* document +) { + std::lock_guard<std::mutex> guard(this->write_mutex_); + + auto result = this->map_.emplace( + std::make_pair(key, std::unique_ptr<item>(new item(document))) + ); + + if ( result.second ) { + return optional_item(true, (*(result.first)).second.get()); + } else { + return optional_item(false, nullptr); } } |