Click here to Skip to main content
Licence 
First Posted 16 Aug 2005
Views 25,715
Bookmarked 17 times

Automated Delegation

By | 16 Aug 2005 | Article
A technique for automated delegation and dynamic inheritance.

Introduction

Delegation, at least in the sense of this article, is a way to model a "is-implemented-in-terms-of" or IIITO object relationships between objects by forwarding functions to a member field. Typically delegation is achieved manually by explicitly writing out all of the functions. This is especially tedious for common interfaces.

Here is some source code which demonstrates how a class, FuBar, can forward the implementation of two separate interfaces, Fu and Bar, to two different fields, without introducing extra memory overhead.

#include <cstdlib>
#include <iostream>

using namespace std;

template<typename Derived, typename Base>
Derived* ToDerived(Base* x) {
  return static_cast<Derived*>(x);
}

/*
  interface Fu {
    int Fu();
  }
*/
template<typename Delegator>
struct FuDelegation {
  int Fu() {
    return 
      ToDerived<Delegator>(this)->Delegatee(this)->Fu();
  }
};

/*
  interface Bar {
    int Bar();
  }
*/
template<typename Delegator>
struct BarDelegation {
  int Bar() {
    return 
      ToDerived<Delegator>(this)->Delegatee(this)->Bar();
  }
};

struct FuStruct {
  int Fu() { return 1; }
};

struct BarStruct {
  int Bar() { return 2; }
};

struct FuBar :
  FuDelegation<MyFuBar>, // no overhead
  BarDelegation<MyFuBar> // no overhead
{
  FuStruct* Delegatee(FuDelegation<MyFuBar>* x) 
    { return &fu; }
  BarStruct* Delegatee(BarDelegation<MyFuBar>* x) 
    { return &bar; }
  FuStruct fu;
  BarStruct bar;
};

int main() {
  MyFuBar fb;
  cout << fb.Fu() << endl;
  cout << fb.Bar() << endl;
  system("pause");
}

What is required by FuBar to achieve the automated delegation is to inherit the two delegator structs, FuDelegation and BarDelegation. Notice that if your compiler implements the Empty Base Optimization (EBO) then this introduces no extra memory requirements to the class FuBar. Next the FuBar is required to provide member functions with the signature T* Delegatee(DelegationType* x) for each of the delegations, where T is a type which implements the appropriate interface.

Automated delegation can be used to implement what is sometimes called "dynamic inheritance", because the pointer returned by the Delegatee() function can be determined at runtime. All other techniques for automated delegation I know of introduce the overhead of at least one pointer, which is not strictly speaking required.

I am going to leave it as an exercise to the reader to figure out why the code works. I plan on explaining it in more detail in a future article if there is interest.

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Christopher Diggins

Architect
Autodesk
Canada Canada

Member

Follow on Twitter Follow on Twitter
I've been programming personal computers since my I got my first Atari 400 in 1980. I currently work at Autodesk as an SDK specialist.
 
One of my passions is the design and implementation of programming languages. My current project is the Jigsaw library which includes a parsing engine and other utilities for implementing programming languages in C#.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMyFuBar PinsussGastonez22:41 23 Aug '05  
Sorry but what is MyFuBar?
 
Fabio
AnswerRe: MyFuBar PinmemberRainer Blome12:02 27 Oct '05  
GeneralToDerived Pinmemberpablo_mag8:37 17 Aug '05  
GeneralRe: ToDerived PinmemberChristopher Diggins8:54 17 Aug '05  
GeneralRe: ToDerived Pinmemberpablo_mag9:16 17 Aug '05  
GeneralRe: ToDerived PinmemberChristopher Diggins10:22 17 Aug '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 16 Aug 2005
Article Copyright 2005 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid