Howdy. I'm writing a Globals class that stashes our app's globals into the
storage heap, and simply extends accessors and mutators to get at them.
Pretty straightforward stuff.
I'm not a C++ wizard, so I'd appreciate some tips for minimizing code
duplication for the accessors and mutators. Here's what I'm looking at, as
an example:
void SGlobals::Set_bGoingBack( Boolean bIn )
{
// Lock down the record handle:
SGlobalsStructPtr pGlob = (SGlobalsStructPtr) MemHandleLock(
m_recH );
ErrFatalDisplayIf ( !pGlob, "Unable to lock down globals record.");
Err err = DmWrite( pGlob,
offsetof(SGlobalsStruct,
bGoingBack),
&bIn,
sizeof( Boolean ) );
ErrFatalDisplayIf( err, "Unable to mutate global.");
// Unlock the record handle, return the value
MemHandleUnlock( m_recH );
}
Boolean SGlobals::Get_bGoingBack( void )
{
// Lock down the record handle:
SGlobalsStructPtr pGlob = (SGlobalsStructPtr) MemHandleLock(
m_recH );
ErrFatalDisplayIf ( !pGlob, "Unable to lock down globals record.");
Boolean bRet = pGlob->bGoingBack;
// Unlock the record handle, return the value
MemHandleUnlock( m_recH );
return bRet;
}
For each variable, I've cut-and-pasted the same function. Then I change the
method name, the type of the intermediate variable (bRet in this example),
the member (SGlobalsStruct.bGoingBack, in this case), and the return type of
the method. Whenever I end up cutting-and-pasting, a little voice in my
head says "you're not doing this the right way."
I understand the concept of templates (and have the references to get smart
on them real fast), but I'm not quite sure how to switch off of an
argument. For example, this would be ideal:
SGlobals.Set( offsetof(SGlobalsStruct, bGoingBack), (Boolean)true);
Boolean bTmp = SGlobals.Get( offsetof( SGlobalsStruct, bGoingBack));
All I can think of is a templatized second param and a big-ass switch
statement inside. Is there a Better Way? Is this a pipe dream, and I
should just cut-and-paste away and be done with it?
Thanks!
-Jeff Ishaq
The Windward Group