Design discussion: inheritance + (template or parameters)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arch

    Design discussion: inheritance + (template or parameters)

    Hi,

    I am trying to implement a class hierarchy where there will be a base class
    defining the interface and some common operations (interface+impl entation):
    Constraints : no STL. This will be implemented for an ARM processor for an
    embedded application.

    class ActorA: public Actor;
    class ActorB: public Actor;
    class ActorC: public Actor;

    Actor {
    virtual void decide() = 0;
    void Preprocess();
    virtual void Process() = 0;
    }

    Decide() and Process() are interfaces defined by this base class.

    My problem is with the types associated with these actor objects. Internally
    all these classes act on a two dimensional array of values of size (X, Y)
    which will be configurable. SizeX and SizeY are in the order of 20-40. Each
    element of the array can be a single value, two values, or 7 values. .
    Specifically,

    ActorA will act on a 2-D array of single values
    ActorB will act on a 2-D array of doublets.
    ActorC will act on a 2-D array of 7-lets.

    The fact that you may have 1, 2, or 7 values only affect the decide()
    function. The process() functions acts on each of these values independently
    of the decision is made.

    Possibilities:
    1) Everything is a parameter
    Actor::Actor(Si zeX, SizeY, N) {
    // N : n-tuple
    pArray = new int(SizeX*SizeY *N);
    // Define access operators
    // etc.

    Actor will be general enough to do everything.
    ActorX will be using the access operators etc. from Actor class and just
    implement decide() and process().

    2) Define a template
    template <int SizeX, int SizeY, int N>
    class Actor {
    private:
    int[SizeX][SizeY][N];
    public:
    Actor() {}
    // etc.

    Alll other actor classes will be inherited from this template base class. i
    believe this is doable. No need to define access operators in this case.

    Those are the two ideas I have. Later there may be a need to define other
    Actors defining other decide/process pairs. As you realize, SizeX and Y are
    not known at compile time, but N is known. I would be happy to find a way to
    make the dependency on N specific to actors. Template based idea seems to do
    it, but I am not sure...

    Which one is better? Which is a better design? code size is a slightly
    lesser issue than speed.

    Thanks for the feedback.

    Arcin



  • Gianni Mariani

    #2
    Re: Design discussion: inheritance + (template or parameters)

    arch wrote:
    ....[color=blue]
    > The fact that you may have 1, 2, or 7 values only affect the decide()
    > function. The process() functions acts on each of these values independently
    > of the decision is made.
    >
    > Possibilities:
    > 1) Everything is a parameter
    > Actor::Actor(Si zeX, SizeY, N) {
    > // N : n-tuple
    > pArray = new int(SizeX*SizeY *N);
    > // Define access operators
    > // etc.
    >
    > Actor will be general enough to do everything.
    > ActorX will be using the access operators etc. from Actor class and just
    > implement decide() and process().
    >
    > 2) Define a template
    > template <int SizeX, int SizeY, int N>
    > class Actor {
    > private:
    > int[SizeX][SizeY][N];
    > public:
    > Actor() {}
    > // etc.
    >
    > Alll other actor classes will be inherited from this template base class. i
    > believe this is doable. No need to define access operators in this case.
    >
    > Those are the two ideas I have. Later there may be a need to define other
    > Actors defining other decide/process pairs. As you realize, SizeX and Y are
    > not known at compile time, but N is known.[/color]

    If SizeX and SizeY are not known at compile time then the preceeding
    template won't work for you. Template parameter values MUST BE KNOWN at
    compile time.

    I would be happy to find a way to[color=blue]
    > make the dependency on N specific to actors. Template based idea seems to do
    > it, but I am not sure...
    >
    > Which one is better? Which is a better design? code size is a slightly
    > lesser issue than speed.[/color]

    Another alternative:

    template <int N>
    class Actor {
    private:
    typedef int value_type[N];
    value_type * m_values;
    int m_xsize;
    int m_ysize;

    public:
    Actor(int SizeX, int SizeY)
    : values( new value_type[ SizeX * SizeY ] ),
    m_xsize( SizeX ),
    m_ysize( SizeY )
    {
    }
    };


    I really don't have enough to go on to help you but you may get a slight
    performance improvement by using the Actor<int> template because there
    is a constant multiply and hence the compliler may be able to produce
    some multiply optimizations.



    Comment

    • arch

      #3
      Re: Design discussion: inheritance + (template or parameters)


      "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
      news:bhm4cs$m4c @dispatch.conce ntric.net...[color=blue]
      > arch wrote:
      > ...[color=green]
      > > The fact that you may have 1, 2, or 7 values only affect the decide()
      > > function. The process() functions acts on each of these values[/color][/color]
      independently[color=blue][color=green]
      > > of the decision is made.
      > >
      > > Possibilities:
      > > 1) Everything is a parameter
      > > Actor::Actor(Si zeX, SizeY, N) {
      > > // N : n-tuple
      > > pArray = new int(SizeX*SizeY *N);
      > > // Define access operators
      > > // etc.
      > >
      > > Actor will be general enough to do everything.
      > > ActorX will be using the access operators etc. from Actor class and just
      > > implement decide() and process().
      > >
      > > 2) Define a template
      > > template <int SizeX, int SizeY, int N>
      > > class Actor {
      > > private:
      > > int[SizeX][SizeY][N];
      > > public:
      > > Actor() {}
      > > // etc.
      > >
      > > Alll other actor classes will be inherited from this template base[/color][/color]
      class. i[color=blue][color=green]
      > > believe this is doable. No need to define access operators in this case.
      > >
      > > Those are the two ideas I have. Later there may be a need to define[/color][/color]
      other[color=blue][color=green]
      > > Actors defining other decide/process pairs. As you realize, SizeX and Y[/color][/color]
      are[color=blue][color=green]
      > > not known at compile time, but N is known.[/color]
      >
      > If SizeX and SizeY are not known at compile time then the preceeding
      > template won't work for you. Template parameter values MUST BE KNOWN at
      > compile time.[/color]

      I thought I would be able to do it. Oh, what you are saying is that if I
      have
      SizeX = GetInput();
      SizeY=GetInput( );
      Actor<SizeX, SizeY, 7> a; // this will not work because SizeX and SizeY are
      not constants.
      [color=blue]
      > I would be happy to find a way to[color=green]
      > > make the dependency on N specific to actors. Template based idea seems[/color][/color]
      to do[color=blue][color=green]
      > > it, but I am not sure...
      > >
      > > Which one is better? Which is a better design? code size is a slightly
      > > lesser issue than speed.[/color]
      >
      > Another alternative:
      >
      > template <int N>
      > class Actor {
      > private:
      > typedef int value_type[N];
      > value_type * m_values;
      > int m_xsize;
      > int m_ysize;
      >
      > public:
      > Actor(int SizeX, int SizeY)
      > : values( new value_type[ SizeX * SizeY ] ),
      > m_xsize( SizeX ),
      > m_ysize( SizeY )
      > {
      > }
      > };[/color]

      I like your alternative in the sense that int[N] typedef makes it look like
      an internal type. And since I know the value of N for different actors, that
      works great. Since template is out of the picture, that is sth between a
      full template (which is not possible) and full 1-D array implementation.

      And this fits the objects in my domain a lot closer.

      Thank you.

      I am wondering: Is it worth defining classes for these different int[N]
      datatypes. OR Let's assume that these classes are created for other needs.
      In this case the template will turn into

      template <typename NewType>
      class Actor {
      private:
      NewType *mp_values;
      //etc...
      }

      Each NewType will include the appropriate int[N] private member where N is
      1,2,7, etc.

      it is not really look useful to create them unless they are created for
      other purposes. To me, even when these NewTypes are created, it may be
      simpler (=faster?) to stick to your definition of Actor.
      [color=blue]
      >
      > I really don't have enough to go on to help you but you may get a slight
      > performance improvement by using the Actor<int> template because there
      > is a constant multiply and hence the compliler may be able to produce
      > some multiply optimizations.
      >[/color]



      Comment

      Working...