help to identify an appropriate design idions
If anyone want to tighten up the following idiom with constructive solutions
I would be happy for the comments. My need is for a robust flexible and
extensible high performance (ie no virtual functions) idiom that can allow
for a robust package with a series of (mathematical) interacting objects to
rewrite an exisitng and much too complex package. The difficulties arise
because there is a huge amount of shared functionality, and many types
demonstrating the same concept, and a multitude of finely graded concepts.
I expect/need it to compile in all major compilers and be standard
compliant. At the moment this, annoyingly, blocks some of the natural friend
based solutions (at least with my skill).
Typically an instance of a given object is defined by its state or data, and
certain core functionality that acesses that state, and then reuses a shared
(generic) functionality that is common to all instances matching a certain
concept which is then sealed with a wrapper. To make matters much trickier,
one wants to be able to move between the different views of the same
instance. Think
a point in R^n <-> data
+= , *= = <-> defining implementation
+ * <-> generic operations can be implemented once and
reused for any object with *= += etc.
+= *= + * <-> wrapper
together making it in instance of a vector
however it should be possible to downgrade to an additive group concept with
the same data; and instances of a matrix concept should downgrade to
instances of a ring concept. In the attached one can use the wrappers to
hide functionality. Code should not get repeated more than absolutely
neccesary.
This must have been done before.Thoughts and comments?
#include <iostream>
namespace WT2{
template <class T>
class assignable_wrap : protected T
{
protected:
typedef typename T::impl impl;
typedef typename impl::data data;
using T::assign;
public:
assignable_wrap& assign(const assignable_wrap& rhs)
{
return T::assign(rhs);
}
public:
const data& Data () const
{
return *this ;// protected inheritance so accessible from here
}
static assignable_wrap& Wrapper (data& rhs)
{
return static_cast< assignable_wrap& > (rhs);// protected inheritance so
castable from here
}
};
class assignable_generic{};
class assignable_data{};
class impl1: protected assignable_generic, protected assignable_data {
protected:
typedef WT2::assignable_data data;
typedef WT2::impl1 impl;
template <class OB>
OB& assign (const OB & rhs)
{
const data & data_rhs = rhs.Data();// OB -> data
data & data_lhs = *this; // impl -> data
data_lhs = data_rhs;
return OB::Wrapper(data_lhs); // data -> OB // data -> impl -> OB
}
};
}
}
int testW1()
{
using namespace WT2;
typedef comparable_wrap< comparable_impl > ob1;
{
ob1 a, b;
ob1 c = a.assign(b);
}
typedef comparable_wrap< ob1 > ob2;
{
ob2 a, b;
ob2 c = a.assign(b);
}
return true;
}
int main(int argc, char* argv[])
{
if (testW1())
std::cout << "all seems well" << std::endl;
return 0;
}
{ Please use proper indentation for readability of your code. -mod }
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]