Operations and AccountOperations boilerplate

This commit is contained in:
Quentin Snow 2023-01-17 11:58:31 -06:00
parent 94e63e8f76
commit 287f9f4751
7 changed files with 85 additions and 1 deletions

View File

@ -4,4 +4,4 @@ project(budget)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
add_executable(${PROJECT_NAME} src/main.cpp add_executable(${PROJECT_NAME} src/main.cpp
src/optHandlers/mainOptHandler.h src/optHandlers/mainOptHandler.cpp) src/optHandlers/mainOptHandler.h src/optHandlers/mainOptHandler.cpp src/optHandlers/operation.cpp src/optHandlers/operation.h src/optHandlers/accountOperation.cpp src/optHandlers/accountOperation.h)

View File

@ -0,0 +1,15 @@
//
// Created by quentin on 1/17/23.
//
#include "accountOperation.h"
#include <utility>
using namespace Budget::OptHandlers;
void AccountOperation::commit() {
//TODO This function will be called when the action needs to be done
}
AccountOperation::AccountOperation() = default;

View File

@ -0,0 +1,31 @@
//
// Created by quentin on 1/17/23.
//
#ifndef BUDGET_ACCOUNTOPERATION_H
#define BUDGET_ACCOUNTOPERATION_H
#include <string>
#include <utility>
#include "operation.h"
namespace Budget::OptHandlers {
class AccountOperation : public Operation {
public:
void commit() override;
explicit AccountOperation();
struct Flags : public Operation::Flags {
bool del = false;
bool forceDel = false;
bool value = false;
std::string description;
};
Flags flags;
private:
};
} // OptHandlers
#endif //BUDGET_ACCOUNTOPERATION_H

View File

@ -2,6 +2,7 @@
// Created by quentin on 1/8/23. // Created by quentin on 1/8/23.
// //
#include "mainOptHandler.h" #include "mainOptHandler.h"
#include "accountOperation.h"
#include <iostream> #include <iostream>
#include <getopt.h> #include <getopt.h>
@ -60,6 +61,8 @@ void MainOptHandler::accountOptHandler(std::string account) {
{"description", no_argument, nullptr, 'D'}, {"description", no_argument, nullptr, 'D'},
}; };
auto acctOperation = std::make_unique<AccountOperation>();
while (true) { while (true) {
int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:dFvD", accountLongOpts, nullptr); int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:dFvD", accountLongOpts, nullptr);
if (opt == -1) if (opt == -1)
@ -74,14 +77,20 @@ void MainOptHandler::accountOptHandler(std::string account) {
case 'e': case 'e':
case 'p': case 'p':
optind--; optind--;
operations.push(std::move(acctOperation));
return; return;
case 'd': case 'd':
acctOperation->flags.del = true;
break; break;
case 'F': case 'F':
acctOperation->flags.del = true;
acctOperation->flags.forceDel = true;
break; break;
case 'v': case 'v':
acctOperation->flags.value = true;
break; break;
case 'D': case 'D':
acctOperation->flags.description = optarg;
break; break;
case '?': case '?':
help(); help();
@ -90,6 +99,7 @@ void MainOptHandler::accountOptHandler(std::string account) {
break; break;
} }
} }
operations.push(std::move(acctOperation));
} }
void MainOptHandler::createOptHandler(std::string account) { void MainOptHandler::createOptHandler(std::string account) {

View File

@ -7,6 +7,9 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <queue>
#include <memory>
#include "operation.h"
namespace Budget::OptHandlers{ namespace Budget::OptHandlers{
class MainOptHandler { class MainOptHandler {
@ -15,6 +18,8 @@ namespace Budget::OptHandlers{
void help(); void help();
std::queue<std::unique_ptr<Operation>> operations;
private: private:
void accountOptHandler(std::string account); void accountOptHandler(std::string account);

View File

@ -0,0 +1,5 @@
//
// Created by quentin on 1/17/23.
//
#include "operation.h"

View File

@ -0,0 +1,18 @@
//
// Created by quentin on 1/17/23.
//
#ifndef BUDGET_OPERATION_H
#define BUDGET_OPERATION_H
namespace Budget::OptHandlers {
class Operation {
public:
virtual void commit() = 0;
struct Flags {
};
};
}
#endif //BUDGET_OPERATION_H