On Tue, 10 Apr 2001, Allan Rae wrote:
> 2. Ideally I'd like to derive new policies from existing ones but how will
>    that affect the SMInput and State enums we currently use.  Can we
>    simply overload the enums and expect everything to work?
>       2a. I really don't want to have to add every conceivable state or
>           state input to those defined already in ButtonPolicy but it
>           may work out as the simplest way to extend policies.  I
>           _think_ an alternative might be to keep the existing enums,
>           create a new enum in each new policy that needs more states or
>           inputs, eg. enum MoreStateInputs {SMI_FIRSTNEW=SMI_TOTAL, ...};
>           but only pass ints as parameters into/out of BC/Policy. That
>           should allow the use of enums for our sanity.
>       2b. (2a) would affect the ability to switch to a new policy

Here's one idea for extending enums across classes.  Someone, somewhere
probably has a better idea.  I haven't gone looking.  Note the key is the
int parameter in groupy().  This could be used for the State, Group and
SMInput enums.  Of these only Group and SMInput are needed outside the
Policy hierarchy.  This is a little bit awkward because a change of policy
will require a s/MyPolicy/NewPolicy/g but worst of all it means we have to
use an int instead of an SMInput to hold a temporary return value.  Not
that big a loss and this could just be a "typedef int SMInput;" anyway
since the real name of the enum isn't significant for knowing an enums
value.  You just have to know which class it's in.  See the bottom part of
main() -- maybe this would just be more confusing but at least it would
have the names of the types matching even if we don't really have
typechecking in operation.

--------------------------------
#include <iostream>

class Policy {
 public:
   enum Group {
      one = 1, two, three, maxgroup
   };
};


class MyPolicy : public Policy {
 public:
   enum Group {
      four = maxgroup, five, six
   };

   int groupy(int grp) {
      if (one == grp) cout << "it's a one!\n";
      if (four == grp) cout << "it's a four!\n";
      return grp;
   }
};

int main()
{
   MyPolicy mp;
   cout << "one = " << mp.groupy(MyPolicy::one) << endl;
   cout << "two = " << mp.groupy(MyPolicy::two) << endl;
   cout << "three = " << mp.groupy(MyPolicy::three) << endl;
   cout << "four = " << mp.groupy(MyPolicy::four) << endl;
   cout << "five = " << mp.groupy(MyPolicy::five) << endl;
   cout << "six = " << mp.groupy(MyPolicy::six) << endl;

   typedef int Group;
   Group solo = MyPolicy::one;
   cout << "solo = " << mp.groupy(solo) << endl;
   return 0;
}
-------------------------

Allan. (ARRae)

Reply via email to