Incovenience of avoiding duplicate code due to an if statement c++11 - c++

I want to avoid duplicated code in this usecase
class A {
protected:
virtual void A1(const void* const s, const std::streamsize n) const;
inline void A2(const void* const s, const std::streamsize n) const;
};
class B : public A {
private:
const char *a;
void B1(const char *b) {
if (!b) {
return;
}
if (a < b) {
A1(a, b-a);
}
}
void B2(const char *b) {
if (!b) {
return;
}
if (a < b) {
A2(a, b-a);
};
}
};
So, as you can see above in both B1() and B2() there is duplicate code (that check for b) except for the call inside that if (note that the if condition is the same). I think this ifmakes somehow inconvenient to extract a new method, but also I think it can be done using lambdas and/or templates. There is no point of interest on how A1() and A2() are implemented for this usecase.
My question: What is the best and simplest way to avoid this duplication of code ?

You can write a function that accepts pointer to member to be executed
class B : public A {
private:
const char *a;
using F = void(A::*)(const void* const, const std::streamsize) const;
void RunFun(F f, const char *b) {
if (!b) {
return;
}
if (a < b) {
(this->*f)(a, b-a);
}
}
void B1(const char *b) {
RunFun(&B::A1,b);
}
void B2(const char *b) {
RunFun(&B::A2,b);
}
};

Another (simplified) example, using lambda and std::function
#include <cstring>
#include <iostream>
#include <functional>
struct A
{
virtual void A1 (char const * const b)
{ std::cout << b << "\n- A1 call" << std::endl; }
void A2 (char const * const b)
{ std::cout << b << "\n- A2 call" << std::endl; }
};
struct B : public A
{
const char * a;
std::function<void(char const * const, void(A::*)(char const * const))>
funcA { [this](char const * const b, void(A::*f)(char const * const))
{ if ( b && std::strlen(b) ) (this->*f)(b); } };
void B1 (char const * b)
{ funcA(b, &A::A1); }
void B2 (char const * b)
{ funcA(b, &A::A2); }
};
int main ()
{
B b;
b.B1("- B1 call");
b.B2("- B2 call");
}

Related

How can I decrease the number of overloaded functions

I want to know if there is an approach to decrease the number of overloaded function (function edit) in the below code.
class foo
{
public:
foo(int _a, char _b, float _c) : a(_a), b(_b), c(_c){};
void edit(int new_a);
void edit(char new_b);
void edit(float new_c);
void edit(int new_a, char new_b);
void edit(int new_a, float new_c);
void edit(char new_b, float new_c);
void edit(int new_a, char new_b, float new_c);
void info();
private:
int a;
char b;
float c;
};
Here is the implementation of the edit functions :
void foo::edit(int new_a)
{
a = new_a;
}
void foo::edit(char new_b)
{
b = new_b;
}
void foo::edit(float new_c)
{
c = new_c;
}
void foo::edit(int new_a, char new_b)
{
a = new_a;
b = new_b;
}
void foo::edit(int new_a, float new_c)
{
a = new_a;
c = new_c;
}
void foo::edit(char new_b, float new_c)
{
b = new_b;
c = new_c;
}
void foo::edit(int new_a, char new_b, float new_c)
{
a = new_a;
b = new_b;
c = new_c;
}
The edit function will let the user change the parameters of the object as he wishes.
But the thing is that if we add a new parameter we have to add to many overloaded function and I thought there should be a better way.
Here with 3 parameters we need 7 overloaded functions but if we had 4 parameters (a, b, c and d) then we had to develop 14 overloaded function!
That's why I think there should be a better approach.
Thanks.
With variadic and (ab)using std::get<T> on std::tuple, you might do
template <typename... Ts>
void edit(Ts... values)
{
((std::get<Ts&>(std::tie(a, b, c)) = std::get<Ts&>(std::tie(values...))), ...);
}
Demo.
Note: I use std::get<Ts&>(std::tie(values...)) instead of simply values to get error with duplicated input types(edit(42, 42);).
You can avoid the huge number of overloads and still allow the caller to set more than one member in a single expression:
class foo
{
public:
foo(int _a, char _b, float _c) : a(_a), b(_b), c(_c){};
foo& edit(int new_a) { a = new_a; return *this;}
foo& edit(char new_b) { b = new_b; return *this; }
foo& edit(float new_c) { c = new_c; return *this; }
private:
int a;
char b;
float c;
};
int main() {
foo f(1,'c',2.0);
f.edit(42).edit(42.0f).edit('a');
}
Adding a member requires you to write one overload rather than N to support all combinations.
The previous solutions are quite fine, but suppose that all elements have a different type.
A possibility is to still use a variadic template, and in the call to indicate with a string which element must be modified.
This would allow the possibility to have the same type for different elements.
#include <iostream>
#include <string>
class foo {
public:
foo(int _a, char _b, float _c) : a(_a), b(_b), c(_c){};
void edit() {};
template<typename T1, typename... T2>
void edit (const std::string& id, T1 var1, T2... var2) {
if (id == "a") a = var1;
else if (id == "b") b = var1;
else if (id == "c") c = var1;
edit(var2...);
};
void info();
//private:
int a;
char b;
float c;
};
std::ostream& operator<<(std::ostream& os, const foo& obj) {
std::cout << "a = " << obj.a << " b = " << obj.b << " c = " << obj.c;
return os;
}
int main() {
foo example(1, 'a', 2.0);
example.edit("c", 3.0f, "b", 'g', "a", 5);
std::cout << example << std::endl;
}
Given your edit functions that modify a single member:
void edit(int new_a)
{
a = new_a;
}
void edit(char new_b)
{
b = new_b;
}
void edit(float new_c)
{
c = new_c;
}
You can define a single function in C++11 using variadic templates to support multiple parameters in terms of multiple calls with a single parameter:
template< typename FirstType, typename ...OtherTypes >
void edit(FirstType ft, OtherTypes ...ot)
{
edit(ft);
edit(ot...);
}
Using C++17, fold expressions can make this function even simpler.
template< typename ...Types >
void edit(Types ...types)
{
(edit(types), ...);
}
Note: This solution will not try to prevent multiple changes to the same type, such as edit(1, 2, 3);

Architecture pattern: function to handle 2 identical implementations

I have the following problem which I guess I am solving incorrectly given the problem I am facing:
I have an interface I and implementations A, B, C... I want to somehow express that I can get some results from couples (f(A, A), f(B, B), f(C, C)) and so on. In other words, I want to interface I to express that 2 identical implementations can be combined to produce some results, while others can be not (you can't get any valid result from f(A, B)).
Right now I have the following:
#include <iostream>
using namespace std;
class A;
class B;
class I{
public:
virtual int f (const I &other) const = 0;
virtual int fSpecific (const A &other) const { throw runtime_error(""); };
virtual int fSpecific (const B &other) const { throw runtime_error(""); };
};
class A: public I{
public:
A(int a) : a(a) {}
int f (const I &other) const override { other.fSpecific(*this); }
int fSpecific (const A &other) const override { /*logic here*/ return a + other.a; }
int a;
};
class B: public I{
public:
B(int b1, int b2) : b1(b1), b2(b2) {}
int f (const I &other) const override { other.fSpecific(*this); }
int fSpecific (const B &other) const override { /*logic here*/ return b1*b1 + b2*b2 + other.b1*other.b1 + other.b2*other.b2; }
private:
int b1;
int b2;
};
int f(const I &a, const I &b) {
a.f(b);
}
int main()
{
cout << f(A(1), A(2)) << std::endl; // prints 3
cout << f(B(1, 2), B(3, 4)) << std::endl; // prints 30
cout << f(A(1), B(3, 4)) << std::endl; // throws an error
return 0;
}
/*and so on*/
But I guess I use a wrong architecture. as adding classes results in changing I. Are there any better solution to express this such a relation?
Your interface is indeed strange, asking for method which should not be implemented.
We don't have multiple dynamic dispatch, except with std::visit of std::variant.
So following might help:
using V = std::variant<A, B, C>;
int f(const V& v1, const V& v2) {
struct {
template <typename T1, typename T2>
int operator()(const T& t1, const T2& t2) const { throw runtime_error(""); };
int operator()(const A& a1, const A& a2) const { return a1.a + a2.a; };
int operator()(const B& b1, const B& b2) const { return b1.b1*b1.b1 + b1.b2*b1.b2 + b2.b1*b2.b1 + b2.b2*b2.b2; };
int operator()(const C& c1, const C& c2) const { return c1.c * c2.c; };
} visitor;
return std::visit(visitor, v1, v2);
}
or keeping you hierarchy:
using CV = std::variant<const A*, const B*, const C*>;
class I
{
public:
virtual ~I() = default;
virtual CV asVariant() const = 0;
};
class A: public I{
public:
A(int a) : a(a) {}
CV asVariant() const override { return this; }
friend int f (const A& a1, const A& a2) { /*logic here*/ return a1.a + a2.a; }
int a;
};
class B: public I{
public:
B(int b1, int b2) : b1(b1), b2(b2) {}
CV asVariant() const override { return this; }
friend int f (const B& b1, const B& b2) {
/*logic here*/ return b1.b1*b1.b1 + b1.b2*b1.b2 + b2.b1*b2.b1 + b2.b2*b2.b2;
}
private:
int b1;
int b2;
};
int f(const I& i1, const I& &2) {
struct {
template <typename T1, typename T2>
int operator()(const T1*, const T2*) const { throw runtime_error(""); };
template <typename T>
int operator()(const T* t1, const T* t2) const { return f(*t1, *t2); };
} visitor;
return std::visit(visitor, i1.AsVariant(), i2.AsVariant());
}
You may employ dynamic_cast:
class I {
public:
template<typename T>
void fSpecific (T &other) {
if (dynamic_cast<T*>(this))
std::cout << "OK" << std::endl;
else
std::cout << "ERROR" << std::endl;
}
virtual ~I() {}
};
class A : public I {
};
class B : public I {
};
int main()
{
A a;
a.fSpecific(a);
B b;
b.fSpecific(a);
b.fSpecific((I&)a);
return 0;
}
There are some problems however:
Multiple inheritance
Objects need to be dynamically castable (that is why I added a virtual interface)
Casting to I also works.

Two otherwise identical functions (one uses Template Pattern, other doesn't)

#include <iostream>
void doSomething (int x) {std::cout << "Do something with " << x << std::endl;}
struct Base {
virtual int foo() const {return 5;}
virtual int goo() const {return 6;}
virtual int hoo() const {return 7;}
void noTemplatePattern() const {
// Code A
if (Base::foo() < 6) {
// Code B
}
doSomething (Base::goo());
// Code C
if (Base::hoo() > 10) {
// Code D
}
}
void templatePattern() const {
// Code A
if (foo() < 6) {
// Code B
}
doSomething (goo());
// Code C
if (hoo() > 10) {
// Code D
}
}
};
struct Derived : Base {
virtual int foo() const override {return 12;}
virtual int goo() const override {return 13;}
virtual int hoo() const override {return 14;}
};
int main() {
Derived d;
d.noTemplatePattern();
d.templatePattern();
}
How best to avoid repeating the codes contained in Code A, Code B, Code C, Code D, etc... other than creating helper functions for each? Is there a more generic way? I have functions that are identical except one uses the Template Pattern, and the other doesn't. The body of code between the virtual functions are identical. If I define a helper function for each identical part, it gets really messy, and there will be too many of them too.
In case you want more clarification, here is a snippet of my production code illustrating this. SpellCaster is derived from LivingBeing, and LivingBeing::cannotAttackLongRange(int) is overridden by SpellCaster::cannotAttackLongRange(int).
inline std::set<LivingBeingProxy*> LivingBeing::unattackableTargets() const {
std::set<LivingBeingProxy*> nonTargets;
if (isCharmed()) {
for (auto it = std::next(getStatesList(CHARM_SPELL).begin(), 1); it != getStatesList(CHARM_SPELL).end(); ++it)
nonTargets.emplace (std::dynamic_pointer_cast<CharmedStateBase>(*it)->getCharmer());
}
for (LivingBeingProxy* x : getLocation()->allBeingsAlive()) {
if ( (x->heightAboveGround() > damageInflictor(0)->getReach()) && !canFly()
&& LivingBeing::cannotAttackLongRange(distanceBetween(this, x->getActual()))) //*** virtual method here!
{nonTargets.emplace(x); continue;}
if ( (x->heightAboveGround()) < 0 && (x->getTerrain() == InWater) && !canSwim() )
{nonTargets.emplace(x); continue;}
}
// ...
return nonTargets;
}
inline std::set<LivingBeingProxy*> LivingBeing::unattackableTargetsIncludingBySpells() const {
std::set<LivingBeingProxy*> nonTargets;
if (isCharmed()) {
for (auto it = std::next(getStatesList(CHARM_SPELL).begin(), 1); it != getStatesList(CHARM_SPELL).end(); ++it)
nonTargets.emplace (std::dynamic_pointer_cast<CharmedStateBase>(*it)->getCharmer());
}
for (LivingBeingProxy* x : getLocation()->allBeingsAlive()) {
if ( (x->heightAboveGround() > damageInflictor(0)->getReach()) && !canFly()
&& cannotAttackLongRange (distanceBetween(this, x->getActual()))) //*** virtual method here!
{nonTargets.emplace(x); continue;}
if ( (x->heightAboveGround()) < 0 && (x->getTerrain() == InWater) && !canSwim() )
{nonTargets.emplace(x); continue;}
}
// ...
return nonTargets;
}
LivingBeing::unattackableTargets() computes all enemies that are not attackable by ordinary weapons, while LivingBeing::unattackableTargetsIncludingBySpells() computes all enemies that are not attackable by ordinary weapons and spells. A SpellCaster will want to call the first when attacking with an ordinary weapon, and will want to call the second when attacking with a spell.
With template and CRTP, if appropriate, you may do something like:
template <typename T, typename D>
void helper(const D& base)
{
// Code A
if (base.T::foo() < 6) {
// Code B
}
doSomething (base.T::goo());
// Code C
if (base.T::hoo() > 10) {
// Code D
}
}
struct Base {
virtual ~Base() = default;
virtual int foo() const {return 5;}
virtual int goo() const {return 6;}
virtual int hoo() const {return 7;}
void noTemplatePattern() const
{
// use Base::foo, Base::goo and Base::hoo
helper<Base>(*this);
}
#if 0
virtual void templatePattern() const = 0;
#endif
};
template <typename Derived>
struct BaseImpl : Base {
template <typename Derived>
void BaseImpl<Derived>::templatePattern() const {
// use Derived::foo, Derived::goo and Derived::hoo
helper<Derived>(static_cast<const Derived&>(*this));
}
};
Live example
If templatePattern / noTemplatePattern are long and complicated and boo, goo and hoo are simple, you could do the following:
struct Base {
virtual int foo(bool = false) const {return 5;}
virtual int goo(bool = false) const {return 6;}
virtual int hoo(bool = false) const {return 7;}
void Pattern(bool base) const {
// Code A
if (foo(base) < 6) {
// Code B
}
doSomething (goo(base));
// Code C
if (hoo(base) > 10) {
// Code D
}
}
};
struct Derived : Base {
int foo(bool base = false) const override {return base ? Base::foo() : 12;}
int goo(bool base = false) const override {return base ? Base::goo() : 13;}
int hoo(bool base = false) const override {return base ? Base::hoo() : 14;}
};
int main() {
Derived d;
d.Pattern(true); // d.noTemplatePattern();
d.Pattern(false); // d.templatePattern();
}
Not exactly elegant, but might work in a particular situation.
NB: If you use override keyword, you don't need to repeat virtual keyword.
One solution with Tag dispatching (but require more code for foo, goo, hoo)
struct Base {
virtual int foo() const {return foo(std::false_type());}
virtual int goo() const {return goo(std::false_type());}
virtual int hoo() const {return hoo(std::false_type());}
void noTemplatePattern() const { doIt (std::false_type()); }
void templatePattern() const { doIt (std::true_type()); }
private:
template <typename T>
void doIt (T t) const {
// Code A
if (foo(t) < 6) {
// Code B
}
doSomething (goo(t));
// Code C
if (hoo(t) > 10) {
// Code D
}
}
// tag dispatching between virtual call and Base::call
int foo(std::false_type) const {return 5;}
int goo(std::false_type) const {return 6;}
int hoo(std::false_type) const {return 7;}
int foo(std::true_type) const {return foo();}
int goo(std::true_type) const {return goo();}
int hoo(std::true_type) const {return hoo();}
};
Live example
Ok, this is one solution I thought of, but though it works I don't know if it is considered any good (need some opinions). But at least all repeated code is avoided, and thus any changes to those parts needs only be made once:
#include <iostream>
void doSomething (int x) {std::cout << "Do something with " << x << std::endl;}
struct Base {
virtual int foo() const {return fooBase();}
virtual int goo() const {return gooBase();}
virtual int hoo() const {return hooBase();}
virtual void voidFunction() const {voidFunctionBase();}
void noTemplatePattern() const {
doIt (&Base::fooBase, &Base::gooBase, &Base::hooBase, &Base::voidFunctionBase);
}
void templatePattern() const {
doIt (&Base::foo, &Base::goo, &Base::hoo, &Base::voidFunction);
}
private:
void doIt (int(Base::*a)()const, int(Base::*b)()const, int(Base::*c)()const,
void(Base::*d)()const) const {
// Code A
if ((this->*a)() < 6) {
// Code B
}
doSomething((this->*b)());
// Code C
if ((this->*c)() > 10) {
// Code D
}
(this->*d)();
// Code E
}
int fooBase() const {return 5;}
int gooBase() const {return 6;}
int hooBase() const {return 7;}
void voidFunctionBase() const {std::cout << "Base::voidFunction() called.\n";}
};
struct Derived : Base {
virtual int foo() const override {return 12;}
virtual int goo() const override {return 13;}
virtual int hoo() const override {return 14;}
virtual void voidFunction() const override {std::cout << "Derived::voidFunction() called.\n";}
};
int main() {
Derived d;
d.noTemplatePattern();
d.templatePattern();
}
Output:
Do something with 6
Base::voidFunction() called.
Do something with 13
Derived::voidFunction() called.
Opinions? A better solution?
A possibility, if applicable, is to slice your object:
void noTemplatePattern() const {
// copy only Base part (slicing). Require that Base is constructible
Base(*this).templatePattern();
}
Live example
There is a pretty easy, no-nonsense solution for this: Just replace the two functions by one which takes a parameter bool includeSpells. Then you can just check for this parameter in the function and execute the appropriate function call. The rest of the function remains unchanged.

How can I avoid this code duplication?

I have two methods which have almost the same code except for two methods they call (and some other details I can easily parameterize). However, those method calls have the same signature, so I think I can generalize them into a single method.
class A{
IApi* m_pApi;
void M1();
void M2();
public:
void DoThings();
}
void A::M1(){
int i;
bool b;
m_pApi->method1( &i, &b );
//Other stuff...
}
void A::M2(){
int i;
bool b;
m_pApi->method2( &i, &b );
//Other stuff...
}
void A::DoThings(){
M1();
M2();
}
I can figure out how to "parameterize" the "Other stuff" code, but the problem are the calls to method1 and method2. I think I have to use std::bind somehow, but I can't do something like this...
void A::M( std::function<void(int*,bool*)> f ){
int i;
bool b;
f( &i, &b );
}
void A::DoThings(){
M( std::bind( ???, m_pApi ) ); //M1
M( std::bind( ???, m_pApi ) ); //M2
}
The problem here is that m_pApi is not a concrete class (it's an interface implemented by a bunch of concrete classes), so I'm not sure if I can do the usual &Class::Method thing. Suggestions?
Use pointers to member function.
#include <iostream>
using namespace std;
struct IApi {
void method1(int * i, bool * b) {
*i = 1; *b = true;
}
void method2(int * i, bool * b) {
*i = 2; *b = false;
}
};
class A {
IApi* m_pApi;
void M(void (IApi::*)(int*, bool*));
public:
A() : m_pApi(new IApi()) {}
void DoThings();
};
void A::M(void (IApi::*mptr)(int*, bool*)) {
int i;
bool b;
(m_pApi->*mptr)( &i, &b );
cout << i << ' ' << b << endl;
}
void A::DoThings(){
M(&IApi::method1);
M(&IApi::method2);
}
int main() {
A a;
a.DoThings();
}

Any suggestion for doing an arbitrary operation using given arguments of arbitrary types?

Basically i just want to do an arbitrary operation using given arguments of arbitrary types.
Argument type base class is Var, and Operation is base class of the operation that will executed for given arguments.
I have Evaluator class, that hold a collection of operators which mapped using opId. Evaluator will do operation based on opId argument given in evaluate() member function, then evaluate() function will do search for supported operator that will accept argument type and opId.
what I want to ask is, is there any efficient pattern or algorithm that will do this without dynamic_cast<> and/or looping through operator collection.
`
class Var {
public:
bool isValidVar();
static Var invalidVar();
}
template<typename T> class VarT : public Var {
public:
virtual const T getValue() const;
}
class Operator {
public:
virtual Var evaluate(const Var& a, const Var& b) = 0;
}
template<typename T> class AddOperator : public Operator {
public:
virtual Var evaluate(const Var& a, const Var& b)
{ //dynamic_cast is slow!
const VarT<T>* varA = dynamic_cast<const VarT<T>*>(&a);
const VarT<T>* varB = dynamic_cast<const VarT<T>*>(&b);
if(varA && varB) //operation supported
{
return VarT<T>(varA->getValue() + varA->getValue());
}
return Var::invalidVar(); //operation for this type is not supported
}
}
class Evaluator {
private:
std::map<int,std::vector<Operator>> operatorMap;
public:
virtual Var evaluate(const Var& a, const Var& b,int opId)
{
std::map<int,std::vector<Operator>>::iterator it = this->operatorMap.find(opId);
if(it != this->operatorMap.end())
{
for(size_t i=0 ; i<it->second.size() ; i++)
{
Var result = it->second.at(i).evaluate(a,b);
if(result.isValidVar())
{
return result;
}
}
}
//no operator mapped, or no operator support the type
return Var::invalidVar();
}
}
`
if you do not want to use dynamic_cast, consider adding type traits into your design.
Added 05/03/10 : The following sample will demonstrate how runtime-traits works
CommonHeader.h
#ifndef GENERIC_HEADER_INCLUDED
#define GENERIC_HEADER_INCLUDED
#include <map>
#include <vector>
#include <iostream>
// Default template
template <class T>
struct type_traits
{
static const int typeId = 0;
static const int getId() { return typeId; }
};
class Var
{
public:
virtual ~Var() {}
virtual int getType() const = 0;
virtual void print() const = 0;
};
template<typename T>
class VarT : public Var
{
T value;
public:
VarT(const T& v): value(v) {}
virtual int getType() const { return type_traits<T>::getId(); };
virtual void print() const { std::cout << value << std::endl; };
const T& getValue() const { return value; }
};
class Operator
{
public:
virtual ~Operator() {}
virtual Var* evaluate(const Var& a, const Var& b) const = 0;
};
template<typename T>
class AddOperator : public Operator
{
public:
virtual Var* evaluate(const Var& a, const Var& b) const
{
// Very basic condition guarding
// Allow operation within similar type only
// else have to create additional compatibility checker
// ie. AddOperator<Matrix> for Matrix & int
// it will also requires complicated value retrieving mechanism
// as static_cast no longer can be used due to unknown type.
if ( (a.getType() == b.getType()) &&
(a.getType() == type_traits<T>::getId()) &&
(b.getType() != type_traits<void>::getId()) )
{
const VarT<T>* varA = static_cast<const VarT<T>*>(&a);
const VarT<T>* varB = static_cast<const VarT<T>*>(&b);
return new VarT<T>(varA->getValue() + varB->getValue());
}
return 0;
}
};
class Evaluator {
private:
std::map<int, std::vector<Operator*>> operatorMap;
public:
void registerOperator(Operator* pOperator, int iCategory)
{
operatorMap[iCategory].push_back( pOperator );
}
virtual Var* evaluate(const Var& a, const Var& b, int opId)
{
Var* pResult = 0;
std::vector<Operator*>& opList = operatorMap.find(opId)->second;
for ( std::vector<Operator*>::const_iterator opIter = opList.begin();
opIter != opList.end();
opIter++ )
{
pResult = (*opIter)->evaluate( a, b );
if (pResult)
break;
}
return pResult;
}
};
#endif
DataProvider header
#ifdef OBJECTA_EXPORTS
#define OBJECTA_API __declspec(dllexport)
#else
#define OBJECTA_API __declspec(dllimport)
#endif
// This is the "common" header
#include "CommonHeader.h"
class CFraction
{
public:
CFraction(void);
CFraction(int iNum, int iDenom);
CFraction(const CFraction& src);
int m_iNum;
int m_iDenom;
};
extern "C" OBJECTA_API Operator* createOperator();
extern "C" OBJECTA_API Var* createVar();
DataProvider implementation
#include "Fraction.h"
// user-type specialization
template<>
struct type_traits<CFraction>
{
static const int typeId = 10;
static const int getId() { return typeId; }
};
std::ostream& operator<<(std::ostream& os, const CFraction& data)
{
return os << "Numerator : " << data.m_iNum << " # Denominator : " << data.m_iDenom << std::endl;
}
CFraction operator+(const CFraction& lhs, const CFraction& rhs)
{
CFraction obj;
obj.m_iNum = (lhs.m_iNum * rhs.m_iDenom) + (rhs.m_iNum * lhs.m_iDenom);
obj.m_iDenom = lhs.m_iDenom * rhs.m_iDenom;
return obj;
}
OBJECTA_API Operator* createOperator(void)
{
return new AddOperator<CFraction>;
}
OBJECTA_API Var* createVar(void)
{
return new VarT<CFraction>( CFraction(1,4) );
}
CFraction::CFraction() :
m_iNum (0),
m_iDenom (0)
{
}
CFraction::CFraction(int iNum, int iDenom) :
m_iNum (iNum),
m_iDenom (iDenom)
{
}
CFraction::CFraction(const CFraction& src) :
m_iNum (src.m_iNum),
m_iDenom (src.m_iDenom)
{
}
DataConsumer
#include "CommonHeader.h"
#include "windows.h"
// user-type specialization
template<>
struct type_traits<int>
{
static const int typeId = 1;
static const int getId() { return typeId; }
};
int main()
{
Evaluator e;
HMODULE hModuleA = LoadLibrary( "ObjectA.dll" );
if (hModuleA)
{
FARPROC pnProcOp = GetProcAddress(hModuleA, "createOperator");
FARPROC pnProcVar = GetProcAddress(hModuleA, "createVar");
// Prepare function pointer
typedef Operator* (*FACTORYOP)();
typedef Var* (*FACTORYVAR)();
FACTORYOP fnCreateOp = reinterpret_cast<FACTORYOP>(pnProcOp);
FACTORYVAR fnCreateVar = reinterpret_cast<FACTORYVAR>(pnProcVar);
// Create object
Operator* pOp = fnCreateOp();
Var* pVar = fnCreateVar();
AddOperator<int> intOp;
AddOperator<double> doubleOp;
e.registerOperator( &intOp, 0 );
e.registerOperator( &doubleOp, 0 );
e.registerOperator( pOp, 0 );
VarT<int> i1(10);
VarT<double> d1(2.5);
VarT<float> f1(1.0f);
std::cout << "Int Obj id : " << i1.getType() << std::endl;
std::cout << "Double Obj id : " << d1.getType() << std::endl;
std::cout << "Float Obj id : " << f1.getType() << std::endl;
std::cout << "Import Obj id : " << pVar->getType() << std::endl;
Var* i_result = e.evaluate(i1, i1, 0); // result = 20
Var* d_result = e.evaluate(d1, d1, 0); // no result
Var* f_result = e.evaluate(f1, f1, 0); // no result
Var* obj_result = e.evaluate(*pVar, *pVar, 0); // result depend on data provider
Var* mixed_result1 = e.evaluate(f1, d1, 0); // no result
Var* mixed_result2 = e.evaluate(*pVar, i1, 0); // no result
obj_result->print();
FreeLibrary( hModuleA );
}
return 0;
}
If you can modify the type Var you could add type-Ids for the argument types. But in the implementation of your operations you would always have to use a dynamic_cast at some point. If your types and operations are fixed at compile-time, you can do the whole thing with templates using Boost.MPL (specifically the containers).
Your sample code contains many errors, including slicing problems.
I'm not 100% sure, but I seem to remember you can use const type_info* as a key for a map.
If so, you could use something like following. It is not free from RTTI (type_info), but since Evaluator already checks the typeids, you can use a static_cast instead of a dynamic_cast (but it isn't that important now that the code doesn't blindly search for the right operator to apply).
Of course, the following is completely broken in terms of memory management. Reimplement with smart pointers of your choice.
#include <map>
#include <typeinfo>
#include <cassert>
#include <iostream>
struct CompareTypeinfo
{
bool operator()(const std::type_info* a, const std::type_info* b) const
{
return a->before(*b);
}
};
class Var {
public:
virtual ~Var() {}
virtual const std::type_info& getType() const = 0;
virtual void print() const = 0;
};
template<typename T> class VarT : public Var {
T value;
public:
VarT(const T& v): value(v) {}
const T& getValue() const { return value; }
virtual const std::type_info& getType() const { return typeid(T); }
virtual void print() const { std::cout << value << '\n'; }
};
class Operator {
public:
virtual ~Operator() {}
virtual Var* evaluate(const Var& a, const Var& b) const = 0;
virtual const std::type_info& getType() const = 0;
};
template<typename T> class AddOperator : public Operator {
public:
typedef T type;
virtual const std::type_info& getType() const { return typeid(T); }
virtual Var* evaluate(const Var& a, const Var& b) const
{
//it is the responsibility of Evaluator to make sure that the types match the operator
const VarT<T>* varA = static_cast<const VarT<T>*>(&a);
const VarT<T>* varB = static_cast<const VarT<T>*>(&b);
return new VarT<T>(varA->getValue() + varB->getValue());
}
};
class Evaluator {
private:
typedef std::map<const std::type_info*, Operator*, CompareTypeinfo> TypedOpMap;
typedef std::map<int, TypedOpMap> OpMap;
OpMap operatorMap;
public:
template <class Op>
void registerOperator(int opId)
{
operatorMap[opId].insert(std::make_pair(&typeid(typename Op::type), new Op));
}
Var* evaluate(const Var& a, const Var& b,int opId)
{
OpMap::const_iterator op = operatorMap.find(opId);
if (op != operatorMap.end() && a.getType() == b.getType()) {
TypedOpMap::const_iterator typed_op = op->second.find(&a.getType());
if (typed_op != op->second.end()) {
//double-checked
assert(typed_op->second->getType() == a.getType());
return typed_op->second->evaluate(a, b);
}
}
return 0;
}
};
int main()
{
Evaluator e;
e.registerOperator<AddOperator<int> >(0);
e.registerOperator<AddOperator<double> >(0);
VarT<int> i1(10), i2(20);
VarT<double> d1(2.5), d2(1.5);
VarT<float> f1(1.0), f2(2.0);
Var* i_result = e.evaluate(i1, i2, 0);
Var* d_result = e.evaluate(d1, d2, 0);
Var* f_result = e.evaluate(f1, f2, 0);
Var* mixed_result = e.evaluate(i1, d2, 0);
assert(i_result != 0);
assert(d_result != 0);
assert(f_result == 0); //addition not defined for floats in Evaluator
assert(mixed_result == 0); //and never for mixed types
i_result->print(); //30
d_result->print(); //4.0
}