
/******************************  LICENSE  *******************************

 * (C) Copyright 1996-2017 ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.

 *******************************  LICENSE  *******************************/

/*! \\file {{object }}Attributes.h
    \\brief Definition of {{ object }} Attributes class.

    This file is automatically generated.
    Do Not Edit!

*/

#include "MagRequest.h"
#include "{{ object }}Wrapper.h"
#include "MagicsParameter.h"
#include "Factory.h"
#include "MagTranslator.h"
#include "MagicsGlobal.h"

using namespace magics;

{%if abstract %}
{{ object }}Wrapper::{{ object }}Wrapper(): {{ object | lower }}_(0)
{% else %}
{%if static_methods %}
{{ object }}Wrapper::{{ object }}Wrapper()
{% else %}
{{ object }}Wrapper::{{ object }}Wrapper(): {{ object | lower }}_(new {{ object }}())
{% endif %}
{% endif %}
{


	{%if inherit != "" %}
	{{ inherit }}Wrapper::object({{ object | lower }}_);
	{% endif %}

}
{{ object }}Wrapper::{{ object }}Wrapper({{ object }}* {{ object | lower }}): {{ object | lower }}_({{ object | lower}})
{

	{%if inherit != "" %}
	{{ inherit }}Wrapper::object({{ object | lower }}_);
	{% endif %}
}

{{ object }}Wrapper::~{{ object }}Wrapper()
{

}

void {{ object }}Wrapper::set(const MagRequest& request)
{

	{%if inherit != "" %}

	{{ inherit }}Wrapper::set(request);
	{% endif %}

	{% for param  in string_parameters -%}

	{% if param.to == "intarray" -%}

	{{ param.to }} {{ param.member}}_value;
	for (int i = 0; i < request.countValues("{{ param.name | upper }}"); i++)
		{{ param.member}}_value.push_back((int)request("{{ param.name | upper }}", i));
	if ( !{{ param.member}}_value.empty() )
		{{ object | lower }}_->{{ param.member}}_ = {{ param.member}}_value;
	{% elif param.to == "longintarray" -%}

	{{ param.to }} {{ param.member}}_value;
	for (int i = 0; i < request.countValues("{{ param.name | upper }}"); i++)
		{{ param.member}}_value.push_back((long int)request("{{ param.name | upper }}", i));
	if ( !{{ param.member}}_value.empty() )
		{{ object | lower }}_->{{ param.member}}_ = {{ param.member}}_value;
	{% elif param.to == "doublearray" -%}
	{{ param.to }}  {{ param.member}}_value;
	for (int i = 0; i < request.countValues("{{ param.name | upper }}"); i++)
		{{ param.member}}_value.push_back((double)request("{{ param.name | upper }}", i));
	if ( !{{ param.member}}_value.empty() )
		{{ object | lower }}_->{{ param.member}}_ = {{ param.member}}_value;
	{% elif param.to == "stringarray" -%}
	{{ param.to }}  {{ param.member}}_value;
	for (int i = 0; i < request.countValues("{{ param.name | upper }}"); i++)
		{{ param.member}}_value.push_back((string)request("{{ param.name | upper }}", i));
	if ( !{{ param.member}}_value.empty() )
		{{ object | lower }}_->{{ param.member}}_ = {{ param.member}}_value;
	{% else -%}
	if  (request.countValues("{{ param.name | upper }}") ) {
		{{ param.from }} {{ param.member}}_value = request("{{ param.name | upper }}");
		{%if param.from  == param.to  -%}
		{{ object | lower }}_->{{ param.member}}_ = {{ param.member}}_value;
		{% else  -%}
        {%if static_methods %}
		{{ object }}::{{ param.member}}(MagTranslator<{{ param.from}}, {{ param.to }}>()({{ param.member}}_value));
        {% else %}
		{{ object | lower }}_->{{ param.member}}_ = MagTranslator<{{ param.from}}, {{ param.to }}>()({{ param.member}}_value);
		{% endif %}
		{% endif -%}
	}
	{% endif -%}

	{% endfor %}
	{% for param  in factory_parameters -%}
		{%if param.niceprint  -%}
		{%if param.to == "Colour" -%}
		if  (request.countValues("{{ param.name | upper }}") ) {
		string {{ param.member}}_value = request("{{ param.name | upper }}");
		{{ object | lower }}_->{{ param.member }}_ = unique_ptr<Colour>(MagTranslator<string, {{ param.to }}>()({{ param.member}}_value));
	}
		{% else %}
	if  (request.countValues("{{ param.name | upper }}") ) {
		string {{ param.member}}_value = request("{{ param.name | upper }}");
		{{ object | lower }}_->{{ param.member }}_ = MagTranslator<string, {{ param.to }}>()({{ param.member}}_value);
	}
		{% endif %}
	{% else %}
		string {{ param.member}}_value = request.countValues("{{ param.name | upper }}") ?  (string) request("{{ param.name | upper }}") : "{{ param.default }}";
	MagLog::debug() << " {{ param.name | upper }} set to " << {{ param.member}}_value << endl;
	{{ param.to }}Wrapper* {{ param.member}}_wrapper = 0;
	try
	{
		{{ param.member}}_wrapper = SimpleFactory<{{ param.to }}Wrapper>::create({{ param.member}}_value);
	}
	 catch (NoFactoryException&) {
		if (MagicsGlobal::strict()) {
            throw;
        }
		MagLog::warning() << "[" << {{ param.member}}_value << "] is not a valid value for {{ param.member}}: reset to default -> [{{ param.default }}]" << endl;
		{{ param.member}}_wrapper = SimpleFactory<{{ param.to }}Wrapper>::create("{{ param.default }}");
	}
	{{ param.member}}_wrapper->set(request);
	{{ object | lower }}_->{{ param.member }}_ =  unique_ptr<{{ param.to }}>({{ param.member }}_wrapper->object());
	delete {{ param.member}}_wrapper;
	{% endif -%}
	{% endfor %}
}

void {{ object }}Wrapper::print(ostream& out)  const
{
	out << "{{ object }}Wrapper[]";
}


{% for param  in factory_parameters -%}
{%if not param.niceprint  -%}
{% for option  in param.options -%}
#include "{{ option.object }}Wrapper.h"
{%if option.object !=  option.parent  -%}
static SimpleObjectMaker<{{ option.object }}, {{ option.parent }}> {{ object }}_{{ param.name }}_{{ option.key | replace(":", "")}} ("{{ option.key }}");
static SimpleObjectMaker<{{ option.object }}Wrapper, {{ option.parent }}Wrapper> {{ object }}_{{ param.name }}_{{ option.key  | replace(":", "")}}_wrapper ("{{ option.key }}");
{% else %}
static SimpleObjectMaker<{{ option.object }}Wrapper> {{ object }}_{{ param.name }}_{{ option.key }}_Wrapper("{{ option.key }}");
{% endif %}

{% endfor -%}

{% endif  %}
{% endfor -%}
