| OLD | NEW |
| (Empty) |
| 1 // Copyright 2007-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 // Helper functions to enable a useful pattern of using scoped pointers in | |
| 17 // which the ownership of the memory is transferred out to an empty | |
| 18 // scoped_ptr or scoped_array. | |
| 19 // | |
| 20 // The usage pattern is as follows: | |
| 21 // void foo(Bar** b); | |
| 22 // | |
| 23 // scoped_ptr<B> p; | |
| 24 // foo(address(p)); | |
| 25 // | |
| 26 // To receive the ownwership of the resource the scoped pointer must be | |
| 27 // empty, otherwise it will leak. | |
| 28 // | |
| 29 // As an implementation detail, the scoped pointers in "base/scoped_ptr.h" do | |
| 30 // not offer support for this idiom. The code below may break if the | |
| 31 // implementation of the scoped_ptr changes. The code works with the vast | |
| 32 // majority of the scoped_ptr implementations though. | |
| 33 // | |
| 34 // TODO(omaha): add unit tests. | |
| 35 | |
| 36 #ifndef OMAHA_COMMON_SCOPED_PTR_ADDRESS__ | |
| 37 #define OMAHA_COMMON_SCOPED_PTR_ADDRESS__ | |
| 38 | |
| 39 #include "base/basictypes.h" | |
| 40 #include "base/scoped_ptr.h" | |
| 41 #include "omaha/base/debug.h" | |
| 42 | |
| 43 template <typename T> | |
| 44 inline T** address(const scoped_ptr<T>& t) { | |
| 45 COMPILE_ASSERT(sizeof(T*) == sizeof(scoped_ptr<T>), types_do_not_match); | |
| 46 ASSERT1(!t.get()); | |
| 47 return reinterpret_cast<T**>(&const_cast<scoped_ptr<T>&>(t)); | |
| 48 } | |
| 49 | |
| 50 template <typename T> | |
| 51 inline T** address(const scoped_array<T>& t) { | |
| 52 COMPILE_ASSERT(sizeof(T*) == sizeof(scoped_ptr<T>), types_do_not_match); | |
| 53 ASSERT1(!t.get()); | |
| 54 return reinterpret_cast<T**>(&const_cast<scoped_array<T>&>(t)); | |
| 55 } | |
| 56 | |
| 57 #endif // OMAHA_COMMON_SCOPED_PTR_ADDRESS__ | |
| 58 | |
| OLD | NEW |