Numworks Epsilon  1.4.1
Graphing Calculator Operating System
function.cpp
Go to the documentation of this file.
1 #include "function.h"
2 #include <string.h>
3 #include <cmath>
4 #include <assert.h>
5 
6 using namespace Poincare;
7 
8 namespace Shared {
9 
10 Function::Function(const char * name, KDColor color) :
11  m_expression(nullptr),
12  m_text{0},
13  m_name(name),
14  m_color(color),
15  m_layout(nullptr),
16  m_active(true)
17 {
18 }
19 
21  // Self-assignment is benign
22  m_color = other.m_color;
23  m_name = other.m_name;
24  m_active = other.m_active;
25  setContent(other.m_text);
26  return *this;
27 }
28 
30  char data[k_dataLengthInBytes/sizeof(char)] = {};
32  data[k_dataLengthInBytes-2] = m_name != nullptr ? m_name[0] : 0;
33  data[k_dataLengthInBytes-1] = m_active ? 1 : 0;
34  return Ion::crc32((uint32_t *)data, k_dataLengthInBytes/sizeof(uint32_t));
35 }
36 
37 void Function::setContent(const char * c) {
38  strlcpy(m_text, c, sizeof(m_text));
39  if (m_layout != nullptr) {
40  delete m_layout;
41  m_layout = nullptr;
42  }
43  if (m_expression != nullptr) {
44  delete m_expression;
45  m_expression = nullptr;
46  }
47 }
48 
50  m_color = color;
51 }
52 
54  if (m_layout != nullptr) {
55  delete m_layout;
56  m_layout = nullptr;
57  }
58  if (m_expression != nullptr) {
59  delete m_expression;
60  m_expression = nullptr;
61  }
62 }
63 
64 const char * Function::text() const {
65  return m_text;
66 }
67 
68 const char * Function::name() const {
69  return m_name;
70 }
71 
73  if (m_expression == nullptr) {
74  m_expression = Expression::ParseAndSimplify(m_text, *context);
75  }
76  return m_expression;
77 }
78 
80  if (m_layout == nullptr) {
81  Expression * nonSimplifiedExpression = Expression::parse(m_text);
82  if (nonSimplifiedExpression != nullptr) {
83  m_layout = nonSimplifiedExpression->createLayout(PrintFloat::Mode::Decimal);
84  delete nonSimplifiedExpression;
85  }
86  }
87  return m_layout;
88 }
89 
91  return m_text[0] != 0;
92 }
93 
95  return m_active;
96 }
97 
98 void Function::setActive(bool active) {
99  m_active = active;
100 }
101 
103  return m_text[0] == 0;
104 }
105 
106 template<typename T>
107 T Function::templatedApproximateAtAbscissa(T x, Poincare::Context * context) const {
108  Poincare::VariableContext<T> variableContext = Poincare::VariableContext<T>(symbol(), context);
109  Poincare::Symbol xSymbol(symbol());
111  variableContext.setExpressionForSymbolName(&e, &xSymbol, variableContext);
112  return expression(context)->approximateToScalar<T>(variableContext);
113 }
114 
116  if (m_layout != nullptr) {
117  delete m_layout;
118  m_layout = nullptr;
119  }
120  if (m_expression != nullptr) {
121  delete m_expression;
122  m_expression = nullptr;
123  }
124 }
125 
126 }
127 
128 template float Shared::Function::templatedApproximateAtAbscissa<float>(float, Poincare::Context*) const;
129 template double Shared::Function::templatedApproximateAtAbscissa<double>(double, Poincare::Context*) const;
virtual void setContent(const char *c)
Definition: function.cpp:37
void setColor(KDColor m_color)
Definition: function.cpp:49
static constexpr int maxBufferSize()
Definition: text_field.h:23
void(* Function)(const char *input)
Definition: command.h:11
def data
Definition: i18n.py:176
virtual void tidy()
Definition: function.cpp:115
const char * text() const
Definition: function.cpp:64
#define T(x)
Definition: events.cpp:26
size_t strlcpy(char *dst, const char *src, size_t len)
Definition: strlcpy.c:3
KDColor color() const
Definition: function.h:21
virtual bool isEmpty()
Definition: function.cpp:102
Poincare::Expression * expression(Poincare::Context *context) const
Definition: function.cpp:72
c(generic_all_nodes)
void setActive(bool active)
Definition: function.cpp:98
virtual uint32_t checksum()
Definition: function.cpp:29
unsigned int uint32_t
Definition: stdint.h:6
const char * name() const
Definition: function.cpp:68
ExpressionLayout * createLayout(PrintFloat::Mode floatDisplayMode=PrintFloat::Mode::Default, ComplexFormat complexFormat=ComplexFormat::Default) const
Definition: expression.cpp:244
Poincare::ExpressionLayout * layout()
Definition: function.cpp:79
static Complex< T > Float(T x)
Definition: complex.cpp:23
virtual bool isDefined()
Definition: function.cpp:90
Definition: color.h:6
uint32_t crc32(const uint32_t *data, size_t length)
Definition: device.cpp:37
Function & operator=(const Function &other)
Definition: function.cpp:20
virtual ~Function()
Definition: function.cpp:53
T approximateToScalar(Context &context, AngleUnit angleUnit=AngleUnit::Default) const
Definition: expression.cpp:347
void setExpressionForSymbolName(const Expression *expression, const Symbol *symbol, Context &context) override