Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame^] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_BOXED_VALUE_H |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_BOXED_VALUE_H |
| 7 | |
| 8 | #include <base/basictypes.h> |
| 9 | |
| 10 | namespace chromeos_policy_manager { |
| 11 | |
| 12 | // BoxedValue is a class to hold pointers of a given type that deletes them when |
| 13 | // the instance goes out of scope, as scoped_ptr<T> does. The main difference |
| 14 | // with it is that the type T is not part of the class, i.e., this isn't a |
| 15 | // parametric class. The class has a parametric contructor that accepts a |
| 16 | // const T* which will define the type of the object passed on delete. |
| 17 | // |
| 18 | // It is safe to use this class in linked containers such as std::list and |
| 19 | // std::map but the object can't be copied. This means that you need to |
| 20 | // construct the BoxedValue inplace using a container method like emplace() |
| 21 | // or move it with std::move(). |
| 22 | // |
| 23 | // list<BoxedValue> lst; |
| 24 | // lst.emplace_back(new const int(42)); |
| 25 | // lst.emplace_back(new const string("Hello world!")); |
| 26 | // |
| 27 | // map<int, BoxedValue> m; |
| 28 | // m.emplace(123, std::move(BoxedValue(new const string("Hola mundo!")))); |
| 29 | // |
| 30 | // auto it = m.find(42); |
| 31 | // if (it != m.end()) |
| 32 | // cout << "m[42] points to " << it->second.value() << endl; |
| 33 | // cout << "m[33] points to " << m[33].value() << endl; |
| 34 | // |
| 35 | // Since copy and assign are not allowed, you can't create a copy of the |
| 36 | // BoxedValue which means that you can only use a reference to it. |
| 37 | // |
| 38 | |
| 39 | class BoxedValue { |
| 40 | public: |
| 41 | // Creates an empty BoxedValue. Since the pointer can't be assigned from other |
| 42 | // BoxedValues or pointers, this is only useful in places where a default |
| 43 | // constructor is required, such as std::map::operator[]. |
| 44 | BoxedValue() : value_(NULL), deleter_(NULL) {} |
| 45 | |
| 46 | // Creates a BoxedValue for the passed pointer |value|. The BoxedValue keeps |
| 47 | // the ownership of this pointer and can't be released. |
| 48 | template<typename T> |
| 49 | explicit BoxedValue(const T* value) |
| 50 | : value_(static_cast<const void*>(value)), deleter_(ValueDeleter<T>) {} |
| 51 | |
| 52 | // The move constructor takes ownership of the pointer since the semantics of |
| 53 | // it allows to render the passed BoxedValue undefined. You need to use the |
| 54 | // move constructor explictly preventing it from accidental references, |
| 55 | // like in: |
| 56 | // BoxedValue new_box(std::move(other_box)); |
| 57 | BoxedValue(BoxedValue&& other) |
| 58 | : value_(other.value_), deleter_(other.deleter_) { |
| 59 | other.value_ = NULL; |
| 60 | other.deleter_ = NULL; |
| 61 | } |
| 62 | |
| 63 | // Deletes the |value| passed on construction using the delete for the passed |
| 64 | // type. |
| 65 | ~BoxedValue() { |
| 66 | if (deleter_) |
| 67 | deleter_(value_); |
| 68 | } |
| 69 | |
| 70 | const void* value() const { return value_; } |
| 71 | |
| 72 | // Static method to call the destructor of the right type. |
| 73 | template<typename T> |
| 74 | static void ValueDeleter(const void* value) { |
| 75 | delete reinterpret_cast<const T*>(value); |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | // A pointer to the cached value. |
| 80 | const void* value_; |
| 81 | |
| 82 | // A function that calls delete for the right type of value_. |
| 83 | void (*deleter_)(const void*); |
| 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(BoxedValue); |
| 86 | }; |
| 87 | |
| 88 | } // namespace chromeos_policy_manager |
| 89 | |
| 90 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_BOXED_VALUE_H |