Reputation: 6781
I am implementing an opaque data structure that is internally an array of opaque data structures. Let's call them records. There will be multiple queries to different fields of a record each with the same key. I have a list of APIs of this form:
QueryFieldX(KEY key, FieldX& Data)
Each query currently searches through the records with the key and then gets the Data.
I want to change it to the following form:
GetHandleToRecord(KEY key, MYHANDLE handle);
QueryFieldX(MYHANDLE handle, FieldX& Data);
The advantage is that searching every single time through the records is eliminated. I can implement a MYHANDLE myself but I want to know if there is a good library / data structure / primitive in C++ that gives me an opaque handle that I can use.
Upvotes: 1
Views: 970
Reputation: 63946
A handle is typically an obfuscated pointer to the data, such as this.
typedef void* MYHANDLE;
You would explicitly reinterpret_cast
the pointer as needed, such as in this code.
MYHANDLE GetHandleToRecord(KEY key)
{
FieldX *the_result;
// (assign the correct pointer to the_result)
return reinterpret_cast<MYHANDLE>( the_result );
}
QueryFieldX(MYHANDLE handle, FieldX& Data)
{
Data = *reinterpret_cast<FieldX*>( handle )
}
In your case, the handle could also be an array index, or a key to a std::map
. Anything that can later be converted back to the data itself.
Upvotes: 1