Skip to content

Commit

Permalink
Rename namespace Key to key
Browse files Browse the repository at this point in the history
Google naming convention
  • Loading branch information
leonmavr committed Dec 4, 2023
1 parent 688ce4e commit 6a71a71
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 59 deletions.
4 changes: 2 additions & 2 deletions lib/inc/backend.hpp
Expand Up @@ -132,7 +132,7 @@ typedef struct Flags {
class Backend: public IBackend, public Subject {
public:
Backend() = delete;
Backend(const Key::Keypad& keypad);
Backend(const key::Keypad& keypad);
Backend(const Backend& other) :
keypad_(other.keypad_),
stack_(std::make_unique<Stack>(*other.stack_)),
Expand Down Expand Up @@ -265,7 +265,7 @@ class Backend: public IBackend, public Subject {

private:
/** reference to a keypad that describes the calculator's key configuration */
const Key::Keypad& keypad_;
const key::Keypad& keypad_;
// owns the stack - unique_ptr manages its lifetime and deallocation
std::unique_ptr<Stack> stack_;
// LASTX register; stores the value of X before a function is invoked
Expand Down
10 changes: 5 additions & 5 deletions lib/inc/frontend.hpp
Expand Up @@ -24,7 +24,7 @@ typedef struct {
class Frontend
{
public:
Frontend(const Key::Keypad& keypad);
Frontend(const key::Keypad& keypad);
~Frontend();
/**
* @brief Draw a calculator's key on the screen. When given a
Expand Down Expand Up @@ -98,7 +98,7 @@ class Frontend
void SetUiDimensions();

// reference to keypad - we draw the interface based on it
const Key::Keypad& keypad_;
const key::Keypad& keypad_;
//------------------------------------------------------
// screen and keypad dimensions
//------------------------------------------------------
Expand All @@ -121,12 +121,12 @@ class Frontend
// 10 general registers
//------------------------------------------------------
// top left position of the general register display area
Key::Point gen_regs_top_left_;
std::unordered_map<std::string, Key::Point> gen_reg_area_;
key::Point gen_regs_top_left_;
std::unordered_map<std::string, key::Point> gen_reg_area_;
// how many characters each general register can display
unsigned gen_reg_width_;
// maps the display key of each gen. register to its coordinates
std::unordered_map<std::string, Key::Point> gen_regs_;
std::unordered_map<std::string, key::Point> gen_regs_;
//------------------------------------------------------
// ncurses and terminal
//------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions lib/inc/hip35.hpp
Expand Up @@ -17,7 +17,7 @@ class Hip35
{
public:
Hip35() = delete;
Hip35(const Key::Keypad& keypad);
Hip35(const key::Keypad& keypad);
~Hip35() { delete observer_; }
double RunUI(bool run_headless = false);
double EvalString(std::string expression);
Expand All @@ -30,7 +30,7 @@ class Hip35
// how many milliseconds to keep a button highlighted for after being pressed
std::chrono::milliseconds delay_ms_;
std::vector<std::string> tokens_no_ui_;
const Key::Keypad& keypad_;
const key::Keypad& keypad_;
};

} // namespace Ui
Expand Down
14 changes: 7 additions & 7 deletions lib/inc/keypad.hpp
Expand Up @@ -33,7 +33,7 @@ namespace backend {
* keys w/o taking width and height into account. So
* two adjacent keys will be (x, y), (x+1, y).
*/
namespace Key {
namespace key {

// define key presses to call calculator functions
// for their implementation see the mapping in this file
Expand Down Expand Up @@ -85,7 +85,7 @@ typedef struct {
struct StackKeyInfo {
std::function<void(backend::Backend& b)> function;
std::string annotation;
Key::Point point;
key::Point point;
std::string long_key;
};
/**
Expand Down Expand Up @@ -116,7 +116,7 @@ using StackKeys = std::unordered_map<std::string, StackKeyInfo>;
struct SingleKeyInfo {
std::function<double(double)> function;
std::string annotation;
Key::Point point;
key::Point point;
std::string long_key;
};
/** @copydoc StackKeys */
Expand All @@ -126,15 +126,15 @@ using SingleArgKeys = std::unordered_map<std::string, SingleKeyInfo>;
struct DoubleKeyInfo {
std::function<double(double, double)> function;
std::string annotation;
Key::Point point;
key::Point point;
std::string long_key;
};
using DoubleArgKeys = std::unordered_map<std::string, DoubleKeyInfo>;

struct StorageKeyInfo {
std::function<void(backend::Backend& b, std::string name)> function;
std::string annotation;
Key::Point point;
key::Point point;
std::string long_key;
};
/** @copydoc StackKeys */
Expand All @@ -143,7 +143,7 @@ using StorageKeys = std::unordered_map<std::string, StorageKeyInfo>;
struct KeyInfoEex {
std::function<void(backend::Backend&, std::optional<double>)> function;
std::string annotation;
Key::Point point;
key::Point point;
std::string long_key;
};
/** @copydoc StackKeys */
Expand Down Expand Up @@ -189,6 +189,6 @@ static std::string AnnotateKey(T& it, const std::string keypress) {
extern const Keypad keypad;


} // namespace Key
} // namespace key

#endif /* KEYPAD_HPP */
24 changes: 12 additions & 12 deletions lib/src/backend.cpp
Expand Up @@ -32,15 +32,15 @@ void Subject::NotifyOperation(const std::string& operation) {

namespace backend {

Backend::Backend(const Key::Keypad& keypad):
Backend::Backend(const key::Keypad& keypad):
keypad_(keypad),
stack_(std::make_unique<Stack>()),
lastx_(0.0),
sto_regs_({0})
{
// map a general register name to the index of the gr array
std::size_t i = 0;
for (const auto& n: Key::kNamesGenRegs)
for (const auto& n: key::kNamesGenRegs)
gen_regs_name2idx[n] = i++;
// initialize flags
flags_.shift_up = true;
Expand All @@ -57,15 +57,15 @@ void Backend::Rdn() {
flags_.eex_pressed = false;
// inform the observer
NotifyValue(Peek());
NotifyOperation(Key::kKeyRdn);
NotifyOperation(key::kKeyRdn);
}

void Backend::SwapXY() {
std::swap((*stack_)[IDX_REG_X], (*stack_)[IDX_REG_Y]);
flags_.eex_pressed = false;
// inform the observer
NotifyValue(Peek());
NotifyOperation(Key::kKeySwap);
NotifyOperation(key::kKeySwap);
}

void Backend::Insert(double num) {
Expand All @@ -91,7 +91,7 @@ void Backend::Enter() {
// notify class observer since enter manipulates the stack
NotifyValue(Peek());
// don't forget to notify the observer so we can use the event later
NotifyOperation(Key::kKeyEnter);
NotifyOperation(key::kKeyEnter);
}

void Backend::LastX() {
Expand All @@ -101,7 +101,7 @@ void Backend::LastX() {
flags_.eex_pressed = false;
// inform the observer
NotifyValue(Peek());
NotifyOperation(Key::kKeyLastX);
NotifyOperation(key::kKeyLastX);
}

double Backend::Calculate(std::string operation) {
Expand Down Expand Up @@ -144,7 +144,7 @@ void Backend::Clx() {
stack_->writeX(0.0);
flags_.shift_up = false;
// inform the observer
NotifyOperation(Key::kKeyClx);
NotifyOperation(key::kKeyClx);
NotifyValue(Peek());
}

Expand All @@ -153,15 +153,15 @@ void Backend::Cls() {
Enter();
Enter();
Enter();
NotifyOperation(Key::kKeyCls);
NotifyOperation(key::kKeyCls);
NotifyValue(Peek());
}

void Backend::Pi() {
flags_.eex_pressed = false;
Insert(M_PI);
// inform the observer
NotifyOperation(Key::kKeyPi);
NotifyOperation(key::kKeyPi);
NotifyValue(Peek());
}

Expand All @@ -181,7 +181,7 @@ void Backend::Eex(std::optional<double> token) {
stack_->writeX(*token);
flags_.shift_up = false;
flags_.eex_pressed = true;
NotifyOperation(Key::kKeyEex);
NotifyOperation(key::kKeyEex);
NotifyValue(Peek());
}

Expand Down Expand Up @@ -211,7 +211,7 @@ void Backend::Sto(std::string name) {
flags_.shift_up = true;
flags_.eex_pressed = false;

NotifyOperation(Key::kKeyStore);
NotifyOperation(key::kKeyStore);
// doesn't change the stack so no values sent to observer
}

Expand All @@ -234,7 +234,7 @@ void Backend::Rcl(std::string name) {
(*stack_)[IDX_REG_X] = sto_regs_[idx];
flags_.shift_up = true;
flags_.eex_pressed = false;
NotifyOperation(Key::kKeyRcl);
NotifyOperation(key::kKeyRcl);
NotifyValue(Peek());
}

Expand Down
28 changes: 14 additions & 14 deletions lib/src/frontend.cpp
Expand Up @@ -137,7 +137,7 @@ namespace gui {
//-------------------------------------------------------------//
// Class methods //
//-------------------------------------------------------------//
Frontend::Frontend(const Key::Keypad& keypad):
Frontend::Frontend(const key::Keypad& keypad):
keypad_(keypad),
key_width_(12),
key_height_(3),
Expand All @@ -163,7 +163,7 @@ Frontend::~Frontend() {

void Frontend::SetUiDimensions() {
// find max x and y of keys in the grid
std::vector<Key::Point> grid_coords;
std::vector<key::Point> grid_coords;
for (const auto& pair: keypad_.stack_keys)
grid_coords.push_back(pair.second.point);
for (const auto& pair: keypad_.single_arg_keys)
Expand All @@ -175,10 +175,10 @@ void Frontend::SetUiDimensions() {
for (const auto& pair: keypad_.eex_key)
grid_coords.push_back(pair.second.point);
auto it = std::max_element(grid_coords.begin(), grid_coords.end(),
[] (const Key::Point& p1, const Key::Point& p2) { return p1.x < p2.x; });
[] (const key::Point& p1, const key::Point& p2) { return p1.x < p2.x; });
max_width_pixels_ = it->x;
it = std::max_element(grid_coords.begin(), grid_coords.end(),
[] (const Key::Point& p1, const Key::Point& p2) { return p1.y < p2.y; });
[] (const key::Point& p1, const key::Point& p2) { return p1.y < p2.y; });
max_height_pixels_ = it->y;

//------------------------------------------------------
Expand All @@ -202,9 +202,9 @@ void Frontend::SetUiDimensions() {
max_width_pixels_ += 3;
constexpr unsigned offsety = 3;
// this is the top left point where general register will be printed
gen_regs_top_left_ = Key::Point{max_width_pixels_, offsety};
for (unsigned i = 0; i < Key::kNamesGenRegs.size(); ++i)
gen_regs_[Key::kNamesGenRegs[i]] = Key::Point{max_width_pixels_, offsety + i};
gen_regs_top_left_ = key::Point{max_width_pixels_, offsety};
for (unsigned i = 0; i < key::kNamesGenRegs.size(); ++i)
gen_regs_[key::kNamesGenRegs[i]] = key::Point{max_width_pixels_, offsety + i};
// make enough horizontal space for general register display
max_width_pixels_ += gen_reg_width_ + 3;
}
Expand All @@ -221,7 +221,7 @@ static inline std::string ToLower(std::string s) {

void Frontend::PrintGenRegister(const std::string& name, double val) {
// where to print the number
Key::Point xy;
key::Point xy;
if (gen_regs_.find(ToLower(name)) != gen_regs_.end())
xy = gen_regs_[ToLower(name)];
else if (gen_regs_.find(ToUpper(name)) != gen_regs_.end())
Expand Down Expand Up @@ -261,32 +261,32 @@ bool Frontend::DrawKey(const std::string& key, bool highlight) {
// whether the keypress corresponds to a key in the keypad
bool found = false;

Key::Point grid_pos{0, 0};
key::Point grid_pos{0, 0};
std::string text_on_key = "";
// search which map key belongs to and get the info from there
if (it1 != keypad_.stack_keys.end()) {
grid_pos = it1->second.point;
text_on_key = Key::AnnotateKey(
text_on_key = key::AnnotateKey(
it1, key);
found = true;
} else if (it2 != keypad_.single_arg_keys.end()) {
grid_pos = it2->second.point;
text_on_key = Key::AnnotateKey(
text_on_key = key::AnnotateKey(
it2, key);
found = true;
} else if (it3 != keypad_.double_arg_keys.end()) {
grid_pos = it3->second.point;
text_on_key = Key::AnnotateKey(
text_on_key = key::AnnotateKey(
it3, key);
found = true;
} else if (it4 != keypad_.storage_keys.end()) {
grid_pos = it4->second.point;
text_on_key = Key::AnnotateKey(
text_on_key = key::AnnotateKey(
it4, key);
found = true;
} else if (it5 != keypad_.eex_key.end()) {
grid_pos = it5->second.point;
text_on_key = Key::AnnotateKey(
text_on_key = key::AnnotateKey(
it5, key);
found = true;
}
Expand Down
16 changes: 8 additions & 8 deletions lib/src/hip35.cpp
Expand Up @@ -9,7 +9,7 @@

namespace Ui {

Hip35::Hip35(const Key::Keypad& keypad):
Hip35::Hip35(const key::Keypad& keypad):
delay_ms_(std::chrono::milliseconds(100)),
tokens_no_ui_(),
keypad_(keypad) {
Expand Down Expand Up @@ -68,7 +68,7 @@ double Hip35::RunUI(bool run_headless) {
const auto it3 = keypad_.double_arg_keys.find(keypress);
const auto it4 = keypad_.storage_keys.find(keypress);

if (keypress == Key::kKeyEnter)
if (keypress == key::kKeyEnter)
key_type = backend::kTypeEnter;
else if (it1 != keypad_.stack_keys.end())
key_type = backend::kTypeStack;
Expand Down Expand Up @@ -100,7 +100,7 @@ double Hip35::RunUI(bool run_headless) {
//------------------------------------------------------
// Call Backend instance to execute
//------------------------------------------------------
if (keypress == Key::kKeyEex) {
if (keypress == key::kKeyEex) {
// it can be unset (empty operand) or a decimal
std::optional<double> opt_operand;
if (IsDecimal(operand))
Expand All @@ -119,10 +119,10 @@ double Hip35::RunUI(bool run_headless) {
// don't do anything and wait for next key
}
operation = observer_->GetState().first;
if (operation == Key::kKeyStore) {
if (operation == key::kKeyStore) {
const double regx = observer_->GetState().second.first;
frontend_->PrintGenRegister(keypress, regx);
} else if (operation == Key::kKeyRcl) {
} else if (operation == key::kKeyRcl) {
// registers have changed due to RCL
PrintRegs();
}
Expand Down Expand Up @@ -150,7 +150,7 @@ double Hip35::RunUI(bool run_headless) {
PrintRegs();
operand = "";
is_prev_op_storage = true;
} else if (keypress == Key::kKeyEnter) {
} else if (keypress == key::kKeyEnter) {
if (!operand.empty())
backend_->Insert(std::stod(operand));
backend_->Enter();
Expand All @@ -171,11 +171,11 @@ double Hip35::RunUI(bool run_headless) {
operation = observer_->GetState().first;
regx = observer_->GetState().second.first;
regy = observer_->GetState().second.second;
if (operation == Key::kKeyEex) {
if (operation == key::kKeyEex) {
// EEX was pressed - show the result for the curently typed operand
if (!run_headless)
frontend_->PrintRegisters(std::pow(10, std::stod(operand)) * regx, regy);
} else if (operation != Key::kKeyClx) {
} else if (operation != key::kKeyClx) {
// We're about to insert to register X so display current
// token at X as if the stack was lifted already
if (!run_headless)
Expand Down

0 comments on commit 6a71a71

Please sign in to comment.