Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have the following interface:

struct IFilter
{
    virtual enterClass(ClassData&, int recursiveLevel) = 0;
    virtual enterStructure(ClassData&, int recursiveLevel) = 0;
    virtual enterVariable(ClassData&, int recursiveLevel) = 0;
    virtual enterFunction(ClassData&, int recursiveLevel) = 0;
};

And here is an example of an implementation:

struct ReadOnlyFilter: public IFilter
{
    virtual enterClass(ClassData& classData, int recursiveLevel)
    {
        if(recursiveLevel==0 || classData->isReadondOnly())
        {
            this.out << "Class " << classData->name() << "\n{";
        }
    }

So items can contain classes, structs, variables and functions. A class or struct can contain items as children and grandchildren and so on. The class ReadOnlyFilter is used by a function which will loop recursively over all of the children and grandchildren etc of an item. The recursiveLevel is incremented as we go deeper and deeper, like this:

Class c (recursiveLevel=0)
{
    Variable v1 (recursiveLevel=1)
    Class cChild (recursiveLevel=1)
    {
       Class cGrandChild (recursiveLevel=2)

All my implementations have one thing in common: they should always write to this.out if the recursiveLevel is zero. This is why I have if(recursiveLevel==0 || in my code. This results in ifs everywhere in my code.

To solve this, I was thinking of doing something like this instead and was wondering if you think this is a good solution:

(I'm using C++98 and Boost)

struct IFilter
{
    virtual enterClass(ClassData&, int recursiveLevel) = 0;
    virtual enterStructure(ClassData&, int recursiveLevel) = 0;
    virtual enterVariable(ClassData&, int recursiveLevel) = 0;
    virtual enterFunction(ClassData&, int recursiveLevel) = 0;

    virtual enterChildClass(ClassData&, int recursiveLevel) = 0;
    virtual enterChildStructure(ClassData&, int recursiveLevel) = 0;
    virtual enterChildVariable(ClassData&, int recursiveLevel) = 0;
    virtual enterChildFunction(ClassData&, int recursiveLevel) = 0;
};

struct BaseFilter: public IStyler
{
    virtual enterClass(ClassData& classData, int recursiveLevel)
    {
        out << "Class " << classData->name() << "\n{";
    }

struct ReadOnlyFilter: public BaseFilter
{
    virtual enterChildClass(ClassData& classData, int recursiveLevel)
    {
        if(classData->isReadondOnly())
        {
            enterClass(classData, recursiveLevel);
        }
    }
share|improve this question

1 Answer 1

I'm not sure what your question is about recursiveLevel, but printing only when recursiveLevel == 0 seems like pretty weird behaviour to me. If you only care about the outermost level of your data structure, then why even bother recursing?

I'm also puzzled by what you are trying to accomplish by introducing the enterChild...() methods. What does "child" mean — that the method should only be called when recursiveLevel > 0?

Or perhaps you mean to have ReadOnlyFilter::enterClass() chain to the superclass implementation?

struct ReadOnlyFilter: public BaseFilter {
    virtual enterClass(ClassData& classData, int recursiveLevel) {
        if (classData->isReadOnly()) {  // isReadondOnly() in original code
            BaseFilter::enterClass(classData, recursiveLevel);
        }
    }
    // etc.
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.