| RDB 2

transactionScope.hpp
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright 2026 RIEGL Laser Measurement Systems
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * SPDX-License-Identifier: Apache-2.0
19 *
20 *******************************************************************************
21 */
22 /*!
23 *******************************************************************************
24 *
25 * \file transactionScope.hpp
26 * \author RIEGL LMS GmbH, Austria
27 * \brief Point cloud transaction scope helper class
28 * \version 2015-10-14/AW: Initial version
29 * \version 2017-11-24/AW: Constructors declared as "explicit" (#2825)
30 *
31 *******************************************************************************
32 */
33
34 #ifndef RIEGL_RDB_POINTCLOUD_TRANSACTIONSCOPE_HPP
35 #define RIEGL_RDB_POINTCLOUD_TRANSACTIONSCOPE_HPP
36
37 //---< INCLUDES >---------------------------------------------------------------
38
39 #include "riegl/rdb/pointcloud.hpp"
40 #include "riegl/rdb/pointcloud/transaction.hpp"
41
42 //---< NAMESPACE >--------------------------------------------------------------
43
44 namespace riegl {
45 namespace rdb {
46 namespace pointcloud {
47
48 //---< CLASS TransactionScope >-------------------------------------------------
49 /*!
50 * \brief Point cloud transaction scope helper class
51 *
52 * This class wraps transaction creation, commit and rollback. It automatically
53 * starts a new transaction on construction and either commits or reverts the
54 * transaction on deletion. Whether commit() or rollback() is called is defined
55 * by variable _autoCommit_, default is false (i.e. rollback).
56 *
57 * \see riegl::rdb::pointcloud::Transactions
58 */
59 class TransactionScope
60 {
61 public:
62 bool autoCommit; //!< true: call commit(), false: call rollback() on deletion
63
64 /*!
65 * \brief Begin new transaction
66 * \see riegl::rdb::pointcloud::Transactions::begin()
67 */
68 explicit TransactionScope(
69 riegl::rdb::Pointcloud &pointcloud, //!< [in] target point cloud object
70 const std::string &title, //!< [in] short description, e.g. "Import"
71 const std::string &agent, //!< [in] software name, e.g. "rdbimport v1.0"
72 const std::string &comments = "", //!< [in] e.g. process details for humans
73 const std::string &settings = "" //!< [in] e.g. process settings for software
74
75 ):
76 autoCommit(false),
77 target(pointcloud),
78 active(false),
79 tid (0)
80 {
81 tid = target.transaction().begin(title, agent, comments, settings);
82 active = true;
83 }
84
85 /*!
86 * \brief End transaction
87 */
88 ~TransactionScope()
89 {
90 if (autoCommit)
91 commit();
92 else rollback();
93 }
94
95 /*!
96 * \brief Get transaction ID
97 * \see riegl::rdb::pointcloud::Transaction::id
98 */
99 Transaction::ID id() const
100 {
101 return tid;
102 }
103
104 /*!
105 * \brief Commit transaction
106 * \see riegl::rdb::pointcloud::Transactions::commit()
107 */
108 void commit()
109 {
110 if (active)
111 {
112 target.transaction().commit();
113 active = false;
114 }
115 }
116 template <typename Callable>
117 void commit(Callable &&progress)
118 {
119 if (active)
120 {
121 target.transaction().commit(
122 std::forward<Callable>(progress)
123 );
124 active = false;
125 }
126 }
127 template <typename Callable, typename Receiver>
128 void commit(Callable &&progress, Receiver &&receiver)
129 {
130 if (active)
131 {
132 target.transaction().commit(
133 std::forward<Callable>(progress),
134 std::forward<Receiver>(receiver)
135 );
136 active = false;
137 }
138 }
139
140 /*!
141 * \brief Rollback transaction
142 * \see riegl::rdb::pointcloud::Transactions::rollback()
143 */
144 void rollback()
145 {
146 if (active)
147 {
148 target.transaction().rollback();
149 active = false;
150 }
151 }
152
153 private:
154 riegl::rdb::Pointcloud &target;
155 bool active;
156 Transaction::ID tid;
157 };
158
159 }}} // namespace riegl::rdb::pointcloud
160
161 #endif // RIEGL_RDB_POINTCLOUD_TRANSACTIONSCOPE_HPP
riegl::rdb::pointcloud::Transactions::commit
void commit(Progress progress=nullptr, void *userdata=nullptr, const std::uint32_t signature=0, const std::uint32_t key_size=0, const void *const key_data=nullptr)
Commit current transaction.
riegl::rdb::Pointcloud::transaction
pointcloud::Transactions & transaction()
Manage point cloud transactions.
riegl::rdb::pointcloud::TransactionScope::autoCommit
bool autoCommit
true: call commit(), false: call rollback() on deletion
Definition: transactionScope.hpp:62
pointcloud.hpp
Main point cloud database class.
riegl::rdb::pointcloud::TransactionScope::commit
void commit(Callable &&progress)
Definition: transactionScope.hpp:117
transaction.hpp
Point cloud transaction.
riegl::rdb::pointcloud::TransactionScope::commit
void commit(Callable &&progress, Receiver &&receiver)
Definition: transactionScope.hpp:128
riegl::rdb::pointcloud::Transactions::begin
Transaction::ID begin(const std::string &title, const std::string &agent, const std::string &comments="", const std::string &settings="")
Create new transaction.
riegl::rdb::Pointcloud
Main point cloud database class.
Definition: pointcloud.hpp:89
riegl::rdb::pointcloud::TransactionScope::~TransactionScope
~TransactionScope()
End transaction.
Definition: transactionScope.hpp:88
riegl
RIEGL Laser Measurement Systems GmbH, Austria.
Definition: context.hpp:48
riegl::rdb::pointcloud::TransactionScope::id
Transaction::ID id() const
Get transaction ID.
Definition: transactionScope.hpp:99
riegl::rdb::pointcloud::Transactions::rollback
void rollback()
Abort current transaction.
riegl::rdb::pointcloud::TransactionScope
Point cloud transaction scope helper class.
Definition: transactionScope.hpp:59
riegl::rdb::pointcloud::TransactionScope::TransactionScope
TransactionScope(riegl::rdb::Pointcloud &pointcloud, const std::string &title, const std::string &agent, const std::string &comments="", const std::string &settings="")
Begin new transaction.
Definition: transactionScope.hpp:68
riegl::rdb::pointcloud::Transaction::ID
std::uint32_t ID
Definition: transaction.hpp:60
riegl::rdb::pointcloud::TransactionScope::rollback
void rollback()
Rollback transaction.
Definition: transactionScope.hpp:144
riegl::rdb::pointcloud::TransactionScope::commit
void commit()
Commit transaction.
Definition: transactionScope.hpp:108