Android 13(T) - 常用的类型记录

阅读 101

2023-06-19

1 libutils

libutils代码位于 system/core/libutils 以及 system/core/libutils/include/utils目录下,封装了常用工具以及数据结构等,比如之前学习的智能指针类就出自于libutils

1.1 Mutex.h & Condition.h

libutils提供了封装的锁Mutex,内部的实现就是C语言中的pthread_mutex_tMutex可以搭配Condition以及Autolock来使用,更加方便。

1.2 CallStack.h

CallStack可以帮助我们打印函数的调用堆栈,这在我们追代码时会很有帮助!它被封装在libutilscallstack这个库中。

1.3 Errors.h

提供了常用返回值类型status_t,常用返回值OKNO_ERRORINVALID_OPERATION等等,以及返回值转字符串的方法statusToString

1.4 KeyedVector.h

键值对,现在建议使用std::map

1.5 List.h

链表,现在建议使用std::list<T>

1.6 NativeHandle.h

封装的native_handle

1.7 Singleton.h

单例模式基类,如果继承自该类则不应当暴露构造函数。目前不建议使用,建议单独实现getInstance方法。

1.8 SortedVector.h

顺序容器,建议使用std::set

1.9 String16.h

用于存储char16_t类型的字符串,提供了常用的字符串处理函数

1.10 String8.h

用于存储char类型的字符串,提供了常用的字符串处理函数

1.11 Thread.h

一个封装的线程库,现在建议使用std::thread

1.12 Vector.h

数组容器,目前建议使用std::vector

1.13 总结

看完了一圈常用类型,很多已经被弃用了,但是仍有好多非常有用的,比如CallStack等,不管有没有用了解下总是没错的,哈哈。


2 libstagefright_foundation

libstagefright_foundation是android media的基础库,里面有media相关的常用类型。这里主要记录AMessageAStringFileDescriptor

2.1 AMessage

AMessage在之前的异步消息机制中已经有过简单的介绍,这边要补充介绍两个方法:

#if !defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
static sp<AMessage> FromParcel(const Parcel &parcel,
								size_t maxNestingLevel = 255);
void writeToParcel(Parcel *parcel) const;
#endif // !defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)

根据宏我们可以知道,FromParcelwriteToParcel只能在system中使用,vendor下是无法使用的。

writeToParcel可以将AMessage中的基本数据(int32_t、int64_t、size_t、float、double,AString)以及嵌套的AMessage写入到Parcel当中。

FromParcel可以将Parcel中存储的数据转换为AMessage

这两个方法在后面的binder学习中可能会带来一些便利。

2.2 AString

AString 是 android封装的有一个字符串类型,可以兼容String8(提供了对应的构造函数),同样的也提供了AStringParcel相互转换的方法。

AString(const String8 &from);

#if !defined(__ANDROID_VNDK__) && !defined(__ANDROID_APEX__)
static AString FromParcel(const Parcel &parcel);
status_t writeToParcel(Parcel *parcel) const;
#endif

Parcel会在Binder相关章节学习,这边暂不了解。

精彩评论(0)

0 0 举报