1 #ifndef PARAMETERS_H
2 #define PARAMETERS_H
3 ///
4 /// \file Parameters.h
5 /// \brief Declaration of Parameters class
6 /// \author Pariterre
7 /// \version 1.0
8 /// \date October 17th, 2018
9 ///
10 
11 #include "Group.h"
12 
13 ///
14 /// \brief Group holder of C3D parameters
15 ///
16 class EZC3D_API ezc3d::ParametersNS::Parameters{
17     //---- CONSTRUCTOR ----//
18 public:
19     ///
20     /// \brief Create a default group holder with minimal groups to have a valid c3d
21     ///
22     Parameters();
23 
24     ///
25     /// \brief Construct group holder from a C3D file
26     /// \param c3d C3D reference to copy the data in
27     /// \param file Already opened fstream file with read access
28     ///
29     Parameters(
30             c3d &c3d,
31             std::fstream &file);
32 
33 public:
34     ///
35     /// \brief Test if a given group is mandatory or not
36     /// \param groupName The name of the group to test
37     /// \return If a given group is mandatory or not
38     ///
39     static bool isMandatory(
40             const std::string& groupName);
41 
42     ///
43     /// \brief Test if a given parameter is mandatory or not
44     /// \param groupName The name of the group that contains the parameter
45     /// \param parameterName The name of the parameter to test
46     /// \return If a given parameter is mandatory or not
47     ///
48     static bool isMandatory(
49             const std::string& groupName,
50             const std::string& parameterName);
51 
52 protected:
53     ///
54     /// \brief Add all required parameter for a c3d to be valid
55     ///
56     void setMandatoryParameters();
57 
58     //---- STREAM ----//
59 public:
60     ///
61     /// \brief Print the groups by calling the print method of all the groups
62     ///
63     void print() const;
64 
65     ///
66     /// \brief Write the groups to an opened file by calling the write method of all the groups
67     /// \param f Already opened fstream file with write access
68     /// \param dataStartPosition Returns the byte where to put the data start parameter
69     /// \param header A reference to the header section
70     /// \param format What order should the file has
71     ///
72     ezc3d::ParametersNS::Parameters write(
73             std::fstream &f,
74             std::streampos &dataStartPosition,
75             const ezc3d::Header& header,
76             const ezc3d::WRITE_FORMAT& format = ezc3d::WRITE_FORMAT::DEFAULT
77             ) const;
78 
79     //---- PARAMETER METADATA ----//
80 protected:
81     ///
82     /// \brief Prepare a copy of all parameters that will be used to write
83     /// \param header A reference to the header section
84     /// \param format What order should the file has
85     /// \return
86     ///
87     Parameters prepareCopyForWriting(
88             const ezc3d::Header& header,
89             const ezc3d::WRITE_FORMAT& format = ezc3d::WRITE_FORMAT::DEFAULT
90             ) const;
91 
92     // Read the Parameters Header
93     size_t _parametersStart;    ///< Byte 1 of the parameter's section of the C3D file.
94                                 ///<
95                                 ///< If the value is 1 then it starts at byte 3
96                                 ///< otherwise, it starts at byte 512*parametersStart
97     size_t _checksum;   ///< Byte 2 of the C3D file
98                         ///<
99                         ///< It should be equals to 0x50 for a valid a c3d
100     size_t _nbParamBlock;   ///< Byte 3 of the C3D file
101                             ///<
102                             ///< Number of 256-bytes blocks the paramertes fits in.
103                             ///< It defines the starting position of the data
104     PROCESSOR_TYPE _processorType;  ///< Byte 4 of the C3D file
105                             ///<
106                             ///< Processor type (83 + [1 Inter, 2 DEC, 3 MIPS])
107 
108 public:
109     ///
110     /// \brief Get the byte in the file where the data starts
111     /// \return The byte in the file where the data starts
112     ///
113     size_t parametersStart() const;
114 
115     ///
116     /// \brief Get the checksum of the parameters
117     /// \return The checksum of the parameters
118     ///
119     /// The chechsum, according to C3D.org documentation, should be equals to 0x50 for a valid C3D
120     ///
121     size_t checksum() const;
122 
123     ///
124     /// \brief Get the number of 256-bytes the parameters need in the file
125     /// \return The number of 256-bytes the parameters need in the file
126     ///
127     size_t nbParamBlock() const;
128 
129     ///
130     /// \brief Get the processor type the file was writen on
131     /// \return The processor type the file was writen on
132     ///
133     /// The processor type is defined by the value 83 + index. Where index is 1 for Intel, 2 for DEC and 3 for MIPS
134     ///
135     PROCESSOR_TYPE processorType() const;
136 
137 
138     //---- GROUPS ----//
139 protected:
140     std::vector<ezc3d::ParametersNS::GroupNS::Group> _groups; ///< Holder for the group of parameters
141 
142 public:
143     /// \brief Get the number of groups
144     /// \return The number of groups
145     ///
146     size_t nbGroups() const;
147 
148     ///
149     /// \brief Return if a group of a specific name exists
150     /// \param groupName The group name to return
151     /// \return If the group exists (true) or not (false)
152     ///
153     bool isGroup(
154             const std::string& groupName) const;
155 
156     ///
157     /// \brief Get the index of a group in the group holder
158     /// \param groupName Name of the group
159     /// \return The index of the group
160     ///
161     /// Search for the index of a group into the group holder by the name of this group.
162     ///
163     /// Throw a std::invalid_argument if groupName is not found
164     ///
165     size_t groupIdx(
166             const std::string& groupName) const;
167 
168     ///
169     /// \brief Get a particular group of index idx from the group holder
170     /// \param idx The index of the group
171     /// \return The group
172     ///
173     /// Get a particular group of index idx from the group holder.
174     ///
175     /// Throw a std::out_of_range exception if idx is larger than the number of groups
176     ///
177     const ezc3d::ParametersNS::GroupNS::Group& group(
178             size_t idx) const;
179 
180     ///
181     /// \brief Get a particular group of index idx from the group holder in order to be modified by the caller
182     /// \param idx The index of the group
183     /// \return The group
184     ///
185     /// Get a particular group of index idx from the group holder in the form of a non-const reference.
186     /// The user can thereafter modify the parameter at will, but with the caution it requires.
187     ///
188     /// Throw a std::out_of_range exception if idx is larger than the number of groups
189     ///
190     ezc3d::ParametersNS::GroupNS::Group& group(
191             size_t idx);
192 
193     ///
194     /// \brief Get a particular group with the name groupName from the group holder
195     /// \param groupName The name of the group
196     /// \return The group
197     ///
198     /// Throw a std::invalid_argument if groupName is not found
199     ///
200     const ezc3d::ParametersNS::GroupNS::Group& group(
201             const std::string& groupName) const;
202 
203     ///
204     /// \brief Get a particular group with the name groupName from the group holder
205     /// \param groupName The name of the group
206     /// \return The group
207     ///
208     /// Throw a std::invalid_argument if groupName is not found
209     ///
210     ezc3d::ParametersNS::GroupNS::Group& group(
211             const std::string& groupName);
212 
213     ///
214     /// \brief Add/replace a group in the group holder
215     /// \param group The group to copy
216     ///
217     /// If the group sent does not exist in the group holder, it is appended. Otherwise it is replaced
218     ///
219     void group(
220             const ezc3d::ParametersNS::GroupNS::Group& group);
221 
222     ///
223     /// \brief Remove a group
224     /// \param name The name of the group to remove
225     ///
226     void remove(
227             const std::string& name);
228 
229     ///
230     /// \brief Remove a group
231     /// \param idx The index of the group to remove
232     ///
233     void remove(
234             size_t idx);
235 
236     ///
237     /// \brief Get all groups the group holder with read-only access
238     /// \return The groups
239     ///
240     const std::vector<ezc3d::ParametersNS::GroupNS::Group>& groups() const;
241 };
242 
243 #endif
244