rex/src/loaders/Units.cpp

73 lines
2.2 KiB
C++
Raw Normal View History

2017-04-22 12:07:11 +00:00
//
// Created by phanes on 4/22/17.
//
2017-04-19 05:50:34 +00:00
2017-04-22 12:07:11 +00:00
#include "Units.h"
Unit::Unit( Json::Value loader_root )
/*
* Constructor for Unit type. Receives a UnitHolder loader_root.
*/
2017-04-18 23:16:10 +00:00
{
this->name = loader_root.get("name", "?").asString();
this->target = loader_root.get("target", "?").asString();
this->output = loader_root.get("output", "?").asString();
this->rectifier = loader_root.get("rectifier", "?").asString();
this->active = loader_root.get("active", "?").asString();
this->required = loader_root.get("required", "?").asString();
this->rectify = loader_root.get("rectify", "?").asString();
}
2017-04-19 05:50:34 +00:00
/*
* getters for Unit type.
*/
std::string Unit::get_name() { return this->name; }
std::string Unit::get_target() { return this->target; }
std::string Unit::get_output() { return this->output; }
std::string Unit::get_rectifier() { return this->rectifier; }
std::string Unit::get_active() { return this->active; }
std::string Unit::get_required() { return this->required; }
std::string Unit::get_rectify() { return this->rectify; }
Suite::Suite( std::string filename ): JLoader( filename )
/* Suite loads a file and deserializes the Unit JSON object to Unit types as a vector member
* Suite { vector<Unit> }
*/
{
Json::Value raw_units = this->get_root()["units"];
2017-04-18 23:04:21 +00:00
for ( int index = 0; index < raw_units.size(); index++ )
{
this->units.push_back( Unit( raw_units[ index ] ) );
}
2017-04-18 23:16:10 +00:00
};
2017-04-18 23:04:21 +00:00
Unit Suite::select_unit( std::string provided_name )
/*
* returns a unit from a unitholder object by name
* this will need reworked. maybe should return int, populate a pointer.
* error handling is the concern here.
*/
2017-04-19 06:27:54 +00:00
{
Unit * returnable;
2017-04-21 06:04:15 +00:00
bool foundMatch = false;
for ( int i = 0; i < this->units.size(); i++ )
2017-04-19 06:27:54 +00:00
{
std::string unit_name = this->units[i].get_name();
2017-04-21 06:04:15 +00:00
if ( unit_name == provided_name )
{
returnable = & this->units[i];
2017-04-21 06:04:15 +00:00
foundMatch = true;
break;
2017-04-19 06:27:54 +00:00
}
}
2017-04-21 06:04:15 +00:00
if (! foundMatch )
{
std::cerr << "Unit name \"" << provided_name << "\" was referenced but not defined!" << std::endl;
2017-04-21 06:04:15 +00:00
std::exit(1);
}
return * returnable;
2017-04-19 06:27:54 +00:00
}