// implementation file: CoderFactory.cpp
// description:         Factory function for Coder objects.
// variant:             2 with flexible composition
// author:              Karlheinz Hug
// creation date:       2012-07-05
// last update:         2012-08-10

#include <cassert>
#include <cstdlib>

#include "CoderFactory.hpp"
#include "Copier.hpp"
#include "Expander.hpp"
#include "Caesar.hpp"
#include "Vernam.hpp"
#include "Composite.hpp"

	Coder * CoderFactory::new_coder (Coder * const first, CoderType coder_type, const std::string & key)
	{
		Coder * result = 0;
		switch (coder_type)
		{
			case copier :
			default :
				result = new Copier();
				break;
			case expander :
				result = new Expander();
				break;
			case caesar :
				Caesar * caesar;
				caesar = new Caesar(); // Declaration + initialization can't be abbreviated!
				caesar->set_key(std::atoi(key.c_str()));
				result = caesar;
				break;
			case vernam :
				Vernam * vernam;
				vernam = new Vernam();
				vernam->set_key(std::vector<char>(key.begin(), key.end()));
				result = vernam;
				break;
		}
		if (first)
		{
			result = new Composite(first, result);
		}
		assert(result);   // postcondition
		return result;
	}

