DART equivalent of multiple inheritance of pure virtual interfaces

75 Views Asked by At

In C++ I can do something like this:

class async_get_if
{
public:
  virtual void get( int & ) = 0;
};

class sync_get_if
{
public:
  bool empty() = 0;
  bool try_get( int &t ) = 0;
};

class async_put_if
{
public:
  virtual void put( const int & ) = 0;
};

class sync_put_if
{
public:
  bool full() = 0;
  bool try_put( const int &t ) = 0;
};

class get_if : public virtual async_get_if , public virtual sync_get_if
{
};

class put_if : public virtual async_put_if , public virtual sync_put_if
{
};

// the fifo implementation implements all four async/sync interfaces
// as well as get_if and put if

class fifo : public named_object , public virtual get_if , public virtual put_if
{
public: 
  fifo( const string &name , int size );

  void get( int & );
  bool empty();
  bool try_get( int &t );

  void put( const int & );
  bool full();
  bool try_put( const int &t )
};

// fifo method implementations omitted ...

In Dart, it's obvious enough how to do the async and sync interfaces - they would all be abstract interfaces - and how to do the fifo. But how would I do the intermediate get_if and put_if interfaces ? Is it even possible ?

PS as a partial reply to Robert Schwartz, the reason I want to do this is that I want to refer to the fifo using one of the six possible interfaces.

fifo f;

async_get_if &g1( f ); // fifo implements async_get_if
sync_get_if &g2( f );  // fifo implements sync_get_if
get_if &g3( f );       // fifo implements get_if

int get_data;

g1.get( get_data );
if( !g2.empty() ) g3.try_get( get_data );

and also:

// get_if knows it is composite of async_get_if and sync_get_if
async_get_if &g4( g3 );  

if( g4.empty() ) ...

If mixins help with this, it's not obvious to me how.

1

There are 1 best solutions below

0
mmcdon20 On

In Dart, it's obvious enough how to do the async and sync interfaces - they would all be abstract interfaces.

Like you said, you can define the sync and async classes as abstract interfaces.

abstract interface class AsyncGetIf {
  void get(int t);
}

abstract interface class SyncGetIf {
  bool empty();
  bool tryGet(int t);
}

abstract interface class AsyncPutIf {
  void put(int t);
}

abstract interface class SyncPutIf {
  bool full();
  bool tryPut(int t);
}

how would I do the intermediate get_if and put_if interfaces?

You could make them also abstract interfaces which implement the sync and async interfaces.

abstract interface class GetIf implements AsyncGetIf, SyncGetIf {}

abstract interface class PutIf implements AsyncPutIf, SyncPutIf {}

and how to do the fifo?

Define fifo as a class that implements the intermediate GetIf and PutIf interfaces.

class Fifo implements GetIf, PutIf {
  @override
  void get(int t) => false;

  @override
  bool empty() => false;

  @override
  bool tryGet(int t) => false;

  @override
  void put(int t) => false;

  @override
  bool full() => false;

  @override
  bool tryPut(int t) => false;
}

In the above code, fifo would conform to all 6 interfaces.