blob: 200464fd15e08f0b0d0278fe64711d33e8fda60d [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
Ben Chan02f7c1d2014-10-18 15:18:02 -07008#include <memory>
David Zeuthenfe225c12014-04-29 10:37:35 -07009#include <string>
10
Ben Chan05735a12014-09-03 07:48:22 -070011#include <base/macros.h>
Alex Deymo23949d42014-02-05 15:20:59 -080012
Alex Deymo63784a52014-05-28 10:46:14 -070013namespace chromeos_update_manager {
Alex Deymo23949d42014-02-05 15:20:59 -080014
15// BoxedValue is a class to hold pointers of a given type that deletes them when
Ben Chan02f7c1d2014-10-18 15:18:02 -070016// the instance goes out of scope, as std::unique_ptr<T> does. The main
17// difference with it is that the type T is not part of the class, i.e., this
18// isn't a parametric class. The class has a parametric constructor that accepts
19// a const T* which will define the type of the object passed on delete.
Alex Deymo23949d42014-02-05 15:20:59 -080020//
21// It is safe to use this class in linked containers such as std::list and
22// std::map but the object can't be copied. This means that you need to
Alex Vakulenko072359c2014-07-18 11:41:07 -070023// construct the BoxedValue in place using a container method like emplace()
Alex Deymo23949d42014-02-05 15:20:59 -080024// or move it with std::move().
25//
26// list<BoxedValue> lst;
27// lst.emplace_back(new const int(42));
28// lst.emplace_back(new const string("Hello world!"));
29//
30// map<int, BoxedValue> m;
31// m.emplace(123, std::move(BoxedValue(new const string("Hola mundo!"))));
32//
33// auto it = m.find(42);
34// if (it != m.end())
35// cout << "m[42] points to " << it->second.value() << endl;
36// cout << "m[33] points to " << m[33].value() << endl;
37//
38// Since copy and assign are not allowed, you can't create a copy of the
39// BoxedValue which means that you can only use a reference to it.
40//
41
42class BoxedValue {
43 public:
44 // Creates an empty BoxedValue. Since the pointer can't be assigned from other
45 // BoxedValues or pointers, this is only useful in places where a default
46 // constructor is required, such as std::map::operator[].
Alex Vakulenko88b591f2014-08-28 16:48:57 -070047 BoxedValue() : value_(nullptr), deleter_(nullptr), printer_(nullptr) {}
Alex Deymo23949d42014-02-05 15:20:59 -080048
49 // Creates a BoxedValue for the passed pointer |value|. The BoxedValue keeps
50 // the ownership of this pointer and can't be released.
51 template<typename T>
52 explicit BoxedValue(const T* value)
David Zeuthenfe225c12014-04-29 10:37:35 -070053 : value_(static_cast<const void*>(value)), deleter_(ValueDeleter<T>),
54 printer_(ValuePrinter<T>) {}
Alex Deymo23949d42014-02-05 15:20:59 -080055
56 // The move constructor takes ownership of the pointer since the semantics of
57 // it allows to render the passed BoxedValue undefined. You need to use the
Alex Vakulenko072359c2014-07-18 11:41:07 -070058 // move constructor explicitly preventing it from accidental references,
Alex Deymo23949d42014-02-05 15:20:59 -080059 // like in:
60 // BoxedValue new_box(std::move(other_box));
Alex Vakulenko072359c2014-07-18 11:41:07 -070061 BoxedValue(BoxedValue&& other) // NOLINT(build/c++11)
David Zeuthenfe225c12014-04-29 10:37:35 -070062 : value_(other.value_), deleter_(other.deleter_),
63 printer_(other.printer_) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070064 other.value_ = nullptr;
65 other.deleter_ = nullptr;
66 other.printer_ = nullptr;
Alex Deymo23949d42014-02-05 15:20:59 -080067 }
68
69 // Deletes the |value| passed on construction using the delete for the passed
70 // type.
71 ~BoxedValue() {
72 if (deleter_)
73 deleter_(value_);
74 }
75
76 const void* value() const { return value_; }
77
David Zeuthenfe225c12014-04-29 10:37:35 -070078 std::string ToString() const {
79 if (!printer_)
80 return "(no printer)";
81 if (!value_)
82 return "(no value)";
83 return printer_(value_);
84 }
85
Alex Deymo23949d42014-02-05 15:20:59 -080086 // Static method to call the destructor of the right type.
87 template<typename T>
88 static void ValueDeleter(const void* value) {
89 delete reinterpret_cast<const T*>(value);
90 }
91
David Zeuthenfe225c12014-04-29 10:37:35 -070092 // Static method to print a type. See boxed_value.cc for common
93 // instantiations.
94 template<typename T>
95 static std::string ValuePrinter(const void* value);
96
Alex Deymo23949d42014-02-05 15:20:59 -080097 private:
98 // A pointer to the cached value.
99 const void* value_;
100
101 // A function that calls delete for the right type of value_.
102 void (*deleter_)(const void*);
103
David Zeuthenfe225c12014-04-29 10:37:35 -0700104 // A function that converts value_ to a string.
105 std::string (*printer_)(const void*);
106
Alex Deymo23949d42014-02-05 15:20:59 -0800107 DISALLOW_COPY_AND_ASSIGN(BoxedValue);
108};
109
Alex Deymo63784a52014-05-28 10:46:14 -0700110} // namespace chromeos_update_manager
Alex Deymo23949d42014-02-05 15:20:59 -0800111
Gilad Arnold48415f12014-06-27 07:10:58 -0700112#endif // UPDATE_ENGINE_UPDATE_MANAGER_BOXED_VALUE_H_