memory.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2017 The Abseil Authors.
  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. // https://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. // File: memory.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file contains utility functions for managing the creation and
  20. // conversion of smart pointers. This file is an extension to the C++
  21. // standard <memory> library header file.
  22. #ifndef CERES_PUBLIC_INTERNAL_MEMORY_H_
  23. #define CERES_PUBLIC_INTERNAL_MEMORY_H_
  24. #include <memory>
  25. #ifdef CERES_HAVE_EXCEPTIONS
  26. #define CERES_INTERNAL_TRY try
  27. #define CERES_INTERNAL_CATCH_ANY catch (...)
  28. #define CERES_INTERNAL_RETHROW \
  29. do { \
  30. throw; \
  31. } while (false)
  32. #else // CERES_HAVE_EXCEPTIONS
  33. #define CERES_INTERNAL_TRY if (true)
  34. #define CERES_INTERNAL_CATCH_ANY else if (false)
  35. #define CERES_INTERNAL_RETHROW \
  36. do { \
  37. } while (false)
  38. #endif // CERES_HAVE_EXCEPTIONS
  39. namespace ceres::internal {
  40. template <typename Allocator, typename Iterator, typename... Args>
  41. void ConstructRange(Allocator& alloc,
  42. Iterator first,
  43. Iterator last,
  44. const Args&... args) {
  45. for (Iterator cur = first; cur != last; ++cur) {
  46. CERES_INTERNAL_TRY {
  47. std::allocator_traits<Allocator>::construct(
  48. alloc, std::addressof(*cur), args...);
  49. }
  50. CERES_INTERNAL_CATCH_ANY {
  51. while (cur != first) {
  52. --cur;
  53. std::allocator_traits<Allocator>::destroy(alloc, std::addressof(*cur));
  54. }
  55. CERES_INTERNAL_RETHROW;
  56. }
  57. }
  58. }
  59. template <typename Allocator, typename Iterator, typename InputIterator>
  60. void CopyRange(Allocator& alloc,
  61. Iterator destination,
  62. InputIterator first,
  63. InputIterator last) {
  64. for (Iterator cur = destination; first != last;
  65. static_cast<void>(++cur), static_cast<void>(++first)) {
  66. CERES_INTERNAL_TRY {
  67. std::allocator_traits<Allocator>::construct(
  68. alloc, std::addressof(*cur), *first);
  69. }
  70. CERES_INTERNAL_CATCH_ANY {
  71. while (cur != destination) {
  72. --cur;
  73. std::allocator_traits<Allocator>::destroy(alloc, std::addressof(*cur));
  74. }
  75. CERES_INTERNAL_RETHROW;
  76. }
  77. }
  78. }
  79. } // namespace ceres::internal
  80. #endif // CERES_PUBLIC_INTERNAL_MEMORY_H_