-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C++] Add RAII util (defer) to auto cleanup / close resources after e…
…xiting the scope (#369)
- Loading branch information
Showing
6 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright 2022 Lance Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
|
||
add_library(lance_util OBJECT defer.h defer.cc) | ||
|
||
add_lance_test(defer_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2022 Lance Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "lance/util/defer.h" | ||
|
||
#include <utility> | ||
|
||
namespace lance::util { | ||
|
||
Defer::Defer(std::function<void()>&& cb) : callback_(std::move(cb)) {} | ||
|
||
Defer::Defer(Defer&& other) : callback_(std::move(other.callback_)){}; | ||
|
||
Defer::~Defer() { | ||
callback_(); | ||
} | ||
|
||
} // namespace lance::util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2022 Lance Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
|
||
namespace lance::util { | ||
|
||
/// Defer the execution of a lambda function until out of scope. | ||
/// | ||
/// It offers RAII to general function calls. It is modeled after Golang's Defer statement. | ||
/// | ||
/// \example | ||
/// | ||
/// void WriteData(const std::string& path) { | ||
/// auto fd = fopen(path.c_str()); | ||
/// Defer auto_closer([&]() { fclose(fd); } | ||
/// for (int i = 0; i < 10; i++) { | ||
/// fprint(fd, "line: %d\n", i); | ||
/// } | ||
/// // fd is closed when out of scope. | ||
/// } | ||
/// | ||
class Defer { | ||
public: | ||
Defer() = delete; | ||
|
||
explicit Defer(std::function<void(void)>&& cb); | ||
|
||
/// Move constructor. | ||
Defer(Defer&& other); | ||
|
||
~Defer(); | ||
|
||
private: | ||
std::function<void()> callback_; | ||
}; | ||
|
||
} // namespace lance::util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2022 Lance Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "lance/util/defer.h" | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
TEST_CASE("Test Call Defer") { | ||
int i = 100; | ||
|
||
{ | ||
lance::util::Defer incr([&i]() { i += 200; }); | ||
|
||
CHECK(i == 100); | ||
i += 20; | ||
} | ||
|
||
CHECK(i == 320); | ||
} |