shushus

Спринт 16. Готовимся к дизайн-ревью.

Jun 20th, 2023
1,302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.25 KB | None | 0 0
  1.  
  2. В этой пасте приведен пример моих заголовочных файлов, которые были сданы на дизайн-ревью.
  3. В них добавлены предполагаемые сигнатуры новых методов программы.
  4.  
  5. При сдаче вам потребуется также предоставить pdf-диаграммы взаимосвязей в вашем проекте, а именно:
  6.  
  7.     formula_calculation.pdf
  8.     cyclic_dependencies.pdf
  9.     cache_invalidation.pdf
  10.     new_cell.pdf
  11.  
  12. Прикрепляю ссылку на примерные pdf-диаграммы для дизайн-ревью: https://github.com/uetto/design-review-pdf
  13.  
  14. -----------------------------------------------------------------------
  15. cell.h
  16. -----------------------------------------------------------------------
  17. #pragma once
  18.  
  19. #include "common.h"
  20. #include "formula.h"
  21.  
  22. class Cell : public CellInterface {
  23. public:
  24.     Cell();
  25.     ~Cell();
  26.  
  27.     void Set(std::string text);
  28.     void Clear();
  29.  
  30.     Value GetValue() const override;
  31.     std::string GetText() const override;
  32.  
  33. private:
  34.  
  35.     std::unique_ptr<Impl> impl_;
  36.     SheetInterface& sheet_;
  37.  
  38.     set<Cell*> dependent_cells_;        //ячейки в которых вычисляем (меняем их значения)
  39.     set<Cell*> referenced_cells_;       //ячейки используемые при вычислении (берем их значения)
  40.  
  41.     mutable std::optional<Value> cache_;        //вычисленное значение ячейки хранящееся в кеше
  42.  
  43.     class Impl {
  44.     public:
  45.  
  46.         virtual Value GetValue() const = 0;
  47.         virtual std::string GetText() const = 0;
  48.         virtual std::vector<Position> GetReferencedCells() const = 0;
  49.  
  50.         virtual ~Impl() = default;
  51.     };
  52.    
  53.     class EmptyImpl : public Impl {
  54.     public:    
  55.  
  56.         Value GetValue() const override;
  57.         std::string GetText() const override;      
  58.     };
  59.    
  60.     class TextImpl : public Impl {
  61.     public:
  62.        
  63.         explicit TextImpl(std::string text);
  64.         Value GetValue() const override;      
  65.         std::string GetText() const override;
  66.        
  67.     private:
  68.         std::string text_;        
  69.     };
  70.    
  71.     class FormulaImpl : public Impl {
  72.     public:
  73.        
  74.         explicit FormulaImpl(std::string text, const SheetInterface& sheet);
  75.         Value GetValue() const override;
  76.         std::string GetText() const override;
  77.  
  78.     std::vector<Position> GetReferencedCells() const override;
  79.        
  80.     private:
  81.         std::unique_ptr<FormulaInterface> formula_ptr_;
  82.     };
  83.  
  84.    
  85.     bool HasCircularDependency(Impl& impl);
  86.     void InvalidateCache();
  87.     void RefreshCells();
  88. };
  89. -----------------------------------------------------------------------
  90. formula.h
  91. -----------------------------------------------------------------------
  92. #pragma once
  93.  
  94. #include "common.h"
  95.  
  96. #include "FormulaAST.h"
  97.  
  98. #include <memory>
  99. #include <variant>
  100.  
  101. class FormulaInterface {
  102. public:
  103.     using Value = std::variant<double, FormulaError>;
  104.  
  105.     virtual Value Evaluate(const SheetInterface& sheet) const = 0;
  106.     virtual std::string GetExpression() const = 0;
  107.     virtual std::vector<Position> GetReferencedCells() const = 0;
  108.  
  109.     virtual ~FormulaInterface() = default;
  110. };
  111.  
  112. std::unique_ptr<FormulaInterface> ParseFormula(std::string expression);
  113. -----------------------------------------------------------------------
  114. FormulaAST.h
  115. -----------------------------------------------------------------------
  116. #pragma once
  117.  
  118. #include "FormulaLexer.h"
  119. #include "common.h"
  120.  
  121. #include <forward_list>
  122. #include <functional>
  123. #include <stdexcept>
  124.  
  125. namespace ASTImpl {
  126. class Expr;
  127. }
  128.  
  129. class ParsingError : public std::runtime_error {
  130.     using std::runtime_error::runtime_error;
  131. };
  132.  
  133. class FormulaAST {
  134. public:
  135.     explicit FormulaAST(std::unique_ptr<ASTImpl::Expr> root_expr);
  136.     FormulaAST(FormulaAST&&) = default;
  137.     FormulaAST& operator=(FormulaAST&&) = default;
  138.     ~FormulaAST();
  139.  
  140.     double Execute(std::function<double(Position* pos)> cell_pos_func) const;
  141.     void Print(std::ostream& out) const;
  142.     void PrintFormula(std::ostream& out) const;
  143.  
  144.     std::list<Position>& GetCells();
  145.  
  146. private:
  147.     std::unique_ptr<ASTImpl::Expr> root_expr_;
  148.     std::list<Position> cells_;
  149. };
  150.  
  151. FormulaAST ParseFormulaAST(std::istream& in);
  152. FormulaAST ParseFormulaAST(const std::string& in_str);
  153. -----------------------------------------------------------------------
  154. sheet.h
  155. -----------------------------------------------------------------------
  156. #pragma once
  157.  
  158. #include "cell.h"
  159. #include "common.h"
  160.  
  161. #include <functional>
  162. #include <vector>
  163.  
  164. using Table = std::vector<std::vector<std::unique_ptr<Cell>>>;
  165.  
  166. class Sheet : public SheetInterface {
  167. public:
  168.     ~Sheet();
  169.  
  170.     void SetCell(Position pos, std::string text) override;
  171.  
  172.     const CellInterface* GetCell(Position pos) const override;
  173.     CellInterface* GetCell(Position pos) override;
  174.  
  175.     void ClearCell(Position pos) override;
  176.  
  177.     Size GetPrintableSize() const override;
  178.  
  179.     void PrintValues(std::ostream& output) const override;
  180.     void PrintTexts(std::ostream& output) const override;
  181.  
  182. private:
  183.     Table cells_;
  184. };
Advertisement
Add Comment
Please, Sign In to add comment