rex/src/loaders/Unit.cpp

93 lines
2.2 KiB
C++
Raw Normal View History

#include "Unit.h"
2017-06-20 06:06:33 +00:00
#include <iostream>
#include <fstream>
#include <cstdlib>
2017-06-21 04:01:00 +00:00
#include <stdexcept>
/// Unit_NotPopulated - Meant to be thrown when a Unit type is not populated before being used.
/// Signaled by use of the 'populated' boolean member of the Unit class.
2017-06-21 04:01:00 +00:00
class Unit_NotPopulated: public std::runtime_error
{
public:
Unit_NotPopulated(): std::runtime_error("Unit: Attempted to access a member before loading values.") {}
};
Unit::Unit() {}
2017-06-21 02:53:47 +00:00
// where the serialized json is broken down into object members
int Unit::load_root(Json::Value loader_root)
{
// this needs reworked to throw an Exception if any of the values aren't loadable.
2017-06-21 04:01:00 +00:00
this->name = loader_root.get("name", "").asString();
2017-06-21 02:53:47 +00:00
2017-06-21 04:01:00 +00:00
this->target = loader_root.get("target", "").asString();
2017-06-21 02:53:47 +00:00
2017-06-21 04:01:00 +00:00
this->output = loader_root.get("output", "").asString();
2017-06-21 02:53:47 +00:00
2017-06-21 04:01:00 +00:00
this->rectifier = loader_root.get("rectifier", "").asString();
2017-06-21 02:53:47 +00:00
2017-06-21 04:01:00 +00:00
this->active = loader_root.get("active", "").asString();
2017-06-21 02:53:47 +00:00
2017-06-21 04:01:00 +00:00
this->required = loader_root.get("required", "").asString();
2017-06-21 02:53:47 +00:00
2017-06-21 04:01:00 +00:00
this->rectify = loader_root.get("rectify", "").asString();
2017-06-21 02:53:47 +00:00
this->populated = true;
return 0;
}
2017-06-20 06:06:33 +00:00
2017-06-21 02:53:47 +00:00
// populates from a string json object
2017-06-20 06:06:33 +00:00
int Unit::load_string(std::string json_val)
{
2017-06-21 02:53:47 +00:00
// serialize
this->load_json_string( json_val, true );
// deserialize
this->load_root( this->json_root );
return 0;
}
2017-06-20 06:06:33 +00:00
// getters for Unit type.
std::string Unit::get_name()
{
2017-06-21 04:01:00 +00:00
if ( ! this->populated ) { throw Unit_NotPopulated(); }
return this->name;
}
std::string Unit::get_target()
{
2017-06-21 04:01:00 +00:00
if ( ! this->populated ) { throw Unit_NotPopulated(); }
return this->target;
}
std::string Unit::get_output()
{
2017-06-21 04:01:00 +00:00
if ( ! this->populated ) { throw Unit_NotPopulated(); }
return this->output;
}
std::string Unit::get_rectifier()
{
2017-06-21 04:01:00 +00:00
if ( ! this->populated ) { throw Unit_NotPopulated(); }
return this->rectifier;
}
std::string Unit::get_active()
{
2017-06-21 04:01:00 +00:00
if ( ! this->populated ) { throw Unit_NotPopulated(); }
return this->active;
}
std::string Unit::get_required()
{
2017-06-21 04:01:00 +00:00
if ( ! this->populated ) { throw Unit_NotPopulated(); }
return this->required;
}
std::string Unit::get_rectify()
{
2017-06-21 04:01:00 +00:00
if ( ! this->populated ) { throw Unit_NotPopulated(); }
return this->rectify;
}