On Thu, 2003-08-21 at 05:45, Duane Wessels wrote:

>    - I haven't figured out the new magic OO way of doing cache_dir
>      options yet.

SwapDir::parseOptions(int) is the OOish parser driver.

it calls getOptionTree() to get a Composite that will either accept or
reject each value.

in the ufs-family of swapdir drivers, the following occurs on
getOptionTree:
the base getOptionTree is overriden by UFSSwapDir::getOptionTree().
UFSSwapDir::getOptionTree retrieves the base class options:
    SwapDirOption *parentResult = SwapDir::getOptionTree();
and then asks the io module - aufs,diskd, or ufs currently - for it's
options
    SwapDirOption *ioOptions = IO->getOptionTree();

if there are no IO module options, the base swapdir options are all
that's needed:
    if (!ioOptions)
        return parentResult;
otherwise we need to add concatenate the two option trees - we put them
in a SwapDirOptionVector, which will try to parse a symbol via each
element it has, only failing if all elements can't parse it.

    SwapDirOptionVector *result = new SwapDirOptionVector();
    result->options.push_back(parentResult);
    result->options.push_back(ioOptions);
    return result;

So, in Coss, you need to :
override SwapDir::getOptionTree.
create a SwapDirOptionVector and add the parent options, + your coss
specific options to it, and return that.

There is a helper template that allows a method pair on an object to be
handed to the parser as an option, so that you don't need lots of silly
bind-to-SwapDir and bind-to-IOModule mini-classes.

in SwapDir::getOptionTree:
    result->options.push_back(new
SwapDirOptionAdapter<SwapDir>(*const_cast<SwapDir *>(this),
&SwapDir::optionReadOnlyParse, &SwapDir::optionReadOnlyDump));

this creates a new option from the object 'this', and the two methods:
SwapDir::optionReadOnlyParse, and
SwapDir::optionReadOnlyDump.

To do a similar thing with a coss option, in the overridden
CossSwapDir::getOptionTree, you use do:
  new SwapDirOptionAdapter<CossSwapDir>(*const_cast<CossSwapDir
*>(this), &CossSwapDir::optionBlockSizeParse,
&CossSwapDir::optionBlockSizeDump)

Finally, the Parse and Dump routines called there are very straight
forward - using ReadOnly as an example:
void
SwapDir::optionReadOnlyDump(StoreEntry * e) const
{
    if (flags.read_only)
        storeAppendPrintf(e, " read-only");
}

That shouldn't need commentary :]. Other than noting that it's typesafe,
with no need for casts, or checking of void pointers etc.

Likewise, optionReadOnlyParse is trivial - if the option is "read-only".
try to parse the option. This has moved largely intact from it's
previous home in cache_cf.cc.
bool
SwapDir::optionReadOnlyParse(char const *option, const char *value, int
reconfiguring)
{
    if (strcmp(option, "read-only") != 0)
        return false;

    int read_only = 0;

    if (value)
        read_only = xatoi(value);
    else
        read_only = 1;

    flags.read_only = read_only;

    return true;
}

Hope that clears things up with the magic OO :}.

Cheers,
Rob
-- 
GPG key available at: <http://members.aardvark.net.au/lifeless/keys.txt>.

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to