Little idiom: enhancing access control without dynamic polymorphism
Hi,
In this article, I want to show an idiom that addresses the problem of
providing different access to users without dynamic interfaces.
Problem
-------
The problem here described applies when different subsets of methods should be
accessible to different sets of users.
When dynamic polymorphism is available, access control to different set of
methods can be achieved by splitting the methods in different abstract classes
and implementing them all in a derived class, so users operate the base
classes and don't see other methods.
The problem the idiom addresses occurs when dynamic polymorphism is not
available due to embedded environments with tight resources so no virtual
tables and the number of indirections has to be reduced to the bare minimum.
Example
-------
Consider three operations: read, write and close. One might want to consider
two kind of users: the "writers" that can write and close, and the "readers"
that can read and close.
With dynamic polymorphism, this can be achieved as following:
struct WriterInterface
{
virtual void write() = 0;
virtual void close() = 0;
};
struct ReaderInterface
{
virtual void read() = 0;
virtual void close() = 0;
};
class MyClass : public WriterInterface, public ReaderInterface
{
virtual void read() { /* ... */ }
virtual void write() { /* ... */ }
virtual void close() { /* ... */ }
};
void writerUser(WriterInterface* wif);
Note that writeUser can only invoke the write and close operations.
How to achieve the same goal without dynamic polymorphism?
The proposed solution
---------------------
Rather than providing the operations as base classes and the implementation in
the derived class, the idiom proposes to do the contrary: specify the
implementation in the base class and the interfaces as derived classes with
protected inheritance containing the using declarations, plus a final class
closing the hierarchy and providing access to the various interfaces.
The code above can then be re-written as follows: (note that I use a few C++11
features but this can perfectly be written in previous C++ standard versions)
class MyClassBase
{
protected:
void read() { /* ... */ }
void write() { /* ... */ }
void close() { /* ... */ }
MyClassBase() = default;
};
class WriterInterface : protected MyClassBase
{
public:
using MyClassBase::write;
using MyClassBase::close;
protected:
WriterInterface() = default;
};
class ReaderInterface : protected WriterInterface
{
public:
using MyClassBase::read;
using MyClassBase::close;
protected:
ReaderInterface() = default;
};
class MyClass final : private ReaderInterface
{
public:
ReaderInterface& getReader() { return *this; }
WriterInterface& getWriter() { return *this; }
};
void writerUser(WriterInterface* wif); // unchanged
Note that writeUser will not be able to access other methods than those that
WriterInterface exposes, thus achieving the goal.
The only consideration and drawback is that automatic upcast is no longer
available, so a getter for each interface has to be coded in the final class.
Hope it will be useful.
Daniel.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]