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 //
16 
17 #ifndef UPDATE_ENGINE_UPDATE_MANAGER_BOXED_VALUE_H_
18 #define UPDATE_ENGINE_UPDATE_MANAGER_BOXED_VALUE_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include <base/macros.h>
24 
25 namespace chromeos_update_manager {
26 
27 // BoxedValue is a class to hold pointers of a given type that deletes them when
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.
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
35 // construct the BoxedValue in place using a container method like emplace()
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[].
BoxedValue()59   BoxedValue() : value_(nullptr), deleter_(nullptr), printer_(nullptr) {}
60 
61   // Creates a BoxedValue for the passed pointer |value|. The BoxedValue keeps
62   // the ownership of this pointer and can't be released.
63   template<typename T>
BoxedValue(const T * value)64   explicit BoxedValue(const T* value)
65     : value_(static_cast<const void*>(value)), deleter_(ValueDeleter<T>),
66       printer_(ValuePrinter<T>) {}
67 
68   // The move constructor takes ownership of the pointer since the semantics of
69   // it allows to render the passed BoxedValue undefined. You need to use the
70   // move constructor explicitly preventing it from accidental references,
71   // like in:
72   //   BoxedValue new_box(std::move(other_box));
BoxedValue(BoxedValue && other)73   BoxedValue(BoxedValue&& other)  // NOLINT(build/c++11)
74       : value_(other.value_), deleter_(other.deleter_),
75         printer_(other.printer_) {
76     other.value_ = nullptr;
77     other.deleter_ = nullptr;
78     other.printer_ = nullptr;
79   }
80 
81   // Deletes the |value| passed on construction using the delete for the passed
82   // type.
~BoxedValue()83   ~BoxedValue() {
84     if (deleter_)
85       deleter_(value_);
86   }
87 
value()88   const void* value() const { return value_; }
89 
ToString()90   std::string ToString() const {
91     if (!printer_)
92       return "(no printer)";
93     if (!value_)
94       return "(no value)";
95     return printer_(value_);
96   }
97 
98   // Static method to call the destructor of the right type.
99   template<typename T>
ValueDeleter(const void * value)100   static void ValueDeleter(const void* value) {
101     delete reinterpret_cast<const T*>(value);
102   }
103 
104   // Static method to print a type. See boxed_value.cc for common
105   // instantiations.
106   template<typename T>
107   static std::string ValuePrinter(const void* value);
108 
109  private:
110   // A pointer to the cached value.
111   const void* value_;
112 
113   // A function that calls delete for the right type of value_.
114   void (*deleter_)(const void*);
115 
116   // A function that converts value_ to a string.
117   std::string (*printer_)(const void*);
118 
119   DISALLOW_COPY_AND_ASSIGN(BoxedValue);
120 };
121 
122 }  // namespace chromeos_update_manager
123 
124 #endif  // UPDATE_ENGINE_UPDATE_MANAGER_BOXED_VALUE_H_
125