/*************************************
* Author: Stephen Lombardi
*         www.stephenlombardi.com
* Date: April 8th, 2012
*
*    Copyright (C) 2012 Stephen Lombardi
*
*    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
*    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
* Abstract factory
*************************************/

#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>

// example class hierarchy
class Base {
};

class Derived1 : public Base {
public:
    Derived1(int a, float b) { std::cout << "Derived1 " << a << " " << b << std::endl; }
private:
};

class Derived2 : public Base {
public:
    Derived2(double a, short b, int c) { std::cout << "Derived2 " << a << " " << b << " " << c << std::endl; }
private:
};

// apply input stream to function
template<typename FuncRet>
FuncRet applystream(std::function<FuncRet ()> f, std::istream & in) {
    return f();
}

template<typename FuncRet, typename Arg1, typename ...Args>
FuncRet applystream(std::function<FuncRet (Arg1, Args...)> f, std::istream & in) {
    Arg1 arg1;
    in >> arg1;

    if(in) {
        std::function<FuncRet (Args...)> g =
            [f, arg1](Args... args) -> FuncRet
                { return f(arg1, args...); };
        return applystream(g, in);
    } else {
        throw std::runtime_error("stream error");
    }
}

// factory base class
template<typename T>
class AbstractFactory {
protected:
public:
    virtual std::shared_ptr<T> operator()(std::istream & in) const = 0;
};

template<typename T, typename D, typename ...Args>
class Factory : public AbstractFactory<T> {
private:
    typedef std::shared_ptr<T> TPtr;
    typedef std::shared_ptr<D> DPtr;
    typedef std::function< DPtr (Args...)> Func;
public:
    // simply calls applystream with make_shared cast to a function
    TPtr operator()(std::istream & in) const {
        using std::make_shared;
        return applystream(Func(make_shared<D, Args...>), in);
    }
private:
};

int main() {
    using std::cout;
    using std::endl;
    using std::istringstream;
    using std::map;
    using std::make_shared;
    using std::runtime_error;
    using std::shared_ptr;
    using std::string;

    // set up map
    map<string, shared_ptr< AbstractFactory<Base> > > factories;
    factories["derived1"] = make_shared< Factory<Base, Derived1, int, float> >();
    factories["derived2"] = make_shared< Factory<Base, Derived2, double, short, int> >();

    // set up stream
    istringstream stream1("derived1 5 3.4");
    istringstream stream2("derived2 9.9 -1 7");

    // construct objects
    try {
        string type;

        stream1 >> type;
        if(factories.find(type) != factories.end()) {
            shared_ptr<Base> obj1 = (*factories[type])(stream1);
            // do something with obj1
        }

        stream2 >> type;
        if(factories.find(type) != factories.end()) {
            shared_ptr<Base> obj2 = (*factories[type])(stream2);
            // do something with obj2
        }
    } catch(runtime_error ex) {
        cout << ex.what() << endl;
    }

    return 0;
}
