blob: 6b547b60bca44950c25738b99264e24d61fe0fb3 [file] [log] [blame]
Alex Deymo23949d42014-02-05 15:20:59 -08001// 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
Gilad Arnold48415f12014-06-27 07:10:58 -07005#ifndef UPDATE_ENGINE_UPDATE_MANAGER_BOXED_VALUE_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_BOXED_VALUE_H_
Alex Deymo23949d42014-02-05 15:20:59 -08007
David Zeuthenfe225c12014-04-29 10:37:35 -07008#include <string>
9
Alex Deymo23949d42014-02-05 15:20:59 -080010#include <base/basictypes.h>
11
Alex Deymo63784a52014-05-28 10:46:14 -070012namespace chromeos_update_manager {
Alex Deymo23949d42014-02-05 15:20:59 -080013
14// BoxedValue is a class to hold pointers of a given type that deletes them when
15// the instance goes out of scope, as scoped_ptr<T> does. The main difference
16// with it is that the type T is not part of the class, i.e., this isn't a
Alex Vakulenko072359c2014-07-18 11:41:07 -070017// parametric class. The class has a parametric constructor that accepts a
Alex Deymo23949d42014-02-05 15:20:59 -080018// const T* which will define the type of the object passed on delete.
19//
20// It is safe to use this class in linked containers such as std::list and
21// std::map but the object can't be copied. This means that you need to
Alex Vakulenko072359c2014-07-18 11:41:07 -070022// construct the BoxedValue in place using a container method like emplace()
Alex Deymo23949d42014-02-05 15:20:59 -080023// or move it with std::move().
24//
25// list<BoxedValue> lst;
26// lst.emplace_back(new const int(42));
27// lst.emplace_back(new const string("Hello world!"));
28//
29// map<int, BoxedValue> m;
30// m.emplace(123, std::move(BoxedValue(new const string("Hola mundo!"))));
31//
32// auto it = m.find(42);
33// if (it != m.end())
34// cout << "m[42] points to " << it->second.value() << endl;
35// cout << "m[33] points to " << m[33].value() << endl;
36//
37// Since copy and assign are not allowed, you can't create a copy of the
38// BoxedValue which means that you can only use a reference to it.
39//
40
41class BoxedValue {
42 public:
43 // Creates an empty BoxedValue. Since the pointer can't be assigned from other
44 // BoxedValues or pointers, this is only useful in places where a default
45 // constructor is required, such as std::map::operator[].
Alex Vakulenko88b591f2014-08-28 16:48:57 -070046 BoxedValue() : value_(nullptr), deleter_(nullptr), printer_(nullptr) {}
Alex Deymo23949d42014-02-05 15:20:59 -080047
48 // Creates a BoxedValue for the passed pointer |value|. The BoxedValue keeps
49 // the ownership of this pointer and can't be released.
50 template<typename T>
51 explicit BoxedValue(const T* value)
David Zeuthenfe225c12014-04-29 10:37:35 -070052 : value_(static_cast<const void*>(value)), deleter_(ValueDeleter<T>),
53 printer_(ValuePrinter<T>) {}
Alex Deymo23949d42014-02-05 15:20:59 -080054
55 // The move constructor takes ownership of the pointer since the semantics of
56 // it allows to render the passed BoxedValue undefined. You need to use the
Alex Vakulenko072359c2014-07-18 11:41:07 -070057 // move constructor explicitly preventing it from accidental references,
Alex Deymo23949d42014-02-05 15:20:59 -080058 // like in:
59 // BoxedValue new_box(std::move(other_box));
Alex Vakulenko072359c2014-07-18 11:41:07 -070060 BoxedValue(BoxedValue&& other) // NOLINT(build/c++11)
David Zeuthenfe225c12014-04-29 10:37:35 -070061 : value_(other.value_), deleter_(other.deleter_),
62 printer_(other.printer_) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070063 other.value_ = nullptr;
64 other.deleter_ = nullptr;
65 other.printer_ = nullptr;
Alex Deymo23949d42014-02-05 15:20:59 -080066 }
67
68 // Deletes the |value| passed on construction using the delete for the passed
69 // type.
70 ~BoxedValue() {
71 if (deleter_)
72 deleter_(value_);
73 }
74
75 const void* value() const { return value_; }
76
David Zeuthenfe225c12014-04-29 10:37:35 -070077 std::string ToString() const {
78 if (!printer_)
79 return "(no printer)";
80 if (!value_)
81 return "(no value)";
82 return printer_(value_);
83 }
84
Alex Deymo23949d42014-02-05 15:20:59 -080085 // Static method to call the destructor of the right type.
86 template<typename T>
87 static void ValueDeleter(const void* value) {
88 delete reinterpret_cast<const T*>(value);
89 }
90
David Zeuthenfe225c12014-04-29 10:37:35 -070091 // Static method to print a type. See boxed_value.cc for common
92 // instantiations.
93 template<typename T>
94 static std::string ValuePrinter(const void* value);
95
Alex Deymo23949d42014-02-05 15:20:59 -080096 private:
97 // A pointer to the cached value.
98 const void* value_;
99
100 // A function that calls delete for the right type of value_.
101 void (*deleter_)(const void*);
102
David Zeuthenfe225c12014-04-29 10:37:35 -0700103 // A function that converts value_ to a string.
104 std::string (*printer_)(const void*);
105
Alex Deymo23949d42014-02-05 15:20:59 -0800106 DISALLOW_COPY_AND_ASSIGN(BoxedValue);
107};
108
Alex Deymo63784a52014-05-28 10:46:14 -0700109} // namespace chromeos_update_manager
Alex Deymo23949d42014-02-05 15:20:59 -0800110
Gilad Arnold48415f12014-06-27 07:10:58 -0700111#endif // UPDATE_ENGINE_UPDATE_MANAGER_BOXED_VALUE_H_