]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: migrate from boost::variant to std::variant 62336/head
authorKefu Chai <tchaikov@gmail.com>
Mon, 17 Mar 2025 10:06:17 +0000 (18:06 +0800)
committerKefu Chai <tchaikov@gmail.com>
Tue, 18 Mar 2025 02:31:49 +0000 (10:31 +0800)
This change is part of a broader effort to reduce dependencies on
third-party libraries by leveraging C++ standard library alternatives.

Migrating from boost::variant to std::variant improves code readability
and maintainability while reducing external dependencies.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
13 files changed:
src/librbd/internal.cc
src/librbd/io/ImageDispatchSpec.h
src/librbd/io/ImageDispatcher.cc
src/test/librbd/crypto/luks/test_mock_FlattenRequest.cc
src/test/librbd/crypto/luks/test_mock_FormatRequest.cc
src/test/librbd/crypto/luks/test_mock_LoadRequest.cc
src/test/librbd/crypto/test_mock_FormatRequest.cc
src/test/librbd/crypto/test_mock_LoadRequest.cc
src/test/librbd/deep_copy/test_mock_ObjectCopyRequest.cc
src/test/librbd/exclusive_lock/test_mock_PreReleaseRequest.cc
src/test/librbd/image/test_mock_RefreshRequest.cc
src/test/librbd/io/test_mock_CopyupRequest.cc
src/test/librbd/operation/test_mock_ResizeRequest.cc

index f7adce4cd343e9447ba3e0abbd79fdc4b13c9322..5676631723291c73ebb917f27946c29bb7197669 100644 (file)
 #include "journal/Journaler.h"
 
 #include <boost/scope_exit.hpp>
-#include <boost/variant.hpp>
 #include "include/ceph_assert.h"
 
 #include <shared_mutex> // for std::shared_lock
+#include <variant>
 
 #define dout_subsys ceph_subsys_rbd
 #undef dout_prefix
@@ -275,7 +275,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
     return io_ctx.tmap_update(RBD_DIRECTORY, cmdbl);
   }
 
-  typedef boost::variant<std::string,uint64_t> image_option_value_t;
+  typedef std::variant<std::string, uint64_t> image_option_value_t;
   typedef std::map<int,image_option_value_t> image_options_t;
   typedef std::shared_ptr<image_options_t> image_options_ref;
 
@@ -433,7 +433,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
       return -ENOENT;
     }
 
-    *optval = boost::get<std::string>(j->second);
+    *optval = std::get<std::string>(j->second);
     return 0;
   }
 
@@ -454,7 +454,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
       return -ENOENT;
     }
 
-    *optval = boost::get<uint64_t>(j->second);
+    *optval = std::get<uint64_t>(j->second);
     return 0;
   }
 
index 9323f9879fe568665ff2fd98659ef2a3c3c8ffd6..ddda55c48e634a8a310664efbac81b7b1f679322 100644 (file)
@@ -11,8 +11,8 @@
 #include "librbd/io/AioCompletion.h"
 #include "librbd/io/Types.h"
 #include "librbd/io/ReadResult.h"
-#include <boost/variant/variant.hpp>
 #include <atomic>
+#include <variant>
 
 namespace librbd {
 
@@ -99,13 +99,13 @@ public:
     }
   };
 
-  typedef boost::variant<Read,
-                         Discard,
-                         Write,
-                         WriteSame,
-                         CompareAndWrite,
-                         Flush,
-                         ListSnaps> Request;
+  typedef std::variant<Read,
+                       Discard,
+                       Write,
+                       WriteSame,
+                       CompareAndWrite,
+                       Flush,
+                       ListSnaps> Request;
 
   C_Dispatcher dispatcher_ctx;
 
index e6b3878c101ce3a7d7fc31400355bccc2e2ac7ea..9bbd91ac9f0a3325014b80542372740e535defec 100644 (file)
@@ -14,7 +14,6 @@
 #include "librbd/io/RefreshImageDispatch.h"
 #include "librbd/io/Utils.h"
 #include "librbd/io/WriteBlockImageDispatch.h"
-#include <boost/variant.hpp>
 
 #include <shared_mutex> // for std::shared_lock
 
@@ -280,7 +279,7 @@ bool ImageDispatcher<I>::send_dispatch(
     }
   }
 
-  return boost::apply_visitor(
+  return std::visit(
     SendVisitor{image_dispatch, image_dispatch_spec},
     image_dispatch_spec->request);
 }
@@ -288,7 +287,7 @@ bool ImageDispatcher<I>::send_dispatch(
 template <typename I>
 bool ImageDispatcher<I>::preprocess(
     ImageDispatchSpec* image_dispatch_spec) {
-  return boost::apply_visitor(
+  return std::visit(
     PreprocessVisitor{this, image_dispatch_spec},
     image_dispatch_spec->request);
 }
index bc615bcf76e00dced7185bcdb676c682c5877634..1f8217eafe1c58b3389c080a9fcccbcf376ff5b3 100644 (file)
@@ -96,7 +96,7 @@ struct TestMockCryptoLuksFlattenRequest : public TestMockFixture {
     EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_))
             .WillOnce(Invoke([this, offset,
                               length](io::ImageDispatchSpec* spec) {
-                auto* read = boost::get<io::ImageDispatchSpec::Read>(
+                auto* read = std::get_if<io::ImageDispatchSpec::Read>(
                         &spec->request);
                 ASSERT_TRUE(read != nullptr);
 
@@ -122,7 +122,7 @@ struct TestMockCryptoLuksFlattenRequest : public TestMockFixture {
   void expect_image_write() {
     EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_))
             .WillOnce(Invoke([this](io::ImageDispatchSpec* spec) {
-                auto* write = boost::get<io::ImageDispatchSpec::Write>(
+                auto* write = std::get_if<io::ImageDispatchSpec::Write>(
                         &spec->request);
                 ASSERT_TRUE(write != nullptr);
 
@@ -145,7 +145,7 @@ struct TestMockCryptoLuksFlattenRequest : public TestMockFixture {
   void expect_image_flush(int r) {
     EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_)).WillOnce(
             Invoke([r](io::ImageDispatchSpec* spec) {
-              ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
+              ASSERT_TRUE(std::get_if<io::ImageDispatchSpec::Flush>(
                       &spec->request) != nullptr);
               spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
               spec->aio_comp->set_request_count(1);
index 86026b4567b649c036622ca6fdeb6310a2e5aa78..bc23182ad396ed45c160a9fbd0b4a37b1c3d587b 100644 (file)
@@ -66,7 +66,7 @@ struct TestMockCryptoLuksFormatRequest : public TestMockFixture {
   void expect_image_write() {
     EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_))
             .WillOnce(Invoke([this](io::ImageDispatchSpec* spec) {
-                auto* write = boost::get<io::ImageDispatchSpec::Write>(
+                auto* write = std::get_if<io::ImageDispatchSpec::Write>(
                         &spec->request);
                 ASSERT_TRUE(write != nullptr);
 
index 5fb566a07bb7458b6c8dbff83b16ca028a09d492..4871d08d80b44efa0b4dc827831e7dcb09a51898 100644 (file)
@@ -81,7 +81,7 @@ struct TestMockCryptoLuksLoadRequest : public TestMockFixture {
     EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_))
             .WillOnce(Invoke([this, offset,
                               length](io::ImageDispatchSpec* spec) {
-                auto* read = boost::get<io::ImageDispatchSpec::Read>(
+                auto* read = std::get_if<io::ImageDispatchSpec::Read>(
                         &spec->request);
                 ASSERT_TRUE(read != nullptr);
 
index 81b82429d7fddf0326f52d251eb426d55eaacf48..b8a58c77038b9f945b5d7e1ca7c1a72765029d38 100644 (file)
@@ -135,7 +135,7 @@ struct TestMockCryptoFormatRequest : public TestMockFixture {
   void expect_image_flush(int r) {
     EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_)).WillOnce(
             Invoke([r](io::ImageDispatchSpec* spec) {
-              ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
+              ASSERT_TRUE(std::get_if<io::ImageDispatchSpec::Flush>(
                       &spec->request) != nullptr);
               spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
               spec->aio_comp->set_request_count(1);
index 849710d827e83788358e55f033978506a6a31cdd..291495e95874d2282ed31b79d52fc97f3977c5ab 100644 (file)
@@ -119,7 +119,7 @@ struct TestMockCryptoLoadRequest : public TestMockFixture {
   void expect_image_flush(int r = 0) {
     EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_)).WillOnce(
             Invoke([r](io::ImageDispatchSpec* spec) {
-              ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
+              ASSERT_TRUE(std::get_if<io::ImageDispatchSpec::Flush>(
                       &spec->request) != nullptr);
               spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
               spec->aio_comp->set_request_count(1);
index 5fbb4d6ce5c0089e28caa84ff04c3892ff1449c9..d63cda5e5d85f437c86897bc0eee0c9e85910c5e 100644 (file)
@@ -119,12 +119,12 @@ void scribble(librbd::ImageCtx *image_ctx, int num_ops, size_t max_size,
 
 
 MATCHER(IsListSnaps, "") {
-  auto req = boost::get<io::ImageDispatchSpec::ListSnaps>(&arg->request);
+  auto req = std::get_if<io::ImageDispatchSpec::ListSnaps>(&arg->request);
   return (req != nullptr);
 }
 
 MATCHER_P2(IsRead, snap_id, image_interval, "") {
-  auto req = boost::get<io::ImageDispatchSpec::Read>(&arg->request);
+  auto req = std::get_if<io::ImageDispatchSpec::Read>(&arg->request);
   if (req == nullptr ||
       arg->io_context->get_read_snap() != snap_id) {
     return false;
index f37939cf5ba7ab30cf75130b3d112742e29628f3..320b20b811ca6ef56c735b962aafa549549613fe 100644 (file)
@@ -168,7 +168,7 @@ public:
   void expect_flush_io(MockTestImageCtx &mock_image_ctx, int r) {
     EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, send(_))
       .WillOnce(Invoke([&mock_image_ctx, r](io::ImageDispatchSpec* spec) {
-                  ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
+                  ASSERT_TRUE(std::get_if<io::ImageDispatchSpec::Flush>(
                     &spec->request) != nullptr);
                   spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
                   auto aio_comp = spec->aio_comp;
index e60409615ecd0a6d61c9df5cf5e4c4046914cc2e..eb3a91e07c8c1b66c4b5a743ebeb5b4c1609da0d 100644 (file)
@@ -513,7 +513,7 @@ public:
   void expect_image_flush(MockImageCtx &mock_image_ctx, int r) {
     EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, send(_))
       .WillOnce(Invoke([r](io::ImageDispatchSpec* spec) {
-                  ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
+                  ASSERT_TRUE(std::get_if<io::ImageDispatchSpec::Flush>(
                     &spec->request) != nullptr);
                   spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
                   spec->aio_comp->set_request_count(1);
index a4fe54af2117c18988eed0fd680d7e43c623e7d1..bc74dd26303b02c5336d16ece1459a9d26de1b73 100644 (file)
@@ -138,7 +138,7 @@ static bool operator==(const SnapContext& rhs, const SnapContext& lhs) {
 #include "librbd/io/CopyupRequest.cc"
 
 MATCHER_P(IsRead, image_extents, "") {
-  auto req = boost::get<librbd::io::ImageDispatchSpec::Read>(&arg->request);
+  auto req = std::get_if<librbd::io::ImageDispatchSpec::Read>(&arg->request);
   return (req != nullptr && image_extents == arg->image_extents);
 }
 
@@ -210,7 +210,7 @@ struct TestMockIoCopyupRequest : public TestMockFixture {
                 send(IsRead(image_extents)))
       .WillOnce(Invoke(
         [&mock_image_ctx, image_extents, data, r](io::ImageDispatchSpec* spec) {
-          auto req = boost::get<librbd::io::ImageDispatchSpec::Read>(
+          auto req = std::get_if<librbd::io::ImageDispatchSpec::Read>(
             &spec->request);
           ASSERT_TRUE(req != nullptr);
 
index b80ef20f0a49cd3a4db714251ae8f3006d3efd3d..481dffd937c63d1f8b7243f6332b6231025858a7 100644 (file)
@@ -129,7 +129,7 @@ public:
   void expect_flush_cache(MockImageCtx &mock_image_ctx, int r) {
     EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, send(_))
       .WillOnce(Invoke([&mock_image_ctx, r](io::ImageDispatchSpec* spec) {
-                  ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
+                  ASSERT_TRUE(std::get_if<io::ImageDispatchSpec::Flush>(
                     &spec->request) != nullptr);
                   spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
                   auto aio_comp = spec->aio_comp;