g2o
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
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
23#ifndef G2O_CERES_PUBLIC_INTERNAL_MEMORY_H_
24#define G2O_CERES_PUBLIC_INTERNAL_MEMORY_H_
25
26#include <memory>
27
28#ifdef G2O_CERES_HAVE_EXCEPTIONS
29#define G2O_CERES_INTERNAL_TRY try
30#define G2O_CERES_INTERNAL_CATCH_ANY catch (...)
31#define G2O_CERES_INTERNAL_RETHROW \
32 do { \
33 throw; \
34 } while (false)
35#else // G2O_CERES_HAVE_EXCEPTIONS
36#define G2O_CERES_INTERNAL_TRY if (true)
37#define G2O_CERES_INTERNAL_CATCH_ANY else if (false)
38#define G2O_CERES_INTERNAL_RETHROW \
39 do { \
40 } while (false)
41#endif // G2O_CERES_HAVE_EXCEPTIONS
42
43namespace g2o {
44namespace ceres {
45namespace internal {
46
47template <typename Allocator, typename Iterator, typename... Args>
48void ConstructRange(Allocator& alloc, Iterator first, Iterator last,
49 const Args&... args) {
50 for (Iterator cur = first; cur != last; ++cur) {
52 std::allocator_traits<Allocator>::construct(alloc, std::addressof(*cur),
53 args...);
54 }
56 while (cur != first) {
57 --cur;
58 std::allocator_traits<Allocator>::destroy(alloc, std::addressof(*cur));
59 }
61 }
62 }
63}
64
65template <typename Allocator, typename Iterator, typename InputIterator>
66void CopyRange(Allocator& alloc, Iterator destination, InputIterator first,
67 InputIterator last) {
68 for (Iterator cur = destination; first != last;
69 static_cast<void>(++cur), static_cast<void>(++first)) {
71 std::allocator_traits<Allocator>::construct(alloc, std::addressof(*cur),
72 *first);
73 }
75 while (cur != destination) {
76 --cur;
77 std::allocator_traits<Allocator>::destroy(alloc, std::addressof(*cur));
78 }
80 }
81 }
82}
83
84} // namespace internal
85} // namespace ceres
86} // namespace g2o
87
88#endif // G2O_CERES_PUBLIC_INTERNAL_MEMORY_H_
#define G2O_CERES_INTERNAL_RETHROW
Definition memory.h:38
#define G2O_CERES_INTERNAL_TRY
Definition memory.h:36
#define G2O_CERES_INTERNAL_CATCH_ANY
Definition memory.h:37
void CopyRange(Allocator &alloc, Iterator destination, InputIterator first, InputIterator last)
Definition memory.h:66
void ConstructRange(Allocator &alloc, Iterator first, Iterator last, const Args &... args)
Definition memory.h:48