Przeglądaj źródła

signal: [[nodiscard]] (see #501)

Michele Caini 5 lat temu
rodzic
commit
5676e420a6

+ 8 - 8
src/entt/signal/delegate.hpp

@@ -47,7 +47,7 @@ using function_pointer_t = decltype(internal::function_pointer(std::declval<Type
 
 
 
 
 template<typename... Class, typename Ret, typename... Args>
 template<typename... Class, typename Ret, typename... Args>
-constexpr auto index_sequence_for(Ret(*)(Args...)) {
+[[nodiscard]] constexpr auto index_sequence_for(Ret(*)(Args...)) {
     return std::index_sequence_for<Class..., Args...>{};
     return std::index_sequence_for<Class..., Args...>{};
 }
 }
 
 
@@ -96,7 +96,7 @@ class delegate;
 template<typename Ret, typename... Args>
 template<typename Ret, typename... Args>
 class delegate<Ret(Args...)> {
 class delegate<Ret(Args...)> {
     template<auto Candidate, std::size_t... Index>
     template<auto Candidate, std::size_t... Index>
-    auto wrap(std::index_sequence<Index...>) ENTT_NOEXCEPT {
+    [[nodiscard]] auto wrap(std::index_sequence<Index...>) ENTT_NOEXCEPT {
         return [](const void *, Args... args) -> Ret {
         return [](const void *, Args... args) -> Ret {
             const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
             const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
             return Ret(std::invoke(Candidate, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(arguments))...));
             return Ret(std::invoke(Candidate, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(arguments))...));
@@ -104,7 +104,7 @@ class delegate<Ret(Args...)> {
     }
     }
 
 
     template<auto Candidate, typename Type, std::size_t... Index>
     template<auto Candidate, typename Type, std::size_t... Index>
-    auto wrap(Type &, std::index_sequence<Index...>) ENTT_NOEXCEPT {
+    [[nodiscard]] auto wrap(Type &, std::index_sequence<Index...>) ENTT_NOEXCEPT {
         return [](const void *payload, Args... args) -> Ret {
         return [](const void *payload, Args... args) -> Ret {
             const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
             const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
             Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
             Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
@@ -113,7 +113,7 @@ class delegate<Ret(Args...)> {
     }
     }
 
 
     template<auto Candidate, typename Type, std::size_t... Index>
     template<auto Candidate, typename Type, std::size_t... Index>
-    auto wrap(Type *, std::index_sequence<Index...>) ENTT_NOEXCEPT {
+    [[nodiscard]] auto wrap(Type *, std::index_sequence<Index...>) ENTT_NOEXCEPT {
         return [](const void *payload, Args... args) -> Ret {
         return [](const void *payload, Args... args) -> Ret {
             const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
             const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
             Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
             Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
@@ -276,7 +276,7 @@ public:
      * @brief Returns the instance or the payload linked to a delegate, if any.
      * @brief Returns the instance or the payload linked to a delegate, if any.
      * @return An opaque pointer to the underlying data.
      * @return An opaque pointer to the underlying data.
      */
      */
-    const void * instance() const ENTT_NOEXCEPT {
+    [[nodiscard]] const void * instance() const ENTT_NOEXCEPT {
         return data;
         return data;
     }
     }
 
 
@@ -303,7 +303,7 @@ public:
      * @brief Checks whether a delegate actually stores a listener.
      * @brief Checks whether a delegate actually stores a listener.
      * @return False if the delegate is empty, true otherwise.
      * @return False if the delegate is empty, true otherwise.
      */
      */
-    explicit operator bool() const ENTT_NOEXCEPT {
+    [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
         // no need to test also data
         // no need to test also data
         return !(fn == nullptr);
         return !(fn == nullptr);
     }
     }
@@ -313,7 +313,7 @@ public:
      * @param other Delegate with which to compare.
      * @param other Delegate with which to compare.
      * @return False if the two contents differ, true otherwise.
      * @return False if the two contents differ, true otherwise.
      */
      */
-    bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
+    [[nodiscard]] bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
         return fn == other.fn && data == other.data;
         return fn == other.fn && data == other.data;
     }
     }
 
 
@@ -332,7 +332,7 @@ private:
  * @return True if the two contents differ, false otherwise.
  * @return True if the two contents differ, false otherwise.
  */
  */
 template<typename Ret, typename... Args>
 template<typename Ret, typename... Args>
-bool operator!=(const delegate<Ret(Args...)> &lhs, const delegate<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
+[[nodiscard]] bool operator!=(const delegate<Ret(Args...)> &lhs, const delegate<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
     return !(lhs == rhs);
     return !(lhs == rhs);
 }
 }
 
 

+ 5 - 5
src/entt/signal/dispatcher.hpp

@@ -33,7 +33,7 @@ class dispatcher {
         virtual ~basic_pool() = default;
         virtual ~basic_pool() = default;
         virtual void publish() = 0;
         virtual void publish() = 0;
         virtual void clear() ENTT_NOEXCEPT = 0;
         virtual void clear() ENTT_NOEXCEPT = 0;
-        virtual id_type type_id() const ENTT_NOEXCEPT = 0;
+        [[nodiscard]] virtual id_type type_id() const ENTT_NOEXCEPT = 0;
     };
     };
 
 
     template<typename Event>
     template<typename Event>
@@ -55,7 +55,7 @@ class dispatcher {
             events.clear();
             events.clear();
         }
         }
 
 
-        sink_type sink() ENTT_NOEXCEPT {
+        [[nodiscard]] sink_type sink() ENTT_NOEXCEPT {
             return entt::sink{signal};
             return entt::sink{signal};
         }
         }
 
 
@@ -73,7 +73,7 @@ class dispatcher {
             }
             }
         }
         }
 
 
-        id_type type_id() const ENTT_NOEXCEPT override {
+        [[nodiscard]] id_type type_id() const ENTT_NOEXCEPT override {
             return type_info<Event>::id();
             return type_info<Event>::id();
         }
         }
 
 
@@ -83,7 +83,7 @@ class dispatcher {
     };
     };
 
 
     template<typename Event>
     template<typename Event>
-    pool_handler<Event> & assure() {
+    [[nodiscard]] pool_handler<Event> & assure() {
         static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
         static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
 
 
         if constexpr(ENTT_FAST_PATH(has_type_index_v<Event>)) {
         if constexpr(ENTT_FAST_PATH(has_type_index_v<Event>)) {
@@ -123,7 +123,7 @@ public:
      * @return A temporary sink object.
      * @return A temporary sink object.
      */
      */
     template<typename Event>
     template<typename Event>
-    auto sink() {
+    [[nodiscard]] auto sink() {
         return assure<Event>().sink();
         return assure<Event>().sink();
     }
     }
 
 

+ 6 - 6
src/entt/signal/emitter.hpp

@@ -55,7 +55,7 @@ class emitter {
         using container_type = std::list<element_type>;
         using container_type = std::list<element_type>;
         using connection_type = typename container_type::iterator;
         using connection_type = typename container_type::iterator;
 
 
-        bool empty() const ENTT_NOEXCEPT override {
+        [[nodiscard]] bool empty() const ENTT_NOEXCEPT override {
             auto pred = [](auto &&element) { return element.first; };
             auto pred = [](auto &&element) { return element.first; };
 
 
             return std::all_of(once_list.cbegin(), once_list.cend(), pred) &&
             return std::all_of(once_list.cbegin(), once_list.cend(), pred) &&
@@ -114,7 +114,7 @@ class emitter {
             on_list.remove_if([](auto &&element) { return element.first; });
             on_list.remove_if([](auto &&element) { return element.first; });
         }
         }
 
 
-        id_type type_id() const ENTT_NOEXCEPT override {
+        [[nodiscard]] id_type type_id() const ENTT_NOEXCEPT override {
             return type_info<Event>::id();
             return type_info<Event>::id();
         }
         }
 
 
@@ -125,7 +125,7 @@ class emitter {
     };
     };
 
 
     template<typename Event>
     template<typename Event>
-    const pool_handler<Event> & assure() const {
+    [[nodiscard]] const pool_handler<Event> & assure() const {
         static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
         static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
 
 
         if constexpr(ENTT_FAST_PATH(has_type_index_v<Event>)) {
         if constexpr(ENTT_FAST_PATH(has_type_index_v<Event>)) {
@@ -147,7 +147,7 @@ class emitter {
     }
     }
 
 
     template<typename Event>
     template<typename Event>
-    pool_handler<Event> & assure() {
+    [[nodiscard]] pool_handler<Event> & assure() {
         return const_cast<pool_handler<Event> &>(std::as_const(*this).template assure<Event>());
         return const_cast<pool_handler<Event> &>(std::as_const(*this).template assure<Event>());
     }
     }
 
 
@@ -309,7 +309,7 @@ public:
      * @return True if there are no listeners registered, false otherwise.
      * @return True if there are no listeners registered, false otherwise.
      */
      */
     template<typename Event>
     template<typename Event>
-    bool empty() const {
+    [[nodiscard]] bool empty() const {
         return assure<Event>().empty();
         return assure<Event>().empty();
     }
     }
 
 
@@ -317,7 +317,7 @@ public:
      * @brief Checks if there are listeners registered with the event emitter.
      * @brief Checks if there are listeners registered with the event emitter.
      * @return True if there are no listeners registered, false otherwise.
      * @return True if there are no listeners registered, false otherwise.
      */
      */
-    bool empty() const ENTT_NOEXCEPT {
+    [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
         return std::all_of(pools.cbegin(), pools.cend(), [](auto &&cpool) {
         return std::all_of(pools.cbegin(), pools.cend(), [](auto &&cpool) {
             return !cpool || cpool->empty();
             return !cpool || cpool->empty();
         });
         });

+ 10 - 10
src/entt/signal/sigh.hpp

@@ -77,7 +77,7 @@ public:
      * @brief Number of listeners connected to the signal.
      * @brief Number of listeners connected to the signal.
      * @return Number of listeners currently connected.
      * @return Number of listeners currently connected.
      */
      */
-    size_type size() const ENTT_NOEXCEPT {
+    [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
         return calls.size();
         return calls.size();
     }
     }
 
 
@@ -85,7 +85,7 @@ public:
      * @brief Returns false if at least a listener is connected to the signal.
      * @brief Returns false if at least a listener is connected to the signal.
      * @return True if the signal has no listeners connected, false otherwise.
      * @return True if the signal has no listeners connected, false otherwise.
      */
      */
-    bool empty() const ENTT_NOEXCEPT {
+    [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
         return calls.empty();
         return calls.empty();
     }
     }
 
 
@@ -166,7 +166,7 @@ public:
      * @brief Checks whether a connection is properly initialized.
      * @brief Checks whether a connection is properly initialized.
      * @return True if the connection is properly initialized, false otherwise.
      * @return True if the connection is properly initialized, false otherwise.
      */
      */
-    explicit operator bool() const ENTT_NOEXCEPT {
+    [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
         return static_cast<bool>(disconnect);
         return static_cast<bool>(disconnect);
     }
     }
 
 
@@ -233,7 +233,7 @@ struct scoped_connection {
      * @brief Checks whether a scoped connection is properly initialized.
      * @brief Checks whether a scoped connection is properly initialized.
      * @return True if the connection is properly initialized, false otherwise.
      * @return True if the connection is properly initialized, false otherwise.
      */
      */
-    explicit operator bool() const ENTT_NOEXCEPT {
+    [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
         return static_cast<bool>(conn);
         return static_cast<bool>(conn);
     }
     }
 
 
@@ -290,7 +290,7 @@ public:
      * @brief Returns false if at least a listener is connected to the sink.
      * @brief Returns false if at least a listener is connected to the sink.
      * @return True if the sink has no listeners connected, false otherwise.
      * @return True if the sink has no listeners connected, false otherwise.
      */
      */
-    bool empty() const ENTT_NOEXCEPT {
+    [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
         return signal->calls.empty();
         return signal->calls.empty();
     }
     }
 
 
@@ -301,7 +301,7 @@ public:
      * @return A properly initialized sink object.
      * @return A properly initialized sink object.
      */
      */
     template<auto Function>
     template<auto Function>
-    sink before() {
+    [[nodiscard]] sink before() {
         delegate<Ret(Args...)> call{};
         delegate<Ret(Args...)> call{};
         call.template connect<Function>();
         call.template connect<Function>();
 
 
@@ -322,7 +322,7 @@ public:
      * @return A properly initialized sink object.
      * @return A properly initialized sink object.
      */
      */
     template<auto Candidate, typename Type>
     template<auto Candidate, typename Type>
-    sink before(Type &&value_or_instance) {
+    [[nodiscard]] sink before(Type &&value_or_instance) {
         delegate<Ret(Args...)> call{};
         delegate<Ret(Args...)> call{};
         call.template connect<Candidate>(std::forward<Type>(value_or_instance));
         call.template connect<Candidate>(std::forward<Type>(value_or_instance));
 
 
@@ -342,7 +342,7 @@ public:
      * @return A properly initialized sink object.
      * @return A properly initialized sink object.
      */
      */
     template<typename Type>
     template<typename Type>
-    sink before(Type &value_or_instance) {
+    [[nodiscard]] sink before(Type &value_or_instance) {
         return before(&value_or_instance);
         return before(&value_or_instance);
     }
     }
 
 
@@ -354,7 +354,7 @@ public:
      * @return A properly initialized sink object.
      * @return A properly initialized sink object.
      */
      */
     template<typename Type>
     template<typename Type>
-    sink before(Type *value_or_instance) {
+    [[nodiscard]] sink before(Type *value_or_instance) {
         sink other{*this};
         sink other{*this};
 
 
         if(value_or_instance) {
         if(value_or_instance) {
@@ -373,7 +373,7 @@ public:
      * @brief Returns a sink that connects before anything else.
      * @brief Returns a sink that connects before anything else.
      * @return A properly initialized sink object.
      * @return A properly initialized sink object.
      */
      */
-    sink before() {
+    [[nodiscard]] sink before() {
         sink other{*this};
         sink other{*this};
         other.offset = signal->calls.size();
         other.offset = signal->calls.size();
         return other;
         return other;