Easy object member access for C++

Under the hood, Copper is a rather complicated system. It has to be that way in order to ensure Copper is safe (no null pointer errors) within Copper code. However, on the C++ side, this makes it tricky to just access the members and data of a FunctionObject, the most important of all Copper objects. Now there’s an easy way.

While the underlying Copper code itself has not been made simpler, I’ve introduced a new helper class that handles the mysterious boilerplate code for accessing members. It’s called AccessHelper, found in CuAccessHelper.h in the stdlib folder. AccessHelper is a class that wraps a FunctionObject and allows for the easy getting and setting of members and data of the FunctionObject.

Consider for example the following Copper code:

a.b = 5

Very simple for Copper, but if you have a foreign function in C++ that is given FunctionObject “a”, the task for getting member “b” involved a number of steps. You really needed to dig into the engine code itself to see what you were doing. Now, I’ve done the work for you. Here’s an example foreign function that would extract “b”:

ForeignFunc::Result myff( FFIServices& ffi ) {
  if (!ffi.demandArgType(ObjectType::Function))
    return ForeignFunc::NONFATAL;
  FunctionObject& foo = (FunctionObject&)ffi.arg(0);
  AccessHelper helper(&foo);
  FunctionObject* member = helper.getMember("b");
  // ...
}

As you can see, getting member “b” is very easy with AccessHelper. Your code will be immensely shorter.

Notably, it is normally messy to set the return data of a function, but AccessHelper has functions for this too.

In short, AccessHelper is a very long-awaited tool that takes the pain out of accessing and setting Copper FunctionObject members.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s