Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium 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 | |
| 5 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/numerics/safe_math.h" | |
| 11 #include "base/strings/string16.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 | |
| 14 namespace chrome_pdf { | |
| 15 | |
| 16 template <typename STRING_TYPE> | |
|
raymes
2015/01/15 04:33:24
nit: most template style I see in Chrome tends to
| |
| 17 PDFiumAPIStringBufferAdapter<STRING_TYPE>::PDFiumAPIStringBufferAdapter( | |
| 18 STRING_TYPE* str, | |
| 19 size_t expected_size, | |
| 20 bool check_expected_size) | |
| 21 : str_(str), | |
| 22 data_(WriteInto(str, expected_size + 1)), | |
| 23 expected_size_(expected_size), | |
| 24 check_expected_size_(check_expected_size), | |
| 25 is_closed_(false) { | |
| 26 } | |
| 27 | |
| 28 template <typename STRING_TYPE> | |
| 29 PDFiumAPIStringBufferAdapter<STRING_TYPE>::~PDFiumAPIStringBufferAdapter() { | |
| 30 DCHECK(is_closed_); | |
| 31 } | |
| 32 | |
| 33 template <typename STRING_TYPE> | |
| 34 void* PDFiumAPIStringBufferAdapter<STRING_TYPE>::GetData() { | |
| 35 DCHECK(!is_closed_); | |
| 36 return data_; | |
| 37 } | |
| 38 | |
| 39 template <typename STRING_TYPE> | |
| 40 void PDFiumAPIStringBufferAdapter<STRING_TYPE>::Close(int actual_size) { | |
| 41 base::CheckedNumeric<size_t> unsigned_size = actual_size; | |
| 42 Close(unsigned_size.ValueOrDie()); | |
| 43 } | |
| 44 | |
| 45 template <typename STRING_TYPE> | |
| 46 void PDFiumAPIStringBufferAdapter<STRING_TYPE>::Close(size_t actual_size) { | |
| 47 DCHECK(!is_closed_); | |
| 48 is_closed_ = true; | |
| 49 | |
| 50 if (check_expected_size_) | |
| 51 DCHECK_EQ(expected_size_, actual_size); | |
| 52 | |
| 53 if (actual_size > 0) { | |
| 54 DCHECK((*str_)[actual_size - 1] == 0); | |
| 55 str_->resize(actual_size - 1); | |
| 56 } else { | |
| 57 str_->clear(); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 // explicit instantiations | |
| 62 template class PDFiumAPIStringBufferAdapter<std::string>; | |
| 63 template class PDFiumAPIStringBufferAdapter<base::string16>; | |
| 64 | |
| 65 } // namespace chrome_pdf | |
| OLD | NEW |