diff --git a/apps/calcul/Makefile b/apps/calcul/Makefile index 866ecd119..d214874fc 100644 --- a/apps/calcul/Makefile +++ b/apps/calcul/Makefile @@ -1,6 +1,7 @@ app_objs += $(addprefix apps/calcul/,\ app.o\ calcul_controller.o\ + calcul.o\ ) inline_images += apps/calcul/calcul_icon.png diff --git a/apps/calcul/calcul.cpp b/apps/calcul/calcul.cpp new file mode 100644 index 000000000..47f5c24bc --- /dev/null +++ b/apps/calcul/calcul.cpp @@ -0,0 +1,55 @@ +#include "calcul.h" +#include + +namespace Calcul { + +Calcul::Calcul() : + m_text("1+3"), + m_expression(nullptr), + m_layout(nullptr), + m_evaluation(Float(3.0f)) +{ + // TODO: this is temporary. To test the render. + m_expression = Expression::parse(m_text); + m_layout = expression()->createLayout(); +} + +void Calcul::setContent(const char * c, Context * context) { + strlcpy(m_text, c, sizeof(m_text)); + if (m_expression != nullptr) { + delete m_expression; + } + m_expression = Expression::parse(m_text); + if (m_layout != nullptr) { + delete m_layout; + } + m_layout = expression()->createLayout(); + m_evaluation = m_expression->approximate(*context); +} + +Calcul::~Calcul() { + if (m_layout != nullptr) { + delete m_layout; + } + if (m_expression != nullptr) { + delete m_expression; + } +} + +const char * Calcul::text() { + return m_text; +} + +Expression * Calcul::expression() { + return m_expression; +} + +ExpressionLayout * Calcul::layout() { + return m_layout; +} + +Float * Calcul::evaluation() { + return &m_evaluation; +} + +} diff --git a/apps/calcul/calcul.h b/apps/calcul/calcul.h new file mode 100644 index 000000000..d2defa005 --- /dev/null +++ b/apps/calcul/calcul.h @@ -0,0 +1,27 @@ +#ifndef CALCUL_CALCUL_H +#define CALCUL_CALCUL_H + +#include + +namespace Calcul { + +class Calcul { +public: + Calcul(); + ~Calcul(); // Delete expression and layout, if needed + const char * text(); + Expression * expression(); + ExpressionLayout * layout(); + Float * evaluation(); + void setContent(const char * c, Context * context); +private: + constexpr static int k_bodyLength = 255; + char m_text[k_bodyLength]; + Expression * m_expression; + ExpressionLayout * m_layout; + Float m_evaluation; +}; + +} + +#endif