Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "test_runner.h"
- #include <functional>
- #include <memory>
- #include <string>
- using namespace std;
- template <typename T>
- class LazyValue {
- public:
- explicit LazyValue(std::function<T()> init)
- : init_ { init }
- {
- }
- bool HasValue() const { return bool(Value); }
- const T& Get() const
- {
- if (Value)
- return *Value.get();
- Value = make_unique<T>(init_());
- return *Value.get();
- }
- private:
- mutable unique_ptr<T> Value;
- std::function<T()>& init_;
- };
- void UseExample()
- {
- const string big_string = "Giant amounts of memory";
- LazyValue<string> lazy_string([&big_string] { return big_string; });
- ASSERT(!lazy_string.HasValue());
- ASSERT_EQUAL(lazy_string.Get(), big_string);
- ASSERT_EQUAL(lazy_string.Get(), big_string);
- }
- void TestInitializerIsntCalled()
- {
- bool called = false;
- {
- LazyValue<int> lazy_int([&called] {
- called = true;
- return 0;
- });
- }
- ASSERT(!called);
- }
- int main()
- {
- TestRunner tr;
- RUN_TEST(tr, UseExample);
- RUN_TEST(tr, TestInitializerIsntCalled);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement