OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SERVICES_MEDIA_FRAMEWORK_SAFE_CLONE_H_ | 5 #ifndef SERVICES_MEDIA_FRAMEWORK_SAFE_CLONE_H_ |
6 #define SERVICES_MEDIA_FRAMEWORK_SAFE_CLONE_H_ | 6 #define SERVICES_MEDIA_FRAMEWORK_SAFE_CLONE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 namespace media { | 12 namespace media { |
13 | 13 |
14 template<typename T> | 14 template <typename T> |
15 std::unique_ptr<T> SafeClone(const std::unique_ptr<T>& t_ptr) { | 15 std::unique_ptr<T> SafeClone(const std::unique_ptr<T>& t_ptr) { |
16 return t_ptr ? t_ptr.get()->Clone() : nullptr; | 16 return t_ptr ? t_ptr.get()->Clone() : nullptr; |
17 } | 17 } |
18 | 18 |
19 template<typename T> | 19 template <typename T> |
20 std::unique_ptr<std::vector<std::unique_ptr<T>>> SafeClone( | 20 std::unique_ptr<std::vector<std::unique_ptr<T>>> SafeClone( |
21 const std::unique_ptr<std::vector<std::unique_ptr<T>>>& vec) { | 21 const std::unique_ptr<std::vector<std::unique_ptr<T>>>& vec) { |
22 if (vec == nullptr) { | 22 if (vec == nullptr) { |
23 return nullptr; | 23 return nullptr; |
24 } | 24 } |
25 | 25 |
26 std::unique_ptr<std::vector<std::unique_ptr<T>>> result = | 26 std::unique_ptr<std::vector<std::unique_ptr<T>>> result = |
27 std::unique_ptr<std::vector<std::unique_ptr<T>>>( | 27 std::unique_ptr<std::vector<std::unique_ptr<T>>>( |
28 new std::vector<std::unique_ptr<T>>(vec->size())); | 28 new std::vector<std::unique_ptr<T>>(vec->size())); |
29 | 29 |
30 for (const std::unique_ptr<T>& t_ptr : *vec.get()) { | 30 for (const std::unique_ptr<T>& t_ptr : *vec.get()) { |
31 result->push_back(SafeClone(t_ptr)); | 31 result->push_back(SafeClone(t_ptr)); |
32 } | 32 } |
33 | 33 |
34 return result; | 34 return result; |
35 } | 35 } |
36 | 36 |
37 } // namespace media | 37 } // namespace media |
38 } // namespace mojo | 38 } // namespace mojo |
39 | 39 |
40 #endif // SERVICES_MEDIA_FRAMEWORK_SAFE_CLONE_H_ | 40 #endif // SERVICES_MEDIA_FRAMEWORK_SAFE_CLONE_H_ |
OLD | NEW |