程序中是否有任何基于索引的表来存储可执行文件中每个函数的元数据?我需要将指针附加到给定的每个函数指针上;例如:

if (!HasMetadata(functionPointer)) //Something of the form ...(*)(...)
    SetMetadata(new FunctionMetadata()); //Pointer of object of some structure of data
((FunctionMetadata*)GetMetadata(functionPointer))->Counter++;


注意:我考虑使用键/值类型的对象;我不能,因为我有3000多个函数,所有这些函数都可能需要在表中。如果我没有3000多个函数,那么我会手动考虑为每个函数添加静态值。

最佳答案

C ++没有将固有的元数据附加到函数,类或实例。但是,有一些可用的库,经过一定的训练,可以使您向各种事物添加元数据。参见this stackoverflow问题。

为了您的目的,在函数指针及其元数据之间建立某种全局映射可能就足够了。例如,

// we'll use a generic function pointer as the key type for functions.  Note that things will
// be somewhat trickier should you want to work with virtual functions or instance
//members.

typedef void(*)() FunctionPtr;

static std::map<FunctionPtr, Metadata *> gFunctionMetadata;

Metadata *GetMetadata(FunctionPtr functionPtr){
   return gFunctionMetadata[functionPtr];
}


当然,更漂亮的解决方案是拥有一个单例类(MetadataManager或类似的类),该类包含地图并提供访问元数据的方法。

关于c++ - 如何将元数据附加到函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27204424/

10-14 15:20
查看更多