Added clang-format and clang-tidy

This commit is contained in:
Egor 2024-10-21 11:03:12 +03:00
parent a80da53d0c
commit 363778019e
8 changed files with 146 additions and 99 deletions

3
.clang-format Normal file
View file

@ -0,0 +1,3 @@
Language: Cpp
BasedOnStyle: LLVM

39
.clang-tidy Normal file
View file

@ -0,0 +1,39 @@
Checks: "*,
-abseil-*,
-altera-*,
-android-*,
-fuchsia-*,
-google-*,
-llvm*,
-modernize-use-trailing-return-type,
-zircon-*,
-readability-else-after-return,
-readability-static-accessed-through-instance,
-readability-avoid-const-params-in-decls,
-cppcoreguidelines-non-private-member-variables-in-classes,
-misc-non-private-member-variables-in-classes,
"
WarningsAsErrors: ''
HeaderFilterRegex: ''
FormatStyle: none
CheckOptions:
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
- { key: readability-identifier-naming.StructCase, value: CamelCase }
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
- { key: readability-identifier-naming.FunctionCase, value: camelBack }
- { key: readability-identifier-naming.VariableCase, value: lower_case }
- { key: readability-identifier-naming.PrivateMemberSuffix, value: _ }
- { key: readability-identifier-naming.ProtectedMemberSuffix, value: _ }
- { key: readability-identifier-naming.MacroDefinitionCase, value: UPPER_CASE }
- { key: readability-identifier-naming.EnumConstantCase, value: CamelCase }
- { key: readability-identifier-naming.EnumConstantPrefix, value: k }
- { key: readability-identifier-naming.ConstexprVariableCase, value: CamelCase }
- { key: readability-identifier-naming.ConstexprVariablePrefix, value: k }
- { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase }
- { key: readability-identifier-naming.GlobalConstantPrefix, value: k }
- { key: readability-identifier-naming.MemberConstantCase, value: CamelCase }
- { key: readability-identifier-naming.MemberConstantPrefix, value: k }
- { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
- { key: readability-identifier-naming.StaticConstantPrefix, value: k }

1
.gitignore vendored
View file

@ -1 +1,2 @@
build build
.cache

View file

@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.30)
project(yadro-task VERSION 0.1) project(yadro-task VERSION 0.1)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "-Wall")
file(GLOB SOURCES "src/*.cpp") file(GLOB SOURCES "src/*.cpp")

View file

@ -7,56 +7,64 @@
#include <string> #include <string>
/** /**
* Mock tape implementation that uses text files as data source. * Mock tape implementation that uses text files as data source.
* *
* Simulates tape operations by artificially pausing the execution of some methods * Simulates tape operations by artificially pausing the execution of some
* using time intervals, specified in command line arguments or a configuration file. * methods using time intervals, specified in command line arguments or a
* Each cell is represented as a seperate line in a file, cell data is stored as plain text. * configuration file. Each cell is represented as a seperate line in a file,
*/ * cell data is stored as plain text.
*/
class FileTape : public Tape { class FileTape : public Tape {
private: private:
std::fstream file; std::fstream file_;
public: public:
/** ~FileTape() override;
* Initialize a new instance of FileTape that will open a file with the specified name. FileTape &operator=(const FileTape &) = delete;
* File must be read and write accessible. FileTape(const FileTape &) = delete;
* FileTape(FileTape &&) = delete;
* @param file_name Name of a file to be opened. FileTape &operator=(FileTape &&) = delete;
*/
FileTape(std::string file_name);
/** /**
* Advances the underlying fstream to a new line. * Initialize a new instance of FileTape that will open a file with the
* * specified name. File must be read and write accessible.
* @return false if EOF is reached, otherwise true. *
*/ * @param file_name Name of a file to be opened.
bool seekForward() override; */
explicit FileTape(const std::string &fileName);
/** /**
* Rewinds the underlying fstream to a previous line. * Advances the underlying fstream to a new line.
* *
* @return false if called from the first line of a file, otherwise true. * @return false if EOF is reached, otherwise true.
*/ */
bool seekBackwards() override; bool seekForward() override;
/** /**
* Reads and parses data from the current line. * Rewinds the underlying fstream to a previous line.
* *
* @return number from the line, or 0 if data can not be parsed. * @return false if called from the first line of a file, otherwise true.
*/ */
int32_t read() override; bool seekBackwards() override;
/** /**
* Writes data to the current line. * Reads and parses data from the current line.
* *
* @param data Number, that should be written to the current line. * @return number from the line, or 0 if data can not be parsed.
*/ */
void write(int32_t data) override; int32_t read() override;
/** /**
* Closes the underlying fstream * Writes data to the current line.
*/ *
~FileTape() override; * @param data Number, that should be written to the current line.
*/
void write(int32_t data) override;
};
struct FileTapeSettings {
unsigned int readDelayMs, writeDelayMs;
unsigned int seekForwardDelayMs, seekBackwardsDelayMs;
}; };
#endif #endif

View file

@ -4,47 +4,52 @@
#include <cstdint> #include <cstdint>
/** /**
* Abstract class representing a magnetic tape. * Abstract class representing a magnetic tape.
* *
* Tape is a storage medium consisting of N cells, where each cell contains a 32-bit signed integer. * Tape is a storage medium consisting of N cells, where each cell contains a
* Data can be read or written into one cell at a time. Tape can be moved * 32-bit signed integer. Data can be read or written into one cell at a time.
* one cell forward or backwards in order to change the targeted cell. * Tape can be moved one cell forward or backwards in order to change the
* Tape performs every operation sequentially. The amount of data stored on a single tape * targeted cell. Tape performs every operation sequentially. The amount of data
* could exceed the amount of available RAM. Moving tape forward or backwards * stored on a single tape could exceed the amount of available RAM. Moving tape
* is a very time-consuming operation. * forward or backwards is a very time-consuming operation.
*/ */
class Tape { class Tape {
public: public:
/** Tape(const Tape &) = default;
* Advances the tape one cell forward. Tape(Tape &&) = default;
* Tape &operator=(const Tape &) = default;
* @return false if the next cell doesn't exist (reached end of tape), otherwise true. Tape &operator=(Tape &&) = default;
*/ virtual ~Tape() = default;
virtual bool seekForward() = 0;
/** /**
* Rewinds the tape one cell backwards. * Advances the tape one cell forward.
* *
* @return false if the previous cell doesn't exist (reached beginning of tape), otherwise true. * @return false if the next cell doesn't exist (reached end of tape),
*/ * otherwise true.
virtual bool seekBackwards() = 0; */
virtual bool seekForward() = 0;
/** /**
* Reads data from the current cell. * Rewinds the tape one cell backwards.
* *
* @return number, that is stored on the current cell. * @return false if the previous cell doesn't exist (reached beginning of
*/ * tape), otherwise true.
virtual int32_t read() = 0; */
virtual bool seekBackwards() = 0;
/** /**
* Writes data to the current cell. * Reads data from the current cell.
* *
* @param data Number, that should be written to the current cell. * @return number, that is stored on the current cell.
*/ */
virtual void write(int32_t data) = 0; virtual int32_t read() = 0;
// default virtual destructor to prevent memory leaks in implementing classes /**
virtual ~Tape() = default; * Writes data to the current cell.
*
* @param data Number, that should be written to the current cell.
*/
virtual void write(int32_t data) = 0;
}; };
#endif #endif

View file

@ -3,26 +3,16 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
FileTape::FileTape(std::string file_name) { FileTape::FileTape(const std::string &fileName) {
this->file = std::fstream(file_name); this->file_ = std::fstream(fileName);
} }
bool FileTape::seekForward() { bool FileTape::seekForward() { return true; }
return true;
}
bool FileTape::seekBackwards() { bool FileTape::seekBackwards() { return true; }
return true;
}
int32_t FileTape::read() { int32_t FileTape::read() { return 0; }
return 0;
}
void FileTape::write(int32_t data) { void FileTape::write(int32_t data) {}
}
FileTape::~FileTape() { FileTape::~FileTape() { this->file_.close(); }
this->file.close();
}

View file

@ -1,7 +1,9 @@
#include "filetape.h" #include "filetape.h"
#include <memory> #include <iostream>
#include <ostream>
int main(int argc, char *argv[]) { int main(int /*argc*/, char * /*argv*/[]) {
std::unique_ptr<Tape> tape(new FileTape("test")); FileTape tape("waw");
return 0; std::cout << "Hello, World!" << std::endl;
return 0;
} }