新增:实现 EventQueue 类,单线程事件队列
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/build/libs/
|
||||||
|
/build/obj/
|
||||||
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"list": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
28
build/Ndk_Android.mk
Normal file
28
build/Ndk_Android.mk
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# 设置当前模块的源文件路径
|
||||||
|
LOCAL_PATH := $(call my-dir)
|
||||||
|
|
||||||
|
# 清除几乎所有 LOCAL_XXX 变量,为定义新模块做准备
|
||||||
|
include $(CLEAR_VARS)
|
||||||
|
|
||||||
|
# 模块名称,这将决定输出文件的名字(例如生成 libmynative.so)
|
||||||
|
LOCAL_MODULE := myapp
|
||||||
|
|
||||||
|
# 指定需要编译的源文件
|
||||||
|
LOCAL_SRC_FILES := \
|
||||||
|
../code/EventQueue.cpp ../code/main.cpp
|
||||||
|
|
||||||
|
# 指定头文件搜索路径
|
||||||
|
LOCAL_C_INCLUDES := \
|
||||||
|
$(LOCAL_PATH)/../code/
|
||||||
|
|
||||||
|
# 编译C文件时的标志
|
||||||
|
LOCAL_CFLAGS := -D__OS_ANDROID
|
||||||
|
|
||||||
|
# 编译C++文件时的标志
|
||||||
|
LOCAL_CPPFLAGS += -std=c++11 -android
|
||||||
|
|
||||||
|
# 链接时依赖的系统库
|
||||||
|
LOCAL_LDLIBS := -llog -lc
|
||||||
|
|
||||||
|
# 指示构建系统将其编译为动态链接库
|
||||||
|
include $(BUILD_EXECUTABLE)
|
||||||
4
build/Ndk_Application.mk
Normal file
4
build/Ndk_Application.mk
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
APP_STL:=gnustl_static
|
||||||
|
APP_ABI:=armeabi-v7a
|
||||||
|
APP_PLATFORM:=android-23
|
||||||
|
APP_BUILD_SCRIPT:=Ndk_Android.mk
|
||||||
99
build/ndk_make.sh
Executable file
99
build/ndk_make.sh
Executable file
@ -0,0 +1,99 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
source /opt/ndk/env/ndk_env.sh
|
||||||
|
|
||||||
|
# 定义颜色
|
||||||
|
RED="\033[0;31m"
|
||||||
|
GREEN="\033[0;32m"
|
||||||
|
YELLOW="\033[1;33m"
|
||||||
|
BLUE="\033[0;34m"
|
||||||
|
RESET="\033[0m"
|
||||||
|
|
||||||
|
# 路径
|
||||||
|
NDK_PROJECT_PATH=$(pwd)
|
||||||
|
APP_MK="./Ndk_Application.mk"
|
||||||
|
ANDROID_MK="./Ndk_Android.mk"
|
||||||
|
FIRMWARE_PATH="obj/local/armeabi-v7a/myapp"
|
||||||
|
DEVICE_DATA="/data/"
|
||||||
|
DEVICE_IP=""
|
||||||
|
|
||||||
|
clean() {
|
||||||
|
echo -e "${YELLOW}Cleaning up...${RESET}"
|
||||||
|
ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=$APP_MK APP_BUILD_SCRIPT=$ANDROID_MK clean
|
||||||
|
echo -e "${YELLOW}Removing obj/ and libs/ directories...${RESET}"
|
||||||
|
rm -rf obj/ libs/
|
||||||
|
sync
|
||||||
|
echo -e "${GREEN}Cleaned up.${RESET}"
|
||||||
|
}
|
||||||
|
|
||||||
|
build_only() {
|
||||||
|
echo -e "${BLUE}Building myapp...${RESET}"
|
||||||
|
ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=$APP_MK APP_BUILD_SCRIPT=$ANDROID_MK
|
||||||
|
echo -e "${GREEN}Only build completed successfully.${RESET}"
|
||||||
|
}
|
||||||
|
|
||||||
|
push_only() {
|
||||||
|
if [ -z "$DEVICE_IP" ]; then
|
||||||
|
echo -e "${RED}Error: No device IP provided. Use -p <IP>${RESET}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo -e "${BLUE}Connecting to device $DEVICE_IP...${RESET}"
|
||||||
|
adb connect $DEVICE_IP
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo -e "${RED}Failed to connect to device $DEVICE_IP${RESET}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${BLUE}Pushing myapp to $DEVICE_DATA...${RESET}"
|
||||||
|
adb -s $DEVICE_IP push $FIRMWARE_PATH $DEVICE_DATA
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo -e "${RED}Failed to push myapp to $DEVICE_DATA${RESET}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}Push completed successfully.${RESET}"
|
||||||
|
}
|
||||||
|
|
||||||
|
build_and_push() {
|
||||||
|
build_only
|
||||||
|
push_only
|
||||||
|
}
|
||||||
|
|
||||||
|
show_help() {
|
||||||
|
echo -e "${YELLOW}Usage: $0 [options]${RESET}"
|
||||||
|
echo
|
||||||
|
echo "Options:"
|
||||||
|
echo -e " ${BLUE}-c${RESET} Clean project (remove obj/ and libs/)"
|
||||||
|
echo -e " ${BLUE}-p <IP>${RESET} Push myapp to device with given IP"
|
||||||
|
echo -e " ${BLUE}-bp <IP>${RESET} Build and push to device with given IP"
|
||||||
|
echo -e " ${BLUE}-h${RESET} Show this help message"
|
||||||
|
echo
|
||||||
|
echo -e "${YELLOW}Default:${RESET} If no options are given, only build the project."
|
||||||
|
}
|
||||||
|
|
||||||
|
# 参数解析
|
||||||
|
if [ $# -eq 0 ]; then
|
||||||
|
build_only
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
-c)
|
||||||
|
clean
|
||||||
|
;;
|
||||||
|
-p)
|
||||||
|
DEVICE_IP="$2"
|
||||||
|
push_only
|
||||||
|
;;
|
||||||
|
-bp)
|
||||||
|
DEVICE_IP="$2"
|
||||||
|
build_and_push
|
||||||
|
;;
|
||||||
|
-h)
|
||||||
|
show_help
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
show_help
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
46
code/EventQueue.cpp
Normal file
46
code/EventQueue.cpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#include "EventQueue.h"
|
||||||
|
|
||||||
|
bool EventQueue::EventQueue_AddEvent(int tInputCmd, void *pInputParam)
|
||||||
|
{
|
||||||
|
cmdParam.push_back(pInputParam);
|
||||||
|
cmdList.push_back(tInputCmd);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EventQueue::EventQueue_AddEventToFront(int tInputCmd, void *pInputParam)
|
||||||
|
{
|
||||||
|
cmdParam.push_front(pInputParam);
|
||||||
|
cmdList.push_front(tInputCmd);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EventQueue::EventQueue_PeekEvent(int *pOutputCmd, void **ppOutputParam)
|
||||||
|
{
|
||||||
|
if (cmdList.empty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
(*pOutputCmd) = cmdList.front();
|
||||||
|
(*ppOutputParam) = cmdParam.front();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EventQueue::EventQueue_PopEvent(void)
|
||||||
|
{
|
||||||
|
if (!cmdList.empty())
|
||||||
|
{
|
||||||
|
cmdList.pop_front();
|
||||||
|
cmdParam.pop_front();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EventQueue::EventQueue_GetEvent(int *pOutputCmd, void **ppOutputParam)
|
||||||
|
{
|
||||||
|
if (cmdList.empty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
(*pOutputCmd) = cmdList.front();
|
||||||
|
(*ppOutputParam) = cmdParam.front();
|
||||||
|
cmdList.pop_front();
|
||||||
|
cmdParam.pop_front();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
23
code/EventQueue.h
Normal file
23
code/EventQueue.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef EVENTQUEUE_H
|
||||||
|
#define EVENTQUEUE_H
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
class EventQueue
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
bool EventQueue_AddEvent(int tInputCmd, void *pInputParam);
|
||||||
|
bool EventQueue_AddEventToFront(int tInputCmd, void *pInputParam);
|
||||||
|
bool EventQueue_PeekEvent(int *pOutputCmd, void **ppOutputParam);
|
||||||
|
void EventQueue_PopEvent(void);
|
||||||
|
bool EventQueue_GetEvent(int *pOutputCmd, void **ppOutputParam);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::list<int> cmdList;
|
||||||
|
std::list<void *> cmdParam;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
43
code/main.cpp
Normal file
43
code/main.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include "EventQueue.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
EventQueue queue;
|
||||||
|
|
||||||
|
/* 模拟事件数据 */
|
||||||
|
int cmd1 = 101;
|
||||||
|
std::string param1 = "事件一";
|
||||||
|
int cmd2 = 202;
|
||||||
|
std::string param2 = "事件二";
|
||||||
|
int cmd3 = 303;
|
||||||
|
std::string param3 = "事件三";
|
||||||
|
|
||||||
|
std::cout << "=== 添加事件 ===" << std::endl;
|
||||||
|
queue.EventQueue_AddEvent(cmd1, ¶m1); // 队尾
|
||||||
|
queue.EventQueue_AddEventToFront(cmd2, ¶m2); // 队头
|
||||||
|
queue.EventQueue_AddEvent(cmd3, ¶m3); // 队尾
|
||||||
|
|
||||||
|
/* 查看队头事件 */
|
||||||
|
int peekCmd;
|
||||||
|
void *peekParam;
|
||||||
|
if (queue.EventQueue_PeekEvent(&peekCmd, &peekParam))
|
||||||
|
{
|
||||||
|
std::cout << "[Peek] cmd=" << peekCmd
|
||||||
|
<< " param=" << *(std::string *)peekParam << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "=== 获取并移除事件 ===" << std::endl;
|
||||||
|
int getCmd;
|
||||||
|
void *getParam;
|
||||||
|
while (queue.EventQueue_GetEvent(&getCmd, &getParam))
|
||||||
|
{
|
||||||
|
std::cout << "[Get] cmd=" << getCmd
|
||||||
|
<< " param=" << *(std::string *)getParam << std::endl;
|
||||||
|
|
||||||
|
// queue.EventQueue_PopEvent(); // 队列中 Get 已经 pop 了,PopEvent 可省略
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "=== 队列已清空 ===" << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user