broke classes into their own files
This commit is contained in:
24
src/loaders/Conf.cpp
Normal file
24
src/loaders/Conf.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by phanes on 4/22/17.
|
||||
//
|
||||
|
||||
#include "Conf.h"
|
||||
#include "JLoader.h"
|
||||
#include <string>
|
||||
|
||||
|
||||
Conf::Conf( std::string filename ): JLoader( filename )
|
||||
{
|
||||
this->plan_path = this->get_root()["plan_path"].asString();
|
||||
this->units_path = this->get_root()["units_path"].asString();
|
||||
};
|
||||
|
||||
std::string Conf::get_plan_path()
|
||||
{
|
||||
return this->plan_path;
|
||||
}
|
||||
|
||||
std::string Conf::get_units_path()
|
||||
{
|
||||
return this->units_path;
|
||||
}
|
||||
22
src/loaders/Conf.h
Normal file
22
src/loaders/Conf.h
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by phanes on 4/22/17.
|
||||
//
|
||||
|
||||
#ifndef FTESTS_CONF_H
|
||||
#define FTESTS_CONF_H
|
||||
#include "JLoader.h"
|
||||
|
||||
class Conf: public JLoader
|
||||
{
|
||||
private:
|
||||
std::string plan_path;
|
||||
std::string units_path;
|
||||
|
||||
public:
|
||||
using JLoader::JLoader;
|
||||
Conf( std::string filename );
|
||||
std::string get_plan_path();
|
||||
std::string get_units_path();
|
||||
};
|
||||
|
||||
#endif //FTESTS_CONF_H
|
||||
34
src/loaders/JLoader.cpp
Normal file
34
src/loaders/JLoader.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by phanes on 4/22/17.
|
||||
//
|
||||
|
||||
#include "JLoader.h"
|
||||
#include "helpers.h"
|
||||
|
||||
JLoader::JLoader( std::string filename )
|
||||
{
|
||||
Json::Value json_root;
|
||||
Json::Reader reader;
|
||||
|
||||
if (! exists( filename ) )
|
||||
{
|
||||
std::cerr << "File '" << filename << "' does not exist.";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
std::ifstream jfile( filename, std::ifstream::binary );
|
||||
|
||||
bool parsingSuccessful = reader.parse( jfile, this->json_root );
|
||||
if (! parsingSuccessful )
|
||||
{
|
||||
std::cerr << "Failed to parse " << filename << ":\n\t" << reader.getFormattedErrorMessages();
|
||||
std::exit( 1 );
|
||||
} else {
|
||||
std::cout << "Parsed " << filename << " with " << this->json_root.size() << " elements." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Json::Value JLoader::get_root()
|
||||
{
|
||||
return this->json_root;
|
||||
}
|
||||
21
src/loaders/JLoader.h
Normal file
21
src/loaders/JLoader.h
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by phanes on 4/22/17.
|
||||
//
|
||||
|
||||
#ifndef FTESTS_JLOADER_H
|
||||
#define FTESTS_JLOADER_H
|
||||
#include "../json/json.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
class JLoader
|
||||
{
|
||||
private:
|
||||
Json::Value json_root;
|
||||
public:
|
||||
JLoader( std::string filename );
|
||||
Json::Value get_root();
|
||||
};
|
||||
#endif //FTESTS_JLOADER_H
|
||||
25
src/loaders/Plan.cpp
Normal file
25
src/loaders/Plan.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by phanes on 4/22/17.
|
||||
//
|
||||
|
||||
#include "Plan.h"
|
||||
Task::Task( Json::Value loader_root )
|
||||
{
|
||||
this->name = loader_root.get("name", "?").asString();
|
||||
this->dependencies = loader_root.get("depends on", "");
|
||||
}
|
||||
std::string Task::get_name() { return this->name; }
|
||||
Json::Value Task::get_dependencies() { return this->dependencies;}
|
||||
|
||||
Plan::Plan( std::string filename ): JLoader( filename )
|
||||
{
|
||||
/* Plan loads a file and deserializes the Unit JSON object to Task types as a vector member
|
||||
* Plan { vector<Task> }
|
||||
*/
|
||||
Json::Value raw_tasks = this->get_root()["plan"];
|
||||
|
||||
for ( int index = 0; index < raw_tasks.size(); index++ )
|
||||
{
|
||||
this->tasks.push_back( Task( raw_tasks[index] ) );
|
||||
}
|
||||
};
|
||||
33
src/loaders/Plan.h
Normal file
33
src/loaders/Plan.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by phanes on 4/22/17.
|
||||
//
|
||||
|
||||
#ifndef FTESTS_PLAN_H
|
||||
#define FTESTS_PLAN_H
|
||||
#include <string>
|
||||
#include "../json/json.h"
|
||||
#include "JLoader.h"
|
||||
|
||||
class Task
|
||||
{
|
||||
private:
|
||||
std::string name;
|
||||
Json::Value dependencies;
|
||||
|
||||
public:
|
||||
Task( Json::Value loader_root );
|
||||
std::string get_name();
|
||||
Json::Value get_dependencies();
|
||||
};
|
||||
|
||||
class Plan: public JLoader
|
||||
{
|
||||
public:
|
||||
using JLoader::JLoader;
|
||||
std::vector<Task> tasks;
|
||||
Plan( std::string filename );
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //FTESTS_PLAN_H
|
||||
61
src/loaders/Units.cpp
Normal file
61
src/loaders/Units.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// Created by phanes on 4/22/17.
|
||||
//
|
||||
|
||||
#include "Units.h"
|
||||
Unit::Unit( Json::Value loader_root )
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
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; }
|
||||
|
||||
UnitHolder::UnitHolder( std::string filename ): JLoader( filename )
|
||||
{
|
||||
/* UnitHolder loads a file and deserializes the Unit JSON object to Unit types as a vector member
|
||||
* UnitHolder { vector<Unit> }
|
||||
*/
|
||||
Json::Value raw_units = this->get_root()["units"];
|
||||
|
||||
for ( int index = 0; index < raw_units.size(); index++ )
|
||||
{
|
||||
this->units.push_back(Unit( raw_units[index] ));
|
||||
}
|
||||
};
|
||||
|
||||
Unit UnitHolder::select_unit(std::string provided_name)
|
||||
{
|
||||
Unit * returnable;
|
||||
bool foundMatch = false;
|
||||
|
||||
for ( int i = 0; i < this->units.size(); i++ )
|
||||
{
|
||||
std::string unit_name = this->units[i].get_name();
|
||||
if ( unit_name == provided_name )
|
||||
{
|
||||
returnable = & this->units[i];
|
||||
foundMatch = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (! foundMatch )
|
||||
{
|
||||
std::cerr << "Unit name \"" << provided_name << "\" was referenced in a plan task but not defined!" << std::endl;
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
return * returnable;
|
||||
}
|
||||
45
src/loaders/Units.h
Normal file
45
src/loaders/Units.h
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by phanes on 4/22/17.
|
||||
//
|
||||
|
||||
#ifndef FTESTS_UNITS_H
|
||||
#define FTESTS_UNITS_H
|
||||
|
||||
#include <vector>
|
||||
#include "../json/json.h"
|
||||
#include "JLoader.h"
|
||||
|
||||
class Unit
|
||||
{
|
||||
private:
|
||||
std::string name;
|
||||
std::string target;
|
||||
std::string output;
|
||||
std::string rectifier;
|
||||
std::string active;
|
||||
std::string required;
|
||||
std::string rectify;
|
||||
|
||||
public:
|
||||
Unit( Json::Value loader_root );
|
||||
std::string get_name();
|
||||
std::string get_target();
|
||||
std::string get_output();
|
||||
std::string get_rectifier();
|
||||
std::string get_active();
|
||||
std::string get_required();
|
||||
std::string get_rectify();
|
||||
};
|
||||
|
||||
class UnitHolder: public JLoader
|
||||
{
|
||||
public:
|
||||
using JLoader::JLoader;
|
||||
std::vector<Unit> units;
|
||||
UnitHolder( std::string filename );
|
||||
Unit select_unit( std::string provided_name );
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //FTESTS_UNITS_H
|
||||
11
src/loaders/helpers.cpp
Normal file
11
src/loaders/helpers.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// Created by phanes on 4/22/17.
|
||||
//
|
||||
|
||||
#include "helpers.h"
|
||||
|
||||
bool exists(const std::string& name)
|
||||
{
|
||||
struct stat buffer;
|
||||
return (stat (name.c_str(), &buffer) == 0);
|
||||
}
|
||||
14
src/loaders/helpers.h
Normal file
14
src/loaders/helpers.h
Normal file
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by phanes on 4/22/17.
|
||||
//
|
||||
|
||||
#ifndef FTESTS_HELPERS_H
|
||||
#define FTESTS_HELPERS_H
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
|
||||
bool exists (const std::string& name);
|
||||
|
||||
|
||||
|
||||
#endif //FTESTS_HELPERS_H
|
||||
3
src/loaders/loaders.cpp
Normal file
3
src/loaders/loaders.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "loaders.h"
|
||||
|
||||
|
||||
12
src/loaders/loaders.h
Normal file
12
src/loaders/loaders.h
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Created by phanes on 4/16/17.
|
||||
//
|
||||
#ifndef FTESTS_LOADERS_H
|
||||
#define FTESTS_LOADERS_H
|
||||
|
||||
#include "JLoader.h"
|
||||
#include "Units.h"
|
||||
#include "Plan.h"
|
||||
#include "Conf.h"
|
||||
|
||||
#endif //FTESTS_LOADERS_H
|
||||
Reference in New Issue
Block a user