Skip to content

Commit

Permalink
prepare for more smart pointer types (and smart pointers wrapped in p…
Browse files Browse the repository at this point in the history
…ropagate_const)
  • Loading branch information
ehren committed Feb 13, 2025
1 parent 64749c9 commit 165500d
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions include/ceto.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,40 @@
#define CETO_SOURCE_LOC_ARG
#endif

#ifdef CETO_USING_GODBOLT
// also remember to manually: #include "https://raw.githubusercontent.com/ehren/ceto/refs/heads/main/include/propagate_const_copyable.h"
namespace ceto {
typedef local_shared_ptr std::shared_ptr;
}
#else
#include "propagate_const_copyable.h"
#include "kit_local_shared_ptr/smart_ptr.hpp"
#endif

namespace ceto {

template <typename T>
concept is_basic_smart_ptr = std::same_as<T, std::shared_ptr<typename T::value_type>> || std::same_as<T, std::weak_ptr<typename T::value_type>> || std::same_as<T, std::unique_ptr<typename T::value_type>> || std::same_as<T, ceto::local_shared_ptr<typename T::value_type>>;

template <typename T>
concept is_smart_ptr = is_basic_smart_ptr<T> || (ceto::propagate_const::is_propagate_const<T> && is_basic_smart_ptr<decltype(ceto::get_underlying(std::declval<T>()))>)

template <typename T>
concept is_optional = std::same_as<T, std::optional<typename T::value_type>>;

template<typename T>
concept is_dereferencable = requires (T t) {
*t;
};

template<typename T>
concept is_raw_dereferencable = is_dereferencable<T> && !is_optional<T> && !is_smart_ptr<T>;

} // namespace ceto

// this works but disable until unsafe blocks fully implemented
//#define CETO_BAN_RAW_DEREFERENCABLE(expr) [&]() -> decltype(auto) { static_assert(!ceto::is_raw_dereferencable<std::remove_cvref_t<decltype(expr)>>); return expr; }()
#define CETO_BAN_RAW_DEREFERENCABLE(expr) (expr)

struct object {
};
Expand Down Expand Up @@ -339,30 +372,4 @@ inline constexpr bool is_non_aggregate_init_and_if_convertible_then_non_narrowin
(!std::is_convertible_v<From, To> ||
is_convertible_without_narrowing_v<From, To>);

template<class T>
struct is_safe_dereferencable : std::false_type {};

template<class T>
struct is_safe_dereferencable<std::shared_ptr<T>> : std::true_type {};

template<class T>
struct is_safe_dereferencable<std::unique_ptr<T>> : std::true_type {};

template<class T>
struct is_safe_dereferencable<std::optional<T>> : std::true_type {};

template<typename T>
concept is_dereferencable = requires (T t) {
*t;
};

template<typename T>
concept is_raw_dereferencable = is_dereferencable<T> && !is_safe_dereferencable<T>::value;

} // namespace ceto

// this works but disable until unsafe blocks implemented
//#define CETO_BAN_RAW_DEREFERENCABLE(expr) [&]() -> decltype(auto) { static_assert(!ceto::is_raw_dereferencable<std::remove_cvref_t<decltype(expr)>>); return expr; }()
#define CETO_BAN_RAW_DEREFERENCABLE(expr) (expr)

#endif // CETO_H

0 comments on commit 165500d

Please sign in to comment.