Blame view

Modif/epsilon-master/apps/shared/expression_model.cpp 2.12 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  #include "function.h"
  #include "poincare_helpers.h"
  #include <string.h>
  #include <cmath>
  #include <assert.h>
  
  using namespace Poincare;
  
  namespace Shared {
  
  ExpressionModel::ExpressionModel() :
    m_text{0},
    m_expression(nullptr),
    m_layout(nullptr)
  {
  }
  
  ExpressionModel::~ExpressionModel() {
    /* We cannot call tidy here because tidy is a virtual function and does not
     * do the same thing for all children class. */
    if (m_layout != nullptr) {
      delete m_layout;
      m_layout = nullptr;
    }
    if (m_expression != nullptr) {
      delete m_expression;
      m_expression = nullptr;
    }
  }
  
  ExpressionModel& ExpressionModel::operator=(const ExpressionModel& other) {
    // Self-assignment is benign
    setContent(other.m_text);
    return *this;
  }
  
  const char * ExpressionModel::text() const {
    return m_text;
  }
  
  Poincare::Expression * ExpressionModel::expression(Poincare::Context * context) const {
    if (m_expression == nullptr) {
      m_expression = PoincareHelpers::ParseAndSimplify(m_text, *context);
    }
    return m_expression;
  }
  
  Poincare::ExpressionLayout * ExpressionModel::layout() {
    if (m_layout == nullptr) {
      Expression * nonSimplifiedExpression = Expression::parse(m_text);
      if (nonSimplifiedExpression != nullptr) {
        m_layout = PoincareHelpers::CreateLayout(nonSimplifiedExpression);
        delete nonSimplifiedExpression;
      }
    }
    return m_layout;
  }
  
  bool ExpressionModel::isDefined() {
    return m_text[0] != 0;
  }
  
  bool ExpressionModel::isEmpty() {
    return m_text[0] == 0;
  }
  
  void ExpressionModel::setContent(const char * c) {
    strlcpy(m_text, c, sizeof(m_text));
    /* We cannot call tidy here because tidy is a virtual function and does not
     * do the same thing for all children class. And here we want to delete only
     * the m_layout and m_expression. */
    if (m_layout != nullptr) {
      delete m_layout;
      m_layout = nullptr;
    }
    if (m_expression != nullptr) {
      delete m_expression;
      m_expression = nullptr;
    }
  }
  
  void ExpressionModel::tidy() {
    if (m_layout != nullptr) {
      delete m_layout;
      m_layout = nullptr;
    }
    if (m_expression != nullptr) {
      delete m_expression;
      m_expression = nullptr;
    }
  }
  
  }