function_algorithm(adaptor(structure));
But what about the class algorithms:
class_algorithm alg; alg.add(adaptor(structure)); alg.run();
add()
function is evaluated it will be destructed. It is very dangerous handling of the adaptors.Let's first create some helper class:
template <typename _Type, typename Enable = void> struct SmartConstReference { typedef const _Type& Type; }; template <typename _Type> struct SmartConstReference< _Type, typename enable_if<typename _Type::NeedCopy, void>::type > { typedef const _Type Type; };
template<class M1,class M2> class AddMap { typename SmartConstReference<M1>::Type m1; typename SmartConstReference<M2>::Type m2; public: typedef True NeedCopy; typedef typename M1::Key Key; typedef typename M1::Value Value; AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; Value operator[](Key k) const {return m1[k]+m2[k];} };
SmartConstReference<Map>::Type
or SmartConstReference<Graph>::Type
. This way we copy all of maps and graphs what we should copy but the greater data structures are not copied.SmartReferences:
template <typename _Type, typename Enable = void> struct SmartReference { typedef _Type& Type; }; template <typename _Type> struct SmartReference< _Type, typename enable_if<typename _Type::NeedCopy, void>::type > { typedef _Type Type; };
template <typename Map> class Algorithm { public: SmartReference<Map>::Type map; Algorithm(Map& _map) : map(_map) {} ... };
Make more helper class:
template <typename _Type, typename Enable = void> struct SmartParameter { typedef _Type& Type; }; template <typename _Type> struct SmartParameter< _Type, typename enable_if<typename _Type::NeedCopy, void>::type > { typedef const _Type& Type; };
template <typename Map> class Algorithm { public: SmartReference<Map>::Type map; Algorithm(SmartParameter<Map>::Type _map) : map(_map) {} ... };
class Algorithm { public: ... template <typename Map> void addMap(SmartParameter<Map>::Type _map) { ... } ... };
class Algorithm { public: ... template <typename Map> void addMap(const Map& _map) { _addMap<SmartParameter<Map>::Type, Map>(_map); } template <typename Map> void addMap(const Map& _map) { _addMap<SmartParameter<Map>::Type, Map>(_map); } private: template <typename MapParameter, typename Map> void _addMap(MapParameter _map) { ... } ... };