Coder Social home page Coder Social logo

aloop's Introduction

轻量的c++ looper实现,移植自aosp的libstagefright。

特色

  1. 纯c++11实现,可在任意平台编译
  2. 只有一个头文件,一个源文件,容易集成
  3. 扩展性强

如何使用

编译

将src下的文件复制到你的工程中,编译时开启std=c++11支持即可。

g++ -std=c++11 main.cpp aloop.cpp -lpthread

概念说明

主要的类是ALooper, AHandler, AMessage。

ALooper封装了线程循环,已经进行事件分发;

AHandler需要用户继承,主要用来进行事件处理。一个ALooper可以注册多个AHandler复用线程资源。

AMessage代表一个消息,可附带各种类型的用户自定义数据,甚至附带一个函数。一般,一个AMessage只发往一个指定的AHandler处理。

代码调用

这里只演示基础用法,更高级的用法见example/main.cpp

启动Looper

auto looper = ALooper::create();
looper->start();

准备Handler

class MyHanlder: public AHandler{
protected:
    void onMessageReceived(const std::shared_ptr<AMessage> &msg){
        //在这里处理事件
    }
};

auto handler = std::shared_ptr<MyHandler>(new MyHandler);

looper->registerHandler(handler);

发送消息

auto msg = AMessage::create(1, handler);//1是自定义的整数消息类型
msg->setInt32("extraInt", 3);//使用setXXX函数设置附加数据
msg->post();//发送消息

目录说明

  • reference: 参考目录。源码来自aosp 6.0的android/frameworks/av/media/libstagefright。源码分析见:https://zhuanlan.zhihu.com/p/68713221
  • src: 源码目录。使用所需的所有文件
  • test: 测试代码
  • example: 实例代码

aloop's People

Contributors

compilelife avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.