"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can You Improve Scripting Engine Efficiency with STL Maps for Function Pointer Management?

How Can You Improve Scripting Engine Efficiency with STL Maps for Function Pointer Management?

Published on 2024-11-03
Browse:921

How Can You Improve Scripting Engine Efficiency with STL Maps for Function Pointer Management?

Incorporating STL Map into a Scripting Engine for Function Pointer Storage

To enhance the efficiency of your scripting engine, consider leveraging an STL map for managing function pointers. This approach eliminates the need for lengthy conditional statements to invoke specific functions.

For this implementation, begin by declaring your function pointer type as a typedef for readability:

typedef void (*ScriptFunction)(void); // function pointer type

Next, define an unordered_map named script_map with string keys representing function names and ScriptFunction values for the corresponding pointer addresses:

typedef std::unordered_map<:string scriptfunction> script_map;

Example function registration:

void some_function() {}
script_map m;
m.emplace("blah", &some_function);

To call a function, define a call_script function:

void call_script(const std::string& pFunction) {
  auto iter = m.find(pFunction);
  if (iter == m.end()) {
    // function not found
  } else {
    (*iter->second)(); // invoke the function via the pointer
  }
}

Emphasize that you can generalize the ScriptFunction type to std::function* whatever*/> to cater to more than just bare function pointers.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3