broke classes into their own files

This commit is contained in:
Chris Punches
2017-04-22 08:07:11 -04:00
parent f66391d2b0
commit 69539ae019
37 changed files with 1016 additions and 353 deletions

View File

@@ -1,90 +0,0 @@
//
// Created by phanes on 4/16/17.
//
#ifndef FTESTS_LOADERS_H
#define FTESTS_LOADERS_H
#include "loaders.h"
#include "json/json.h"
#include "json/json-forwards.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
inline bool exists (const std::string& name);
class JLoader
{
private:
Json::Value json_root;
public:
JLoader( std::string filename );
Json::Value get_root();
};
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 );
};
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 );
};
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_LOADERS_H

24
src/loaders/Conf.cpp Normal file
View 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
View 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
View 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
View 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
View 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
View 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

View File

@@ -1,41 +1,8 @@
#include "loaders.h"
#include <sys/stat.h>
inline bool exists(const std::string& name)
{
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
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;
}
//
// Created by phanes on 4/22/17.
//
#include "Units.h"
Unit::Unit( Json::Value loader_root )
{
this->name = loader_root.get("name", "?").asString();
@@ -92,43 +59,3 @@ Unit UnitHolder::select_unit(std::string provided_name)
return * returnable;
}
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] ) );
}
};
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;
}

45
src/loaders/Units.h Normal file
View 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
View 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
View 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
View File

@@ -0,0 +1,3 @@
#include "loaders.h"

12
src/loaders/loaders.h Normal file
View 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