Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 16 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_BOXED_VALUE_H_ |
| 18 | #define UPDATE_ENGINE_UPDATE_MANAGER_BOXED_VALUE_H_ |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 19 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 20 | #include <memory> |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 23 | #include <base/macros.h> |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 24 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 25 | namespace chromeos_update_manager { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 26 | |
| 27 | // BoxedValue is a class to hold pointers of a given type that deletes them when |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 28 | // the instance goes out of scope, as std::unique_ptr<T> does. The main |
| 29 | // difference with it is that the type T is not part of the class, i.e., this |
| 30 | // isn't a parametric class. The class has a parametric constructor that accepts |
| 31 | // a const T* which will define the type of the object passed on delete. |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 32 | // |
| 33 | // It is safe to use this class in linked containers such as std::list and |
| 34 | // std::map but the object can't be copied. This means that you need to |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 35 | // construct the BoxedValue in place using a container method like emplace() |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 36 | // or move it with std::move(). |
| 37 | // |
| 38 | // list<BoxedValue> lst; |
| 39 | // lst.emplace_back(new const int(42)); |
| 40 | // lst.emplace_back(new const string("Hello world!")); |
| 41 | // |
| 42 | // map<int, BoxedValue> m; |
| 43 | // m.emplace(123, std::move(BoxedValue(new const string("Hola mundo!")))); |
| 44 | // |
| 45 | // auto it = m.find(42); |
| 46 | // if (it != m.end()) |
| 47 | // cout << "m[42] points to " << it->second.value() << endl; |
| 48 | // cout << "m[33] points to " << m[33].value() << endl; |
| 49 | // |
| 50 | // Since copy and assign are not allowed, you can't create a copy of the |
| 51 | // BoxedValue which means that you can only use a reference to it. |
| 52 | // |
| 53 | |
| 54 | class BoxedValue { |
| 55 | public: |
| 56 | // Creates an empty BoxedValue. Since the pointer can't be assigned from other |
| 57 | // BoxedValues or pointers, this is only useful in places where a default |
| 58 | // constructor is required, such as std::map::operator[]. |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 59 | BoxedValue() : value_(nullptr), deleter_(nullptr), printer_(nullptr) {} |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 60 | |
| 61 | // Creates a BoxedValue for the passed pointer |value|. The BoxedValue keeps |
| 62 | // the ownership of this pointer and can't be released. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 63 | template <typename T> |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 64 | explicit BoxedValue(const T* value) |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 65 | : value_(static_cast<const void*>(value)), |
| 66 | deleter_(ValueDeleter<T>), |
| 67 | printer_(ValuePrinter<T>) {} |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 68 | |
| 69 | // The move constructor takes ownership of the pointer since the semantics of |
| 70 | // it allows to render the passed BoxedValue undefined. You need to use the |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 71 | // move constructor explicitly preventing it from accidental references, |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 72 | // like in: |
| 73 | // BoxedValue new_box(std::move(other_box)); |
Chih-Hung Hsieh | 270524f | 2018-10-01 17:55:21 -0700 | [diff] [blame] | 74 | BoxedValue(BoxedValue&& other) noexcept |
Chih-Hung Hsieh | 1fae1c1 | 2018-09-25 14:33:32 -0700 | [diff] [blame] | 75 | : value_(other.value_), |
| 76 | deleter_(other.deleter_), |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 77 | printer_(other.printer_) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 78 | other.value_ = nullptr; |
| 79 | other.deleter_ = nullptr; |
| 80 | other.printer_ = nullptr; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Deletes the |value| passed on construction using the delete for the passed |
| 84 | // type. |
| 85 | ~BoxedValue() { |
| 86 | if (deleter_) |
| 87 | deleter_(value_); |
| 88 | } |
| 89 | |
| 90 | const void* value() const { return value_; } |
| 91 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 92 | std::string ToString() const { |
| 93 | if (!printer_) |
| 94 | return "(no printer)"; |
| 95 | if (!value_) |
| 96 | return "(no value)"; |
| 97 | return printer_(value_); |
| 98 | } |
| 99 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 100 | // Static method to call the destructor of the right type. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 101 | template <typename T> |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 102 | static void ValueDeleter(const void* value) { |
| 103 | delete reinterpret_cast<const T*>(value); |
| 104 | } |
| 105 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 106 | // Static method to print a type. See boxed_value.cc for common |
| 107 | // instantiations. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 108 | template <typename T> |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 109 | static std::string ValuePrinter(const void* value); |
| 110 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 111 | private: |
| 112 | // A pointer to the cached value. |
| 113 | const void* value_; |
| 114 | |
| 115 | // A function that calls delete for the right type of value_. |
| 116 | void (*deleter_)(const void*); |
| 117 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 118 | // A function that converts value_ to a string. |
| 119 | std::string (*printer_)(const void*); |
| 120 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 121 | DISALLOW_COPY_AND_ASSIGN(BoxedValue); |
| 122 | }; |
| 123 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 124 | } // namespace chromeos_update_manager |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 125 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 126 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_BOXED_VALUE_H_ |