Skip to content

feat(bindings/cpp): eliminate indirect pointers by manually managing memory #6147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions bindings/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,21 @@ set(CPP_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
${CARGO_TARGET_DIR}/cxxbridge
${CARGO_TARGET_DIR}/cxxbridge/opendal-cpp/src)

file(GLOB CPP_SOURCE_FILE "src/details/*.cpp")
file(GLOB CPP_HEADER_FILE "src/details/*.hpp")

list(APPEND CPP_SOURCE_FILE src/opendal.cpp)
list(APPEND CPP_HEADER_FILE include/opendal.hpp)
if (OPENDAL_ENABLE_ASYNC)
list(APPEND CPP_SOURCE_FILE src/opendal_async.cpp)
list(APPEND CPP_HEADER_FILE include/opendal_async.hpp)
file(GLOB CPP_SOURCE_FILE
"src/*.cpp"
"src/utils/*.cpp"
)
file(GLOB CPP_HEADER_FILE
"include/*.hpp"
"src/*.hpp"
"src/utils/*.hpp"
)
if (NOT OPENDAL_ENABLE_ASYNC)
file (GLOB ASYNC_SOURCE_FILE "src/*async*.cpp")
list(REMOVE_ITEM CPP_SOURCE_FILE ${ASYNC_SOURCE_FILE})

file(GLOB ASYNC_HEADER_FILE "include/*async*.hpp")
list(REMOVE_ITEM CPP_HEADER_FILE ${ASYNC_HEADER_FILE})
endif()

if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,47 @@

#pragma once

#include "lib.rs.h"
#include <cstdint>
#include <optional>
#include <string>

#include "boost/date_time/posix_time/ptime.hpp"

namespace opendal {

/**
* @class Lister
* @brief Lister is designed to list the entries of a directory.
* @details It provides next operation to get the next entry. You can also use
* it like an iterator.
* @code{.cpp}
* auto lister = operator.lister("dir/");
* for (const auto &entry : lister) {
* // Do something with entry
* }
* @endcode
* @enum EntryMode
* @brief The mode of the entry
*/
namespace opendal::details {
enum class EntryMode : int {
FILE = 1,
DIR = 2,
UNKNOWN = 0,
};

class Lister {
/**
* @struct Metadata
* @brief The metadata of a file or directory
*/
class Metadata {
public:
Lister(rust::Box<opendal::ffi::Lister> &&lister)
: raw_lister_{std::move(lister)} {}

Lister(Lister &&) = default;

~Lister() = default;

/**
* @brief Get the next entry of the lister
*
* @return The next entry of the lister
*/
ffi::OptionalEntry next() { return raw_lister_->next(); }
EntryMode type;
std::uint64_t content_length;
std::optional<std::string> cache_control;
std::optional<std::string> content_disposition;
std::optional<std::string> content_md5;
std::optional<std::string> content_type;
std::optional<std::string> etag;
std::optional<boost::posix_time::ptime> last_modified;
};

private:
rust::Box<opendal::ffi::Lister> raw_lister_;
/**
* @struct Entry
* @brief The entry of a file or directory
*/
class Entry {
public:
std::string path;
};

} // namespace opendal::details
} // namespace opendal
86 changes: 31 additions & 55 deletions bindings/cpp/include/opendal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,22 @@

#pragma once

#include <cstdint>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>

#include "boost/date_time/posix_time/ptime.hpp"
#include "boost/iostreams/concepts.hpp"
#include "boost/iostreams/stream.hpp"
#include "data_structure.hpp"

namespace opendal {

namespace details {
namespace ffi {
class Operator;
class Reader;
class Lister;
} // namespace details

/**
* @enum EntryMode
* @brief The mode of the entry
*/
enum class EntryMode : int {
FILE = 1,
DIR = 2,
UNKNOWN = 0,
};

/**
* @struct Metadata
* @brief The metadata of a file or directory
*/
struct Metadata {
EntryMode type;
std::uint64_t content_length;
std::optional<std::string> cache_control;
std::optional<std::string> content_disposition;
std::optional<std::string> content_md5;
std::optional<std::string> content_type;
std::optional<std::string> etag;
std::optional<boost::posix_time::ptime> last_modified;
};

/**
* @struct Entry
* @brief The entry of a file or directory
*/
struct Entry {
std::string path;
};
} // namespace ffi

class Reader;
class Lister;
Expand All @@ -79,7 +45,7 @@ class Lister;
*/
class Operator {
public:
Operator();
Operator() noexcept;

/**
* @brief Construct a new Operator object
Expand All @@ -95,10 +61,10 @@ class Operator {
Operator &operator=(const Operator &) = delete;

// Enable move
Operator(Operator &&);
Operator &operator=(Operator &&);
Operator(Operator &&other) noexcept;
Operator &operator=(Operator &&other) noexcept;

~Operator();
~Operator() noexcept;

/**
* @brief Check if the operator is available
Expand Down Expand Up @@ -200,7 +166,9 @@ class Operator {
Lister lister(std::string_view path);

private:
std::unique_ptr<details::Operator> operator_;
void destroy() noexcept;

ffi::Operator *operator_{nullptr};
};

/**
Expand All @@ -215,18 +183,22 @@ class Operator {
class Reader
: public boost::iostreams::device<boost::iostreams::input_seekable> {
public:
Reader(std::unique_ptr<details::Reader> &&reader);
Reader(Reader &&other) noexcept;

Reader(Reader &&);

~Reader();
~Reader() noexcept;

std::streamsize read(void *s, std::streamsize n);

std::streampos seek(std::streamoff off, std::ios_base::seekdir way);

private:
std::unique_ptr<details::Reader> raw_reader_;
friend class Operator;

Reader(ffi::Reader *pointer) noexcept;

void destroy() noexcept;

ffi::Reader *reader_{nullptr};
};

// Boost IOStreams requires it to be copyable. So we need to use
Expand Down Expand Up @@ -265,11 +237,9 @@ class ReaderStream
*/
class Lister {
public:
Lister(std::unique_ptr<details::Lister> lister);

Lister(Lister &&);
Lister(Lister &&other) noexcept;

~Lister();
~Lister() noexcept;

/**
* @class ListerIterator
Expand Down Expand Up @@ -305,13 +275,13 @@ class Lister {

protected:
// Only used for end iterator
Iterator(Lister &lister, bool /*end*/) : lister_(lister) {}
Iterator(Lister &lister, bool /*end*/) noexcept : lister_(lister) {}

private:
friend class Lister;

Lister &lister_;
std::optional<Entry> current_entry_;

friend class Lister;
};

/**
Expand All @@ -325,7 +295,13 @@ class Lister {
Iterator end() { return Iterator(*this, true); }

private:
std::unique_ptr<details::Lister> raw_lister_;
friend class Operator;

Lister(ffi::Lister *pointer) noexcept;

void destroy() noexcept;

ffi::Lister *lister_{nullptr};
};

} // namespace opendal
84 changes: 0 additions & 84 deletions bindings/cpp/src/details/operator.cpp

This file was deleted.

Loading
Loading