root/tlate/lazy_structured_value.hh
| Revision 868b365c127bbf4f0b52f22da03e32a94a4f6653, 2.0 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 3 years ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | /* This file is part of Alue, the multiprotocol Internet discussion daemon |
| 2 | |
| 3 | Copyright © 2009 Antti-Juhani Kaijanaho |
| 4 | |
| 5 | Alue is free software: you can redistribute it and/or modify it |
| 6 | under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | Alue is distributed in the hope that it will be useful, but |
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with Alue. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | */ |
| 19 | |
| 20 | #ifndef GUARD_TLATE_LAZY_STRUCTURED_VALUE_HH |
| 21 | #define GUARD_TLATE_LAZY_STRUCTURED_VALUE_HH |
| 22 | |
| 23 | #include "missing_value.hh" |
| 24 | #include "structured_value.hh" |
| 25 | |
| 26 | #include <boost/thread/mutex.hpp> |
| 27 | #include <map> |
| 28 | |
| 29 | namespace tlate |
| 30 | { |
| 31 | class lazy_structured_value : public structured_value |
| 32 | { |
| 33 | protected: |
| 34 | mutable boost::mutex mt; |
| 35 | structured_value::const_ptr computer; |
| 36 | mutable std::map<std::string,value::const_ptr> memo; |
| 37 | public: |
| 38 | typedef boost::shared_ptr<lazy_structured_value> ptr; |
| 39 | typedef boost::shared_ptr<const lazy_structured_value> |
| 40 | const_ptr; |
| 41 | |
| 42 | explicit lazy_structured_value(structured_value::const_ptr computer) |
| 43 | : computer(computer) |
| 44 | {} |
| 45 | |
| 46 | value::const_ptr get(std::string) const; |
| 47 | }; |
| 48 | inline |
| 49 | value::const_ptr lazy_structured_value::get(std::string key) const |
| 50 | { |
| 51 | boost::lock_guard<boost::mutex> lg(mt); |
| 52 | value::const_ptr &rv = memo[key]; |
| 53 | if (!rv) rv = computer->get(key); |
| 54 | if (!rv) rv.reset(new missing_value); |
| 55 | return rv; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | #endif /* GUARD_TLATE_LAZY_STRUCTURED_VALUE_HH */ |
Note: See TracBrowser
for help on using the browser.
