CallBack function from Pointers on C

本文介绍了一个使用C语言实现的链表查找功能。通过定义一个Node结构体并利用回调函数进行节点比较,实现了对链表中特定值的搜索。文章提供了一个完整的示例程序,演示了如何创建链表、定义比较函数以及遍历链表来查找目标值。

/** * @file KWidget.h * @brief Declaration of the base GUI widget class * @author(ma.ji@kotei.com.cn) * @version 0.1 * @date 2025-06-16 * * @copyright Copyright (c) 2025 */ #pragma once #include <vector> #include <memory> #include "KGuiObject.h" const uint32_t kConstMaxScreenWidth = 2560U; const uint32_t kConstMaxScreenHeight = 1440U; /** * @enum eDrawMode * @brief Defines how the background image should be drawn. */ enum class eDrawMode { kOriginal, ///< Usesing the original size of the image to fill the widget. kStretch, ///< Stretch the image to fill the widget. }; /** * @enum eWindowStyle * @brief Enumerates different types of GUI window components. */ enum class eWindowStyle : uint32_t { kGuiWidget, ///< Generic container widget (e.g., KWidget) kGuiDialog, ///< modal dialog widget that blocks input to other widgets when shown kGuiCheckButton, ///< Clickable button (e.g., KCheckButton) kGuiToggleButton, ///< Clickable button (e.g., KToggleButton) kGuiExclusiveGroup, ///< Exclusive selection group (e.g., ExclusiveSelectionGroup) kGuiMultiLabel, ///< Label widget supporting multiple foreground images with different states kGuiComposite, ///< Composite window container with child management widget kEllipseTracker, ///< tracking along an ellipse widget kGuiCustom ///< User-defined window type }; /** * @class KWidget * @brief Base class for all user interface components * KWidget represents a rectangular area in a GUI system, handling user input, * rendering, and layout management. It implements parent-child relationships * and basic event handling mechanisms. * @note Derived classes must implement pure virtual functions for specific behavior. * @see none */ class KWidget : public KGuiObject { // Give an alias to the callback function that notify gui event using EventCallback = std::function<void(KGuiEvent *)>; public: /** * @brief Construct a widget with optional parent * @param parent Parent widget (nullptr for top-level) * @param style widget style * @return none * @note none * @see none */ explicit KWidget(KWidget* parent = nullptr, eWindowStyle style = eWindowStyle::kGuiWidget); /** * @brief Virtual destructor ensuring proper cleanup * @return none * @note Automatically removes itself from parent's children list * and deletes all child widgets * Do not use smart pointers when creating widget * @see none */ virtual ~KWidget(); // Prohibit copying KWidget(const KWidget&) = delete; KWidget& operator=(const KWidget&) = delete; public: /** * @brief Sets or updates the ID for widget * @param id Unique integer identifier for the window * @return none * @note none * @see none */ virtual void setWindowId(uint32_t id); /** * @brief Retrieves the ID of a specific window * @return Optional containing the window ID if found * @note none * @see setWindowId */ virtual uint32_t getWindowId() const; /** * @brief Sets theme definitions * @param style The style of theme enumeration value * @return none * @note none * @see none */ virtual void setSkinThemes(const uint32_t& style); /** * @brief Gets theme definitions * @param style The style of theme enumeration value * @return Number of the current loaded skin themes * @note none * @see none */ virtual uint32_t skinThemes() const; /** * @brief Register a event notify callback function. * @param callback The callback function that is finally executed. * @return none * @note none * @see none */ void registerEvent(EventCallback callback); /** * @brief Widget visibility state * @return Return the widget's visibility state. true if it is visible, otherwise return false. * @note none * @see none */ virtual bool isVisible() const override; /** * @brief Sets Widget visibility * @param visible widget visibility state * @return none * @note none * @see none */ virtual void setVisible(bool visible) override; /** * @brief Sets Widget show * @return none * @note none * @see none */ virtual void show() override; /** * @brief Sets Widget hide * @return none * @note none * @see none */ virtual void hide() override; /** * @brief Widget enabled state * @return Return the widget's enabled state. return true ff it is enabled, otherwise return false. * @note none * @see none */ virtual bool isEnabled() const override; /** * @brief Sets Widget enabled state * @return none * @note none * @see none */ virtual void setEnabled(bool enabled) override; /** * @brief Checks if point is within Widget boundaries * @param p Point to test * @return true if point is inside Widget * @note none * @see none */ virtual bool contains(const KPoint& p) const override; /** * @brief Process a GUI event in the current context. * @param e Pointer to the KGuiEvent object describing the event details. * The caller retains ownership of the event object. * @return true if the event was fully handled and should not propagate further, otherwise return false. * @note none * @see KGuiEvent */ virtual void onEvent(KGuiEvent *e) override; /** * @brief Set the widget's geometry * @param x Horizontal position relative to parent * @param y Vertical position relative to parent * @param width Width in pixels * @param height Height in pixels * @return none * @note none * @see none */ virtual void setGeometry(int x, int y, int width, int height) override; /** * @brief Move widget to new position * @param x New horizontal position * @param y New vertical position * @return none * @note none * @see none */ virtual void move(int x, int y); /** * @brief Resize widget dimensions * @param width New width in pixels * @param height New height in pixels * @return none * @note none * @see none */ virtual void resize(int width, int height); /** * @brief Get widget geometry parameters * @return KRectangle containing (x, y, width, height) * @note none * @see none */ virtual KRectangle geometry() const; /** * @brief Sets the background image id. * @param image id to the image. * @return none * @note none * @see none */ void setBackgroundImage(const uint32_t image); /** * @brief Sets the background draw mode. * @param mode Drawing mode (Original/Stretch). * @return none * @note none * @see none */ void setDrawMode(eDrawMode mode); /** * @brief Gets the current background draw mode. * @return Active draw mode. * @note none * @see none */ eDrawMode getDrawMode() const; /** * @brief Gets the current windows style. * @return window style mode. * @note none * @see eWindowStyle */ eWindowStyle getWindowStyle() const; public: /** * @brief Set parent widget * @param parent New parent widget (nullptr removes parent) * @return none * @note none * @see none */ void setParent(KWidget* parent); /** * @brief Requests that the child container capacity be at least enough to contain a specified number of elements. * @param n Minimum capacity requested (in terms of number of elements). * @return none * @note none * @see capacity(), shrink_to_fit() */ void setChildReserve(uint32_t n); /** * @brief Get current parent widget * @return Pointer to parent widget * @note none * @see none */ KWidget* parent() const noexcept; /** * @brief Get list of child widgets * @return Const reference to children collection * @note none * @see none */ const std::vector<KWidget*>& children() const noexcept; /** * @brief Adds a child widget to the current widget. * @param child The child widget to add. Must be a valid KWidget instance. * @return none * @note If the child already exists in the current widget, no action is taken. * @see none. */ void addChild(KWidget* childs); /** * @brief Removes a child widget from the current widget. * @param child The child widget to remove. * @return none * @note If the child is not found, no action is taken. * @see none */ void removeChild(KWidget* childs); /** * @brief Clear all child from the group * @return none * @note none * @see none */ void clearChildren(); /** * @brief Called when window needs repaint by outside * @param pen Affected screen pen * @return none * @note none * @see none */ virtual void paintEvent(KPen pen) override; /** * @brief Called on mouse press * @param x Mouse X coordinate * @param y Mouse Y coordinate * @return true if point is within geometry area, false otherwise * @note none * @see none */ virtual bool mousePressEvent(int x, int y) override; /** * @brief Called on mouse press * @param pos Mouse Pressed (X, Y)coordinate * @return true if point is within geometry area, false otherwise * @note none * @see none */ virtual bool mousePressEvent(const KPoint &pos) override; /** * @brief Called on mouse release * @param x Mouse X coordinate * @param y Mouse Y coordinate * @return true if point is within geometry area, false otherwise * @note none * @see none */ virtual bool mouseReleaseEvent(int x, int y) override; /** * @brief Called on mouse release * @param pos Mouse release (X, Y)coordinate * @return true if point is within geometry area, false otherwise * @note none * @see none */ virtual bool mouseReleaseEvent(const KPoint &pos) override; /** * @brief Called on mouse move * @param pos Mouse move (X, Y)coordinate * @return true if widget is currently in the state where the mouse press has been triggered, false otherwise * @note none * @see none */ virtual bool mouseMoveEvent(const KPoint &pos) override; /** * @brief Resize event handler * @param event Resize event * @return none * @note none * @see none */ virtual void resizeEvent(KGuiEvent* event) override; /** * @brief Check whether the widget is being pressed * @param event Resize event * @return Return true if is pressed, otherwise return false. * @note none * @see none */ inline bool isPressed() const { return m_pressed;} protected: /** * @brief Updates the layout of the widget and its children. * @return none * @note none. * @see none */ void updateGeometry(); protected: /** * @brief Called repaint by KWidget inside triggered. * @param pen Affected screen pen * @param image image resource id * @return none * @note none * @see none */ virtual void renderer(KPen pen, const uint32_t &image); private: bool m_visible; ///< Storge widget visibility property bool m_enabled; ///< Storge widget enable property KPoint m_position; ///< Widget position (top-left corner) KSize m_size; ///< Widget dimensions (width/height) #if defined(__QNXNTO__) IVSAVMViewPortAndViewInfo m_geometry; #endif protected: KWidget* m_pressedWidget; ///< Storge pressed child widget KWidget* m_parent; ///< Parent widget relationships std::vector<KWidget*> m_children; ///< child widget relationships eDrawMode m_draw_mode; ///< Storge image rendering mode eWindowStyle m_window_style; ///< Storge window style KSignalObject<KGuiEvent*> m_trigger; ///< Define widget base event handler EventCallback m_callback; ///< event notify callback function. uint32_t m_windowId; ///< Storge widget identifier ID uint32_t m_image; ///< Storge widget background image id. uint32_t m_skin_style; ///< Storge current skin type bool m_pressed; ///< Pressed state }; 写测试用例
08-20
/** * * Retrieve a burst of input packets from a receive queue of an Ethernet * device. The retrieved packets are stored in *rte_mbuf* structures whose * pointers are supplied in the *rx_pkts* array. * * The rte_eth_rx_burst() function loops, parsing the RX ring of the * receive queue, up to *nb_pkts* packets, and for each completed RX * descriptor in the ring, it performs the following operations: * * - Initialize the *rte_mbuf* data structure associated with the * RX descriptor according to the information provided by the NIC into * that RX descriptor. * * - Store the *rte_mbuf* data structure into the next entry of the * *rx_pkts* array. * * - Replenish the RX descriptor with a new *rte_mbuf* buffer * allocated from the memory pool associated with the receive queue at * initialization time. * * When retrieving an input packet that was scattered by the controller * into multiple receive descriptors, the rte_eth_rx_burst() function * appends the associated *rte_mbuf* buffers to the first buffer of the * packet. * * The rte_eth_rx_burst() function returns the number of packets * actually retrieved, which is the number of *rte_mbuf* data structures * effectively supplied into the *rx_pkts* array. * A return value equal to *nb_pkts* indicates that the RX queue contained * at least *rx_pkts* packets, and this is likely to signify that other * received packets remain in the input queue. Applications implementing * a "retrieve as much received packets as possible" policy can check this * specific case and keep invoking the rte_eth_rx_burst() function until * a value less than *nb_pkts* is returned. * * This receive method has the following advantages: * * - It allows a run-to-completion network stack engine to retrieve and * to immediately process received packets in a fast burst-oriented * approach, avoiding the overhead of unnecessary intermediate packet * queue/dequeue operations. * * - Conversely, it also allows an asynchronous-oriented processing * method to retrieve bursts of received packets and to immediately * queue them for further parallel processing by another logical core, * for instance. However, instead of having received packets being * individually queued by the driver, this approach allows the caller * of the rte_eth_rx_burst() function to queue a burst of retrieved * packets at a time and therefore dramatically reduce the cost of * enqueue/dequeue operations per packet. * * - It allows the rte_eth_rx_burst() function of the driver to take * advantage of burst-oriented hardware features (CPU cache, * prefetch instructions, and so on) to minimize the number of CPU * cycles per packet. * * To summarize, the proposed receive API enables many * burst-oriented optimizations in both synchronous and asynchronous * packet processing environments with no overhead in both cases. * * The rte_eth_rx_burst() function does not provide any error * notification to avoid the corresponding overhead. As a hint, the * upper-level application might check the status of the device link once * being systematically returned a 0 value for a given number of tries. * * @param port_id * The port identifier of the Ethernet device. * @param queue_id * The index of the receive queue from which to retrieve input packets. * The value must be in the range [0, nb_rx_queue - 1] previously supplied * to rte_eth_dev_configure(). * @param rx_pkts * The address of an array of pointers to *rte_mbuf* structures that * must be large enough to store *nb_pkts* pointers in it. * @param nb_pkts * The maximum number of packets to retrieve. * @return * The number of packets actually retrieved, which is the number * of pointers to *rte_mbuf* structures effectively supplied to the * *rx_pkts* array. */ static inline uint16_t rte_eth_rx_burst(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **rx_pkts, const uint16_t nb_pkts) { struct rte_eth_dev *dev = &rte_eth_devices[port_id]; uint16_t nb_rx; #ifdef RTE_LIBRTE_ETHDEV_DEBUG RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0); RTE_FUNC_PTR_OR_ERR_RET(*dev->rx_pkt_burst, 0); if (queue_id >= dev->data->nb_rx_queues) { RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id); return 0; } #endif nb_rx = (*dev->rx_pkt_burst)(dev->data->rx_queues[queue_id], rx_pkts, nb_pkts); #ifdef RTE_ETHDEV_RXTX_CALLBACKS struct rte_eth_rxtx_callback *cb; /* __ATOMIC_RELEASE memory order was used when the * call back was inserted into the list. * Since there is a clear dependency between loading * cb and cb->fn/cb->next, __ATOMIC_ACQUIRE memory order is * not required. */ cb = __atomic_load_n(&dev->post_rx_burst_cbs[queue_id], __ATOMIC_RELAXED); if (unlikely(cb != NULL)) { do { nb_rx = cb->fn.rx(port_id, queue_id, rx_pkts, nb_rx, nb_pkts, cb->param); cb = cb->next; } while (cb != NULL); } #endif return nb_rx; }解释
最新发布
11-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值