OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COURGETTE_COURGETTE_FLOW_H_ | |
6 #define COURGETTE_COURGETTE_FLOW_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 | |
11 #include "base/macros.h" | |
12 #include "courgette/courgette.h" | |
13 #include "courgette/region.h" | |
14 #include "courgette/streams.h" | |
15 | |
16 namespace courgette { | |
17 | |
18 class AssemblyProgram; | |
19 class EncodedProgram; | |
20 | |
21 // An adaptor for Region as BasicBuffer. | |
22 class RegionBuffer : public BasicBuffer { | |
23 public: | |
24 explicit RegionBuffer(const Region& region) : region_(region) {} | |
25 | |
26 // BasicBuffer: | |
27 const uint8_t* data() const override { return region_.start(); } | |
28 size_t length() const override { return region_.length(); } | |
29 | |
30 private: | |
31 Region region_; | |
32 | |
33 DISALLOW_COPY_AND_ASSIGN(RegionBuffer); | |
34 }; | |
35 | |
36 // CourgetteFlow stores Courgette data arranged into groups, and exposes | |
37 // "commands" that operate on them. On the first occurrence of an error, the | |
38 // Courgette error code is recorded, error messages are generated and stored, | |
39 // and all subsequent commands become no-op. This allows callers to concisely | |
40 // specify high-level logic with minimal code for error handling. | |
41 class CourgetteFlow { | |
42 public: | |
43 // A group of Courgette data, for a single executable. Takes negligible space | |
44 // when unused. | |
45 struct Data { | |
46 Data(); | |
47 ~Data(); | |
48 | |
49 std::unique_ptr<AssemblyProgram> program; | |
50 std::unique_ptr<EncodedProgram> encoded; | |
51 SinkStreamSet sinks; | |
52 SourceStreamSet sources; | |
53 }; | |
54 | |
55 // Group enumeration into |data_*_| fields. | |
56 enum Group { ONLY, OLD, NEW }; | |
chrisha
2017/04/24 18:15:50
Could use comments? What is "ONLY"?
huangs
2017/04/24 20:08:14
Done.
| |
57 | |
58 CourgetteFlow(); | |
59 ~CourgetteFlow(); | |
60 | |
61 static const char* name(Group group); | |
62 Data* data(Group group); // Allows caller to modify. | |
63 bool ok(); | |
64 bool failed(); | |
65 Status status(); | |
66 const std::string& message(); | |
67 | |
68 // Commands that perform no-op on error. This allows caller to concisely | |
chrisha
2017/04/24 18:15:50
remove extra space
huangs
2017/04/24 20:08:14
Done.
| |
69 // specify high-level logic, and perform a single error check at the end. Care | |
70 // must be taken w.r.t. error handling if |data()| is harvested between | |
71 // commands. | |
72 | |
73 void ReadSourceStreamSetFromBuffer(Group group, const BasicBuffer& buffer); | |
chrisha
2017/04/24 18:15:50
In general, all of these functions could use comme
huangs
2017/04/24 20:08:14
Done.
| |
74 | |
75 void ReadAssemblyProgramFromBuffer(Group group, | |
76 const BasicBuffer& buffer, | |
77 bool annotate); | |
78 | |
79 // If |opt_sources| is unspecified then |data(which)->sources| is used. | |
chrisha
2017/04/24 18:15:50
|which|? Do you mean |group|?
huangs
2017/04/24 20:08:14
Done.
| |
80 void ReadEncodedProgramFromSourceStreamSet( | |
81 Group group, | |
82 SourceStreamSet* opt_sources = nullptr); | |
83 | |
84 void CreateEncodedProgramFromAssemblyProgram(Group group); | |
85 | |
86 void WriteSinkStreamFromSinkStreamSet(Group group, SinkStream* sink); | |
87 | |
88 // If |opt_sinks| is unspecified then |data(which)->sinks| is used. | |
89 void WriteSinkStreamSetFromEncodedProgram(Group group, | |
90 SinkStreamSet* opt_sinks = nullptr); | |
91 | |
92 void WriteExecutableFromEncodedProgram(Group group, SinkStream* sink); | |
93 | |
94 void AdjustNewAssemblyProgramToMatchOld(); | |
95 | |
96 // Destructor commands to reduce memory usage. | |
97 | |
98 void DestroyAssemblyProgram(Group group); | |
99 | |
100 void DestroyEncodedProgram(Group group); | |
101 | |
102 private: | |
103 // Utilities to process return values from Courgette functions, and assign | |
104 // |status_| and |message_|. Usage: | |
105 // if (!check(some_couregtte_function(param1, ...))) | |
chrisha
2017/04/24 18:15:50
courgette*
huangs
2017/04/24 20:08:14
Done.
| |
106 // setMessage("format string %s...", value1, ...); | |
107 | |
108 // Reassigns |status_|, and returns true if |C_OK|. | |
109 bool check(Status new_status); | |
110 | |
111 // check() alternative for functions that return true on succes. On failure | |
112 // assigns |status_| to |failure_mode|. | |
113 bool check(bool success, Status failure_mode); | |
114 | |
115 void setMessage(const char* format, ...); | |
116 | |
117 Status status_ = C_OK; | |
118 std::string message_; | |
119 Data data_only_; | |
120 Data data_old_; | |
121 Data data_new_; | |
122 | |
123 DISALLOW_COPY_AND_ASSIGN(CourgetteFlow); | |
124 }; | |
125 | |
126 } // namespace courgette | |
127 | |
128 #endif // COURGETTE_COURGETTE_FLOW_H_ | |
OLD | NEW |