/
lyapdy
/
Object_oriented_programming_cpp_318
Обзор
Документация
Войти
/
lyapdy
/
Object_oriented_programming_cpp_318
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lab8/task_5.cpp
258 строк
7 KB
danillyapunov
tasks 7 and 8 added
30 ноя 2025, 14:31
30 ноя 2025, 14:31
5a9b53a
Код
Авторство
О чём код?
#include <iostream> #include <thread> #include <chrono> #include <atomic> #include <stdexcept> #include <string> template<typename T> class RingBuffer { private: T* buffer; size_t capacity; size_t head; size_t tail; size_t count; std::atomic<bool> writing_active; // ����頥� �������� RingBuffer(const RingBuffer&) = delete; RingBuffer& operator=(const RingBuffer&) = delete; public: // ���� ��������� (����頥� ���� �८�ࠧ������) explicit RingBuffer(size_t size) : capacity(size), head(0), tail(0), count(0), writing_active(false) { if (size == 0) { throw std::invalid_argument("Buffer size cannot be zero"); } buffer = new (std::nothrow) T[capacity]; if (!buffer) { throw std::runtime_error("Failed to allocate memory for ring buffer"); } } ~RingBuffer() { writing_active = false; if (buffer) { delete[] buffer; } } // ������ �⥭�� ������ ���� T read() { if (empty()) { throw std::runtime_error("Buffer is empty"); } T value = buffer[head]; head = (head + 1) % capacity; --count; return value; } // ������ ����� ������ void write(const T& value) { buffer[tail] = value; tail = (tail + 1) % capacity; if (count < capacity) { ++count; } else { // ��१����뢠�� ���� ����� head = (head + 1) % capacity; } } // ��ઠ �� ������ bool empty() const { return count == 0; } // ��ઠ �� ������� bool full() const { return count == capacity; } // ����祭�� ������⢠ ������⮢ size_t size() const { return count; } // ����祭�� ������ size_t getCapacity() const { return capacity; } // ��ॣ�㧪� ������ [] ��� ����㯠 � ������⠬ T& operator[](size_t index) { if (index >= count) { throw std::out_of_range("Index out of range"); } return buffer[(head + index) % capacity]; } const T& operator[](size_t index) const { if (index >= count) { throw std::out_of_range("Index out of range"); } return buffer[(head + index) % capacity]; } // ��䨪�� ���६��� (ᤢ�� ������) RingBuffer& operator++() { if (!empty()) { head = (head + 1) % capacity; --count; } return *this; } // ����䨪�� ���६��� RingBuffer operator++(int) { RingBuffer temp = *this; ++(*this); return temp; } // ��ॣ�㧪� ������ + ��� ����� RingBuffer& operator+(const T& value) { write(value); return *this; } // �뢮� ��� ������⮢ void printAll() const { std::cout << "Buffer contents (" << count << "/" << capacity << "): "; for (size_t i = 0; i < count; ++i) { std::cout << buffer[(head + i) % capacity] << " "; } std::cout << std::endl; } // �뢮� ������⮢ � ������1 �� ������2 void printRange(size_t index1, size_t index2) const { if (index1 >= count || index2 >= count || index1 > index2) { throw std::out_of_range("Invalid index range"); } std::cout << "Buffer range [" << index1 << "-" << index2 << "]: "; for (size_t i = index1; i <= index2; ++i) { std::cout << buffer[(head + i) % capacity] << " "; } std::cout << std::endl; } // ����� ��⮪� ����� void startWriting() { writing_active = true; std::thread writer(&RingBuffer::writingThread, this); writer.detach(); } // ��⠭���� ��⮪� ����� void stopWriting() { writing_active = false; } private: // ��⮪ ��� ����� void writingThread() { static int value = 1; while (writing_active) { // �����뢠�� ������⥫�� ���祭�� ��� ��������樨 "��������������" *this + value++; std::cout << "Written: " << value - 1 << " | "; printAll(); std::this_thread::sleep_for(std::chrono::milliseconds(500)); } } // ��ॣ�㧪� ����� �����/�뢮�� friend std::ostream& operator<<(std::ostream& os, const RingBuffer& rb) { os << "RingBuffer[" << rb.count << "/" << rb.capacity << "]: "; for (size_t i = 0; i < rb.count; ++i) { os << rb.buffer[(rb.head + i) % rb.capacity] << " "; } return os; } friend std::istream& operator>>(std::istream& is, RingBuffer& rb) { T value; if (is >> value) { rb.write(value); } return is; } }; // �㭪�� ��� ��������樨 ࠡ��� void demonstrateRingBuffer() { try { // ������� ����楢�� ���� ࠧ�� 5 RingBuffer<int> buffer(5); std::cout << "=== Ring Buffer Demonstration ===" << std::endl; std::cout << "Buffer capacity: " << buffer.getCapacity() << std::endl; // ���������� ������� ����権 std::cout << "\n1. Basic operations:" << std::endl; buffer + 10 + 20 + 30; buffer.printAll(); // �ᯮ�짮����� ������ [] std::cout << "\n2. Using operator[]:" << std::endl; std::cout << "buffer[0] = " << buffer[0] << std::endl; std::cout << "buffer[1] = " << buffer[1] << std::endl; // �ᯮ�짮����� ������ ++ std::cout << "\n3. Using operator++ (removing first element):" << std::endl; ++buffer; buffer.printAll(); // ���������� ��९������� std::cout << "\n4. Demonstrating circular behavior:" << std::endl; buffer + 40 + 50 + 60 + 70; buffer.printAll(); // �뢮� ��������� std::cout << "\n5. Printing range:" << std::endl; buffer.printRange(1, 3); // ���������� ��⮪���� ����� std::cout << "\n6. Starting writing thread (press 'q' + Enter to stop):" << std::endl; buffer.startWriting(); // �������� ������� ��⠭���� std::string input; while (std::cin >> input) { if (input == "q" || input == "Q") { buffer.stopWriting(); std::cout << "Stopping writing thread..." << std::endl; break; } // ����� ⠪�� ��������� ���祭�� ������ try { int value = std::stoi(input); buffer + value; std::cout << "Manually added: " << value << " | "; buffer.printAll(); } catch (const std::exception& e) { std::cout << "Invalid input. Enter number or 'q' to quit." << std::endl; } } // �����쭮� ���ﭨ� std::cout << "\nFinal buffer state:" << std::endl; buffer.printAll(); } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } } int task_5() { demonstrateRingBuffer(); return 0; }