-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure class/structs name consistency, add more docs (#15)
- Loading branch information
Showing
21 changed files
with
468 additions
and
321 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,45 @@ | ||
#pragma once | ||
#include <memory> | ||
#include "base.hpp" | ||
#include "macros.hpp" | ||
#include <utility> | ||
|
||
#define CAT_(x, y) x##y | ||
#define CAT(x, y) CAT_(x, y) | ||
/// \brief A class that holds a callable object and executes it upon destruction. | ||
/// \tparam Callable The type of the callable object. | ||
template <typename Callable> | ||
class DeferHolder : public base::NonCopyable { | ||
public: | ||
/// \brief Constructor that takes a callable object. | ||
/// \param callable The callable object to be executed upon destruction. | ||
explicit DeferHolder(Callable&& callable) noexcept: callable_(std::forward<Callable>(callable)) { } | ||
|
||
#define defer auto CAT(_defer_instance_, __COUNTER__) = defer_::defer_{} % [&]() | ||
/// \brief Destructor that executes the stored callable object. | ||
~DeferHolder() { | ||
callable_(); | ||
} | ||
|
||
namespace defer_ { | ||
template <typename callable> | ||
struct type { | ||
callable cb; | ||
private: | ||
Callable callable_; ///< The stored callable object. | ||
}; | ||
|
||
explicit type(callable&& _cb): cb(std::forward<callable>(_cb)) { } | ||
/// \brief A helper class for creating deferred execution objects. | ||
class Defer : public base::NonCopyable { | ||
public: | ||
/// \brief Default constructor. | ||
constexpr Defer() noexcept = default; | ||
|
||
~type() { | ||
cb(); | ||
} | ||
}; | ||
/// \brief Default destructor. | ||
constexpr ~Defer() noexcept = default; | ||
|
||
struct defer_ { | ||
template <typename callable> | ||
type<callable> operator%(callable&& cb) { | ||
return type<callable>{std::forward<callable>(cb)}; | ||
} | ||
}; | ||
} // namespace defer_ | ||
/// \brief Overloaded modulus operator to create a DeferHolder object. | ||
/// \tparam Callable The type of the callable object. | ||
/// \param cb The callable object to be deferred. | ||
/// \return A DeferHolder object containing the callable. | ||
template <typename Callable> | ||
DeferHolder<Callable> operator%(Callable&& cb) { | ||
return DeferHolder<Callable>{std::forward<Callable>(cb)}; | ||
} | ||
}; | ||
|
||
/// \brief Create a deferred execution object. | ||
/// \note This macro creates a unique variable name for each use. | ||
#define defer auto COMMON_CAT(_defer_instance_, __LINE__) = Defer{} % [&]() |
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
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,7 @@ | ||
#pragma once | ||
|
||
/// \brief Concatenate two tokens. | ||
#define COMMON_CAT_(x, y) x##y | ||
|
||
/// \brief Concatenate two tokens (indirect). | ||
#define COMMON_CAT(x, y) COMMON_CAT_(x, y) |
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
Oops, something went wrong.