10 Commits

Author SHA1 Message Date
6ed7befa47 Doxygen generated by local qwen3-coder-next 2026-02-28 18:32:00 +03:00
0878891cd8 more ai generated doc with human review 2026-02-28 12:29:00 +03:00
077f12c9e5 Merge branch 'master' into doc_ai 2026-02-27 23:59:05 +03:00
9588b48105 PIVector2D - add funcs, optimize, tests, fixes, doxygen (#194)
Reviewed-on: #194
Co-authored-by: andrey.bychkov <andrey@signalmodelling.ru>
Co-committed-by: andrey.bychkov <andrey@signalmodelling.ru>
2026-02-27 23:58:44 +03:00
b53ef184dc add doxygen via opencode 2026-02-27 23:54:48 +03:00
1739836a18 fix pistatistic mean calc 2026-02-25 17:50:56 +03:00
7195734765 add more tests 2026-02-17 21:53:18 +03:00
8ecec6b914 add more funcs 2026-02-17 21:40:36 +03:00
9029bcf099 Vibecoding PIVector2D - add funcs and doc
Добавь в файл pivector2d.h комментарии для Doxygen ко всем классам и
всем функциям.
Комментарии должны быть в таком же стиле как в файле pivector.h.

Проанализируй функциональность классов pivector2d в файле pivector2d.h и
класс pivector в файле pivector.h и добавь недостающую функциональность
в pivector2d по аналогии с pivector.
2026-02-17 21:04:40 +03:00
6f1660fd9e version 5.5.5
fix PIHIDevice enumeration for Windows
2026-02-13 18:47:33 +03:00
140 changed files with 10330 additions and 1942 deletions

4
.gitignore vendored
View File

@@ -1,8 +1,10 @@
/.*
/src_main/piversion.h
/.svn
/doc/rtf
_unsused
CMakeLists.txt.user*
/include
/release
/build*
/AGENTS.md
/plans

View File

@@ -5,8 +5,8 @@ if (POLICY CMP0177)
endif()
project(PIP)
set(PIP_MAJOR 5)
set(PIP_MINOR 5)
set(PIP_REVISION 4)
set(PIP_MINOR 6)
set(PIP_REVISION 0)
set(PIP_SUFFIX )
set(PIP_COMPANY SHS)
set(PIP_DOMAIN org.SHS)
@@ -72,9 +72,11 @@ option(COVERAGE "Build project with coverage info" OFF)
option(PIP_FFTW_F "Support fftw module for float" ON)
option(PIP_FFTW_L "Support fftw module for long double" ON)
option(PIP_FFTW_Q "Support fftw module for quad double" OFF)
set(PIP_UTILS 1)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
shstk_is_parent_exists(_pe)
if (_pe)

115
README.md
View File

@@ -40,3 +40,118 @@ You should add ${<out_var>} to your target.
[🇷🇺 Онлайн документация](https://shstk.ru/pip/html/ru/index.html)
[🇷🇺 Qt-help](https://shstk.ru/pip/pip_ru.qch)
## Основные опции сборки
### Стандартные опции (option())
| Опция | Описание | По умолчанию |
|-------|----------|--------------|
| `ICU` | ICU support для конвертации кодовых страниц | ON (кроме Win/Android/Apple) |
| `STD_IOSTREAM` | Поддержка std::iostream операторов | OFF |
| `INTROSPECTION` | Сборка с интроспекцией | OFF |
| `TESTS` | Сборка тестов | OFF |
| `COVERAGE` | Сборка с информацией о покрытии | OFF |
| `PIP_FFTW_F` | Поддержка FFTW для float | ON |
| `PIP_FFTW_L` | Поддержка FFTW для long double | ON |
| `PIP_FFTW_Q` | Поддержка FFTW для quad double | OFF |
### Опции модулей (PIP_BUILD_*)
| Опция | Модуль |
|-------|--------|
| `PIP_BUILD_CONSOLE` | console |
| `PIP_BUILD_CRYPT` | crypt (требует libsodium) |
| `PIP_BUILD_COMPRESS` | compress (требует zlib) |
| `PIP_BUILD_USB` | usb |
| `PIP_BUILD_FFTW` | fftw |
| `PIP_BUILD_OPENCL` | opencl |
| `PIP_BUILD_IO_UTILS` | io_utils |
| `PIP_BUILD_CLIENT_SERVER` | client_server |
| `PIP_BUILD_CLOUD` | cloud |
| `PIP_BUILD_LUA` | lua |
| `PIP_BUILD_HTTP_CLIENT` | http_client (требует libcurl) |
| `PIP_BUILD_HTTP_SERVER` | http_server (требует libmicrohttpd) |
### Дополнительные переменные
| Переменная | Описание |
|------------|----------|
| `PIP_BUILD_DEBUG` | Сборка debug версии |
| `PIP_FREERTOS` | Режим сборки для FreeRTOS |
| `CROSSTOOLS` | Собрать инструменты кросс-сборки под хостовую систему (pip_cmg, pip_rc, ...) |
| `LOCAL` | Локальная установка (bin/lib/include) |
| `PIP_CONTAINERS_MIN_ALLOC` | Переопределить минимальный размер аллокации контейнеров |
| `PIP_CONTAINERS_MAX_POT_ALLOC` | Переопределить максимальный размер дополнительной аллокации (поддерживает X_KiB, X_MiB) |
### Примеры использования
```bash
# Базовая сборка с тестами
cmake -B build -DTESTS=ON
# Сборка с покрытием и ICU
cmake -B build -DTESTS=ON -DCOVERAGE=ON -DICU=ON
# Отключение отдельных модулей
cmake -B build -DPIP_BUILD_CRYPT=OFF -DPIP_BUILD_OPENCL=OFF
# Переопределение параметров контейнеров
cmake -B build -DPIP_CONTAINERS_MIN_ALLOC=64
# Локальная установка
cmake -B build -DLOCAL=ON
```
## PIP Dependencies
### Встроенные (included in 3rd/)
| Библиотека | Назначение | Модуль PIP |
|------------|------------|------------|
| **PCRE2** | Регулярные выражения | main (internal) |
| **BLAKE2** | Хеширование | main (internal) |
| **SipHash** | Хеширование | main (internal) |
| **Lua** | Lua scripting | lua |
| **LuaBridge** | Lua bindings | lua |
### Внешние (системные)
| Библиотека | Опция | Модуль PIP |
|------------|-------|------------|
| **ICU** | `-DICU=ON` | main (string conversion) |
| **zlib** | `PIP_BUILD_COMPRESS` | compress |
| **libsodium** | `PIP_BUILD_CRYPT` | crypt, io_utils, cloud |
| **libusb** | `PIP_BUILD_USB` | usb |
| **FFTW3** (+ threads) | `PIP_BUILD_FFTW` | fftw |
| **OpenCL** | `PIP_BUILD_OPENCL` | opencl |
| **libmicrohttpd** | `PIP_BUILD_HTTP_SERVER` | http_server |
| **libcurl** | `PIP_BUILD_HTTP_CLIENT` | http_client |
### Опциональные (тесты/документация)
| Инструмент | Назначение |
|------------|------------|
| **Google Test** | Тестирование (fetched automatically) |
| **Doxygen** | Генерация документации |
### Схема зависимостей модулей
```
main (core)
├── PCRE2 (встроен)
├── BLAKE2 (встроен)
├── SipHash (встроен)
└── ICU (опционально)
console → main
compress → zlib
crypt → libsodium
usb → libusb
fftw → FFTW3
opencl → OpenCL
io_utils → [crypt, если доступен]
client_server → io_utils
cloud → io_utils, crypt
lua → Lua (встроен), LuaBridge (встроен)
http_server → libmicrohttpd
http_client → libcurl
```

View File

@@ -64,13 +64,25 @@ public:
//! \~english Returns unparsed command-line argument by index "index". Index 0 is program execute command.
//! \~russian Возвращает исходный аргумент командной строки по индексу "index". Индекс 0 это команда вызова программы.
PIString rawArgument(int index);
//! \~english Returns mandatory command-line argument by index "index".
//! \~russian Возвращает обязательный аргумент командной строки по индексу "index".
PIString mandatoryArgument(int index);
//! \~english Returns optional command-line argument by index "index".
//! \~russian Возвращает опциональный аргумент командной строки по индексу "index".
PIString optionalArgument(int index);
//! \~english Returns unparsed command-line arguments.
//! \~russian Возвращает исходные аргументы командной строки.
const PIStringList & rawArguments();
//! \~english Returns mandatory command-line arguments.
//! \~russian Возвращает обязательные аргументы командной строки.
const PIStringList & mandatoryArguments();
//! \~english Returns optional command-line arguments.
//! \~russian Возвращает опциональные аргументы командной строки.
const PIStringList & optionalArguments();
//! \~english Returns program execute command without arguments.
@@ -93,18 +105,52 @@ public:
//! \~russian Возвращает полный ключ аргумента "name" или пустую строку, если аргумента нет.
PIString argumentFullKey(const PIString & name);
//! \~english Returns short key prefix.
//! \~russian Возвращает префикс короткого ключа.
const PIString & shortKeyPrefix() const { return _prefix_short; }
//! \~english Returns full key prefix.
//! \~russian Возвращает префикс полного ключа.
const PIString & fullKeyPrefix() const { return _prefix_full; }
//! \~english Returns mandatory arguments count.
//! \~russian Возвращает количество обязательных аргументов.
int mandatoryArgumentsCount() const { return _count_mand; }
//! \~english Returns optional arguments count.
//! \~russian Возвращает количество опциональных аргументов.
int optionalArgumentsCount() const { return _count_opt; }
//! \~english Sets short key prefix.
//! \~russian Устанавливает префикс короткого ключа.
void setShortKeyPrefix(const PIString & prefix);
//! \~english Sets full key prefix.
//! \~russian Устанавливает префикс полного ключа.
void setFullKeyPrefix(const PIString & prefix);
//! \~english Sets mandatory arguments count.
//! \~russian Устанавливает количество обязательных аргументов.
void setMandatoryArgumentsCount(const int count);
//! \~english Sets optional arguments count.
//! \~russian Устанавливает количество опциональных аргументов.
void setOptionalArgumentsCount(const int count);
//! \~english Returns debug mode state.
//! \~russian Возвращает состояние режима отладки.
bool debug() const { return debug_; }
//! \~english Sets debug mode state.
//! \~russian Устанавливает состояние режима отладки.
void setDebug(bool debug) { debug_ = debug; }
//! \~english Returns class name.
//! \~russian Возвращает имя класса.
PIConstChars className() const { return "PICLI"; }
//! \~english Returns object name.
//! \~russian Возвращает имя объекта.
PIString name() const { return PIStringAscii("CLI"); }
private:

View File

@@ -38,7 +38,12 @@ class PIP_EXPORT PILog: public PIThread {
PIOBJECT_SUBCLASS(PILog, PIThread)
public:
//! \~english Constructs empty %PILog.
//! \~russian Создает пустой %PILog.
PILog();
//! \~english Destructor. Calls stop().
//! \~russian Деструктор. Вызывает stop().
~PILog();
//! \~english Message category

View File

@@ -42,6 +42,8 @@ public:
//! \~russian Создает %PISingleApplication с именем "app_name"
PISingleApplication(const PIString & app_name = PIString());
//! \~english Destructor.
//! \~russian Деструктор.
~PISingleApplication();

View File

@@ -227,23 +227,25 @@ public:
//! \~russian Возвращает статистику потоков наблюдаемого процесса
PIVector<ThreadStats> threadsStatistic() const;
//! \~english Sets process statistics.
//! \~russian Устанавливает статистику процесса.
void setStatistic(const ProcessStats & s);
//! \~english
//! \~russian
//! \~english Returns total RAM in bytes.
//! \~russian Возвращает общий объем ОЗУ в байтах.
static ullong totalRAM();
//! \~english
//! \~russian
//! \~english Returns free RAM in bytes.
//! \~russian Возвращает свободный объем ОЗУ в байтах.
static ullong freeRAM();
//! \~english
//! \~russian
//! \~english Returns used RAM in bytes.
//! \~russian Возвращает используемый объем ОЗУ в байтах.
static ullong usedRAM();
//! \~english
//! \~russian
//! \~english Event raised when new measurements are available.
//! \~russian Событие вызывается когда доступны новые измерения.
EVENT(measured);
private:

View File

@@ -38,15 +38,40 @@
//! \~russian Поддержка перевода
class PIP_EXPORT PITranslator {
public:
//! \~english Translates string "in" with optional context.
//! \~russian Переводит строку "in" с опциональным контекстом.
static PIString tr(const PIString & in, const PIString & context = {});
//! \~english Translates string "in" with optional context.
//! \~russian Переводит строку "in" с опциональным контекстом.
static PIString tr(const char * in, const PIString & context = {}) { return tr(PIString::fromUTF8(in), context); }
//! \~english Returns original string without translation.
//! \~russian Возвращает оригинальную строку без перевода.
static PIString trNoOp(const PIString & in, const PIString & context = {}) { return in; }
//! \~english Returns original string without translation.
//! \~russian Возвращает оригинальную строку без перевода.
static PIString trNoOp(const char * in, const PIString & context = {}) { return trNoOp(PIString::fromUTF8(in), context); }
//! \~english Clears all loaded translations.
//! \~russian Очищает все загруженные переводы.
static void clear();
//! \~english Loads translation for language "short_lang" from directory "dir".
//! \~russian Загружает перевод для языка "short_lang" из директории "dir".
static void loadLang(const PIString & short_lang, PIString dir = {});
//! \~english Loads translation from config "content".
//! \~russian Загружает перевод из конфигурации "content".
static void loadConfig(const PIString & content);
//! \~english Loads translation from binary content.
//! \~russian Загружает перевод из бинарного содержимого.
static bool load(const PIByteArray & content);
//! \~english Loads translation from file.
//! \~russian Загружает перевод из файла.
static bool loadFile(const PIString & path);
private:
@@ -59,10 +84,20 @@ private:
};
//! \~english Context-aware string wrapper for translation.
//! \~russian Контекстно-зависимая обертка строки для перевода.
class PIStringContextTr {
public:
//! \~english Constructs wrapper from string.
//! \~russian Создает обертку из строки.
PIStringContextTr(PIString && s): _s(s) {}
//! \~english Returns translated string.
//! \~russian Возвращает переведенную строку.
operator PIString() const { return PITranslator::tr(_s); }
//! \~english Returns translated string with context.
//! \~russian Возвращает переведенную строку с контекстом.
PIString operator()(const PIString & ctx = {}) const { return PITranslator::tr(_s, ctx); }
private:
@@ -70,10 +105,20 @@ private:
};
//! \~english Context-aware string wrapper without translation (no-op).
//! \~russian Контекстно-зависимая обертка строки без перевода (заглушка).
class PIStringContextTrNoOp {
public:
//! \~english Constructs wrapper from string.
//! \~russian Создает обертку из строки.
PIStringContextTrNoOp(PIString && s): _s(s) {}
//! \~english Returns original string.
//! \~russian Возвращает оригинальную строку.
operator PIString() const { return _s; }
//! \~english Returns original string with context.
//! \~russian Возвращает оригинальную строку с контекстом.
PIString operator()(const PIString & ctx = {}) const { return _s; }
private:

View File

@@ -1,9 +1,11 @@
/*! \file piclientserver_client.h
* \ingroup ClientServer
* \~\brief
* \~english
* \~russian
*/
//! \file piclientserver_client.h
//! \ingroup ClientServer
//! \brief
//! \~english Client and ServerClient classes
//! \~russian Классы Client и ServerClient
//! \details
//! \~english Provides client implementation for connecting to servers and server-side client representation.
//! \~russian Обеспечивает реализацию клиента для подключения к серверам и представление клиента на стороне сервера.
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
@@ -33,13 +35,18 @@ namespace PIClientServer {
// ServerClient
//! ~english Server-side client implementation
//! ~russian Серверная реализация клиента
//! \details
//! \~english Server-side client representation. Created and managed by Server. Used to communicate with remote clients connected to the
//! server.
//! \~russian Представление клиента на стороне сервера. Создаётся и управляется сервером. Используется для коммуникации с удалёнными
//! клиентами, подключёнными к серверу.
class PIP_CLIENT_SERVER_EXPORT ServerClient: public ClientBase {
friend class Server;
NO_COPY_CLASS(ServerClient);
public:
//! \~english Creates empty ServerClient instance
//! \~russian Создает пустой экземпляр ServerClient
ServerClient() {}
protected:
@@ -54,13 +61,20 @@ private:
// Client
//! ~english Client implementation for connecting to servers
//! ~russian Клиентская реализация для подключения к серверам
//! \details
//! \~english Client implementation for connecting to servers. Provides TCP connection to remote server with diagnostics and packet
//! streaming support.
//! \~russian Реализация клиента для подключения к серверам. Обеспечивает TCP-соединение с удалённым сервером с поддержкой диагностики и
//! потоковой передачи пакетов.
class PIP_CLIENT_SERVER_EXPORT Client: public ClientBase {
NO_COPY_CLASS(Client);
public:
//! \~english Creates Client instance
//! \~russian Создает экземпляр Client
Client();
//! \~english Destroys Client instance
//! \~russian Уничтожает экземпляр Client
~Client();
//! ~english Connects to specified server address

View File

@@ -1,9 +1,11 @@
/*! \file piclientserver_client_base.h
* \ingroup ClientServer
* \~\brief
* \~english
* \~russian
*/
//! \file piclientserver_client_base.h
//! \ingroup ClientServer
//! \brief
//! \~english Base class for client-server communication
//! \~russian Базовый класс для клиент-серверного взаимодействия
//! \details
//! \~english Provides base functionality for client-server communication with diagnostics support.
//! \~russian Обеспечивает базовую функциональность для клиент-серверного взаимодействия с поддержкой диагностики.
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
@@ -37,77 +39,117 @@ class Server;
class ClientInterface {};
//! ~english Base class for client-server communication with diagnostics support
//! ~russian Базовый класс для клиент-серверного взаимодействия с поддержкой диагностики
//! \brief
//! \~english Base class for client and server-side client
//! \~russian Базовый класс для клиента и клиента на стороне сервера
//! \details
//! \~english Base class for client and server-side client communication. Provides TCP connection management, diagnostics, and packet
//! streaming capabilities.
//! \~russian Базовый класс для клиентской и серверной коммуникации. Обеспечивает управление TCP-соединением, диагностику и потоковую
//! передачу пакетов.
// template<bool EnableDiagnostics = false>
class PIP_CLIENT_SERVER_EXPORT ClientBase {
friend class Server;
NO_COPY_CLASS(ClientBase);
public:
//! \brief
//! \~english Constructs ClientBase
//! \~russian Создает ClientBase
ClientBase();
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~ClientBase();
//! ~english Gets underlying TCP connection
//! ~russian Возвращает TCP-соединение
//! \brief
//! \~english Gets underlying TCP connection
//! \~russian Возвращает TCP-соединение
const PIEthernet * getTCP() const { return tcp; }
//! ~english Closes the connection
//! ~russian Закрывает соединение
//! \brief
//! \~english Closes the connection
//! \~russian Закрывает соединение
void close();
//! ~english Gracefully stops and waits for completion
//! ~russian Плавно останавливает и ожидает завершения
//! \brief
//! \~english Gracefully stops and waits for completion
//! \~russian Плавно останавливает и ожидает завершения
void stopAndWait();
//! ~english Writes byte array to the connection
//! ~russian Записывает массив байтов в соединение
//! \brief
//! \~english Writes byte array to the connection
//! \~russian Записывает массив байтов в соединение
int write(const void * d, const size_t s);
//! ~english Writes byte array to the connection
//! ~russian Записывает массив байтов в соединение
//! \brief
//! \~english Writes byte array to the connection
//! \~russian Записывает массив байтов в соединение
int write(const PIByteArray & ba) { return write(ba.data(), ba.size()); }
//! ~english Enables diagnostics collection
//! ~russian Включает сбор диагностики
//! \brief
//! \~english Enables diagnostics collection
//! \~russian Включает сбор диагностики
void enableDiagnostics();
//! ~english Gets current diagnostics state
//! ~russian Возвращает текущее состояние диагностики
//! \brief
//! \~english Gets current diagnostics state
//! \~russian Возвращает текущее состояние диагностики
PIDiagnostics::State diagnostics() const;
//! ~english Gets current received packet bytes already received (all bytes count passed in \a receivePacketStart())
//! ~russian Возвращает сколько байт принимаемого пакета получено (общее количество передается в \a receivePacketStart())
//! \brief
//! \~english Gets current received packet bytes already received (all bytes count passed in \a receivePacketStart())
//! \~russian Возвращает сколько байт принимаемого пакета получено (общее количество передается в \a receivePacketStart())
int receivePacketProgress() const;
//! \brief
//! \~english Returns stream configuration
//! \~russian Возвращает конфигурацию стрима
const PIStreamPackerConfig & configuration() const { return stream.configuration(); }
//! \brief
//! \~english Returns stream configuration
//! \~russian Возвращает конфигурацию стрима
PIStreamPackerConfig & configuration() { return stream.configuration(); }
//! \brief
//! \~english Sets stream configuration
//! \~russian Устанавливает конфигурацию стрима
void setConfiguration(const PIStreamPackerConfig & config) { stream.setConfiguration(config); }
protected:
//! ~english Called when data is received
//! ~russian Вызывается при получении данных
//! \brief
//! \~english Called when data is received
//! \~russian Вызывается при получении данных
virtual void readed(PIByteArray data) {}
//! ~english Called when connection is established
//! ~russian Вызывается при установке соединения
//! \brief
//! \~english Called when connection is established
//! \~russian Вызывается при установке соединения
virtual void connected() {}
//! ~english Called when connection is closed
//! ~russian Вызывается при закрытии соединения
//! \brief
//! \~english Called when connection is closed
//! \~russian Вызывается при закрытии соединения
virtual void disconnected() {}
//! ~english Called when packet receiving starts
//! ~russian Вызывается при начале получения пакета
//! \brief
//! \~english Called when packet receiving starts
//! \~russian Вызывается при начале получения пакета
virtual void receivePacketStart(int size) {}
//! ~english Called when packet receiving ends
//! ~russian Вызывается при завершении получения пакета
//! \brief
//! \~english Called when packet receiving ends
//! \~russian Вызывается при завершении получения пакета
virtual void receivePacketEnd() {}
//! \brief
//! \~english Initializes the client
//! \~russian Инициализирует клиента
void init();
bool own_tcp = false;

View File

@@ -1,9 +1,11 @@
/*! \file piclientserver_server.h
* \ingroup ClientServer
* \~\brief
* \~english
* \~russian
*/
//! \file piclientserver_server.h
//! \ingroup ClientServer
//! \brief
//! \~english TCP Server
//! \~russian TCP Сервер
//! \details
//! \~english TCP server implementation for client-server communication.
//! \~russian Реализация TCP сервера для клиент-серверного взаимодействия.
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
@@ -38,14 +40,21 @@ namespace PIClientServer {
class ServerClient;
//! ~english TCP server for client-server communication
//! ~russian TCP сервер для клиент-серверного взаимодействия
//! \details
//! \~english TCP server implementation for client-server communication. Accepts incoming connections and manages multiple clients with
//! configurable factory for client instance creation.
//! \~russian Реализация TCP сервера для клиент-серверного взаимодействия. Принимает входящие соединения и управляет несколькими клиентами с
//! настраиваемой фабрикой для создания экземпляров клиентов.
class PIP_CLIENT_SERVER_EXPORT Server: public PIStreamPackerConfig {
friend class ServerClient;
NO_COPY_CLASS(Server);
public:
//! \~english Creates Server instance
//! \~russian Создает экземпляр Server
Server();
//! \~english Destroys Server instance
//! \~russian Уничтожает экземпляр Server
virtual ~Server();
//! ~english Starts listening on specified address

View File

@@ -1,9 +1,11 @@
/*! \file picloudbase.h
* \ingroup Cloud
* \~\brief
* \~english Base class for PICloudClient and PICloudServer
* \~russian Базовый класс для PICloudClient и PICloudServer
*/
//! \file picloudbase.h
//! \ingroup Cloud
//! \brief
//! \~english Base class for PICloudClient and PICloudServer
//! \~russian Базовый класс для PICloudClient и PICloudServer
//! \details
//! \~english Provides common functionality for cloud client and server implementations.
//! \~russian Обеспечивает общую функциональность для реализаций облачного клиента и сервера.
/*
PIP - Platform Independent Primitives
PICloud Base - Base class for PICloudClient and PICloud Server
@@ -31,10 +33,20 @@
#include "pistreampacker.h"
//! \brief
//! \~english Base class for cloud client and server
//! \~russian Базовый класс для облачного клиента и сервера
class PIP_CLOUD_EXPORT PICloudBase {
public:
//! \brief
//! \~english Constructs PICloudBase
//! \~russian Создает PICloudBase
PICloudBase();
//! \brief
//! \~english Returns server name
//! \~russian Возвращает имя сервера
PIString serverName() const;
protected:

View File

@@ -1,9 +1,11 @@
/*! \file picloudclient.h
* \ingroup Cloud
* \~\brief
* \~english PICloud Client
* \~russian Клиент PICloud
*/
//! \file picloudclient.h
//! \ingroup Cloud
//! \brief
//! \~english PICloud Client
//! \~russian Клиент PICloud
//! \details
//! \~english Client implementation for connecting to PICloud servers over TCP.
//! \~russian Реализация клиента для подключения к серверам PICloud по TCP.
/*
PIP - Platform Independent Primitives
PICloud Client
@@ -30,7 +32,9 @@
#include "piconditionvar.h"
//! \brief PICloudClient
//! \brief PICloud client implementation
//! \~english PICloud client for connecting to servers
//! \~russian Клиент PICloud для подключения к серверам
class PIP_CLOUD_EXPORT PICloudClient
: public PIIODevice
@@ -38,23 +42,75 @@ class PIP_CLOUD_EXPORT PICloudClient
PIIODEVICE(PICloudClient, "");
public:
//! \brief
//! \~english Constructs PICloudClient
//! \~russian Создает PICloudClient
explicit PICloudClient(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~PICloudClient();
//! \brief
//! \~english Sets server name
//! \~russian Устанавливает имя сервера
void setServerName(const PIString & server_name);
//! \brief
//! \~english Sets keep connection mode
//! \~russian Устанавливает режим поддержания соединения
void setKeepConnection(bool on);
//! \brief
//! \~english Checks if connected to server
//! \~russian Проверяет подключение к серверу
bool isConnected() const { return is_connected; }
//! \brief
//! \~english Returns number of bytes available
//! \~russian Возвращает количество доступных байт
ssize_t bytesAvailable() const override { return buff.size(); }
//! \brief
//! \~english Interrupts connection
//! \~russian Прерывает соединение
void interrupt() override;
//! \brief
//! \~english Raised when connected to server
//! \~russian Вызывается при подключении к серверу
EVENT(connected);
//! \brief
//! \~english Raised when disconnected from server
//! \~russian Вызывается при отключении от сервера
EVENT(disconnected);
protected:
//! \brief
//! \~english Opens device
//! \~russian Открывает устройство
bool openDevice() override;
//! \brief
//! \~english Closes device
//! \~russian Закрывает устройство
bool closeDevice() override;
//! \brief
//! \~english Reads data from device
//! \~russian Читает данные из устройства
ssize_t readDevice(void * read_to, ssize_t max_size) override;
//! \brief
//! \~english Writes data to device
//! \~russian Записывает данные в устройство
ssize_t writeDevice(const void * data, ssize_t size) override;
//! \brief
//! \~english Returns device info flags
//! \~russian Возвращает флаги информации об устройстве
DeviceInfoFlags deviceInfoFlags() const override { return PIIODevice::Reliable; }
private:

View File

@@ -1,9 +1,11 @@
/*! \file picloudserver.h
* \ingroup Cloud
* \~\brief
* \~english PICloud Server
* \~russian Сервер PICloud
*/
//! \file picloudserver.h
//! \ingroup Cloud
//! \brief
//! \~english PICloud Server
//! \~russian Сервер PICloud
//! \details
//! \~english Server implementation for accepting PICloud client connections over TCP.
//! \~russian Реализация сервера для приема подключений клиентов PICloud по TCP.
/*
PIP - Platform Independent Primitives
PICloud Server
@@ -30,31 +32,79 @@
#include "piconditionvar.h"
//! \brief PICloud server implementation
//! \~english PICloud server for accepting client connections
//! \~russian Сервер PICloud для приема подключений клиентов
class PIP_CLOUD_EXPORT PICloudServer
: public PIIODevice
, public PICloudBase {
PIIODEVICE(PICloudServer, "");
public:
//! PICloudServer
//! \brief
//! \~english Constructs PICloudServer
//! \~russian Создает PICloudServer
explicit PICloudServer(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~PICloudServer();
//! \brief Connected client representation
//! \~english Represents a client connected to the server
//! \~russian Представляет клиента, подключенного к серверу
class Client: public PIIODevice {
PIIODEVICE(PICloudServer::Client, "");
friend class PICloudServer;
public:
//! \brief
//! \~english Constructs Client
//! \~russian Создает Client
//! \param srv Parent server / Родительский сервер
//! \param id Client ID / ID клиента
Client(PICloudServer * srv = nullptr, uint id = 0);
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~Client();
protected:
//! \brief
//! \~english Opens device
//! \~russian Открывает устройство
bool openDevice() override;
//! \brief
//! \~english Closes device
//! \~russian Закрывает устройство
bool closeDevice() override;
//! \brief
//! \~english Reads data from device
//! \~russian Читает данные из устройства
ssize_t readDevice(void * read_to, ssize_t max_size) override;
//! \brief
//! \~english Writes data to device
//! \~russian Записывает данные в устройство
ssize_t writeDevice(const void * data, ssize_t size) override;
//! \brief
//! \~english Returns device info flags
//! \~russian Возвращает флаги информации об устройстве
DeviceInfoFlags deviceInfoFlags() const override { return PIIODevice::Reliable; }
//! \brief
//! \~english Returns number of bytes available
//! \~russian Возвращает количество доступных байт
ssize_t bytesAvailable() const override { return buff.size(); }
//! \brief
//! \~english Interrupts connection
//! \~russian Прерывает соединение
void interrupt() override;
private:
@@ -67,17 +117,45 @@ public:
std::atomic_bool is_connected;
};
//! \brief
//! \~english Sets server name
//! \~russian Устанавливает имя сервера
void setServerName(const PIString & server_name);
//! \brief
//! \~english Returns list of connected clients
//! \~russian Возвращает список подключенных клиентов
PIVector<PICloudServer::Client *> clients() const;
//! \brief
//! \~english Raised when new client connects
//! \~russian Вызывается при подключении нового клиента
EVENT1(newConnection, PICloudServer::Client *, client);
protected:
//! \brief
//! \~english Opens device
//! \~russian Открывает устройство
bool openDevice() override;
//! \brief
//! \~english Closes device
//! \~russian Закрывает устройство
bool closeDevice() override;
//! \brief
//! \~english Reads data from device
//! \~russian Читает данные из устройства
ssize_t readDevice(void * read_to, ssize_t max_size) override;
//! \brief
//! \~english Writes data to device
//! \~russian Записывает данные в устройство
ssize_t writeDevice(const void * data, ssize_t max_size) override;
//! \brief
//! \~english Interrupts connection
//! \~russian Прерывает соединение
void interrupt() override;
private:

View File

@@ -1,9 +1,11 @@
/*! \file picloudtcp.h
* \ingroup Cloud
* \~\brief
* \~english PICloud TCP transport
* \~russian TCP слой PICloud
*/
//! \file picloudtcp.h
//! \ingroup Cloud
//! \brief
//! \~english PICloud TCP transport
//! \~russian TCP слой PICloud
//! \details
//! \~english Low-level TCP protocol implementation for PICloud communication.
//! \~russian Реализация низкоуровневого TCP-протокола для коммуникации PICloud.
/*
PIP - Platform Independent Primitives
PICloud TCP transport
@@ -36,45 +38,118 @@ class PIStreamPacker;
namespace PICloud {
//! \brief TCP transport protocol
//! \~english TCP transport protocol for cloud communication
//! \~russian TCP транспортный протокол для облачной коммуникации
class PIP_CLOUD_EXPORT TCP {
public:
//! \brief Protocol version
enum Version {
Version_1 = 1,
Version_2 = 2,
Version_1 = 1, //!< Version 1 / Версия 1
Version_2 = 2, //!< Version 2 / Версия 2
};
//! \brief Connection role
enum Role {
InvalidRole = 0,
Server = 1,
Client = 2,
InvalidRole = 0, //!< Invalid role / Неверная роль
Server = 1, //!< Server role / Роль сервера
Client = 2, //!< Client role / Роль клиента
};
//! \brief Message type
enum Type {
InvalidType = 0,
Connect = 1,
Disconnect = 2,
Data = 3,
Ping = 4,
InvalidType = 0, //!< Invalid type / Неверный тип
Connect = 1, //!< Connect message / Сообщение о подключении
Disconnect = 2, //!< Disconnect message / Сообщение об отключении
Data = 3, //!< Data message / Сообщение с данными
Ping = 4, //!< Ping message / Сообщение ping
};
//! \brief
//! \~english Constructs TCP transport
//! \~russian Создает TCP транспорт
//! \param s Stream packer instance / Экземпляр стримового упаковщика
TCP(PIStreamPacker * s);
//! \brief
//! \~english Sets connection role
//! \~russian Устанавливает роль соединения
void setRole(Role r);
//! \brief
//! \~english Returns connection role
//! \~russian Возвращает роль соединения
Role role() const { return (Role)header.role; }
//! \brief
//! \~english Sets server name
//! \~russian Устанавливает имя сервера
void setServerName(const PIString & server_name_);
//! \brief
//! \~english Returns server name
//! \~russian Возвращает имя сервера
PIString serverName() const;
//! \brief
//! \~english Sends start message
//! \~russian Отправляет сообщение о старте
void sendStart();
//! \brief
//! \~english Sends connected message
//! \~russian Отправляет сообщение о подключении
void sendConnected(uint client_id);
//! \brief
//! \~english Sends disconnected message
//! \~russian Отправляет сообщение об отключении
void sendDisconnected(uint client_id);
//! \brief
//! \~english Sends data to all clients
//! \~russian Отправляет данные всем клиентам
int sendData(const PIByteArray & data);
//! \brief
//! \~english Sends data to specific client
//! \~russian Отправляет данные конкретному клиенту
int sendData(const PIByteArray & data, uint client_id);
//! \brief
//! \~english Sends ping message
//! \~russian Отправляет сообщение ping
void sendPing();
//! \brief
//! \~english Parses header from buffer
//! \~russian Парсит заголовок из буфера
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
//! \brief
//! \~english Checks if data can be parsed
//! \~russian Проверяет возможность парсинга данных
bool canParseData(PIByteArray & ba);
//! \brief
//! \~english Parses data for server
//! \~russian Парсит данные для сервера
PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba);
//! \brief
//! \~english Parses connect data
//! \~russian Парсит данные подключения
PIByteArray parseConnect_d(PIByteArray & ba);
//! \brief
//! \~english Parses connect message
//! \~russian Парсит сообщение подключения
uint parseConnect(PIByteArray & ba);
//! \brief
//! \~english Parses disconnect message
//! \~russian Парсит сообщение отключения
uint parseDisconnect(PIByteArray & ba);
private:

View File

@@ -64,6 +64,8 @@ typedef int (*AccessOffsetFunction)(const char *);
//! \~english Type information
//! \~russian Информация о типе
struct PIP_EXPORT TypeInfo {
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
TypeInfo(const PIConstChars & n = PIConstChars(), const PIConstChars & t = PIConstChars(), PICodeInfo::TypeFlags f = 0, int b = -1) {
name = n;
type = t;
@@ -121,6 +123,8 @@ struct PIP_EXPORT FunctionInfo {
//! \~english Class or struct information
//! \~russian Информация о классе или структуре
struct PIP_EXPORT ClassInfo {
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
ClassInfo() { is_anonymous = false; }
//! \~english Custom PIMETA content
@@ -160,10 +164,14 @@ struct PIP_EXPORT ClassInfo {
//! \~english Enumerator information
//! \~russian Информация об элементе перечисления
struct PIP_EXPORT EnumeratorInfo {
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
EnumeratorInfo(const PIConstChars & n = PIConstChars(), int v = 0) {
name = n;
value = v;
}
//! \~english Converts to PIVariantTypes::Enumerator
//! \~russian Конвертирует в PIVariantTypes::Enumerator
PIVariantTypes::Enumerator toPIVariantEnumerator() { return PIVariantTypes::Enumerator(value, name.toString()); }
//! \~english Custom PIMETA content
@@ -209,6 +217,8 @@ struct PIP_EXPORT EnumInfo {
};
//! \~english Output stream operator for TypeInfo
//! \~russian Оператор вывода для TypeInfo
inline PICout operator<<(PICout s, const PICodeInfo::TypeInfo & v) {
if (v.flags[Inline]) s << "inline ";
if (v.flags[Virtual]) s << "virtual ";
@@ -221,11 +231,15 @@ inline PICout operator<<(PICout s, const PICodeInfo::TypeInfo & v) {
return s;
}
//! \~english Output stream operator for EnumeratorInfo
//! \~russian Оператор вывода для EnumeratorInfo
inline PICout operator<<(PICout s, const PICodeInfo::EnumeratorInfo & v) {
s << v.name << " = " << v.value << " Meta" << v.meta;
return s;
}
//! \~english Output stream operator for ClassInfo
//! \~russian Оператор вывода для ClassInfo
inline PICout operator<<(PICout s, const PICodeInfo::ClassInfo & v) {
s.saveAndSetControls(0);
s << "class " << v.name;
@@ -262,6 +276,8 @@ inline PICout operator<<(PICout s, const PICodeInfo::ClassInfo & v) {
return s;
}
//! \~english Output stream operator for EnumInfo
//! \~russian Оператор вывода для EnumInfo
inline PICout operator<<(PICout s, const PICodeInfo::EnumInfo & v) {
s.saveAndSetControls(0);
s << "enum " << v.name << " Meta" << v.meta << " {\n";
@@ -279,11 +295,19 @@ inline PICout operator<<(PICout s, const PICodeInfo::EnumInfo & v) {
}
//! \~english Storage singleton for PICodeInfo
//! \~russian Синглтон хранилища для PICodeInfo
class PIP_EXPORT __Storage__ {
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
__Storage__();
//! \~english Destructor
//! \~russian Деструктор
~__Storage__();
public:
//! \~english Returns singleton instance
//! \~russian Возвращает экземпляр синглтона
static __Storage__ * instance();
PIMap<PIConstChars, PICodeInfo::ClassInfo *> * classesInfo;
@@ -296,71 +320,83 @@ private:
NO_COPY_CLASS(__Storage__)
};
class PIP_EXPORT
__StorageAccess__{public:
//! \~english Access to singleton storage of PICodeInfo
//! \~russian Доступ к синглтону хранилища PICodeInfo
class PIP_EXPORT __StorageAccess__ {
public:
//! \~english Getter for single storage of PICodeInfo::ClassInfo, access by name
//! \~russian Доступ к единому хранилищу PICodeInfo::ClassInfo, доступ по имени
static const PIMap<PIConstChars, PICodeInfo::ClassInfo *> & classes(){return *(__Storage__::instance()->classesInfo);
static const PIMap<PIConstChars, PICodeInfo::ClassInfo *> & classes() {
return *(__Storage__::instance()->classesInfo);
} // namespace PICodeInfo
//! \~english Getter for single storage of PICodeInfo::EnumInfo, access by name
//! \~russian Доступ к единому хранилищу хранилище PICodeInfo::EnumInfo, доступ по имени
static const PIMap<PIConstChars, PICodeInfo::EnumInfo *> & enums() {
return *(__Storage__::instance()->enumsInfo);
}
static const PIMap<PIConstChars, PICodeInfo::EnumInfo *> & enums() { return *(__Storage__::instance()->enumsInfo); }
//! \~english Access to value functions map
//! \~russian Доступ к карте функций значений
static const PIMap<PIConstChars, PICodeInfo::AccessValueFunction> & accessValueFunctions() {
return *(__Storage__::instance()->accessValueFunctions);
}
//! \~english Access to type functions map
//! \~russian Доступ к карте функций типов
static const PIMap<PIConstChars, PICodeInfo::AccessTypeFunction> & accessTypeFunctions() {
return *(__Storage__::instance()->accessTypeFunctions);
}
//! \~english Access to offset functions map
//! \~russian Доступ к карте функций смещений
static const PIMap<PIConstChars, PICodeInfo::AccessOffsetFunction> & accessOffsetFunctions() {
return *(__Storage__::instance()->accessOffsetFunctions);
}
}
;
};
#define PICODEINFO PICodeInfo::__StorageAccess__
class PIP_EXPORT
ClassInfoInterface{public: const PIMap<PIConstChars, PICodeInfo::ClassInfo *> * operator->() const DEPRECATEDM("use PICODEINFO::classes()"){
//! \~english Deprecated interface for accessing classes info
//! \~russian Устаревший интерфейс для доступа к информации о классах
class PIP_EXPORT ClassInfoInterface {
public:
const PIMap<PIConstChars, PICodeInfo::ClassInfo *> * operator->() const DEPRECATEDM("use PICODEINFO::classes()") {
return __Storage__::instance()->classesInfo;
}
}
;
};
static ClassInfoInterface classesInfo;
class PIP_EXPORT
EnumsInfoInterface{public: const PIMap<PIConstChars, PICodeInfo::EnumInfo *> * operator->() const DEPRECATEDM("use PICODEINFO::enums()"){
//! \~english Deprecated interface for accessing enums info
//! \~russian Устаревший интерфейс для доступа к информации о перечислениях
class PIP_EXPORT EnumsInfoInterface {
public:
const PIMap<PIConstChars, PICodeInfo::EnumInfo *> * operator->() const DEPRECATEDM("use PICODEINFO::enums()") {
return __Storage__::instance()->enumsInfo;
}
}
;
};
static EnumsInfoInterface enumsInfo;
//! \~english Deprecated interface for accessing value functions
//! \~russian Устаревший интерфейс для доступа к функциям значений
class PIP_EXPORT AccessValueFunctionInterface {
public: const PIMap<PIConstChars, PICodeInfo::AccessValueFunction> * operator->()
const DEPRECATEDM("use PICODEINFO::accessValueFunctions()"){
public:
const PIMap<PIConstChars, PICodeInfo::AccessValueFunction> * operator->() const DEPRECATEDM("use PICODEINFO::accessValueFunctions()") {
return __Storage__::instance()->accessValueFunctions;
}
}
;
};
static AccessValueFunctionInterface accessValueFunctions;
//! \~english Deprecated interface for accessing type functions
//! \~russian Устаревший интерфейс для доступа к функциям типов
class PIP_EXPORT AccessTypeFunctionInterface {
public: const PIMap<PIConstChars, PICodeInfo::AccessTypeFunction> * operator->()
const DEPRECATEDM("use PICODEINFO::accessTypeFunctions()"){
public:
const PIMap<PIConstChars, PICodeInfo::AccessTypeFunction> * operator->() const DEPRECATEDM("use PICODEINFO::accessTypeFunctions()") {
return __Storage__::instance()->accessTypeFunctions;
}
}
;
};
static AccessTypeFunctionInterface accessTypeFunctions;
@@ -372,6 +408,8 @@ STATIC_INITIALIZER_BEGIN
STATIC_INITIALIZER_END
//! \~english Gets member value as PIByteArray
//! \~russian Получает значение члена как PIByteArray
inline PIByteArray getMemberValue(const void * p, const char * class_name, const char * member_name) {
if (!p || !class_name || !member_name) return PIByteArray();
AccessValueFunction af = PICODEINFO::accessValueFunctions().value(class_name, (AccessValueFunction)0);
@@ -379,6 +417,8 @@ inline PIByteArray getMemberValue(const void * p, const char * class_name, const
return af(p, member_name);
}
//! \~english Gets member type as string
//! \~russian Получает тип члена как строку
inline const char * getMemberType(const char * class_name, const char * member_name) {
if (!class_name || !member_name) return "";
AccessTypeFunction af = PICODEINFO::accessTypeFunctions().value(class_name, (AccessTypeFunction)0);
@@ -386,14 +426,20 @@ inline const char * getMemberType(const char * class_name, const char * member_n
return af(member_name);
}
//! \~english Gets member value as PIVariant
//! \~russian Получает значение члена как PIVariant
PIP_EXPORT PIVariant getMemberAsVariant(const void * p, const char * class_name, const char * member_name);
//! \~english Serializes value to PIByteArray (for assignable types)
//! \~russian Сериализует значение в PIByteArray (для назначаемых типов)
template<typename T, typename std::enable_if<std::is_assignable<T &, const T &>::value, int>::type = 0>
void serialize(PIByteArray & ret, const T & v) {
ret << v;
}
//! \~english Empty serialization (for non-assignable types)
//! \~russian Пустая сериализация (для неназначаемых типов)
template<typename T, typename std::enable_if<!std::is_assignable<T &, const T &>::value, int>::type = 0>
void serialize(PIByteArray & ret, const T & v) {}

View File

@@ -1,8 +1,13 @@
/*! \file picodeparser.h
* \ingroup Code
* \~\brief
* \addtogroup Code
* \{
* \~
* \brief
* \~english C++ code parser
* \~russian Разбор C++ кода
* \details
* \~english Parser for analyzing C++ source files. Extracts information about classes, structures, enums, macros, functions, and members.
* \~russian Парсер для анализа C++ исходных файлов. Извлекает информацию о классах, структурах, перечислениях, макросах, функциях и членах.
*/
/*
PIP - Platform Independent Primitives
@@ -39,6 +44,8 @@ inline bool _isCChar(const PIString & c) {
class PIP_EXPORT PICodeParser {
public:
//! \~english Default constructor.
//! \~russian Конструктор по умолчанию.
PICodeParser();
enum Visibility {
@@ -63,18 +70,27 @@ public:
typedef PIPair<PIString, PIString> Typedef;
typedef PIMap<PIString, PIString> MetaMap;
//! \~english Represents a preprocessor macro.
//! \~russian Представляет препроцессорный макрос.
struct PIP_EXPORT Macro {
Macro(const PIString & n = PIString(), const PIString & v = PIString(), const PIStringList & a = PIStringList()) {
name = n;
value = v;
args = a;
}
//! \~english Expand macro with given arguments.
//! \~russian Раскрыть макрос с заданными аргументами.
//! \param args_ Arguments string
//! \param ok Output flag indicating success
//! \return Expanded macro value
PIString expand(PIString args_, bool * ok = 0) const;
PIString name;
PIString value;
PIStringList args;
};
//! \~english Represents a class/struct member variable or function.
//! \~russian Представляет переменную-член или функцию класса/структуры.
struct PIP_EXPORT Member {
Member() {
visibility = Global;
@@ -83,6 +99,8 @@ public:
is_type_ptr = false;
attributes = NoAttributes;
}
//! \~english Check if member is a bitfield.
//! \~russian Проверить, является ли член битовым полем.
bool isBitfield() const { return bits > 0; }
MetaMap meta;
PIString type;
@@ -97,6 +115,8 @@ public:
int bits;
};
//! \~english Represents a parsed entity (class, struct, namespace, etc.).
//! \~russian Представляет разобранную сущность (класс, структуру, пространство имен и т.д.).
struct PIP_EXPORT Entity {
Entity() {
visibility = Global;
@@ -118,6 +138,8 @@ public:
PIVector<Typedef> typedefs;
};
//! \~english Represents an enumerator value.
//! \~russian Представляет значение перечисления.
struct PIP_EXPORT EnumeratorInfo {
EnumeratorInfo(const PIString & n = PIString(), int v = 0, const MetaMap & m = MetaMap()) {
name = n;
@@ -129,6 +151,8 @@ public:
int value;
};
//! \~english Represents a parsed enum type.
//! \~russian Представляет разобранный тип перечисления.
struct PIP_EXPORT Enum {
Enum(const PIString & n = PIString()) { name = n; }
MetaMap meta;
@@ -136,25 +160,89 @@ public:
PIVector<EnumeratorInfo> members;
};
//! \~english Parse C++ source file.
//! \~russian Разбор C++ исходного файла.
//! \param file Path to source file
//! \param follow_includes Whether to follow include directives
void parseFile(const PIString & file, bool follow_includes = true);
//! \~english Parse multiple C++ source files.
//! \~russian Разбор нескольких C++ исходных файлов.
//! \param files List of file paths
//! \param follow_includes Whether to follow include directives
void parseFiles(const PIStringList & files, bool follow_includes = true);
//! \~english Parse C++ source from string content.
//! \~russian Разбор C++ исходного кода из строки.
//! \param fc Source code content
void parseFileContent(PIString fc);
//! \~english Add directory to search for include files.
//! \~russian Добавить директорию для поиска включаемых файлов.
//! \param dir Directory path
void includeDirectory(const PIString & dir) { includes << dir; }
//! \~english Add custom macro definition.
//! \~russian Добавить пользовательское определение макроса.
//! \param def_name Macro name
//! \param def_value Macro value
void addDefine(const PIString & def_name, const PIString & def_value) { custom_defines << Define(def_name, def_value); }
//! \~english Check if name refers to an enum type.
//! \~russian Проверить, является ли имя типом перечисления.
//! \param name Name to check
//! \return true if name is an enum
bool isEnum(const PIString & name);
//! \~english Find entity by name.
//! \~russian Найти сущность по имени.
//! \param en Entity name
//! \return Pointer to entity or nullptr
Entity * findEntityByName(const PIString & en);
//! \~english Get list of parsed files.
//! \~russian Получить список разобранных файлов.
//! \return List of file paths
PIStringList parsedFiles() const { return PIStringList(proc_files.toVector()); }
//! \~english Get main file path.
//! \~russian Получить путь к главному файлу.
//! \return Main file path
PIString mainFile() const { return main_file; }
//! \~english Get global scope entity.
//! \~russian Получить сущность глобальной области видимости.
//! \return Pointer to global entity
const PICodeParser::Entity * global() const { return &root_; }
//! \~english Get maximum iterations for macros substitution.
//! \~russian Получить максимальное количество итераций для подстановки макросов.
//! \return Maximum iterations count
int macrosSubstitutionMaxIterations() const { return macros_iter; }
//! \~english Set maximum iterations for macros substitution.
//! \~russian Установить максимальное количество итераций для подстановки макросов.
//! \param value Maximum iterations count
void setMacrosSubstitutionMaxIterations(int value) { macros_iter = value; }
//! \~english List of defined macros.
//! \~russian Список определенных макросов.
PIVector<Define> defines, custom_defines;
//! \~english List of macro definitions with expansion.
//! \~russian Список определений макросов с подстановкой.
PIVector<Macro> macros;
//! \~english List of enumerated types.
//! \~russian Список типов перечислений.
PIVector<Enum> enums;
//! \~english List of type definitions.
//! \~russian Список определений типов.
PIVector<Typedef> typedefs;
//! \~english List of parsed entities (classes, structs, etc.).
//! \~russian Список разобранных сущностей (классов, структур и т.д.).
PIVector<Entity *> entities;
private:
@@ -195,4 +283,7 @@ private:
PIMap<PIString, MetaMap> tmp_meta;
};
/*! \}
*/
#endif // PICODEPARSER_H

View File

@@ -177,13 +177,29 @@ public:
//! Returns exit key, default 'Q'
int exitKey() const { return exit_key; }
//! \brief
//! \~english Returns double click interval in seconds
//! \~russian Возвращает интервал двойного клика в секундах
double doubleClickInterval() const { return dbl_interval; }
//! \brief
//! \~english Sets double click interval
//! \~russian Устанавливает интервал двойного клика
void setDoubleClickInterval(double v) { dbl_interval = v; }
//! \brief
//! \~english Reads keyboard input
//! \~russian Читает ввод с клавиатуры
void readKeyboard();
//! \brief
//! \~english Stops keyboard listening
//! \~russian Останавливает прослушивание клавиатуры
void stop();
//! \brief
//! \~english Stops and waits for completion
//! \~russian Останавливает и ожидает завершения
bool stopAndWait(PISystemTime timeout = {});
//! Returns if keyboard listening is active (not running!)

View File

@@ -1,9 +1,11 @@
/*! \file piscreen.h
* \ingroup Console
* \~\brief
* \~english Console tiling manager
* \~russian Консольный тайловый менеджер
*/
//! \file piscreen.h
//! \ingroup Console
//! \brief
//! \~english Console tiling manager
//! \~russian Консольный тайловый менеджер
//! \details
//! \~english Main console screen manager providing tile-based UI rendering and keyboard input.
//! \~russian Основной менеджер консольного экрана, обеспечивающий отрисовку UI на основе тайлов и ввод с клавиатуры.
/*
PIP - Platform Independent Primitives
Console GUI
@@ -31,6 +33,9 @@
#include "piscreentile.h"
//! \brief
//! \~english Console tiling manager
//! \~russian Консольный тайловый менеджер
class PIP_CONSOLE_EXPORT PIScreen
: public PIThread
, public PIScreenTypes::PIScreenBase {
@@ -38,46 +43,126 @@ class PIP_CONSOLE_EXPORT PIScreen
class SystemConsole;
public:
//! Constructs %PIScreen with key handler "slot" and if "startNow" start it
//! \brief
//! \~english Constructs PIScreen
//! \~russian Создает PIScreen
//! \param startNow Start immediately / Запустить немедленно
//! \param slot Keyboard handler function / Обработчик клавиатуры
PIScreen(bool startNow = true, PIKbdListener::KBFunc slot = 0);
//! \brief
//! \~english Destructor
//! \~russian Деструктор
~PIScreen();
//! Directly call function from \a PIKbdListener
//! \brief
//! \~english Enables exit capture with key
//! \~russian Включает захват выхода по клавише
void enableExitCapture(int key = 'Q') { listener->enableExitCapture(key); }
//! Directly call function from \a PIKbdListener
//! \brief
//! \~english Disables exit capture
//! \~russian Отключает захват выхода
void disableExitCapture() { listener->disableExitCapture(); }
//! Directly call function from \a PIKbdListener
//! \brief
//! \~english Checks if exit is captured
//! \~russian Проверяет, захвачен ли выход
bool exitCaptured() const { return listener->exitCaptured(); }
//! Directly call function from \a PIKbdListener
//! \brief
//! \~english Returns exit key
//! \~russian Возвращает клавишу выхода
int exitKey() const { return listener->exitKey(); }
//! \brief
//! \~english Returns window width
//! \~russian Возвращает ширину окна
int windowWidth() const { return console.width; }
//! \brief
//! \~english Returns window height
//! \~russian Возвращает высоту окна
int windowHeight() const { return console.height; }
//! \brief
//! \~english Checks if mouse is enabled
//! \~russian Проверяет, включена ли мышь
bool isMouseEnabled() const { return mouse_; }
//! \brief
//! \~english Sets mouse enabled state
//! \~russian Устанавливает состояние мыши
void setMouseEnabled(bool on);
//! \brief
//! \~english Returns root tile
//! \~russian Возвращает корневой тайл
PIScreenTile * rootTile() { return &root; }
//! \brief
//! \~english Finds tile by name
//! \~russian Находит тайл по имени
PIScreenTile * tileByName(const PIString & name);
//! \brief
//! \~english Sets dialog tile
//! \~russian Устанавливает диалоговый тайл
void setDialogTile(PIScreenTile * t);
//! \brief
//! \~english Returns current dialog tile
//! \~russian Возвращает текущий диалоговый тайл
PIScreenTile * dialogTile() const { return tile_dialog; }
//! \brief
//! \~english Returns screen drawer
//! \~russian Возвращает отрисовщик экрана
PIScreenDrawer * drawer() { return &drawer_; }
//! \brief
//! \~english Clears the screen
//! \~russian Очищает экран
void clear() { drawer_.clear(); }
//! \brief
//! \~english Resizes screen
//! \~russian Изменяет размер экрана
void resize(int w, int h) { console.resize(w, h); }
//! \brief
//! \~english Waits for finish
//! \~russian Ожидает завершения
EVENT_HANDLER0(void, waitForFinish);
//! \brief
//! \~english Starts screen
//! \~russian Запускает экран
EVENT_HANDLER0(void, start) { start(false); }
//! \brief
//! \~english Starts screen
//! \~russian Запускает экран
EVENT_HANDLER1(void, start, bool, wait);
//! \brief
//! \~english Stops screen
//! \~russian Останавливает экран
EVENT_HANDLER0(void, stop) { stop(false); }
//! \brief
//! \~english Stops screen
//! \~russian Останавливает экран
EVENT_HANDLER1(void, stop, bool, clear);
//! \brief
//! \~english Raised on key pressed
//! \~russian Вызывается при нажатии клавиши
EVENT2(keyPressed, PIKbdListener::KeyEvent, key, void *, data);
//! \brief
//! \~english Raised on tile event
//! \~russian Вызывается при событии тайла
EVENT2(tileEvent, PIScreenTile *, tile, PIScreenTypes::TileEvent, e);
//! \handlers

View File

@@ -1,9 +1,11 @@
/*! \file piscreenconsole.h
* \ingroup Console
* \~\brief
* \~english Tile for PIScreen with PIConsole API
* \~russian Тайл для PIScreen с API PIConsole
*/
//! \file piscreenconsole.h
//! \ingroup Console
//! \brief
//! \~english Tile for PIScreen with PIConsole API
//! \~russian Тайл для PIScreen с API PIConsole
//! \details
//! \~english Provides tiles for displaying variable data and console-like content.
//! \~russian Обеспечивает тайлы для отображения данных переменных и консольного контента.
/*
PIP - Platform Independent Primitives
Tile for PIScreen with PIConsole API
@@ -32,17 +34,36 @@
/// NOTE: incomplete class
/// TODO: write TileVars
//! \brief
//! \~english Tile for displaying variable data
//! \~russian Тайл для отображения данных переменных
//! \addtogroup Console
//! \{
//! \brief
//! \~english Tile for displaying variable data
//! \~russian Тайл для отображения данных переменных
class PIP_CONSOLE_EXPORT TileVars: public PIScreenTile {
public:
TileVars(const PIString & n = PIString());
//! \brief
//! \~english Constructs TileVars
//! \~russian Создает TileVars
//! \param n Tile name / Имя тайла
explicit TileVars(const PIString & n = PIString());
protected:
//! \brief Variable data structure
struct PIP_CONSOLE_EXPORT Variable {
Variable() {
nx = ny = type = offset = bitFrom = bitCount = size = 0;
format = PIScreenTypes::CellFormat();
ptr = 0;
}
//! \brief
//! \~english Checks if variable is empty
//! \~russian Проверяет, пустая ли переменная
bool isEmpty() const { return (ptr == 0); }
PIString name;
PIScreenTypes::CellFormat format;
@@ -67,16 +88,38 @@ protected:
ptr = src.ptr;
}*/
};
//! \brief
//! \~english Returns variables
//! \~russian Возвращает переменные
PIVector<Variable> variables;
PIScreenTypes::Alignment alignment;
//! \brief
//! \~english Calculates tile size hint
//! \~russian Вычисляет рекомендуемый размер тайла
void sizeHint(int & w, int & h) const override;
//! \brief
//! \~english Draws tile content
//! \~russian Рисует содержимое тайла
void drawEvent(PIScreenDrawer * d) override;
};
//! \brief
//! \~english Console-style tile for PIScreen
//! \~russian Консольный тайл для PIScreen
//! \brief
//! \~english Console-style tile for PIScreen
//! \~russian Консольный тайл для PIScreen
class PIP_CONSOLE_EXPORT PIScreenConsoleTile: public PIScreenTile {
public:
PIScreenConsoleTile();
//! \brief
//! \~english Constructs PIScreenConsoleTile
//! \~russian Создает PIScreenConsoleTile
explicit PIScreenConsoleTile();
};
#endif // PISCREENCONSOLE_H

View File

@@ -1,9 +1,11 @@
/*! \file piscreendrawer.h
* \ingroup Console
* \~\brief
* \~english Drawer for PIScreen
* \~russian Отрисовщик для PIScreen
*/
//! \file piscreendrawer.h
//! \ingroup Console
//! \brief
//! \~english Drawer for PIScreen
//! \~russian Отрисовщик для PIScreen
//! \details
//! \~english Provides drawing primitives for console screen rendering.
//! \~russian Обеспечивает примитивы отрисовки для рендеринга консольного экрана.
/*
PIP - Platform Independent Primitives
Drawer for PIScreen
@@ -30,31 +32,54 @@
#include "piscreentypes.h"
#include "pistring.h"
//! \brief Screen drawer for console rendering
//! \~english Console screen drawer for rendering graphics
//! \~russian Отрисовщик консольного экрана для рендеринга графики
//! \brief
//! \~english Console screen drawer for rendering graphics
//! \~russian Отрисовщик консольного экрана для рендеринга графики
class PIP_CONSOLE_EXPORT PIScreenDrawer {
friend class PIScreen;
PIScreenDrawer(PIVector<PIVector<PIScreenTypes::Cell>> & c);
public:
//! \brief ASCII art characters
enum ArtChar {
LineVertical = 1,
LineHorizontal,
Cross,
CornerTopLeft,
CornerTopRight,
CornerBottomLeft,
CornerBottomRight,
Unchecked,
Checked
LineVertical = 1, //!< Vertical line / Вертикальная линия
LineHorizontal, //!< Horizontal line / Горизонтальная линия
Cross, //!< Cross / Крест
CornerTopLeft, //!< Top-left corner / Угол сверху-слева
CornerTopRight, //!< Top-right corner / Угол сверху-справа
CornerBottomLeft, //!< Bottom-left corner / Угол снизу-слева
CornerBottomRight, //!< Bottom-right corner / Угол снизу-справа
Unchecked, //!< Unchecked box / Неотмеченная клетка
Checked //!< Checked box / Отмеченная клетка
};
//! \brief
//! \~english Clears the screen
//! \~russian Очищает экран
void clear();
//! \brief
//! \~english Clears rectangle
//! \~russian Очищает прямоугольник
void clearRect(int x0, int y0, int x1, int y1) { fillRect(x0, y0, x1, y1, ' '); }
//! \brief
//! \~english Draws pixel
//! \~russian Рисует пиксель
void drawPixel(int x,
int y,
const PIChar & c,
PIScreenTypes::Color col_char = PIScreenTypes::Default,
PIScreenTypes::Color col_back = PIScreenTypes::Default,
PIScreenTypes::CharFlags flags_char = 0);
//! \brief
//! \~english Draws line
//! \~russian Рисует линию
void drawLine(int x0,
int y0,
int x1,
@@ -63,6 +88,10 @@ public:
PIScreenTypes::Color col_char = PIScreenTypes::Default,
PIScreenTypes::Color col_back = PIScreenTypes::Default,
PIScreenTypes::CharFlags flags_char = 0);
//! \brief
//! \~english Draws rectangle
//! \~russian Рисует прямоугольник
void drawRect(int x0,
int y0,
int x1,
@@ -71,6 +100,10 @@ public:
PIScreenTypes::Color col_char = PIScreenTypes::Default,
PIScreenTypes::Color col_back = PIScreenTypes::Default,
PIScreenTypes::CharFlags flags_char = 0);
//! \brief
//! \~english Draws frame
//! \~russian Рисует рамку
void drawFrame(int x0,
int y0,
int x1,
@@ -78,12 +111,20 @@ public:
PIScreenTypes::Color col_char = PIScreenTypes::Default,
PIScreenTypes::Color col_back = PIScreenTypes::Default,
PIScreenTypes::CharFlags flags_char = 0);
//! \brief
//! \~english Draws text
//! \~russian Рисует текст
void drawText(int x,
int y,
const PIString & s,
PIScreenTypes::Color col_char = PIScreenTypes::Default,
PIScreenTypes::Color col_back = PIScreenTypes::Transparent,
PIScreenTypes::CharFlags flags_char = 0);
//! \brief
//! \~english Fills rectangle
//! \~russian Заполняет прямоугольник
void fillRect(int x0,
int y0,
int x1,
@@ -92,10 +133,20 @@ public:
PIScreenTypes::Color col_char = PIScreenTypes::Default,
PIScreenTypes::Color col_back = PIScreenTypes::Default,
PIScreenTypes::CharFlags flags_char = 0);
//! \brief
//! \~english Fills rectangle with content
//! \~russian Заполняет прямоугольник содержимым
void fillRect(int x0, int y0, int x1, int y1, PIVector<PIVector<PIScreenTypes::Cell>> & content);
//! \brief
//! \~english Returns art character by type
//! \~russian Возвращает символ искусства по типу
PIChar artChar(const ArtChar type) const { return arts_.value(type, PIChar(' ')); }
//! \brief
//! \~english Clears cell matrix
//! \~russian Очищает матрицу ячеек
static void clear(PIVector<PIVector<PIScreenTypes::Cell>> & cells);
private:

View File

@@ -1,9 +1,11 @@
/*! \file piscreentile.h
* \ingroup Console
* \~\brief
* \~english Basic PIScreen tile
* \~russian Базовый тайл для PIScreen
*/
//! \file piscreentile.h
//! \ingroup Console
//! \brief
//! \~english Basic PIScreen tile
//! \~russian Базовый тайл для PIScreen
//! \details
//! \~english Base class for all screen tiles providing layout and event handling.
//! \~russian Базовый класс для всех экранных тайлов, обеспечивающий компоновку и обработку событий.
/*
PIP - Platform Independent Primitives
Basic PIScreen tile
@@ -32,27 +34,92 @@
class PIScreenDrawer;
//! \brief
//! \~english Base tile class for console screen
//! \~russian Базовый класс тайла для консольного экрана
//! \brief
//! \~english Base tile class for console screen
//! \~russian Базовый класс тайла для консольного экрана
class PIP_CONSOLE_EXPORT PIScreenTile: public PIObject {
friend class PIScreen;
PIOBJECT_SUBCLASS(PIScreenTile, PIObject);
public:
//! \brief
//! \~english Constructs PIScreenTile
//! \~russian Создает PIScreenTile
//! \param n Tile name / Имя тайла
//! \param d Layout direction / Направление компоновки
//! \param p Size policy / Политика размера
PIScreenTile(const PIString & n = PIString(),
PIScreenTypes::Direction d = PIScreenTypes::Vertical,
PIScreenTypes::SizePolicy p = PIScreenTypes::Preferred);
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~PIScreenTile();
//! \brief
//! \~english Adds child tile
//! \~russian Добавляет дочерний тайл
void addTile(PIScreenTile * t);
//! \brief
//! \~english Takes ownership of tile
//! \~russian Забирает владение тайла
void takeTile(PIScreenTile * t);
//! \brief
//! \~english Removes child tile
//! \~russian Удаляет дочерний тайл
void removeTile(PIScreenTile * t);
//! \brief
//! \~english Returns parent tile
//! \~russian Возвращает родительский тайл
PIScreenTile * parentTile() const { return parent; }
//! \brief
//! \~english Returns child tiles
//! \~russian Возвращает дочерние тайлы
//! \param only_visible Only visible tiles / Только видимые тайлы
PIVector<PIScreenTile *> children(bool only_visible = false);
//! \brief
//! \~english Returns child under mouse position
//! \~russian Возвращает тайл под мышью
PIScreenTile * childUnderMouse(int x, int y);
//! \brief
//! \~english Shows tile
//! \~russian Показывает тайл
void show() { visible = true; }
//! \brief
//! \~english Hides tile
//! \~russian Скрывает тайл
void hide() { visible = false; }
//! \brief
//! \~english Sets focus to this tile
//! \~russian Устанавливает фокус на этот тайл
void setFocus();
//! \brief
//! \~english Checks if tile has focus
//! \~russian Проверяет, имеет ли тайл фокус
bool hasFocus() const { return has_focus; }
//! \brief
//! \~english Sets all margins
//! \~russian Устанавливает все отступы
void setMargins(int m) { marginLeft = marginRight = marginTop = marginBottom = m; }
//! \brief
//! \~english Sets individual margins
//! \~russian Устанавливает отдельные отступы
void setMargins(int l, int r, int t, int b) {
marginLeft = l;
marginRight = r;
@@ -60,9 +127,24 @@ public:
marginBottom = b;
}
//! \brief
//! \~english Returns tile X position
//! \~russian Возвращает позицию X тайла
int x() const { return x_; }
//! \brief
//! \~english Returns tile Y position
//! \~russian Возвращает позицию Y тайла
int y() const { return y_; }
//! \brief
//! \~english Returns tile width
//! \~russian Возвращает ширину тайла
int width() const { return width_; }
//! \brief
//! \~english Returns tile height
//! \~russian Возвращает высоту тайла
int height() const { return height_; }
PIScreenTypes::Direction direction;
@@ -77,29 +159,64 @@ public:
bool visible;
protected:
//! Returns desired tile size in "w" and "h"
//! \brief
//! \~english Returns desired tile size in "w" and "h"
//! \~russian Возвращает желаемый размер тайла в "w" и "h"
virtual void sizeHint(int & w, int & h) const;
//! Tile has been resized to "w"x"h"
//! \brief
//! \~english Tile has been resized to "w"x"h"
//! \~russian Тайл был изменен на "w"x"h"
virtual void resizeEvent(int w, int h) {}
//! Draw tile with drawer "d" in world-space coordinates
//! \brief
//! \~english Draw tile with drawer "d" in world-space coordinates
//! \~russian Рисует тайл отрисовщиком "d" в мировых координатах
virtual void drawEvent(PIScreenDrawer * d) {}
//! Return "true" if you process key
//! \brief
//! \~english Return "true" if you process key
//! \~russian Возвращает "true" если вы обрабатываете клавишу
virtual bool keyEvent(PIKbdListener::KeyEvent key) { return false; }
//! Return "true" if you process event
//! \brief
//! \~english Return "true" if you process event
//! \~russian Возвращает "true" если вы обрабатываете событие
virtual bool mouseEvent(PIKbdListener::MouseEvent me) { return false; }
//! Return "true" if you process wheel
//! \brief
//! \~english Return "true" if you process wheel
//! \~russian Возвращает "true" если вы обрабатываете колесо
virtual bool wheelEvent(PIKbdListener::WheelEvent we) { return false; }
//! \brief
//! \~english Raises tile event
//! \~russian Вызывает событие тайла
void raiseEvent(PIScreenTypes::TileEvent e);
//! \brief
//! \~english Sets screen reference
//! \~russian Устанавливает ссылку на экран
void setScreen(PIScreenTypes::PIScreenBase * s);
//! \brief
//! \~english Deletes all children
//! \~russian Удаляет всех потомков
void deleteChildren();
//! \brief
//! \~english Internal draw event
//! \~russian Внутреннее событие отрисовки
void drawEventInternal(PIScreenDrawer * d);
//! \brief
//! \~english Performs layout
//! \~russian Выполняет компоновку
void layout();
//! \brief
//! \~english Checks if layout is needed
//! \~russian Проверяет, нужна ли компоновка
bool needLayout() { return size_policy != PIScreenTypes::Ignore; }
PIVector<PIScreenTile *> tiles;

View File

@@ -1,9 +1,11 @@
/*! \file piscreentiles.h
* \ingroup Console
* \~\brief
* \~english Various tiles for PIScreen
* \~russian Различные тайлы для PIScreen
*/
//! \file piscreentiles.h
//! \ingroup Console
//! \brief
//! \~english Various tiles for PIScreen
//! \~russian Различные тайлы для PIScreen
//! \details
//! \~english Provides ready-to-use tile implementations for common UI elements.
//! \~russian Обеспечивает готовые к использованию реализации тайлов для общих элементов UI.
/*
PIP - Platform Independent Primitives
Various tiles for PIScreen
@@ -30,125 +32,326 @@
#include "piscreentile.h"
//! \brief
//! \~english Tile for displaying simple text content
//! \~russian Тайл для отображения простого текстового контента
class PIP_CONSOLE_EXPORT TileSimple: public PIScreenTile {
PIOBJECT_SUBCLASS(TileSimple, PIScreenTile);
public:
//! \brief Row type
typedef PIPair<PIString, PIScreenTypes::CellFormat> Row;
//! \brief
//! \~english Constructs TileSimple
//! \~russian Создает TileSimple
//! \param n Tile name / Имя тайла
TileSimple(const PIString & n = PIString());
//! \brief
//! \~english Constructs TileSimple with row
//! \~russian Создает TileSimple со строкой
//! \param r Row content / Содержимое строки
TileSimple(const Row & r);
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~TileSimple() {}
//! \brief Tile content rows
PIVector<Row> content;
//! \brief Text alignment
PIScreenTypes::Alignment alignment;
protected:
//! \brief
//! \~english Calculates tile size hint
//! \~russian Вычисляет рекомендуемый размер тайла
void sizeHint(int & w, int & h) const override;
//! \brief
//! \~english Draws tile content
//! \~russian Рисует содержимое тайла
void drawEvent(PIScreenDrawer * d) override;
};
class TileList;
//! \brief
//! \~english Scrollbar for list containers
//! \~russian Полоса прокрутки для списков
class PIP_CONSOLE_EXPORT TileScrollBar: public PIScreenTile {
PIOBJECT_SUBCLASS(TileScrollBar, PIScreenTile);
friend class TileList;
public:
//! \brief
//! \~english Constructs TileScrollBar
//! \~russian Создает TileScrollBar
//! \param n Tile name / Имя тайла
TileScrollBar(const PIString & n = PIString());
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~TileScrollBar() {}
//! \brief
//! \~english Sets minimum value
//! \~russian Устанавливает минимальное значение
void setMinimum(int v);
//! \brief
//! \~english Sets maximum value
//! \~russian Устанавливает максимальное значение
void setMaximum(int v);
//! \brief
//! \~english Sets current value
//! \~russian Устанавливает текущее значение
void setValue(int v);
//! \brief
//! \~english Returns minimum value
//! \~russian Возвращает минимальное значение
int minimum() const { return minimum_; }
//! \brief
//! \~english Returns maximum value
//! \~russian Возвращает максимальное значение
int maximum() const { return maximum_; }
//! \brief
//! \~english Returns current value
//! \~russian Возвращает текущее значение
int value() const { return value_; }
//! \brief Scrollbar thickness
int thickness;
protected:
//! \brief Validates scrollbar state
//! \~english Validates scrollbar state
//! \~russian Проверяет состояние полосы прокрутки
void _check();
//! \brief
//! \~english Calculates tile size hint
//! \~russian Вычисляет рекомендуемый размер тайла
void sizeHint(int & w, int & h) const override;
//! \brief
//! \~english Draws tile content
//! \~russian Рисует содержимое тайла
void drawEvent(PIScreenDrawer * d) override;
//! \brief
//! \~english Handles mouse events
//! \~russian Обрабатывает события мыши
bool mouseEvent(PIKbdListener::MouseEvent me) override;
int minimum_, maximum_, value_;
PIChar line_char;
};
//! \brief
//! \~english Scrollable list tile
//! \~russian Прокручиваемый список
class PIP_CONSOLE_EXPORT TileList: public PIScreenTile {
PIOBJECT_SUBCLASS(TileList, PIScreenTile);
public:
//! \brief Selection mode
enum SelectionMode {
NoSelection,
SingleSelection,
MultiSelection
};
enum EventType {
SelectionChanged,
RowPressed
NoSelection, //!< No selection / Без выделения
SingleSelection, //!< Single item selection / Выделение одного элемента
MultiSelection //!< Multiple items selection / Выделение нескольких элементов
};
//! \brief Event type
enum EventType {
SelectionChanged, //!< Selection changed / Выделение изменено
RowPressed //!< Row pressed / Строка нажата
};
//! \brief
//! \~english Constructs TileList
//! \~russian Создает TileList
//! \param n Tile name / Имя тайла
//! \param sm Selection mode / Режим выделения
TileList(const PIString & n = PIString(), SelectionMode sm = NoSelection);
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~TileList() {}
//! \brief Row type
typedef PIPair<PIString, PIScreenTypes::CellFormat> Row;
//! \brief List content
PIDeque<Row> content;
//! \brief Text alignment
PIScreenTypes::Alignment alignment;
//! \brief Selection mode
SelectionMode selection_mode;
//! \brief Selected indices
PISet<int> selected;
//! \brief Line height
int lhei, cur, offset;
protected:
//! \brief
//! \~english Calculates tile size hint
//! \~russian Вычисляет рекомендуемый размер тайла
void sizeHint(int & w, int & h) const override;
//! \brief
//! \~english Called when resized
//! \~russian Вызывается при изменении размера
void resizeEvent(int w, int h) override;
//! \brief
//! \~english Draws tile content
//! \~russian Рисует содержимое тайла
void drawEvent(PIScreenDrawer * d) override;
//! \brief
//! \~english Handles key events
//! \~russian Обрабатывает события клавиатуры
bool keyEvent(PIKbdListener::KeyEvent key) override;
//! \brief
//! \~english Handles mouse events
//! \~russian Обрабатывает события мыши
bool mouseEvent(PIKbdListener::MouseEvent me) override;
//! \brief
//! \~english Handles wheel events
//! \~russian Обрабатывает события колеса
bool wheelEvent(PIKbdListener::WheelEvent we) override;
TileScrollBar * scroll;
bool mouse_sel;
};
//! \brief
//! \~english Clickable button tile
//! \~russian Кликабельная кнопка
class PIP_CONSOLE_EXPORT TileButton: public PIScreenTile {
PIOBJECT_SUBCLASS(TileButton, PIScreenTile);
public:
//! \brief
//! \~english Constructs TileButton
//! \~russian Создает TileButton
//! \param n Tile name / Имя тайла
TileButton(const PIString & n = PIString());
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~TileButton() {}
//! \brief Event type
enum EventType {
ButtonClicked
ButtonClicked //!< Button clicked / Кнопка нажата
};
//! \brief Button format
PIScreenTypes::CellFormat format;
//! \brief Button text
PIString text;
protected:
//! \brief
//! \~english Calculates tile size hint
//! \~russian Вычисляет рекомендуемый размер тайла
void sizeHint(int & w, int & h) const override;
//! \brief
//! \~english Draws tile content
//! \~russian Рисует содержимое тайла
void drawEvent(PIScreenDrawer * d) override;
//! \brief
//! \~english Handles key events
//! \~russian Обрабатывает события клавиатуры
bool keyEvent(PIKbdListener::KeyEvent key) override;
//! \brief
//! \~english Handles mouse events
//! \~russian Обрабатывает события мыши
bool mouseEvent(PIKbdListener::MouseEvent me) override;
};
//! \brief
//! \~english Group of buttons with selection
//! \~russian Группа кнопок с выбором
class PIP_CONSOLE_EXPORT TileButtons: public PIScreenTile {
PIOBJECT_SUBCLASS(TileButtons, PIScreenTile);
public:
//! \brief
//! \~english Constructs TileButtons
//! \~russian Создает TileButtons
//! \param n Tile name / Имя тайла
TileButtons(const PIString & n = PIString());
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~TileButtons() {}
//! \brief Event type
enum EventType {
ButtonSelected
ButtonSelected //!< Button selected / Кнопка выбрана
};
//! \brief Button type
typedef PIPair<PIString, PIScreenTypes::CellFormat> Button;
//! \brief Button alignment
PIScreenTypes::Alignment alignment;
//! \brief Button content
PIVector<Button> content;
//! \brief Current selection
int cur;
protected:
//! \brief
//! \~english Calculates tile size hint
//! \~russian Вычисляет рекомендуемый размер тайла
void sizeHint(int & w, int & h) const override;
//! \brief
//! \~english Draws tile content
//! \~russian Рисует содержимое тайла
void drawEvent(PIScreenDrawer * d) override;
//! \brief
//! \~english Handles key events
//! \~russian Обрабатывает события клавиатуры
bool keyEvent(PIKbdListener::KeyEvent key) override;
//! \brief
//! \~english Handles mouse events
//! \~russian Обрабатывает события мыши
bool mouseEvent(PIKbdListener::MouseEvent me) override;
//! \brief Button rectangle
struct Rect {
Rect(int _x0 = 0, int _y0 = 0, int _x1 = 0, int _y1 = 0): x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
int x0, y0, x1, y1;
@@ -157,74 +360,198 @@ protected:
};
//! \brief
//! \~english Checkbox with toggle state
//! \~russian Флажок с переключаемым состоянием
class PIP_CONSOLE_EXPORT TileCheck: public PIScreenTile {
PIOBJECT_SUBCLASS(TileCheck, PIScreenTile);
public:
//! \brief
//! \~english Constructs TileCheck
//! \~russian Создает TileCheck
//! \param n Tile name / Имя тайла
TileCheck(const PIString & n = PIString());
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~TileCheck() {}
//! \brief Event type
enum EventType {
Toggled
Toggled //!< State toggled / Состояние переключено
};
//! \brief Checkbox format
PIScreenTypes::CellFormat format;
//! \brief Checkbox text
PIString text;
//! \brief Checkbox state
bool toggled;
protected:
//! \brief
//! \~english Calculates tile size hint
//! \~russian Вычисляет рекомендуемый размер тайла
void sizeHint(int & w, int & h) const override;
//! \brief
//! \~english Draws tile content
//! \~russian Рисует содержимое тайла
void drawEvent(PIScreenDrawer * d) override;
//! \brief
//! \~english Handles key events
//! \~russian Обрабатывает события клавиатуры
bool keyEvent(PIKbdListener::KeyEvent key) override;
//! \brief
//! \~english Handles mouse events
//! \~russian Обрабатывает события мыши
bool mouseEvent(PIKbdListener::MouseEvent me) override;
};
//! \brief
//! \~english Progress bar for displaying progress
//! \~russian Индикатор прогресса
class PIP_CONSOLE_EXPORT TileProgress: public PIScreenTile {
PIOBJECT_SUBCLASS(TileProgress, PIScreenTile);
public:
//! \brief
//! \~english Constructs TileProgress
//! \~russian Создает TileProgress
//! \param n Tile name / Имя тайла
TileProgress(const PIString & n = PIString());
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~TileProgress() {}
//! \brief Progress format
PIScreenTypes::CellFormat format;
//! \brief Prefix text
PIString prefix;
//! \brief Suffix text
PIString suffix;
//! \brief Maximum value
double maximum;
//! \brief Current value
double value;
protected:
//! \brief
//! \~english Calculates tile size hint
//! \~russian Вычисляет рекомендуемый размер тайла
void sizeHint(int & w, int & h) const override;
//! \brief
//! \~english Draws tile content
//! \~russian Рисует содержимое тайла
void drawEvent(PIScreenDrawer * d) override;
};
//! \brief
//! \~english Tile for displaying console output
//! \~russian Тайл для отображения консольного вывода
//! \brief
//! \~english Tile for displaying console output
//! \~russian Тайл для отображения консольного вывода
class PIP_CONSOLE_EXPORT TilePICout: public TileList {
PIOBJECT_SUBCLASS(TilePICout, PIScreenTile);
public:
//! \brief
//! \~english Constructs TilePICout
//! \~russian Создает TilePICout
//! \param n Tile name / Имя тайла
TilePICout(const PIString & n = PIString());
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~TilePICout() {}
//! \brief Output format
PIScreenTypes::CellFormat format;
//! \brief Maximum lines
int max_lines;
protected:
//! \brief
//! \~english Draws tile content
//! \~russian Рисует содержимое тайла
void drawEvent(PIScreenDrawer * d) override;
//! \brief
//! \~english Handles key events
//! \~russian Обрабатывает события клавиатуры
bool keyEvent(PIKbdListener::KeyEvent key) override;
};
//! \brief
//! \~english Text input tile
//! \~russian Тайл текстового ввода
//! \brief
//! \~english Text input tile
//! \~russian Тайл текстового ввода
class PIP_CONSOLE_EXPORT TileInput: public PIScreenTile {
PIOBJECT_SUBCLASS(TileInput, PIScreenTile);
public:
//! \brief
//! \~english Constructs TileInput
//! \~russian Создает TileInput
//! \param n Tile name / Имя тайла
TileInput(const PIString & n = PIString());
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~TileInput() {}
//! \brief Input format
PIScreenTypes::CellFormat format;
//! \brief Input text
PIString text;
//! \brief Maximum text length
int max_length;
protected:
//! \brief
//! \~english Calculates tile size hint
//! \~russian Вычисляет рекомендуемый размер тайла
void sizeHint(int & w, int & h) const override;
//! \brief
//! \~english Draws tile content
//! \~russian Рисует содержимое тайла
void drawEvent(PIScreenDrawer * d) override;
//! \brief
//! \~english Handles key events
//! \~russian Обрабатывает события клавиатуры
bool keyEvent(PIKbdListener::KeyEvent key) override;
//! \brief
//! \~english Resets cursor position
//! \~russian Сбрасывает позицию курсора
void reserCursor();
int cur, offset;
bool inv;

View File

@@ -1,9 +1,11 @@
/*! \file piscreentypes.h
* \ingroup Console
* \~\brief
* \~english Types for PIScreen
* \~russian Типы для PIScreen
*/
//! \file piscreentypes.h
//! \ingroup Console
//! \brief
//! \~english Types for PIScreen
//! \~russian Типы для PIScreen
//! \details
//! \~english Provides common types used by screen tiles and drawer.
//! \~russian Обеспечивает общие типы, используемые тайлами и отрисовщиком экрана.
/*
PIP - Platform Independent Primitives
Types for PIScreen
@@ -34,7 +36,9 @@ class PIScreenTile;
namespace PIScreenTypes {
//! Color for chars or background
//! \brief Color for chars or background
//! \~english Console color values
//! \~russian Значения цветов консоли
enum Color {
Default /** Default */,
Black /** Black */,
@@ -48,7 +52,9 @@ enum Color {
Transparent /** Save previous color */
};
//! Flags for chars
//! \brief Flags for chars
//! \~english Character formatting flags
//! \~russian Флаги форматирования символов
enum CharFlag {
Bold /** Bold or bright */ = 0x1,
Blink /** Blink text */ = 0x2,
@@ -56,14 +62,18 @@ enum CharFlag {
Inverse = 0x08
};
//! Alignment
//! \brief Alignment
//! \~english Text alignment modes
//! \~russian Режимы выравнивания текста
enum Alignment {
Left /** Left */,
Center /** Center */,
Right /** Right */
};
//! Size policy
//! \brief Size policy
//! \~english Tile sizing policies
//! \~russian Политики размеров тайлов
enum SizePolicy {
Fixed /** Fixed size */,
Preferred /** Preferred size */,
@@ -71,13 +81,17 @@ enum SizePolicy {
Ignore /** Ignore layout logic */
};
//! Direction
//! \brief Direction
//! \~english Layout directions
//! \~russian Направления компоновки
enum Direction {
Horizontal /** Horizontal */,
Vertical /** Vertical */
};
//! Focus flags
//! \brief Focus flags
//! \~english Tile focus behavior flags
//! \~russian Флаги поведения фокуса тайла
enum FocusFlag {
CanHasFocus /** Tile can has focus */ = 0x1,
NextByTab /** Focus passed to next tile by tab key */ = 0x2,
@@ -92,8 +106,19 @@ enum FocusFlag {
typedef PIFlags<CharFlag> CharFlags;
typedef PIFlags<FocusFlag> FocusFlags;
//! \brief Cell format union
//! \~english Packed cell formatting data
//! \~russian Упакованные данные форматирования ячейки
union PIP_CONSOLE_EXPORT CellFormat {
//! \brief
//! \~english Constructs CellFormat from raw value
//! \~russian Создает CellFormat из сырого значения
CellFormat(ushort f = 0) { raw_format = f; }
//! \brief
//! \~english Constructs CellFormat from color and flags
//! \~russian Создает CellFormat из цвета и флагов
CellFormat(Color col_char, Color col_back = Default, CharFlags flags_ = 0) {
color_char = col_char;
color_back = col_back;
@@ -105,19 +130,49 @@ union PIP_CONSOLE_EXPORT CellFormat {
ushort color_back: 4;
ushort flags : 8;
};
//! \brief
//! \~english Equality operator
//! \~russian Оператор равенства
bool operator==(const CellFormat & c) const { return raw_format == c.raw_format; }
//! \brief
//! \~english Inequality operator
//! \~russian Оператор неравенства
bool operator!=(const CellFormat & c) const { return raw_format != c.raw_format; }
};
//! \brief Screen cell
//! \~english Single character cell with formatting
//! \~russian Одна символьная ячейка с форматированием
struct PIP_CONSOLE_EXPORT Cell {
//! \brief
//! \~english Constructs Cell
//! \~russian Создает Cell
Cell(PIChar c = PIChar(' '), CellFormat f = CellFormat()) {
symbol = c;
format = f;
}
//! \brief Cell format
CellFormat format;
//! \brief Cell character
PIChar symbol;
//! \brief
//! \~english Equality operator
//! \~russian Оператор равенства
bool operator==(const Cell & c) const { return format == c.format && symbol == c.symbol; }
//! \brief
//! \~english Inequality operator
//! \~russian Оператор неравенства
bool operator!=(const Cell & c) const { return format != c.format || symbol != c.symbol; }
//! \brief
//! \~english Assignment operator
//! \~russian Оператор присваивания
Cell & operator=(const Cell & c) {
symbol = c.symbol;
if (c.format.color_back == Transparent) {
@@ -129,18 +184,52 @@ struct PIP_CONSOLE_EXPORT Cell {
}
};
//! \brief Tile event data
//! \~english Event data passed to tiles
//! \~russian Данные события, передаваемые тайлам
struct PIP_CONSOLE_EXPORT TileEvent {
//! \brief
//! \~english Constructs TileEvent
//! \~russian Создает TileEvent
//! \param t Event type / Тип события
//! \param d Event data / Данные события
TileEvent(int t = -1, const PIVariant & d = PIVariant()): type(t), data(d) {}
//! \brief Event type
int type;
//! \brief Event data
PIVariant data;
};
//! \brief Base screen interface
//! \~english Base interface for screen tiles
//! \~russian Базовый интерфейс для экранных тайлов
class PIP_CONSOLE_EXPORT PIScreenBase {
public:
//! \brief
//! \~english Constructs PIScreenBase
//! \~russian Создает PIScreenBase
PIScreenBase() {}
//! \brief
//! \~english Destructor
//! \~russian Деструктор
virtual ~PIScreenBase() {}
//! \brief
//! \~english Handles internal tile event
//! \~russian Обрабатывает внутреннее событие тайла
virtual void tileEventInternal(PIScreenTile *, TileEvent) {}
//! \brief
//! \~english Handles tile removal
//! \~russian Обрабатывает удаление тайла
virtual void tileRemovedInternal(PIScreenTile *) {}
//! \brief
//! \~english Handles tile focus change
//! \~russian Обрабатывает изменение фокуса тайла
virtual void tileSetFocusInternal(PIScreenTile *) {}
};

View File

@@ -1,9 +1,11 @@
/*! \file piterminal.h
* \ingroup Console
* \~\brief
* \~english Virtual terminal
* \~russian Виртуальный терминал
*/
//! \file piterminal.h
//! \ingroup Console
//! \brief
//! \~english Virtual terminal
//! \~russian Виртуальный терминал
//! \details
//! \~english Provides terminal emulation for reading console input and output.
//! \~russian Обеспечивает эмуляцию терминала для чтения ввода и вывода консоли.
/*
PIP - Platform Independent Primitives
Virtual terminal
@@ -31,26 +33,73 @@
#include "piscreentypes.h"
//! \brief
//! \~english Virtual terminal
//! \~russian Виртуальный терминал
class PIP_CONSOLE_EXPORT PITerminal: public PIThread {
PIOBJECT_SUBCLASS(PITerminal, PIThread);
public:
//! Constructs %PITerminal
//! \brief
//! \~english Constructs PITerminal
//! \~russian Создает PITerminal
PITerminal();
//! \brief
//! \~english Destructor
//! \~russian Деструктор
~PITerminal();
//! \brief
//! \~english Returns number of columns
//! \~russian Возвращает количество колонок
int columns() const { return size_x; }
//! \brief
//! \~english Returns number of rows
//! \~russian Возвращает количество строк
int rows() const { return size_y; }
//! \brief
//! \~english Resizes terminal
//! \~russian Изменяет размер терминала
//! \param cols Number of columns / Количество колонок
//! \param rows Number of rows / Количество строк
bool resize(int cols, int rows);
//! \brief
//! \~english Writes data to terminal
//! \~russian Записывает данные в терминал
void write(const PIByteArray & d);
//! \brief
//! \~english Writes special key to terminal
//! \~russian Записывает специальную клавишу в терминал
void write(PIKbdListener::SpecialKey k, PIKbdListener::KeyModifiers m);
//! \brief
//! \~english Writes key event to terminal
//! \~russian Записывает событие клавиши в терминал
void write(PIKbdListener::KeyEvent ke);
//! \brief
//! \~english Returns terminal content
//! \~russian Возвращает содержимое терминала
PIVector<PIVector<PIScreenTypes::Cell>> content();
//! \brief
//! \~english Checks if key is special
//! \~russian Проверяет, является ли клавиша специальной
static bool isSpecialKey(int k);
//! \brief
//! \~english Initializes terminal
//! \~russian Инициализирует терминал
bool initialize();
//! \brief
//! \~english Destroys terminal
//! \~russian Уничтожает терминал
void destroy();
private:

View File

@@ -49,14 +49,28 @@
#include <type_traits>
//! \~english Reverse wrapper template class for iterating containers in reverse order.
//! \~russian Класс-обертка для обратного обхода контейнера через итераторы.
template<typename C>
class _PIReverseWrapper {
public:
//! \~english Constructor from non-const container reference.
//! \~russian Конструктор из неконстантной ссылки на контейнер.
_PIReverseWrapper(C & c): c_(c) {}
//! \~english Constructor from const container reference.
//! \~russian Конструктор из константной ссылки на контейнер.
_PIReverseWrapper(const C & c): c_(const_cast<C &>(c)) {}
//! \~english Returns reverse iterator to the first element.
//! \~russian Возвращает обратный итератор на первый элемент.
typename C::reverse_iterator begin() { return c_.rbegin(); }
//! \~english Returns reverse iterator past the last element.
//! \~russian Возвращает обратный итератор за последний элемент.
typename C::reverse_iterator end() { return c_.rend(); }
//! \~english Returns const reverse iterator to the first element.
//! \~russian Возвращает константный обратный итератор на первый элемент.
typename C::const_reverse_iterator begin() const { return c_.rbegin(); }
//! \~english Returns const reverse iterator past the last element.
//! \~russian Возвращает константный обратный итератор за последний элемент.
typename C::const_reverse_iterator end() const { return c_.rend(); }
private:
@@ -64,32 +78,47 @@ private:
};
//! \~english Base class for container constants calculation.
//! \~russian Базовый класс для вычисления констант контейнера.
class PIP_EXPORT _PIContainerConstantsBase {
public:
//! \~english Calculate minimum elements count for power of two growth.
//! \~russian Вычисляет минимальное количество элементов для роста кратного двум.
static size_t calcMinCountPoT(size_t szof);
//! \~english Calculate maximum elements count for power of two growth.
//! \~russian Вычисляет максимальное количество элементов для роста кратного двум.
static size_t calcMaxCountForPoT(size_t szof);
//! \~english Calculate step size after power of two growth.
//! \~russian Вычисляет размер шага после роста кратного двум.
static size_t calcStepAfterPoT(size_t szof);
};
//! \~english Template class for container constants based on element type size.
//! \~russian Шаблонный класс для констант контейнера на основе размера типа элемента.
template<typename T>
class _PIContainerConstants {
public:
// minimum elements for container
//! \~english Get minimum elements count for power of two growth.
//! \~russian Возвращает минимальное количество элементов для роста кратного двум.
static size_t minCountPoT() {
static const size_t ret = _PIContainerConstantsBase::calcMinCountPoT(sizeof(T));
return ret;
}
// maximum elements for 2^n growth
//! \~english Get maximum elements count for power of two growth.
//! \~russian Возвращает максимальное количество элементов для роста кратного двум.
static size_t maxCountForPoT() {
static const size_t ret = _PIContainerConstantsBase::calcMaxCountForPoT(sizeof(T));
return ret;
}
// add elements after 2^n growth
//! \~english Get step size after power of two growth.
//! \~russian Возвращает размер шага после роста кратного двум.
static size_t stepAfterPoT() {
static const size_t ret = _PIContainerConstantsBase::calcStepAfterPoT(sizeof(T));
return ret;
}
//! \~english Calculate new container size based on old and requested size.
//! \~russian Вычисляет новый размер контейнера на основе старого и запрошенного размера.
static size_t calcNewSize(size_t old_size, size_t new_size) {
if (new_size == 0) return 0;
if (new_size < maxCountForPoT()) {

View File

@@ -1,8 +1,17 @@
/*! \file piset.h
* \brief Set container
*
* This file declare PISet
*/
//! \addtogroup Containers
//! \{
//! \file piset.h
//! \brief
//! \~english Declares \a PISet
//! \~russian Объявление \a PISet
//! \~\authors
//! \~english
//! Ivan Pelipenko peri4ko@yandex.ru;
//! Andrey Bychkov work.a.b@yandex.ru;
//! \~russian
//! Иван Пелипенко peri4ko@yandex.ru;
//! Андрей Бычков work.a.b@yandex.ru;
//! \~\}
/*
PIP - Platform Independent Primitives
Set container
@@ -27,13 +36,33 @@
#include "pimap.h"
/*! \brief Set of any type
* \details This class used to store collection of unique elements
* of any type. You can only add values to set with \a operator<< or
* with function \a insert(). You can discover if value already in
* set with \a operator[] or with function \a find(). These function
* has logarithmic complexity.
*/
//! \addtogroup Containers
//! \{
//! \class PISet
//! \brief
//! \~english A set is a collection of unique elements.
//! \~russian Множество - это коллекция уникальных элементов.
//! \~\}
//! \details
//! \~english
//! This class is used to store a collection of unique elements of any type.
//! You can add values to the set using \a operator<< or the \a insert() function.
//! You can check if a value already exists in the set using \a operator[] or the \a contains() function.
//! These operations have logarithmic complexity.
//! To iterate over all elements, use STL-style iterators \a begin() and \a end().
//!
//! The set is implemented as a wrapper around \a PIMap, where keys are the elements
//! and values are dummy byte values (used only for storage).
//! \~russian
//! Этот класс используется для хранения коллекции уникальных элементов любого типа.
//! Вы можете добавлять значения в множество с помощью \a operator<< или функции \a insert().
//! Вы можете проверить, существует ли значение в множестве, используя \a operator[] или функцию \a contains().
//! Эти операции имеют логарифмическую сложность.
//! Для перебора всех элементов используйте итераторы в стиле STL \a begin() и \a end().
//!
//! Множество реализовано как обёртка над \a PIMap, где ключами являются элементы,
//! а значениями являются фиктивные байтовые значения (используются только для хранения).
//! \~\sa \a PIMap, \a PIVector
template<typename T>
class PISet: public PIMap<T, uchar> {
typedef PIMap<T, uchar> _CSet;
@@ -43,26 +72,31 @@ class PISet: public PIMap<T, uchar> {
friend PIBinaryStream<P> & operator>>(PIBinaryStream<P> & s, PISet<T1> & v);
public:
//! Contructs an empty set
//! \~english Constructs an empty set.
//! \~russian Создает пустое множество.
PISet() {}
//! Contructs set with one element "value"
//! \~english Constructs a set with one element `value`.
//! \~russian Создает множество с одним элементом `value`.
explicit PISet(const T & value) { _CSet::insert(value, 0); }
//! Contructs set with elements "v0" and "v1"
//! \~english Constructs a set with two elements `v0` and `v1`.
//! \~russian Создает множество с двумя элементами `v0` и `v1`.
PISet(const T & v0, const T & v1) {
_CSet::insert(v0, 0);
_CSet::insert(v1, 0);
}
//! Contructs set with elements "v0", "v1" and "v2"
//! \~english Constructs a set with three elements `v0`, `v1` and `v2`.
//! \~russian Создает множество с тремя элементами `v0`, `v1` и `v2`.
PISet(const T & v0, const T & v1, const T & v2) {
_CSet::insert(v0, 0);
_CSet::insert(v1, 0);
_CSet::insert(v2, 0);
}
//! Contructs set with elements "v0", "v1", "v2" and "v3"
//! \~english Constructs a set with four elements `v0`, `v1`, `v2` and `v3`.
//! \~russian Создает множество с четырьмя элементами `v0`, `v1`, `v2` и `v3`.
PISet(const T & v0, const T & v1, const T & v2, const T & v3) {
_CSet::insert(v0, 0);
_CSet::insert(v1, 0);
@@ -154,7 +188,8 @@ public:
inline const_iterator begin() const { return const_iterator(this, 0); }
inline const_iterator end() const { return const_iterator(this, _CSet::size()); }
//! Contructs set from vector of elements
//! \~english Constructs a set from a vector of elements.
//! \~russian Создает множество из вектора элементов.
explicit PISet(const PIVector<T> & values) {
if (values.isEmpty()) return;
for (int i = 0; i < values.size_s(); ++i) {
@@ -162,7 +197,8 @@ public:
}
}
//! Contructs set from deque of elements
//! \~english Constructs a set from a deque of elements.
//! \~russian Создает множество из deque элементов.
explicit PISet(const PIDeque<T> & values) {
if (values.isEmpty()) return;
for (int i = 0; i < values.size_s(); ++i) {
@@ -172,6 +208,8 @@ public:
typedef T key_type;
//! \~english Inserts element `t` into the set.
//! \~russian Вставляет элемент `t` в множество.
PISet<T> & operator<<(const T & t) {
_CSet::insert(t, 0);
return *this;
@@ -185,52 +223,62 @@ public:
return *this;
}
//! \~english Tests if element `key` exists in the set.
//! \~russian Проверяет наличие элемента `key` в массиве.
//! \~english Tests if element `t` exists in the set.
//! \~russian Проверяет наличие элемента `t` в множестве.
inline bool contains(const T & t) const { return _CSet::contains(t); }
//! Returns if element "t" exists in this set
//! \~english Tests if element `t` exists in the set.
//! \~russian Проверяет наличие элемента `t` в множестве.
bool operator[](const T & t) const { return _CSet::contains(t); }
//! Returns if element "t" exists in this set
//! \~english Removes element `t` from the set.
//! \~russian Удаляет элемент `t` из множества.
PISet<T> & remove(const T & t) {
_CSet::remove(t);
return *this;
}
//! Unite set with "v"
//! \~english Returns the union of this set with set `v`.
//! \~russian Возвращает объединение этого множества с множеством `v`.
PISet<T> & unite(const PISet<T> & v) {
for (const auto & i: v)
_CSet::insert(i, 0);
return *this;
}
//! Subtract set with "v"
//! \~english Returns the difference of this set and set `v`.
//! \~russian Возвращает разность этого множества и множества `v`.
PISet<T> & subtract(const PISet<T> & v) {
for (const auto & i: v)
_CSet::remove(i);
return *this;
}
//! Intersect set with "v"
//! \~english Returns the intersection of this set with set `v`.
//! \~russian Возвращает пересечение этого множества с множеством `v`.
PISet<T> & intersect(const PISet<T> & v) {
_CSet::removeWhere([&v](const T & k, uchar) { return !v.contains(k); });
return *this;
}
//! Unite set with "v"
//! \~english Returns the union of this set with set `v`.
//! \~russian Возвращает объединение этого множества с множеством `v`.
PISet<T> & operator+=(const PISet<T> & v) { return unite(v); }
//! Unite set with "v"
//! \~english Returns the union of this set with set `v`.
//! \~russian Возвращает объединение этого множества с множеством `v`.
PISet<T> & operator|=(const PISet<T> & v) { return unite(v); }
//! Subtract set with "v"
//! \~english Returns the difference of this set and set `v`.
//! \~russian Возвращает разность этого множества и множества `v`.
PISet<T> & operator-=(const PISet<T> & v) { return subtract(v); }
//! Intersect set with "v"
//! \~english Returns the intersection of this set with set `v`.
//! \~russian Возвращает пересечение этого множества с множеством `v`.
PISet<T> & operator&=(const PISet<T> & v) { return intersect(v); }
//! Returns content of set as PIVector
//! \~english Converts the set to a vector.
//! \~russian Преобразует множество в вектор.
PIVector<T> toVector() const {
PIVector<T> ret;
for (const auto & i: *this)
@@ -238,7 +286,8 @@ public:
return ret;
}
//! Returns content of set as PIDeque
//! \~english Converts the set to a deque.
//! \~russian Преобразует множество в deque.
PIDeque<T> toDeque() const {
PIDeque<T> ret;
for (const auto & i: *this)
@@ -248,7 +297,8 @@ public:
};
//! \relatesalso PISet \brief Returns unite of two sets
//! \~english Returns the union of two sets.
//! \~russian Возвращает объединение двух множеств.
template<typename T>
PISet<T> operator+(const PISet<T> & v0, const PISet<T> & v1) {
PISet<T> ret(v0);
@@ -256,7 +306,8 @@ PISet<T> operator+(const PISet<T> & v0, const PISet<T> & v1) {
return ret;
}
//! \relatesalso PISet \brief Returns subtraction of two sets
//! \~english Returns the difference of two sets.
//! \~russian Возвращает разность двух множеств.
template<typename T>
PISet<T> operator-(const PISet<T> & v0, const PISet<T> & v1) {
PISet<T> ret(v0);
@@ -264,7 +315,8 @@ PISet<T> operator-(const PISet<T> & v0, const PISet<T> & v1) {
return ret;
}
//! \relatesalso PISet \brief Returns unite of two sets
//! \~english Returns the union of two sets.
//! \~russian Возвращает объединение двух множеств.
template<typename T>
PISet<T> operator|(const PISet<T> & v0, const PISet<T> & v1) {
PISet<T> ret(v0);
@@ -272,7 +324,8 @@ PISet<T> operator|(const PISet<T> & v0, const PISet<T> & v1) {
return ret;
}
//! \relatesalso PISet \brief Returns intersetion of two sets
//! \~english Returns the intersection of two sets.
//! \~russian Возвращает пересечение двух множеств.
template<typename T>
PISet<T> operator&(const PISet<T> & v0, const PISet<T> & v1) {
PISet<T> ret(v0);
@@ -281,6 +334,9 @@ PISet<T> operator&(const PISet<T> & v0, const PISet<T> & v1) {
}
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
template<typename Type>
inline PICout operator<<(PICout s, const PISet<Type> & v) {
s.space();
@@ -297,4 +353,6 @@ inline PICout operator<<(PICout s, const PISet<Type> & v) {
return s;
}
//! \}
#endif // PISET_H

View File

@@ -835,8 +835,8 @@ public:
//! piCout << v.contains({1,4}); // true
//! piCout << v.contains({1,5}); // false
//! \endcode
//! \~\sa \a every(), \a any(), \a entries(), \a forEach()
inline bool contains(const PIVector<T> & v, ssize_t start = 0) const {
//! \~\sa \a every(), \a any(), \a entries(), \a forEach(), \a contains()
inline bool containsAll(const PIVector<T> & v, ssize_t start = 0) const {
if (start < 0) {
start = piv_size + start;
if (start < 0) start = 0;
@@ -854,6 +854,24 @@ public:
return true;
}
//! \~english Tests if any element of `v` exists in the array.
//! \~russian Проверяет наличие хотя бы одного из элементов `v` в массиве.
//! \~\sa \a containsAll(), \a contains()
inline bool containsAny(const PIVector<T> & v, ssize_t start = 0) const {
if (start < 0) {
start = piv_size + start;
if (start < 0) start = 0;
}
for (const T & e: v) {
for (size_t i = start; i < piv_size; ++i) {
if (e == piv_data[i]) {
return true;
}
}
}
return false;
}
//! \~english Count elements equal `e` in the array.
//! \~russian Подсчитывает количество элементов, совпадающих с элементом `e` в массиве.
//! \~\details
@@ -1303,14 +1321,16 @@ public:
//! piCout << v; // {1, 3, 7, 5}
//! \endcode
//! \~\sa \a append(), \a prepend(), \a remove()
inline PIVector<T> & insert(size_t index, const T & e = T()) {
alloc(piv_size + 1);
if (index < piv_size - 1) {
const size_t os = piv_size - index - 1;
memmove(reinterpret_cast<void *>(piv_data + index + 1), reinterpret_cast<const void *>(piv_data + index), os * sizeof(T));
inline PIVector<T> & insert(size_t index, const T & e = T(), size_t count = 1) {
alloc(piv_size + count);
if (index < piv_size - count) {
const size_t os = piv_size - index - count;
memmove(reinterpret_cast<void *>(piv_data + index + count), reinterpret_cast<const void *>(piv_data + index), os * sizeof(T));
}
PIINTROSPECTION_CONTAINER_USED(T, count)
for (size_t i = 0; i < count; ++i) {
elementNew(piv_data + index + i, e);
}
PIINTROSPECTION_CONTAINER_USED(T, 1)
elementNew(piv_data + index, e);
return *this;
}

View File

@@ -1,8 +1,15 @@
/*! \file pivector2d.h
* \brief 2D wrapper around PIVector
*
* This file declares PIVector
*/
//! \addtogroup Containers
//! \{
//! \file pivector2d.h
//! \brief
//! \~english Declares \a PIVector2D
//! \~russian Объявление \a PIVector2D
//! \~\authors
//! \~english
//! Andrey Bychkov work.a.b@yandex.ru;
//! \~russian
//! Андрей Бычков work.a.b@yandex.ru;
//! \~\}
/*
PIP - Platform Independent Primitives
2D wrapper around PIVector
@@ -27,29 +34,111 @@
#include "pivector.h"
/*! \brief 2D array,
* \details This class used to store 2D array of any type elements as plain vector.
* You can read/write any element via operators [][], first dimension - row, second - column.
* The first dimension is Row, and you can operate with Row as PIVector<T>: modify any element, assign to another Row and etc.
* You can't add values to array, but you can modify any elements or create another PIVector2D.
* PIVector2D has constructors from PIVector<T> and PIVector<PIVector<T> >
*/
//! \addtogroup Containers
//! \{
//! \class PIVector2D
//! \brief
//! \~english 2D array container.
//! \~russian Двумерный контейнер-массив.
//! \details
//! \~english
//! This class is used to store a 2D array of elements of any type as a single continuous block of memory (a plain PIVector).
//! Elements can be accessed using the `[][]` operators, where the first index is the row and the second is the column.
//! Rows can be manipulated as \a PIVector objects, allowing modification of individual elements or assignment of entire rows.
//! You cannot directly add or remove elements to change the dimensions of the array after construction
//! (use \a resize(), \a addRow(), \a removeRow(), \a removeColumn() instead), but you can modify the values of existing elements.
//! \~russian
//! Этот класс используется для хранения двумерного массива элементов любого типа в виде единого непрерывного блока памяти (обычного
//! \a PIVector). Доступ к элементам осуществляется с помощью операторов `[][]`, где первый индекс — это строка, а второй — столбец. Со
//! строками можно работать как с объектами \a PIVector, что позволяет изменять отдельные элементы или присваивать целые строки. Нельзя
//! напрямую добавлять или удалять элементы, чтобы изменить размеры массива после создания (используйте \a resize(), \a addRow(), \a
//! removeRow(), \a removeColumn() для этого), но можно изменять значения существующих элементов.
template<typename T>
class PIVector2D {
public:
//! \brief
//! \~english Index structure for 2D array elements (row, column).
//! \~russian Структура индекса для элементов двумерного массива (строка, столбец).
struct Index {
//! \~english Row index in the 2D array.
//! \~russian Индекс строки в двумерном массиве.
ssize_t row = -1;
//! \~english Column index in the 2D array.
//! \~russian Индекс столбца в двумерном массиве.
ssize_t col = -1;
//! \~english Default constructor. Initializes row and col to -1 (invalid index).
//! \~russian Конструктор по умолчанию. Инициализирует row и col значениями -1 (некорректный индекс).
inline Index() = default;
//! \~english Constructs an Index with the given row and column values.
//! \~russian Создаёт Index с заданными значениями строки и столбца.
inline Index(ssize_t r, ssize_t c): row(r), col(c) {}
//! \~english Checks if the index is valid (both row and column are non-negative).
//! \~russian Проверяет, является ли индекс корректным (строка и столбец неотрицательны).
//! \~\sa isNotValid()
inline bool isValid() const { return row >= 0 && col >= 0; }
//! \~english Checks if the index is invalid (either row or column is negative).
//! \~russian Проверяет, является ли индекс некорректным (строка или столбец отрицательны).
//! \~\sa isValid()
inline bool isNotValid() const { return !isValid(); }
};
//! \~english Constructs an empty 2D array. No memory is allocated.
//! \~russian Создаёт пустой двумерный массив. Память не выделяется.
//! \details
//! \~english After this constructor, \a rows() and \a cols() return 0, and \a isEmpty() returns true.
//! \~russian После этого конструктора \a rows() и \a cols() возвращают 0, а \a isEmpty() возвращает true.
//! \~\sa PIVector::PIVector()
inline PIVector2D() { rows_ = cols_ = 0; }
//! \~english Constructs a 2D array with the given dimensions, filled with copies of `f`.
//! \~russian Создаёт двумерный массив заданного размера, заполненный копиями `f`.
//! \details
//! \~english The underlying storage is a single contiguous block of memory of size `rows * cols`.
//! All elements are initialized with the value `f`.
//! \~russian Внутреннее хранилище представляет собой единый непрерывный блок памяти размером `rows * cols`.
//! Все элементы инициализируются значением `f`.
//! \~\sa PIVector::PIVector(size_t, const T&)
inline PIVector2D(size_t rows, size_t cols, const T & f = T()) {
rows_ = rows;
cols_ = cols;
mat.resize(rows * cols, f);
}
//! \~english Constructs a 2D array from an existing 1D vector, reshaping it.
//! \~russian Создаёт двумерный массив из существующего одномерного вектора, изменяя его форму.
//! \details
//! \~english The constructor copies the data from `v` into the internal flat vector.
//! If `v` is larger than `rows * cols`, the excess elements are ignored (the vector is truncated).
//! If `v` is smaller, other values filled whith default cunstructor T()
//! \~russian Конструктор копирует данные из `v` во внутренний плоский вектор.
//! Если `v` больше, чем `rows * cols`, лишние элементы игнорируются (вектор обрезается).
//! Если `v` меньше, остальные значения будут заполнены из конструктора по умолчанию T()
//! \~\sa PIVector::PIVector(const PIVector&), reshape()
inline PIVector2D(size_t rows, size_t cols, const PIVector<T> & v): rows_(rows), cols_(cols), mat(v) { mat.resize(rows * cols); }
//! \~english Move constructs a 2D array from an existing 1D vector, reshaping it.
//! \~russian Конструктор перемещения из существующего одномерного вектора, изменяя его форму.
//! \details
//! \~english The data is moved from `v` into the internal flat vector, avoiding a copy.
//! After construction, `v` is left in a valid but unspecified state.
//! \~russian Данные перемещаются из `v` во внутренний плоский вектор, что позволяет избежать копирования.
//! После завершения конструктора `v` остаётся в корректном, но неопределённом состоянии.
//! \~\sa PIVector::PIVector(PIVector&&)
inline PIVector2D(size_t rows, size_t cols, PIVector<T> && v): rows_(rows), cols_(cols), mat(std::move(v)) { mat.resize(rows * cols); }
//! \~english Constructs a 2D array from a vector of vectors (jagged array). Assumes all inner vectors have the same size.
//! \~russian Создаёт двумерный массив из вектора векторов (рваного массива). Предполагается, что все внутренние векторы имеют
//! одинаковый размер.
//! \details
//! \~english If the input is empty, the constructed array is also empty. Otherwise, the number of columns is taken from the size of the
//! first inner vector. All inner vectors are concatenated in the internal flat storage.
//! \~russian Если входной массив пуст, создаётся пустой двумерный массив. В противном случае количество столбцов берётся из размера
//! первого внутреннего вектора. Все внутренние векторы конкатенируются во внутреннем плоском хранилище. \sa PIVector::append()
inline PIVector2D(const PIVector<PIVector<T>> & v) {
rows_ = v.size();
if (rows_) {
@@ -63,219 +152,858 @@ public:
if (mat.isEmpty()) rows_ = cols_ = 0;
}
//! \~english Returns the number of rows in the 2D array.
//! \~russian Возвращает количество строк в двумерном массиве.
//! \return Number of rows.
//! \details
//! \~english The result is always non-negative. If the array is empty, returns 0.
//! \~russian Результат всегда неотрицательный. Если массив пуст, возвращает 0.
//! \~\sa cols(), size(), PIVector::size()
inline size_t rows() const { return rows_; }
//! \~english Returns the number of columns in the 2D array.
//! \~russian Возвращает количество столбцов в двумерном массиве.
//! \return Number of columns.
//! \details
//! \~english The result is always non-negative. If the array is empty, returns 0.
//! \~russian Результат всегда неотрицательный. Если массив пуст, возвращает 0.
//! \~\sa rows(), size(), PIVector::size()
inline size_t cols() const { return cols_; }
//! \~english Returns the total number of elements (`rows * cols`).
//! \~russian Возвращает общее количество элементов (`строки * столбцы`).
//! \return Total number of elements.
//! \details
//! \~english This is equivalent to the size of the underlying flat vector.
//! \~russian Это эквивалентно размеру внутреннего плоского вектора.
//! \~\sa rows(), cols(), PIVector::size()
inline size_t size() const { return mat.size(); }
//! \~english Returns the total number of elements as a signed value.
//! \~russian Возвращает общее количество элементов в виде знакового числа.
//! \return Signed size.
//! \~\sa size(), PIVector::size_s()
inline ssize_t size_s() const { return mat.size_s(); }
//! \~english Returns the total number of elements (same as \a size()).
//! \~russian Возвращает общее количество элементов (то же, что и \a size()).
//! \return Total number of elements.
//! \~\sa size(), PIVector::length()
inline size_t length() const { return mat.length(); }
//! \~english Returns the number of elements that the underlying container has currently allocated space for.
//! \~russian Возвращает количество элементов, для которого сейчас выделена память во внутреннем контейнере.
//! \return Capacity of the flat vector.
//! \details
//! \~english This value may be larger than \a size(). It indicates how many elements can be added before a reallocation is needed.
//! \~russian Это значение может быть больше, чем \a size(). Оно показывает, сколько элементов можно добавить до того, как потребуется
//! перераспределение памяти. \sa reserve(), PIVector::capacity()
inline size_t capacity() const { return mat.capacity(); }
//! \~english Checks if the array has no elements.
//! \~russian Проверяет, пуст ли массив.
//! \return \c true if the array is empty, \c false otherwise.
//! \details
//! \~english An empty array has both rows and columns equal to 0.
//! \~russian Пустой массив имеет и строки, и столбцы равные 0.
//! \~\sa isNotEmpty(), PIVector::isEmpty()
inline bool isEmpty() const { return mat.isEmpty(); }
//! \~english Checks if the array has at least one element.
//! \~russian Проверяет, не пуст ли массив.
//! \return \c true if the array is not empty, \c false otherwise.
//! \~\sa isEmpty(), PIVector::isNotEmpty()
inline bool isNotEmpty() const { return mat.isNotEmpty(); }
class Row {
friend class PIVector2D<T>;
class RowConst;
class ColConst;
class Row;
class Col;
private:
inline Row(PIVector2D<T> * p, size_t row): p_(&(p->mat)) {
st_ = p->cols_ * row;
sz_ = p->cols_;
}
PIVector<T> * p_;
size_t st_, sz_;
public:
inline size_t size() const { return sz_; }
inline T & operator[](size_t index) { return (*p_)[st_ + index]; }
inline const T & operator[](size_t index) const { return (*p_)[st_ + index]; }
inline T * data(size_t index = 0) { return p_->data(st_ + index); }
inline const T * data(size_t index = 0) const { return p_->data(st_ + index); }
inline Row & operator=(const Row & other) {
if (p_ == other.p_ && st_ == other.st_) return *this;
const size_t sz = piMin<size_t>(sz_, other.sz_);
p_->_copyRaw(p_->data(st_), other.data(), sz);
return *this;
}
inline Row & operator=(const PIVector<T> & other) {
const size_t sz = piMin<size_t>(sz_, other.size());
p_->_copyRaw(p_->data(st_), other.data(), sz);
return *this;
}
inline PIVector<T> toVector() const { return PIVector<T>(p_->data(st_), sz_); }
};
class Col {
friend class PIVector2D<T>;
private:
inline Col(PIVector2D<T> * p, size_t row): p_(&(p->mat)) {
step_ = p->cols_;
row_ = row;
sz_ = p->rows_;
}
PIVector<T> * p_;
size_t step_, row_, sz_;
public:
inline size_t size() const { return sz_; }
inline T & operator[](size_t index) { return (*p_)[index * step_ + row_]; }
inline const T & operator[](size_t index) const { return (*p_)[index * step_ + row_]; }
inline T * data(size_t index = 0) { return p_->data(index * step_ + row_); }
inline const T * data(size_t index = 0) const { return p_->data(index * step_ + row_); }
inline Col & operator=(const Col & other) {
if (p_ == other.p_ && row_ == other.row_) return *this;
const size_t sz = piMin<size_t>(sz_, other.sz_);
for (int i = 0; i < sz; ++i)
(*p_)[i * step_ + row_] = other[i];
return *this;
}
inline Row & operator=(const PIVector<T> & other) {
const size_t sz = piMin<size_t>(sz_, other.size());
for (int i = 0; i < sz; ++i)
(*p_)[i * step_ + row_] = other[i];
return *this;
}
inline PIVector<T> toVector() const {
PIVector<T> ret;
ret.reserve(sz_);
for (size_t i = 0; i < sz_; i++)
ret << (*p_)[i * step_ + row_];
return ret;
}
};
//! \class RowConst
//! \brief
//! \~english Proxy class representing a single read-only row in a \a PIVector2D.
//! \~russian Прокси-класс, представляющий одну строку в \a PIVector2D только для чтения.
//! \details
//! \~english Returned by const \a operator[] or \a row(). Provides const access to row elements.
//! \~russian Возвращается константными версиями \a operator[] или \a row(). Предоставляет константный доступ к элементам строки.
//! \~\sa Row, ColConst
class RowConst {
friend class PIVector2D<T>;
private:
inline RowConst(const PIVector2D<T> * p, size_t row): p_(&(p->mat)) {
st_ = p->cols_ * row;
sz_ = p->cols_;
}
protected:
inline RowConst(const PIVector2D<T> * p, size_t row): p_(&(p->mat)), st_(p->cols_ * row), sz_(p->cols_) {}
const PIVector<T> * p_;
size_t st_, sz_;
const size_t st_, sz_;
public:
//! \~english Copy constructor from modifiable Row to read-only RowConst.
//! \~russian Конструктор копирования из модифицируемого класса Row в константный RowConst.
//! \~\sa Row
inline RowConst(const PIVector2D<T>::Row & r): p_(r.p_), st_(r.st_), sz_(r.sz_) {}
//! \~english Size of the row (number of columns).
//! \~russian Размер строки (количество столбцов).
inline size_t size() const { return sz_; }
//! \~english Const access to the element at the given column index within the row.
//! \~russian Константный доступ к элементу по заданному индексу столбца в строке.
inline const T & operator[](size_t index) const { return (*p_)[st_ + index]; }
//! \~english Returns a const pointer to the row data starting at an optional offset.
//! \~russian Возвращает константный указатель на данные строки, начиная с опционального смещения.
inline const T * data(size_t index = 0) const { return p_->data(st_ + index); }
//! \~english Converts the row to a \a PIVector.
//! \~russian Преобразует строку в \a PIVector.
inline PIVector<T> toVector() const { return PIVector<T>(p_->data(st_), sz_); }
//! \~english Returns the first index of element `e` in the row, starting from `start`.
//! \~russian Возвращает первый индекс элемента `e` в строке, начиная с позиции `start`.
//! \details
//! \~english See \a PIVector::indexOf() for details on negative start handling.
//! \~russian Подробнее об обработке отрицательного `start` см. \a PIVector::indexOf().
//! \~\sa PIVector::indexOf()
inline ssize_t indexOf(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if ((*p_)[st_ + i] == e) return (ssize_t)i;
}
return -1;
}
//! \~english Returns the last index of element `e` in the row, searching backwards from `start`.
//! \~russian Возвращает последний индекс элемента `e` в строке, выполняя поиск в обратном направлении от `start`.
//! \return Index if found, -1 otherwise.
//! \~\sa PIVector::lastIndexOf()
inline ssize_t lastIndexOf(const T & e, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
if ((*p_)[st_ + i] == e) return i;
}
return -1;
}
//! \~english Returns the first index where the predicate `test` returns true, starting from `start`.
//! \~russian Возвращает первый индекс, для которого предикат `test` возвращает true, начиная с `start`.
//! \~\sa PIVector::indexWhere()
inline ssize_t indexWhere(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if (test((*p_)[st_ + i])) return (ssize_t)i;
}
return -1;
}
//! \~english Returns the last index where the predicate `test` returns true, searching backwards from `start`.
//! \~russian Возвращает последний индекс, для которого предикат `test` возвращает true,
//! выполняя поиск в обратном направлении от `start`.
//! \~\sa PIVector::lastIndexWhere()
inline ssize_t lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
if (test((*p_)[st_ + i])) return i;
}
return -1;
}
//! \~english Applies a function to each element of the row (read-only).
//! \~russian Применяет функцию к каждому элементу строки (только чтение).
//! \details
//! \~english The function can't modify the elements.
//! \~russian Функция не может изменять элементы.
//! \~\sa forEach (modifiable)
inline void forEach(std::function<void(const T &)> func) const {
for (size_t i = 0; i < sz_; ++i) {
func((*p_)[st_ + i]);
}
}
//! \~english Checks if the row contains the element `e`.
//! \~russian Проверяет, содержит ли строка элемент `e`.
//! \~\sa PIVector::contains()
inline bool contains(const T & e, ssize_t start = 0) const { return indexOf(e, start) != -1; }
//! \~english Counts occurrences of `e` in the row.
//! \~russian Подсчитывает количество вхождений `e` в строке.
//! \~\sa PIVector::entries()
inline int entries(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if ((*p_)[st_ + i] == e) ++count;
}
return count;
}
//! \~english Counts elements in the row that pass the `test`.
//! \~russian Подсчитывает элементы в строке, проходящие `test`.
//! \~\sa PIVector::entries(std::function)
inline int entries(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if (test((*p_)[st_ + i])) ++count;
}
return count;
}
//! \~english Tests if any element in the row passes the `test`.
//! \~russian Проверяет, проходит ли какой-либо элемент в строке `test`.
//! \~\sa PIVector::any()
inline bool any(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (test((*p_)[st_ + i])) return true;
}
return false;
}
//! \~english Tests if all elements in the row pass the `test`.
//! \~russian Проверяет, проходят ли все элементы в строке `test`.
//! \~\sa PIVector::every()
inline bool every(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (!test((*p_)[st_ + i])) return false;
}
return true;
}
};
//! \class ColConst
//! \brief
//! \~english Proxy class representing a single read-only column in a \a PIVector2D.
//! \~russian Прокси-класс, представляющий один столбец в \a PIVector2D только для чтения.
//! \details
//! \~english Returned by const \a col(). Provides const access to column elements.
//! \~russian Возвращается константной версией \a col(). Предоставляет константный доступ к элементам столбца.
//! \~\sa Col, RowConst
class ColConst {
friend class PIVector2D<T>;
private:
inline ColConst(const PIVector2D<T> * p, size_t row): p_(&(p->mat)) {
step_ = p->cols_;
row_ = row;
sz_ = p->rows_;
}
protected:
inline ColConst(const PIVector2D<T> * p, size_t col): p_(&(p->mat)), step_(p->cols_), col_(col), sz_(p->rows_) {}
const PIVector<T> * p_;
size_t step_, row_, sz_;
const size_t step_, col_, sz_;
public:
inline size_t size() const { return p_->rows_; }
inline const T & operator[](size_t index) const { return (*p_)[index * step_ + row_]; }
inline const T * data(size_t index = 0) const { return p_->data(index * step_ + row_); }
//! \~english Copy constructor from modifiable Col to read-only ColConst.
//! \~russian Конструктор копирования из модифицируемого класса Col в константный ColConst.
//! \~\sa Col
inline ColConst(const PIVector2D<T>::Col & c): p_(c.p_), step_(c.step_), col_(c.col_), sz_(c.sz_) {}
//! \~english Size of the column (number of rows).
//! \~russian Размер столбца (количество строк).
inline size_t size() const { return sz_; }
//! \~english Const access to the element at the given row index within the column.
//! \~russian Константный доступ к элементу по заданному индексу строки в столбце.
inline const T & operator[](size_t index) const { return (*p_)[index * step_ + col_]; }
//! \~english Returns a const pointer to the column data starting at an optional row offset.
//! \~russian Возвращает константный указатель на данные столбца, начиная с опционального смещения по строкам.
inline const T * data(size_t index = 0) const { return p_->data(index * step_ + col_); }
//! \~english Converts the column to a \a PIVector.
//! \~russian Преобразует столбец в \a PIVector.
inline PIVector<T> toVector() const {
PIVector<T> ret;
ret.reserve(sz_);
for (int i = 0; i < size(); i++)
ret << (*p_)[i * step_ + row_];
for (size_t i = 0; i < size(); i++)
ret << (*p_)[i * step_ + col_];
return ret;
}
//! \~english Returns the first index of element `e` in the row, starting from `start`.
//! \~russian Возвращает первый индекс элемента `e` в строке, начиная с позиции `start`.
//! \details
//! \~english See \a PIVector::indexOf() for details on negative start handling.
//! \~russian Подробнее об обработке отрицательного `start` см. \a PIVector::indexOf().
//! \~\sa PIVector::indexOf()
inline ssize_t indexOf(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if ((*p_)[i * step_ + col_] == e) return (ssize_t)i;
}
return -1;
}
//! \~english Returns the last index of element `e` in the row, searching backwards from `start`.
//! \~russian Возвращает последний индекс элемента `e` в строке, выполняя поиск в обратном направлении от `start`.
//! \~\sa PIVector::lastIndexOf()
inline ssize_t lastIndexOf(const T & e, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
if ((*p_)[i * step_ + col_] == e) return i;
}
return -1;
}
//! \~english Returns the first index where the predicate `test` returns true, starting from `start`.
//! \~russian Возвращает первый индекс, для которого предикат `test` возвращает true, начиная с `start`.
//! \~\sa PIVector::indexWhere()
inline ssize_t indexWhere(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if (test((*p_)[i * step_ + col_])) return (ssize_t)i;
}
return -1;
}
//! \~english Returns the last index where the predicate `test` returns true, searching backwards from `start`.
//! \~russian Возвращает последний индекс, для которого предикат `test` возвращает true,
//! выполняя поиск в обратном направлении от `start`.
//! \~\sa PIVector::lastIndexWhere()
inline ssize_t lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const {
ssize_t from = (start < 0 || (size_t)start >= sz_) ? (ssize_t)sz_ - 1 : start;
for (ssize_t i = from; i >= 0; --i) {
if (test((*p_)[i * step_ + col_])) return i;
}
return -1;
}
//! \~english Applies a function to each element of the column (read-only).
//! \~russian Применяет функцию к каждому элементу столбца (только чтение).
//! \details
//! \~english The function can't modify the elements.
//! \~russian Функция не может изменять элементы.
//! \~\sa forEach (modifiable)
inline void forEach(std::function<void(const T &)> func) const {
for (size_t i = 0; i < sz_; ++i) {
func((*p_)[i * step_ + col_]);
}
}
//! \~english Checks if the column contains the element `e`.
//! \~russian Проверяет, содержит ли столбец элемент `e`.
//! \~\sa PIVector::contains()
inline bool contains(const T & e, ssize_t start = 0) const { return indexOf(e, start) != -1; }
//! \~english Counts occurrences of `e` in the column.
//! \~russian Подсчитывает количество вхождений `e` в столбце.
//! \~\sa PIVector::entries()
inline int entries(const T & e, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if ((*p_)[i * step_ + col_] == e) ++count;
}
return count;
}
//! \~english Counts elements in the column that pass the `test`.
//! \~russian Подсчитывает элементы в столбце, проходящие `test`.
//! \~\sa PIVector::entries(std::function)
inline int entries(std::function<bool(const T & e)> test, ssize_t start = 0) const {
if (start < 0) start = 0;
int count = 0;
for (size_t i = (size_t)start; i < sz_; ++i) {
if (test((*p_)[i * step_ + col_])) ++count;
}
return count;
}
//! \~english Tests if any element in the column passes the `test`.
//! \~russian Проверяет, проходит ли какой-либо элемент в столбце `test`.
//! \~\sa PIVector::any()
inline bool any(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (test((*p_)[i * step_ + col_])) return true;
}
return false;
}
//! \~english Tests if all elements in the column pass the `test`.
//! \~russian Проверяет, проходят ли все элементы в столбце `test`.
//! \~\sa PIVector::every()
inline bool every(std::function<bool(const T & e)> test) const {
for (size_t i = 0; i < sz_; ++i) {
if (!test((*p_)[i * step_ + col_])) return false;
}
return true;
}
};
inline T & element(size_t row, size_t col) { return mat[row * cols_ + col]; }
inline const T & element(size_t row, size_t col) const { return mat[row * cols_ + col]; }
inline const T & at(size_t row, size_t col) const { return mat[row * cols_ + col]; }
inline Row operator[](size_t index) { return Row(this, index); }
inline RowConst operator[](size_t index) const { return RowConst(this, index); }
inline T * data(size_t index = 0) { return mat.data(index); }
inline const T * data(size_t index = 0) const { return mat.data(index); }
//! \class Row
//! \brief
//! \~english Proxy class representing a single row in a \a PIVector2D for modification.
//! \~russian Прокси-класс, представляющий одну строку в \a PIVector2D для модификации.
//! \details
//! \~english Objects of this class are returned by non-const \a operator[] or \a row().
//! They provide array-like access to the elements of a specific row and allow operations such as assignment from another row or a \a
//! PIVector, searching, filling, and iteration.
//! \~russian Объекты этого класса возвращаются неконстантными операторами \a operator[] или методом \a row().
//! Они предоставляют доступ к элементам конкретной строки, подобный массиву, и позволяют выполнять такие операции, как присваивание из
//! другой строки или \a PIVector, поиск, заполнение и итерацию. \sa Col, RowConst
class Row: public RowConst {
friend class PIVector2D<T>;
inline Row row(size_t index) { return Row(this, index); }
inline RowConst row(size_t index) const { return RowConst(this, index); }
inline Col col(size_t index) { return Col(this, index); }
inline ColConst col(size_t index) const { return ColConst(this, index); }
inline PIVector2D<T> & setRow(size_t row, const Row & other) {
const size_t sz = piMin<size_t>(cols_, other.sz_);
mat._copyRaw(mat.data(cols_ * row), other.data(), sz);
private:
inline Row(PIVector2D<T> * p, size_t row): RowConst(p, row), p_(&(p->mat)) {}
PIVector<T> * p_;
public:
using RowConst::operator[];
using RowConst::data;
using RowConst::size;
//! \~english Accesses the element at the given column index within the row.
//! \~russian Доступ к элементу по заданному индексу столбца в строке.
//! \details
//! \~english No bounds checking is performed; use with caution.
//! \~russian Проверка границ не выполняется; используйте с осторожностью.
//! \~\sa PIVector::operator[]
inline T & operator[](size_t index) { return (*p_)[this->st_ + index]; }
//! \~english Returns a pointer to the row data starting at an optional offset.
//! \~russian Возвращает указатель на данные строки, начиная с опционального смещения.
//! \details
//! \~english The pointer can be used for direct memory operations. It remains valid as long as the underlying 2D array is not
//! reallocated.
//! \~russian Указатель можно использовать для прямых операций с памятью. Он остаётся действительным, пока не произойдёт
//! перераспределение памяти внутреннего двумерного массива. \sa PIVector::data()
inline T * data(size_t index = 0) { return p_->data(this->st_ + index); }
//! \~english Assigns the contents of another Row to this row.
//! \~russian Присваивает этой строке содержимое другой строки.
//! \details
//! \~english Only the minimum of the two row sizes is copied; if this row is shorter, excess elements in `other` are ignored.
//! \~russian Копируется только минимум из размеров двух строк; если эта строка короче, лишние элементы из `other` игнорируются.
//! \~\sa PIVector::operator=
inline Row & operator=(const Row & other) {
if (p_ == other.p_ && this->st_ == other.st_) return *this;
const size_t sz = piMin<size_t>(this->sz_, other.sz_);
p_->_copyRaw(p_->data(this->st_), other.data(), sz);
return *this;
}
//! \~english Assigns the contents of a \a PIVector to this row.
//! \~russian Присваивает этой строке содержимое \a PIVector.
//! \details
//! \~english Only the minimum of the row size and vector size is copied.
//! \~russian Копируется только минимум из размера строки и размера вектора.
//! \~\sa PIVector::operator=
inline Row & operator=(const PIVector<T> & other) {
const size_t sz = piMin<size_t>(this->sz_, other.size());
p_->_copyRaw(p_->data(this->st_), other.data(), sz);
return *this;
}
//! \~english Applies a function to each element of the row (modifiable).
//! \~russian Применяет функцию к каждому элементу строки (с возможностью изменения).
//! \param func Function that takes a reference to T.
//! \details
//! \~english The function can modify the elements.
//! \~russian Функция может изменять элементы.
//! \~\sa PIVector::forEach()
inline void forEach(std::function<void(T &)> func) {
for (size_t i = 0; i < this->sz_; ++i) {
func((*p_)[this->st_ + i]);
}
}
//! \~english Fills the row with copies of `value`.
//! \~russian Заполняет строку копиями `value`.
//! \~\sa PIVector::fill()
inline void fill(const T & value) {
for (size_t i = 0; i < this->sz_; ++i) {
(*p_)[this->st_ + i] = value;
}
}
};
//! \class Col
//! \brief
//! \~english Proxy class representing a single column in a \a PIVector2D for modification.
//! \~russian Прокси-класс, представляющий один столбец в \a PIVector2D для модификации.
//! \details
//! \~english Objects of this class are returned by non-const \a col(). They provide column-wise access and operations similar to \a
//! Row.
//! \~russian Объекты этого класса возвращаются неконстантным методом \a col(). Они предоставляют доступ к столбцам и операции,
//! аналогичные \a Row. \sa Row, ColConst
class Col: public ColConst {
friend class PIVector2D<T>;
private:
inline Col(PIVector2D<T> * p, size_t col): ColConst(p, col), p_(&(p->mat)) {}
PIVector<T> * p_;
public:
using ColConst::operator[];
using ColConst::data;
using ColConst::size;
//! \~english Accesses the element at the given row index within the column.
//! \~russian Доступ к элементу по заданному индексу строки в столбце.
//! \return Reference to the element.
inline T & operator[](size_t index) { return (*p_)[index * this->step_ + this->col_]; }
//! \~english Returns a pointer to the column data starting at an optional row offset.
//! \~russian Возвращает указатель на данные столбца, начиная с опционального смещения по строкам.
//! \details
//! \~english Note that column elements are not stored contiguously in memory, so this pointer cannot be used to iterate over the
//! whole column.
//! \~russian Обратите внимание, что элементы столбца не хранятся в памяти непрерывно, поэтому этот указатель нельзя использовать
//! для итерации по всему столбцу.
inline T * data(size_t index = 0) { return p_->data(index * this->step_ + this->col_); }
//! \~english Assigns the contents of another Col to this column.
//! \~russian Присваивает этому столбцу содержимое другого столбца.
inline Col & operator=(const Col & other) {
if (p_ == other.p_ && this->col_ == other.col_) return *this;
const size_t sz = piMin<size_t>(this->sz_, other.sz_);
for (size_t i = 0; i < sz; ++i)
(*p_)[i * this->step_ + this->col_] = other[i];
return *this;
}
//! \~english Assigns the contents of a \a PIVector to this column.
//! \~russian Присваивает этому столбцу содержимое \a PIVector.
inline Col & operator=(const PIVector<T> & other) {
const size_t sz = piMin<size_t>(this->sz_, other.size());
for (size_t i = 0; i < sz; ++i)
(*p_)[i * this->step_ + this->col_] = other[i];
return *this;
}
//! \~english Applies a function to each element of the column (modifiable).
//! \~russian Применяет функцию к каждому элементу столбца (с возможностью изменения).
//! \details
//! \~english The function can modify the elements.
//! \~russian Функция может изменять элементы.
//! \~\sa PIVector::forEach()
inline void forEach(std::function<void(T &)> func) {
for (size_t i = 0; i < this->sz_; ++i) {
func((*p_)[i * this->step_ + this->col_]);
}
}
//! \~english Fills the column with copies of `value`.
//! \~russian Заполняет столбец копиями `value`.
//! \~\sa PIVector::fill()
inline void fill(const T & value) {
for (size_t i = 0; i < this->sz_; ++i) {
(*p_)[i * this->step_ + this->col_] = value;
}
}
};
//! \~english Returns a reference to the element at the given row and column.
//! \~russian Возвращает ссылку на элемент по заданной строке и столбцу.
//! \details
//! \~english No bounds checking is performed.
//! \~russian Проверка границ не выполняется.
//! \~\sa at() (const version), PIVector::operator[]
inline T & element(size_t row, size_t col) { return mat[row * cols_ + col]; }
//! \~english Returns a const reference to the element at the given row and column.
//! \~russian Возвращает константную ссылку на элемент по заданной строке и столбцу.
inline const T & element(size_t row, size_t col) const { return mat[row * cols_ + col]; }
//! \~english Returns a const reference to the element at the given row and column
//! \~russian Возвращает константную ссылку на элемент по заданной строке и столбцу
//! \details
//! \~english No bounds checking is performed.
//! \~russian Проверка границ не выполняется.
inline const T & at(size_t row, size_t col) const { return mat[row * cols_ + col]; }
//! \~english Returns a reference to the element at the given Index.
//! \~russian Возвращает ссылку на элемент по заданному Index.
inline T & operator[](const Index & idx) { return element(idx.row, idx.col); }
//! \~english Returns a const reference to the element at the given Index.
//! \~russian Возвращает константную ссылку на элемент по заданному Index.
inline const T & operator[](const Index & idx) const { return element(idx.row, idx.col); }
//! \~english Returns a reference to the element at the given Index.
//! \~russian Возвращает ссылку на элемент по заданному Index.
inline T & element(const Index & idx) { return element(idx.row, idx.col); }
//! \~english Returns a const reference to the element at the given Index.
//! \~russian Возвращает константную ссылку на элемент по заданному Index.
inline const T & element(const Index & idx) const { return element(idx.row, idx.col); }
//! \~english Returns a const reference to the element at the given Index (bounds-checked only in debug).
//! \~russian Возвращает константную ссылку на элемент по заданному Index (проверка границ только в отладочном режиме).
inline const T & at(const Index & idx) const { return at(idx.row, idx.col); }
//! \~english Returns a proxy object for the row at the given index for modification.
//! \~russian Возвращает прокси-объект для строки по заданному индексу для модификации.
//! \~\sa row(), Col
inline Row operator[](size_t index) { return Row(this, index); }
//! \~english Returns a proxy object for the row at the given index for read-only access.
//! \~russian Возвращает прокси-объект для строки по заданному индексу только для чтения.
inline RowConst operator[](size_t index) const { return RowConst(this, index); }
//! \~english Returns a pointer to the underlying flat data starting at an optional offset.
//! \~russian Возвращает указатель на внутренние плоские данные, начиная с опционального смещения.
//! \~\sa PIVector::data()
inline T * data(size_t index = 0) { return mat.data(index); }
//! \~english Returns a const pointer to the underlying flat data starting at an optional offset.
//! \~russian Возвращает константный указатель на внутренние плоские данные, начиная с опционального смещения.
inline const T * data(size_t index = 0) const { return mat.data(index); }
//! \~english Returns a proxy object for the row at the given index for modification.
//! \~russian Возвращает прокси-объект для строки по заданному индексу для модификации.
//! \~\sa operator[]
inline Row row(size_t index) { return Row(this, index); }
//! \~english Returns a proxy object for the row at the given index for read-only access.
//! \~russian Возвращает прокси-объект для строки по заданному индексу только для чтения.
inline RowConst row(size_t index) const { return RowConst(this, index); }
//! \~english Returns a proxy object for the column at the given index for modification.
//! \~russian Возвращает прокси-объект для столбца по заданному индексу для модификации.
//! \~\sa col() const
inline Col col(size_t index) { return Col(this, index); }
//! \~english Returns a proxy object for the column at the given index for read-only access.
//! \~russian Возвращает прокси-объект для столбца по заданному индексу только для чтения.
inline ColConst col(size_t index) const { return ColConst(this, index); }
//! \~english Replaces a row with the contents of a read-only RowConst object.
//! \~russian Заменяет строку содержимым объекта RowConst только для чтения.
inline PIVector2D<T> & setRow(size_t row, const RowConst & other) {
const size_t sz = piMin<size_t>(cols_, other.sz_);
mat._copyRaw(mat.data(cols_ * row), other.data(), sz);
return *this;
}
//! \~english Replaces a row with the contents of a \a PIVector.
//! \~russian Заменяет строку содержимым \a PIVector.
inline PIVector2D<T> & setRow(size_t row, const PIVector<T> & other) {
const size_t sz = piMin<size_t>(cols_, other.size());
mat._copyRaw(mat.data(cols_ * row), other.data(), sz);
return *this;
}
inline PIVector2D<T> & addRow(const Row & other) {
if (cols_ == 0) cols_ = other.sz_;
const size_t sz = piMin<size_t>(cols_, other.sz_);
const size_t ps = mat.size();
mat.resize(mat.size() + cols_);
mat._copyRaw(mat.data(ps), other.data(), sz);
rows_++;
return *this;
}
//! \~english Appends a new row to the bottom of the array from another Row object.
//! \~russian Добавляет новую строку в конец массива из другого объекта Row.
//! \details
//! \~english If the array was empty, its column count is set to the size of the source row.
//! Otherwise, only `min(cols(), other.size())` elements are copied; the rest of the new row is default-initialized.
//! \~russian Если массив был пуст, количество столбцов устанавливается равным размеру исходной строки.
//! В противном случае копируется только `min(cols(), other.size())` элементов; остальные элементы новой строки инициализируются по
//! умолчанию.
//! \~\sa PIVector::push_back()
inline PIVector2D<T> & addRow(const RowConst & other) {
if (cols_ == 0) cols_ = other.sz_;
const size_t sz = piMin<size_t>(cols_, other.sz_);
const size_t ps = mat.size();
mat.resize(mat.size() + cols_);
mat._copyRaw(mat.data(ps), other.data(), sz);
rows_++;
return *this;
}
inline PIVector2D<T> & addRow(const PIVector<T> & other) {
if (cols_ == 0) cols_ = other.size();
const size_t sz = piMin<size_t>(cols_, other.size());
const size_t ps = mat.size();
mat.resize(mat.size() + cols_);
mat._copyRaw(mat.data(ps), other.data(), sz);
mat.append(other.toVector());
rows_++;
mat.resize(rows_ * cols_);
return *this;
}
inline PIVector2D<T> & resize(size_t rows, size_t cols, const T & f = T()) {
mat.resize(rows * cols_, f);
rows_ = rows;
const int cs = (cols - cols_);
if (cs < 0) {
for (size_t r = 0; r < rows; ++r) {
mat.remove(r * cols + cols, -cs);
//! \~english Appends a new row to the bottom of the array from a \a PIVector.
//! \~russian Добавляет новую строку в конец массива из \a PIVector.
inline PIVector2D<T> & addRow(const PIVector<T> & other) {
if (cols_ == 0) cols_ = other.size();
mat.append(other);
rows_++;
mat.resize(rows_ * cols_);
return *this;
}
//! \~english Appends \a count new empty rows to the bottom of the array, filled with value \a f.
//! \~russian Добавляет \a count новых пустых строк в конец массива, заполненных значением \a f.
//! \details
//! \~english If the array was empty (no columns defined), the column count is set to 1.
//! The new rows are filled with the default value \a f.
//! \~russian Если массив был пуст (количество столбцов не определено), количество столбцов устанавливается равным 1.
//! Новые строки заполняются значением по умолчанию \a f.
//! \~\sa addRow(), appendColumns()
inline PIVector2D<T> & appendRows(size_t count, const T & f = T()) {
if (count == 0) return *this;
if (cols_ == 0) ++cols_;
mat.resize(mat.size() + count * cols_, f);
rows_ += count;
return *this;
}
mat.resize(rows * cols, f);
if (!mat.isEmpty()) {
if (cs > 0) {
//! \~english Appends \a count new empty columns to the end of each row of the array.
//! \~russian Добавляет \a count новых пустых столбцов в конец каждой строки массива.
//! \details
//! \~english If the array was empty (rows not defined), the array becomes a single row with \a count columns.
//! If the array already has rows, new elements are inserted at the end of each existing row.
//! \~russian Если массив был пуст (строки не определены), массив становится одной строкой с \a count столбцов.
//! Если массив уже содержит строки, новые элементы добавляются в конец каждой существующей строки.
//! \~\sa appendRows(), addColumn()
inline PIVector2D<T> & appendColumns(size_t count, const T & f = T()) {
if (count == 0) return *this;
if (rows_ == 0) {
mat.resize(count, f);
rows_ = 1;
cols_ = count;
return *this;
}
const size_t newCols = cols_ + count;
mat.reserve(rows_ * newCols);
for (size_t r = rows_; r > 0; --r) {
mat.insert(r * cols_, f, count);
}
cols_ = newCols;
return *this;
}
//! \~english Deletes `count` rows starting from the specified row index.
//! \~russian Удаляет `count` строк, начиная с указанного индекса строки.
//! \details
//! \~english Removes the specified rows from the array and updates the row count. If all elements are deleted (array becomes empty),
//! both rows and columns are set to 0.
//! \~russian Удаляет указанные строки из массива и обновляет количество строк. Если все элементы удалены (массив становится пустым),
//! количество строк и столбцов устанавливается в 0.
//! \~\sa deleteColumns()
inline PIVector2D<T> & deleteRows(size_t row_start, size_t count) {
if (row_start >= rows_ || count == 0) return *this;
mat.remove(row_start * cols_, cols_ * count);
if (isEmpty()) {
cols_ = 0;
rows_ = 0;
} else {
rows_ -= count;
}
return *this;
}
//! \~english Removes the specified columns from the array and updates the column count.
//! \~russian Удаляет указанные столбцы из массива и обновляет количество столбцов.
//! \details
//! \~english Removes \a count columns starting from \a col_start. If \a col_start is out of range or \a count is 0,
//! the function does nothing. If \a count extends beyond the last column, only available columns are deleted.
//! \~russian Удаляет \a count столбцов начиная с \a col_start. Если \a col_start выходит за границы или \a count равен 0,
//! функция ничего не делает. Если \a count выходит за последний столбец, удаляются только доступные столбцы.
//! \~\sa removeColumn(), deleteRows()
inline PIVector2D<T> & deleteColumns(size_t col_start, size_t count) {
if (col_start >= cols_ || rows_ == 0) return *this;
count = piMin(count, cols_ - col_start);
if (count == 0) return *this;
for (size_t r = 0; r < rows_; ++r) {
for (int i = 0; i < cs; ++i)
mat.insert(r * cols + cols_, mat.take_back());
mat.remove(r * (cols_ - count) + col_start, count);
}
cols_ -= count;
mat.resize(rows_ * cols_);
return *this;
}
//! \~english Appends a new column to the right of the array from a \a ColConst.
//! \~russian Добавляет новую строку в конец массива из \a ColConst.
inline PIVector2D<T> & addColumn(const ColConst & other) {
if (other.size() == 0) return *this;
if (isEmpty()) {
mat.reserve(other.size());
for (size_t r = 0; r < other.size(); ++r) {
mat.append(other[r]);
}
rows_ = mat.size();
cols_ = 1;
return *this;
}
const size_t newCols = cols_ + 1;
mat.reserve(rows_ * newCols);
for (size_t r = rows_; r > 0; --r) {
if (r - 1 < other.size()) {
mat.insert(r * cols_, other[r - 1]);
} else {
mat.insert(r * cols_);
}
}
cols_ = newCols;
return *this;
}
//! \~english Appends a new column to the right of the array from a \a PIVector.
//! \~russian Добавляет новую строку в конец массива из \a PIVector.
inline PIVector2D<T> & addColumn(const PIVector<T> & other) {
if (other.size() == 0) return *this;
if (isEmpty()) {
mat.append(other);
rows_ = mat.size();
cols_ = 1;
return *this;
}
const size_t newCols = cols_ + 1;
mat.reserve(rows_ * newCols);
for (size_t r = rows_; r > 0; --r) {
if (r - 1 < other.size()) {
mat.insert(r * cols_, other[r - 1]);
} else {
mat.insert(r * cols_);
}
}
cols_ = newCols;
return *this;
}
//! \~english Resizes the 2D array to new dimensions.
//! \~russian Изменяет размер двумерного массива.
//! \details
//! \~english If the new dimensions are larger, new elements are appended and filled with copies of `f`.
//! If they are smaller, the array is truncated (excess elements are destroyed). The underlying memory may be reallocated.
//! \~russian Если новые размеры больше текущих, новые элементы добавляются в конец и заполняются копиями `f`.
//! Если новые размеры меньше, массив усекается (лишние элементы уничтожаются). Внутренняя память может быть перераспределена.
//! \code
//! PIVector2D<int> mat(2, 3, 0); // 2x3 matrix filled with 0
//! mat.resize(3, 4, 1); // becomes 3x4, new elements filled with 1
//! \endcode
//! \~\sa PIVector::resize()
inline PIVector2D<T> & resize(size_t rows, size_t cols, const T & f = T()) {
if (rows == rows_ && cols == cols_) return *this;
if (rows_ == 0 || cols_ == 0) {
mat.resize(rows * cols, f);
rows_ = rows;
cols_ = cols;
return *this;
}
if (rows != rows_ && cols == cols_) {
mat.resize(rows * cols_, f);
rows_ = rows;
return *this;
}
if (cols > cols_) {
appendColumns(cols - cols_, f);
}
if (rows > rows_) {
appendRows(rows - rows_, f);
}
if (cols < cols_) {
deleteColumns(cols, cols_ - cols);
}
if (rows < rows_) {
deleteRows(rows, rows_ - rows);
}
return *this;
}
//! \~english Equality operator.
//! \~russian Оператор равенства.
//! \~\sa PIVector::operator==
inline bool operator==(const PIVector2D<T> & t) const {
if (cols_ != t.cols_ || rows_ != t.rows_) return false;
return mat == t.mat;
}
//! \~english Inequality operator.
//! \~russian Оператор неравенства.
inline bool operator!=(const PIVector2D<T> & t) const { return !(*this == t); }
//! \~english Converts the 2D array to a vector of vectors (PIVector<PIVector<T>>).
//! \~russian Преобразует двумерный массив в вектор векторов (PIVector<PIVector<T>>).
//! \details
//! \~english Each row vector is a copy of the corresponding row.
//! \~russian Каждый вектор-строка является копией соответствующей строки.
//! \~\sa fromVectors(), PIVector::PIVector(const T*, size_t)
inline PIVector<PIVector<T>> toVectors() const {
PIVector<PIVector<T>> ret;
ret.reserve(rows_);
@@ -284,18 +1012,31 @@ public:
return ret;
}
//! \~english Returns a const reference to the underlying flat \a PIVector.
//! \~russian Возвращает константную ссылку на внутренний плоский \a PIVector.
inline const PIVector<T> & asPlainVector() const { return mat; }
//! \~english Returns a reference to the underlying flat \a PIVector.
//! \~russian Возвращает ссылку на внутренний плоский \a PIVector.
inline PIVector<T> & asPlainVector() { return mat; }
//! \~english Returns a copy of the underlying flat \a PIVector.
//! \~russian Возвращает копию внутреннего плоского \a PIVector.
inline PIVector<T> toPlainVector() const { return mat; }
inline PIVector<T> & plainVector() { return mat; }
inline const PIVector<T> & plainVector() const { return mat; }
//! \~english Swaps this 2D array with another.
//! \~russian Меняет местами этот двумерный массив с другим.
//! \details
//! \~english Swaps the flat vectors and the dimension members. Very fast, no memory allocation.
//! \~russian Обменивает внутренние плоские векторы и члены, хранящие размеры. Очень быстро, без выделения памяти.
//! \~\sa PIVector::swap()
inline void swap(PIVector2D<T> & other) {
mat.swap(other.mat);
piSwap<size_t>(rows_, other.rows_);
piSwap<size_t>(cols_, other.cols_);
}
//! \internal
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0>
inline PIVector2D<T> & _resizeRaw(size_t r, size_t c) {
rows_ = r;
@@ -304,29 +1045,348 @@ public:
return *this;
}
//! \~english Clears the array, removing all elements and setting dimensions to 0.
//! \~russian Очищает массив, удаляя все элементы и устанавливая размеры в 0.
//! \details
//! \~english The capacity of the underlying flat vector may remain unchanged.
//! \~russian Ёмкость внутреннего плоского вектора может остаться неизменной.
//! \~\sa PIVector::clear()
inline void clear() {
rows_ = cols_ = 0;
mat.clear();
}
template<typename ST>
inline PIVector2D<ST> map(std::function<ST(const T & e)> f) const {
return PIVector2D<ST>(rows_, cols_, mat.map(f));
//! \~english Checks if the underlying flat vector contains the element `e`.
//! \~russian Проверяет, содержит ли внутренний плоский вектор элемент `e`.
//! \~\sa PIVector::contains()
inline bool contains(const T & e) const { return mat.contains(e); }
//! \~english Counts occurrences of `e` in the underlying flat vector.
//! \~russian Подсчитывает количество вхождений `e` во внутреннем плоском векторе.
//! \~\sa PIVector::entries()
inline int entries(const T & e) const { return mat.entries(e); }
//! \~english Counts elements in the flat vector that pass the `test`.
//! \~russian Подсчитывает элементы в плоском векторе, проходящие `test`.
//! \~\sa PIVector::entries(std::function)
inline int entries(std::function<bool(const T & e)> test) const { return mat.entries(test); }
//! \~english Returns the first index (row, col) of `e` in the 2D array.
//! \~russian Возвращает первый индекс (строка, столбец) элемента `e` в двумерном массиве.
//! \~\sa PIVector::indexOf()
inline Index indexOf(const T & e) const {
ssize_t flat = mat.indexOf(e);
if (flat < 0 || cols_ == 0) return Index{-1, -1};
return Index{flat / static_cast<ssize_t>(cols_), flat % static_cast<ssize_t>(cols_)};
}
inline void forEach(std::function<void(const T &)> f) const { mat.forEach(f); }
//! \~english Returns the first index (row, col) in the 2D array that passes the `test`.
//! \~russian Возвращает первый индекс (строка, столбец) в двумерном массиве, проходящий `test`.
//! \~\sa PIVector::indexWhere()
inline Index indexWhere(std::function<bool(const T & e)> test, ssize_t start = 0) const {
ssize_t flat = mat.indexWhere(test, start);
if (flat < 0 || cols_ == 0) return Index{-1, -1};
return Index{flat / static_cast<ssize_t>(cols_), flat % static_cast<ssize_t>(cols_)};
}
inline PIVector2D<T> & forEach(std::function<void(T &)> f) {
mat.forEach(f);
//! \~english Returns the last index (row, col) of `e` in the 2D array.
//! \~russian Возвращает последний индекс (строка, столбец) элемента `e` в двумерном массиве.
//! \~\sa PIVector::lastIndexOf()
inline Index lastIndexOf(const T & e, ssize_t start = -1) const {
ssize_t flat = mat.lastIndexOf(e, start);
if (flat < 0 || cols_ == 0) return Index{-1, -1};
return Index{flat / static_cast<ssize_t>(cols_), flat % static_cast<ssize_t>(cols_)};
}
//! \~english Returns the last index (row, col) in the 2D array that passes the `test`.
//! \~russian Возвращает последний индекс (строка, столбец) в двумерном массиве, проходящий `test`.
//! \~\sa PIVector::lastIndexWhere()
inline Index lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const {
ssize_t flat = mat.lastIndexWhere(test, start);
if (flat < 0 || cols_ == 0) return Index{-1, -1};
return Index{flat / static_cast<ssize_t>(cols_), flat % static_cast<ssize_t>(cols_)};
}
//! \~english Tests if any element in the flat vector passes the `test`.
//! \~russian Проверяет, проходит ли какой-либо элемент в плоском векторе `test`.
//! \~\sa PIVector::any()
inline bool any(std::function<bool(const T & e)> test) const { return mat.any(test); }
//! \~english Tests if all elements in the flat vector pass the `test`.
//! \~russian Проверяет, проходят ли все элементы в плоском векторе `test`.
//! \~\sa PIVector::every()
inline bool every(std::function<bool(const T & e)> test) const { return mat.every(test); }
//! \~english Fills the entire 2D array with copies of `e`.
//! \~russian Заполняет весь двумерный массив копиями `e`.
//! \~\sa PIVector::fill()
inline PIVector2D<T> & fill(const T & e = T()) {
mat.fill(e);
return *this;
}
//! \~english Fills the entire 2D array using a generator function `f` based on flat index.
//! \~russian Заполняет весь двумерный массив, используя функцию-генератор `f` на основе плоского индекса.
//! \~\sa PIVector::fill(std::function)
inline PIVector2D<T> & fill(std::function<T(size_t i)> f) {
mat.fill(f);
return *this;
}
//! \~english Same as \a fill().
//! \~russian То же, что и \a fill().
inline PIVector2D<T> & assign(const T & e = T()) { return fill(e); }
//! \~english Assigns new size and fills with value.
//! \~russian Задаёт новый размер и заполняет значением.
//! \~\sa PIVector::assign(size_t, const T&)
inline PIVector2D<T> & assign(size_t rows, size_t cols, const T & f = T()) {
mat.assign(rows * cols, f);
rows_ = rows;
cols_ = cols;
return *this;
}
//! \~english Returns a transposed 2D array (rows become columns and vice versa).
//! \~russian Возвращает транспонированный двумерный массив (строки становятся столбцами и наоборот).
//! \details
//! \~english The element at (r, c) in the original becomes at (c, r) in the result.
//! \~russian Элемент (r, c) исходного массива становится элементом (c, r) в результате.
//! \code
//! PIVector2D<int> mat(2, 3, ...);
//! auto t = mat.transposed(); // now 3x2
//! \endcode
inline PIVector2D<T> transposed() const {
if (isEmpty()) return PIVector2D<T>();
PIVector2D<T> result(cols_, rows_);
for (size_t r = 0; r < rows_; ++r) {
for (size_t c = 0; c < cols_; ++c) {
result.element(c, r) = element(r, c);
}
}
return result;
}
//! \~english Reverses the order of rows in place.
//! \~russian Изменяет порядок строк на обратный на месте.
//! \~\sa reverseColumns(), PIVector::reverse()
inline PIVector2D<T> & reverseRows() {
const size_t half = rows_ / 2;
for (size_t i = 0; i < half; ++i) {
T * row1 = data(i * cols_);
T * row2 = data((rows_ - 1 - i) * cols_);
for (size_t j = 0; j < cols_; ++j) {
piSwap(row1[j], row2[j]);
}
}
return *this;
}
//! \~english Reverses the order of columns in each row in place.
//! \~russian Изменяет порядок столбцов в каждой строке на обратный на месте.
//! \~\sa reverseRows(), PIVector::reverse()
inline PIVector2D<T> & reverseColumns() {
for (size_t r = 0; r < rows_; ++r) {
Row currentRow = row(r);
const size_t half = cols_ / 2;
for (size_t c = 0; c < half; ++c) {
piSwap<T>(currentRow[c], currentRow[cols_ - 1 - c]);
}
}
return *this;
}
//! \~english Returns a sub-2D array (a range of rows and columns).
//! \~russian Возвращает подмассив (диапазон строк и столбцов).
//! \details
//! \~english If the range exceeds the array boundaries, it is clipped. If rowCount or colCount is 0, an empty array is returned.
//! \~russian Если диапазон выходит за границы массива, он обрезается. Если rowCount или colCount равны 0, возвращается пустой массив.
//! \~\sa PIVector::getRange()
inline PIVector2D<T> getRange(size_t rowStart, size_t rowCount, size_t colStart, size_t colCount) const {
if (rowStart >= rows_ || colStart >= cols_ || rowCount == 0 || colCount == 0) return PIVector2D<T>();
const size_t actualRowCount = piMin<size_t>(rowCount, rows_ - rowStart);
const size_t actualColCount = piMin<size_t>(colCount, cols_ - colStart);
PIVector2D<T> result(actualRowCount, actualColCount);
for (size_t r = 0; r < actualRowCount; ++r) {
for (size_t c = 0; c < actualColCount; ++c) {
result.element(r, c) = element(rowStart + r, colStart + c);
}
}
return result;
}
//! \~english Applies a function to each element and returns a new 2D array of a different type.
//! \~russian Применяет функцию к каждому элементу и возвращает новый двумерный массив другого типа.
//! \details
//! \~english The original array is not modified.
//! \~russian Исходный массив не изменяется.
//! \~\sa PIVector::map()
template<typename ST>
inline PIVector2D<ST> map(std::function<ST(const T & e)> f) const {
return PIVector2D<ST>(rows_, cols_, mat.template map<ST>(f));
}
//! \~english Applies a function (with row and col indices) to each element and returns a new 2D array.
//! \~russian Применяет функцию (с индексами строки и столбца) к каждому элементу и возвращает новый двумерный массив.
//! \~\sa PIVector::mapIndexed()
template<typename ST>
inline PIVector2D<ST> mapIndexed(std::function<ST(size_t row, size_t col, const T & e)> f) const {
PIVector<ST> mappedMat;
mappedMat.reserve(size());
for (size_t r = 0; r < rows_; ++r) {
for (size_t c = 0; c < cols_; ++c) {
mappedMat << f(r, c, element(r, c));
}
}
return PIVector2D<ST>(rows_, cols_, std::move(mappedMat));
}
//! \~english Applies a function to each row (modifiable).
//! \~russian Применяет функцию к каждой строке (с возможностью изменения).
//! \~\sa forEachRow() const, PIVector::forEach()
inline PIVector2D<T> & forEachRow(std::function<void(Row)> f) {
for (size_t r = 0; r < rows_; ++r)
f(row(r));
return *this;
}
//! \~english Applies a function to each row (read-only).
//! \~russian Применяет функцию к каждой строке (только чтение).
inline void forEachRow(std::function<void(RowConst)> f) const {
for (size_t r = 0; r < rows_; ++r)
f(row(r));
}
//! \~english Applies a function to each column (modifiable).
//! \~russian Применяет функцию к каждому столбцу (с возможностью изменения).
inline PIVector2D<T> & forEachColumn(std::function<void(Col)> f) {
for (size_t c = 0; c < cols_; ++c)
f(col(c));
return *this;
}
//! \~english Applies a function to each column (read-only).
//! \~russian Применяет функцию к каждому столбцу (только чтение).
//! \param f Function taking a \a ColConst.
inline void forEachColumn(std::function<void(ColConst)> f) const {
for (size_t c = 0; c < cols_; ++c)
f(col(c));
}
//! \~english Accumulates a value across all elements.
//! \~russian Аккумулирует значение по всем элементам.
//! \~\sa PIVector::reduce()
template<typename ST>
inline ST reduce(std::function<ST(const T & e, const ST & acc)> f, const ST & initial = ST()) const {
return mat.template reduce<ST>(f, initial);
}
//! \~english Accumulates a value across all elements with indices.
//! \~russian Аккумулирует значение по всем элементам с индексами.
//! \~\sa PIVector::reduceIndexed()
template<typename ST>
inline ST reduceIndexed(std::function<ST(size_t row, size_t col, const T & e, const ST & acc)> f, const ST & initial = ST()) const {
ST ret(initial);
for (size_t r = 0; r < rows_; ++r) {
for (size_t c = 0; c < cols_; ++c) {
ret = f(r, c, element(r, c), ret);
}
}
return ret;
}
//! \~english Removes a row from the 2D array.
//! \~russian Удаляет строку из двумерного массива.
//! \details
//! \~english If the last row is removed and the array becomes empty, \a cols() is set to 0.
//! \~russian Если удаляется последняя строка и массив становится пустым, \a cols() устанавливается в 0.
//! \~\sa removeColumn(), PIVector::remove()
inline PIVector2D<T> & removeRow(size_t row) { return deleteRows(row, 1); }
//! \~english Removes a column from the 2D array.
//! \~russian Удаляет столбец из двумерного массива.
//! \details
//! \~english This operation is more expensive than removing a row because elements must be moved.
//! \~russian Эта операция дороже, чем удаление строки, поскольку требуется перемещение элементов.
//! \~\sa removeRow(), PIVector::remove()
inline PIVector2D<T> & removeColumn(size_t col) { return deleteColumns(col, 1); }
//! \~english Removes all rows that satisfy a condition.
//! \~russian Удаляет все строки, удовлетворяющие условию.
//! \details
//! \~english Rows are removed from the bottom to avoid index shifting issues.
//! \~russian Строки удаляются снизу вверх, чтобы избежать проблем со смещением индексов.
//! \~\sa removeColumnsWhere(), PIVector::removeWhere()
inline PIVector2D<T> & removeRowsWhere(std::function<bool(const RowConst &)> test) {
ssize_t r = rows_;
while (--r >= 0) {
if (test(RowConst(this, r))) {
removeRow(r);
}
}
return *this;
}
//! \~english Removes all columns that satisfy a condition.
//! \~russian Удаляет все столбцы, удовлетворяющие условию.
//! \~\sa removeRowsWhere()
inline PIVector2D<T> & removeColumnsWhere(std::function<bool(const ColConst &)> test) {
ssize_t c = cols_;
while (--c >= 0) {
if (test(ColConst(this, c))) {
removeColumn(c);
}
}
return *this;
}
//! \~english Returns a new 2D array containing only the rows that pass the test.
//! \~russian Возвращает новый двумерный массив, содержащий только строки, прошедшие проверку.
//! \~\sa filterColumns(), PIVector::filter()
inline PIVector2D<T> filterRows(std::function<bool(const RowConst &)> test) const {
PIVector2D<T> result;
for (size_t r = 0; r < rows_; ++r) {
RowConst currentRow = row(r);
if (test(currentRow)) {
result.addRow(currentRow);
}
}
return result;
}
//! \~english Returns a new 2D array containing only the columns that pass the test.
//! \~russian Возвращает новый двумерный массив, содержащий только столбцы, прошедшие проверку.
//! \~\sa filterRows()
inline PIVector2D<T> filterColumns(std::function<bool(const ColConst &)> test) const {
if (isEmpty()) return PIVector2D<T>();
PIVector<size_t> goodCols;
for (size_t c = 0; c < cols_; ++c) {
if (test(col(c))) {
goodCols << c;
}
}
PIVector2D<T> result;
for (size_t gc = 0; gc < goodCols.size(); ++gc) {
result.addColumn(col(goodCols[gc]));
}
return result;
}
protected:
size_t rows_, cols_;
PIVector<T> mat;
};
//! \relatesalso PICout
//! \~english Output operator for \a PIVector2D to \a PICout.
//! \~russian Оператор вывода \a PIVector2D в \a PICout.
template<typename T>
inline PICout operator<<(PICout s, const PIVector2D<T> & v) {
s.saveAndSetControls(0);
@@ -346,5 +1406,6 @@ inline PICout operator<<(PICout s, const PIVector2D<T> & v) {
return s;
}
//! \}
#endif // PIVECTOR2D_H

View File

@@ -245,6 +245,9 @@ inline constexpr T piAbs(const T & v) {
}
//! \~\brief
//! \~english Templated function return minimum of two values
//! \~russian Шаблонный метод, возвращающий минимум из двух значений
template<typename T>
constexpr T piMin(const T & f, const T & s) {
return ((f > s) ? s : f);
@@ -282,6 +285,9 @@ constexpr T piMin(const T & f, const T & s, const Args &... args) {
}
//! \~\brief
//! \~english Templated function return maximum of two values
//! \~russian Шаблонный метод, возвращающий максимум из двух значений
template<typename T>
constexpr T piMax(const T & f, const T & s) {
return ((f < s) ? s : f);

View File

@@ -100,14 +100,14 @@
#endif
//! \ingroup Core
//! \~\brief
//! \~english Helper to collect and retrieve classes to groups.
//! \~russian Помощник для создания и получения классов в группы.
/// \~english Helper to collect and retrieve classes to groups.
/// \~russian Помощник для создания и получения классов в группы.
class PIP_EXPORT PICollection {
friend class __PICollectionInitializer;
public:
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
PICollection() { ; }
//! \~english Returns all existing groups by their names
@@ -118,10 +118,14 @@ public:
//! \~russian Возвращает все элементы группы "group"
static PIVector<const PIObject *> groupElements(const PIString & group);
//! \~english Add element "element" to group "group"
//! \~russian Добавляет элемент "element" в группу "group"
static bool addToGroup(const PIString & group, const PIObject * element);
class PIP_EXPORT CollectionAdder {
public:
//! \~english Constructor that adds element to group
//! \~russian Конструктор, который добавляет элемент в группу
CollectionAdder(const PIString & group, const PIObject * element, const PIString & name = PIString(), bool own = false);
};

View File

@@ -64,7 +64,16 @@ PIP_EXPORT PIString errorString();
//! \~russian Сброс последней ошибки
PIP_EXPORT void errorClear();
//! \ingroup Core
//! \brief
//! \~english Initialize random number generator
//! \~russian Инициализация генератора случайных чисел
PIP_EXPORT void randomize();
//! \ingroup Core
//! \brief
//! \~english Returns random integer value
//! \~russian Возвращает случайное целое число
PIP_EXPORT int randomi();
//! \ingroup Core

View File

@@ -1,10 +1,13 @@
/*! \file piinit.h
* \ingroup Core
* \~\brief
* \~english Library initialization
* \~russian Инициализация библиотеки
*/
/*
//! \addtogroup Core
//! \{
//! \file piinit.h
//! \brief
//! \~english Library initialization
//! \~russian Инициализация библиотеки
//! \details
//! \~english This file provides initialization and build information for the PIP library.
//! \~russian Этот файл предоставляет инициализацию и информацию о сборке для библиотеки PIP.
/*!
PIP - Platform Independent Primitives
Initialization
Ivan Pelipenko peri4ko@yandex.ru
@@ -37,6 +40,8 @@ class PIFile;
class PIStringList;
//! \~english Internal initializer class for automatic library initialization
//! \~russian Внутренний класс инициализации для автоматической инициализации библиотеки
class PIP_EXPORT __PIInit_Initializer__ {
public:
__PIInit_Initializer__();
@@ -48,6 +53,8 @@ public:
static __PIInit_Initializer__ __piinit_initializer__;
//! \~english Provides library initialization and build information
//! \~russian Предоставляет инициализацию библиотеки и информацию о сборке
class PIP_EXPORT PIInit {
friend class __PIInit_Initializer__;
friend class PIFile;
@@ -69,6 +76,8 @@ public:
boCloud /*! \~english PICloud transport support \~russian Поддержка облачного транспорта PICloud */ = 0x200,
boConsole /*! \~english Console graphics support \~russian Поддержка графики в консоли */ = 0x400,
};
//! \~english Returns singleton instance of PIInit
//! \~russian Возвращает синглтон экземпляр PIInit
static PIInit * instance() { return __PIInit_Initializer__::__instance__; }
//! \ingroup Core
@@ -89,4 +98,5 @@ private:
#endif // MICRO_PIP
//! \}
#endif // PIINIT_H

View File

@@ -45,11 +45,15 @@ public:
s = size_;
}
//! \~english Copy constructor
//! \~russian Конструктор копирования
PIMemoryBlock(const PIMemoryBlock & o) {
d = o.d;
s = o.s;
}
//! \~english Assignment operator
//! \~russian Оператор присваивания
PIMemoryBlock & operator=(const PIMemoryBlock & o) {
d = o.d;
s = o.s;

View File

@@ -1,9 +1,15 @@
/*! \file piobject.h
* \ingroup Core
* \~\brief
* \~english Base object
* \~russian Базовый класс
*/
//! \addtogroup Core
//! \{
//! \file piobject.h
//! \brief
//! \~english Base object class providing event -> handler mechanism
//! \~russian Базовый класс объектов, обеспечивающий механизм событий -> обработчиков
//! \details
//! \~english PIObject is the base class for all PIP classes that need event-driven communication.
//! It provides signal-slot mechanism, property system, and object lifetime management.
//! \~russian PIObject является базовым классом для всех классов PIP, которым необходима событийная коммуникация.
//! Он обеспечивает механизм сигналов-слотов, систему свойств и управление жизненным циклом объектов.
//! \~
/*
PIP - Platform Independent Primitives
Object, base class of some PIP classes, provide EVENT -> EVENT_HANDLER mechanism
@@ -34,10 +40,8 @@
#include "pivariant.h"
#include "pivariantsimple.h"
//! \ingroup Core
//! \~\brief
//! \~english This is base class for any classes which use events -> handlers mechanism.
//! \~russian Этот класс является базовым для использования механизма события -> обработчики.
//! \~english Base class for classes using event -> handler mechanism
//! \~russian Базовый класс для классов, использующих механизм событий -> обработчиков
class PIP_EXPORT PIObject {
#ifndef MICRO_PIP
friend class PIObjectManager;
@@ -50,7 +54,7 @@ class PIP_EXPORT PIObject {
public:
NO_COPY_CLASS(PIObject);
//! \~english Contructs %PIObject with name "name"
//! \~english Constructs %PIObject with name "name"
//! \~russian Создает %PIObject с именем "name"
explicit PIObject(const PIString & name = PIString());
@@ -93,7 +97,7 @@ public:
int args_count;
public:
//! \~english Contructs invalid %Connection
//! \~english Constructs invalid %Connection
//! \~russian Создает недействительный %Connection
Connection();
@@ -175,17 +179,31 @@ public:
bool isPropertyExists(const char * name) const { return properties_.contains(piHashData((const uchar *)name, strlen(name))); }
void setThreadSafe(bool yes) { thread_safe_ = yes; }
//! \~english Returns if object is thread safe
//! \~russian Возвращает является ли объект потокобезопасным
bool isThreadSafe() const { return thread_safe_; }
//! \~english Executes method with specified arguments
//! \~russian Выполняет метод с указанными аргументами
bool execute(const PIString & method, const PIVector<PIVariantSimple> & vl);
//! \~english Executes method without arguments
//! \~russian Выполняет метод без аргументов
bool execute(const PIString & method) { return execute(method, PIVector<PIVariantSimple>()); }
//! \~english Executes method with one argument
//! \~russian Выполняет метод с одним аргументом
bool execute(const PIString & method, const PIVariantSimple & v0) { return execute(method, PIVector<PIVariantSimple>() << v0); }
//! \~english Executes method with two arguments
//! \~russian Выполняет метод с двумя аргументами
bool execute(const PIString & method, const PIVariantSimple & v0, const PIVariantSimple & v1) {
return execute(method, PIVector<PIVariantSimple>() << v0 << v1);
}
//! \~english Executes method with three arguments
//! \~russian Выполняет метод с тремя аргументами
bool execute(const PIString & method, const PIVariantSimple & v0, const PIVariantSimple & v1, const PIVariantSimple & v2) {
return execute(method, PIVector<PIVariantSimple>() << v0 << v1 << v2);
}
//! \~english Executes method with four arguments
//! \~russian Выполняет метод с четырьмя аргументами
bool execute(const PIString & method,
const PIVariantSimple & v0,
const PIVariantSimple & v1,
@@ -194,16 +212,26 @@ public:
return execute(method, PIVector<PIVariantSimple>() << v0 << v1 << v2 << v3);
}
//! \~english Executes method queued with performer
//! \~russian Выполняет метод в очереди с исполнителем
bool executeQueued(PIObject * performer, const PIString & method, const PIVector<PIVariantSimple> & vl);
//! \~english Executes method without arguments queued with performer
//! \~russian Выполняет метод без аргументов в очереди с исполнителем
bool executeQueued(PIObject * performer, const PIString & method) {
return executeQueued(performer, method, PIVector<PIVariantSimple>());
}
//! \~english Executes method with one argument queued with performer
//! \~russian Выполняет метод с одним аргументом в очереди с исполнителем
bool executeQueued(PIObject * performer, const PIString & method, const PIVariantSimple & v0) {
return executeQueued(performer, method, PIVector<PIVariantSimple>() << v0);
}
//! \~english Executes method with two arguments queued with performer
//! \~russian Выполняет метод с двумя аргументами в очереди с исполнителем
bool executeQueued(PIObject * performer, const PIString & method, const PIVariantSimple & v0, const PIVariantSimple & v1) {
return executeQueued(performer, method, PIVector<PIVariantSimple>() << v0 << v1);
}
//! \~english Executes method with three arguments queued with performer
//! \~russian Выполняет метод с тремя аргументами в очереди с исполнителем
bool executeQueued(PIObject * performer,
const PIString & method,
const PIVariantSimple & v0,
@@ -211,6 +239,8 @@ public:
const PIVariantSimple & v2) {
return executeQueued(performer, method, PIVector<PIVariantSimple>() << v0 << v1 << v2);
}
//! \~english Executes method with four arguments queued with performer
//! \~russian Выполняет метод с четырьмя аргументами в очереди с исполнителем
bool executeQueued(PIObject * performer,
const PIString & method,
const PIVariantSimple & v0,
@@ -220,18 +250,30 @@ public:
return executeQueued(performer, method, PIVector<PIVariantSimple>() << v0 << v1 << v2 << v3);
}
//! \~english Executes method on object with specified arguments
//! \~russian Выполняет метод на объекте с указанными аргументами
static bool execute(PIObject * o, const PIString & method, const PIVector<PIVariantSimple> & vl) { return o->execute(method, vl); }
//! \~english Executes method on object without arguments
//! \~russian Выполняет метод на объекте без аргументов
static bool execute(PIObject * o, const PIString & method) { return execute(o, method, PIVector<PIVariantSimple>()); }
//! \~english Executes method on object with one argument
//! \~russian Выполняет метод на объекте с одним аргументом
static bool execute(PIObject * o, const PIString & method, const PIVariantSimple & v0) {
return execute(o, method, PIVector<PIVariantSimple>() << v0);
}
//! \~english Executes method on object with two arguments
//! \~russian Выполняет метод на объекте с двумя аргументами
static bool execute(PIObject * o, const PIString & method, const PIVariantSimple & v0, const PIVariantSimple & v1) {
return execute(o, method, PIVector<PIVariantSimple>() << v0 << v1);
}
//! \~english Executes method on object with three arguments
//! \~russian Выполняет метод на объекте с тремя аргументами
static bool
execute(PIObject * o, const PIString & method, const PIVariantSimple & v0, const PIVariantSimple & v1, const PIVariantSimple & v2) {
return execute(o, method, PIVector<PIVariantSimple>() << v0 << v1 << v2);
}
//! \~english Executes method on object with four arguments
//! \~russian Выполняет метод на объекте с четырьмя аргументами
static bool execute(PIObject * o,
const PIString & method,
const PIVariantSimple & v0,
@@ -241,19 +283,29 @@ public:
return execute(o, method, PIVector<PIVariantSimple>() << v0 << v1 << v2 << v3);
}
//! \~english Executes method on object queued with performer
//! \~russian Выполняет метод на объекте в очереди с исполнителем
static bool executeQueued(PIObject * o, PIObject * performer, const PIString & method, const PIVector<PIVariantSimple> & vl) {
return o->executeQueued(performer, method, vl);
}
//! \~english Executes method on object without arguments queued with performer
//! \~russian Выполняет метод на объекте без аргументов в очереди с исполнителем
static bool executeQueued(PIObject * o, PIObject * performer, const PIString & method) {
return executeQueued(o, performer, method, PIVector<PIVariantSimple>());
}
//! \~english Executes method on object with one argument queued with performer
//! \~russian Выполняет метод на объекте с одним аргументом в очереди с исполнителем
static bool executeQueued(PIObject * o, PIObject * performer, const PIString & method, const PIVariantSimple & v0) {
return executeQueued(o, performer, method, PIVector<PIVariantSimple>() << v0);
}
//! \~english Executes method on object with two arguments queued with performer
//! \~russian Выполняет метод на объекте с двумя аргументами в очереди с исполнителем
static bool
executeQueued(PIObject * o, PIObject * performer, const PIString & method, const PIVariantSimple & v0, const PIVariantSimple & v1) {
return executeQueued(o, performer, method, PIVector<PIVariantSimple>() << v0 << v1);
}
//! \~english Executes method on object with three arguments queued with performer
//! \~russian Выполняет метод на объекте с тремя аргументами в очереди с исполнителем
static bool executeQueued(PIObject * o,
PIObject * performer,
const PIString & method,
@@ -262,6 +314,8 @@ public:
const PIVariantSimple & v2) {
return executeQueued(o, performer, method, PIVector<PIVariantSimple>() << v0 << v1 << v2);
}
//! \~english Executes method on object with four arguments queued with performer
//! \~russian Выполняет метод на объекте с четырьмя аргументами в очереди с исполнителем
static bool executeQueued(PIObject * o,
PIObject * performer,
const PIString & method,
@@ -272,6 +326,8 @@ public:
return executeQueued(o, performer, method, PIVector<PIVariantSimple>() << v0 << v1 << v2 << v3);
}
//! \~english Dumps object information
//! \~russian Выводит информацию об объекте
void dump(const PIString & line_prefix = PIString()) const;
@@ -279,10 +335,20 @@ public:
//! \~russian Возвращает цепочку наследования объекта (вместе с классом самого объекта)
PIStringList scopeList() const;
//! \~english Returns list of all event-handler methods
//! \~russian Возвращает список всех методов-обработчиков событий
PIStringList methodsEH() const;
//! \~english Returns if method "name" is event-handler
//! \~russian Возвращает является ли метод "name" обработчиком событий
bool isMethodEHContains(const PIString & name) const;
//! \~english Returns arguments of event-handler method "name"
//! \~russian Возвращает аргументы метода-обработчика событий "name"
PIString methodEHArguments(const PIString & name) const;
//! \~english Returns full format of event-handler method "name"
//! \~russian Возвращает полный формат метода-обработчика событий "name"
PIString methodEHFullFormat(const PIString & name) const;
//! \~english Returns event-handler method name from address "addr"
//! \~russian Возвращает имя метода-обработчика событий по адресу "addr"
PIString methodEHFromAddr(const void * addr) const;
// / Direct connect
@@ -542,6 +608,8 @@ public:
//! \~english Returns if "o" is valid %PIObject (check signature)
//! \~russian Возвращает действительный ли "o" %PIObject (проверяет подпись)
static bool isPIObject(const PIObject * o);
//! \~english Returns if "o" is valid %PIObject (check signature)
//! \~russian Возвращает действительный ли "o" %PIObject (проверяет подпись)
static bool isPIObject(const void * o) { return isPIObject((PIObject *)o); }
//! \~english Returns if "o" is valid %PIObject subclass "T" (check signature and classname)
@@ -554,6 +622,8 @@ public:
static bool isTypeOf(const void * o) {
return isTypeOf<T>((PIObject *)o);
}
//! \~english Simplifies type name "a" to readable format
//! \~russian Упрощает имя типа "a" в читаемый формат
static PIString simplifyType(const char * a, bool readable = true);
struct PIP_EXPORT __MetaFunc {

View File

@@ -1,8 +1,12 @@
/*! \file piauth.h
* \ingroup Crypt
* \~\brief
* \~english Authentication API
* \~russian API аутентификации
* \addtogroup Crypt
* \{
* \brief Authentication API
* \~english Authentication API for client-server key exchange and authentication
* \~russian API аутентификации для обмена ключами и аутентификации клиент-сервер
* \details
* \~english Provides classes for secure client-server authentication with digital signatures, password verification, and key exchange
* \~russian Предоставляет классы для безопасной клиент-серверной аутентификации с цифровыми подписями, проверкой пароля и обменом ключами
*/
/*
PIP - Platform Independent Primitives
@@ -31,70 +35,106 @@
#include "pip_crypt_export.h"
//! \~english Authentication and key exchange class
//! \~russian Класс аутентификации и обмена ключами
class PIP_CRYPT_EXPORT PIAuth: public PIObject {
PIOBJECT(PIAuth)
public:
//! \~english Authentication state
//! \~russian Состояние аутентификации
enum State {
NotConnected,
AuthProbe,
PassRequest,
AuthReply,
KeyExchange,
Connected
NotConnected, //!< \~english Not connected \~russian Не подключен
AuthProbe, //!< \~english Authentication probe \~russian Зондирование аутентификации
PassRequest, //!< \~english Password request \~russian Запрос пароля
AuthReply, //!< \~english Authentication reply \~russian Ответ аутентификации
KeyExchange, //!< \~english Key exchange \~russian Обмен ключами
Connected //!< \~english Connected, authenticated \~russian Подключено, аутентифицировано
};
//! Create PIAuth with your digital sign
//! \~english Create PIAuth with your digital sign
//! \~russian Создать PIAuth с вашей цифровой подписью
//! \param sign \~english Digital sign private key \~russian Приватный ключ цифровой подписи
PIAuth(const PIByteArray & sign);
//! Set server info data for client authorize event
//! \~english Set server info data for client authorize event
//! \~russian Установить информационные данные сервера для события авторизации клиента
//! \param info \~english Info data \~russian Информационные данные
void setInfoData(const PIByteArray & info) { custom_info = info; }
//! Set server password for check
//! \~english Set server password for check
//! \~russian Установить пароль сервера для проверки
//! \param ps \~english Server password \~russian Пароль сервера
void setServerPassword(const PIString & ps);
//! Set list of trusted clients/servers public digital sign keys
//! \~english Set list of trusted clients/servers public digital sign keys
//! \~russian Установить список доверенных публичных ключей цифровой подписи клиентов/серверов
//! \param pkeys \~english List of public keys \~russian Список публичных ключей
void setAuthorizedPublicKeys(const PIVector<PIByteArray> & pkeys) { auth_pkeys = pkeys; }
//! Get list of trusted clients/servers public digital sign keys
//! \~english Get list of trusted clients/servers public digital sign keys
//! \~russian Получить список доверенных публичных ключей цифровой подписи клиентов/серверов
//! \return \~english List of public keys \~russian Список публичных ключей
PIVector<PIByteArray> getAuthorizedPublicKeys() { return auth_pkeys; }
//! Get your digital sign public key
//! \~english Get your digital sign public key
//! \~russian Получить ваш публичный ключ цифровой подписи
//! \return Public sign key \~english Публичный ключ подписи
PIByteArray getSignPublicKey() { return sign_pk; }
//! Stop authorization
//! \~english Stop authorization
//! \~russian Остановить авторизацию
void stop();
//! Start authorization as client
//! \~english Start authorization as client
//! \~russian Начать авторизацию как клиент
void startClient();
//! Start authorization as server, return first server message for client
//! \~english Start authorization as server, return first server message for client
//! \~russian Начать авторизацию как сервер, вернуть первое сообщение сервера для клиента
//! \return \~english First server message \~russian Первое сообщение сервера
PIByteArray startServer();
//! Process reseived message both for client and server, return current state and new message writed in "ba"
//! \~english Process received message both for client and server, return current state and new message written in "ba"
//! \~russian Обработать полученное сообщение как для клиента, так и для сервера, вернуть текущее состояние и новое сообщение в "ba"
//! \param ba \~english Message to process, output message \~russian Сообщение для обработки, выходное сообщение
//! \return \~english Current authentication state \~russian Текущее состояние аутентификации
State receive(PIByteArray & ba);
//! Get session secret key, return key only when Connected state
//! \~english Get session secret key, return key only when Connected state
//! \~russian Получить секретный ключ сессии, вернуть ключ только в состоянии Connected
//! \return \~english Session secret key \~russian Секретный ключ сессии
//! \note
//! \~english Returns empty array if not connected
//! \~russian Возвращает пустой массив если не подключен
PIByteArray getSecretKey();
//! Generate digital sign from seed
//! \~english Generate digital sign from seed
//! \~russian Сгенерировать цифровую подпись из зерна
//! \param seed \~english Seed for key generation \~russian Зерно для генерации ключа
//! \return \~english Generated digital sign key \~russian Сгенерированный ключ цифровой подписи
static PIByteArray generateSign(const PIByteArray & seed);
//! Disconneted event
//! \~english Disconnected event
//! \~russian Событие отключения
EVENT1(disconnected, PIString, reason);
//! Conneted event
//! \~english Connected event
//! \~russian Событие подключения
EVENT1(connected, PIString, info);
//! Client event for authorize new server
//! \~english Client event for authorize new server
//! \~russian Событие клиента для авторизации нового сервера
EVENT2(authorize, PIByteArray, info, bool *, ok);
//! Client event for input server password
//! \~english Client event for input server password
//! \~russian Событие клиента для ввода пароля сервера
EVENT1(passwordRequest, PIString *, pass);
//! Server event on check client password
//! \~english Server event on check client password
//! \~russian Событие сервера при проверке пароля клиента
EVENT1(passwordCheck, bool, result);
private:

View File

@@ -40,6 +40,9 @@ public:
//! \~russian Конструктор, генерирующий случайный ключ
PICrypt();
//! \~\brief
//! \~english Destructor
//! \~russian Деструктор
~PICrypt();
//! \~\brief

View File

@@ -1,15 +1,16 @@
/*! \file pidigest.h
* \ingroup Core
* \~\brief
* \~english Digest algorithms
* \~russian Алгоритмы хэш-сумм
*
* \~\details
* \~english
* This file implements several common-usage hash algorithms
* \~russian
* Этот файл реализует несколько распространенных алгоритмов хэширования
*/
//! \addtogroup Core
//! \{
//! \file pidigest.h
//! \brief
//! \~english Digest algorithms
//! \~russian Алгоритмы хэш-сумм
//!
//! \details
//! \~english
//! This file implements several common-usage hash algorithms
//! \~russian
//! Этот файл реализует несколько распространенных алгоритмов хэширования
//! \}
/*
PIP - Platform Independent Primitives
Digest algorithms
@@ -37,6 +38,15 @@
class PIP_EXPORT PIDigest {
public:
//! \~english Hash algorithm type
//! \~russian Тип алгоритма хэширования
//! \details
//! \~english
//! Supported algorithms: SHA1, SHA2 family (224, 256, 384, 512, 512_224, 512_256),
//! MD2, MD4, MD5, BLAKE2s, BLAKE2b, SipHash
//! \~russian
//! Поддерживаемые алгоритмы: SHA1, семейство SHA2 (224, 256, 384, 512, 512_224, 512_256),
//! MD2, MD4, MD5, BLAKE2s, BLAKE2b, SipHash
enum class Type {
SHA1,
@@ -70,13 +80,45 @@ public:
Count,
};
//! \~english Get hash output length in bytes
//! \~russian Получить длину хэша в байтах
static int hashLength(Type type);
//! \~english Get hash block length in bytes
//! \~russian Получить длину блока хэширования в байтах
static int blockLength(Type type);
//! \~english Get algorithm name as string
//! \~russian Получить название алгоритма в виде строки
//! \return Algorithm name
//! \sa Type
static PIConstChars typeName(Type type);
//! \~english Calculate hash of message
//! \~russian Вычислить хэш сообщения
//! \param msg Input message
//! \param type Hash algorithm type
//! \return Hash bytes
//! \sa calculateWithKey, HMAC
static PIByteArray calculate(const PIByteArray & msg, Type type);
//! \~english Calculate keyed hash of message
//! \~russian Вычислить ключевой хэш сообщения
//! \param msg Input message
//! \param key Secret key
//! \param type Hash algorithm type (SipHash or BLAKE2)
//! \return Hash bytes
//! \note Only SipHash and BLAKE2 algorithms support keyed hashing
//! \sa calculate, HMAC
static PIByteArray calculateWithKey(const PIByteArray & msg, const PIByteArray & key, Type type);
//! \~english Calculate HMAC (Keyed-Hash Message Authentication Code)
//! \~russian Вычислить HMAC (код аутентификации сообщений на основе ключа)
//! \param msg Input message
//! \param key Secret key
//! \param type Hash algorithm type
//! \return HMAC bytes
//! \note Supports SHA1, SHA2 and MD5 algorithms
//! \sa calculate, calculateWithKey
static PIByteArray HMAC(const PIByteArray & msg, const PIByteArray & key, PIDigest::Type type);
};
//! \}
#endif

View File

@@ -29,21 +29,53 @@
#include "pimathbase.h"
//! \~english Geographical ellipsoid Earth model
//! \~russian Географическая эллипсоидная модель Земли
class PIP_EXPORT PIEllipsoidModel {
public:
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
PIEllipsoidModel();
double eccSquared() const { return eccentricity * eccentricity; } // eccentricity squared
//! \~english Get eccentricity squared
//! \~russian Получить квадрат эксцентриситета
double eccSquared() const { return eccentricity * eccentricity; }
//! \~english Get semi-minor axis (b)
//! \~russian Получить малую полуось (b)
double b() const { return a * sqrt(1 - eccSquared()); }
//! \~english WGS84 ellipsoid model
//! \~russian Эллипсоид WGS84
static PIEllipsoidModel WGS84Ellipsoid();
//! \~english PZ90 ellipsoid model
//! \~russian Эллипсоид ПЗ-90
static PIEllipsoidModel PZ90Ellipsoid();
//! \~english GPS ellipsoid (same as WGS84)
//! \~russian Эллипсоид GPS (то же что WGS84)
static PIEllipsoidModel GPSEllipsoid();
//! \~english Krasovskiy ellipsoid model
//! \~russian Эллипсоид Красовского
static PIEllipsoidModel KrasovskiyEllipsoid();
double a; /// Major axis of Earth in meters
double flattening; /// Flattening (ellipsoid parameter)
double eccentricity; /// Eccentricity (ellipsoid parameter)
double angVelocity; /// Angular velocity of Earth in radians/sec
//! \~english Major semi-axis (meters)
//! \~russian Большая полуось (метры)
double a;
//! \~english Flattening (f = (a-b)/a)
//! \~russian Сплюснутость (f = (a-b)/a)
double flattening;
//! \~english First eccentricity
//! \~russian Первый эксцентриситет
double eccentricity;
//! \~english Angular velocity (rad/sec)
//! \~russian Угловая скорость (рад/сек)
double angVelocity;
};

View File

@@ -29,147 +29,338 @@
#include "piellipsoidmodel.h"
#include "pimathvector.h"
//! \~english Geographical position class
//! \~russian Класс географической позиции
class PIP_EXPORT PIGeoPosition: public PIMathVectorT3d {
public:
//! \~english Coordinate system types
//! \~russian Типы систем координат
enum CoordinateSystem {
Unknown = 0, /// Unknown coordinate system
Geodetic, /// Geodetic latitude, longitude, and height above ellipsoid
Geocentric, /// Geocentric (regular spherical coordinates)
Cartesian, /// Cartesian (Earth-centered, Earth-fixed)
Spherical /// Spherical coordinates (theta,phi,radius)
Unknown = 0, //!< Unknown coordinate system
Geodetic, //!< Geodetic latitude, longitude, and height above ellipsoid
Geocentric, //!< Geocentric (regular spherical coordinates)
Cartesian, //!< Cartesian (Earth-centered, Earth-fixed)
Spherical //!< Spherical coordinates (theta,phi,radius)
};
static const double one_cm_tolerance; /// One centimeter tolerance.
static const double one_mm_tolerance; /// One millimeter tolerance.
static const double one_um_tolerance; /// One micron tolerance.
static double position_tolerance; /// Default tolerance (default 1mm)
//! \~english One centimeter tolerance
//! \~russian Допуск один сантиметр
static const double one_cm_tolerance;
//! \~english One millimeter tolerance
//! \~russian Допуск один миллиметр
static const double one_mm_tolerance;
//! \~english One micron tolerance
//! \~russian Допуск один микрон
static const double one_um_tolerance;
//! \~english Default position tolerance (default 1mm)
//! \~russian Допуск позиции по умолчанию (по умолчанию 1мм)
static double position_tolerance;
//! \~english Set position tolerance
//! \~russian Установить допуск позиции
//! \param tol New tolerance value
//! \return Previous tolerance value
static double setPositionTolerance(const double tol) {
position_tolerance = tol;
return position_tolerance;
}
//! \~english Get position tolerance
//! \~russian Получить допуск позиции
static double getPositionTolerance() { return position_tolerance; }
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
PIGeoPosition();
//! \~english Constructor with coordinates
//! \~russian Конструктор с координатами
//! \param a First coordinate
//! \param b Second coordinate
//! \param c Third coordinate
//! \param s Coordinate system
//! \param ell Ellipsoid model
PIGeoPosition(double a, double b, double c, CoordinateSystem s = Cartesian, PIEllipsoidModel ell = PIEllipsoidModel::WGS84Ellipsoid());
//! \~english Constructor from vector
//! \~russian Конструктор из вектора
//! \param v Vector with coordinates
//! \param s Coordinate system
//! \param ell Ellipsoid model
PIGeoPosition(PIMathVectorT3d v, CoordinateSystem s = Cartesian, PIEllipsoidModel ell = PIEllipsoidModel::WGS84Ellipsoid());
//! \~english Transform to specified coordinate system
//! \~russian Преобразовать в указанную систему координат
//! \param sys Target coordinate system
//! \return Reference to this position
PIGeoPosition & transformTo(CoordinateSystem sys);
//! \~english Convert to geodetic coordinates
//! \~russian Преобразовать в геодезические координаты
PIGeoPosition & asGeodetic() {
transformTo(Geodetic);
return *this;
} /// Convert to geodetic coordinate
}
//! \~english Convert to geodetic coordinates using specified ellipsoid
//! \~russian Преобразовать в геодезические координаты с указанным эллипсоидом
//! \param ell Ellipsoid model to use
//! \return Reference to this position
PIGeoPosition & asGeodetic(const PIEllipsoidModel & ell) {
setEllipsoidModel(ell);
transformTo(Geodetic);
return *this;
} /// Convert to another ell, then to geodetic coordinates
}
//! \~english Convert to ECEF (cartesian) coordinates
//! \~russian Преобразовать в координаты ECEF (декартовы)
PIGeoPosition & asECEF() {
transformTo(Cartesian);
return *this;
} /// Convert to cartesian coordinates
}
//! \~english Get X coordinate (or first coordinate in Cartesian)
//! \~russian Получить координату X (или первую координату в Декартовой)
double x() const;
//! \~english Get Y coordinate (or second coordinate in Cartesian)
//! \~russian Получить координату Y (или вторую координату в Декартовой)
double y() const;
//! \~english Get Z coordinate (or third coordinate in Cartesian)
//! \~russian Получить координату Z (или третью координату в Декартовой)
double z() const;
//! \~english Get geodetic latitude in degrees
//! \~russian Получить геодезическую широту в градусах
double latitudeGeodetic() const;
//! \~english Get geocentric latitude in degrees
//! \~russian Получить геоцентрическую широту в градусах
double latitudeGeocentric() const;
//! \~english Get longitude in degrees
//! \~russian Получить долготу в градусах
double longitude() const;
//! \~english Get theta (angle from Z axis) in degrees
//! \~russian Получить тета (угол от оси Z) в градусах
double theta() const;
//! \~english Get phi (angle in XY plane from X axis) in degrees
//! \~russian Получить фи (угол в плоскости XY от оси X) в градусах
double phi() const;
//! \~english Get radius (distance from Earth center)
//! \~russian Получить радиус (расстояние от центра Земли)
double radius() const;
//! \~english Get height above ellipsoid
//! \~russian Получить высоту над эллипсоидом
double height() const;
/// Set the ellipsoid values for this PIGeoPosition given a ellipsoid.
//! \~english Set ellipsoid model for this position
//! \~russian Установить модель эллипсоида для этой позиции
//! \param ell Ellipsoid model
void setEllipsoidModel(const PIEllipsoidModel & ell) { el = ell; }
/// Set the \a PIGeoPosition given geodetic coordinates in degrees. \a CoordinateSystem is set to \a Geodetic.
//! \~english Set position from geodetic coordinates
//! \~russian Установить позицию из геодезических координат
//! \param lat Latitude in degrees
//! \param lon Longitude in degrees
//! \param ht Height above ellipsoid
//! \param ell Ellipsoid model (default WGS84)
//! \return Reference to this position
PIGeoPosition & setGeodetic(double lat, double lon, double ht, PIEllipsoidModel ell = PIEllipsoidModel::WGS84Ellipsoid());
/// Set the \a PIGeoPosition given geocentric coordinates in degrees. \a CoordinateSystem is set to \a Geocentric
//! \~english Set position from geocentric coordinates
//! \~russian Установить позицию из геоцентрических координат
//! \param lat Latitude in degrees
//! \param lon Longitude in degrees
//! \param rad Radius
//! \return Reference to this position
PIGeoPosition & setGeocentric(double lat, double lon, double rad);
/// Set the \a PIGeoPosition given spherical coordinates in degrees. \a CoordinateSystem is set to \a Spherical
//! \~english Set position from spherical coordinates
//! \~russian Установить позицию из сферических координат
//! \param theta Angle from Z axis in degrees
//! \param phi Angle in XY plane from X axis in degrees
//! \param rad Radius
//! \return Reference to this position
PIGeoPosition & setSpherical(double theta, double phi, double rad);
/// Set the \a PIGeoPosition given ECEF coordinates in meeters. \a CoordinateSystem is set to \a Cartesian.
//! \~english Set position from ECEF coordinates
//! \~russian Установить позицию из координат ECEF
//! \param x X coordinate in meters
//! \param y Y coordinate in meters
//! \param z Z coordinate in meters
//! \return Reference to this position
PIGeoPosition & setECEF(double x, double y, double z);
/// Fundamental conversion from spherical to cartesian coordinates.
//! \~english Convert spherical to Cartesian coordinates
//! \~russian Преобразовать сферические в декартовы координаты
//! \param tpr Input spherical (theta, phi, radius)
//! \param xyz Output Cartesian (x, y, z)
static void convertSphericalToCartesian(const PIMathVectorT3d & tpr, PIMathVectorT3d & xyz);
/// Fundamental routine to convert cartesian to spherical coordinates.
//! \~english Convert Cartesian to spherical coordinates
//! \~russian Преобразовать декартовы в сферические координаты
//! \param xyz Input Cartesian (x, y, z)
//! \param tpr Output spherical (theta, phi, radius)
static void convertCartesianToSpherical(const PIMathVectorT3d & xyz, PIMathVectorT3d & tpr);
/// Fundamental routine to convert ECEF (cartesian) to geodetic coordinates,
//! \~english Convert Cartesian (ECEF) to geodetic coordinates
//! \~russian Преобразовать декартовы (ECEF) в геодезические координаты
//! \param xyz Input Cartesian (x, y, z)
//! \param llh Output geodetic (latitude, longitude, height)
//! \param ell Ellipsoid model (default WGS84)
static void convertCartesianToGeodetic(const PIMathVectorT3d & xyz,
PIMathVectorT3d & llh,
PIEllipsoidModel ell = PIEllipsoidModel::WGS84Ellipsoid());
/// Fundamental routine to convert geodetic to ECEF (cartesian) coordinates,
//! \~english Convert geodetic to Cartesian (ECEF) coordinates
//! \~russian Преобразовать геодезические в декартовы (ECEF) координаты
//! \param llh Input geodetic (latitude, longitude, height)
//! \param xyz Output Cartesian (x, y, z)
//! \param ell Ellipsoid model (default WGS84)
static void convertGeodeticToCartesian(const PIMathVectorT3d & llh,
PIMathVectorT3d & xyz,
PIEllipsoidModel ell = PIEllipsoidModel::WGS84Ellipsoid());
/// Fundamental routine to convert cartesian (ECEF) to geocentric
//! \~english Convert Cartesian (ECEF) to geocentric coordinates
//! \~russian Преобразовать декартовы (ECEF) в геоцентрические координаты
//! \param xyz Input Cartesian (x, y, z)
//! \param llr Output geocentric (latitude, longitude, radius)
static void convertCartesianToGeocentric(const PIMathVectorT3d & xyz, PIMathVectorT3d & llr);
/// Fundamental routine to convert geocentric to cartesian (ECEF)
//! \~english Convert geocentric to Cartesian (ECEF)
//! \~russian Преобразовать геоцентрические в декартовы (ECEF)
//! \param llr Input geocentric (latitude, longitude, radius)
//! \param xyz Output Cartesian (x, y, z)
static void convertGeocentricToCartesian(const PIMathVectorT3d & llr, PIMathVectorT3d & xyz);
/// Fundamental routine to convert geocentric to geodetic
//! \~english Convert geocentric to geodetic
//! \~russian Преобразовать геоцентрические в геодезические
//! \param llr Input geocentric (latitude, longitude, radius)
//! \param llh Output geodetic (latitude, longitude, height)
//! \param ell Ellipsoid model (default WGS84)
static void convertGeocentricToGeodetic(const PIMathVectorT3d & llr,
PIMathVectorT3d & llh,
PIEllipsoidModel ell = PIEllipsoidModel::WGS84Ellipsoid());
/// Fundamental routine to convert geodetic to geocentric
//! \~english Convert geodetic to geocentric
//! \~russian Преобразовать геодезические в геоцентрические
//! \param llh Input geodetic (latitude, longitude, height)
//! \param llr Output geocentric (latitude, longitude, radius)
//! \param ell Ellipsoid model (default WGS84)
static void convertGeodeticToGeocentric(const PIMathVectorT3d & llh,
PIMathVectorT3d & llr,
PIEllipsoidModel ell = PIEllipsoidModel::WGS84Ellipsoid());
/// Compute the radius of the ellipsoidal Earth, given the geodetic latitude.
//! \~english Compute radius of ellipsoid at given latitude
//! \~russian Вычислить радиус эллипсоида на заданной широте
//! \param geolat Geodetic latitude in degrees
//! \param ell Ellipsoid model (default WGS84)
//! \return Radius in meters
static double radiusEarth(double geolat, PIEllipsoidModel ell = PIEllipsoidModel::WGS84Ellipsoid());
//! \~english Compute radius of ellipsoid at current position
//! \~russian Вычислить радиус эллипсоида в текущей позиции
double radiusEarth() const {
PIGeoPosition p(*this);
p.transformTo(PIGeoPosition::Geodetic);
return PIGeoPosition::radiusEarth((*this)[0], p.el);
}
/// Compute the range in meters between two PIGeoPositions.
//! \~english Compute range between two positions
//! \~russian Вычислить расстояние между двумя позициями
//! \param a First position
//! \param b Second position
//! \return Range in meters
static double range(const PIGeoPosition & a, const PIGeoPosition & b);
//! \~english Compute range from this position to another
//! \~russian Вычислить расстояние от этой позиции до другой
//! \param p Target position
//! \return Range in meters
double range(const PIGeoPosition & p) const { return range((*this), p); }
/// Computes the elevation of the input (p) position as seen from this PIGeoPosition.
//! \~english Compute elevation angle to target position
//! \~russian Вычислить угол возвышения до целевой позиции
//! \param p Target position
//! \return Elevation angle in degrees
double elevation(const PIGeoPosition & p) const;
/// Computes the elevation of the input (p) position as seen from this PIGeoPosition, using a Geodetic (ellipsoidal) system.
//! \~english Compute geodetic elevation angle to target position
//! \~russian Вычислить геодезический угол возвышения до целевой позиции
//! \param p Target position
//! \return Elevation angle in degrees
double elevationGeodetic(const PIGeoPosition & p) const;
/// Computes the azimuth of the input (p) position as seen from this PIGeoPosition.
//! \~english Compute azimuth angle to target position
//! \~russian Вычислить азимут до целевой позиции
//! \param p Target position
//! \return Azimuth angle in degrees
double azimuth(const PIGeoPosition & p) const;
/// Computes the azimuth of the input (p) position as seen from this PIGeoPosition, using a Geodetic (ellipsoidal) system.
//! \~english Compute geodetic azimuth angle to target position
//! \~russian Вычислить геодезический азимут до целевой позиции
//! \param p Target position
//! \return Azimuth angle in degrees
double azimuthGeodetic(const PIGeoPosition & p) const;
/// Computes the radius of curvature of the meridian (Rm) corresponding to this PIGeoPosition.
//! \~english Get radius of curvature of the meridian
//! \~russian Получить радиус кривизны меридиана
//! \return Radius in meters
double getCurvMeridian() const;
/// Computes the radius of curvature in the prime vertical (Rn) corresponding to this PIGeoPosition.
//! \~english Get radius of curvature in the prime vertical
//! \~russian Получить радиус кривизны в вертикале
//! \return Radius in meters
double getCurvPrimeVertical() const;
/// Returns as PIMathVectorT3d
//! \~english Get as PIMathVectorT3d
//! \~russian Получить как PIMathVectorT3d
//! \return Reference to underlying vector
const PIMathVectorT3d & vector() const { return *this; }
//! \~english Assignment from vector
//! \~russian Присваивание из вектора
PIGeoPosition & operator=(const PIMathVectorT3d & v);
//! \~english Subtraction
//! \~russian Вычитание
PIGeoPosition & operator-=(const PIGeoPosition & right);
//! \~english Addition
//! \~russian Сложение
PIGeoPosition & operator+=(const PIGeoPosition & right);
//! \~english Subtraction
//! \~russian Вычитание
friend PIGeoPosition operator-(const PIGeoPosition & left, const PIGeoPosition & right);
//! \~english Addition
//! \~russian Сложение
friend PIGeoPosition operator+(const PIGeoPosition & left, const PIGeoPosition & right);
//! \~english Scalar multiplication
//! \~russian Умножение на скаляр
friend PIGeoPosition operator*(const double & scale, const PIGeoPosition & right);
friend PIGeoPosition operator*(const PIGeoPosition & left, const double & scale);
friend PIGeoPosition operator*(const int & scale, const PIGeoPosition & right);
friend PIGeoPosition operator*(const PIGeoPosition & left, const int & scale);
//! \~english Equality comparison
//! \~russian Сравнение на равенство
bool operator==(const PIGeoPosition & right) const;
//! \~english Inequality comparison
//! \~russian Сравнение на неравенство
bool operator!=(const PIGeoPosition & right) const { return !(operator==(right)); }
@@ -181,6 +372,8 @@ private:
};
//! \~english Subtraction
//! \~russian Вычитание
inline PIGeoPosition operator-(const PIGeoPosition & left, const PIGeoPosition & right) {
PIGeoPosition l(left), r(right);
l.transformTo(PIGeoPosition::Cartesian);
@@ -188,6 +381,8 @@ inline PIGeoPosition operator-(const PIGeoPosition & left, const PIGeoPosition &
l -= r;
return l;
}
//! \~english Addition
//! \~russian Сложение
inline PIGeoPosition operator+(const PIGeoPosition & left, const PIGeoPosition & right) {
PIGeoPosition l(left), r(right);
l.transformTo(PIGeoPosition::Cartesian);
@@ -195,17 +390,25 @@ inline PIGeoPosition operator+(const PIGeoPosition & left, const PIGeoPosition &
l += r;
return l;
}
//! \~english Scalar multiplication (double)
//! \~russian Умножение на скаляр (double)
inline PIGeoPosition operator*(const double & scale, const PIGeoPosition & right) {
PIMathVectorT3d tmp(right);
tmp *= scale;
return PIGeoPosition(tmp);
}
//! \~english Scalar multiplication (double)
//! \~russian Умножение на скаляр (double)
inline PIGeoPosition operator*(const PIGeoPosition & left, const double & scale) {
return operator*(scale, left);
}
//! \~english Scalar multiplication (int)
//! \~russian Умножение на скаляр (int)
inline PIGeoPosition operator*(const int & scale, const PIGeoPosition & right) {
return operator*(double(scale), right);
}
//! \~english Scalar multiplication (int)
//! \~russian Умножение на скаляр (int)
inline PIGeoPosition operator*(const PIGeoPosition & left, const int & scale) {
return operator*(double(scale), left);
}

View File

@@ -1,3 +1,14 @@
//! \addtogroup HTTP
//! \{
//! \file pihttpclient.h
//! \brief HTTP client implementation
//! \~english HTTP client for performing asynchronous HTTP requests with callbacks
//! \~russian HTTP клиент для выполнения асинхронных HTTP запросов с колбэками
//! \details
//! \~english Provides async HTTP client functionality using libcurl
//! \~russian Обеспечивает функциональность асинхронного HTTP клиента с использованием libcurl
//! \}
#ifndef pihttpclient_h
#define pihttpclient_h
@@ -6,6 +17,8 @@
#include "pistringlist.h"
//! \~english Base class for HTTP client internal functionality
//! \~russian Базовый класс для внутренней функциональности HTTP клиента
class PIHTTPClientBase {
public:
int __infoFunc(ssize_t dltotal, ssize_t dlnow, ssize_t ultotal, ssize_t ulnow);

View File

@@ -1,9 +1,23 @@
//! \addtogroup HTTP
//! \{
//! \file pihttpconstants.h
//! \brief HTTP constants and enumerations
//! \~english Definitions for HTTP methods, status codes and header names
//! \~russian Определения HTTP методов, кодов состояния и имен заголовков
//! \details
//! \~english Provides enum classes for HTTP methods and status codes, and a namespace with HTTP header name constants
//! \~russian Предоставляет классы перечислений для HTTP методов и кодов состояния, а также пространство имён с константами имён HTTP
//! заголовков
//! \}
#ifndef pihttpconstants_h
#define pihttpconstants_h
namespace PIHTTP {
//! \~english HTTP request methods
//! \~russian HTTP методы запросов
enum class Method {
Unknown,
Get,
@@ -17,6 +31,8 @@ enum class Method {
Patch
};
//! \~english HTTP response status codes
//! \~russian Коды состояния HTTP ответа
enum class Code {
Unknown = -1,
Continue = 100,
@@ -90,6 +106,8 @@ enum class Code {
NetworkAuthenticationRequired = 511,
};
//! \~english HTTP header field names
//! \~russian Имена полей заголовков HTTP
namespace Header {
constexpr static char Accept[] = "Accept";
constexpr static char AcceptCharset[] = "Accept-Charset";

View File

@@ -1,3 +1,14 @@
//! \addtogroup HTTP
//! \{
//! \file pihttptypes.h
//! \brief HTTP types and message classes
//! \~english HTTP message types and containers for requests and responses
//! \~russian Типы HTTP и классы сообщений для запросов и ответов
//! \details
//! \~english Provides MessageConst and MessageMutable classes for HTTP communication
//! \~russian Предоставляет классы MessageConst и MessageMutable для HTTP коммуникации
//! \}
#ifndef pihttptypes_h
#define pihttptypes_h

View File

@@ -1,3 +1,14 @@
//! \addtogroup HTTP
//! \{
//! \file microhttpd_server.h
//! \brief Base HTTP server class
//! \~english Base HTTP server class using libmicrohttpd
//! \~russian Базовый класс HTTP сервера с использованием libmicrohttpd
//! \details
//! \~english Provides core HTTP server functionality with connection handling
//! \~russian Обеспечивает базовую функциональность HTTP сервера с обработкой соединений
//! \}
#ifndef MICROHTTPD_SERVER_P_H
#define MICROHTTPD_SERVER_P_H
@@ -14,7 +25,11 @@ class PIP_HTTP_SERVER_EXPORT MicrohttpdServer: public PIObject {
friend struct MicrohttpdServerConnection;
public:
//! \~english Constructs a new MicroHTTP server
//! \~russian Создает новый MicroHTTP сервер
MicrohttpdServer();
//! \~english Destroys the MicroHTTP server
//! \~russian Уничтожает MicroHTTP сервер
virtual ~MicrohttpdServer();
//! \~english Server configuration options

View File

@@ -1,3 +1,14 @@
//! \addtogroup HTTP
//! \{
//! \file pihttpserver.h
//! \brief High-level HTTP server implementation
//! \~english High-level HTTP server with path-based routing and handler registration
//! \~russian Высокоуровневый HTTP сервер с маршрутизацией по путям и регистрацией обработчиков
//! \details
//! \~english Provides path-based request routing and handler management
//! \~russian Обеспечивает маршрутизацию запросов по путям и управление обработчиками
//! \}
#ifndef PIHTTPSERVER_H
#define PIHTTPSERVER_H
@@ -9,7 +20,11 @@ class PIP_HTTP_SERVER_EXPORT PIHTTPServer: public MicrohttpdServer {
PIOBJECT_SUBCLASS(PIHTTPServer, MicrohttpdServer)
public:
//! \~english Constructs a new HTTP server
//! \~russian Создает новый HTTP сервер
PIHTTPServer();
//! \~english Destroys the HTTP server
//! \~russian Уничтожает HTTP сервер
virtual ~PIHTTPServer();
using RequestFunction = std::function<PIHTTP::MessageMutable(const PIHTTP::MessageConst &)>;

View File

@@ -61,8 +61,13 @@ class PIIntrospection;
class PIIntrospectionServer;
#if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
//! \~english Declare singleton interface for introspection module T
//! \~russian Объявить интерфейс-синглтон для интроспекции модуля T
# define __PIINTROSPECTION_SINGLETON_H__(T) static PIIntrospection##T##Interface * instance();
//! \~english Implement singleton interface for introspection module T
//! \~russian Реализовать интерфейс-синглтон для интроспекции модуля T
# define __PIINTROSPECTION_SINGLETON_CPP__(T) \
PIIntrospection##T##Interface * PIIntrospection##T##Interface::instance() { \
static PIIntrospection##T##Interface ret; \

View File

@@ -16,19 +16,40 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! \addtogroup Introspection
//! \{
//! \file piintrospection_containers.h
//! \brief Container introspection
//! \~english Interface for container introspection and memory tracking
//! \~russian Интерфейс для интроспекции контейнеров и отслеживания памяти
//! \details
//! \~english Provides interface for tracking container memory allocations and usage
//! \~russian Предоставляет интерфейс для отслеживания выделения и использования памяти контейнерами
#ifndef PIINTROSPECTION_CONTAINERS_H
# define PIINTROSPECTION_CONTAINERS_H
# include "pibase.h"
//! \~english Container type information structure
//! \~russian Структура информации о типе контейнера
struct PIP_EXPORT PIIntrospectionContainersType {
~PIIntrospectionContainersType();
void finish();
//! \~english Type identifier
//! \~russian Идентификатор типа
uint id = 0;
//! \~english Type name
//! \~russian Имя типа
const char * name = nullptr;
//! \~english Demangled type name
//! \~russian Демангл-имя типа
const char * demangled = "?";
//! \~english Initialization flag
//! \~russian Флаг инициализации
bool inited = false;
//! \~english Has demangled name flag
//! \~russian Флаг наличия demangled имени
bool has_demangled = false;
};
@@ -36,11 +57,13 @@ struct PIP_EXPORT PIIntrospectionContainersType {
# include "piintrospection_base.h"
class PIIntrospectionContainers;
//! \~english Container type information template
//! \~russian Шаблон информации о типе контейнера
template<typename T>
class PIIntrospectionContainersTypeInfo {
public:
//! \~english Get container type information
//! \~russian Получить информацию о типе контейнера
static const PIIntrospectionContainersType & get() {
static PIIntrospectionContainersType ret = create();
return ret;
@@ -67,6 +90,8 @@ private:
// clang-format on
//! \~english Container introspection interface
//! \~russian Интерфейс интроспекции контейнеров
class PIP_EXPORT PIIntrospectionContainersInterface {
friend class PIIntrospection;
friend class PIIntrospectionServer;
@@ -75,11 +100,23 @@ public:
__PIINTROSPECTION_SINGLETON_H__(Containers)
// clang-format off
//! \~english Notify about new container allocation
//! \~russian Уведомить о новом выделении контейнера
void containerNew (const PIIntrospectionContainersType & ti, uint isz);
//! \~english Notify about container deletion
//! \~russian Уведомить об удалении контейнера
void containerDelete(const PIIntrospectionContainersType & ti);
//! \~english Notify about container memory allocation
//! \~russian Уведомить о выделении памяти контейнером
void containerAlloc (const PIIntrospectionContainersType & ti, ullong cnt);
//! \~english Notify about container memory freeing
//! \~russian Уведомить об освобождении памяти контейнером
void containerFree (const PIIntrospectionContainersType & ti, ullong cnt);
//! \~english Notify about container memory usage
//! \~russian Уведомить об использовании памяти контейнером
void containerUsed (const PIIntrospectionContainersType & ti, ullong cnt);
//! \~english Notify about container unused memory
//! \~russian Уведомить о неиспользуемой памяти контейнера
void containerUnused(const PIIntrospectionContainersType & ti, ullong cnt);
// clang-format on
@@ -98,7 +135,8 @@ private:
# define PIINTROSPECTION_CONTAINER_FREE(t, cnt)
# define PIINTROSPECTION_CONTAINER_USED(t, cnt)
# define PIINTROSPECTION_CONTAINER_UNUSED(t, cnt)
#endif
# endif // defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
#endif // PIINTROSPECTION_CONTAINERS_H
//! \}

View File

@@ -51,13 +51,23 @@ class PISystemMonitor;
# define PIINTROSPECTION_START(name) PIINTROSPECTION_SERVER->start(#name);
# define PIINTROSPECTION_STOP PIINTROSPECTION_SERVER->stop();
//! \~english Introspection server class for collecting and broadcasting PIP internal statistics
//! \~russian Класс сервера интроспекции для сбора и распространения внутренних статистик PIP
class PIP_EXPORT PIIntrospectionServer: public PIPeer {
PIOBJECT_SUBCLASS(PIIntrospectionServer, PIPeer);
public:
//! \~english Get singleton instance of introspection server
//! \~russian Получить синглтон-экземпляр сервера интроспекции
static PIIntrospectionServer * instance();
//! \~english Start introspection server with specified name
//! \~russian Запустить сервер интроспекции с указанным именем
//! \param server_name \~english Name of the server to start \~russian Имя сервера для запуска
void start(const PIString & server_name);
//! \~english Stop introspection server
//! \~russian Остановить сервер интроспекции
void stop();
private:

View File

@@ -17,6 +17,18 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! \addtogroup Introspection
//! \{
//! \file piintrospection_threads.h
//! \brief Thread introspection interface
//! \~english Interface for thread introspection and monitoring
//! \~russian Интерфейс для интроспекции и мониторинга потоков
//! \details
//! \~english Provides callbacks for thread lifecycle events such as creation, deletion, start, run, wait, stop and completion
//! \~russian Предоставляет обратные вызовы для событий жизненного цикла потоков: создание, удаление, запуск, выполнение, ожидание,
//! остановка и завершение
//! \}
#ifndef PIINTROSPECTION_THREADS_H
# define PIINTROSPECTION_THREADS_H
@@ -36,6 +48,8 @@ class PIIntrospectionThreads;
# define PIINTROSPECTION_THREAD_STOP(t) PIINTROSPECTION_THREADS->threadStop(t);
# define PIINTROSPECTION_THREAD_RUN_DONE(t, us) PIINTROSPECTION_THREADS->threadRunDone(t, us);
//! \~english Thread introspection interface class
//! \~russian Класс интерфейса интроспекции потоков
class PIP_EXPORT PIIntrospectionThreadsInterface {
friend class PIIntrospection;
@@ -43,12 +57,26 @@ public:
__PIINTROSPECTION_SINGLETON_H__(Threads)
// clang-format off
//! \~english Called when new thread is created
//! \~russian Вызывается при создании нового потока
void threadNew (PIThread * t);
//! \~english Called when thread is deleted
//! \~russian Вызывается при удалении потока
void threadDelete (PIThread * t);
//! \~english Called when thread is starting
//! \~russian Вызывается при запуске потока
void threadStart (PIThread * t);
//! \~english Called when thread starts execution
//! \~russian Вызывается при начале выполнения потока
void threadRun (PIThread * t);
//! \~english Called when thread is waiting
//! \~russian Вызывается при ожидании потока
void threadWait (PIThread * t);
//! \~english Called when thread is stopped
//! \~russian Вызывается при остановке потока
void threadStop (PIThread * t);
//! \~english Called when thread execution is done
//! \~russian Вызывается при завершении выполнения потока
void threadRunDone(PIThread * t, ullong us);
// clang-format on
@@ -70,3 +98,4 @@ private:
# endif
#endif // PIINTROSPECTION_THREADS_H
//! \}

View File

@@ -1,9 +1,13 @@
/*! \file pibinarylog.h
* \ingroup IO
* \~\brief
* \~english Binary log
* \~russian Бинарный лог
*/
//! \addtogroup IO
//! \{
//! \file pibinarylog.h
//! \brief
//! \~english Binary log
//! \~russian Бинарный лог
//! \details
//! \~english Class for writing and reading binary data to/from log files, with support for playback in different modes
//! \~russian Класс для записи и чтения бинарных данных в/из файлов логов с поддержкой воспроизведения в различных режимах
//! \~\}
/*
PIP - Platform Independent Primitives
Class for write binary data to logfile, and read or playback this data
@@ -29,10 +33,8 @@
#include "pichunkstream.h"
#include "pifile.h"
//! \ingroup IO
//! \~\brief
//! \~english Binary log
//! \~russian Бинарный лог
//! \~english Class for writing and reading binary data to/from log files, with support for playback in different modes
//! \~russian Класс для записи и чтения бинарных данных в/из файлов логов с поддержкой воспроизведения в различных режимах
class PIP_EXPORT PIBinaryLog: public PIIODevice {
PIIODEVICE(PIBinaryLog, "binlog");
@@ -40,93 +42,146 @@ public:
explicit PIBinaryLog();
virtual ~PIBinaryLog();
//! \brief Play modes for \a PIBinaryLog
//! \~english Play modes for \a PIBinaryLog
//! \~russian Режимы воспроизведения для \a PIBinaryLog
enum PlayMode {
PlayRealTime /*! Play in system realtime, default mode */,
PlayVariableSpeed /*! Play in software realtime with speed, set by \a setSpeed */,
PlayStaticDelay /*! Play with custom static delay, ignoring timestamp */
PlayRealTime /*! \~english Play in system realtime, default mode \~russian В системном реальном времени, режим по умолчанию */,
PlayVariableSpeed /*! \~english Play in software realtime with speed, set by \a setSpeed \~russian В программном реальном времени со скоростью, задаваемой через \a setSpeed */,
PlayStaticDelay /*! \~english Play with custom static delay, ignoring timestamp \~russian С пользовательской статической задержкой, игнорируя метку времени */
};
//! \brief Different split modes for writing \a PIBinaryLog, which can separate files by size, by time or by records count
//! \~english Different split modes for writing \a PIBinaryLog, which can separate files by size, by time or by records count
//! \~russian Различные режимы разделения для записи \a PIBinaryLog, позволяющие разделять файлы по размеру, времени или количеству записей
enum SplitMode {
SplitNone /*! Without separate, default mode */,
SplitTime /*! Separate files by record time */,
SplitSize /*! Separate files by size */,
SplitCount /*! Separate files by records count */
SplitNone /*! \~english Without separate, default mode \~russian Без разделения, режим по умолчанию */,
SplitTime /*! \~english Separate files by record time \~russian Разделение файлов по времени записи */,
SplitSize /*! \~english Separate files by size \~russian Разделение файлов по размеру */,
SplitCount /*! \~english Separate files by records count \~russian Разделение файлов по количеству записей */
};
#pragma pack(push, 8)
//! \brief Struct contains information about all records with same ID
//! \~english Struct contains information about all records with same ID
//! \~russian Структура содержит информацию обо всех записях с одинаковым ID
struct PIP_EXPORT BinLogRecordInfo {
//! \~english Constructor, initializes all fields to zero
//! \~russian Конструктор, инициализирует все поля нулями
BinLogRecordInfo() {
id = count = 0;
minimum_size = maximum_size = 0;
}
//! \~english Record ID
//! \~russian ID записи
int id;
//! \~english Records count
//! \~russian Количество записей
int count;
//! \~english Minimum record size
//! \~russian Минимальный размер записи
int minimum_size;
//! \~english Maximum record size
//! \~russian Максимальный размер записи
int maximum_size;
//! \~english Start time of records
//! \~russian Время начала записей
PISystemTime start_time;
//! \~english End time of records
//! \~russian Время окончания записей
PISystemTime end_time;
};
//! \brief Struct contains position, ID and timestamp of record in file
//! \~english Struct contains position, ID and timestamp of record in file
//! \~russian Структура содержит позицию, ID и метку времени записи в файле
struct PIP_EXPORT BinLogIndex {
//! \~english Record ID
//! \~russian ID записи
int id;
//! \~english Data size
//! \~russian Размер данных
int data_size;
//! \~english Position in file
//! \~russian Позиция в файле
llong pos;
//! \~english Timestamp
//! \~russian Метка времени
PISystemTime timestamp;
};
#pragma pack(pop)
//! \brief Struct contains full information about Binary Log file and about all Records using map of \a BinLogRecordInfo
//! \~english Struct contains full information about Binary Log file and about all Records using map of \a BinLogRecordInfo
//! \~russian Структура содержит полную информацию о файле бинарного лога и обо всех записях с использованием карты \a BinLogRecordInfo
struct PIP_EXPORT BinLogInfo {
//! \~english Path to the log file
//! \~russian Путь к файлу лога
PIString path;
//! \~english Total records count
//! \~russian Общее количество записей
int records_count = 0;
//! \~english Log file size
//! \~russian Размер файла лога
llong log_size = 0L;
//! \~english Start time of logging
//! \~russian Время начала логирования
PISystemTime start_time;
//! \~english End time of logging
//! \~russian Время окончания логирования
PISystemTime end_time;
//! \~english Map of record information by ID
//! \~russian Карта информации о записях по ID
PIMap<int, BinLogRecordInfo> records;
//! \~english User-defined header data
//! \~russian Пользовательские данные заголовка
PIByteArray user_header;
};
//! Current \a PlayMode
//! \~english Returns current \a PlayMode
//! \~russian Возвращает текущий \a PlayMode
PlayMode playMode() const { return play_mode; }
//! Current \a SplitMode
//! \~english Returns current \a SplitMode
//! \~russian Возвращает текущий \a SplitMode
SplitMode splitMode() const { return split_mode; }
//! Current directory where billogs wiil be saved
//! \~english Returns current directory where binlogs will be saved
//! \~russian Возвращает текущий каталог, куда будут сохраняться бинарные логи
PIString logDir() const { return property("logDir").toString(); }
//! Returns current file prefix
//! \~english Returns current file prefix
//! \~russian Возвращает текущий префикс файла
PIString filePrefix() const { return property("filePrefix").toString(); }
//! Default ID, used in \a write function
//! \~english Returns default ID, used in \a write function
//! \~russian Возвращает ID по умолчанию, используемый в функции \a write
int defaultID() const { return default_id; }
//! Returns current play speed
//! \~english Returns current play speed
//! \~russian Возвращает текущую скорость воспроизведения
double playSpeed() const { return play_speed > 0 ? 1. / play_speed : 0.; }
//! Returns current play delay
//! \~english Returns current play delay
//! \~russian Возвращает текущую задержку воспроизведения
PISystemTime playDelay() const { return play_delay; }
//! Returns current binlog file split time
//! \~english Returns current binlog file split time
//! \~russian Возвращает текущее время разделения файла бинарного лога
PISystemTime splitTime() const { return split_time; }
//! Returns current binlog file split size
//! \~english Returns current binlog file split size
//! \~russian Возвращает текущий размер разделения файла бинарного лога
llong splitFileSize() const { return split_size; }
//! Returns current binlog file split records count
//! \~english Returns current binlog file split records count
//! \~russian Возвращает текущее количество записей для разделения файла бинарного лога
int splitRecordCount() const { return split_count; }
//! Returns if rapid start enabled
//! \~english Returns if rapid start enabled
//! \~russian Возвращает, включено ли быстрое начало
bool rapidStart() const { return rapid_start; }
//! Returns if index creates while writing
//! \~english Returns if index creates while writing
//! \~russian Возвращает, создается ли индекс во время записи
bool createIndexOnFly() const { return create_index_on_fly; }
//! Create binlog file with Filename = path

View File

@@ -29,16 +29,36 @@
#include "piiodevice.h"
//! \ingroup IO
//! \~\brief
//! \~english CAN device.
//! \~russian Устройство CAN.
class PIP_EXPORT PICAN: public PIIODevice {
PIIODEVICE(PICAN, "can");
public:
//! \~english Constructs %PICAN with empty path and ReadWrite mode
//! \~russian Создаёт %PICAN с пустым путём и режимом ReadWrite
explicit PICAN(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! \~english Destructor
//! \~russian Деструктор
virtual ~PICAN();
//! \~english Set CAN ID for filtering received messages
//! \~russian Устанавливает CAN ID для фильтрации принимаемых сообщений
void setCANID(int id);
//! \~english Returns current CAN ID
//! \~russian Возвращает текущий CAN ID
int CANID() const;
//! \~english Returns CAN ID of last readed message
//! \~russian Возвращает CAN ID последнего прочитанного сообщения
int readedCANID() const;
//! \~english Interrupt blocking operation
//! \~russian Прерывает блокирующую операцию
void interrupt() override;
protected:

View File

@@ -1,9 +1,13 @@
/*! \file piconfig.h
* \ingroup IO
* \~\brief
* \~english Configuration files parser and writer
* \~russian Разбор и запись конфигурационных файлов
*/
//! \addtogroup IO
//! \{
//! \file piconfig.h
//! \brief Configuration files parser and writer
//! \~english Parser and writer for configuration files with tree structure support
//! \~russian Разбор и запись конфигурационных файлов с поддержкой древовидной структуры
//! \details
//! \~english PIConfig provides functionality to read, write and manipulate configuration files in a tree-like structure.
//! \~russian PIConfig предоставляет функциональность для чтения, записи и управления конфигурационными файлами в древовидной структуре.
//! \}
/*
PIP - Platform Independent Primitives
Configuration parser and writer
@@ -58,6 +62,8 @@
Entry & getValue(const PIString & vname, const double def, bool * exists = 0) const {return getValue(vname, PIString::fromNumber(def), exists);}
// clang-format on
//! \~english Main configuration parser and writer class
//! \~russian Главный класс для разбора и записи конфигурационных файлов
class PIP_EXPORT PIConfig {
friend class Entry;
friend class Branch;
@@ -79,6 +85,8 @@ public:
class Entry;
//! \~english Branch class - container for Entry objects
//! \~russian Класс Branch - контейнер для объектов Entry
class PIP_EXPORT Branch: public PIVector<Entry *> {
friend class PIConfig;
friend class Entry;
@@ -90,22 +98,44 @@ public:
public:
Branch() { ; }
//! \~english Get value from branch by name with default value
//! \~russian Получить значение из ветки по имени со значением по умолчанию
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0);
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0) const {
return const_cast<Branch *>(this)->getValue(vname, def, exists);
}
PICONFIG_GET_VALUE
//! \~english Get all leaf entries from the entire tree
//! \~russian Получить все листовые записи из всего дерева
Branch allLeaves();
//! \~english Get all entries with name containing specified substring
//! \~russian Получить все записи с именем, содержащим указанную подстроку
Branch getValues(const PIString & name);
//! \~english Get all leaf entries (entries without children)
//! \~russian Получить все листовые записи (записи без детей)
Branch getLeaves();
//! \~english Get all branch entries (entries with children)
//! \~russian Получить все ветвящиеся записи (записи с детьми)
Branch getBranches();
//! \~english Filter branch by filter string
//! \~russian Фильтровать ветку по строке фильтра
Branch & filter(const PIString & f);
//! \~english Check if entry with specified name exists
//! \~russian Проверить, существует ли запись с указанным именем
bool isEntryExists(const PIString & name) const {
for (const auto * i: *this)
if (entryExists(i, name)) return true;
return false;
}
//! \~english Get index of entry in branch
//! \~russian Получить индекс записи в ветке
int indexOf(const Entry * e) {
for (int i = 0; i < size_s(); ++i)
if (at(i) == e) return i;
@@ -138,6 +168,8 @@ public:
};
//! \~english Entry class - represents a single configuration entry
//! \~russian Класс Entry - представляет отдельную запись конфигурации
class PIP_EXPORT Entry {
friend class PIConfig;
friend class Branch;
@@ -446,18 +478,24 @@ public:
};
//! Read configuration from file at path "path" in mode "mode"
//! \~english Open configuration from file at path in specified mode
//! \~russian Открыть конфигурацию из файла по указанному пути в указанном режиме
bool open(const PIString & path, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! Read configuration from string "string" in mode "mode"
//! \~english Open configuration from string in specified mode
//! \~russian Открыть конфигурацию из строки в указанном режиме
bool open(PIString * string, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! Read configuration from custom device "device" in mode "mode"
//! \~english Open configuration from custom I/O device in specified mode
//! \~russian Открыть конфигурацию из пользовательского устройства ввода-вывода в указанном режиме
bool open(PIIODevice * device, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! \~english Check if configuration is opened
//! \~russian Проверить, открыта ли конфигурация
bool isOpened() const;
//! Returns top-level entry with name "vname", if doesn`t exists return entry with value "def" and set *exist to false
//! \~english Get top-level entry by name with default value
//! \~russian Получить запись верхнего уровня по имени со значением по умолчанию
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0);
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0) const {
return const_cast<PIConfig *>(this)->getValue(vname, def, exists);
@@ -504,13 +542,16 @@ public:
//! \fn Entry & getValue(const PIString & vname, const double def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! Returns top-level entries with names with substrings "vname"
//! \~english Get all top-level entries with name containing substring
//! \~russian Получить все записи верхнего уровня с именем, содержащим подстроку
Branch getValues(const PIString & vname);
//! Set top-level entry with name "name" value to "value", type to "type" and if "write" immediate write to file. Add new entry if there
//! is no suitable exists
//! \~english Set value for top-level entry with name
//! \~russian Установить значение для записи верхнего уровня с именем
//! \details
//! \~english Set top-level entry with name "name" value to "value", type to "type" and if "write" immediate write to file.
//! Add new entry if there is no suitable exists
void setValue(const PIString & name, const PIString & value, const PIString & type = "s", bool write = true);
//! Set top-level entry with name "name" value to "value", type to "l" and if "write" immediate write to file. Add new entry if there is
@@ -561,16 +602,20 @@ public:
//! no suitable exists
void setValue(const PIString & name, const double value, bool write = true) { setValue(name, PIString::fromNumber(value), "f", write); }
//! Returns root entry
//! \~english Get root entry of configuration tree
//! \~russian Получить корневую запись дерева конфигурации
Entry & rootEntry() { return root; }
//! Returns top-level entries count
//! \~english Get count of top-level entries
//! \~russian Получить количество записей верхнего уровня
int entriesCount() const { return childCount(&root); }
//! Returns if top-level entry with name "name" exists
//! \~english Check if top-level entry with name exists
//! \~russian Проверить, существует ли запись верхнего уровня с указанным именем
bool isEntryExists(const PIString & name) const { return entryExists(&root, name); }
//! Returns all top-level entries
//! \~english Get all top-level entries
//! \~russian Получить все записи верхнего уровня
Branch allTree() {
Branch b;
for (auto * i: root._children)
@@ -579,7 +624,8 @@ public:
return b;
}
//! Returns all entries without children
//! \~english Get all entries without children (leaves)
//! \~russian Получить все записи без детей (листья)
Branch allLeaves() {
Branch b;
allLeaves(b, &root);
@@ -588,35 +634,71 @@ public:
return b;
}
//! \~english Get index of entry by name
//! \~russian Получить индекс записи по имени
int entryIndex(const PIString & name);
//! \~english Get entry name by index
//! \~russian Получить имя записи по индексу
PIString getName(uint number) { return entryByIndex(number)._name; }
//! \~english Get entry value by index
//! \~russian Получить значение записи по индексу
PIString getValueByIndex(uint number) { return entryByIndex(number)._value; }
//! \~english Get entry type by index
//! \~russian Получить тип записи по индексу
PIChar getType(uint number) { return entryByIndex(number)._type[0]; }
//! \~english Get entry comment by index
//! \~russian Получить комментарий записи по индексу
PIString getComment(uint number) { return entryByIndex(number)._comment; }
//! \~english Add new entry with name, value and type
//! \~russian Добавить новую запись с именем, значением и типом
void addEntry(const PIString & name, const PIString & value, const PIString & type = "s", bool write = true);
//! \~english Set name of entry by index
//! \~russian Установить имя записи по индексу
void setName(uint number, const PIString & name, bool write = true);
//! \~english Set value of entry by index
//! \~russian Установить значение записи по индексу
void setValue(uint number, const PIString & value, bool write = true);
//! \~english Set type of entry by index
//! \~russian Установить тип записи по индексу
void setType(uint number, const PIString & type, bool write = true);
//! \~english Set comment of entry by index
//! \~russian Установить комментарий записи по индексу
void setComment(uint number, const PIString & comment, bool write = true);
//! \~english Remove entry by name
//! \~russian Удалить запись по имени
void removeEntry(const PIString & name, bool write = true);
//! \~english Remove entry by index
//! \~russian Удалить запись по индексу
void removeEntry(uint number, bool write = true);
//! Remove all tree and device content
void clear();
//! Parse device and build internal tree
//! \~english Parse device and build internal tree
//! \~russian Прочитать устройство и построить внутреннее дерево
void readAll();
//! Write all internal tree to device
//! \~english Write all internal tree to device
//! \~russian Записать всё внутреннее дерево в устройство
void writeAll();
//! Returns current tree delimiter, default "."
//! \~english Get current tree delimiter
//! \~russian Получить текущий разделитель дерева
const PIString & delimiter() const { return delim; }
//! Set current tree delimiter
//! \~english Set tree delimiter
//! \~russian Установить разделитель дерева
void setDelimiter(const PIString & d) {
delim = d;
setEntryDelim(&root, d);
@@ -708,14 +790,18 @@ inline PICout operator<<(PICout s, const PIConfig::Entry & v) {
}
/** \relatesalso PIConfig \relatesalso PIIODevice
* \brief Service function. useful for configuring devices
* \details Function takes entry name "name", default value "def" and two
* \a PIConfig::Entry sections: "em" and their parent "ep". If there is no
* parent ep = 0. If "ep" is not null and entry "name" exists in "ep" function
* returns this value. Else returns value of entry "name" in section "em" or
* "def" if entry doesn`t exists. \n This function useful to read settings
* from configuration file in implementation \a PIIODevice::configureDevice() function */
//! \~english Service function for reading device settings from configuration
//! \~russian Сервисная функция для чтения настроек устройства из конфигурации
//! \note
//! \~english Useful for implementing PIIODevice::configureDevice()
//! \~russian Полезно для реализации PIIODevice::configureDevice()
//! \~\details Function takes entry name "name", default value "def" and two
//! \a PIConfig::Entry sections: "em" and their parent "ep". If there is no
//! parent ep = 0. If "ep" is not null and entry "name" exists in "ep" function
//! returns this value. Else returns value of entry "name" in section "em" or
//! "def" if entry doesn`t exists. \n This function useful to read settings
//! from configuration file in implementation \a PIIODevice::configureDevice() function
//! \relatesalso PIConfig \relatesalso PIIODevice
template<typename T>
T readDeviceSetting(const PIString & name, const T & def, const PIConfig::Entry * em, const PIConfig::Entry * ep) {
PIVariant v = PIVariant::fromValue<T>(def);

View File

@@ -76,30 +76,40 @@ public:
//! Set read address
//! \~english Set read address
//! \~russian Устанавливает адрес для чтения
void setReadAddress(const PIString & ip, int port) {
addr_r.set(ip, port);
setPath(addr_r.toString());
}
//! Set read address in format "i.i.i.i:p"
//! \~english Set read address in format "i.i.i.i:p"
//! \~russian Устанавливает адрес для чтения в формате "i.i.i.i:p"
void setReadAddress(const PIString & ip_port) {
addr_r.set(ip_port);
setPath(addr_r.toString());
}
//! Set read address
//! \~english Set read address
//! \~russian Устанавливает адрес для чтения
void setReadAddress(const PINetworkAddress & addr) {
addr_r = addr;
setPath(addr_r.toString());
}
//! Set read IP
//! \~english Set read IP
//! \~russian Устанавливает IP для чтения
void setReadIP(const PIString & ip) {
addr_r.setIP(ip);
setPath(addr_r.toString());
}
//! Set read port
//! \~english Set read port
//! \~russian Устанавливает порт для чтения
void setReadPort(int port) {
addr_r.setPort(port);
setPath(addr_r.toString());
@@ -153,12 +163,16 @@ public:
//! Set parameters to "parameters_". You should to reopen %PIEthernet to apply them
//! \~english Set parameters to "parameters_". You should to reopen %PIEthernet to apply them
//! \~russian Устанавливает параметры "parameters_". Необходимо переоткрыть %PIEthernet для применения
void setParameters(PIFlags<PIEthernet::Parameters> parameters_) {
params = parameters_;
applyParameters();
}
//! Set parameter "parameter" to state "on". You should to reopen %PIEthernet to apply this
//! \~english Set parameter "parameter" to state "on". You should to reopen %PIEthernet to apply this
//! \~russian Устанавливает параметр "parameter" в состояние "on". Необходимо переоткрыть %PIEthernet для применения
void setParameter(PIEthernet::Parameters parameter, bool on = true) {
params.setFlag(parameter, on);
applyParameters();
@@ -222,18 +236,24 @@ public:
bool connect(bool threaded = true);
//! Connect to TCP server with address "ip":"port". Use only for TCP_Client
//! \~english Connect to TCP server with address "ip":"port". Use only for TCP_Client
//! \~russian Подключается к TCP серверу по адресу "ip":"port". Только для TCP_Client
bool connect(const PIString & ip, int port, bool threaded = true) {
setPath(ip + PIStringAscii(":") + PIString::fromNumber(port));
return connect(threaded);
}
//! Connect to TCP server with address "ip_port". Use only for TCP_Client
//! \~english Connect to TCP server with address "ip_port". Use only for TCP_Client
//! \~russian Подключается к TCP серверу по адресу "ip_port". Только для TCP_Client
bool connect(const PIString & ip_port, bool threaded = true) {
setPath(ip_port);
return connect(threaded);
}
//! Connect to TCP server with address "addr". Use only for TCP_Client
//! \~english Connect to TCP server with address "addr". Use only for TCP_Client
//! \~russian Подключается к TCP серверу по адресу "addr". Только для TCP_Client
bool connect(const PINetworkAddress & addr, bool threaded = true) {
setPath(addr.toString());
return connect(threaded);
@@ -269,11 +289,15 @@ public:
bool send(const void * data, int size, bool threaded = false);
//! Send data "data" with size "size" to address "ip":"port"
//! \~english Send data "data" with size "size" to address "ip":"port"
//! \~russian Отправляет данные "data" размером "size" на адрес "ip":"port"
bool send(const PIString & ip, int port, const void * data, int size, bool threaded = false) {
return send(PINetworkAddress(ip, port), data, size, threaded);
}
//! Send data "data" with size "size" to address "ip_port"
//! \~english Send data "data" with size "size" to address "ip_port"
//! \~russian Отправляет данные "data" размером "size" на адрес "ip_port"
bool send(const PIString & ip_port, const void * data, int size, bool threaded = false) {
return send(PINetworkAddress(ip_port), data, size, threaded);
}
@@ -285,11 +309,15 @@ public:
bool send(const PIByteArray & data, bool threaded = false);
//! Send data "data" to address "ip":"port" for UDP
//! \~english Send data "data" to address "ip":"port" for UDP
//! \~russian Отправляет данные "data" на адрес "ip":"port" для UDP
bool send(const PIString & ip, int port, const PIByteArray & data, bool threaded = false) {
return send(PINetworkAddress(ip, port), data, threaded);
}
//! Send data "data" to address "ip_port" for UDP
//! \~english Send data "data" to address "ip_port" for UDP
//! \~russian Отправляет данные "data" на адрес "ip_port" для UDP
bool send(const PIString & ip_port, const PIByteArray & data, bool threaded = false) {
return send(PINetworkAddress(ip_port), data, threaded);
}

View File

@@ -63,17 +63,11 @@ typedef std::function<bool(const uchar *, int, void *)> ReadRetFunc;
# define PIIODEVICE(name, prefix) \
PIOBJECT_SUBCLASS(name, PIIODevice) \
PIIODevice * copy() const override { \
return new name(); \
} \
PIIODevice * copy() const override { return new name(); } \
\
public: \
PIConstChars fullPathPrefix() const override { \
return prefix; \
} \
static PIConstChars fullPathPrefixS() { \
return prefix; \
} \
PIConstChars fullPathPrefix() const override { return prefix; } \
static PIConstChars fullPathPrefixS() { return prefix; } \
\
private:

View File

@@ -49,11 +49,15 @@ public:
resetReadError();
}
//! \~english Append data to stream
//! \~russian Добавляет данные в поток
bool binaryStreamAppendImp(const void * d, size_t s) {
if (!dev) return false;
return (dev->write(d, s) == (int)s);
}
//! \~english Take data from stream
//! \~russian Извлекает данные из потока
bool binaryStreamTakeImp(void * d, size_t s) {
if (!dev) return false;
return (dev->read(d, s) == (int)s);
@@ -90,6 +94,8 @@ public:
if (io_string) delete io_string;
}
//! \~english Assign "device" device
//! \~russian Назначает устройство "device"
void setDevice(PIIODevice * device) {
bin_stream = PIIOBinaryStream(device);
setStream(&bin_stream);

View File

@@ -29,6 +29,10 @@
#include "pidiagnostics.h"
#include "piethernet.h"
//! \ingroup IO
//! \~\brief
//! \~english Peering net node.
//! \~russian Элемент пиринговой сети.
class PIP_EXPORT PIPeer: public PIIODevice {
PIIODEVICE(PIPeer, "peer");
@@ -36,9 +40,16 @@ private:
class PeerData;
public:
//! \~english Constructs %PIPeer with name "name"
//! \~russian Создаёт %PIPeer с именем "name"
explicit PIPeer(const PIString & name = PIString());
//! \~english Destructor
//! \~russian Деструктор
virtual ~PIPeer();
//! \~english Peer information structure
//! \~russian Структура информации о пире
class PIP_EXPORT PeerInfo {
friend class PIPeer;
BINARY_STREAM_FRIEND(PIPeer::PeerInfo);
@@ -87,38 +98,89 @@ public:
BINARY_STREAM_FRIEND(PIPeer::PeerInfo);
//! \~english Send data to peer with name "to"
//! \~russian Отправляет данные пиру с именем "to"
bool send(const PIString & to, const PIByteArray & data) { return send(to, data.data(), data.size_s()); }
//! \~english Send string to peer with name "to"
//! \~russian Отправляет строку пиру с именем "to"
bool send(const PIString & to, const PIString & data) { return send(to, data.data(), data.size_s()); }
bool send(const PIString & to, const void * data, int size);
//! \~english Send data to peer "to"
//! \~russian Отправляет данные пиру "to"
bool send(const PeerInfo & to, const PIByteArray & data) { return send(to.name, data.data(), data.size_s()); }
bool send(const PeerInfo & to, const PIString & data) { return send(to.name, data.data(), data.size_s()); }
bool send(const PeerInfo & to, const void * data, int size) { return send(to.name, data, size); }
bool send(const PeerInfo * to, const PIByteArray & data);
bool send(const PeerInfo * to, const PIString & data);
bool send(const PeerInfo * to, const void * data, int size);
//! \~english Send data to all peers
//! \~russian Отправляет данные всем пирам
void sendToAll(const PIByteArray & data);
void sendToAll(const PIString & data);
void sendToAll(const void * data, int size);
//! \~english Returns if receiving multicast packets is enabled
//! \~russian Возвращает включён ли приём мультикаст пакетов
bool isMulticastReceive() const { return !eths_mcast.isEmpty(); }
//! \~english Returns if receiving broadcast packets is enabled
//! \~russian Возвращает включён ли приём широковещательных пакетов
bool isBroadcastReceive() const { return !eths_bcast.isEmpty(); }
//! \~english Returns diagnostic service
//! \~russian Возвращает диагностический сервис
PIDiagnostics & diagnosticService() { return diag_s; }
//! \~english Returns diagnostic data
//! \~russian Возвращает диагностические данные
PIDiagnostics & diagnosticData() { return diag_d; }
//! \~english Returns all known peers
//! \~russian Возвращает всех известных пиров
const PIVector<PIPeer::PeerInfo> & allPeers() const { return peers; }
//! \~english Returns if peer with name "name" exists
//! \~russian Возвращает существует ли пир с именем "name"
bool isPeerExists(const PIString & name) const { return getPeerByName(name) != 0; }
//! \~english Returns peer info by name, or nullptr if not found
//! \~russian Возвращает информацию о пире по имени, или nullptr если не найден
const PeerInfo * getPeerByName(const PIString & name) const { return peers_map.value(name, 0); }
//! \~english Returns self info
//! \~russian Возвращает информацию о себе
const PeerInfo & selfInfo() const { return self_info; }
const PIMap<PIString, PIVector<PeerInfo *>> & _peerMap() const { return addresses_map; }
//! \~english Reinitialize peer network
//! \~russian Переинициализирует пиринговую сеть
void reinit();
//! \~english Lock peers list
//! \~russian Блокирует список пиров
void lock() { peers_mutex.lock(); }
//! \~english Unlock peers list
//! \~russian Разблокирует список пиров
void unlock() { peers_mutex.unlock(); }
//! \~english Change peer name to "new_name"
//! \~russian Изменяет имя пира на "new_name"
void changeName(const PIString & new_name);
//! \~english Returns trusted peer name
//! \~russian Возвращает имя доверенного пира
const PIString & trustPeerName() const { return trust_peer; }
//! \~english Set trusted peer name
//! \~russian Устанавливает имя доверенного пира
void setTrustPeerName(const PIString & peer_name) { trust_peer = peer_name; }
//! \~english Set TCP server IP address
//! \~russian Устанавливает IP адрес TCP сервера
void setTcpServerIP(const PIString & ip);
ssize_t bytesAvailable() const override;

View File

@@ -216,6 +216,8 @@ public:
//! \~russian Переключает состояние передачи в break
bool setBreak(bool enabled);
//! \~english Set VTime parameter
//! \~russian Устанавливает параметр VTime
void setVTime(int t) {
vtime = t;
applySettings();

View File

@@ -29,35 +29,59 @@
#include "piiodevice.h"
//! \ingroup IO
//! \~\brief
//! \~english SPI device.
//! \~russian Устройство SPI.
class PIP_EXPORT PISPI: public PIIODevice {
PIIODEVICE(PISPI, "spi");
public:
//! \~english Constructs %PISPI with path "path", speed "speed_hz" and mode "mode"
//! \~russian Создаёт %PISPI с путём "path", скоростью "speed_hz" и режимом "mode"
explicit PISPI(const PIString & path = PIString(), uint speed_hz = 1000000, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! \~english Destructor
//! \~russian Деструктор
virtual ~PISPI();
//! \brief Parameters of PISPI
//! \~english Parameters of PISPI
//! \~russian Параметры PISPI
enum Parameters {
ClockInverse /*! SPI clk polarity control*/ = 0x1,
ClockPhaseShift /*! SPI clk phase control */ = 0x2,
ClockInverse /*! \~english SPI clk polarity control \~russian Управление полярностью SPI clk */ = 0x1,
ClockPhaseShift /*! \~english SPI clk phase control \~russian Управление фазой SPI clk */ = 0x2,
};
//! \~english Set SPI speed in Hz
//! \~russian Устанавливает скорость SPI в Гц
void setSpeed(uint speed_hz);
//! \~english Returns current SPI speed in Hz
//! \~russian Возвращает текущую скорость SPI в Гц
uint speed() const { return spi_speed; }
//! \~english Set number of bits per word, default is 8
//! \~russian Устанавливает количество бит на слово, по умолчанию 8
void setBits(uchar bits = 8);
//! \~english Returns number of bits per word
//! \~russian Возвращает количество бит на слово
uchar bits() const { return spi_bits; }
//! Set parameters to "parameters_"
//! \~english Set parameters to "parameters_"
//! \~russian Устанавливает параметры в "parameters_"
void setParameters(PIFlags<PISPI::Parameters> parameters_) { spi_mode = (int)parameters_; }
//! Set parameter "parameter" to "on" state
//! \~english Set parameter "parameter" to "on" state
//! \~russian Устанавливает параметр "parameter" в состояние "on"
void setParameter(PISPI::Parameters parameter, bool on = true);
//! Returns if parameter "parameter" is set
//! \~english Returns if parameter "parameter" is set
//! \~russian Возвращает установлен ли параметр "parameter"
bool isParameterSet(PISPI::Parameters parameter) const;
//! Returns parameters
//! \~english Returns parameters
//! \~russian Возвращает параметры
PIFlags<PISPI::Parameters> parameters() const { return spi_mode; }
ssize_t bytesAvailable() const override;

View File

@@ -62,13 +62,24 @@
struct usb_dev_handle;
//! \ingroup IO
//! \~\brief
//! \~english USB device.
//! \~russian Устройство USB.
class PIP_USB_EXPORT PIUSB: public PIIODevice {
PIIODEVICE(PIUSB, "usb");
public:
//! \~english Constructs %PIUSB with vendor ID "vid" and product ID "pid"
//! \~russian Создаёт %PIUSB с идентификатором поставщика "vid" и идентификатором продукта "pid"
explicit PIUSB(ushort vid = 0, ushort pid = 0);
//! \~english Destructor
//! \~russian Деструктор
virtual ~PIUSB();
//! \~english USB endpoint structure
//! \~russian Структура USB endpoint
struct PIP_USB_EXPORT Endpoint {
Endpoint(uchar a = 0, uchar at = 0, ushort mps = 0) {
address = a;
@@ -111,6 +122,8 @@ public:
UsageType usage_type = DataEndpoint;
};
//! \~english USB interface structure
//! \~russian Структура USB интерфейса
struct PIP_USB_EXPORT Interface {
uchar index = 0;
uchar value_to_select = 0;
@@ -120,6 +133,8 @@ public:
PIVector<PIUSB::Endpoint> endpoints;
};
//! \~english USB configuration structure
//! \~russian Структура USB конфигурации
struct PIP_USB_EXPORT Configuration {
uchar index = 0;
uchar value_to_select = 0;
@@ -130,6 +145,8 @@ public:
PIVector<PIUSB::Interface> interfaces;
};
//! \~english USB device descriptor structure
//! \~russian Структура дескриптора USB устройства
struct PIP_USB_EXPORT Descriptor {
ushort usb_spec_number = 0;
uchar device_class = 0;
@@ -145,36 +162,100 @@ public:
PIVector<PIUSB::Configuration> configurations;
};
//! \~english Returns current device descriptor
//! \~russian Возвращает текущий дескриптор устройства
const PIUSB::Descriptor & currentDescriptor() const { return desc_; }
//! \~english Returns current configuration
//! \~russian Возвращает текущую конфигурацию
const PIUSB::Configuration & currentConfiguration() const { return conf_; }
//! \~english Returns current interface
//! \~russian Возвращает текущий интерфейс
const PIUSB::Interface & currentInterface() const { return iface_; }
//! \~english Returns vendor ID
//! \~russian Возвращает ID поставщика
ushort vendorID() const { return vid_; }
//! \~english Returns product ID
//! \~russian Возвращает ID продукта
ushort productID() const { return pid_; }
//! \~english Returns device number
//! \~russian Возвращает номер устройства
int deviceNumber() const { return property("deviceNumber").toInt(); }
//! \~english Returns read timeout in milliseconds
//! \~russian Возвращает таймаут чтения в миллисекундах
int timeoutRead() const { return property("timeoutRead").toInt(); }
//! \~english Returns write timeout in milliseconds
//! \~russian Возвращает таймаут записи в миллисекундах
int timeoutWrite() const { return property("timeoutWrite").toInt(); }
//! \~english Returns read endpoint
//! \~russian Возвращает endpoint для чтения
const PIUSB::Endpoint & endpointRead() const { return ep_read; }
//! \~english Returns write endpoint
//! \~russian Возвращает endpoint для записи
const PIUSB::Endpoint & endpointWrite() const { return ep_write; }
//! \~english Returns all endpoints
//! \~russian Возвращает все endpoints
const PIVector<PIUSB::Endpoint> & endpoints() const { return eps; }
//! \~english Returns read endpoints
//! \~russian Возвращает endpoints для чтения
PIVector<PIUSB::Endpoint> endpointsRead();
//! \~english Returns write endpoints
//! \~russian Возвращает endpoints для записи
PIVector<PIUSB::Endpoint> endpointsWrite();
//! \~english Returns endpoint by address
//! \~russian Возвращает endpoint по адресу
PIUSB::Endpoint getEndpointByAddress(uchar address);
//! \~english Set vendor ID to "vid"
//! \~russian Устанавливает ID поставщика в "vid"
void setVendorID(ushort vid);
//! \~english Set product ID to "pid"
//! \~russian Устанавливает ID продукта в "pid"
void setProductID(ushort pid);
//! \~english Set configuration by value
//! \~russian Устанавливает конфигурацию по значению
bool setConfiguration(uchar value);
//! \~english Set interface by value
//! \~russian Устанавливает интерфейс по значению
bool setInterface(uchar value);
//! \~english Set read endpoint
//! \~russian Устанавливает endpoint для чтения
void setEndpointRead(const PIUSB::Endpoint & ep) { ep_read = ep; }
//! \~english Set write endpoint
//! \~russian Устанавливает endpoint для записи
void setEndpointWrite(const PIUSB::Endpoint & ep) { ep_write = ep; }
//! \~english Set device number
//! \~russian Устанавливает номер устройства
void setDeviceNumber(int dn) { setProperty("deviceNumber", dn); }
//! \~english Set read timeout in milliseconds
//! \~russian Устанавливает таймаут чтения в миллисекундах
void setTimeoutRead(int t) { setProperty("timeoutRead", t); }
//! \~english Set write timeout in milliseconds
//! \~russian Устанавливает таймаут записи в миллисекундах
void setTimeoutWrite(int t) { setProperty("timeoutWrite", t); }
//! \~english Control write to device
//! \~russian Управляющая запись в устройство
int controlWrite(const void * data, int max_size);
virtual void flush() override;

View File

@@ -29,24 +29,39 @@
#include "picrc.h"
#include "pidiagnostics.h"
//! \~english Base class for reliable send and receive data in fixed packets with error correction, pause and resume
//! \~russian Базовый класс для надежного обмена данными с помощью фиксированных пакетов с коррекцией ошибок и паузой
class PIP_EXPORT PIBaseTransfer: public PIObject {
PIOBJECT_SUBCLASS(PIBaseTransfer, PIObject);
public:
//! \~english Constructs empty transfer
//! \~russian Создает пустой transfer
PIBaseTransfer();
//! \~english Destructor
//! \~russian Деструктор
~PIBaseTransfer();
#pragma pack(push, 1)
//! \~english Packet header structure
//! \~russian Структура заголовка пакета
struct PIP_EXPORT PacketHeader {
uint sig;
int type; // PacketType
int session_id;
uint id;
uint crc;
//! \~english Check if signature is valid
//! \~russian Проверка валидности подписи
bool check_sig() { return (sig == signature); }
};
//! \~english Part information structure
//! \~russian Структура информации о части
struct PIP_EXPORT Part {
//! \~english Constructor
//! \~russian Конструктор
Part(uint id_ = 0, ullong size_ = 0, ullong start_ = 0): id(id_), size(size_), start(start_) {}
uint id;
ullong size;
@@ -54,29 +69,76 @@ public:
};
#pragma pack(pop)
//! \~english Stop sending data
//! \~russian Остановить отправку данных
void stopSend();
//! \~english Stop receiving data
//! \~russian Остановить прием данных
void stopReceive();
//! \~english Check if currently sending
//! \~russian Проверка, идет ли отправка
bool isSending() const { return is_sending; }
//! \~english Check if currently receiving
//! \~russian Проверка, идет ли прием
bool isReceiving() const { return is_receiving; }
//! \~english Check if paused
//! \~russian Проверка, на паузе ли
bool isPause() const { return is_pause; }
//! \~english Set pause state
//! \~russian Установить состояние паузы
void setPause(bool pause_);
//! \~english Set packet size
//! \~russian Установить размер пакета
void setPacketSize(int size) { packet_size = size; }
//! \~english Get packet size
//! \~russian Получить размер пакета
int packetSize() const { return packet_size; }
//! \~english Set timeout in seconds
//! \~russian Установить таймаут в секундах
void setTimeout(double sec);
//! \~english Get timeout in seconds
//! \~russian Получить таймаут в секундах
double timeout() const { return timeout_; }
//! \~english Enable/disable CRC check
//! \~russian Включить/выключить проверку CRC
void setCRCEnabled(bool en = true) { crc_enabled = en; }
//! \~english Check if CRC is enabled
//! \~russian Проверка, включен ли CRC
bool isCRCEnabled() const { return crc_enabled; }
//! \~english Get state as string
//! \~russian Получить состояние в виде строки
PIString stateString() const { return state_string; }
//! \~english Get packet map as string
//! \~russian Получить карту пакетов в виде строки
PIString packetMap() const { return pm_string; }
//! \~english Get total bytes count
//! \~russian Получить общее количество байт
llong bytesAll() const { return bytes_all; }
//! \~english Get current bytes count
//! \~russian Получить текущее количество байт
llong bytesCur() const { return bytes_cur; }
//! \~english Get diagnostics object
//! \~russian Получить объект диагностики
const PIDiagnostics & diagnostic() { return diag; }
//! \~english Get packet signature
//! \~russian Получить подпись пакета
static uint packetSignature() { return signature; }
EVENT_HANDLER1(void, received, PIByteArray, data);

View File

@@ -31,13 +31,16 @@
#include "pip_io_utils_export.h"
//! \~english Broadcast for all interfaces, including loopback
//! \~russian Широкое вещание на все интерфейсы, включая loopback
class PIP_IO_UTILS_EXPORT PIBroadcast
: public PIThread
, public PIEthUtilBase {
PIOBJECT_SUBCLASS(PIBroadcast, PIThread);
public:
//! %PIBroadcast channels, can be used independently
//! \~english %PIBroadcast channels, can be used independently
//! \~russian Каналы %PIBroadcast, могут использоваться независимо
enum Channel {
Multicast /** Use multicast addresses */ = 0x01,
Broadcast /** Use broadcast addresses */ = 0x02,
@@ -45,75 +48,97 @@ public:
All /** Use all channels */ = 0xFFFF,
};
//! \~english Channels flags type
//! \~russian Тип флагов каналов
typedef PIFlags<Channel> Channels;
/** Contructs %PIBroadcast, if \"send_only\" not set
* all PIEthernets will be binded to receive data
* */
//! \~english Constructs %PIBroadcast. If "send_only" not set, all PIEthernets will be binded to receive data
//! \~russian Создает %PIBroadcast. Если "send_only" не установлен, все PIEthernets будут привязаны для приема данных
PIBroadcast(bool send_only = false);
//! \~english Destructor
//! \~russian Деструктор
~PIBroadcast();
//! Set channels to \"ch\" and queue to reinit
//! \~english Set channels to "ch" and queue to reinit
//! \~russian Установить каналы в "ch" и очередь на реинициализацию
void setChannels(Channels ch);
//! Returns channels
//! \~english Returns channels
//! \~russian Возвращает каналы
Channels channels() const { return _channels; }
//! Returns if is send_only
//! \~english Returns if is send_only
//! \~russian Возвращает, является ли отправка только
bool isSendOnly() const { return _send_only; }
//! Set multicast IP to \"mg\" and queue to reinit
//! \~english Set multicast IP to "mg" and queue to reinit
//! \~russian Установить multicast IP в "mg" и очередь на реинициализацию
void setMulticastGroup(const PIString & mg);
//! Returns multicast IP
//! \~english Returns multicast IP
//! \~russian Возвращает multicast IP
PIString multicastGroup() const { return mcast_address.ipString(); }
//! Set multicast port to \"port\" and queue to reinit
//! \~english Set multicast port to "port" and queue to reinit
//! \~russian Установить multicast порт в "port" и очередь на реинициализацию
void setMulticastPort(ushort port);
//! Returns multicast port
//! \~english Returns multicast port
//! \~russian Возвращает multicast порт
ushort multicastPort() const { return mcast_address.port(); }
//! Set multicast address to \"addr\" and queue to reinit
//! \~english Set multicast address to "addr" and queue to reinit
//! \~russian Установить multicast адрес в "addr" и очередь на реинициализацию
void setMulticastAddress(const PINetworkAddress & addr);
//! Returns multicast address
//! \~english Returns multicast address
//! \~russian Возвращает multicast адрес
PINetworkAddress multicastAddress() const { return mcast_address; }
//! Set broadcast port to \"port\" and queue to reinit
//! \~english Set broadcast port to "port" and queue to reinit
//! \~russian Установить broadcast порт в "port" и очередь на реинициализацию
void setBroadcastPort(ushort port);
//! Returns broadcast port
//! \~english Returns broadcast port
//! \~russian Возвращает broadcast порт
ushort broadcastPort() { return bcast_port; }
//! Set loopback start port to \"port\" and queue to reinit
//! \~english Set loopback start port to "port" and queue to reinit
//! \~russian Установить начальный loopback порт в "port" и очередь на реинициализацию
void setLoopbackPort(ushort port);
//! Returns loopback start port
//! \~english Returns loopback start port
//! \~russian Возвращает начальный loopback порт
ushort loopbackPort() { return lo_port; }
//! Set loopback ports count to \"count\" and queue to reinit
//! \~english Set loopback ports count to "count" and queue to reinit
//! \~russian Установить количество loopback портов в "count" и очередь на реинициализацию
void setLoopbackPortsCount(int count);
//! Returns loopback ports count
//! \~english Returns loopback ports count
//! \~russian Возвращает количество loopback портов
int loopbackPortsCount() const { return lo_pcnt; }
//! If not send_only starts all threaded reads
//! \~english If not send_only starts all threaded reads
//! \~russian Если не send_only запускает все потоковые чтения
void startRead();
//! Stop all threaded reads
//! \~english Stop all threaded reads
//! \~russian Остановить все потоковые чтения
void stopRead();
//! Reinit all PIEthernets with current \a PIEthernet::allAddresses()
//! \~english Reinit all PIEthernets with current \a PIEthernet::allAddresses()
//! \~russian Реинициализировать все PIEthernets с текущими \a PIEthernet::allAddresses()
void reinit();
//! Send packet
//! \~english Send packet
//! \~russian Отправить пакет
void send(const PIByteArray & data);
EVENT1(receiveEvent, PIByteArray, data);

View File

@@ -1,10 +1,13 @@
/*! \file piconnection.h
* \brief
* \ingroup IO-Utils
* \~\brief
* \~english Complex I/O point
* \~russian Составное устройство ввода/вывода
*/
//! \addtogroup IO-Utils
//! \{
//! \file piconnection.h
//! \brief
//! \~english Complex I/O point
//! \~russian Составное устройство ввода/вывода
//! \details
//! \~english This class provides a complex I/O point that can manage multiple devices, filters, channels, and senders.
//! \~russian Этот класс предоставляет составное устройство ввода/вывода, которое может управлять множеством устройств, фильтров, каналов и отправителей.
//! \~\}
/*
PIP - Platform Independent Primitives
Complex I/O point
@@ -32,269 +35,373 @@
class PIConfig;
//! \~english Complex I/O point
//! \~russian Составное устройство ввода/вывода
//! \details This class provides a complex I/O point that can manage multiple devices, filters, channels, and senders.
//! \~russian Этот класс предоставляет составное устройство ввода/вывода, которое может управлять множеством устройств, фильтров, каналов и отправителей.
class PIP_EXPORT PIConnection: public PIObject {
PIOBJECT_SUBCLASS(PIConnection, PIObject);
public:
//! Constructs connection with name "name", or with default name = "connection"
//! \~english Constructs connection with name "name", or with default name = "connection"
//! \~russian Создает подключение с именем "name" или с именем по умолчанию "connection"
PIConnection(const PIString & name = PIStringAscii("connection"));
//! Constructs connection and configure it from config file "config" from section "name"
//! \~english Constructs connection and configure it from config file "config" from section "name"
//! \~russian Создает подключение и настраивает его из файла конфигурации "config" из секции "name"
PIConnection(const PIString & config, const PIString & name);
//! Constructs connection and configure it from config content "string" from section "name"
//! \~english Constructs connection and configure it from config content "string" from section "name"
//! \~russian Создает подключение и настраивает его из содержимого конфигурации "string" из секции "name"
PIConnection(PIString * string, const PIString & name);
//! \~english Destructor
//! \~russian Деструктор
~PIConnection();
/*! \brief Configure connection from config file "config" from section "name". Returns if configuration was successful
* \details \b Warning: all devices, filters and channels removed before configure! */
//! \~english Configure connection from config file "config" from section "name". Returns if configuration was successful
//! \~russian Настраивает подключение из файла конфигурации "config" из секции "name". Возвращает успешность настройки
//! \details \b Warning: all devices, filters and channels removed before configure!
//! \~russian \b Внимание: все устройства, фильтры и каналы удаляются перед настройкой!
bool configureFromConfig(const PIString & config, const PIString & name = PIStringAscii("connection"));
/*! \brief Configure connection from config content "string" from section "name". Returns if configuration was successful
* \details \b Warning: all devices, filters and channels removed before configure! */
//! \~english Configure connection from config content "string" from section "name". Returns if configuration was successful
//! \~russian Настраивает подключение из содержимого конфигурации "string" из секции "name". Возвращает успешность настройки
//! \details \b Warning: all devices, filters and channels removed before configure!
//! \~russian \b Внимание: все устройства, фильтры и каналы удаляются перед настройкой!
bool configureFromString(PIString * string, const PIString & name = PIStringAscii("connection"));
//! Returns config file section of current connection configuration
//! \~english Returns config file section of current connection configuration
//! \~russian Возвращает секцию файла конфигурации текущей конфигурации подключения
PIString makeConfig() const;
/*! \brief Add device with full path "full_path", open mode "mode" to Device pool and connection
* \details Returns pointer to device or null if device can not be created. If "start" is true,
* read thread is started immediately. Else, you can start read thread with functions \a startThreadedRead()
* or \a startAllThreadedReads(). By default, read thread doesn`t start */
//! \~english Add device with full path "full_path", open mode "mode" to Device pool and connection
//! \~russian Добавляет устройство с полным путем "full_path", режимом открытия "mode" в пул устройств и подключение
//! \details Returns pointer to device or null if device can not be created. If "start" is true,
//! \~english read thread is started immediately. Else, you can start read thread with functions \a startThreadedRead()
//! \~russian поток чтения запускается немедленно. В противном случае можно запустить поток чтения с помощью функций \a startThreadedRead()
//! \~english or \a startAllThreadedReads(). By default, read thread doesn`t start
//! \~russian или \a startAllThreadedReads(). По умолчанию поток чтения не запускается
PIIODevice * addDevice(const PIString & full_path, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite, bool start = false);
//! \~english Sets the name for device "dev"
//! \~russian Устанавливает имя для устройства "dev"
void setDeviceName(PIIODevice * dev, const PIString & name);
//! \~english Returns list of device names
//! \~russian Возвращает список имен устройств
PIStringList deviceNames(const PIIODevice * dev) const;
/*! \brief Remove device with full path "full_path" from connection
* \details Returns if device was removed. If there is no connection bounded to this device,
* it will be removed from Device pool */
//! \~english Remove device with full path "full_path" from connection
//! \~russian Удаляет устройство с полным путем "full_path" из подключения
//! \details Returns if device was removed. If there is no connection bounded to this device,
//! \~english it will be removed from Device pool
//! \~russian оно будет удалено из пула устройств
bool removeDevice(const PIString & full_path);
/*! \brief Remove all device from connection
* \details If there is no connection bounded to there devices, they removed from Device pool */
//! \~english Remove all device from connection
//! \~russian Удаляет все устройства из подключения
//! \details If there is no connection bounded to there devices, they removed from Device pool
//! \~russian Если к устройствам не привязано подключение, они удаляются из пула устройств
void removeAllDevices();
//! Returns device with full path "full_path" or null if there is no such device
//! \~english Returns device with full path "full_path" or null if there is no such device
//! \~russian Возвращает устройство с полным путем "full_path" или null, если такого устройства нет
PIIODevice * deviceByFullPath(const PIString & full_path) const;
//! Returns device with name "name" or null if there is no such device
//! \~english Returns device with name "name" or null if there is no such device
//! \~russian Возвращает устройство с именем "name" или null, если такого устройства нет
PIIODevice * deviceByName(const PIString & name) const;
//! Returns all devices bounded to this connection
//! \~english Returns all devices bounded to this connection
//! \~russian Возвращает все устройства, привязанные к этому подключению
PIVector<PIIODevice *> boundedDevices() const;
/*! \brief Add filter with name "name" to device with full path "full_path_name" or filter "full_path_name"
* \details If there is no filter with name "name", connection create new with split mode "mode" and bound
* to it device "full_path_name" or filter "full_path_name". If filter with name "name" already exists,
* device "full_path_name" or filter "full_path_name" add to this filter.
* This function returns PIPacketExtractor * assosiated with this filter
* \n \b Attention! "mode" is altual olny if new filter was created! */
//! \~english Add filter with name "name" to device with full path "full_path_name" or filter "full_path_name"
//! \~russian Добавляет фильтр с именем "name" к устройству с полным путем "full_path_name" или фильтру "full_path_name"
//! \details If there is no filter with name "name", connection create new with split mode "mode" and bound
//! \~english to it device "full_path_name" or filter "full_path_name". If filter with name "name" already exists,
//! \~english device "full_path_name" or filter "full_path_name" add to this filter.
//! \~russian к нему устройство "full_path_name" или фильтр "full_path_name". Если фильтр с именем "name" уже существует,
//! \~english This function returns PIPacketExtractor * assosiated with this filter
//! \~russian устройство "full_path_name" или фильтр "full_path_name" добавляется к этому фильтру.
//! \~english This function returns PIPacketExtractor * assosiated with this filter
//! \~russian Эта функция возвращает PIPacketExtractor *, связанный с этим фильтром
//! \~\sa PIPacketExtractor
//! \details \b Attention! "mode" is altual olny if new filter was created!
//! \~russian \b Внимание! "mode" актуален только если был создан новый фильтр!
PIPacketExtractor *
addFilter(const PIString & name, const PIString & full_path_name, PIPacketExtractor::SplitMode mode = PIPacketExtractor::None);
//! Add filter with name "name" to device "dev"
//! \~english Add filter with name "name" to device "dev"
//! \~russian Добавляет фильтр с именем "name" к устройству "dev"
PIPacketExtractor *
addFilter(const PIString & name, const PIIODevice * dev, PIPacketExtractor::SplitMode mode = PIPacketExtractor::None) {
return addFilter(name, devFPath(dev), mode);
}
//! Add filter with "filter" to device "dev"
//! \~english Add filter with "filter" to device "dev"
//! \~russian Добавляет фильтр "filter" к устройству "dev"
PIPacketExtractor * addFilter(PIPacketExtractor * filter, const PIString & full_path_name);
//! Add filter with "filter" to device "dev"
//! \~english Add filter with "filter" to device "dev"
//! \~russian Добавляет фильтр "filter" к устройству "dev"
PIPacketExtractor * addFilter(PIPacketExtractor * filter, const PIIODevice * dev) { return addFilter(filter, devFPath(dev)); }
/*! \brief Remove from filter with name "name" device with full path "full_path_name" or filter "full_path_name"
* \details If there is no devices bounded to this filter, it will be removed. Returns if device was removed */
//! \~english Remove from filter with name "name" device with full path "full_path_name" or filter "full_path_name"
//! \~russian Удаляет из фильтра с именем "name" устройство с полным путем "full_path_name" или фильтр "full_path_name"
//! \details If there is no devices bounded to this filter, it will be removed. Returns if device was removed
//! \~russian Если к этому фильтру не привязано устройств, он будет удален. Возвращает успешность удаления устройства
bool removeFilter(const PIString & name, const PIString & full_path_name);
//! Remove from filter with name "name" device or filter "dev"
//! \~english Remove from filter with name "name" device or filter "dev"
//! \~russian Удаляет из фильтра с именем "name" устройство или фильтр "dev"
bool removeFilter(const PIString & name, const PIIODevice * dev);
//! Remove filter with name "name". Returns if filter was removed
//! \~english Remove filter with name "name". Returns if filter was removed
//! \~russian Удаляет фильтр с именем "name". Возвращает успешность удаления фильтра
bool removeFilter(const PIString & name);
//! Remove all filters from connection
//! \~english Remove all filters from connection
//! \~russian Удаляет все фильтры из подключения
void removeAllFilters();
//! Returns all filters of connection
//! \~english Returns all filters of connection
//! \~russian Возвращает все фильтры подключения
PIVector<PIPacketExtractor *> filters() const;
//! Returns all filter names of connection
//! \~english Returns all filter names of connection
//! \~russian Возвращает все имена фильтров подключения
PIStringList filterNames() const;
//! Returns PIPacketExtractor * assosiated with filter "name" or null if there is no such filter
//! \~english Returns PIPacketExtractor * assosiated with filter "name" or null if there is no such filter
//! \~russian Возвращает PIPacketExtractor *, связанный с фильтром "name" или null, если такого фильтра нет
PIPacketExtractor * filter(const PIString & name) const;
//! Returns all devices bounded to filter "name"
//! \~english Returns all devices bounded to filter "name"
//! \~russian Возвращает все устройства, привязанные к фильтру "name"
PIVector<PIIODevice *> filterBoundedDevices(const PIString & name) const;
/*! \brief Add to connection channel from "name_from" to "name_to"
* \details "name_from" and "name_to" can be full pathes of devices or device names or filter names.
* Returns \b false if there if no such device or filter, else create channel and returns \b true */
//! \~english Add to connection channel from "name_from" to "name_to"
//! \~russian Добавляет в подключение канал от "name_from" к "name_to"
//! \details "name_from" and "name_to" can be full pathes of devices or device names or filter names.
//! \~english Returns \b false if there if no such device or filter, else create channel and returns \b true
//! \~russian Возвращает \b false, если такого устройства или фильтра нет, иначе создает канал и возвращает \b true
bool addChannel(const PIString & name_from, const PIString & name_to);
//! Add to connection channel from "name_from" to "dev_to"
//! \~english Add to connection channel from "name_from" to "dev_to"
//! \~russian Добавляет в подключение канал от "name_from" к "dev_to"
bool addChannel(const PIString & name_from, const PIIODevice * dev_to) { return addChannel(name_from, devFPath(dev_to)); }
//! Add to connection channel from "dev_from" to "name_to"
//! \~english Add to connection channel from "dev_from" to "name_to"
//! \~russian Добавляет в подключение канал от "dev_from" к "name_to"
bool addChannel(const PIIODevice * dev_from, const PIString & name_to) { return addChannel(devFPath(dev_from), name_to); }
//! Add to connection channel from "dev_from" to "dev_to"
//! \~english Add to connection channel from "dev_from" to "dev_to"
//! \~russian Добавляет в подключение канал от "dev_from" к "dev_to"
bool addChannel(const PIIODevice * dev_from, const PIIODevice * dev_to) { return addChannel(devFPath(dev_from), devFPath(dev_to)); }
/*! \brief Remove from connection channel from "name_from" to "name_to"
* \details "name_from" and "name_to" can be full pathes of devices or filter names.
* Returns \b false if there if no such device or filter, else remove channel and returns \b true */
//! \~english Remove from connection channel from "name_from" to "name_to"
//! \~russian Удаляет из подключения канал от "name_from" к "name_to"
//! \details "name_from" and "name_to" can be full pathes of devices or filter names.
//! \~english Returns \b false if there if no such device or filter, else remove channel and returns \b true
//! \~russian Возвращает \b false, если такого устройства или фильтра нет, иначе удаляет канал и возвращает \b true
bool removeChannel(const PIString & name_from, const PIString & name_to);
//! Remove from connection channel from "name_from" to "dev_to"
//! \~english Remove from connection channel from "name_from" to "dev_to"
//! \~russian Удаляет из подключения канал от "name_from" к "dev_to"
bool removeChannel(const PIString & name_from, const PIIODevice * dev_to) { return removeChannel(name_from, devFPath(dev_to)); }
//! Remove from connection channel from "dev_from" to "name_to"
//! \~english Remove from connection channel from "dev_from" to "name_to"
//! \~russian Удаляет из подключения канал от "dev_from" к "name_to"
bool removeChannel(const PIIODevice * dev_from, const PIString & name_to) { return removeChannel(devFPath(dev_from), name_to); }
//! Remove from connection channel from "dev_from" to "dev_to"
//! \~english Remove from connection channel from "dev_from" to "dev_to"
//! \~russian Удаляет из подключения канал от "dev_from" к "dev_to"
bool removeChannel(const PIIODevice * dev_from, const PIIODevice * dev_to) {
return removeChannel(devFPath(dev_from), devFPath(dev_to));
}
/*! \brief Remove from connection all channels from "name_from"
* \details "name_from" can be full path of device or filter name.
* Returns \b false if there if no such device or filter, else remove channels and returns \b true */
//! \~english Remove from connection all channels from "name_from"
//! \~russian Удаляет из подключения все каналы от "name_from"
//! \details "name_from" can be full path of device or filter name.
//! \~english Returns \b false if there if no such device or filter, else remove channels and returns \b true
//! \~russian Возвращает \b false, если такого устройства или фильтра нет, иначе удаляет каналы и возвращает \b true
bool removeChannel(const PIString & name_from);
//! Remove from connection all channels from "dev_from"
//! \~english Remove from connection all channels from "dev_from"
//! \~russian Удаляет из подключения все каналы от "dev_from"
bool removeChannel(const PIIODevice * dev_from) { return removeChannel(devFPath(dev_from)); }
//! Remove from connection all channels
//! \~english Remove from connection all channels
//! \~russian Удаляет из подключения все каналы
void removeAllChannels();
//! Returns all channels of this connection as full pathes or filter names pair array (from, to)
//! \~english Returns all channels of this connection as full pathes or filter names pair array (from, to)
//! \~russian Возвращает все каналы этого подключения в виде массива пар полных путей или имен фильтров (от, до)
PIVector<PIPair<PIString, PIString>> channels() const;
/*! \brief Add to connection sender with name "name" device with full path "full_path"
* \details If there is no sender with name "name", connection create new, bound
* to it device "full_path_name" and start sender timer with frequency "frequency".
* If sender with name "name" already exists, device "full_path_name" add to this sender
* If "start" is true, sender is started immediately. Else, you can start sender with
* functions \a startSender()
* \n \b Attention! "frequency" is actual olny if new sender was created! */
//! \~english Add to connection sender with name "name" device with full path "full_path"
//! \~russian Добавляет в подключение отправителя с именем "name" устройство с полным путем "full_path"
//! \details If there is no sender with name "name", connection create new, bound
//! \~english to it device "full_path_name" and start sender timer with frequency "frequency".
//! \~russian к нему устройство "full_path_name" и запускает таймер отправителя с частотой "frequency".
//! \~english If sender with name "name" already exists, device "full_path_name" add to this sender
//! \~russian Если отправитель с именем "name" уже существует, устройство "full_path_name" добавляется к этому отправителю
//! \~english If "start" is true, sender is started immediately. Else, you can start sender with
//! \~russian Если "start" равно true, отправитель запускается немедленно. В противном случае можно запустить отправителя с помощью
//! \~english functions \a startSender()
//! \~russian функций \a startSender()
//! \~\sa startSender()
//! \details \b Attention! "frequency" is actual olny if new sender was created!
//! \~russian \b Внимание! "frequency" актуален только если был создан новый отправитель!
void addSender(const PIString & name, const PIString & full_path_name, float frequency, bool start = false);
//! Add to connection sender with name "name" device "dev"
//! \~english Add to connection sender with name "name" device "dev"
//! \~russian Добавляет в подключение отправителя с именем "name" устройство "dev"
void addSender(const PIString & name, const PIIODevice * dev, float frequency, bool start = false) {
addSender(name, devFPath(dev), frequency, start);
}
/*! \brief Remove from sender with name "name" device with full path "full_path_name"
* \details If there is no devices bounded to this sender, it will be removed. Returns if sender was removed */
//! \~english Remove from sender with name "name" device with full path "full_path_name"
//! \~russian Удаляет из отправителя с именем "name" устройство с полным путем "full_path_name"
//! \details If there is no devices bounded to this sender, it will be removed. Returns if sender was removed
//! \~russian Если к этому отправителю не привязано устройств, он будет удален. Возвращает успешность удаления отправителя
bool removeSender(const PIString & name, const PIString & full_path_name);
//! Remove from sender with name "name" device "dev"
//! \~english Remove from sender with name "name" device "dev"
//! \~russian Удаляет из отправителя с именем "name" устройство "dev"
bool removeSender(const PIString & name, const PIIODevice * dev) { return removeSender(name, devFPath(dev)); }
//! Remove sender with name "name", returns if sender was removed
//! \~english Remove sender with name "name", returns if sender was removed
//! \~russian Удаляет отправителя с именем "name", возвращает успешность удаления отправителя
bool removeSender(const PIString & name);
//! Set sender "name" fixed send data "data", returns if sender exists
//! \~english Set sender "name" fixed send data "data", returns if sender exists
//! \~russian Устанавливает фиксированные данные отправки для отправителя "name", возвращает существование отправителя
bool setSenderFixedData(const PIString & name, const PIByteArray & data);
//! Remove sender "name" fixed send data, returns if sender exists
//! \~english Remove sender "name" fixed send data, returns if sender exists
//! \~russian Удаляет фиксированные данные отправки для отправителя "name", возвращает существование отправителя
bool clearSenderFixedData(const PIString & name);
//! Returns sender "name" fixed send data
//! \~english Returns sender "name" fixed send data
//! \~russian Возвращает фиксированные данные отправки для отправителя "name"
PIByteArray senderFixedData(const PIString & name) const;
//! Returns sender "name" timer frequency, -1 if there is no such sender, or 0 if sender is not started yet
//! \~english Returns sender "name" timer frequency, -1 if there is no such sender, or 0 if sender is not started yet
//! \~russian Возвращает частоту таймера отправителя "name", -1 если такого отправителя нет, или 0 если отправитель еще не запущен
float senderFrequency(const PIString & name) const;
//! Remove from connection all senders
//! \~english Remove from connection all senders
//! \~russian Удаляет из подключения всех отправителей
void removeAllSenders();
//! Start read thread of device with full path "full_path"
//! \~english Start read thread of device with full path "full_path"
//! \~russian Запускает поток чтения устройства с полным путем "full_path"
void startThreadedRead(const PIString & full_path_name);
//! Start read thread of device "dev"
//! \~english Start read thread of device "dev"
//! \~russian Запускает поток чтения устройства "dev"
void startThreadedRead(const PIIODevice * dev) { startThreadedRead(devFPath(dev)); }
//! Start read threads of all Device pool device
//! \~english Start read threads of all Device pool device
//! \~russian Запускает потоки чтения всех устройств пула устройств
void startAllThreadedReads();
//! Start sender "name" timer
//! \~english Start sender "name" timer
//! \~russian Запускает таймер отправителя "name"
void startSender(const PIString & name);
//! Start all senders timers
//! \~english Start all senders timers
//! \~russian Запускает все таймеры отправителей
void startAllSenders();
//! Start all read threads and senders
//! \~english Start all read threads and senders
//! \~russian Запускает все потоки чтения и отправителей
void start() {
startAllThreadedReads();
startAllSenders();
}
//! Stop read thread of device with full path "full_path"
//! \~english Stop read thread of device with full path "full_path"
//! \~russian Останавливает поток чтения устройства с полным путем "full_path"
void stopThreadedRead(const PIString & full_path_name);
//! Stop read thread of device "dev"
//! \~english Stop read thread of device "dev"
//! \~russian Останавливает поток чтения устройства "dev"
void stopThreadedRead(const PIIODevice * dev) { stopThreadedRead(devFPath(dev)); }
//! Stop read threads of all Device pool device
//! \~english Stop read threads of all Device pool device
//! \~russian Останавливает потоки чтения всех устройств пула устройств
void stopAllThreadedReads();
//! Stop sender "name" timer
//! \~english Stop sender "name" timer
//! \~russian Останавливает таймер отправителя "name"
void stopSender(const PIString & name);
//! Stop all senders timers
//! \~english Stop all senders timers
//! \~russian Останавливает все таймеры отправителей
void stopAllSenders();
//! Stop all read threads and senders
//! \~english Stop all read threads and senders
//! \~russian Останавливает все потоки чтения и отправителей
void stop() {
stopAllThreadedReads();
stopAllSenders();
}
//! Stop connection and remove all devices
//! \~english Stop connection and remove all devices
//! \~russian Останавливает подключение и удаляет все устройства
void destroy() {
stop();
removeAllDevices();
}
//! Returns if there are no devices in this connection
//! \~english Returns if there are no devices in this connection
//! \~russian Возвращает, если в этом подключении нет устройств
bool isEmpty() const { return device_modes.isEmpty(); }
//! Returns PIDiagnostics * assosiated with device with full path "full_path_name", name "full_path_name" or filter "full_path_name"
//! \~english Returns PIDiagnostics * assosiated with device with full path "full_path_name", name "full_path_name" or filter "full_path_name"
//! \~russian Возвращает PIDiagnostics *, связанный с устройством с полным путем "full_path_name", именем "full_path_name" или фильтром "full_path_name"
PIDiagnostics * diagnostic(const PIString & full_path_name) const;
//! Returns PIDiagnostics * assosiated with device or filter "dev"
//! \~english Returns PIDiagnostics * assosiated with device or filter "dev"
//! \~russian Возвращает PIDiagnostics *, связанный с устройством или фильтром "dev"
PIDiagnostics * diagnostic(const PIIODevice * dev) const { return diags_.value(const_cast<PIIODevice *>(dev), 0); }
//! Write data "data" to device with full path "full_path" and returns result of \a write() function of device
//! \~english Write data "data" to device with full path "full_path" and returns result of \a write() function of device
//! \~russian Записывает данные "data" в устройство с полным путем "full_path" и возвращает результат функции \a write() устройства
int writeByFullPath(const PIString & full_path, const PIByteArray & data);
//! Write data "data" to device with name "name" and returns result of \a write() function of device
//! \~english Write data "data" to device with name "name" and returns result of \a write() function of device
//! \~russian Записывает данные "data" в устройство с именем "name" и возвращает результат функции \a write() устройства
int writeByName(const PIString & name, const PIByteArray & data);
//! Write data "data" to device "dev" and returns result of \a write() function of device
//! \~english Write data "data" to device "dev" and returns result of \a write() function of device
//! \~russian Записывает данные "data" в устройство "dev" и возвращает результат функции \a write() устройства
int write(PIIODevice * dev, const PIByteArray & data);
//! Returns all connections in application
//! \~english Returns all connections in application
//! \~russian Возвращает все подключения в приложении
static PIVector<PIConnection *> allConnections();
//! Returns all devices in Device pool
//! \~english Returns all devices in Device pool
//! \~russian Возвращает все устройства в пуле устройств
static PIVector<PIIODevice *> allDevices();
//! Set Device pool fake mode to \"yes\" and returns previous mode
//! \~english Set Device pool fake mode to \"yes\" and returns previous mode
//! \~russian Устанавливает фальшивый режим пула устройств в \"yes\" и возвращает предыдущий режим
static bool setFakeMode(bool yes);
//! Returns if Device pool works in fake mode
//! \~english Returns if Device pool works in fake mode
//! \~russian Возвращает, работает ли пул устройств в фальшивом режиме
static bool isFakeMode();
//! \~english Device pool class
//! \~russian Класс пула устройств
class PIP_EXPORT DevicePool: public PIThread {
PIOBJECT_SUBCLASS(DevicePool, PIThread);
friend void __DevicePool_threadReadDP(void * ddp);
@@ -314,54 +421,95 @@ public:
void unboundConnection(PIConnection * parent);
PIIODevice * device(const PIString & fp) const;
DeviceData * deviceData(PIIODevice * d) const;
//! \~english Returns list of bounded connections
//! \~russian Возвращает список привязанных подключений
PIVector<PIConnection *> boundedConnections() const;
//! \~english Returns list of bounded devices
//! \~russian Возвращает список привязанных устройств
PIVector<PIIODevice *> boundedDevices() const;
//! \~english Returns list of bounded devices for specific parent connection
//! \~russian Возвращает список привязанных устройств для конкретного родительского подключения
PIVector<PIIODevice *> boundedDevices(const PIConnection * parent) const;
protected:
//! \~english Device data structure
//! \~russian Структура данных устройства
struct PIP_EXPORT DeviceData {
//! \~english Constructor
//! \~russian Конструктор
DeviceData(): dev(0), rthread(0), started(false) {}
//! \~english Destructor
//! \~russian Деструктор
~DeviceData();
//! \~english Device pointer
//! \~russian Указатель на устройство
PIIODevice * dev;
//! \~english Read thread pointer
//! \~russian Указатель на поток чтения
PIThread * rthread;
//! \~english Started flag
//! \~russian Флаг запуска
bool started;
//! \~english List of listeners
//! \~russian Список слушателей
PIVector<PIConnection *> listeners;
};
//! \~english Thread execution function
//! \~russian Функция выполнения потока
void run() override;
//! \~english Called when device is read
//! \~russian Вызывается при чтении устройства
void deviceReaded(DeviceData * dd, const PIByteArray & data);
//! \~english Map of devices
//! \~russian Карта устройств
PIMap<PIString, DeviceData *> devices;
//! \~english Fake mode flag
//! \~russian Флаг фальшивого режима
bool fake;
};
//! \~english Data received event
//! \~russian Событие получения данных
EVENT2(dataReceivedEvent, const PIString &, from, const PIByteArray &, data);
//! \~english Packet received event
//! \~russian Событие получения пакета
EVENT2(packetReceivedEvent, const PIString &, from, const PIByteArray &, data);
//! \~english Quality changed event
//! \~russian Событие изменения качества
EVENT3(qualityChanged, const PIIODevice *, dev, PIDiagnostics::Quality, new_quality, PIDiagnostics::Quality, old_quality);
//! \events
//! \~english Events
//! \~russian События
//! \{
//! \fn void dataReceivedEvent(const PIString & from, const PIByteArray & data)
//! \brief Raise on data received from device with full path "from"
//! \~english Raise on data received from device with full path "from"
//! \~russian Возникает при получении данных от устройства с полным путем "from"
//! \fn void packetReceivedEvent(const PIString & from, const PIByteArray & data)
//! \brief Raise on packet received from filter with name "from"
//! \~english Raise on packet received from filter with name "from"
//! \~russian Возникает при получении пакета от фильтра с именем "from"
//! \fn void qualityChanged(const PIIODevice * device, PIDiagnostics::Quality new_quality, PIDiagnostics::Quality old_quality)
//! \brief Raise on diagnostic quality of device "device" changed from "old_quality" to "new_quality"
//! \~english Raise on diagnostic quality of device "device" changed from "old_quality" to "new_quality"
//! \~russian Возникает при изменении диагностического качества устройства "device" с "old_quality" на "new_quality"
//! \}
protected:
//! Executes on data received from device with full path "from"
//! \~english Executes on data received from device with full path "from"
//! \~russian Выполняется при получении данных от устройства с полным путем "from"
virtual void dataReceived(const PIString & from, const PIByteArray & data) {}
//! Executes on packet received from filter with name "from"
//! \~english Executes on packet received from filter with name "from"
//! \~russian Выполняется при получении пакета от фильтра с именем "from"
virtual void packetReceived(const PIString & from, const PIByteArray & data) {}
//! You should returns data for sender "sender_name"
//! \~english You should returns data for sender "sender_name"
//! \~russian Вы должны возвращать данные для отправителя "sender_name"
virtual PIByteArray senderData(const PIString & sender_name);
private:

View File

@@ -29,14 +29,26 @@
#include "pibasetransfer.h"
//! \~english Class for send and receive PIByteArray via \a PIBaseTransfer
//! \~russian Класс для отправки и приема PIByteArray с помощью \a PIBaseTransfer
class PIP_EXPORT PIDataTransfer: public PIBaseTransfer {
PIOBJECT_SUBCLASS(PIDataTransfer, PIBaseTransfer);
public:
//! \~english Constructs empty data transfer
//! \~russian Создает пустой transfer данных
PIDataTransfer() { ; }
//! \~english Destructor
//! \~russian Деструктор
~PIDataTransfer() { ; }
//! \~english Send data
//! \~russian Отправить данные
bool send(const PIByteArray & ba);
//! \~english Get received data
//! \~russian Получить принятые данные
const PIByteArray & data() { return data_; }
private:

View File

@@ -1,9 +1,13 @@
/*! \file pidiagnostics.h
* \ingroup IO
* \~\brief
* \~english Connection quality diagnostics
* \~russian Диагностика качества связи
*/
//! \addtogroup IO
//! \{
//! \file pidiagnostics.h
//! \brief
//! \~english Connection quality diagnostics
//! \~russian Диагностика качества связи
//! \details
//! \~english This class provides connection quality diagnostics based on packet reception statistics
//! \~russian Класс обеспечивает диагностику качества связи на основе статистики приема пакетов
//! \~\}
/*
PIP - Platform Independent Primitives
Speed and quality in/out diagnostics
@@ -30,6 +34,8 @@
#include "pitimer.h"
//! \~english Connection quality diagnostics class based on packet statistics
//! \~russian Класс диагностики качества связи на основе статистики пакетов
class PIP_EXPORT PIDiagnostics: public PITimer {
PIOBJECT_SUBCLASS(PIDiagnostics, PITimer);
friend class PIConnection;
@@ -37,118 +43,215 @@ class PIP_EXPORT PIDiagnostics: public PITimer {
public:
NO_COPY_CLASS(PIDiagnostics);
//! Constructs an empty diagnostics and if "start_" start it
//! \~english Constructs an empty diagnostics and if "start_" start it
//! \~russian Создает пустую диагностику и запускает её, если "start_" установлено
PIDiagnostics(bool start_ = true);
//! \~english Virtual destructor
//! \~russian Виртуальный деструктор
virtual ~PIDiagnostics();
//! Connection quality
//! \~english Connection quality enumeration
//! \~russian Перечисление качества связи
enum Quality {
Unknown /** Unknown, no one packet received yet */ = 1,
Failure /** No connection, no one correct packet received for last period */ = 2,
Bad /** Bad connection, correct packets received <= 20% */ = 3,
Average /** Average connection, correct packets received > 20% and <= 80% */ = 4,
Good /** Good connection, correct packets received > 80% */ = 5
//! \~english Unknown, no one packet received yet
//! \~russian Неизвестно, еще не получен ни один пакет
Unknown = 1,
//! \~english No connection, no one correct packet received for last period
//! \~russian Нет соединения, за последний период не получено ни одного корректного пакета
Failure = 2,
//! \~english Bad connection, correct packets received <= 20%
//! \~russian Плохое соединение, корректных пакетов получено <= 20%
Bad = 3,
//! \~english Average connection, correct packets received > 20% and <= 80%
//! \~russian Среднее соединение, корректных пакетов получено > 20% и <= 80%
Average = 4,
//! \~english Good connection, correct packets received > 80%
//! \~russian Хорошее соединение, корректных пакетов получено > 80%
Good = 5
};
//! Information about current diagnostics state
//! \~english Information about current diagnostics state
//! \~russian Информация о текущем состоянии диагностики
struct PIP_EXPORT State {
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
State();
//! \~english Immediate frequency in Hz
//! \~russian Мгновенная частота в Гц
float immediate_freq = 0.f;
//! \~english Integral frequency in Hz
//! \~russian Интегральная частота в Гц
float integral_freq = 0.f;
//! \~english Received packets per second
//! \~russian Принятых пакетов в секунду
ullong received_packets_per_sec = 0ull;
//! \~english Total received packets count
//! \~russian Общее количество принятых пакетов
ullong received_packets = 0ull;
//! \~english Total received wrong packets count
//! \~russian Общее количество неправильно принятых пакетов
ullong received_packets_wrong = 0ull;
//! \~english Received bytes per second
//! \~russian Принятых байт в секунду
ullong received_bytes_per_sec = 0ull;
//! \~english Total received bytes count
//! \~russian Общее количество принятых байт
ullong received_bytes = 0ull;
//! \~english Total received wrong bytes count
//! \~russian Общее количество неправильно принятых байт
ullong received_bytes_wrong = 0ull;
//! \~english Sended packets per second
//! \~russian Отправленных пакетов в секунду
ullong sended_packets_per_sec = 0ull;
//! \~english Total sended packets count
//! \~russian Общее количество отправленных пакетов
ullong sended_packets = 0ull;
//! \~english Sended bytes per second
//! \~russian Отправленных байт в секунду
ullong sended_bytes_per_sec = 0ull;
//! \~english Total sended bytes count
//! \~russian Общее количество отправленных байт
ullong sended_bytes = 0ull;
//! \~english Receive speed string in format "n {B|kB|MB|GB|TB}/s"
//! \~russian Строка скорости приема в формате "n {B|kB|MB|GB|TB}/s"
PIString receive_speed;
//! \~english Send speed string in format "n {B|kB|MB|GB|TB}/s"
//! \~russian Строка скорости отправки в формате "n {B|kB|MB|GB|TB}/s"
PIString send_speed;
//! \~english Current connection quality
//! \~russian Текущее качество соединения
PIDiagnostics::Quality quality = PIDiagnostics::Unknown;
};
//! Returns current state
//! \~english Returns current diagnostics state
//! \~russian Возвращает текущее состояние диагностики
PIDiagnostics::State state() const;
//! Returns period of full disconnect in seconds and period of averaging frequency
//! \~english Returns period of full disconnect in seconds and period of averaging frequency
//! \~russian Возвращает период полного отключения в секундах и период усреднения частоты
PISystemTime disconnectTimeout() const { return disconn_; }
//! Returns period of full disconnect in seconds and period of averaging frequency
//! \~english Set disconnect timeout (deprecated, use PISystemTime version)
//! \~russian Установить таймаут отключения (устаревший, используйте версию с PISystemTime)
void setDisconnectTimeout(float s) DEPRECATEDM("use setDisconnectTimeout(PISystemTime)") {
setDisconnectTimeout(PISystemTime::fromSeconds(s));
}
//! Returns period of full disconnect and period of averaging frequency
//! \~english Set disconnect timeout
//! \~russian Установить таймаут отключения
void setDisconnectTimeout(PISystemTime tm) { setProperty("disconnectTimeout", tm); }
//! Returns connection quality
//! \~english Returns connection quality
//! \~russian Возвращает качество соединения
PIDiagnostics::Quality quality() const;
//! Returns receive speed in format "n {B|kB|MB|GB|TB}/s"
//! \~english Returns receive speed in format "n {B|kB|MB|GB|TB}/s"
//! \~russian Возвращает скорость приема в формате "n {B|kB|MB|GB|TB}/s"
PIString receiveSpeed() const;
//! Returns send speed in format "n {B|kB|MB|GB|TB}/s"
//! \~english Returns send speed in format "n {B|kB|MB|GB|TB}/s"
//! \~russian Возвращает скорость отправки в формате "n {B|kB|MB|GB|TB}/s"
PIString sendSpeed() const;
EVENT_HANDLER0(void, start);
EVENT_HANDLER1(void, start, PISystemTime, interval);
EVENT_HANDLER0(void, reset);
EVENT_HANDLER1(void, received, int, size) { received(size, true); }
EVENT_HANDLER2(void, received, int, size, bool, correct);
EVENT_HANDLER1(void, sended, int, size);
EVENT2(qualityChanged, PIDiagnostics::Quality, new_quality, PIDiagnostics::Quality, old_quality);
//! \handlers
//! \{
//! \fn void start(double msecs = 1000.)
//! \brief Start diagnostics evaluations with period "msecs" milliseconds
//! \fn void start()
//! \~english Start diagnostics with default period (1000ms)
//! \~russian Запустить диагностику с периодом по умолчанию (1000мс)
EVENT_HANDLER0(void, start);
//! \fn void start(PISystemTime interval)
//! \~english Start diagnostics with specified period
//! \~russian Запустить диагностику с указанным периодом
EVENT_HANDLER1(void, start, PISystemTime, interval);
//! \fn void reset()
//! \brief Reset diagnostics counters
//! \~english Reset diagnostics counters
//! \~russian Сбросить счетчики диагностики
EVENT_HANDLER0(void, reset);
//! \fn void received(int size, bool correct = true)
//! \brief Notify diagnostics about "correct" corected received packet
//! \fn void received(int size)
//! \~english Notify diagnostics about received packet
//! \~russian Уведомить диагностику о принятом пакете
EVENT_HANDLER1(void, received, int, size) { received(size, true); }
//! \fn void received(int size, bool correct)
//! \~english Notify diagnostics about received packet with correctness flag
//! \~russian Уведомить диагностику о принятом пакете с флагом корректности
EVENT_HANDLER2(void, received, int, size, bool, correct);
//! \fn void sended(int size)
//! \brief Notify diagnostics about sended packet
//! \~english Notify diagnostics about sended packet
//! \~russian Уведомить диагностику об отправленном пакете
EVENT_HANDLER1(void, sended, int, size);
//! \}
//! \events
//! \{
//! \fn void qualityChanged(PIDiagnostics::Quality new_quality, PIDiagnostics::Quality old_quality)
//! \brief Raise on change receive quality from "old_quality" to "new_quality"
//! \~english Emitted when receive quality changes
//! \~russian Генерируется при изменении качества приема
EVENT2(qualityChanged, PIDiagnostics::Quality, new_quality, PIDiagnostics::Quality, old_quality);
//! \}
private:
//! \~english Internal structure for packet statistics history
//! \~russian Внутренняя структура для истории статистики пакетов
struct PIP_EXPORT Entry {
//! \~english Number of correct bytes
//! \~russian Количество корректных байт
ullong bytes_ok = 0;
//! \~english Number of wrong bytes
//! \~russian Количество неправильных байт
ullong bytes_fail = 0;
//! \~english Count of correct packets
//! \~russian Количество корректных пакетов
uint cnt_ok = 0;
//! \~english Count of wrong packets
//! \~russian Количество неправильных пакетов
uint cnt_fail = 0;
//! \~english Flag indicating if entry is empty
//! \~russian Флаг, указывающий, что запись пустая
bool empty = true;
};
//! \~english Equality operator for Entry structure
//! \~russian Оператор равенства для структуры Entry
friend bool operator==(const PIDiagnostics::Entry & f, const PIDiagnostics::Entry & s);
//! \~english Inequality operator for Entry structure
//! \~russian Оператор неравенства для структуры Entry
friend bool operator!=(const PIDiagnostics::Entry & f, const PIDiagnostics::Entry & s);
//! \~english Less-than operator for Entry structure
//! \~russian Оператор меньше для структуры Entry
friend bool operator<(const PIDiagnostics::Entry & f, const PIDiagnostics::Entry & s);
//! \~english Timer tick handler
//! \~russian Обработчик тика таймера
void tick(int) override;
//! \~english Calculate history statistics
//! \~russian Вычислить статистику истории
Entry calcHistory(PIQueue<Entry> & hist, int & cnt);
//! \~english Property changed handler
//! \~russian Обработчик изменения свойства
void propertyChanged(const char *) override;
//! \~english Change disconnect timeout
//! \~russian Изменить таймаут отключения
void changeDisconnectTimeout(PISystemTime disct);
//! \~english History queue for received packets
//! \~russian Очередь истории для принятых пакетов
PIQueue<Entry> history_rec, history_send;
//! \~english Disconnect timeout
//! \~russian Таймаут отключения
PISystemTime disconn_;
//! \~english Current state
//! \~russian Текущее состояние
State cur_state;
//! \~english Mutex for state access protection
//! \~russian Мьютекс для защиты доступа к состоянию
mutable PIMutex mutex_state;
};

View File

@@ -29,45 +29,61 @@
#include "pibytearray.h"
#include "pip_io_utils_export.h"
//! \~english Base class for ethernet utils
//! \~russian Базовый класс для утилит ethernet
class PIP_IO_UTILS_EXPORT PIEthUtilBase {
public:
//! \~english Constructs PIEthUtilBase
//! \~russian Создает PIEthUtilBase
PIEthUtilBase();
//! \~english Destructor
//! \~russian Деструктор
~PIEthUtilBase();
//! Set crypt layer enabled
//! \~english Set crypt layer enabled
//! \~russian Включить слой шифрования
void setCryptEnabled(bool on);
//! Enable crypt layer
//! \~english Enable crypt layer
//! \~russian Включить слой шифрования
void cryptEnable();
//! Disable crypt layer
//! \~english Disable crypt layer
//! \~russian Выключить слой шифрования
void cryptDisable();
//! Returns if crypt layer enabled
//! \~english Returns if crypt layer enabled
//! \~russian Возвращает, включен ли слой шифрования
bool isCryptEnabled() const;
//! Set crypt layer key to \"k\"
//! \~english Set crypt layer key to "k"
//! \~russian Установить ключ шифрования в "k"
void setCryptKey(const PIByteArray & k);
//! Generate crypt layer key by \a PICrypt::hash and
//! set crypt layer enabled
//! \~english Generate crypt layer key by \a PICrypt::hash and set crypt layer enabled
//! \~russian Сгенерировать ключ шифрования с помощью \a PICrypt::hash и включить шифрование
void createCryptKey(const PIString & k);
//! Returns crypt layer key
//! \~english Returns crypt layer key
//! \~russian Возвращает ключ шифрования
PIByteArray cryptKey() const;
//! \brief Returns addition size for crypted data.
//! \~english Returns addition size for crypted data
//! \~russian Возвращает дополнительный размер для зашифрованных данных
static size_t cryptSizeAddition();
protected:
/*! \brief Returns encrypted data if layer enabled,
* otherwise returns unchanged \"data\" */
//! \~english Returns encrypted data if layer enabled, otherwise returns unchanged "data"
//! \~russian Возвращает зашифрованные данные, если слой включен, иначе возвращает неизмененные "data"
PIByteArray cryptData(const PIByteArray & data);
/*! \brief Returns decrypted data if layer enabled,
* otherwise returns unchanged \"data\". If decryption
* was unsuccessfull returns empty %PIByteArray. */
//! \~english Returns decrypted data if layer enabled, otherwise returns unchanged "data". If decryption was unsuccessful returns empty
//! %PIByteArray
//! \~russian Возвращает расшифрованные данные, если слой включен, иначе возвращает неизмененные "data". Если расшифровка неуспешна,
//! возвращает пустой %PIByteArray
PIByteArray decryptData(const PIByteArray & data);
private:

View File

@@ -31,25 +31,40 @@
#define __PIFILETRANSFER_VERSION 2
//! \~english Class for send and receive files and directories via \a PIBaseTransfer
//! \~russian Класс для отправки и приема файлов и папок с помощью \a PIBaseTransfer
class PIP_EXPORT PIFileTransfer: public PIBaseTransfer {
PIOBJECT_SUBCLASS(PIFileTransfer, PIBaseTransfer);
public:
//! \~english Constructs empty file transfer
//! \~russian Создает пустой transfer файлов
PIFileTransfer();
//! \~english Destructor
//! \~russian Деструктор
~PIFileTransfer();
//! \~english Step type for file transfer
//! \~russian Тип шага для передачи файлов
enum StepType {
pft_None,
pft_Description,
pft_Data
};
//! \~english File information structure
//! \~russian Структура информации о файле
struct PIP_EXPORT PFTFileInfo: public PIFile::FileInfo {
//! \~english Constructor
//! \~russian Конструктор
PFTFileInfo(const PIFile::FileInfo & fi = PIFile::FileInfo()): PIFile::FileInfo(fi) {}
PIString dest_path;
};
#pragma pack(push, 1)
//! \~english File transfer header structure
//! \~russian Структура заголовка передачи файлов
struct PIP_EXPORT PFTHeader {
union {
struct {
@@ -60,6 +75,8 @@ public:
};
int step; // PacketType
int session_id;
//! \~english Check if signature is valid
//! \~russian Проверка валидности подписи
bool check_sig() {
if (sig[0] != sign[0] || sig[1] != sign[1] || sig[2] != sign[2] || version != __PIFILETRANSFER_VERSION) return false;
return true;
@@ -67,23 +84,64 @@ public:
};
#pragma pack(pop)
//! \~english Send file
//! \~russian Отправить файл
bool send(const PIFile & file);
//! \~english Send file by path
//! \~russian Отправить файл по пути
bool send(const PIString & file);
//! \~english Send files list
//! \~russian Отправить список файлов
bool send(const PIStringList & files);
//! \~english Send file info
//! \~russian Отправить информацию о файле
bool send(PIFile::FileInfo entry) { return send(PIVector<PIFile::FileInfo>() << entry); }
//! \~english Send file info vector
//! \~russian Отправить вектор информации о файлах
bool send(PIVector<PIFile::FileInfo> entries);
//! \~english Set directory
//! \~russian Установить директорию
void setDirectory(const PIDir & d) { dir = d; }
//! \~english Set directory by path
//! \~russian Установить директорию по пути
void setDirectory(const PIString & path) { dir.setDir(path); }
//! \~english Get directory
//! \~russian Получить директорию
PIDir directory() const { return dir; }
//! \~english Check if transfer is started
//! \~russian Проверить, начата ли передача
bool isStarted() const { return started_; }
//! \~english Get current file name
//! \~russian Получить имя текущего файла
PIString curFile() const;
//! \~english Get total bytes of current file
//! \~russian Получить общее количество байт текущего файла
llong bytesFileAll() const { return bytes_file_all; }
//! \~english Get current bytes of current file
//! \~russian Получить текущие байты текущего файла
llong bytesFileCur() const { return bytes_file_cur; }
//! \~english Get pointer to current file name
//! \~russian Получить указатель на имя текущего файла
const PIString * curFile_ptr() const { return &cur_file_string; }
//! \~english Get pointer to total bytes of current file
//! \~russian Получить указатель на общее количество байт текущего файла
const llong * bytesFileAll_ptr() const { return &bytes_file_all; }
//! \~english Get pointer to current bytes of current file
//! \~russian Получить указатель на текущие байты текущего файла
const llong * bytesFileCur_ptr() const { return &bytes_file_cur; }
EVENT(receiveFilesStarted);

View File

@@ -34,31 +34,45 @@
class PIEthernet;
//! \~english Ethernet device
//! \~russian Устройство Ethernet
class PIP_IO_UTILS_EXPORT PIPackedTCP: public PIIODevice {
PIIODEVICE(PIPackedTCP, "ptcp");
public:
//! \brief Role of %PIPackedTCP
//! \~english Role of %PIPackedTCP
//! \~russian Роль %PIPackedTCP
enum Role {
Client /** TCP client */,
Server /** TCP server for one client */
};
//! Contructs %PIPackedTCP with "role" and "addr" address
//! \~english Constructs %PIPackedTCP with "role" and "addr" address
//! \~russian Создает %PIPackedTCP с ролью "role" и адресом "addr"
explicit PIPackedTCP(Role role = Client, const PINetworkAddress & addr = {});
//! \~english Destructor
//! \~russian Деструктор
virtual ~PIPackedTCP();
//! Set server address for Server role or connect address for Client
//! \~english Set server address for Server role or connect address for Client
//! \~russian Установить адрес сервера для роли Server или адрес подключения для Client
void setAddress(const PINetworkAddress & addr);
//! \~english Check if connected
//! \~russian Проверить подключение
bool isConnected() const;
//! \~english Check if connecting
//! \~russian Проверить подключение
bool isConnecting() const;
//! Returns read address in format "i.i.i.i:p"
//! \~english Returns read address in format "i.i.i.i:p"
//! \~russian Возвращает адрес чтения в формате "i.i.i.i:p"
PINetworkAddress address() const { return m_addr; }
//! Returns %PIEthernet type
//! \~english Returns %PIEthernet type
//! \~russian Возвращает тип %PIEthernet
Role role() const { return m_role; }
EVENT0(connected);

View File

@@ -1,9 +1,13 @@
/*! \file pipacketextractor.h
* \ingroup IO
* \~\brief
* \~english Packets extractor
* \~russian Извлекатель пакетов
*/
//! \addtogroup IO
//! \{
//! \file pipacketextractor.h
//! \brief
//! \~english Packets extractor
//! \~russian Извлекатель пакетов
//! \details
//! \~english The PIPacketExtractor class provides packet recognition from data stream using various algorithms.
//! \~russian Класс PIPacketExtractor предоставляет распознавание пакетов из потока данных с использованием различных алгоритмов.
//! \~\}
/*
PIP - Platform Independent Primitives
Packets extractor
@@ -28,25 +32,35 @@
#include "piiodevice.h"
/// TODO: написать документацию, тут ничего не понятно
/// Pass SourceHeaderPtr, ReceivedHeaderPtr, HeaderSize.
/// Return size of payload if packet is correct, or -1 if incorrect.
//! \~english Header validation function type
//! \~russian Тип функции проверки заголовка
typedef std::function<int(const uchar *, const uchar *, int)> PacketExtractorHeaderFunc;
/// Pass ReceivedDataPtr, DataSize.
/// Return true if packet is correct, false otherwise.
//! \~english Payload validation function type
//! \~russian Тип функции проверки полезной нагрузки
typedef std::function<bool(const uchar *, int)> PacketExtractorPayloadFunc;
/// Pass SourceFooterPtr, ReceivedFooterPtr, FooterSize.
/// Return true if packet is correct, false otherwise.
//! \~english Footer validation function type
//! \~russian Тип функции проверки окончания
typedef std::function<bool(const uchar *, const uchar *, int)> PacketExtractorFooterFunc;
//! \~english Packets extractor
//! \~russian Извлекатель пакетов
//! \details
//! \~english The PIPacketExtractor class provides packet recognition from data stream using various algorithms such as header detection,
//! footer detection, size-based extraction, and timeout-based extraction.
//! \~russian Класс PIPacketExtractor предоставляет распознавание пакетов из потока данных с использованием различных алгоритмов, таких как
//! обнаружение заголовка, обнаружение окончания, извлечение по размеру и извлечение по таймауту.
class PIP_EXPORT PIPacketExtractor: public PIIODevice {
PIIODEVICE(PIPacketExtractor, "pckext");
friend class PIConnection;
public:
//! Extract algorithms
//! \~english Extract algorithms
//! \~russian Алгоритмы извлечения
//! \details
//! \~english Defines the packet extraction algorithms supported by PIPacketExtractor
//! \~russian Определяет алгоритмы извлечения пакетов, поддерживаемые PIPacketExtractor
enum SplitMode {
None /** No data processing */,
Header /** Detect packets with \a header() and following \a payloadSize() */,
@@ -56,73 +70,143 @@ public:
Timeout /** Wait for first read, then read for \a timeout() */
};
//! Contructs extractor with child device "device_" and extract algorithm "mode"
//! \~english Constructs extractor with child device "device_" and extract algorithm "mode"
//! \~russian Создает экстрактор с дочерним устройством "device_" и алгоритмом извлечения "mode"
explicit PIPacketExtractor(PIIODevice * device_ = nullptr, SplitMode mode = None);
//! \~english Destructor
//! \~russian Деструктор
virtual ~PIPacketExtractor() { stop(); }
//! Returns child %device
//! \~english Returns child %device
//! \~russian Возвращает дочернее устройство
PIIODevice * device() { return dev; }
//! Set child %device to "device_"
//! \~english Set child %device to "device_"
//! \~russian Установить дочернее устройство в "device_"
void setDevice(PIIODevice * device_);
//! \~english Get bytes available
//! \~russian Получить доступные байты
//! \details
//! \~english Returns the number of bytes available for reading from the child device
//! \~russian Возвращает количество байт, доступных для чтения из дочернего устройства
ssize_t bytesAvailable() const override;
//! \~english Set header check function
//! \~russian Установить функцию проверки заголовка
//! \details
//! \~english Sets the callback function used to validate packet headers
//! \~russian Устанавливает функцию обратного вызова, используемую для проверки заголовков пакетов
void setHeaderCheckSlot(PacketExtractorHeaderFunc f) { func_header = f; }
//! \~english Set payload check function
//! \~russian Установить функцию проверки полезной нагрузки
//! \details
//! \~english Sets the callback function used to validate packet payload
//! \~russian Устанавливает функцию обратного вызова, используемую для проверки полезной нагрузки пакетов
void setPayloadCheckSlot(PacketExtractorPayloadFunc f) { func_payload = f; }
//! \~english Set footer check function
//! \~russian Установить функцию проверки окончания
//! \details
//! \~english Sets the callback function used to validate packet footers
//! \~russian Устанавливает функцию обратного вызова, используемую для проверки окончаний пакетов
void setFooterCheckSlot(PacketExtractorFooterFunc f) { func_footer = f; }
//! Set extract algorithm
//! \~english Set extract algorithm
//! \~russian Установить алгоритм извлечения
//! \details
//! \~english Sets the packet extraction algorithm to be used
//! \~russian Устанавливает алгоритм извлечения пакетов, который будет использоваться
void setSplitMode(SplitMode mode) { setProperty("splitMode", int(mode)); }
//! Set payload size, used for PIPacketExtractor::Header and PIPacketExtractor::Footer algorithms
//! \~english Set payload size, used for PIPacketExtractor::Header and PIPacketExtractor::Footer algorithms
//! \~russian Установить размер полезной нагрузки
//! \details
//! \~english Sets the expected payload size for Header and Footer extraction algorithms
//! \~russian Устанавливает ожидаемый размер полезной нагрузки для алгоритмов извлечения Header и Footer
void setPayloadSize(int size);
//! Set header data, used for PIPacketExtractor::Header and PIPacketExtractor::HeaderAndFooter algorithms
//! \~english Set header data, used for PIPacketExtractor::Header and PIPacketExtractor::HeaderAndFooter algorithms
//! \~russian Установить данные заголовка
//! \details
//! \~english Sets the header pattern used to identify packet start
//! \~russian Устанавливает шаблон заголовка, используемый для идентификации начала пакета
void setHeader(const PIByteArray & data);
//! Set footer data, used for PIPacketExtractor::Footer and PIPacketExtractor::HeaderAndFooter algorithms
//! \~english Set footer data, used for PIPacketExtractor::Footer and PIPacketExtractor::HeaderAndFooter algorithms
//! \~russian Установить данные окончания
//! \details
//! \~english Sets the footer pattern used to identify packet end
//! \~russian Устанавливает шаблон окончания, используемый для идентификации конца пакета
void setFooter(const PIByteArray & data);
//! Set timeout, used for PIPacketExtractor::Timeout algorithm
//! \~english Set timeout, used for PIPacketExtractor::Timeout algorithm
//! \~russian Установить таймаут
//! \details Sets the timeout duration for the Timeout extraction algorithm
//! \~russian Устанавливает длительность таймаута для алгоритма извлечения Timeout
void setTimeout(PISystemTime tm) { setProperty("timeout", tm); }
//! Returns current extract algorithm
//! \~english Returns current extract algorithm
//! \~russian Возвращает текущий алгоритм извлечения
//! \details
//! \~english Returns the currently configured packet extraction algorithm
//! \~russian Возвращает текущий настроенный алгоритм извлечения пакетов
SplitMode splitMode() const { return mode_; }
//! Returns current payload size, used for PIPacketExtractor::Header and PIPacketExtractor::Footer and PIPacketExtractor::Size
//! algorithms
//! \~english Returns current payload size
//! \~russian Возвращает текущий размер полезной нагрузки
//! \details
//! \~english Returns the expected payload size used by Header and Footer algorithms
//! \~russian Возвращает ожидаемый размер полезной нагрузки, используемый алгоритмами Header и Footer
int payloadSize() const { return dataSize; }
//! Returns current header data, used for PIPacketExtractor::Header and PIPacketExtractor::HeaderAndFooter algorithms
//! \~english Returns current header data
//! \~russian Возвращает текущие данные заголовка
//! \details
//! \~english Returns the configured header pattern
//! \~russian Возвращает настроенный шаблон заголовка
PIByteArray header() const { return src_header; }
//! Returns current footer data, used for PIPacketExtractor::Footer and PIPacketExtractor::HeaderAndFooter algorithms
//! \~english Returns current footer data
//! \~russian Возвращает текущие данные окончания
//! \details
//! \~english Returns the configured footer pattern
//! \~russian Возвращает настроенный шаблон окончания
PIByteArray footer() const { return src_footer; }
//! Returns current timeout in milliseconds, used for PIPacketExtractor::Timeout algorithm
//! \~english Returns current timeout in milliseconds
//! \~russian Возвращает текущий таймаут в миллисекундах
//! \details
//! \~english Returns the configured timeout duration
//! \~russian Возвращает настроенную длительность таймаута
PISystemTime timeout() const { return time_; }
//! Returns missed by validating functions bytes count
//! \~english Returns missed by validating functions bytes count
//! \~russian Возвращает количество байт, пропущенных функциями проверки
//! \details
//! \~english Returns the number of bytes that were skipped during validation failures
//! \~russian Возвращает количество байт, которые были пропущены при неудачных проверках
ullong missedBytes() const { return missed; }
//! Add data to extractor, raise \a packetReceived() if packet is ready
//! \~english Add data to extractor, raise \a packetReceived() if packet is ready
//! \~russian Добавить данные в экстрактор, вызвать \a packetReceived() если пакет готов
void appendData(const uchar * d, int s) { threadedRead(d, s); }
//! Add data to extractor, raise \a packetReceived() if packet is ready
//! \~english Add data to extractor, raise \a packetReceived() if packet is ready
//! \~russian Добавить данные в экстрактор, вызвать \a packetReceived() если пакет готов
void appendData(const PIByteArray & data) { threadedRead(data.data(), data.size_s()); }
EVENT2(packetReceived, const uchar *, data, int, size);
//! \events
//! \{
//! \fn void packetReceived(const uchar * data, int size)
//! \brief Raise on successfull \a packetValidate() function
EVENT2(packetReceived, const uchar *, data, int, size);
//! \}

View File

@@ -1,9 +1,16 @@
/*! \file piparsehelper.h
* \ingroup IO
* \~\brief
* \~english Helper class to automate structs receive
* \~russian Класс для автоматизации приема структур
*/
//! \addtogroup IO
//! \{
//! \file piparsehelper.h
//! \brief
//! \~english Template helper class to automate structs receive and data packet deserialization
//! \~russian Шаблонный класс для автоматизации приема структур и десериализации пакетов данных
//! \details
//! \~english The PIParseHelper class provides a mechanism to deserialize data packets and automatically invoke assigned handler methods. It
//! supports member functions, lambda functions, and functors with 0 or 1 arguments.
//! \~russian Класс PIParseHelper предоставляет механизм для десериализации пакетов данных и автоматического вызова назначаемых
//! обработчиков. Поддерживаются member-функции, lambda-функции и функторы с 0 или 1 аргументами.
//! \~\}
/*
PIP - Platform Independent Primitives
Helper class to automate structs receive
@@ -30,60 +37,49 @@
#include "pip_io_utils_export.h"
/** \class PIParseHelper
* \brief Helper class to automate structs receive
*
*
* \section PIParseHelper_synopsis Synopsis
* This class helps to deserialize and invoke neccessarily methods.
*
* Data packets with header and various data types can be automated by this class.
* Every key value mapped to object member function, lambda-function or functor.
*
* This class can remove \b switch-case with deserialization code and
* replace it with several \a assign() calls, binded to ready-to-use event handlers.
* Moreover data type automatic takes from event handler or lambda argument. One should
* only make \"PIByteArray & operator <<()\" with used types, deserialization will be
* performed by %PIParseHelper.
*
*
* \section PIParseHelper_usage Usage
*
* Create instance of %PIParseHelper, or subclass.
*
* In \a assign() methods you can use object member function, lambda-function
* or functor with 0 or 1 arguments,
*
*
* \section PIParseHelper_lambda Lambda-functions
* \code assign(1, [this](const SomeStruct & s){}) \endcode
*
*
* \section PIParseHelper_examples Examples
* First example describes subclass variant. As one can see, it`s a single place to change
* type of received data - event handler argument.
* \snippet piparsehelper.cpp 0
*
* Second example show separate variant:
* \snippet piparsehelper.cpp 1
*
**/
//! \class PIParseHelper
//! \~english Template helper class to automate structs receive and data packet deserialization
//! \~russian Шаблонный класс для автоматизации приема структур и десериализации пакетов данных
//! \details
//! \~english The PIParseHelper class provides a mechanism to deserialize data packets and automatically invoke assigned handler methods. It
//! supports member functions, lambda functions, and functors with 0 or 1 arguments. The class eliminates the need for switch-case
//! statements by mapping keys to event handlers via assign() calls. Data type extraction is automatically performed from the handler
//! signature.
//! \~russian Класс PIParseHelper предоставляет механизм для десериализации пакетов данных и автоматического вызова назначаемых
//! обработчиков. Поддерживаются member-функции, lambda-функции и функторы с 0 или 1 аргументами. Класс устраняет необходимость операторов
//! switch-case, сопоставляя ключи с обработчиками событий через вызовы assign(). Извлечение типа данных автоматически выполняется из
//! сигнатуры обработчика.
template<typename Key>
class PIParseHelper {
public:
//! \brief Construct %PIParseHelper
//! \~english Construct PIParseHelper
//! \~russian Создать PIParseHelper
PIParseHelper() {}
//! \brief Assign key \"key\" to lambda-function \"func\" without arguments
//! \~english Assign key to lambda-function without arguments
//! \~russian Сопоставить ключ с lambda-функцией без аргументов
//! \param key The key to assign
//! \param func The lambda function to call when key is parsed
//! \~english \param key Ключ для сопоставления
//! \~russian \param key Ключ для сопоставления
//! \~english \param func Lambda-функция, вызываемая при разборе ключа
//! \~russian \param func Lambda-функция, вызываемая при разборе ключа
void assign(Key key, std::function<void()> func) {
auto lf = [func](PIByteArray) { func(); };
functions[key] << lf;
}
//! \brief Assign key \"key\" to lambda-function \"func\" with 1 argument
//! \~english Assign key to lambda-function with 1 argument
//! \~russian Сопоставить ключ с lambda-функцией с 1 аргументом
//! \param key The key to assign
//! \param func The lambda function to call when key is parsed
//! \~english \param key Ключ для сопоставления
//! \~russian \param key Ключ для сопоставления
//! \~english \param func Lambda-функция, вызываемая при разборе ключа
//! \~russian \param func Lambda-функция, вызываемая при разборе ключа
template<typename T>
void assign(Key key, std::function<void(const T &)> func) {
auto lf = [func](PIByteArray data) {
@@ -97,7 +93,17 @@ public:
}
//! \brief Assign key \"key\" to member function of object \"obj\" without arguments
//! \~english Assign key to member function of object without arguments
//! \~russian Сопоставить ключ с member-функцией объекта без аргументов
//! \param key The key to assign
//! \param obj Pointer to the object
//! \param member_func Pointer to the member function
//! \~english \param key Ключ для сопоставления
//! \~russian \param key Ключ для сопоставления
//! \~english \param obj Указатель на объект
//! \~russian \param obj Указатель на объект
//! \~english \param member_func Указатель на member-функцию
//! \~russian \param member_func Указатель на member-функцию
template<typename O>
void assign(Key key, O * obj, void (O::*member_func)()) {
auto lf = [member_func, obj](PIByteArray) { (obj->*member_func)(); };
@@ -105,7 +111,17 @@ public:
}
//! \brief Assign key \"key\" to member function of object \"obj\" with 1 argument
//! \~english Assign key to member function of object with 1 argument
//! \~russian Сопоставить ключ с member-функцией объекта с 1 аргументом
//! \param key The key to assign
//! \param obj Pointer to the object
//! \param member_func Pointer to the member function
//! \~english \param key Ключ для сопоставления
//! \~russian \param key Ключ для сопоставления
//! \~english \param obj Указатель на объект
//! \~russian \param obj Указатель на объект
//! \~english \param member_func Указатель на member-функцию
//! \~russian \param member_func Указатель на member-функцию
template<typename T, typename O>
void assign(Key key, O * obj, void (O::*member_func)(const T &)) {
auto lf = [member_func, obj](PIByteArray data) {
@@ -119,14 +135,28 @@ public:
}
//! \brief Assign key \"key\" to functor \"func\" with 0 or 1 argument
//! \~english Assign key to functor with 0 or 1 argument
//! \~russian Сопоставить ключ с функтором с 0 или 1 аргументом
//! \param key The key to assign
//! \param func The functor to call when key is parsed
//! \~english \param key Ключ для сопоставления
//! \~russian \param key Ключ для сопоставления
//! \~english \param func Функтор, вызываемый при разборе ключа
//! \~russian \param func Функтор, вызываемый при разборе ключа
template<typename L>
void assign(Key key, L func) {
return assign(key, toStdFunction(func));
}
//! \brief Deserialize data and invoke assigned to \"key\" methods
//! \~english Deserialize data and invoke assigned methods
//! \~russian Десериализовать данные и вызвать назначенные методы
//! \param key The key to look up
//! \param ba The byte array to deserialize
//! \~english \param key Ключ для поиска
//! \~russian \param key Ключ для поиска
//! \~english \param ba Байтовый массив для десериализации
//! \~russian \param ba Байтовый массив для десериализации
void parse(Key key, PIByteArray ba) {
auto fl = functions.value(key);
for (auto f: fl)
@@ -134,8 +164,11 @@ public:
}
private:
//! \~english Map of assigned functions
//! \~russian Карта назначенных функций
PIMap<Key, PIVector<std::function<void(PIByteArray)>>> functions;
};
//! \}
#endif // PIPARSEHELPER_H

View File

@@ -33,10 +33,14 @@
class PIIODevice;
//! \~english Stream packer configuration
//! \~russian Конфигурация упаковщика потока
class PIStreamPackerConfig: public PIEthUtilBase {
friend class PIStreamPacker;
public:
//! \~english Constructs default configuration
//! \~russian Создает конфигурацию по умолчанию
PIStreamPackerConfig() {
crypt_size = false;
aggressive_optimization = true;
@@ -44,34 +48,50 @@ public:
packet_sign = 0xAFBE;
}
//! Set maximum size of single packet
//! \~english Set maximum size of single packet
//! \~russian Установить максимальный размер одного пакета
void setMaxPacketSize(int max_size) { max_packet_size = max_size; }
//! Returns maximum size of single packet, default 1400 bytes
//! \~english Returns maximum size of single packet, default 1400 bytes
//! \~russian Возвращает максимальный размер одного пакета, по умолчанию 1400 байт
int maxPacketSize() const { return max_packet_size; }
//! Set packet sinature
//! \~english Set packet signature
//! \~russian Установить подпись пакета
void setPacketSign(ushort sign_) { packet_sign = sign_; }
//! Returns packet sinature, default 0xAFBE
//! \~english Returns packet signature, default 0xAFBE
//! \~russian Возвращает подпись пакета, по умолчанию 0xAFBE
ushort packetSign() const { return packet_sign; }
//! Set receive aggressive optimization. If yes then %PIStreamPacker doesn`t
//! check every byte in incoming stream but check only begin of each read()
//! result. Default is \b true.
//! \~english Set receive aggressive optimization. If yes then %PIStreamPacker doesn`t check every byte in incoming stream but check
//! only begin of each read() result. Default is \b true.
//! \~russian Установить агрессивную оптимизацию приема. Если да, то %PIStreamPacker не проверяет каждый байт во входящем потоке, а
//! только начало каждого результата read(). По умолчанию \b true.
void setaAggressiveOptimization(bool yes) { aggressive_optimization = yes; }
//! Returns aggressive optimization
//! \~english Returns aggressive optimization
//! \~russian Возвращает агрессивную оптимизацию
bool aggressiveOptimization() const { return aggressive_optimization; }
//! \~english Check if crypt size enabled
//! \~russian Проверить, включен ли размер шифрования
bool cryptSizeEnabled() const { return crypt_size; }
//! \~english Set crypt size enabled
//! \~russian Установить включенный размер шифрования
void setCryptSizeEnabled(bool on) { crypt_size = on; }
//! Get configuration
//! \~english Get configuration
//! \~russian Получить конфигурацию
const PIStreamPackerConfig & configuration() const { return *this; }
//! \~english Get configuration
//! \~russian Получить конфигурацию
PIStreamPackerConfig & configuration() { return *this; }
//! Apply configuration
//! \~english Apply configuration
//! \~russian Применить конфигурацию
void setConfiguration(const PIStreamPackerConfig & config) { *this = config; }
private:
@@ -81,31 +101,40 @@ private:
};
//! \~english Simple packet wrap around any PIIODevice
//! \~russian Простая фрагментация пакетов, использует любой PIIODevice
class PIP_IO_UTILS_EXPORT PIStreamPacker
: public PIObject
, public PIStreamPackerConfig {
PIOBJECT(PIStreamPacker)
public:
//! Contructs packer and try to assign \"dev\"
//! \~english Constructs packer and try to assign "dev"
//! \~russian Создает упаковщик и пытается присвоить "dev"
PIStreamPacker(PIIODevice * dev = nullptr);
//! Returns progress of current packet receive in bytes
//! \~english Returns progress of current packet receive in bytes
//! \~russian Возвращает прогресс приема текущего пакета в байтах
int receivePacketProgress() const { return packet.size_s(); }
//! \~english Clear all buffers
//! \~russian Очистить все буферы
void clear();
//! Prepare data for send and raise \a sendRequest() events
//! \~english Prepare data for send and raise \a sendRequest() events
//! \~russian Подготовить данные для отправки и вызвать события \a sendRequest()
void send(const PIByteArray & data);
//! Receive data part. If packet is ready, raise \a packetReceiveEvent() event
//! and \a packetReceived() virtual method
//! \~english Receive data part. If packet is ready, raise \a packetReceiveEvent() event and \a packetReceived() virtual method
//! \~russian Принять часть данных. Если пакет готов, вызвать событие \a packetReceiveEvent() и виртуальный метод \a packetReceived()
void received(const PIByteArray & data);
EVENT_HANDLER2(void, received, const uchar *, readed, ssize_t, size);
//! Connect \"dev\" \a PIIODevice::threadedReadEvent() event to \a received() handler
//! and \a sendRequest() event to \"dev\" \a PIIODevice::write() handler
//! \~english Connect "dev" \a PIIODevice::threadedReadEvent() event to \a received() handler and \a sendRequest() event to "dev" \a
//! PIIODevice::write() handler
//! \~russian Подключить событие \a PIIODevice::threadedReadEvent() устройства "dev" к обработчику \a received() и событие \a
//! sendRequest() к обработчику \a PIIODevice::write()
void assignDevice(PIIODevice * dev);
EVENT1(packetReceiveEvent, PIByteArray &, data);

View File

@@ -1,15 +1,25 @@
/*! \file piliterals.h
* \ingroup Core
* \~\brief
* \~english C++11 literals
* \~russian C++11 суффиксы
*
* \~\details
* \~english
* Include all literals_*.h files
* \~russian
* Включает все файлы literals_*.h
*/
//! \addtogroup Core
//! \{}
//! \file piliterals.h
//! \brief C++11 user-defined literals
//! \~english C++11 user-defined literals for PIP library types
//! \~russian C++11 пользовательские литералы для типов библиотеки PIP
//! \details
//! \~english
//! Includes all literals_*.h files providing user-defined literals (UDL) for:
//! * PIByteArray (hex, base64)
//! * Bytes (KB, MB, GB, TB, PB, KiB, MiB, GiB, TiB, PiB)
//! * PIString (ASCII, UTF-8)
//! * PIRegularExpression (regex, glob)
//! * PISystemTime (d, h, m, s, ms, us, ns, Hz, KHz, MHz, GHz)
//! \~russian
//! Включает все файлы literals_*.h с пользовательскими литералами (UDL) для:
//! * PIByteArray (hex, base64)
//! * Bytes (KB, MB, GB, TB, PB, KiB, MiB, GiB, TiB, PiB)
//! * PIString (ASCII, UTF-8)
//! * PIRegularExpression (regex, glob)
//! * PISystemTime (d, h, m, s, ms, us, ns, Hz, KHz, MHz, GHz)
//! \}
/*
PIP - Platform Independent Primitives
C++11 literals

View File

@@ -1,9 +1,13 @@
/*! \file piluaprogram.h
* \ingroup Lua
* \~\brief
* \~english Lua Program
* \~russian Программа Lua
*/
//! \addtogroup Lua
//! \{
//! \file piluaprogram.h
//! \brief
//! \~english Lua Program wrapper
//! \~russian Обёртка программы Lua
//! \details
//! \~english Provides interface for loading and executing Lua scripts using LuaBridge
//! \~russian Предоставляет интерфейс для загрузки и выполнения скриптов Lua с использованием LuaBridge
//! \}
/*
PIP - Platform Independent Primitives
PILuaProgram
@@ -29,21 +33,46 @@
#include "pip_lua.h"
#include "pip_lua_export.h"
//! \~english Lua Program class provides interface for loading and executing Lua scripts
//! \~russian Класс PILuaProgram предоставляет интерфейс для загрузки и выполнения скриптов Lua
class PIP_LUA_EXPORT PILuaProgram {
public:
//! Constructs an empty PILuaProgram, initialize Lua context
//! \~english Constructs an empty PILuaProgram and initializes Lua context
//! \~russian Создаёт пустой PILuaProgram и инициализирует контекст Lua
PILuaProgram();
//! Load Lua script from PIString
//! \~english Loads Lua script from PIString
//! \~russian Загружает скрипт Lua из PIString
//! \param script
//! \~english The Lua script to load
//! \~russian Загружаемый скрипт Lua
//! \return
//! \~english true if script loaded successfully
//! \~russian true, если скрипт успешно загружен
bool load(const PIString & script);
//! Execute script
//! \~english Prepares the loaded script for execution
//! \~russian Подготавливает загруженный скрипт к выполнению
//! \return
//! \~english true if preparation successful
//! \~russian true, если подготовка успешна
bool prepare();
//! Get Lua Object or Function
//! \~english Gets a global Lua object or function by name
//! \~russian Получает глобальный объект или функцию Lua по имени
//! \param name
//! \~english Name of the global object or function
//! \~russian Имя глобального объекта или функции
//! \return
//! \~english LuaRef to the global object or function
//! \~russian LuaRef на глобальный объект или функцию
luabridge::LuaRef getGlobal(const PIString & name);
//! Return Lua global namespace
//! \~english Returns the global namespace of the Lua state
//! \~russian Возвращает глобальное пространство имён состояния Lua
//! \return
//! \~english Namespace object representing global scope
//! \~russian Объект Namespace, представляющий глобальную область видимости
luabridge::Namespace getGlobalNamespace();
private:

View File

@@ -1,10 +1,10 @@
/*! \file picrc.h
* \ingroup Math
* \~\brief
* \~english CRC checksum calculation
* \~russian Вычисление CRC контрольной суммы
*/
/*
//! \file picrc.h
//! \ingroup Math
//! \~\brief
//! \~english CRC checksum calculation
//! \~russian Вычисление CRC контрольной суммы
/*!
PIP - Platform Independent Primitives
CRC checksum calculator
Ivan Pelipenko peri4ko@yandex.ru
@@ -28,67 +28,113 @@
#include "pistring.h"
//! \addtogroup Math
//! \{
//! \class uint_cl
//! \brief
//! \~english Fixed-size unsigned integer class for CRC calculations
//! \~russian Класс целого числа фиксированного размера для вычисления CRC
//! \details
//! \~english Provides arbitrary length unsigned integer support for CRC computation
//! \~russian Обеспечивает поддержку целых чисел произвольной длины для вычисления CRC
//! \}
template<int L>
class PIP_EXPORT uint_cl {
public:
//! \~english Default constructor, initializes to zero
//! \~russian Конструктор по умолчанию, инициализирует нулём
uint_cl() {
for (int i = 0; i < L / 8; ++i)
data_[i] = 0;
}
//! \~english Copy constructor
//! \~russian Конструктор копирования
uint_cl(const uint_cl<L> & v) {
for (int i = 0; i < L / 8; ++i)
data_[i] = v.data_[i];
}
//! \~english Construct from unsigned char
//! \~russian Конструктор из unsigned char
uint_cl(uchar v) {
for (int i = 0; i < L / 8; ++i)
data_[i] = (i == 0 ? v : 0);
}
//! \~english Construct from char
//! \~russian Конструктор из char
uint_cl(char v) {
for (int i = 0; i < L / 8; ++i)
data_[i] = (i == 0 ? v : 0);
}
//! \~english Construct from unsigned short
//! \~russian Конструктор из unsigned short
uint_cl(ushort v) {
int l = piMin<uint>(L / 8, sizeof(v));
memcpy(data_, &v, l);
for (int i = l; i < L / 8; ++i)
data_[i] = 0;
}
//! \~english Construct from short
//! \~russian Конструктор из short
uint_cl(short v) {
int l = piMin<uint>(L / 8, sizeof(v));
memcpy(data_, &v, l);
for (int i = l; i < L / 8; ++i)
data_[i] = 0;
}
//! \~english Construct from unsigned int
//! \~russian Конструктор из unsigned int
uint_cl(uint v) {
int l = piMin<uint>(L / 8, sizeof(v));
memcpy(data_, &v, l);
for (int i = l; i < L / 8; ++i)
data_[i] = 0;
}
//! \~english Construct from int
//! \~russian Конструктор из int
uint_cl(int v) {
int l = piMin<uint>(L / 8, sizeof(v));
memcpy(data_, &v, l);
for (int i = l; i < L / 8; ++i)
data_[i] = 0;
}
//! \~english Construct from unsigned long
//! \~russian Конструктор из unsigned long
uint_cl(ulong v) {
int l = piMin<uint>(L / 8, sizeof(v));
memcpy(data_, &v, l);
for (int i = l; i < L / 8; ++i)
data_[i] = 0;
}
//! \~english Construct from long
//! \~russian Конструктор из long
uint_cl(long v) {
int l = piMin<uint>(L / 8, sizeof(v));
memcpy(data_, &v, l);
for (int i = l; i < L / 8; ++i)
data_[i] = 0;
}
//! \~english Construct from unsigned long long
//! \~russian Конструктор из unsigned long long
uint_cl(ullong v) {
int l = piMin<uint>(L / 8, sizeof(v));
memcpy(data_, &v, l);
for (int i = l; i < L / 8; ++i)
data_[i] = 0;
}
//! \~english Construct from long long
//! \~russian Конструктор из long long
uint_cl(llong v) {
int l = piMin<uint>(L / 8, sizeof(v));
memcpy(data_, &v, l);
@@ -96,55 +142,87 @@ public:
data_[i] = 0;
}
//! \~english Convert to bool
//! \~russian Преобразование в bool
operator bool() {
for (int i = 0; i < L / 8; ++i)
if (data_[i] > 0) return true;
return false;
}
//! \~english Convert to char
//! \~russian Преобразование в char
operator char() { return (char)data_[0]; }
//! \~english Convert to short
//! \~russian Преобразование в short
operator short() {
short t(0);
int l = piMin<uint>(L / 8, sizeof(t));
memcpy(&t, data_, l);
return t;
}
//! \~english Convert to int
//! \~russian Преобразование в int
operator int() {
int t(0);
int l = piMin<uint>(L / 8, sizeof(t));
memcpy(&t, data_, l);
return t;
}
//! \~english Convert to long
//! \~russian Преобразование в long
operator long() {
long t(0);
int l = piMin<uint>(L / 8, sizeof(t));
memcpy(&t, data_, l);
return t;
}
//! \~english Convert to long long
//! \~russian Преобразование в long long
operator llong() {
llong t(0);
int l = piMin<uint>(L / 8, sizeof(t));
memcpy(&t, data_, l);
return t;
}
//! \~english Convert to unsigned char
//! \~russian Преобразование в unsigned char
operator uchar() { return data_[0]; }
//! \~english Convert to unsigned short
//! \~russian Преобразование в unsigned short
operator ushort() {
ushort t(0);
int l = piMin<uint>(L / 8, sizeof(t));
memcpy(&t, data_, l);
return t;
}
//! \~english Convert to unsigned int
//! \~russian Преобразование в unsigned int
operator uint() {
uint t(0);
int l = piMin<uint>(L / 8, sizeof(t));
memcpy(&t, data_, l);
return t;
}
//! \~english Convert to unsigned long
//! \~russian Преобразование в unsigned long
operator ulong() {
ulong t(0);
int l = piMin<uint>(L / 8, sizeof(t));
memcpy(&t, data_, l);
return t;
}
//! \~english Convert to unsigned long long
//! \~russian Преобразование в unsigned long long
operator ullong() {
ullong t(0);
int l = piMin<uint>(L / 8, sizeof(t));
@@ -152,6 +230,8 @@ public:
return t;
}
//! \~english Addition operator
//! \~russian Оператор сложения
uint_cl<L> operator+(const uint_cl<L> & v) {
uint_cl<L> t;
uint cv;
@@ -165,12 +245,15 @@ public:
return t;
}
//! \~english Bitwise AND operator
//! \~russian Побитовый оператор И
uint_cl<L> operator&(const uint_cl<L> & v) const {
uint_cl<L> t;
for (int i = 0; i < L / 8; ++i)
t.data_[i] = v.data_[i] & data_[i];
return t;
}
uint_cl<L> operator&(const uchar & v) const { return *this & uint_cl<L>(v); }
uint_cl<L> operator&(const ushort & v) const { return *this & uint_cl<L>(v); }
uint_cl<L> operator&(const uint & v) const { return *this & uint_cl<L>(v); }
@@ -182,12 +265,15 @@ public:
uint_cl<L> operator&(const long & v) const { return *this & uint_cl<L>(v); }
uint_cl<L> operator&(const llong & v) const { return *this & uint_cl<L>(v); }
//! \~english Bitwise OR operator
//! \~russian Побитовый оператор ИЛИ
uint_cl<L> operator|(const uint_cl<L> & v) const {
uint_cl<L> t;
for (int i = 0; i < L / 8; ++i)
t.data_[i] = v.data_[i] | data_[i];
return t;
}
uint_cl<L> operator|(const uchar & v) const { return *this | uint_cl<L>(v); }
uint_cl<L> operator|(const ushort & v) const { return *this | uint_cl<L>(v); }
uint_cl<L> operator|(const uint & v) const { return *this | uint_cl<L>(v); }
@@ -199,12 +285,15 @@ public:
uint_cl<L> operator|(const long & v) const { return *this | uint_cl<L>(v); }
uint_cl<L> operator|(const llong & v) const { return *this | uint_cl<L>(v); }
//! \~english Bitwise XOR operator
//! \~russian Побитовый оператор исключающее ИЛИ
uint_cl<L> operator^(const uint_cl<L> & v) const {
uint_cl<L> t;
for (int i = 0; i < L / 8; ++i)
t.data_[i] = v.data_[i] ^ data_[i];
return t;
}
uint_cl<L> operator^(const uchar & v) const { return *this ^ uint_cl<L>(v); }
uint_cl<L> operator^(const ushort & v) const { return *this ^ uint_cl<L>(v); }
uint_cl<L> operator^(const uint & v) const { return *this ^ uint_cl<L>(v); }
@@ -216,6 +305,8 @@ public:
uint_cl<L> operator^(const long & v) const { return *this ^ uint_cl<L>(v); }
uint_cl<L> operator^(const llong & v) const { return *this ^ uint_cl<L>(v); }
//! \~english Less than operator
//! \~russian Оператор меньше
bool operator<(const uint_cl<L> & v) const {
for (int i = 0; i < L / 8; ++i) {
if (v.data_[i] > data_[i]) return true;
@@ -223,6 +314,9 @@ public:
}
return false;
}
//! \~english Less than or equal operator
//! \~russian Оператор меньше или равно
bool operator<=(const uint_cl<L> & v) const {
for (int i = 0; i < L / 8; ++i) {
if (v.data_[i] > data_[i]) return true;
@@ -230,6 +324,9 @@ public:
}
return true;
}
//! \~english Greater than operator
//! \~russian Оператор больше
bool operator>(const uint_cl<L> & v) const {
for (int i = 0; i < L / 8; ++i) {
if (v.data_[i] < data_[i]) return true;
@@ -237,6 +334,9 @@ public:
}
return false;
}
//! \~english Greater than or equal operator
//! \~russian Оператор больше или равно
bool operator>=(const uint_cl<L> & v) const {
for (int i = 0; i < L / 8; ++i) {
if (v.data_[i] < data_[i]) return true;
@@ -244,18 +344,27 @@ public:
}
return true;
}
//! \~english Equality operator
//! \~russian Оператор равенства
bool operator==(const uint_cl<L> & v) const {
for (int i = 0; i < L / 8; ++i)
if (v.data_[i] != data_[i]) return false;
return true;
}
//! \~english Inequality operator
//! \~russian Оператор неравенства
bool operator!=(const uint_cl<L> & v) const {
for (int i = 0; i < L / 8; ++i)
if (v.data_[i] != data_[i]) return true;
return false;
}
bool operator<=(const uint_cl<8> & v1) { return (*(uchar *)data()) <= (*(uchar *)v1.data()); }
//! \~english Right shift operator
//! \~russian Оператор побитового сдвига вправо
uint_cl<L> operator>>(const int & c) const {
uint_cl<L> t;
int l = L - c;
@@ -270,7 +379,11 @@ public:
}
return t;
}
uint_cl<L> operator>>(const uint & c) const { return (*this << (int)c); }
//! \~english Left shift operator
//! \~russian Оператор побитового сдвига влево
uint_cl<L> operator<<(const int & c) const {
uint_cl<L> t;
int l = L - c;
@@ -285,19 +398,28 @@ public:
}
return t;
}
uint_cl<L> operator<<(const uint & c) const { return (*this >> (int)c); }
//! \~english In-place bitwise inversion
//! \~russian Побитовая инверсия на месте
uint_cl<L> & inverse() const {
for (int i = 0; i < L / 8; ++i)
data_[i] = ~data_[i];
return *this;
}
//! \~english Returns bitwise inverted copy
//! \~russian Возвращает копию с побитовой инверсией
uint_cl<L> inversed() const {
uint_cl<L> t(*this);
for (int i = 0; i < L / 8; ++i)
t.data_[i] = ~t.data_[i];
return t;
}
//! \~english Returns bit-reversed copy
//! \~russian Возвращает копию с переставленными битами
uint_cl<L> reversed() const {
uint_cl<L> t;
bool bit;
@@ -311,8 +433,16 @@ public:
return t;
}
//! \~english Get const pointer to data
//! \~russian Получить константный указатель на данные
const uchar * data() const { return data_; }
//! \~english Get pointer to data
//! \~russian Получить указатель на данные
uchar * data() { return data_; }
//! \~english Get data length in bytes
//! \~russian Получить длину данных в байтах
uint length() const { return L / 8; }
private:
@@ -320,6 +450,11 @@ private:
};
//! \~english Reverse byte order
//! \~russian Реверс порядка байтов
//! \details
//! \~english Reverses the bit order within a byte
//! \~russian Инвертирует порядок битов в байте
inline uchar reverseByte(uchar b) {
uchar ret = 0;
bool bit;
@@ -330,9 +465,26 @@ inline uchar reverseByte(uchar b) {
return ret;
}
//! \addtogroup Math
//! \{
//! \class PICRC
//! \brief
//! \~english CRC calculator class
//! \~russian Класс калькулятора CRC
//! \details
//! \~english Calculates CRC checksum using configurable polynomial and parameters
//! \~russian Вычисляет контрольную сумму CRC с использованием настраиваемого полинома и параметров
//! \sa CRC_32, CRC_16, CRC_8
//! \}
template<uint L, typename N = uint_cl<L>>
class PIP_EXPORT PICRC {
public:
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
//! \note
//! \~english Polynomial value defaults to zero
//! \~russian Значение полинома по умолчанию ноль
PICRC(const N & poly = N()) {
poly_ = poly;
reverse_poly = true;
@@ -341,6 +493,13 @@ public:
reverse_before_xor = reverse_data = false;
initTable();
}
//! \~english Constructor with full parameters
//! \~russian Конструктор с полными параметрами
//! \param poly polynomial value
//! \param reverse_poly_ whether to reverse polynomial bits
//! \param initial initial CRC value
//! \param out_xor XOR value for output
PICRC(const N & poly, bool reverse_poly_, const N & initial, const N & out_xor) {
poly_ = poly;
reverse_poly = reverse_poly_;
@@ -350,18 +509,33 @@ public:
initTable();
}
//! \~english Set initial CRC value
//! \~russian Установить начальное значение CRC
void setInitial(const N & v) { init_ = v; }
//! \~english Set output XOR value
//! \~russian Установить значение XOR для вывода
void setOutXor(const N & v) { out_ = v; }
//! \~english Set polynomial bit reversal
//! \~russian Установить реверс битов полинома
void setReversePolynome(bool yes) {
reverse_poly = yes;
initTable();
}
//! \~english Set output bit reversal before XOR
//! \~russian Установить реверс битов вывода перед XOR
void setReverseOutBeforeXOR(bool yes) { reverse_before_xor = yes; }
//! \~english Set data byte reversal
//! \~russian Установить реверс байтов данных
void setReverseDataBytes(bool yes) { reverse_data = yes; }
//! \~english Initialize lookup table
//! \~russian Инициализировать таблицу поиска
void initTable() {
N tmp, pol = reverse_poly ? reversed(poly_) : poly_;
// cout << std::hex << "poly " << (uint)N(poly_) << " -> " << (uint)N(pol) << endl;
for (int i = 0; i < 256; ++i) {
tmp = uchar(i);
for (int j = 0; j < 8; ++j)
@@ -370,10 +544,11 @@ public:
}
}
//! \~english Calculate CRC from raw data
//! \~russian Вычислить CRC из сырых данных
N calculate(const void * data, int size) {
N crc = init_;
uchar *data_ = (uchar *)data, cb;
// cout << "process " << size << endl;
uchar nTemp;
for (int i = 0; i < size; ++i) {
cb = data_[i];
@@ -385,7 +560,13 @@ public:
if (reverse_before_xor) crc = reversed(crc);
return crc ^ out_;
}
//! \~english Calculate CRC from PIByteArray
//! \~russian Вычислить CRC из PIByteArray
N calculate(const PIByteArray & d) { return calculate(d.data(), d.size()); }
//! \~english Calculate CRC from null-terminated string
//! \~russian Вычислить CRC из нуль-терминированной строки
N calculate(const char * str) {
PIByteArray s(PIString(str).toByteArray());
return calculate(s.data(), s.size_s());
@@ -425,22 +606,46 @@ inline uint PICRC<32, uint>::inversed(const uint & v) {
return ~v;
}
//! \~english Standard CRC-32 (Ethernet, ZIP, etc.)
//! \~russian Стандартный CRC-32 (Ethernet, ZIP и т.д.)
typedef PICRC<32, uint> CRC_32;
//! \~english Standard CRC-24
//! \~russian Стандартный CRC-24
typedef PICRC<24> CRC_24;
//! \~english Standard CRC-16
//! \~russian Стандартный CRC-16
typedef PICRC<16, ushort> CRC_16;
//! \~english Standard CRC-8
//! \~russian Стандартный CRC-8
typedef PICRC<8, uchar> CRC_8;
//! \~english Create standard CRC-32 calculator
//! \~russian Создать стандартный калькулятор CRC-32
inline CRC_32 standardCRC_32() {
return CRC_32(0x04C11DB7, true, 0xFFFFFFFF, 0xFFFFFFFF);
}
//! \~english Create standard CRC-16 calculator
//! \~russian Создать стандартный калькулятор CRC-16
inline CRC_16 standardCRC_16() {
return CRC_16(0x8005, true, 0x0, 0x0);
}
//! \~english Create standard CRC-16 Modbus calculator
//! \~russian Создать стандартный калькулятор CRC-16 Modbus
inline CRC_16 standardCRC_16_Modbus() {
return CRC_16(0x8005, 0xFFFF, 0xFFFF, false);
}
//! \~english Create standard CRC-8 calculator
//! \~russian Создать стандартный калькулятор CRC-8
inline CRC_8 standardCRC_8() {
return CRC_8(0xD5, true, 0x0, 0x0);
}
//! \}
#endif // CRC_H

View File

@@ -1,9 +1,15 @@
/*! \file pievaluator.h
* \ingroup Math
* \~\brief
* \~english Mathematic expressions calculator
* \~russian Вычислитель математических выражений
*/
//! \addtogroup Math
//! \{
//! \file pievaluator.h
//! \brief Mathematical expressions evaluator
//! \~english Mathematical expressions evaluator for parsing and calculating math expressions
//! \~russian Вычислитель математических выражений для разбора и вычисления математических выражений
//! \details
//! \~english PIEvaluator provides functionality to parse and evaluate mathematical expressions. It supports variables, functions (both
//! built-in and custom), and various operators including arithmetic, comparison, and logical operations.
//! \~russian PIEvaluator предоставляет функциональность для разбора и вычисления математических выражений. Поддерживаются переменные,
//! функции (как встроенные, так и пользовательские), а также различные операторы: арифметические, сравнения и логические.
//! \}
/*
PIP - Platform Independent Primitives
Evaluator designed for stream calculations
@@ -32,146 +38,292 @@
namespace PIEvaluatorTypes {
//! \~english Handler function type for custom functions
//! \~russian Тип обработчика для пользовательских функций
typedef std::function<complexd(void *, int, complexd *)> FuncHanlder;
//! \~english Type of evaluator element
//! \~russian Тип элемента вычислителя
enum eType {
etNumber,
etOperator,
etVariable,
etFunction
};
enum Operation {
oNone,
oAdd,
oSubtract,
oMultiply,
oDivide,
oResidue,
oPower,
oEqual,
oNotEqual,
oGreater,
oSmaller,
oGreaterEqual,
oSmallerEqual,
oAnd,
oOr,
oFunction
};
enum BaseFunctions {
bfUnknown,
bfSin,
bfCos,
bfTg,
bfCtg,
bfArcsin,
bfArccos,
bfArctg,
bfArcctg,
bfExp,
bfRandom,
bfRandomn,
bfSh,
bfCh,
bfTh,
bfCth,
bfSqrt,
bfSqr,
bfPow,
bfAbs,
bfLn,
bfLg,
bfLog,
bfSign,
bfIm,
bfRe,
bfArg,
bfLen,
bfConj,
bfRad,
bfDeg,
bfJ0,
bfJ1,
bfJN,
bfY0,
bfY1,
bfYN,
bfMin,
bfMax,
bfClamp,
bfStep,
bfMix,
bfDefined,
bfRound,
bfCustom = 0xFFFF
etNumber, //! \~english Number value
//! \~russian Числовое значение
etOperator, //! \~english Operator symbol
//! \~russian Символ оператора
etVariable, //! \~english Variable reference
//! \~russian Ссылка на переменную
etFunction //! \~english Function call
//! \~russian Вызов функции
};
//! \~english Operation types supported by evaluator
//! \~russian Типы операций, поддерживаемые вычислителем
enum Operation {
oNone, //! \~english No operation
//! \~russian Нет операции
oAdd, //! \~english Addition operator (+)
//! \~russian Оператор сложения (+)
oSubtract, //! \~english Subtraction operator (-)
//! \~russian Оператор вычитания (-)
oMultiply, //! \~english Multiplication operator (*)
//! \~russian Оператор умножения (*)
oDivide, //! \~english Division operator (/)
//! \~russian Оператор деления (/)
oResidue, //! \~english Residue operator
//! \~russian Оператор остатка
oPower, //! \~english Power operator (^)
//! \~russian Оператор возведения в степень (^)
oEqual, //! \~english Equal comparison operator (=)
//! \~russian Оператор равенства (=)
oNotEqual, //! \~english Not equal comparison operator (<>)
//! \~russian Оператор неравенства (<>)
oGreater, //! \~english Greater than comparison operator (>)
//! \~russian Оператор больше (>)
oSmaller, //! \~english Smaller than comparison operator (<)
//! \~russian Оператор меньше (<)
oGreaterEqual, //! \~english Greater or equal comparison operator (>=)
//! \~russian Оператор больше или равно (>=)
oSmallerEqual, //! \~english Smaller or equal comparison operator (<=)
//! \~russian Оператор меньше или равно (<=)
oAnd, //! \~english Logical AND operator
//! \~russian Логический оператор И
oOr, //! \~english Logical OR operator
//! \~russian Логический оператор ИЛИ
oFunction //! \~english Function call
//! \~russian Вызов функции
};
//! \~english Built-in base functions supported by evaluator
//! \~russian Встроенные базовые функции, поддерживаемые вычислителем
enum BaseFunctions {
bfUnknown, //! \~english Unknown function
//! \~russian Неизвестная функция
bfSin, //! \~english Sine function
//! \~russian Синус
bfCos, //! \~english Cosine function
//! \~russian Косинус
bfTg, //! \~english Tangent function
//! \~russian Тангенс
bfCtg, //! \~english Cotangent function
//! \~russian Котангенс
bfArcsin, //! \~english Arcsine function
//! \~russian Арксинус
bfArccos, //! \~english Arccosine function
//! \~russian Арккосинус
bfArctg, //! \~english Arctangent function
//! \~russian Арктангенс
bfArcctg, //! \~english Arccotangent function
//! \~russian Арккотангенс
bfExp, //! \~english Exponential function (e^x)
//! \~russian Экспонента (e^x)
bfRandom, //! \~english Random number generator [0,1]
//! \~russian Генератор случайных чисел [0,1]
bfRandomn, //! \~english Normal distribution random number
//! \~russian Случайное число с нормальным распределением
bfSh, //! \~english Hyperbolic sine
//! \~russian Гиперболический синус
bfCh, //! \~english Hyperbolic cosine
//! \~russian Гиперболический косинус
bfTh, //! \~english Hyperbolic tangent
//! \~russian Гиперболический тангенс
bfCth, //! \~english Hyperbolic cotangent
//! \~russian Гиперболический котангенс
bfSqrt, //! \~english Square root
//! \~russian Квадратный корень
bfSqr, //! \~english Square function (x^2)
//! \~russian Возведение в квадрат (x^2)
bfPow, //! \~english Power function
//! \~russian Возведение в степень
bfAbs, //! \~english Absolute value
//! \~russian Модуль (абсолютное значение)
bfLn, //! \~english Natural logarithm
//! \~russian Натуральный логарифм
bfLg, //! \~english Base-10 logarithm
//! \~russian Десятичный логарифм
bfLog, //! \~english Base-N logarithm
//! \~russian Логарифм по основанию N
bfSign, //! \~english Sign function
//! \~russian Знак числа
bfIm, //! \~english Imaginary part of complex number
//! \~russian Мнимая часть комплексного числа
bfRe, //! \~english Real part of complex number
//! \~russian Действительная часть комплексного числа
bfArg, //! \~english Argument (phase) of complex number
//! \~russian Аргумент (фаза) комплексного числа
bfLen, //! \~english Length (magnitude) of complex number
//! \~russian Модуль (длина) комплексного числа
bfConj, //! \~english Complex conjugate
//! \~russian Комплексно-сопряженное
bfRad, //! \~english Convert degrees to radians
//! \~russian Перевод градусов в радианы
bfDeg, //! \~english Convert radians to degrees
//! \~russian Перевод радианов в градусы
bfJ0, //! \~english Bessel function J0
//! \~russian Функция Бесселя J0
bfJ1, //! \~english Bessel function J1
//! \~russian Функция Бесселя J1
bfJN, //! \~english Bessel function Jn
//! \~russian Функция Бесселя Jn
bfY0, //! \~english Bessel function Y0
//! \~russian Функция Бесселя Y0
bfY1, //! \~english Bessel function Y1
//! \~russian Функция Бесселя Y1
bfYN, //! \~english Bessel function Yn
//! \~russian Функция Бесселя Yn
bfMin, //! \~english Minimum of two values
//! \~russian Минимум из двух значений
bfMax, //! \~english Maximum of two values
//! \~russian Максимум из двух значений
bfClamp, //! \~english Clamp value to range
//! \~russian Ограничение значения диапазоном
bfStep, //! \~english Step function
//! \~russian Ступенчатая функция
bfMix, //! \~english Mix/interpolation function
//! \~russian Смешивание/интерполяция
bfDefined, //! \~english Check if variable is defined
//! \~russian Проверка определена ли переменная
bfRound, //! \~english Round to nearest integer
//! \~russian Округление до ближайшего целого
bfCustom = 0xFFFF //! \~english Custom user-defined function
//! \~russian Пользовательская функция
};
//! \~english Instruction for evaluator - contains operation, output index, function index and operator indices
//! \~russian Инструкция для вычислителя - содержит операцию, индекс вывода, индекс функции и индексы операторов
struct PIP_EXPORT Instruction {
//! \~english Constructs empty instruction with default values
//! \~russian Создает пустую инструкцию со значениями по умолчанию
Instruction() {
out = -1;
function = -1;
operation = oNone;
}
//! \~english Constructs instruction with operation, operators, output index and optional function index
//! \~russian Создает инструкцию с указанной операцией, операторами, индексом вывода и опционально индексом функции
Instruction(Operation oper, PIVector<short> opers, short out_ind, short func = -1) {
operation = oper;
operators = opers;
out = out_ind;
function = func;
}
//! \~english Operation to perform
//! \~russian Операция для выполнения
Operation operation;
//! \~english Output variable index
//! \~russian Индекс переменной вывода
short out;
//! \~english Function index (if operation is function call)
//! \~russian Индекс функции (если операция - вызов функции)
short function;
//! \~english Input operator indices
//! \~russian Индексы входных операторов
PIVector<short> operators;
};
//! \~english Evaluator element representing a number, variable, operator or function
//! \~russian Элемент вычислителя, представляющий число, переменную, оператор или функцию
struct PIP_EXPORT Element {
//! \~english Constructs empty element (number type with value 0)
//! \~russian Создает пустой элемент (тип число со значением 0)
Element() {
num = 0;
var_num = -1;
type = etNumber;
}
//! \~english Constructs element with specified type and numeric/variable indices
//! \~russian Создает элемент с указанным типом и числовыми/переменными индексами
Element(eType new_type, short new_num, short new_var_num = -1) { set(new_type, new_num, new_var_num); }
//! \~english Sets element type and numeric/variable indices
//! \~russian Устанавливает тип элемента и числовые/переменные индексы
void set(eType new_type, short new_num, short new_var_num = -1) {
type = new_type;
num = new_num;
var_num = new_var_num;
}
//! \~english Element type (number, operator, variable or function)
//! \~russian Тип элемента (число, оператор, переменная или функция)
eType type;
//! Numeric value or operator index
//! \~english Numeric value or operator index
//! \~russian Числовое значение или индекс оператора
short num;
//! Variable index (for variables)
//! \~english Variable index (for variables)
//! \~russian Индекс переменной (для переменных)
short var_num;
};
//! \~english Function definition with name, type and handler
//! \~russian Определение функции с именем, типом и обработчиком
struct PIP_EXPORT Function {
//! \~english Constructs unknown function with 0 arguments
//! \~russian Создает неизвестную функцию с 0 аргументами
Function() {
arguments = 0;
type = bfUnknown;
handler = nullptr;
}
//! \~english Constructs built-in function with name, argument count and base function type
//! \~russian Создает встроенную функцию с именем, количеством аргументов и типом базовой функции
Function(const PIString & name, short args, BaseFunctions ftype) {
identifier = name;
arguments = args;
type = ftype;
handler = nullptr;
}
//! \~english Constructs custom function with name, argument count and handler function
//! \~russian Создает пользовательскую функцию с именем, количеством аргументов и обработчиком
Function(const PIString & name, short args, FuncHanlder h) {
identifier = name;
arguments = args;
type = bfCustom;
handler = h;
}
//! \~english Function identifier (name)
//! \~russian Идентификатор функции (имя)
PIString identifier;
//! \~english Function type (built-in or custom)
//! \~russian Тип функции (встроенная или пользовательская)
BaseFunctions type;
//! \~english Handler function for custom functions
//! \~russian Обработчик для пользовательских функций
FuncHanlder handler;
//! \~english Number of arguments
//! \~russian Количество аргументов
short arguments;
};
//! \~english Variable definition with name and value
//! \~russian Определение переменной с именем и значением
struct PIP_EXPORT Variable {
//! \~english Constructs variable with value 0
//! \~russian Создает переменную со значением 0
Variable() { value = 0.; }
//! \~english Constructs variable with name and value
//! \~russian Создает переменную с именем и значением
Variable(const PIString & var_name, complexd val) {
name = var_name;
value = val;
}
//! \~english Variable name
//! \~russian Имя переменной
PIString name;
//! \~english Variable value
//! \~russian Значение переменной
complexd value;
};
} // namespace PIEvaluatorTypes
//! \~english Evaluator types namespace containing enums, structs and types for PIEvaluator
//! \~russian Пространство имен типов вычислителя, содержащее перечисления, структуры и типы для PIEvaluator
/*
≠ :
≥ }
@@ -180,36 +332,106 @@ struct PIP_EXPORT Variable {
|
*/
//! Content container for variables and functions
//! \~english Container for variables and functions of the evaluator
//! \~russian Контейнер для переменных и функций вычислителя
//! \details
//! \~english PIEvaluatorContent manages variables and functions for the evaluator. It provides methods to add, find,
//! and retrieve functions and variables.
//! \~russian PIEvaluatorContent управляет переменными и функциями вычислителя. Он предоставляет методы для добавления,
//! поиска и получения функций и переменных.
class PIP_EXPORT PIEvaluatorContent {
friend class PIEvaluator;
BINARY_STREAM_FRIEND(PIEvaluatorContent);
public:
//! \~english Constructs an empty evaluator content
//! \~russian Создает пустой контент вычислителя
PIEvaluatorContent();
~PIEvaluatorContent() { ; }
//! \~english Add function with name and default 1 argument
//! \~russian Добавить функцию с указанным именем и количеством аргументов (по умолчанию 1)
void addFunction(const PIString & name, int args = 1);
//! \~english Add variable with name and optional value, returns variable index
//! \~russian Добавить переменную с указанным именем и значением, возвращает индекс переменной
int addVariable(const PIString & name, const complexd & val = 0.);
//! \~english Add custom function with name, argument count and handler function
//! \~russian Добавить пользовательскую функцию с обработчиком
void addCustomFunction(const PIString & name, int args_count, PIEvaluatorTypes::FuncHanlder func);
//! \~english Returns number of registered functions
//! \~russian Возвращает количество зарегистрированных функций
int functionsCount() const { return functions.size(); }
//! \~english Returns number of registered variables
//! \~russian Возвращает количество зарегистрированных переменных
int variablesCount() const { return variables.size(); }
//! \~english Returns number of custom variables
//! \~russian Возвращает количество пользовательских переменных
int customVariablesCount() const;
//! \~english Finds function index by name, returns -1 if not found
//! \~russian Находит индекс функции по имени, возвращает -1 если не найдена
int findFunction(const PIString & name) const;
//! \~english Finds variable index by name, returns -1 if not found
//! \~russian Находит индекс переменной по имени, возвращает -1 если не найдена
int findVariable(const PIString & var_name) const;
//! \~english Returns function by index
//! \~russian Возвращает функцию по индексу
PIEvaluatorTypes::Function function(int index);
//! \~english Returns variable by index
//! \~russian Возвращает переменную по индексу
PIEvaluatorTypes::Variable variable(int index);
//! \~english Returns function by name, convenience wrapper
//! \~russian Возвращает функцию по имени, удобная обертка
PIEvaluatorTypes::Function function(const PIString & name) { return function(findFunction(name)); }
//! \~english Returns variable by name, convenience wrapper
//! \~russian Возвращает переменную по имени, удобная обертка
PIEvaluatorTypes::Variable variable(const PIString & name) { return variable(findVariable(name)); }
//! \~english Returns custom variable by index
//! \~russian Возвращает пользовательскую переменную по индексу
PIEvaluatorTypes::Variable customVariable(int index);
//! \~english Set variable value by index, returns true on success
//! \~russian Установить значение переменной по индексу, возвращает true при успехе
bool setVariableValue(int index, complexd new_value);
//! \~english Set variable value by name, returns true on success
//! \~russian Установить значение переменной по имени, возвращает true при успехе
bool setVariableValue(const PIString & var_name, const complexd & new_value) {
return setVariableValue(findVariable(var_name), new_value);
}
//! \~english Set variable name by index
//! \~russian Установить имя переменной по индексу
bool setVariableName(int index, const PIString & new_name);
bool setVariableName(const PIString & var_name, const PIString & new_name) { return setVariableName(findVariable(var_name), new_name); }
//! \~english Set variable name by name, convenience wrapper
//! \~russian Установить имя переменной по имени, удобная обертка
bool setVariableName(const PIString & var_name, const PIString & new_name) {
return setVariableName(findVariable(var_name), new_name);
}
//! \~english Clears all custom variables
//! \~russian Очищает все пользовательские переменные
void clearCustomVariables();
//! \~english Returns base function type by name
//! \~russian Возвращает тип базовой функции по имени
PIEvaluatorTypes::BaseFunctions getBaseFunction(const PIString & name);
//! \~english Print all functions and variables to console
//! \~russian Вывести все функции и переменные в консоль
void dump();
private:
@@ -219,9 +441,18 @@ private:
};
//! Main evaluator class for parsing and calculating mathematical expressions
//! \~english Main class for parsing and evaluating mathematical expressions
//! \~russian Главный класс для разбора и вычисления математических выражений
//! \details
//! \~english PIEvaluator provides functionality to parse and evaluate mathematical expressions. It supports variables, functions (both
//! built-in and custom), and various operators including arithmetic, comparison, and logical operations.
//! \~russian PIEvaluator предоставляет функциональность для разбора и вычисления математических выражений. Поддерживаются переменные,
//! функции (как встроенные, так и пользовательские), а также различные операторы: арифметические, сравнения и логические.
class PIP_EXPORT PIEvaluator {
public:
//! Constructs an empty evaluator
//! \~english Constructs an empty evaluator
//! \~russian Создает пустой вычислитель
PIEvaluator() {
correct = false;
data_ = 0;
@@ -230,53 +461,68 @@ public:
~PIEvaluator() { ; }
//! Returns custom data
//! \~english Returns custom data pointer
//! \~russian Возвращает указатель на пользовательские данные
void * data() { return data_; }
//! Set custom data to "_data"
//! \~english Sets custom data pointer to "_data"
//! \~russian Устанавливает указатель на пользовательские данные в "_data"
void setData(void * _data) { data_ = _data; }
//! Check mathematical expression and parse it to list of instructions
//! \~english Checks mathematical expression and parses it to list of instructions
//! \~russian Проверяет математическое выражение и разбирает его в список инструкций
bool check(const PIString & string);
//! Returns true if expression was checked succesfully
//! \~english Returns true if expression was checked successfully
//! \~russian Возвращает true если выражение было успешно проверено
bool isCorrect() const { return correct; }
//! Set variable value with name "name" to value "value". Add variable if it doesn`t exists
//! \~english Set variable value by name, adds variable if it does not exist, returns variable index
//! \~russian Устанавливает значение переменной по имени, добавляет переменную если она не существует, возвращает индекс переменной
int setVariable(const PIString & name, complexd value = complexd(0.));
//! Set variable value with index "index" to value "value". Don`t add variable if it doesn`t exists
//! \~english Set variable value by index, does not add variable if it does not exist
//! \~russian Устанавливает значение переменной по индексу, не добавляет переменную если она не существует
void setVariable(int index, complexd value = 0.);
//! Evaluate last successfully checked with function \a check() expression and returns result
//! \~english Evaluates the last successfully checked expression and returns result
//! \~russian Вычисляет последнее успешно проверенное выражение и возвращает результат
complexd evaluate();
//! Remove all manually added variables
//! \~english Removes all manually added variables
//! \~russian Удаляет все вручную добавленные переменные
void clearCustomVariables() { content.clearCustomVariables(); }
//! Returns index of variable with name "name"
//! \~english Returns index of variable by name
//! \~russian Возвращает индекс переменной по имени
int variableIndex(const PIString & name) const { return content.findVariable(name); }
//! Returns all unknown variables founded in last expression passed to \a check() function
//! \~english Returns list of unknown variables found in the last expression passed to check()
//! \~russian Возвращает список неизвестных переменных, найденных в последнем выражении, переданном в check()
const PIStringList & unknownVariables() const { return unknownVars; }
//! Returns all used variables founded in last expression passed to \a check() function
//! \~english Returns list of used variables found in the last expression passed to check()
//! \~russian Возвращает список используемых переменных, найденных в последнем выражении, переданном в check()
const PIStringList & usedVariables() const { return usedVars; }
//! Returns processed last expression passed to \a check() function
//! \~english Returns the last expression passed to check()
//! \~russian Возвращает последнее выражение, переданное в check()
const PIString & expression() const { return currentString; }
//! Returns last error description occured in \a check() function
//! \~english Returns last error description from check() function
//! \~russian Возвращает описание последней ошибки из функции check()
const PIString & error() const { return lastError; }
//! Returns last result of \a evaluate()
//! \~english Returns last result of evaluate()
//! \~russian Возвращает последний результат evaluate()
const complexd & lastResult() const { return out; }
//! Save to %PIByteArray evaluator state (expression, variables, errors, compiled instructions)
//! \~english Saves evaluator state to PIByteArray (expression, variables, errors, compiled instructions)
//! \~russian Сохраняет состояние вычислителя в PIByteArray (выражение, переменные, ошибки, скомпилированные инструкции)
PIByteArray save() const;
//! Restore from %PIByteArray evaluator state (expression, variables, errors, compiled instructions)
//! \~english Restores evaluator state from PIByteArray (expression, variables, errors, compiled instructions)
//! \~russian Восстанавливает состояние вычислителя из PIByteArray (выражение, переменные, ошибки, скомпилированные инструкции)
void load(PIByteArray ba);
private:

View File

@@ -1,10 +1,13 @@
/*! \file pifft.h
* \ingroup Math
* \ingroup FFTW
* \~\brief
* \~english FFT, IFFT and Hilbert transformations
* \~russian БПФ, ОБПФ и преобразования Гильберта
*/
//! \addtogroup Math
//! \{
//! \file pifft.h
//! \brief
//! \~english Declares \a PIFFT classes
//! \~russian Объявление классов \a PIFFT
//! \~\authors
//! \~english Ivan Pelipenko peri4ko@yandex.ru; Andrey Bychkov work.a.b@yandex.ru
//! \~russian Иван Пелипенко peri4ko@yandex.ru; Андрей Бычков work.a.b@yandex.ru
//! \~\}
/*
PIP - Platform Independent Primitives
Class for FFT, IFFT and Hilbert transformations
@@ -64,16 +67,40 @@
# include "pip_fftw_export.h"
//! \addtogroup Math
//! \{
//! \class PIFFT_double
//! \brief
//! \~english Fast Fourier Transform implementation for double precision.
//! \~russian Реализация быстрого преобразования Фурье для двойной точности.
//! \~\}
//! \sa \a PIFFT_float, \a PIFFTW
class PIP_EXPORT PIFFT_double {
public:
//! \~english Default constructor.
//! \~russian Конструктор по умолчанию.
PIFFT_double();
//! \~english Calculate FFT from complex vector.
//! \~russian Вычисление БПФ из комплексного вектора.
PIVector<complexd> * calcFFT(const PIVector<complexd> & val);
//! \~english Calculate FFT from real vector.
//! \~russian Вычисление БПФ из вещественного вектора.
PIVector<complexd> * calcFFT(const PIVector<double> & val);
//! \~english Calculate inverse FFT.
//! \~russian Вычисление обратного БПФ.
PIVector<complexd> * calcFFTinverse(const PIVector<complexd> & val);
//! \~english Calculate Hilbert transform.
//! \~russian Вычисление преобразования Гильберта.
PIVector<complexd> * calcHilbert(const PIVector<double> & val);
//! \~english Get amplitude spectrum.
//! \~russian Получить амплитудный спектр.
PIVector<double> getAmplitude() const;
//! \~english Get real part.
//! \~russian Получить действительную часть.
PIVector<double> getReal() const;
//! \~english Get imaginary part.
//! \~russian Получить мнимую часть.
PIVector<double> getImag() const;
private:
@@ -118,16 +145,40 @@ private:
void ftbase_ffttwcalc(PIVector<double> * a, int aoffset, int n1, int n2);
};
//! \addtogroup Math
//! \{
//! \class PIFFT_float
//! \brief
//! \~english Fast Fourier Transform implementation for single precision.
//! \~russian Реализация быстрого преобразования Фурье для одинарной точности.
//! \~\}
//! \sa \a PIFFT_double, \a PIFFTW
class PIP_EXPORT PIFFT_float {
public:
//! \~english Default constructor.
//! \~russian Конструктор по умолчанию.
PIFFT_float();
//! \~english Calculate FFT from complex vector.
//! \~russian Вычисление БПФ из комплексного вектора.
PIVector<complexf> * calcFFT(const PIVector<complexf> & val);
//! \~english Calculate FFT from real vector.
//! \~russian Вычисление БПФ из вещественного вектора.
PIVector<complexf> * calcFFT(const PIVector<float> & val);
//! \~english Calculate inverse FFT.
//! \~russian Вычисление обратного БПФ.
PIVector<complexf> * calcFFTinverse(const PIVector<complexf> & val);
//! \~english Calculate Hilbert transform.
//! \~russian Вычисление преобразования Гильберта.
PIVector<complexf> * calcHilbert(const PIVector<float> & val);
//! \~english Get amplitude spectrum.
//! \~russian Получить амплитудный спектр.
PIVector<float> getAmplitude() const;
//! \~english Get real part.
//! \~russian Получить действительную часть.
PIVector<float> getReal() const;
//! \~english Get imaginary part.
//! \~russian Получить мнимую часть.
PIVector<float> getImag() const;
private:
@@ -171,6 +222,7 @@ private:
void ftbase_fftirltrec(PIVector<float> * a, int astart, int astride, PIVector<float> * b, int bstart, int bstride, int m, int n);
void ftbase_ffttwcalc(PIVector<float> * a, int aoffset, int n1, int n2);
};
//! \~\}
typedef PIFFT_double PIFFT;
typedef PIFFT_double PIFFTd;
@@ -193,25 +245,48 @@ _PIFFTW_H(float)
_PIFFTW_H(double)
_PIFFTW_H(ldouble)
//! \addtogroup FFTW
//! \{
//! \class PIFFTW
//! \brief
//! \~english FFTW wrapper for arbitrary precision types.
//! \~russian Обёртка FFTW для типов произвольной точности.
//! \~\}
//! \note Requires linking against libfftw3
//! \sa \a PIFFT_double, \a PIFFT_float
template<typename T>
class PIFFTW {
public:
//! \~english Default constructor.
//! \~russian Конструктор по умолчанию.
explicit PIFFTW() {
p = 0;
newP(p);
}
//! \~english Destructor.
//! \~russian Деструктор.
~PIFFTW() { deleteP(p); }
//! \~english Calculate FFT from complex vector.
//! \~russian Вычисление БПФ из комплексного вектора.
inline const PIVector<complex<T>> & calcFFT(const PIVector<complex<T>> & in) { return PIVector<complex<T>>().resize(in.size()); }
//! \~english Calculate FFT from real vector.
//! \~russian Вычисление БПФ из вещественного вектора.
inline const PIVector<complex<T>> & calcFFT(const PIVector<T> & in) { return PIVector<complex<T>>().resize(in.size()); }
//! \~english Calculate inverse FFT.
//! \~russian Вычисление обратного БПФ.
inline const PIVector<complex<T>> & calcFFTinverse(const PIVector<complex<T>> & in) { return PIVector<complex<T>>().resize(in.size()); }
//! \~english FFT operation type.
//! \~russian Тип операции БПФ.
enum FFT_Operation {
foReal,
foComplex,
foInverse
};
//! \~english Prepare computation plan.
//! \~russian Подготовить план вычислений.
inline void preparePlan(int size, FFT_Operation op) {}
private:
@@ -222,6 +297,7 @@ private:
void * p;
};
//! \~\}
template<>

View File

@@ -1,21 +1,13 @@
/*! \file pigeometry.h
* \ingroup Math
* \brief
* \~english Geometry base classes
* \~russian Базовые геометрические классы
* \~\details
* \~english
* Add \a PIPoint, \a PILine and \a PIRect classes.
* \~russian
* Содержит классы: \a PIPoint, \a PILine и \a PIRect.
* * \~\authors
* \~english
* Ivan Pelipenko peri4ko@yandex.ru;
* Andrey Bychkov work.a.b@yandex.ru;
* \~russian
* Иван Пелипенко peri4ko@yandex.ru;
* Андрей Бычков work.a.b@yandex.ru;
*/
//! \addtogroup Math
//! \{
//! \file pigeometry.h
//! \brief
//! \~english Geometry utilities
//! \~russian Геометрические утилиты
//! \~\authors
//! \~english Ivan Pelipenko peri4ko@yandex.ru; Andrey Bychkov work.a.b@yandex.ru
//! \~russian Иван Пелипенко peri4ko@yandex.ru; Андрей Бычков work.a.b@yandex.ru
//! \~\}
/*
PIP - Platform Independent Primitives
Geometry base classes

View File

@@ -43,98 +43,99 @@ public:
PIPoint<Type> p0;
PIPoint<Type> p1;
//! \~russian
//! Пустой конструктор.
//! \~english Default constructor.
//! \~russian Пустой конструктор.
//! \details
//! При выполнении пустого конструктора координаты не изменяются.
//! Начало и конец совпадают.
//! \~english Creates an empty line where start and end points coincide.
//! \~russian При выполнении пустого конструктора координаты не изменяются. Начало и конец совпадают.
PILine() {}
//! \~russian
//! Создает линию по двум принятым точкам \a PIPoint начала и конца.
//! \~english Creates a line from two points.
//! \~russian Создает линию по двум принятым точкам \a PIPoint начала и конца.
PILine(const PIPoint<Type> & p0_, const PIPoint<Type> & p1_) {
p0 = p0_;
p1 = p1_;
}
//! \~russian
//! Создает линию по принятым координатам начала и конца.
//! \~english Creates a line from coordinates.
//! \~russian Создает линию по принятым координатам начала и конца.
PILine(Type x0, Type y0, Type x1, Type y1) {
p0.set(x0, y0);
p1.set(x1, y1);
}
//! \~russian
//! Задать новые координаты начала и конца по двум принятым точкам \a PIPoint.
//! \~english Set new coordinates from two points.
//! \~russian Задать новые координаты начала и конца по двум принятым точкам \a PIPoint.
PILine<Type> & set(const PIPoint<Type> & p0_, const PIPoint<Type> & p1_) {
p0 = p0_;
p1 = p1_;
return *this;
}
//! \~russian
//! Задать новые координаты начала и конца.
//! \~english Set new coordinates.
//! \~russian Задать новые координаты начала и конца.
PILine<Type> & set(Type x0, Type y0, Type x1, Type y1) {
p0.set(x0, y0);
p1.set(x1, y1);
return *this;
}
//! \~russian
//! Проверить на совпадение координат начала и конца.
//! \~english Check if start and end points coincide.
//! \~russian Проверить на совпадение координат начала и конца.
bool isEmpty() const { return (p0 == p1); }
//! \~russian
//! Вычислить ширину прямоугольника, диагональю которого является данный отрезок.
//! \~english Calculate width of the rectangle whose diagonal is this line.
//! \~russian Вычислить ширину прямоугольника, диагональю которого является данный отрезок.
Type width() const { return piAbs<Type>(p1.x - p0.x); }
//! \~russian
//! Вычислить высоту прямоугольника, диагональю которого является данный отрезок.
//! \~english Calculate height of the rectangle whose diagonal is this line.
//! \~russian Вычислить высоту прямоугольника, диагональю которого является данный отрезок.
Type height() const { return piAbs<Type>(p1.y - p0.y); }
//! \~russian
//! Сдвинуть линию на \a x, \a y.
//! \~english Translate line by x and y.
//! \~russian Сдвинуть линию на \a x, \a y.
PILine<Type> & translate(Type x, Type y) {
p0.translate(x, y);
p1.translate(x, y);
return *this;
}
//! \~russian
//! Сдвинуть линию на значение координат точки \a PIPoint.
//! \~english Translate line by point coordinates.
//! \~russian Сдвинуть линию на значение координат точки \a PIPoint.
PILine<Type> & translate(const PIPoint<Type> & p) {
p0.translate(p);
p1.translate(p);
return *this;
}
//! Создать копию отрезка и сдвинуть её на `x` и `y`.
//! \~english Create a copy of the line and translate it by x and y.
//! \~russian Создать копию отрезка и сдвинуть её на `x` и `y`.
PILine<Type> translated(Type x, Type y) const {
PILine<Type> l(*this);
l.translate(x, y);
return l;
}
//! \~russian
//! Создать копию отрезка и сдвинуть её на значение координат точки \a PIPoint.
//! \~english Create a copy of the line and translate it by point coordinates.
//! \~russian Создать копию отрезка и сдвинуть её на значение координат точки \a PIPoint.
PILine<Type> translated(const PIPoint<Type> & p) const {
PILine<Type> l(*this);
l.translate(p);
return l;
}
//! \~russian
//! Сдвинуть линию на \a x, \a y.
//! \~english Translate line by x and y. Alias for \a translate().
//! \~russian Сдвинуть линию на \a x, \a y.
//! \details Является копией метода \a translate().
PILine<Type> & move(Type x, Type y) { return translate(x, y); }
//! \~russian
//! Сдвинуть линию на значение координат точки \a PIPoint.
//! \~english Translate line by point coordinates. Alias for \a translate().
//! \~russian Сдвинуть линию на значение координат точки \a PIPoint.
//! \details Является копией метода \a translate().
PILine<Type> & move(const PIPoint<Type> & p) { return translate(p); }
//! \~russian
//! Создать копию отрезка и сдвинуть её на \a x, \a y.
//! \~english Create a copy of the line and translate it by x and y. Alias for \a translated().
//! \~russian Создать копию отрезка и сдвинуть её на \a x, \a y.
//! \details Является копией метода \a translated().
PILine<Type> moved(Type x, Type y) const {
PILine<Type> l(*this);
@@ -142,8 +143,8 @@ public:
return l;
}
//! \~russian
//! Создать копию отрезка и сдвинуть её на значение координат точки \a PIPoint.
//! \~english Create a copy of the line and translate it by point coordinates. Alias for \a translated().
//! \~russian Создать копию отрезка и сдвинуть её на значение координат точки \a PIPoint.
//! \details Является копией метода \a translated().
PILine<Type> moved(const PIPoint<Type> & p) const {
PILine<Type> l(*this);
@@ -151,31 +152,40 @@ public:
return l;
}
//! \~english Translate line by x value on both coordinates.
//! \~russian Сдвинуть линию по двум координатам на значение \a x.
void operator+=(Type x) { translate(x, x); }
//! \~english Translate line by point coordinates.
//! \~russian Сдвинуть линию по двум координатам на величину координат точки \a PIPoint.
void operator+=(const PIPoint<Type> & p) { translate(p); }
//! \~english Translate line by negative x value on both coordinates.
//! \~russian Сдвинуть линию по двум координатам на значение \a x.
void operator-=(Type x) { translate(-x, -x); }
//! \~english Translate line by negative point coordinates.
//! \~russian Сдвинуть линию по двум координатам на величину координат точки \a PIPoint.
void operator-=(const PIPoint<Type> & p) { translate(-p); }
//! \~english Translate line by point coordinates.
//! \~russian Сдвинуть линию по двум координатам на величину координат точки \a PIPoint.
PILine<Type> operator+(const PIPoint<Type> & p) { return translated(p); }
//! \~english Translate line by negative point coordinates.
//! \~russian Сдвинуть линию по двум координатам на величину координат точки \a PIPoint.
PILine<Type> operator-(const PIPoint<Type> & p) { return translated(-p); }
//! \~english Check equality of two lines.
//! \~russian Проверить равенство координат двух отрезков.
bool operator==(const PILine<Type> & r) const { return (p0 == r.p0 && p1 == r.p1); }
//! \~english Check inequality of two lines.
//! \~russian Проверить неравенство координат двух отрезков.
bool operator!=(const PILine<Type> & r) const { return (p1 != r.p1 || p1 != r.p1); }
};
//! \~english Stream output operator for PILine.
//! \~russian Перегруженный оператор для вывода координат в \a PICout.
template<typename Type>
PICout operator<<(PICout & s, const PILine<Type> & v) {

View File

@@ -1,9 +1,13 @@
/*! \file pimathbase.h
* \ingroup Math
* \~\brief
* \~english Basic mathematical functions and defines
* \~russian Базовые математические функции и дефайны
*/
//! \addtogroup Math
//! \{
//! \file pimathbase.h
//! \brief
//! \~english Basic mathematical functions and defines
//! \~russian Базовые математические функции и дефайны
//! \details
//! \~english Common mathematical constants, conversion functions and utility functions
//! \~russian Общие математические константы, функции преобразования и утилиты
//! \}
/*
PIP - Platform Independent Primitives
Basic mathematical functions and defines
@@ -114,11 +118,29 @@ inline double sinc(const double & v) {
return sin(t) / t;
}
//! Bessel function of the first kind of order 0
//! \~english Bessel function J0(x)
//! \~russian Функция Бесселя первого рода порядка 0
PIP_EXPORT double piJ0(const double & v);
//! Bessel function of the first kind of order 1
//! \~english Bessel function J1(x)
//! \~russian Функция Бесселя первого рода порядка 1
PIP_EXPORT double piJ1(const double & v);
//! Bessel function of the first kind of order n
//! \~english Bessel function Jn(n, x)
//! \~russian Функция Бесселя первого рода порядка n
PIP_EXPORT double piJn(int n, const double & v);
//! Bessel function of the second kind of order 0
//! \~english Bessel function Y0(x)
//! \~russian Функция Бесселя второго рода порядка 0
PIP_EXPORT double piY0(const double & v);
//! Bessel function of the second kind of order 1
//! \~english Bessel function Y1(x)
//! \~russian Функция Бесселя второго рода порядка 1
PIP_EXPORT double piY1(const double & v);
//! Bessel function of the second kind of order n
//! \~english Bessel function Yn(n, x)
//! \~russian Функция Бесселя второго рода порядка n
PIP_EXPORT double piYn(int n, const double & v);
// clang-format off
@@ -129,24 +151,45 @@ inline constexpr float toDeg(float rad) {return rad * M_180_PI;}
inline constexpr double toDeg(double rad) {return rad * M_180_PI;}
inline constexpr ldouble toDeg(ldouble rad) {return rad * M_180_PI;}
// clang-format on
//! Square of a value
//! \~english Returns the square of value v (v * v)
//! \~russian Возвращает квадрат значения v (v * v)
template<typename T>
inline constexpr T sqr(const T & v) {
return v * v;
}
//! Convert linear value to decibels
//! \~english Convert linear value to decibels: 10 * log10(val)
//! \~russian Преобразовать линейное значение в децибелы: 10 * log10(val)
template<typename T>
inline constexpr T toDb(T val) {
return T(10.) * std::log10(val);
}
//! Convert decibels to linear value
//! \~english Convert decibels to linear value: 10^(val/10)
//! \~russian Преобразовать децибелы в линейное значение: 10^(val/10)
template<typename T>
inline constexpr T fromDb(T val) {
return std::pow(T(10.), val / T(10.));
}
// [-1 ; 1]
//! Generate random double in range [-1, 1]
//! \~english Returns random double in range [-1, 1]
//! \~russian Генерирует случайное число double в диапазоне [-1, 1]
PIP_EXPORT double randomd();
// [-1 ; 1] normal
//! Generate random double with normal (Gaussian) distribution
//! \~english Returns random double with normal distribution, mean=dv, stddev=sv
//! \~russian Генерирует случайное число double с нормальным распределением, среднее=dv, стандартное отклонение=sv
PIP_EXPORT double randomn(double dv = 0., double sv = 1.);
//! Absolute value of vector elements
//! \~english Returns vector with absolute values of each element
//! \~russian Возвращает вектор с абсолютными значениями каждого элемента
template<typename T>
inline PIVector<T> piAbs(const PIVector<T> & v) {
PIVector<T> result;
@@ -157,6 +200,9 @@ inline PIVector<T> piAbs(const PIVector<T> & v) {
}
//! Normalize angle to [0, 360) range (in-place)
//! \~english Normalizes angle to range [0, 360) degrees
//! \~russian Нормализует угол в диапазон [0, 360) градусов (на месте)
template<typename T>
void normalizeAngleDeg360(T & a) {
while (a < 0.)
@@ -164,6 +210,9 @@ void normalizeAngleDeg360(T & a) {
while (a > 360.)
a -= 360.;
}
//! Normalize angle to [0, 360) range
//! \~english Returns angle normalized to range [0, 360) degrees
//! \~russian Возвращает угол нормализованный в диапазон [0, 360) градусов
template<typename T>
double normalizedAngleDeg360(T a) {
normalizeAngleDeg360(a);
@@ -171,6 +220,9 @@ double normalizedAngleDeg360(T a) {
}
//! Normalize angle to (-180, 180] range (in-place)
//! \~english Normalizes angle to range (-180, 180] degrees
//! \~russian Нормализует угол в диапазон (-180, 180] градусов (на месте)
template<typename T>
void normalizeAngleDeg180(T & a) {
while (a < -180.)
@@ -178,6 +230,9 @@ void normalizeAngleDeg180(T & a) {
while (a > 180.)
a -= 360.;
}
//! Normalize angle to (-180, 180] range
//! \~english Returns angle normalized to range (-180, 180] degrees
//! \~russian Возвращает угол нормализованный в диапазон (-180, 180] градусов
template<typename T>
double normalizedAngleDeg180(T a) {
normalizeAngleDeg180(a);
@@ -185,6 +240,13 @@ double normalizedAngleDeg180(T a) {
}
//! Ordinary Least Squares linear regression
//! \~english Calculates linear regression coefficients using OLS method
//! \~russian Вычисляет коэффициенты линейной регрессии методом наименьших квадратов
//! \param input Vector of (x, y) pairs
//! \param out_a Output pointer for slope coefficient (a), can be nullptr
//! \param out_b Output pointer for intercept coefficient (b), can be nullptr
//! \return true on success
template<typename T>
bool OLS_Linear(const PIVector<PIPair<T, T>> & input, T * out_a, T * out_b) {
static_assert(std::is_arithmetic<T>::value, "Type must be arithmetic");
@@ -207,6 +269,14 @@ bool OLS_Linear(const PIVector<PIPair<T, T>> & input, T * out_a, T * out_b) {
}
//! Weighted Least Squares linear regression
//! \~english Calculates linear regression coefficients using WLS method
//! \~russian Вычисляет коэффициенты линейной регрессии методом взвешенных наименьших квадратов
//! \param input Vector of (x, y) pairs
//! \param weights Vector of weights for each point
//! \param out_a Output pointer for slope coefficient (a), can be nullptr
//! \param out_b Output pointer for intercept coefficient (b), can be nullptr
//! \return true on success
template<typename T>
bool WLS_Linear(const PIVector<PIPair<T, T>> & input, const PIVector<T> & weights, T * out_a, T * out_b) {
static_assert(std::is_arithmetic<T>::value, "Type must be arithmetic");

View File

@@ -1,6 +1,13 @@
/*! \file pimathsolver.h
* \brief PIMathSolver
*/
//! \addtogroup Math
//! \{
//! \file pimathsolver.h
//! \brief
//! \~english Mathematical solver for differential equations
//! \~russian Математический решатель дифференциальных уравнений
//! \details
//! \~english Solver for ordinary differential equations using various numerical methods
//! \~russian Решатель обыкновенных дифференциальных уравнений с использованием различных численных методов
//! \}
/*
PIP - Platform Independent Primitives
PIMathSolver
@@ -25,50 +32,110 @@
#include "pimathmatrix.h"
/// Differential evaluations
//! \~english Differential evaluations
//! \~russian Дифференциальные вычисления
//! \~english Transfer function representation
//! \~russian Представление передаточной функции
struct PIP_EXPORT TransferFunction {
PIVector<double> vector_Bm, vector_An;
};
//! \~english Mathematical solver for differential equations
//! \~russian Математический решатель дифференциальных уравнений
class PIP_EXPORT PIMathSolver {
public:
//! \~english Solving methods for differential equations
//! \~russian Методы решения дифференциальных уравнений
enum Method {
Global = -1,
Eyler_1 = 01,
Eyler_2 = 02,
EylerKoshi = 03,
RungeKutta_4 = 14,
AdamsBashfortMoulton_2 = 22,
AdamsBashfortMoulton_3 = 23,
AdamsBashfortMoulton_4 = 24,
PolynomialApproximation_2 = 32,
PolynomialApproximation_3 = 33,
PolynomialApproximation_4 = 34,
PolynomialApproximation_5 = 35
Global = -1, //!< \~english Use global method
//! \~russian Использовать глобальный метод
Eyler_1 = 01, //!< \~english Euler method (first order)
//! \~russian Метод Эйлера (1-й порядок)
Eyler_2 = 02, //!< \~english Euler method (second order)
//! \~russian Метод Эйлера (2-й порядок)
EylerKoshi = 03, //!< \~english Euler-Cauchy method
//! \~russian Метод Эйлера-Коши
RungeKutta_4 = 14, //!< \~english Runge-Kutta 4th order
//! \~russian Метод Рунге-Кутта 4-го порядка
AdamsBashfortMoulton_2 = 22, //!< \~english Adams-Bashforth-Moulton 2nd order
//! \~russian Метод Адамса-Башфорта-Мултона 2-го порядка
AdamsBashfortMoulton_3 = 23, //!< \~english Adams-Bashforth-Moulton 3rd order
//! \~russian Метод Адамса-Башфорта-Мултона 3-го порядка
AdamsBashfortMoulton_4 = 24, //!< \~english Adams-Bashforth-Moulton 4th order
//! \~russian Метод Адамса-Башфорта-Мултона 4-го порядка
PolynomialApproximation_2 = 32, //!< \~english Polynomial approximation 2nd order
//! \~russian Метод полиномиальной аппроксимации 2-го порядка
PolynomialApproximation_3 = 33, //!< \~english Polynomial approximation 3rd order
//! \~russian Метод полиномиальной аппроксимации 3-го порядка
PolynomialApproximation_4 = 34, //!< \~english Polynomial approximation 4th order
//! \~russian Метод полиномиальной аппроксимации 4-го порядка
PolynomialApproximation_5 = 35 //!< \~english Polynomial approximation 5th order
//! \~russian Метод полиномиальной аппроксимации 5-го порядка
};
//! Constructs an empty solver
PIMathSolver();
//! \~english Solve differential equation at point u with step h
//! \~russian Решить дифференциальное уравнение в точке u с шагом h
void solve(double u, double h);
//! Initialize from transfer function
//! \~english Set up solver from transfer function coefficients
//! \~russian Инициализировать решатель из коэффициентов передаточной функции
void fromTF(const TransferFunction & TF);
//! Set solving method
//! \~english Set numerical method for solving
//! \~russian Установить численный метод решения
void setMethod(Method m) { method = m; }
//! Set simulation time
//! \~english Set simulation time
//! \~russian Установить время моделирования
void setTime(double time);
//! \~english Solve using Euler method (1st order)
//! \~russian Решение методом Эйлера (1-й порядок)
void solveEyler1(double u, double h);
//! \~english Solve using Euler method (2nd order)
//! \~russian Решение методом Эйлера (2-й порядок)
void solveEyler2(double u, double h);
//! \~english Solve using Runge-Kutta 4th order
//! \~russian Решение методом Рунге-Кутта 4-го порядка
void solveRK4(double u, double h);
//! \~english Solve using Adams-Bashforth-Moulton 2nd order
//! \~russian Решение методом Адамса-Башфорта-Мултона 2-го порядка
void solveABM2(double u, double h);
//! \~english Solve using Adams-Bashforth-Moulton 3rd order
//! \~russian Решение методом Адамса-Башфорта-Мултона 3-го порядка
void solveABM3(double u, double h);
//! \~english Solve using Adams-Bashforth-Moulton 4th order
//! \~russian Решение методом Адамса-Башфорта-Мултона 4-го порядка
void solveABM4(double u, double h);
//! \~english Solve using polynomial approximation
//! \~russian Решение методом полиномиальной аппроксимации
void solvePA(double u, double h, uint deg);
//! \~english Solve using polynomial approximation 2nd order
//! \~russian Решение методом полиномиальной аппроксимации 2-го порядка
void solvePA2(double u, double h);
//! \~english Solve using polynomial approximation 3rd order
//! \~russian Решение методом полиномиальной аппроксимации 3-го порядка
void solvePA3(double u, double h);
//! \~english Solve using polynomial approximation 4th order
//! \~russian Решение методом полиномиальной аппроксимации 4-го порядка
void solvePA4(double u, double h);
//! \~english Solve using polynomial approximation 5th order
//! \~russian Решение методом полиномиальной аппроксимации 5-го порядка
void solvePA5(double u, double h);
//! \~english Solution vector
//! \~russian Вектор решения
PIMathVectord X;
//! \~english Global default method
//! \~russian Глобальный метод по умолчанию
static Method method_global;
//! \~english Description of available methods
//! \~russian Описание доступных методов
static const char methods_desc[];
private:

View File

@@ -1,9 +1,16 @@
/*! \file pimathvector.h
* \ingroup Math
* \~\brief
* \~english Math vector
* \~russian Математический вектор
*/
//! \addtogroup Math
//! \{
//! \file pimathvector.h
//! \brief
//! \~english Math vector
//! \~russian Математический вектор
//! \details
//! \~english Fixed-size and dynamic mathematical vector implementations. Provides PIMathVectorT for compile-time fixed-size vectors and
//! PIMathVector for runtime dynamic-size vectors with support for arithmetic and complex number types.
//! \~russian Реализации математических векторов фиксированного и динамического размера. Предоставляет PIMathVectorT для векторов
//! фиксированного размера во время компиляции и PIMathVector для векторов динамического размера во время выполнения с поддержкой
//! арифметических и комплексных типов чисел.
//! \}
/*
PIP - Platform Independent Primitives
PIMathVector
@@ -40,6 +47,15 @@ class PIMathMatrixT;
#define PIMV_FOR for (uint i = 0; i < Size; ++i)
//! \~english Template class for fixed-size mathematical vector
//! \~russian Шаблонный класс для математического вектора фиксированного размера
//! \brief Fixed-size mathematical vector with compile-time size
//! \~english Fixed-size mathematical vector with compile-time size
//! \~russian Вектор математический фиксированного размера с размером во время компиляции
//! \details Provides vector operations including arithmetic, normalization, angles, cross product, and dot product
//! \~russian Предоставляет операции вектора включая арифметику, нормализацию, углы, векторное произведение и скалярное произведение
//! \tparam Size The fixed size of the vector
//! \tparam Type The element type (arithmetic or complex)
template<uint Size, typename Type = double>
class PIP_EXPORT PIMathVectorT {
typedef PIMathVectorT<Size, Type> _CVector;
@@ -47,50 +63,93 @@ class PIP_EXPORT PIMathVectorT {
static_assert(Size > 0, "Size must be > 0");
public:
//! \~english Constructor with default value for all elements
//! \~russian Конструктор со значением по умолчанию для всех элементов
//! \param v The value to initialize all elements
PIMathVectorT(const Type & v = Type()) { PIMV_FOR c[i] = v; }
//! \~english Constructor from PIVector
//! \~russian Конструктор из PIVector
//! \param val The PIVector to copy from
PIMathVectorT(const PIVector<Type> & val) {
assert(Size == val.size());
PIMV_FOR c[i] = val[i];
}
//! \~english Constructor from initializer list
//! \~russian Конструктор из списка инициализации
//! \param init_list The initializer list to copy from
PIMathVectorT(std::initializer_list<Type> init_list) {
assert(Size == init_list.size());
PIMV_FOR c[i] = init_list.begin()[i];
}
//! \~english Create vector from two points (st -> fn)
//! \~russian Создать вектор из двух точек (st -> fn)
//! \param st The start point
//! \param fn The finish point
//! \returns Vector from st to fn
static _CVector fromTwoPoints(const _CVector & st, const _CVector & fn) {
_CVector tv;
PIMV_FOR tv[i] = fn[i] - st[i];
return tv;
}
//! \~english Get vector size
//! \~russian Получить размер вектора
//! \returns The fixed size of the vector
constexpr uint size() const { return Size; }
//! \~english Fill vector with a single value
//! \~russian Заполнить вектор одним значением
//! \param v The value to fill
//! \returns Reference to this vector
_CVector & fill(const Type & v) {
PIMV_FOR c[i] = v;
return *this;
}
//! \~english Add value to all elements
//! \~russian Прибавить значение ко всем элементам
//! \param v The value to add
//! \returns Reference to this vector
_CVector & move(const Type & v) {
PIMV_FOR c[i] += v;
return *this;
}
//! \~english Add vector to this vector
//! \~russian Прибавить вектор к этому вектору
//! \param v The vector to add
//! \returns Reference to this vector
_CVector & move(const _CVector & v) {
PIMV_FOR c[i] += v[i];
return *this;
}
//! \~english Swap two elements
//! \~russian Поменять два элемента местами
//! \param f First element index
//! \param s Second element index
//! \returns Reference to this vector
_CVector & swapElements(uint f, uint s) {
piSwap<Type>(c[f], c[s]);
return *this;
}
//! \~english Get squared length of vector
//! \~russian Получить квадрат длины вектора
//! \returns Sum of squares of all elements
Type lengthSqr() const {
Type tv(0);
PIMV_FOR tv += c[i] * c[i];
return tv;
}
//! \~english Get length of vector
//! \~russian Получить длину вектора
//! \returns Square root of lengthSqr, unavailable for complex types
Type length() const {
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
if (std::is_arithmetic<Type>::value) return std::sqrt(lengthSqr());
// if (is_complex<Type>::value) return 1000.; // std::sqrt(lengthSqr());
}
//! \~english Get Manhattan length (sum of absolute values)
//! \~russian Получить манхэттенскую длину (сумма абсолютных значений)
//! \returns Sum of absolute values of all elements, unavailable for complex types
Type manhattanLength() const {
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
if (std::is_arithmetic<Type>::value) {
@@ -99,6 +158,10 @@ public:
return tv;
}
}
//! \~english Get cosine of angle between two vectors
//! \~russian Получить косинус угла между двумя векторами
//! \param v The other vector
//! \returns cos(angle), unavailable for complex types
Type angleCos(const _CVector & v) const {
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
if (std::is_arithmetic<Type>::value) {
@@ -107,6 +170,10 @@ public:
return dot(v) / tv;
}
}
//! \~english Get sine of angle between two vectors
//! \~russian Получить синус угла между двумя векторами
//! \param v The other vector
//! \returns sin(angle), unavailable for complex types
Type angleSin(const _CVector & v) const {
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
if (std::is_arithmetic<Type>::value) {
@@ -114,24 +181,40 @@ public:
return std::sqrt(Type(1) - tv * tv);
}
}
//! \~english Get angle in radians between two vectors
//! \~russian Получить угол в радианах между двумя векторами
//! \param v The other vector
//! \returns Angle in radians, unavailable for complex types
Type angleRad(const _CVector & v) const {
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
if (std::is_arithmetic<Type>::value) {
return std::acos(angleCos(v));
}
}
//! \~english Get angle in degrees between two vectors
//! \~russian Получить угол в градусах между двумя векторами
//! \param v The other vector
//! \returns Angle in degrees, unavailable for complex types
Type angleDeg(const _CVector & v) const {
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
if (std::is_arithmetic<Type>::value) {
return toDeg(angleRad(v));
}
}
//! \~english Get elevation angle
//! \~russian Получить угол возвышения
//! \param v The other vector
//! \returns Elevation angle in degrees, unavailable for complex types
Type angleElevation(const _CVector & v) const {
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
if (std::is_arithmetic<Type>::value) {
return 90.0 - angleDeg(v - *this);
}
}
//! \~english Get projection of this vector onto another vector
//! \~russian Получить проекцию этого вектора на другой вектор
//! \param v The vector to project onto
//! \returns Projection vector, unavailable for complex types
_CVector projection(const _CVector & v) {
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
if (std::is_arithmetic<Type>::value) {
@@ -140,6 +223,9 @@ public:
return v * (dot(v) / tv);
}
}
//! \~english Normalize vector in place
//! \~russian Нормализовать вектор на месте
//! \returns Reference to this vector, unavailable for complex types
_CVector & normalize() {
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
if (std::is_arithmetic<Type>::value) {
@@ -150,61 +236,134 @@ public:
return *this;
}
}
//! \~english Get normalized copy of vector
//! \~russian Получить нормализованную копию вектора
//! \returns New normalized vector, unavailable for complex types
_CVector normalized() {
_CVector tv(*this);
tv.normalize();
return tv;
}
//! \~english Check if vector is null (all elements zero)
//! \~russian Проверить, является ли вектор нулевым (все элементы нули)
//! \returns true if vector is null
bool isNull() const {
PIMV_FOR if (c[i] != Type{}) return false;
return true;
}
//! \~english Check if vectors are orthogonal
//! \~russian Проверить, перпендикулярны ли векторы
//! \param v The other vector
//! \returns true if vectors are orthogonal
bool isOrtho(const _CVector & v) const { return ((*this) ^ v) == Type{}; }
//! \~english Get element at index (non-const)
//! \~russian Получить элемент по индексу (non-const)
//! \param index Element index
//! \returns Reference to element
Type & operator[](uint index) { return c[index]; }
//! \~english Get element at index (const)
//! \~russian Получить элемент по индексу (const)
//! \param index Element index
//! \returns Copy of element
const Type & operator[](uint index) const { return c[index]; }
//! \~english Get element at index (const version)
//! \~russian Получить элемент по индексу (версия const)
//! \param index Element index
//! \returns Copy of element
Type at(uint index) const { return c[index]; }
//! \~english Get element at index (non-const, alternative)
//! \~russian Получить элемент по индексу (non-const, альтернативный метод)
//! \param index Element index
//! \returns Reference to element
inline Type & element(uint index) { return c[index]; }
//! \~english Get element at index (const, alternative)
//! \~russian Получить элемент по индексу (const, альтернативный метод)
//! \param index Element index
//! \returns Copy of element
inline const Type & element(uint index) const { return c[index]; }
//! \~english Assign value to all elements
//! \~russian Присвоить значение всем элементам
//! \param v The value to assign
//! \returns Reference to this vector
_CVector & operator=(const Type & v) {
PIMV_FOR c[i] = v;
return *this;
}
//! \~english Check equality with another vector
//! \~russian Проверить равенство с другим вектором
//! \param v The vector to compare with
//! \returns true if all elements are equal
bool operator==(const _CVector & v) const {
PIMV_FOR if (c[i] != v[i]) return false;
return true;
}
//! \~english Check inequality with another vector
//! \~russian Проверить неравенство с другим вектором
//! \param v The vector to compare with
//! \returns true if any element differs
bool operator!=(const _CVector & v) const { return !(*this == c); }
//! \~english Add vector to this vector
//! \~russian Прибавить вектор к этому вектору
//! \param v The vector to add
void operator+=(const _CVector & v) { PIMV_FOR c[i] += v[i]; }
//! \~english Subtract vector from this vector
//! \~russian Вычесть вектор из этого вектора
//! \param v The vector to subtract
void operator-=(const _CVector & v) { PIMV_FOR c[i] -= v[i]; }
//! \~english Multiply all elements by scalar
//! \~russian Умножить все элементы на скаляр
//! \param v The scalar to multiply by
void operator*=(const Type & v) { PIMV_FOR c[i] *= v; }
//! \~english Divide all elements by scalar
//! \~russian Разделить все элементы на скаляр
//! \param v The scalar to divide by
void operator/=(const Type & v) {
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
PIMV_FOR c[i] /= v;
}
//! \~english Unary minus operator
//! \~russian Унарный оператор минус
//! \returns New vector with negated elements
_CVector operator-() const {
_CVector tv;
PIMV_FOR tv[i] = -c[i];
return tv;
}
//! \~english Add two vectors
//! \~russian Сложить два вектора
//! \param v The vector to add
//! \returns New vector with sum of elements
_CVector operator+(const _CVector & v) const {
_CVector tv(*this);
PIMV_FOR tv[i] += v[i];
return tv;
}
//! \~english Subtract two vectors
//! \~russian Вычесть два вектора
//! \param v The vector to subtract
//! \returns New vector with difference of elements
_CVector operator-(const _CVector & v) const {
_CVector tv(*this);
PIMV_FOR tv[i] -= v[i];
return tv;
}
//! \~english Multiply vector by scalar
//! \~russian Умножить вектор на скаляр
//! \param v The scalar to multiply by
//! \returns New vector with scaled elements
_CVector operator*(const Type & v) const {
_CVector tv(*this);
PIMV_FOR tv[i] *= v;
return tv;
}
//! \~english Divide vector by scalar
//! \~russian Разделить вектор на скаляр
//! \param v The scalar to divide by
//! \returns New vector with scaled elements
_CVector operator/(const Type & v) const {
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
_CVector tv = _CVector(*this);
@@ -212,6 +371,10 @@ public:
return tv;
}
//! \~english Compute cross product with another vector (3D only)
//! \~russian Вычислить векторное произведение с другим вектором (только 3D)
//! \param v The other vector
//! \returns Cross product vector
_CVector cross(const _CVector & v) const {
static_assert(Size == 3, "cross product avalible only for 3D vectors");
_CVector tv;
@@ -220,17 +383,33 @@ public:
tv[2] = c[0] * v[1] - v[0] * c[1];
return tv;
}
//! \~english Compute dot product with another vector
//! \~russian Вычислить скалярное произведение с другим вектором
//! \param v The other vector
//! \returns Dot product (sum of element-wise products)
Type dot(const _CVector & v) const {
Type tv{};
PIMV_FOR tv += c[i] * v[i];
return tv;
}
//! \~english Element-wise multiplication with another vector
//! \~russian Покомпонентное умножение с другим вектором
//! \param v The other vector
//! \returns New vector with element-wise products
_CVector mul(const _CVector & v) const {
_CVector tv(*this);
PIMV_FOR tv[i] *= v[i];
return tv;
}
//! \~english Element-wise multiplication with scalar
//! \~russian Покомпонентное умножение на скаляр
//! \param v The scalar to multiply by
//! \returns New vector with scaled elements
_CVector mul(const Type & v) const { return (*this) * v; }
//! \~english Element-wise division by another vector
//! \~russian Покомпонентное деление на другой вектор
//! \param v The vector to divide by
//! \returns New vector with element-wise quotients
_CVector div(const _CVector & v) const {
_CVector tv(*this);
PIMV_FOR {
@@ -239,14 +418,26 @@ public:
}
return tv;
}
//! \~english Element-wise division by scalar
//! \~russian Покомпонентное деление на скаляр
//! \param v The scalar to divide by
//! \returns New vector with scaled elements
_CVector div(const Type & v) const { return (*this) / v; }
//! \~english Transpose vector to 1xN matrix
//! \~russian Транспонировать вектор в матрицу 1xN
//! \returns 1xN matrix representation of this vector
PIMathMatrixT<1, Size, Type> transposed() const {
PIMathMatrixT<1, Size, Type> ret;
PIMV_FOR ret[0][i] = c[i];
return ret;
}
//! \~english Get distance from this point to line defined by two points
//! \~russian Получить расстояние от этой точки до линии, заданной двумя точками
//! \param lp0 First point on the line
//! \param lp1 Second point on the line
//! \returns Distance from point to line, unavailable for complex types
Type distToLine(const _CVector & lp0, const _CVector & lp1) {
static_assert(std::is_arithmetic<Type>::value, "Unavailable for complex");
if (std::is_arithmetic<Type>::value) {
@@ -258,6 +449,11 @@ public:
}
}
//! \~english Convert vector to another size and type
//! \~russian Преобразовать вектор в другой размер и тип
//! \tparam Size1 New vector size
//! \tparam Type1 New element type
//! \returns Vector with new size and type
template<uint Size1, typename Type1> /// vector {Size, Type} to vector {Size1, Type1}
PIMathVectorT<Size1, Type1> turnTo() const {
PIMathVectorT<Size1, Type1> tv;
@@ -267,10 +463,10 @@ public:
return tv;
}
//! \~english
//! \brief Returns this vector with another element type.
//! \~russian
//! \brief Возвращает этот вектор с другим типом элементов.
//! \~english Returns this vector with another element type
//! \~russian Возвращает этот вектор с другим типом элементов
//! \tparam T New element type
//! \returns Vector with new element type
template<typename T>
PIMathVectorT<Size, T> toType() const {
PIMathVectorT<Size, T> ret;
@@ -278,13 +474,22 @@ public:
return ret;
}
//! \~english
//! \brief Returns the subvector with size SubSize. Elements takes from coordinates "offset".
//! \details
//! \~russian
//! \brief Возвращает подвектор с размерами SubSize. Элементы берутся с координат "offset".
//! \details Координаты могут быть отрицательными. Возвращаемый подвектор может быть любого размера. Если исходные элементы выходят
//! за границы исходного подвектора, то в подвекторе будут нули.
//! \~english Returns the subvector with size SubSize. Elements are taken from coordinates "offset"
//! \~russian Возвращает подвектор с размерами SubSize. Элементы берутся с координат "offset"
//! \tparam SubSize Size of the subvector
//! \param offset Starting coordinate (can be negative)
//! \details Coordinates can be negative. The returned subvector can be of any size. If original elements are out of bounds, zeros will
//! be used
//! \~russian Координаты могут быть отрицательными. Возвращаемый подвектор может быть любого размера. Если исходные элементы выходят за
//! границы исходного подвектора, то в подвекторе будут нули \returns Subvector of specified size
//! \~english Returns the subvector with size SubSize. Elements are taken from coordinates "offset"
//! \~russian Возвращает подвектор с размерами SubSize. Элементы берутся с координат "offset"
//! \tparam SubSize Size of the subvector
//! \param offset Starting coordinate (can be negative)
//! \details Coordinates can be negative. The returned subvector can be of any size. If original elements are out of bounds, zeros will
//! be used
//! \~russian Координаты могут быть отрицательными. Возвращаемый подвектор может быть любого размера. Если исходные элементы выходят за
//! границы исходного подвектора, то в подвекторе будут нули \returns Subvector of specified size
template<uint SubSize>
PIMathVectorT<SubSize, Type> subvector(int offset = 0) const {
PIMathVectorT<SubSize, Type> ret;
@@ -296,14 +501,16 @@ public:
return ret;
}
//! \~english
//! \brief Set the subvector "v" in coordinates "index".
//! \details
//! \~russian
//! \brief Устанавливает подвектор "v" в координаты "index".
//! \details Присваивает значения из вектора "v" в область текущиего вектора, ограниченную
//! размерами "v", самого вектор и границами, исходя из координат установки. Координаты могут быть отрицательными.
//! Вектор "v" может быть любого размера. Возвращает ссылку на этот вектор.
//! \~english Set the subvector "v" at coordinates "index"
//! \~russian Устанавливает подвектор "v" в координаты "index"
//! \tparam SubSize Size of the subvector
//! \param index Starting coordinate (can be negative)
//! \param v The subvector to set
//! \details Assigns values from vector "v" to the area of current vector bounded by "v"'s size and vector boundaries, based on the
//! installation coordinates. Coordinates can be negative. Vector "v" can be of any size. Returns reference to this vector.
//! \~russian Присваивает значения из вектора "v" в область текущего вектора, ограниченную размерами "v", самого вектор и границами,
//! исходя из координат установки. Координаты могут быть отрицательными. Вектор "v" может быть любого размера. Возвращает ссылку на этот
//! вектор. \returns Reference to this vector
template<uint SubSize>
PIMathVectorT<Size, Type> & setSubvector(int index, const PIMathVectorT<SubSize, Type> & v) {
for (int i = 0; i < (int)SubSize; ++i) {
@@ -314,23 +521,68 @@ public:
return *this;
}
//! \~english Static cross product of two vectors
//! \~russian Статическое векторное произведение двух векторов
//! \param v1 First vector
//! \param v2 Second vector
//! \returns Cross product vector
static _CVector cross(const _CVector & v1, const _CVector & v2) { return v1.cross(v2); }
//! \~english Static dot product of two vectors
//! \~russian Статическое скалярное произведение двух векторов
//! \param v1 First vector
//! \param v2 Second vector
//! \returns Dot product
static Type dot(const _CVector & v1, const _CVector & v2) { return v1.dot(v2); }
//! \~english Static element-wise multiplication of two vectors
//! \~russian Статическое покомпонентное умножение двух векторов
//! \param v1 First vector
//! \param v2 Second vector
//! \returns New vector with element-wise products
static _CVector mul(const _CVector & v1, const _CVector & v2) { return v1.mul(v2); }
//! \~english Static scalar multiplication (scalar * vector)
//! \~russian Статическое скалярное умножение (скаляр * вектор)
//! \param v1 Scalar
//! \param v2 Vector
//! \returns Scaled vector
static _CVector mul(const Type & v1, const _CVector & v2) { return v2 * v1; }
//! \~english Static scalar multiplication (vector * scalar)
//! \~russian Статическое скалярное умножение (вектор * скаляр)
//! \param v1 Vector
//! \param v2 Scalar
//! \returns Scaled vector
static _CVector mul(const _CVector & v1, const Type & v2) { return v1 * v2; }
//! \~english Static element-wise division of two vectors
//! \~russian Статическое покомпонентное деление двух векторов
//! \param v1 First vector
//! \param v2 Second vector
//! \returns New vector with element-wise quotients
static _CVector div(const _CVector & v1, const _CVector & v2) { return v1.div(v2); }
//! \~english Static scalar division (vector / scalar)
//! \~russian Статическое скалярное деление (вектор / скаляр)
//! \param v1 Vector
//! \param v2 Scalar
//! \returns Scaled vector
static _CVector div(const _CVector & v1, const Type & v2) { return v1 / v2; }
private:
Type c[Size];
};
//! \~english Scalar multiplication (scalar * vector)
//! \~russian Скалярное умножение (скаляр * вектор)
//! \param x Scalar
//! \param v Vector
//! \returns Scaled vector
template<uint Size, typename Type>
inline PIMathVectorT<Size, Type> operator*(const Type & x, const PIMathVectorT<Size, Type> & v) {
return v * x;
}
//! \~english Output vector to PIP stream
//! \~russian Вывести вектор в поток PIP
//! \param s The PIP output stream
//! \param v The vector to output
//! \returns Reference to the stream
template<uint Size, typename Type>
inline PICout operator<<(PICout s, const PIMathVectorT<Size, Type> & v) {
s.space();
@@ -345,12 +597,23 @@ inline PICout operator<<(PICout s, const PIMathVectorT<Size, Type> & v) {
return s;
}
//! \~english 2D integer vector
//! \~russian 2D целочисленный вектор
typedef PIMathVectorT<2u, int> PIMathVectorT2i;
//! \~english 3D integer vector
//! \~russian 3D целочисленный вектор
typedef PIMathVectorT<3u, int> PIMathVectorT3i;
//! \~english 4D integer vector
//! \~russian 4D целочисленный вектор
typedef PIMathVectorT<4u, int> PIMathVectorT4i;
//! \~english 2D double vector
//! \~russian 2D вектор с числами двойной точности
typedef PIMathVectorT<2u, double> PIMathVectorT2d;
//! \~english 3D double vector
//! \~russian 3D вектор с числами двойной точности
typedef PIMathVectorT<3u, double> PIMathVectorT3d;
//! \~english 4D double vector
//! \~russian 4D вектор с числами двойной точности
typedef PIMathVectorT<4u, double> PIMathVectorT4d;
@@ -360,6 +623,14 @@ typedef PIMathVectorT<4u, double> PIMathVectorT4d;
#define PIMV_FOR for (uint i = 0; i < c.size(); ++i)
//! \~english Template class for dynamic-size mathematical vector
//! \~russian Шаблонный класс для математического вектора динамического размера
//! \brief Dynamic-size mathematical vector with runtime size
//! \~english Dynamic-size mathematical vector with runtime size
//! \~russian Вектор математический динамического размера с размером во время выполнения
//! \details Provides vector operations including arithmetic, normalization, angles, cross product, and dot product for dynamic-size vectors
//! \~russian Предоставляет операции вектора включая арифметику, нормализацию, углы, векторное произведение и скалярное произведение для
//! векторов динамического размера \tparam Type The element type (arithmetic or complex)
template<typename Type>
class PIP_EXPORT PIMathVector {
typedef PIMathVector<Type> _CVector;
@@ -369,17 +640,38 @@ class PIP_EXPORT PIMathVector {
friend PIBinaryStream<P> & operator>>(PIBinaryStream<P> & s, PIMathVector<Type1> & v);
public:
//! \~english Constructor with size and initial value
//! \~russian Конструктор с размером и начальным значением
//! \param size The initial size of the vector
//! \param new_value The initial value for all elements
PIMathVector(const uint size = 0, const Type & new_value = Type()) { c.resize(size, new_value); }
//! \~english Constructor from PIVector
//! \~russian Конструктор из PIVector
//! \param val The PIVector to copy from
PIMathVector(const PIVector<Type> & val) { c = val; }
//! \~english Constructor from move PIVector
//! \~russian Конструктор из move PIVector
//! \param val The PIVector to move from
PIMathVector(PIVector<Type> && val): c(std::move(val)) {}
//! \~english Constructor from initializer list
//! \~russian Конструктор из списка инициализации
//! \param init_list The initializer list to copy from
PIMathVector(std::initializer_list<Type> init_list) { c = PIVector<Type>(init_list); }
//! \~english Constructor from fixed-size vector
//! \~russian Конструктор из фиксированного вектора
//! \param val The fixed-size vector to copy from
template<uint Size>
PIMathVector(const PIMathVectorT<Size, Type> & val) {
c.resize(Size);
PIMV_FOR c[i] = val[i];
}
//! \~english Create vector from two points (st -> fn)
//! \~russian Создать вектор из двух точек (st -> fn)
//! \param st The start point
//! \param fn The finish point
//! \returns Vector from st to fn
static PIMathVector fromTwoPoints(const _CVector & st, const _CVector & fn) {
assert(st.size() == fn.size());
_CVector v(st.size());
@@ -387,8 +679,22 @@ public:
v.c[i] = fn[i] - st[i];
}
//! \~english Create zero vector of specified size
//! \~russian Создать нулевой вектор указанного размера
//! \param size The size of the vector
//! \returns Zero vector
static PIMathVector zeros(const uint size) { return PIMathVector(size, Type()); }
//! \~english Create vector with all elements set to 1
//! \~russian Создать вектор со всеми элементами, равными 1
//! \param size The size of the vector
//! \returns Vector filled with ones
static PIMathVector ones(const uint size) { return PIMathVector(size, Type(1)); }
//! \~english Create vector with values in arithmetic progression
//! \~russian Создать вектор со значениями в арифметической прогрессии
//! \param start The start value
//! \param stop The stop value (exclusive)
//! \param step The step between values
//! \returns Vector with arithmetic progression
static PIMathVector arange(const Type start, const Type stop, const Type step = Type(1)) {
PIVector<Type> v;
for (Type i = start; i < stop; i += step)
@@ -396,63 +702,125 @@ public:
return PIMathVector(std::move(v));
}
//! \~english Get vector size
//! \~russian Получить размер вектора
//! \returns The dynamic size of the vector
uint size() const { return c.size(); }
//! \~english Resize the vector
//! \~russian Изменить размер вектора
//! \param size The new size
//! \param new_value The value for new elements
//! \returns Reference to this vector
_CVector & resize(uint size, const Type & new_value = Type()) {
c.resize(size, new_value);
return *this;
}
//! \~english Get resized copy of the vector
//! \~russian Получить копию вектора с измененным размером
//! \param size The new size
//! \param new_value The value for new elements
//! \returns New vector with new size
_CVector resized(uint size, const Type & new_value = Type()) {
_CVector tv = _CVector(*this);
tv.resize(size, new_value);
return tv;
}
//! \~english Fill vector with a single value
//! \~russian Заполнить вектор одним значением
//! \param v The value to fill
//! \returns Reference to this vector
_CVector & fill(const Type & v) {
c.fill(v);
return *this;
}
//! \~english Add value to all elements
//! \~russian Прибавить значение ко всем элементам
//! \param v The value to add
//! \returns Reference to this vector
_CVector & move(const Type & v) {
PIMV_FOR c[i] += v;
return *this;
}
//! \~english Add vector to this vector
//! \~russian Прибавить вектор к этому вектору
//! \param v The vector to add
//! \returns Reference to this vector
_CVector & move(const _CVector & v) {
assert(c.size() == v.size());
PIMV_FOR c[i] += v[i];
return *this;
}
//! \~english Swap two elements
//! \~russian Поменять два элемента местами
//! \param f First element index
//! \param s Second element index
//! \returns Reference to this vector
_CVector & swapElements(uint f, uint s) {
piSwap<Type>(c[f], c[s]);
return *this;
}
//! \~english Get squared length of vector
//! \~russian Получить квадрат длины вектора
//! \returns Sum of squares of all elements
Type lengthSqr() const {
Type tv(0);
PIMV_FOR tv += c[i] * c[i];
return tv;
}
//! \~english Get length of vector
//! \~russian Получить длину вектора
//! \returns Square root of lengthSqr
Type length() const { return std::sqrt(lengthSqr()); }
//! \~english Get Manhattan length (sum of absolute values)
//! \~russian Получить манхэттенскую длину (сумма абсолютных значений)
//! \returns Sum of absolute values of all elements
Type manhattanLength() const {
Type tv(0);
PIMV_FOR tv += piAbs<Type>(c[i]);
return tv;
}
//! \~english Get cosine of angle between two vectors
//! \~russian Получить косинус угла между двумя векторами
//! \param v The other vector
//! \returns cos(angle)
Type angleCos(const _CVector & v) const {
assert(c.size() == v.size());
Type tv = v.length() * length();
assert(std::abs(tv) > PIMATHVECTOR_ZERO_CMP);
return dot(v) / tv;
}
//! \~english Get sine of angle between two vectors
//! \~russian Получить синус угла между двумя векторами
//! \param v The other vector
//! \returns sin(angle)
Type angleSin(const _CVector & v) const {
assert(c.size() == v.size());
Type tv = angleCos(v);
return std::sqrt(Type(1) - tv * tv);
}
//! \~english Get angle in radians between two vectors
//! \~russian Получить угол в радианах между двумя векторами
//! \param v The other vector
//! \returns Angle in radians
Type angleRad(const _CVector & v) const { return std::acos(angleCos(v)); }
//! \~english Get angle in degrees between two vectors
//! \~russian Получить угол в градусах между двумя векторами
//! \param v The other vector
//! \returns Angle in degrees
Type angleDeg(const _CVector & v) const { return toDeg(angleRad(v)); }
//! \~english Get projection of this vector onto another vector
//! \~russian Получить проекцию этого вектора на другой вектор
//! \param v The vector to project onto
//! \returns Projection vector
_CVector projection(const _CVector & v) {
assert(c.size() == v.size());
Type tv = v.length();
assert(std::abs(tv) > PIMATHVECTOR_ZERO_CMP);
return v * (dot(v) / tv);
}
//! \~english Normalize vector in place
//! \~russian Нормализовать вектор на месте
//! \returns Reference to this vector
_CVector & normalize() {
Type tv = length();
assert(std::abs(tv) > PIMATHVECTOR_ZERO_CMP);
@@ -460,71 +828,143 @@ public:
PIMV_FOR c[i] /= tv;
return *this;
}
//! \~english Get normalized copy of vector
//! \~russian Получить нормализованную копию вектора
//! \returns New normalized vector
_CVector normalized() {
_CVector tv(*this);
tv.normalize();
return tv;
}
//! \~english Check if vector is null (all elements zero)
//! \~russian Проверить, является ли вектор нулевым (все элементы нули)
//! \returns true if vector is null
bool isNull() const {
PIMV_FOR if (c[i] != Type(0)) return false;
return true;
}
//! \~english Check if vector is valid (non-empty)
//! \~russian Проверить, валиден ли вектор (не пустой)
//! \returns true if vector is valid
bool isValid() const { return !c.isEmpty(); }
//! \~english Check if vectors are orthogonal
//! \~russian Проверить, перпендикулярны ли векторы
//! \param v The other vector
//! \returns true if vectors are orthogonal
bool isOrtho(const _CVector & v) const { return dot(v) == Type(0); }
//! \~english Get element at index (non-const)
//! \~russian Получить элемент по индексу (non-const)
//! \param index Element index
//! \returns Reference to element
Type & operator[](uint index) { return c[index]; }
//! \~english Get element at index (const)
//! \~russian Получить элемент по индексу (const)
//! \param index Element index
//! \returns Copy of element
const Type & operator[](uint index) const { return c[index]; }
//! \~english Get element at index (const version)
//! \~russian Получить элемент по индексу (версия const)
//! \param index Element index
//! \returns Copy of element
Type at(uint index) const { return c[index]; }
//! \~english Assign value to all elements
//! \~russian Присвоить значение всем элементам
//! \param v The value to assign
//! \returns Reference to this vector
_CVector & operator=(const Type & v) {
PIMV_FOR c[i] = v;
return *this;
}
//! \~english Check equality with another vector
//! \~russian Проверить равенство с другим вектором
//! \param v The vector to compare with
//! \returns true if all elements are equal
bool operator==(const _CVector & v) const { return c == v.c; }
//! \~english Check inequality with another vector
//! \~russian Проверить неравенство с другим вектором
//! \param v The vector to compare with
//! \returns true if any element differs
bool operator!=(const _CVector & v) const { return c != v.c; }
//! \~english Add vector to this vector
//! \~russian Прибавить вектор к этому вектору
//! \param v The vector to add
void operator+=(const _CVector & v) {
assert(c.size() == v.size());
PIMV_FOR c[i] += v[i];
}
//! \~english Subtract vector from this vector
//! \~russian Вычесть вектор из этого вектора
//! \param v The vector to subtract
void operator-=(const _CVector & v) {
assert(c.size() == v.size());
PIMV_FOR c[i] -= v[i];
}
//! \~english Multiply all elements by scalar
//! \~russian Умножить все элементы на скаляр
//! \param v The scalar to multiply by
void operator*=(const Type & v) { PIMV_FOR c[i] *= v; }
//! \~english Divide all elements by scalar
//! \~russian Разделить все элементы на скаляр
//! \param v The scalar to divide by
void operator/=(const Type & v) {
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
PIMV_FOR c[i] /= v;
}
//! \~english Unary minus operator
//! \~russian Унарный оператор минус
//! \returns New vector with negated elements
_CVector operator-() const {
_CVector tv(c.size());
PIMV_FOR tv[i] = -c[i];
return tv;
}
//! \~english Add two vectors
//! \~russian Сложить два вектора
//! \param v The vector to add
//! \returns New vector with sum of elements
_CVector operator+(const _CVector & v) const {
assert(c.size() == v.size());
_CVector tv(*this);
PIMV_FOR tv[i] += v[i];
return tv;
}
//! \~english Subtract two vectors
//! \~russian Вычесть два вектора
//! \param v The vector to subtract
//! \returns New vector with difference of elements
_CVector operator-(const _CVector & v) const {
assert(c.size() == v.size());
_CVector tv(*this);
PIMV_FOR tv[i] -= v[i];
return tv;
}
//! \~english Multiply vector by scalar
//! \~russian Умножить вектор на скаляр
//! \param v The scalar to multiply by
//! \returns New vector with scaled elements
_CVector operator*(const Type & v) const {
_CVector tv(*this);
PIMV_FOR tv[i] *= v;
return tv;
}
//! \~english Divide vector by scalar
//! \~russian Разделить вектор на скаляр
//! \param v The scalar to divide by
//! \returns New vector with scaled elements
_CVector operator/(const Type & v) const {
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
_CVector tv(*this);
PIMV_FOR tv[i] /= v;
return tv;
}
//! \~english Compute cross product with another vector (3D only)
//! \~russian Вычислить векторное произведение с другим вектором (только 3D)
//! \param v The other vector
//! \returns Cross product vector
_CVector cross(const _CVector & v) const {
assert(c.size() == 3);
assert(v.size() == 3);
@@ -534,19 +974,35 @@ public:
tv[2] = c[0] * v[1] - v[0] * c[1];
return tv;
}
//! \~english Compute dot product with another vector
//! \~russian Вычислить скалярное произведение с другим вектором
//! \param v The other vector
//! \returns Dot product (sum of element-wise products)
Type dot(const _CVector & v) const {
assert(c.size() == v.size());
Type tv(0);
PIMV_FOR tv += c[i] * v[i];
return tv;
}
//! \~english Element-wise multiplication with another vector
//! \~russian Покомпонентное умножение с другим вектором
//! \param v The other vector
//! \returns New vector with element-wise products
_CVector mul(const _CVector & v) const {
assert(c.size() == v.size());
_CVector tv(*this);
PIMV_FOR tv[i] *= v[i];
return tv;
}
//! \~english Element-wise multiplication with scalar
//! \~russian Покомпонентное умножение на скаляр
//! \param v The scalar to multiply by
//! \returns New vector with scaled elements
_CVector mul(const Type & v) const { return (*this) * v; }
//! \~english Element-wise division by another vector
//! \~russian Покомпонентное деление на другой вектор
//! \param v The vector to divide by
//! \returns New vector with element-wise quotients
_CVector div(const _CVector & v) const {
assert(c.size() == v.size());
_CVector tv(*this);
@@ -556,8 +1012,17 @@ public:
}
return tv;
}
//! \~english Element-wise division by scalar
//! \~russian Покомпонентное деление на скаляр
//! \param v The scalar to divide by
//! \returns New vector with scaled elements
_CVector div(const Type & v) const { return (*this) / v; }
//! \~english Get distance from this point to line defined by two points
//! \~russian Получить расстояние от этой точки до линии, заданной двумя точками
//! \param lp0 First point on the line
//! \param lp1 Second point on the line
//! \returns Distance from point to line
Type distToLine(const _CVector & lp0, const _CVector & lp1) {
assert(c.size() == lp0.size());
assert(c.size() == lp1.size());
@@ -568,24 +1033,75 @@ public:
return piAbs<Type>(a[0] * b[1] - a[1] * b[0]) / tv;
}
//! \~english Convert to PIVector
//! \~russian Преобразовать в PIVector
//! \returns PIVector with same elements
PIVector<Type> toVector() const { return c; }
//! \~english Apply function to each element (const)
//! \~russian Применить функцию к каждому элементу (const)
//! \param f The function to apply
void forEach(std::function<void(const Type &)> f) const { c.forEach(f); }
//! \~english Apply function to each element
//! \~russian Применить функцию к каждому элементу
//! \param f The function to apply
//! \returns Reference to this vector
_CVector & forEach(std::function<void(Type &)> f) {
c.forEach(f);
return *this;
}
//! \~english Get pointer to underlying data
//! \~russian Получить указатель на внутренние данные
//! \returns Pointer to first element
inline Type * data() { return c.data(); }
//! \~english Get pointer to underlying data (const)
//! \~russian Получить указатель на внутренние данные (const)
//! \returns Pointer to first element
inline const Type * data() const { return c.data(); }
//! \~english Static cross product of two vectors
//! \~russian Статическое векторное произведение двух векторов
//! \param v1 First vector
//! \param v2 Second vector
//! \returns Cross product vector
static _CVector cross(const _CVector & v1, const _CVector & v2) { return v1.cross(v2); }
//! \~english Static dot product of two vectors
//! \~russian Статическое скалярное произведение двух векторов
//! \param v1 First vector
//! \param v2 Second vector
//! \returns Dot product
static Type dot(const _CVector & v1, const _CVector & v2) { return v1.dot(v2); }
//! \~english Static element-wise multiplication of two vectors
//! \~russian Статическое покомпонентное умножение двух векторов
//! \param v1 First vector
//! \param v2 Second vector
//! \returns New vector with element-wise products
static _CVector mul(const _CVector & v1, const _CVector & v2) { return v1.mul(v2); }
//! \~english Static scalar multiplication (scalar * vector)
//! \~russian Статическое скалярное умножение (скаляр * вектор)
//! \param v1 Scalar
//! \param v2 Vector
//! \returns Scaled vector
static _CVector mul(const Type & v1, const _CVector & v2) { return v2 * v1; }
//! \~english Static scalar multiplication (vector * scalar)
//! \~russian Статическое скалярное умножение (вектор * скаляр)
//! \param v1 Vector
//! \param v2 Scalar
//! \returns Scaled vector
static _CVector mul(const _CVector & v1, const Type & v2) { return v1 * v2; }
//! \~english Static element-wise division of two vectors
//! \~russian Статическое покомпонентное деление двух векторов
//! \param v1 First vector
//! \param v2 Second vector
//! \returns New vector with element-wise quotients
static _CVector div(const _CVector & v1, const _CVector & v2) { return v1.div(v2); }
//! \~english Static scalar division (vector / scalar)
//! \~russian Статическое скалярное деление (вектор / скаляр)
//! \param v1 Vector
//! \param v2 Scalar
//! \returns Scaled vector
static _CVector div(const _CVector & v1, const Type & v2) { return v1 / v2; }
private:
@@ -600,6 +1116,11 @@ inline PIMathVector<Type> operator*(const Type & x, const PIMathVector<Type> & v
#undef PIMV_FOR
#ifdef PIP_STD_IOSTREAM
//! \~english Output vector to stream
//! \~russian Вывести вектор в поток
//! \param s The output stream
//! \param v The vector to output
//! \returns Reference to the stream
template<typename Type>
inline std::ostream & operator<<(std::ostream & s, const PIMathVector<Type> & v) {
s << "{";
@@ -612,6 +1133,11 @@ inline std::ostream & operator<<(std::ostream & s, const PIMathVector<Type> & v)
}
#endif
//! \~english Output vector to PIP stream
//! \~russian Вывести вектор в поток PIP
//! \param s The PIP output stream
//! \param v The vector to output
//! \returns Reference to the stream
template<typename Type>
inline PICout operator<<(PICout s, const PIMathVector<Type> & v) {
s.space();
@@ -626,11 +1152,21 @@ inline PICout operator<<(PICout s, const PIMathVector<Type> & v) {
return s;
}
//! \~english Serialize vector to binary stream
//! \~russian Сериализовать вектор в бинарный поток
//! \param s The binary stream
//! \param v The vector to serialize
//! \returns Reference to the stream
template<typename P, typename T>
inline PIBinaryStream<P> & operator<<(PIBinaryStream<P> & s, const PIMathVector<T> & v) {
s << v.c;
return s;
}
//! \~english Deserialize vector from binary stream
//! \~russian Десериализовать вектор из бинарного потока
//! \param s The binary stream
//! \param v The vector to deserialize
//! \returns Reference to the stream
template<typename P, typename T>
inline PIBinaryStream<P> & operator>>(PIBinaryStream<P> & s, PIMathVector<T> & v) {
s >> v.c;
@@ -638,7 +1174,11 @@ inline PIBinaryStream<P> & operator>>(PIBinaryStream<P> & s, PIMathVector<T> & v
}
//! \~english 32-bit integer dynamic vector
//! \~russian Динамический вектор с 32-битными целыми числами
typedef PIMathVector<int> PIMathVectori;
//! \~english Double precision dynamic vector
//! \~russian Динамический вектор с числами двойной точности
typedef PIMathVector<double> PIMathVectord;
#endif // PIMATHVECTOR_H

View File

@@ -40,12 +40,15 @@ public:
Type x;
Type y;
//! \~english Creates a new point.
//! \~russian Создает новую точку.
PIPoint() { x = y = Type(); }
//! \~english Creates a new point with given coordinates.
//! \~russian Создает новую точку с заданными координатами.
PIPoint(Type x_, Type y_) { set(x_, y_); }
//! \~english Set new coordinates for the point.
//! \~russian Задать новые координаты точке.
PIPoint<Type> & set(Type x_, Type y_) {
x = x_;
@@ -53,6 +56,7 @@ public:
return *this;
}
//! \~english Set new coordinates from another point.
//! \~russian Задать новые координаты точке.
PIPoint<Type> & set(const PIPoint<Type> & p) {
x = p.x;
@@ -60,6 +64,7 @@ public:
return *this;
}
//! \~english Translate the point by x and y.
//! \~russian Переместить точку.
PIPoint<Type> & translate(Type x_, Type y_) {
x += x_;
@@ -67,6 +72,7 @@ public:
return *this;
}
//! \~english Translate the point by another point.
//! \~russian Переместить точку.
PIPoint<Type> & translate(const PIPoint<Type> & p) {
x += p.x;
@@ -74,6 +80,7 @@ public:
return *this;
}
//! \~english Create a copy of the point and translate it.
//! \~russian Создать копию точки и переместить её.
PIPoint<Type> translated(Type x_, Type y_) const {
PIPoint<Type> rp(*this);
@@ -81,6 +88,7 @@ public:
return rp;
}
//! \~english Create a copy of the point and translate it by another point.
//! \~russian Создать копию точки и переместить её.
PIPoint<Type> translated(const PIPoint<Type> & p) const {
PIPoint<Type> rp(*this);
@@ -88,14 +96,17 @@ public:
return rp;
}
//! \~english Translate the point. Alias for \a translate().
//! \~russian Переместить точку.
//! \details Является копией метода \a translate().
PIPoint<Type> & move(Type x_, Type y_) { return translate(x_, y_); }
//! \~english Translate the point by another point. Alias for \a translate().
//! \~russian Переместить точку.
//! \details Является копией метода \a translate().
PIPoint<Type> & move(const PIPoint<Type> & p) { return translate(p); }
//! \~english Create a copy of the point and translate it. Alias for \a translated().
//! \~russian Создать копию точки и переместить её.
//! \details Является копией метода \a translated().
PIPoint<Type> moved(Type x_, Type y_) const {
@@ -104,6 +115,7 @@ public:
return rp;
}
//! \~english Create a copy of the point and translate it by another point. Alias for \a translated().
//! \~russian Создать копию точки и переместить её.
//! \details Является копией метода \a translated().
PIPoint<Type> moved(const PIPoint<Type> & p) const {
@@ -112,65 +124,82 @@ public:
return rp;
}
//! \~english Calculate angle in radians in polar coordinate system.
//! \~russian Посчитать угол(радианы) в поолярной системе координат.
double angleRad() const { return atan2(y, x); }
//! \~english Calculate angle in degrees in polar coordinate system.
//! \~russian Посчитать угол(градусы) в поолярной системе координат.
double angleDeg() const { return toDeg(atan2(y, x)); }
//! \~english Convert copy of point to polar coordinate system.
//! \~russian Перевести копию точки в полярную систему координат.
PIPoint<Type> toPolar(bool isDeg = false) const { return PIPoint<Type>(sqrt(x * x + y * y), isDeg ? angleDeg() : angleRad()); }
//! \~english Convert point from polar to cartesian coordinate system.
//! \~russian Перевести копию точки из полярной системы координат в декартовую.
static PIPoint<Type> fromPolar(const PIPoint<Type> & p) { return PIPoint<Type>(p.y * cos(p.x), p.y * sin(p.x)); }
//! \~russian
//! Прибавить координаты второй точки и сохранить.
//! \~english Add second point coordinates and save.
//! \~russian Прибавить координаты второй точки и сохранить.
//! \details Является копией метода \a translate().
PIPoint<Type> & operator+=(const PIPoint<Type> & p) {
translate(p);
return *this;
}
//! \~english Multiply coordinates by value.
PIPoint<Type> & operator*=(Type v) {
x *= v;
y *= v;
return *this;
}
//! \~english Divide coordinates by value.
PIPoint<Type> & operator/=(Type v) {
x /= v;
y /= v;
return *this;
}
//! \~english Add coordinates of two points.
//! \~russian Сложить координаты двух точек.
PIPoint<Type> operator+(const PIPoint<Type> & p) { return PIPoint<Type>(x + p.x, y + p.y); }
//! \~english Add value to both coordinates.
//! \~russian Прибавить к координатам одинаковое значение.
PIPoint<Type> operator+(const Type & p) { return PIPoint<Type>(x + p, y + p); }
//! \~english Subtract second point coordinates - get offset.
//! \~russian Вычесть из координат координаты второй точки - найти смещение.
PIPoint<Type> operator-(const PIPoint<Type> & p) { return PIPoint<Type>(x - p.x, y - p.y); }
//! \~english Subtract value from both coordinates.
//! \~russian Вычесть из координат одинаковое значение.
PIPoint<Type> operator-(const Type & p) { return PIPoint<Type>(x - p, y - p); }
//! \~english Invert point coordinates.
//! \~russian Инвертировать координаты точки.
PIPoint<Type> operator-() { return PIPoint<Type>(-x, -y); }
//! \~english Multiply point coordinates.
//! \~russian Умножить координаты точки.
PIPoint<Type> operator*(Type v) { return PIPoint<Type>(x * v, y * v); }
//! \~english Divide point coordinates.
//! \~russian Делить координаты точки.
PIPoint<Type> operator/(Type v) { return PIPoint<Type>(x / v, y / v); }
//! \~english Check equality of two points.
//! \~russian Проверить равенство координат двух точек.
bool operator==(const PIPoint<Type> & p) const { return (x == p.x && y == p.y); }
//! \~english Check inequality of two points.
//! \~russian Проверить неравенство координат двух точек.
bool operator!=(const PIPoint<Type> & p) const { return (x != p.x || y != p.y); }
};
//! \~english Stream output operator for PIPoint.
//! \~russian Перегруженный оператор для вывода координат в \a PICout.
template<typename Type>
PICout operator<<(PICout & s, const PIPoint<Type> & v) {

View File

@@ -1,9 +1,13 @@
/*! \file piquaternion.h
* \ingroup Math
* \~\brief
* \~english Quaternion
* \~russian Кватернион
*/
//! \addtogroup Math
//! \{
//! \file piquaternion.h
//! \brief
//! \~english Quaternion
//! \~russian Кватернион
//! \details
//! \~english Quaternion for 3D rotations and orientations
//! \~russian Кватернион для 3D вращений и ориентаций
//! \}
/*
PIP - Platform Independent Primitives
Class for quaternions
@@ -28,29 +32,69 @@
#include "pimathmatrix.h"
//! \~english Quaternion for representing 3D rotations and orientations
//! \~russian Кватернион для представления 3D вращений и ориентаций
class PIP_EXPORT PIQuaternion {
friend PIP_EXPORT PIQuaternion operator*(const PIQuaternion & q0, const PIQuaternion & q1);
friend PIP_EXPORT PIQuaternion operator*(const double & a, const PIQuaternion & q);
public:
//! \~english Construct quaternion from rotation axis and angle
//! \~russian Создать кватернион из оси вращения и угла
PIQuaternion(const PIMathVectorT3d & u = PIMathVectorT3d(), double a = 0.);
//! \~english Returns conjugate of this quaternion (negated vector part)
//! \~russian Возвращает сопряженный кватернион (с инвертированной векторной частью)
PIQuaternion conjugate() const { return PIQuaternion(-vector(), scalar()); }
//! \~english Returns new quaternion rotated around axis u by angle a
//! \~russian Возвращает новый кватернион, повернутый вокруг оси u на угол a
PIQuaternion rotated(const PIMathVectorT3d & u, double a) const;
//! \~english Rotate this quaternion around axis u by angle a
//! \~russian Повернуть этот кватернион вокруг оси u на угол a
void rotate(const PIMathVectorT3d & u, double a);
//! \~english Normalize quaternion to unit length
//! \~russian Нормализовать кватернион к единичной длине
void normalize();
//! Get/Set scalar component
double & scalar() { return q[0]; }
//! Get scalar component
double scalar() const { return q[0]; }
//! \~english Returns vector part of quaternion
//! \~russian Возвращает векторную часть кватерниона
PIMathVectorT3d vector() const { return PIMathVectorT3d({q[1], q[2], q[3]}); }
//! \~english Returns Euler angles from quaternion
//! \~russian Возвращает углы Эйлера из кватерниона
PIMathVectorT3d eyler() const;
//! \~english Returns 3x3 rotation matrix from quaternion
//! \~russian Возвращает матрицу вращения 3x3 из кватерниона
PIMathMatrixT33d rotationMatrix() const;
//! \~english Extracts rotation axis from quaternion
//! \~russian Извлекает ось вращения из кватерниона
void axis(PIMathVectorT3d * ret) const;
//! \~english Create quaternion from Euler angles (roll, pitch, yaw)
//! \~russian Создать кватернион из углов Эйлера (крен, тангаж, рыскание)
static PIQuaternion fromEyler(double ax, double ay, double az);
//! \~english Create quaternion from 3x3 rotation matrix
//! \~russian Создать кватернион из матрицы вращения 3x3
static PIQuaternion fromRotationMatrix(const PIMathMatrixT33d & m);
//! \~english Create quaternion from rotation angles
//! \~russian Создать кватернион из углов поворота
static PIQuaternion fromAngles(double ax, double ay, double az);
//! \~english Create quaternion from rotation angles (alternative method)
//! \~russian Создать кватернион из углов поворота (альтернативный метод)
static PIQuaternion fromAngles2(double ax, double ay, double az);
protected:

View File

@@ -1,8 +1,11 @@
//! \file pirect.h
//! \ingroup Math
//! \brief
//! \~english Rect class
//! \~russian Класс прямоугольника
//! \~english Rect class for 2D geometry
//! \~russian Класс прямоугольника для 2D геометрии
//! \details
//! \~english The PIRect class provides a two-dimensional rectangle class for 2D geometry.
//! \~russian Класс PIRect предоставляет двумерный класс прямоугольника для 2D геометрии.
/*
PIP - Platform Independent Primitives
Rect class
@@ -30,25 +33,26 @@
//! \brief
//! \~english Rect class
//! \~russian Класс прямоугольника
//! \~\details
//! \~russian
//! Этот класс описывает прямоугольник на плоскости в прямоугольной системе координат
//! \details
//! \~english The PIRect class provides a two-dimensional rectangle class for 2D geometry.
//! \~russian Класс PIRect предоставляет двумерный класс прямоугольника для 2D геометрии.
template<typename Type>
class PIP_EXPORT PIRect {
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
public:
//!
//! \~english Default constructor.
//! \~russian Конструктор по умолчанию.
PIRect() {}
//! \brief
//! \~english Constructor from bottom-left corner coordinates and width/height.
//! \~russian Конструктор прямоугольника из координат левого нижнего угла и размеров ширины и высоты
PIRect(Type left_, Type bottom_, Type width_, Type height_) {
set(left_, bottom_, width_, height_);
normalize();
}
//! \brief
//! \~english Constructor from bottom-left and top-right points.
//! \~russian Конструктор прямоугольника из координат левого нижнего угла и правого верхнего угла
PIRect(const PIPoint<Type> & bottom_left, const PIPoint<Type> & top_right) {
bl = bottom_left;
@@ -61,128 +65,128 @@ public:
// piMax<Type>(p0.x, p1.x, p2.x), piMax<Type>(p0.y, p1.y, p2.y));
// }
//!
//! \~english Set rectangle from coordinates and dimensions.
PIRect<Type> & set(Type left_, Type bottom_, Type width_, Type height_) {
bl = PIPoint<Type>(left_, bottom_);
tr = PIPoint<Type>(left_ + width_, bottom_ + height_);
return normalize();
}
//!
//! \~english Set rectangle from two points.
PIRect<Type> & set(const PIPoint<Type> & top_left, const PIPoint<Type> & bottom_right) {
bl = top_left;
tr = bottom_right;
return normalize();
}
//! \brief
//! \~english Check if point with given coordinates is inside the rectangle.
//! \~russian Возвращает true если точка с указанными координатами принадлежит прямоугольнику
bool pointIn(Type x, Type y) const { return (x <= bl.x && x >= tr.x && y <= bl.y && y >= tr.y); }
//! \brief
//! \~english Check if point is inside the rectangle.
//! \~russian Возвращает true если точка с указанными координатами принадлежит прямоугольнику
bool pointIn(const PIPoint<Type> & p) const { return pointIn(p.x, p.y); }
//!
//! \~english Check if rectangle is empty.
bool isEmpty() const { return (width() == 0 && height() == 0); }
//!
//! \~english Translate rectangle by x and y.
PIRect<Type> & translate(Type x, Type y) {
bl.translate(x, y);
tr.translate(x, y);
return *this;
}
//!
//! \~english Translate rectangle by point.
PIRect<Type> & translate(const PIPoint<Type> & p) {
bl.translate(p);
tr.translate(p);
return *this;
}
//!
//! \~english Create a copy of rectangle and translate it by x and y.
PIRect<Type> translated(Type x, Type y) const {
PIRect<Type> r(*this);
r.translate(x, y);
return r;
}
//!
//! \~english Create a copy of rectangle and translate it by point.
PIRect<Type> translated(const PIPoint<Type> & p) const {
PIRect<Type> r(*this);
r.translate(p);
return r;
}
//!
//! \~english Translate rectangle by x and y. Alias for \a translate().
PIRect<Type> & move(Type x, Type y) { return translate(x, y); }
//!
//! \~english Translate rectangle by point. Alias for \a translate().
PIRect<Type> & move(const PIPoint<Type> & p) { return translate(p); }
//!
//! \~english Create a copy of rectangle and translate it by x and y. Alias for \a translated().
PIRect<Type> moved(Type x, Type y) const {
PIRect<Type> r(*this);
r.translate(x, y);
return r;
}
//!
//! \~english Create a copy of rectangle and translate it by point. Alias for \a translated().
PIRect<Type> moved(const PIPoint<Type> & p) const {
PIRect<Type> r(*this);
r.translate(p);
return r;
}
//!
//! \~english Scale rectangle by x and y factors.
PIRect<Type> & scale(Type x, Type y) {
setWidth(width() * x);
setHeight(height() * y);
return normalize();
}
//!
//! \~english Scale rectangle uniformly.
PIRect<Type> & scale(Type s) { return scale(s, s); }
//!
//! \~english Scale rectangle by point factors.
PIRect<Type> & scale(const PIPoint<Type> & p) { return scale(p.x, p.y); }
//!
//! \~english Create a copy of rectangle and scale it by x and y factors.
PIRect<Type> scaled(Type x, Type y) const {
PIRect<Type> r(*this);
r.scale(x, y);
return r;
}
//!
//! \~english Create a copy of rectangle and scale it uniformly.
PIRect<Type> scaled(Type s) const {
PIRect<Type> r(*this);
r.scale(s);
return r;
}
//!
//! \~english Create a copy of rectangle and scale it by point factors.
PIRect<Type> scaled(const PIPoint<Type> & p) const {
PIRect<Type> r(*this);
r.scale(p);
return r;
}
//!
//! \~english Normalize rectangle so that left <= right and bottom <= top.
PIRect<Type> & normalize() {
if (bl.x > tr.x) piSwap<Type>(bl.x, tr.x);
if (bl.y > tr.y) piSwap<Type>(bl.y, tr.y);
return *this;
}
//!
//! \~english Create normalized copy of rectangle.
PIRect<Type> normalized() const {
PIRect<Type> r(*this);
r.normalize();
return r;
}
//!
//! \~english Unite rectangle with another rectangle.
PIRect<Type> & unite(const PIRect<Type> & r) {
bl.x = piMax<Type>(bl.x, r.left());
bl.y = piMax<Type>(bl.y, r.bottom());
@@ -191,14 +195,14 @@ public:
return normalize();
}
//!
//! \~english Create united copy of rectangle with another rectangle.
PIRect<Type> united(const PIRect<Type> & rect) const {
PIRect<Type> r(*this);
r.unite(rect);
return r;
}
//!
//! \~english Intersect rectangle with another rectangle.
PIRect<Type> & intersect(const PIRect<Type> & r) {
bl.x = piMax<Type>(bl.x, r.left());
bl.y = piMax<Type>(bl.y, r.bottom());
@@ -208,101 +212,101 @@ public:
return *this;
}
//!
//! \~english Create intersected copy of rectangle with another rectangle.
PIRect<Type> intersected(const PIRect<Type> & rect) const {
PIRect<Type> r(*this);
r.intersect(rect);
return r;
}
//!
//! \~english Get top coordinate.
Type top() const { return tr.y; }
//!
//! \~english Get left coordinate.
Type left() const { return bl.x; }
//!
//! \~english Get right coordinate.
Type right() const { return tr.x; }
//!
//! \~english Get bottom coordinate.
Type bottom() const { return bl.y; }
//!
//! \~english Get rectangle width.
Type width() const { return tr.x - bl.x; }
//!
//! \~english Get rectangle height.
Type height() const { return tr.y - bl.y; }
//!
//! \~english Get top-left corner point.
PIPoint<Type> topLeft() const { return PIPoint<Type>(bl.x, tr.y); }
//!
//! \~english Get top-right corner point.
PIPoint<Type> topRigth() const { return tr; }
//!
//! \~english Get bottom-left corner point.
PIPoint<Type> bottomLeft() const { return bl; }
//!
//! \~english Get bottom-right corner point.
PIPoint<Type> bottomRight() const { return PIPoint<Type>(tr.x, bl.y); }
//!
//! \~english Get center point of rectangle.
PIPoint<Type> center() const { return bl.moved(width() / 2, height() / 2); }
//!
//! \~english Set top coordinate.
void setTop(Type v) {
tr.y = v;
normalize();
}
//!
//! \~english Set left coordinate.
void setLeft(Type v) {
bl.x = v;
normalize();
}
//!
//! \~english Set right coordinate.
void setRigth(Type v) {
tr.x = v;
normalize();
}
//!
//! \~english Set bottom coordinate.
void setBottom(Type v) {
bl.y = v;
normalize();
}
//!
//! \~english Set rectangle width.
void setWidth(Type v) { setTop(bl.x + v); }
//!
//! \~english Set rectangle height.
void setHeight(Type v) { setRigth(bl.y + v); }
//!
//! \~english Set top-left corner point.
void setTopLeft(const PIPoint<Type> & p) {
setLeft(p.x);
setTop(p.y);
}
//!
//! \~english Set bottom-right corner point.
void setBottomRight(const PIPoint<Type> & p) {
setRigth(p.x);
setBottom(p.y);
}
//!
//! \~english Set bottom-left corner point.
void setBottomLeft(const PIPoint<Type> & p) {
bl = p;
normalize();
}
//!
//! \~english Set top-right corner point.
void setTopRigth(const PIPoint<Type> & p) {
tr = p;
normalize();
}
//!
//! \~english Set center point.
void setCenter(const PIPoint<Type> & p) {
Type w = width();
Type h = height();
@@ -310,46 +314,46 @@ public:
tr = PIPoint<Type>(bl.x + w, bl.y + h);
}
//!
//! \~english Set rectangle size.
void setSize(Type w, Type h) {
tr = PIPoint<Type>(bl.x + w, bl.y + h);
normalize();
}
//!
//! \~english Translate rectangle by x on both coordinates.
void operator+=(Type x) { translate(x, x); }
//!
//! \~english Translate rectangle by point.
void operator+=(const PIPoint<Type> & p) { translate(p); }
//!
//! \~english Translate rectangle by negative x on both coordinates.
void operator-=(Type x) { translate(-x, -x); }
//!
//! \~english Translate rectangle by negative point.
void operator-=(const PIPoint<Type> & p) { translate(-p); }
//!
//! \~english Unite rectangle with another.
void operator|=(const PIRect<Type> & r) { unite(r); }
//!
//! \~english Intersect rectangle with another.
void operator&=(const PIRect<Type> & r) { intersect(r); }
//!
//! \~english Translate rectangle by point.
PIRect<Type> operator+(const PIPoint<Type> & p) { return translated(p); }
//!
//! \~english Translate rectangle by negative point.
PIRect<Type> operator-(const PIPoint<Type> & p) { return translated(-p); }
//!
//! \~english Unite rectangle with another.
PIRect<Type> operator|(const PIRect<Type> & r) { return united(r); }
//!
//! \~english Intersect rectangle with another.
PIRect<Type> operator&(const PIRect<Type> & r) { return intersected(r); }
//!
//! \~english Check equality of two rectangles.
bool operator==(const PIRect<Type> & r) const { return (bl == r.bl && tr == r.tr); }
//!
//! \~english Check inequality of two rectangles.
bool operator!=(const PIRect<Type> & r) const { return (bl != r.bl || tr != r.tr); }
private:
@@ -357,7 +361,11 @@ private:
PIPoint<Type> tr;
};
//! \~english Stream output operator for PIRect.
//! \~russian Перегруженный оператор для вывода прямоугольника в \a PICout.
//! \details
//! \~english The operator outputs rectangle information in format: Rect{bottomLeft:widthxheight}
//! \~russian Оператор выводит информацию о прямоугольнике в формате: Rect{bottomLeft:widthxheight}
template<typename Type>
PICout operator<<(PICout & s, const PIRect<Type> & v) {
s.space();

View File

@@ -1,9 +1,13 @@
/*! \file pistatistic.h
* \ingroup Math
* \~\brief
* \~english Calculating math statistic of values array
* \~russian Вычисление математической статистики у массива чисел
*/
//! \addtogroup Math
//! \{
//! \file pistatistic.h
//! \brief
//! \~english Calculating math statistic of values array
//! \~russian Вычисление математической статистики у массива чисел
//! \details
//! \~english Template class for calculating statistical measures of a data set
//! \~russian Шаблонный класс для вычисления статистических характеристик набора данных
//! \}
/*
PIP - Platform Independent Primitives
Class for calculacing math statistic in values array
@@ -28,13 +32,20 @@
#include "pimathbase.h"
//! Statistical calculator template class
//! \~english Template class for calculating statistical measures (mean, variance, skewness, kurtosis)
//! \~russian Шаблонный класс для вычисления статистических показателей (среднее, дисперсия, асимметрия, эксцесс)
template<typename T>
class PIStatistic {
static_assert(std::is_arithmetic<T>::value, "Type must be arithmetic");
public:
//! Construct empty statistic calculator
PIStatistic() { mean = variance = skewness = kurtosis = T(); }
//! Calculate arithmetic mean
//! \~english Calculate arithmetic mean of values
//! \~russian Вычислить среднее арифметическое значение
static T calculateMean(const PIVector<T> & val) {
T ret = T();
int n = val.size();
@@ -43,11 +54,14 @@ public:
ret += val[i];
return ret / n;
}
//! Calculate all statistics with given mean
//! \~english Calculate variance, skewness and kurtosis using provided mean value
//! \~russian Вычислить дисперсию, асимметрию и эксцесс с использованием заданного среднего значения
bool calculate(const PIVector<T> & val, const T & given_mean) {
T v = T(), v1 = T(), v2 = T(), stddev = T(), var = T();
int i, n = val.size();
if (n < 2) return false;
mean = given_mean;
if (n < 2) return false;
variance = skewness = kurtosis = T();
// Variance (using corrected two-pass algorithm)
for (i = 0; i < n; i++)
@@ -72,11 +86,18 @@ public:
}
return true;
}
//! Calculate all statistics
//! \~english Calculate mean, variance, skewness and kurtosis
//! \~russian Вычислить среднее, дисперсию, асимметрию и эксцесс
bool calculate(const PIVector<T> & val) { return calculate(val, calculateMean(val)); }
//! Arithmetic mean
T mean;
//! Variance
T variance;
//! Skewness (third standardized moment)
T skewness;
//! Kurtosis (fourth standardized moment)
T kurtosis;
};

View File

@@ -22,6 +22,9 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! \~english OpenCL module
//! \~russian Модуль OpenCL
//! \defgroup OpenCL OpenCL
//! \~\brief
//! \~english OpenCL support
@@ -61,19 +64,45 @@
#include "pivariant.h"
//! \~english Main OpenCL wrapper class
//! \~russian Главный класс-обёртка OpenCL
class PIP_OPENCL_EXPORT PIOpenCL {
public:
//! \~english Kernel argument structure
//! \~russian Структура аргумента ядра
struct KernelArg;
//! \~english Device structure
//! \~russian Структура устройства
struct Device;
//! \~english Platform structure
//! \~russian Структура платформы
struct Platform;
//! \~english OpenCL context class
//! \~russian Класс контекста OpenCL
class Context;
//! \~english OpenCL buffer class
//! \~russian Класс буфера OpenCL
class Buffer;
//! \~english OpenCL program class
//! \~russian Класс программы OpenCL
class Program;
//! \~english OpenCL kernel class
//! \~russian Класс ядра OpenCL
class Kernel;
//! \~english List of devices
//! \~russian Список устройств
typedef PIVector<Device> DeviceList;
//! \~english Address space qualifiers
//! \~russian Квалификаторы адресного пространства
enum AddressQualifier {
AddressGlobal,
AddressLocal,
@@ -81,6 +110,8 @@ public:
AddressPrivate,
};
//! \~english Access qualifiers
//! \~russian Квалификаторы доступа
enum AccessQualifier {
AccessReadOnly,
AccessWriteOnly,
@@ -88,12 +119,16 @@ public:
AccessNone,
};
//! \~english Buffer direction
//! \~russian Направление буфера
enum Direction {
Input = 0x01,
Output = 0x02,
InputOutput = Input | Output,
};
//! \~english Type qualifiers
//! \~russian Квалификаторы типа
enum TypeQualifier {
TypeConst,
TypeRestrict,
@@ -101,6 +136,8 @@ public:
TypeNone,
};
//! \~english Argument types
//! \~russian Типы аргументов
enum ArgType {
Char = 1,
UChar,
@@ -115,17 +152,51 @@ public:
};
//! \~english Kernel argument information
//! \~russian Информация об аргументе ядра
struct PIP_OPENCL_EXPORT KernelArg {
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
KernelArg();
//! \~english Address qualifier
//! \~russian Квалификатор адреса
AddressQualifier address_qualifier;
//! \~english Access qualifier
//! \~russian Квалификатор доступа
AccessQualifier access_qualifier;
//! \~english Buffer direction
//! \~russian Направление буфера
Direction direction;
//! \~english Type qualifier
//! \~russian Квалификатор типа
TypeQualifier type_qualifier;
//! \~english Argument name
//! \~russian Имя аргумента
PIString arg_name;
//! \~english Type name
//! \~russian Имя типа
PIString type_name;
//! \~english Base type name
//! \~russian Имя базового типа
PIString base_type_name;
//! \~english Is pointer
//! \~russian Является указателем
bool is_pointer;
//! \~english Argument type
//! \~russian Тип аргумента
ArgType arg_type;
//! \~english Dimensions
//! \~russian Размерности
int dims;
private:
@@ -134,68 +205,180 @@ public:
};
//! \~english OpenCL device information
//! \~russian Информация об устройстве OpenCL
struct PIP_OPENCL_EXPORT Device {
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
Device() {
id = platform_id = 0;
max_compute_units = max_clock_frequency = 0;
max_memory_size = 0;
}
//! \~english Check if device is valid
//! \~russian Проверить устройство на корректность
bool isValid() const { return id != 0; }
//! \~english Get display text
//! \~russian Получить текст для отображения
PIString displayText() const { return name.trimmed() + " (" + device_version.trimmed() + ")"; }
//! \~english Device handle
//! \~russian Дескриптор устройства
void * id;
//! \~english Platform ID
//! \~russian ID платформы
void * platform_id;
//! \~english Device name
//! \~russian Имя устройства
PIString name;
//! \~english Vendor name
//! \~russian Имя производителя
PIString vendor;
//! \~english Device version
//! \~russian Версия устройства
PIString device_version;
//! \~english Driver version
//! \~russian Версия драйвера
PIString driver_version;
//! \~english Maximum compute units
//! \~russian Максимум вычислительных блоков
int max_compute_units;
//! \~english Maximum clock frequency
//! \~russian Максимальная тактовая частота
int max_clock_frequency;
//! \~english Maximum memory size
//! \~russian Максимальный размер памяти
ullong max_memory_size;
};
//! \~english OpenCL platform information
//! \~russian Информация о платформе OpenCL
struct PIP_OPENCL_EXPORT Platform {
//! \~english Default constructor
//! \~russian Конструктор по умолчанию
Platform() { id = 0; }
//! \~english Check if platform is valid
//! \~russian Проверить платформу на корректность
bool isValid() const { return id != 0; }
//! \~english Get display text
//! \~russian Получить текст для отображения
PIString displayText() const { return name.trimmed() + " (" + version.trimmed() + ", " + profile.trimmed() + ")"; }
//! \~english Platform handle
//! \~russian Дескриптор платформы
void * id;
//! \~english Platform name
//! \~russian Имя платформы
PIString name;
//! \~english Vendor name
//! \~russian Имя производителя
PIString vendor;
//! \~english Profile
//! \~russian Профиль
PIString profile;
//! \~english Version
//! \~russian Версия
PIString version;
//! \~english Extensions
//! \~russian Расширения
PIStringList extensions;
//! \~english Available devices
//! \~russian Доступные устройства
PIVector<Device> devices;
};
//! \~english OpenCL context for managing devices and resources
//! \~russian Контекст OpenCL для управления устройствами и ресурсами
class PIP_OPENCL_EXPORT Context {
friend class Buffer;
friend class Program;
friend class Kernel;
public:
//! \~english Destructor
//! \~russian Деструктор
~Context();
//! \~english Get context handle
//! \~russian Получить дескриптор контекста
void * handle();
//! \~english Get command queue
//! \~russian Получить очередь команд
void * queue();
//! \~english Create context for device list
//! \~russian Создать контекст для списка устройств
//! \param dl List of devices
//! \return New context or nullptr
static Context * create(const DeviceList & dl);
//! \~english Create context for single device
//! \~russian Создать контекст для одного устройства
//! \param d Device
//! \return New context or nullptr
static Context * create(const Device & d) { return create(DeviceList() << d); }
//! \~english Create context by platform name
//! \~russian Создать контекст по имени платформы
//! \param part_name Platform name pattern
//! \return New context or nullptr
static Context * create(const PIString & part_name);
//! \~english Create program from source
//! \~russian Создать программу из исходного кода
//! \param source OpenCL source code
//! \param args Build arguments
//! \param error Error message output
//! \return New program or nullptr
Program * createProgram(const PIString & source, const PIStringList & args = PIStringList(), PIString * error = 0);
//! \~english Create buffer from vector
//! \~russian Создать буфер из вектора
template<typename T>
Buffer * createBuffer(PIOpenCL::Direction dir, PIVector<T> & container) {
T def = T();
return createBuffer(dir, &container, Buffer::cVector, PIByteArray(&def, sizeof(T)), container.size());
}
//! \~english Create buffer from deque
//! \~russian Создать буфер из deque
template<typename T>
Buffer * createBuffer(PIOpenCL::Direction dir, PIDeque<T> & container) {
T def = T();
return createBuffer(dir, &container, Buffer::cDeque, PIByteArray(&def, sizeof(T)), container.size());
}
//! \~english Create buffer from 2D vector
//! \~russian Создать буфер из 2D вектора
template<typename T>
Buffer * createBuffer(PIOpenCL::Direction dir, PIVector2D<T> & container) {
T def = T();
return createBuffer(dir, &container, Buffer::cVector2D, PIByteArray(&def, sizeof(T)), container.size());
}
//! \~english Create buffer for elements
//! \~russian Создать буфер для элементов
template<typename T>
Buffer * createBuffer(PIOpenCL::Direction dir, uint elements) {
T def = T();
@@ -216,26 +399,70 @@ public:
};
//! \~english OpenCL buffer for data storage
//! \~russian Буфер OpenCL для хранения данных
class PIP_OPENCL_EXPORT Buffer {
friend class Context;
friend class Kernel;
public:
//! \~english Destructor
//! \~russian Деструктор
~Buffer();
//! \~english Get buffer handle
//! \~russian Получить дескриптор буфера
void * handle();
//! \~english Resize buffer
//! \~russian Изменить размер буфера
//! \param new_elements New number of elements
//! \return true if successful
bool resize(uint new_elements);
//! \~english Clear buffer
//! \~russian Очистить буфер
void clear();
//! \~english Copy to container
//! \~russian Копировать в контейнер
void copyToContainer();
//! \~english Copy to memory
//! \~russian Копировать в память
void copyTo(void * data);
//! \~english Copy to memory with offset
//! \~russian Копировать в память со смещением
void copyTo(void * data, int elements_count, int elements_offset = 0);
//! \~english Copy to another buffer
//! \~russian Копировать в другой буфер
void copyTo(Buffer * buffer, int elements_count = -1, int elements_from_offset = 0, int elements_to_offset = 0);
//! \~english Copy from container
//! \~russian Копировать из контейнера
void copyFromContainer();
//! \~english Copy from memory
//! \~russian Копировать из памяти
void copyFrom(void * data);
//! \~english Copy from memory with offset
//! \~russian Копировать из памяти со смещением
void copyFrom(void * data, int elements_count, int elements_offset = 0);
//! \~english Copy from another buffer
//! \~russian Копировать из другого буфера
void copyFrom(Buffer * buffer, int elements_count = -1, int elements_from_offset = 0, int elements_to_offset = 0);
//! \~english Get elements count
//! \~russian Получить количество элементов
uint elementsCount() const { return elements; }
private:
//! \~english Container types
//! \~russian Типы контейнеров
enum Container {
cNone,
cVector,
@@ -257,16 +484,32 @@ public:
};
//! \~english OpenCL program containing kernels
//! \~russian Программа OpenCL содержащая ядра
class PIP_OPENCL_EXPORT Program {
friend class Context;
friend class Kernel;
friend class Buffer;
public:
//! \~english Destructor
//! \~russian Деструктор
~Program();
//! \~english Get context
//! \~russian Получить контекст
Context * context() const { return context_; }
//! \~english Get source code
//! \~russian Получить исходный код
const PIString & sourceCode() const { return source_; }
//! \~english Get kernel by index
//! \~russian Получить ядро по индексу
Kernel * kernel(int index = 0) const { return kernels_[index]; }
//! \~english Get all kernels
//! \~russian Получить все ядра
const PIVector<Kernel *> & kernels() const { return kernels_; }
private:
@@ -280,28 +523,68 @@ public:
};
//! \~english OpenCL kernel for execution
//! \~russian Ядро OpenCL для выполнения
class PIP_OPENCL_EXPORT Kernel {
friend class Program;
friend class Buffer;
public:
//! \~english Get parent program
//! \~russian Получить родительскую программу
Program * program() const { return program_; }
//! \~english Execute kernel
//! \~russian Выполнить ядро
//! \return true if successful
bool execute();
//! \~english Wait for execution to finish
//! \~russian Ждать завершения выполнения
void waitForFinish();
//! \~english Set execution range (1D)
//! \~russian Установить диапазон выполнения (1D)
//! \param size Number of work items
void setExecuteRange(int size) { setExecuteRanges(PIVector<int>() << size); }
//! \~english Set execution ranges (ND)
//! \~russian Установить диапазоны выполнения (ND)
//! \param ranges Array of sizes per dimension
void setExecuteRanges(const PIVector<int> & ranges);
//! \~english Get kernel name
//! \~russian Получить имя ядра
const PIString & name() const { return name_; }
//! \~english Get kernel arguments
//! \~russian Получить аргументы ядра
const PIVector<KernelArg> & args() const { return args_; }
//! \~english Set argument value by index
//! \~russian Установить значение аргумента по индексу
template<typename T>
bool setArgValue(int index, const T & value) {
return setArgValueS(index, PIVariant::fromValue(value));
}
//! \~english Set argument value by name
//! \~russian Установить значение аргумента по имени
template<typename T>
bool setArgValue(const PIString & arg, const T & value) {
return setArgValue(argIndex(arg), value);
}
//! \~english Set variant argument value
//! \~russian Установить значение аргумента variant
bool setArgValue(const PIString & arg, const PIVariant & value) { return setArgValueS(argIndex(arg), value); }
//! \~english Bind buffer to argument
//! \~russian Привязать буфер к аргументу
bool bindArgValue(int index, Buffer * buffer);
//! \~english Bind buffer to argument by name
//! \~russian Привязать буфер к аргументу по имени
bool bindArgValue(const PIString & arg, Buffer * buffer) { return bindArgValue(argIndex(arg), buffer); }
private:
@@ -321,15 +604,37 @@ public:
};
//! \~english Initialize OpenCL
//! \~russian Инициализировать OpenCL
static void init();
//! \~english Get available platforms
//! \~russian Получить доступные платформы
//! \return Vector of platforms
static const PIVector<Platform> & platforms();
//! \~english Get all available devices
//! \~russian Получить все доступные устройства
//! \return Vector of devices
static const PIVector<Device> devices();
//! \~english Get device by ID
//! \~russian Получить устройство по ID
//! \param id Device handle
//! \return Device info
static Device deviceByID(void * id);
//! \~english Prepare program source
//! \~russian Подготовить исходный код программы
//! \param prog Program source
//! \return Prepared source
static PIString prepareProgram(const PIString & prog);
private:
static PIString prog_header;
PIOpenCL() { ; }
//! \~english Initialization helper
//! \~russian Помощник инициализации
class PIP_OPENCL_EXPORT Initializer {
public:
Initializer();
@@ -341,6 +646,8 @@ private:
};
//! \~english Output stream operator for KernelArg
//! \~russian Оператор вывода в поток для KernelArg
PIP_OPENCL_EXPORT PICout operator<<(PICout s, const PIOpenCL::KernelArg & v);

View File

@@ -17,6 +17,40 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! \~english PIP - Platform Independent Primitives
//! \~russian PIP - Кроссплатформенные примитивы
//! \mainpage
//!
//! \~\brief
//! \~english Main include file for PIP library
//! \~russian Главный включаемый файл библиотеки PIP
//!
//! \~\details
//! \~english \section overview Overview
//! \~russian \section overview Обзор
//!
//! \~english
//! PIP is a C++ cross-platform library providing platform-independent abstractions for:
//! * Core/Types: Strings, variants, containers, datetime, networks
//! * Threading: Mutexes, semaphores, thread pools, timers
//! * I/O: Files, serial, CAN, GPIO, SPI, Ethernet
//! * Math: Vectors, matrices, FFT, quaternions
//! * Crypto: MD5, SHA, BLAKE2, SipHash
//! * Compression: zlib support
//! * HTTP: Client and server support
//! * Serialization: JSON, binary, XML
//!
//! \~russian
//! PIP - это кроссплатформенная C++ библиотека, предоставляющая платформонезависимые абстракции для:
//! * Ядро/Типы: Строки, варианты, контейнеры, дата/время, сети
//! * Потоки: Мьютексы, семафоры, пулы потоков, таймеры
//! * Ввод/Вывод: Файлы, последовательные порты, CAN, GPIO, SPI, Ethernet
//! * Математика: Векторы, матрицы, FFT, кватернионы
//! * Криптография: MD5, SHA, BLAKE2, SipHash
//! * Сжатие: Поддержка zlib
//! * HTTP: Клиент и сервер
//! * Сериализация: JSON, бинарная, XML
#ifndef PIP_H
#define PIP_H

View File

@@ -22,12 +22,15 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! \~english Resources subsystem
//! \~russian Подсистема ресурсов
//! \defgroup Resources Resources
//! \~\brief
//! \~english Resources subsystem
//! \~russian Подсистема ресурсов
//!
//! \~\details
//! \~
//! \~english \section cmake_module_Resources Building with CMake
//! \~russian \section cmake_module_Resources Сборка с использованием CMake
//!
@@ -59,18 +62,31 @@
#include "pistring.h"
//! \~english Macro for initializing compiled-in resources
//! \~russian Макрос для инициализации вкомпиленных ресурсов
#define INIT_RESOURCE(name) \
{ \
extern void _pirc_##name##_init_(); \
_pirc_##name##_init_(); \
}
//! \~english Class for accessing compiled-in resources
//! \~russian Класс для доступа к вкомпиленным ресурсам
class PIP_EXPORT PIResources {
public:
//!
//! \~english Get resource by section and name
//! \~russian Получить ресурс по секции и имени
//! \details
//! \~english Searches for resource in specified section
//! \~russian Ищет ресурс в указанной секции
static PIByteArray get(const PIString & section, const PIString & name);
//! \~english Get resource by name (searches all sections)
//! \~russian Получить ресурс по имени (ищет во всех секциях)
static PIByteArray get(const PIString & name);
//! \~english Dump all resources to console
//! \~russian Вывести все ресурсы в консоль
static void dump();
private:

View File

@@ -17,6 +17,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! \~english Resources storage subsystem
//! \~russian Подсистема хранения ресурсов
//! \defgroup Resources Resources
//! \~\brief
//! \~english Resources storage subsystem
//! \~russian Подсистема хранения ресурсов
#ifndef PIRESOURCES_P_H
#define PIRESOURCES_P_H
@@ -26,20 +33,41 @@
class PIResources;
//! \~english Storage for compiled-in resources
//! \~russian Хранилище вкомпиленных ресурсов
class PIP_EXPORT PIResourcesStorage {
friend class PIResources;
public:
//! \~english Get singleton instance
//! \~russian Получить синглтон
static PIResourcesStorage * instance();
//! \~english Section containing resource entries
//! \~russian Секция, содержащая записи ресурсов
struct PIP_EXPORT Section {
//! \~english Constructor
//! \~russian Конструктор
Section();
//! \~english Destructor
//! \~russian Деструктор
~Section();
//! \~english Add another section to this one
//! \~russian Добавить другую секцию к этой
void add(const Section & s);
//! \~english Clear all entries
//! \~russian Очистить все записи
void purge();
//! \~english Map of resource names to data
//! \~russian Карта имен ресурсов к данным
PIMap<PIString, PIByteArray *> entries;
};
//! \~english Resource entry constructor
//! \~russian Конструктор записи ресурса
struct PIP_EXPORT __RCEntry {
__RCEntry(const PIString & s = PIString(),
const PIString & n = PIString(),
@@ -56,21 +84,65 @@ public:
size = si;
flags = fl;
}
//! \~english Section name
//! \~russian Имя секции
PIString section;
//! \~english Resource name
//! \~russian Имя ресурса
PIString name;
//! \~english Alias
//! \~russian Псевдоним
PIString alias;
//! \~english Source file
//! \~russian Исходный файл
PIString file;
//! \~english Offset in file
//! \~russian Смещение в файле
llong offset;
//! \~english Size
//! \~russian Размер
llong size;
//! \~english Flags
//! \~russian Флаги
int flags;
};
//! \~english Register a section with data
//! \~russian Зарегистрировать секцию с данными
//! \param section_name Name of the section
//! \param data Section data
void registerSection(const PIString & section_name, const Section & data);
//! \~english Register from raw data
//! \~russian Зарегистрировать из сырых данных
//! \param rc_data Resource data
//! \param rc_desc Resource description
//! \param rc_desc_size Description size
void registerSection(const uchar * rc_data, const uchar * rc_desc, int rc_desc_size);
//! \~english Get section by name
//! \~russian Получить секцию по имени
//! \param section_name Name of section
//! \return Pointer to section or nullptr
Section * section(const PIString & section_name) const;
//! \~english Get resource by section and name
//! \~russian Получить ресурс по секции и имени
PIByteArray get(const PIString & section_name, const PIString & entry_name) const;
//! \~english Get resource by name (searches all sections)
//! \~russian Получить ресурс по имени (ищет во всех секциях)
PIByteArray get(const PIString & entry_name) const;
//! \~english Clear all sections
//! \~russian Очистить все секции
void clear();
private:

View File

@@ -106,7 +106,12 @@ public:
return true;
}
//! \~english Set source buffer for read from "data"
//! \~russian Устанавливает исходный буфер для чтения из "data"
void setSource(const PIByteArray & data);
//! \~english Set source buffer for read or write to/from "data", or empty stream for write if "data" = 0
//! \~russian Устанавливает исходный буфер для чтения или записи из/в "data", или пустой поток на запись если "data" = 0
void setSource(PIByteArray * data);
//! \~english Returns internal buffer with written data

View File

@@ -1,8 +1,13 @@
/*! \file pijson.h
* \ingroup Serialization
/*! \file pijsonserialization.h
* \addtogroup Serialization
* \brief
* \~english JSON serialization
* \~russian Сериализация JSON
* \~english JSON serialization and deserialization template functions
* \~russian Шаблонные функции сериализации и десериализации JSON
* \details
* \~english This file provides template functions for serializing and deserializing
* various types to and from PIJSON format.
* \~russian Этот файл предоставляет шаблонные функции для сериализации и десериализации
* различных типов в формат PIJSON и из него.
*/
/*
PIP - Platform Independent Primitives

View File

@@ -1,8 +1,13 @@
/*! \file pivaluetree_conversions.h
* \ingroup Serialization
* \addtogroup Serialization
* \brief
* \~english PIValueTree conversions
* \~russian Преобразования PIValueTree
* \~english PIValueTree conversion functions
* \~russian Функции преобразования PIValueTree
* \details
* \~english This file provides functions for converting PIValueTree to and from
* various formats (JSON, text, property storage).
* \~russian Этот файл предоставляет функции для преобразования PIValueTree в различные
* форматы (JSON, текст, хранилище свойств) и из них.
*/
/*
PIP - Platform Independent Primitives
@@ -32,31 +37,65 @@ class PIPropertyStorage;
class PIJSON;
class PIIODevice;
//! \~english PIValueTree conversion namespace.
//! \~russian Пространство имён преобразований PIValueTree.
namespace PIValueTreeConversions {
//! \~english Conversion options. \~russian Параметры преобразования.
enum Option {
WithAttributes = 0x1,
WithComment = 0x2,
WithType = 0x4,
WithAll = 0xFFF,
IncludeRoot = 0x1000,
Default = WithAll
WithAttributes = 0x1, //!< \~english Include attributes \~russian Включить атрибуты
WithComment = 0x2, //!< \~english Include comments \~russian Включить комментарии
WithType = 0x4, //!< \~english Include type information \~russian Включить информацию о типе
WithAll = 0xFFF, //!< \~english Include everything \~russian Включить всё
IncludeRoot = 0x1000, //!< \~english Include root node \~russian Включить корневой узел
Default = WithAll //!< \~english Default options \~russian Параметры по умолчанию
};
//! \~english Options flags type.
//! \~russian Тип флагов параметров.
typedef PIFlags<Option> Options;
//! \~english Convert PIPropertyStorage to PIValueTree.
//! \~russian Преобразование PIPropertyStorage в PIValueTree.
PIP_EXPORT PIValueTree fromPropertyStorage(const PIPropertyStorage & ps);
//! \~english Convert PIVariantMap to PIValueTree.
//! \~russian Преобразование PIVariantMap в PIValueTree.
PIP_EXPORT PIValueTree fromVariantMap(const PIVariantMap & vm);
//! \~english Convert PIJSON to PIValueTree.
//! \~russian Преобразование PIJSON в PIValueTree.
PIP_EXPORT PIValueTree fromJSON(const PIJSON & json);
//! \~english Read PIValueTree from IODevice as text.
//! \~russian Чтение PIValueTree из IODevice как текст.
PIP_EXPORT PIValueTree fromText(PIIODevice * device);
//! \~english Parse PIValueTree from text string.
//! \~russian Разбор PIValueTree из текстовой строки.
PIP_EXPORT PIValueTree fromText(const PIString & str);
//! \~english Load PIValueTree from JSON file.
//! \~russian Загрузка PIValueTree из JSON файла.
PIP_EXPORT PIValueTree fromJSONFile(const PIString & path);
//! \~english Load PIValueTree from text file.
//! \~russian Загрузка PIValueTree из текстового файла.
PIP_EXPORT PIValueTree fromTextFile(const PIString & path);
//! \~english Convert PIValueTree to JSON.
//! \~russian Преобразование PIValueTree в JSON.
PIP_EXPORT PIJSON toJSON(const PIValueTree & root, Options options = Default);
//! \~english Convert PIValueTree to text.
//! \~russian Преобразование PIValueTree в текст.
PIP_EXPORT PIString toText(const PIValueTree & root, Options options = Default);
//! \~english Save PIValueTree to JSON file.
//! \~russian Сохранение PIValueTree в JSON файл.
PIP_EXPORT bool toJSONFile(const PIString & path, const PIValueTree & root, Options options = Default);
//! \~english Save PIValueTree to text file.
//! \~russian Сохранение PIValueTree в текстовый файл.
PIP_EXPORT bool toTextFile(const PIString & path, const PIValueTree & root, Options options = Default);
} // namespace PIValueTreeConversions

View File

@@ -1,9 +1,13 @@
/*! \file pistatemachine.h
* \ingroup StateMachine
* \~\brief
* \~english State machine.
* \~russian Машина состояний.
*/
//! \addtogroup StateMachine
//! \{
//! \file pistatemachine.h
//! \brief
//! \~english State machine.
//! \~russian Машина состояний.
//! \details
//! \~english Main state machine class that manages states and transitions
//! \~russian Основной класс машины состояний, управляющий состояниями и переходами
//! \}
/*
PIP - Platform Independent Primitives
State machine
@@ -31,17 +35,36 @@
//! \ingroup StateMachine
//! \~\brief
//! \~english
//! \~russian
//! \~english Main state machine class
//! \~russian Основной класс машины состояний
class PIP_EXPORT PIStateMachine: public PIStateBase {
public:
//! \~english Creates state machine with optional name
//! \~russian Создает машину состояний с опциональным именем
//! \param n Machine name
PIStateMachine(const PIString & n = {});
//! \~english Starts state machine execution
//! \~russian Запускает выполнение машины состояний
//! \return true if started successfully
bool start();
//! \~english Checks if state machine is running
//! \~russian Проверяет, запущена ли машина состояний
//! \return true if running
bool isRunning() const { return is_running; }
//! \~english Sets finish callback
//! \~russian Устанавливает коллбэк завершения
//! \param f Callback function to call when machine finishes
void setOnFinish(std::function<void()> f) { on_finish = f; }
//! \~english Posts event to state machine
//! \~russian Отправляет событие в машину состояний
//! \tparam Args Event arguments types
//! \param event_id Event identifier
//! \param args Event arguments
//! \return true if transition was triggered
template<typename... Args>
bool postEvent(int event_id, Args... args) {
if (!is_running) return false;

View File

@@ -1,9 +1,13 @@
/*! \file pistatemachine_base.h
* \ingroup StateMachine
* \~\brief
* \~english Some template helpers for PIStateMachine
* \~russian Несколько шаблонов для PIStateMachine
*/
//! \addtogroup StateMachine
//! \{
//! \file pistatemachine_base.h
//! \brief
//! \~english Some template helpers for PIStateMachine
//! \~russian Несколько шаблонов для PIStateMachine
//! \details
//! \~english Contains helper classes and functions for state machine implementation
//! \~russian Содержит вспомогательные классы и функции для реализации машины состояний
//! \}
/*
PIP - Platform Independent Primitives
Some template helpers for PIStateMachine
@@ -31,22 +35,45 @@
namespace PIStateMachineHelpers {
//! \~english Helper namespace for state machine implementation
//! \~russian Вспомогательное пространство имён для реализации машины состояний
//! \~english Base class for function wrappers
//! \~russian Базовый класс для обёрток функций
class FunctionBase {
public:
//! \~english Virtual destructor
//! \~russian Виртуальный деструктор
virtual ~FunctionBase() {}
//! \~english Returns format hash for type checking
//! \~russian Возвращает хеш формата для проверки типов
virtual uint formatHash() = 0;
};
//! \~english Template class for function wrappers
//! \~russian Шаблонный класс для обёрток функций
template<typename... Args>
class Function: public FunctionBase {
public:
//! \~english Returns format hash for type checking
//! \~russian Возвращает хеш формата для проверки типов
uint formatHash() override {
static uint ret = PIConstChars(typeid(std::function<void(Args...)>).name()).hash();
return ret;
}
//! \~english Stored function
//! \~russian Сохранённая функция
std::function<bool(Args...)> func;
};
//! \~english Creates function wrapper from std::function
//! \~russian Создает обёртку функции из std::function
//! \tparam Ret Return type of the function
//! \tparam Args Argument types of the function
//! \param func Function to wrap
//! \return Pointer to function wrapper
template<typename Ret, typename... Args>
FunctionBase * makeFunction(std::function<Ret(Args...)> func) {
auto * ret = new Function<Args...>();

View File

@@ -1,9 +1,13 @@
/*! \file pistatemachine_state.h
* \ingroup StateMachine
* \~\brief
* \~english State machine node
* \~russian Узел машины состояний
*/
//! \addtogroup StateMachine
//! \{
//! \file pistatemachine_state.h
//! \brief
//! \~english State machine node
//! \~russian Узел машины состояний
//! \details
//! \~english Contains state classes for building state machines
//! \~russian Содержит классы состояний для построения машин состояний
//! \}
/*
PIP - Platform Independent Primitives
State machine node
@@ -31,50 +35,117 @@
#include "pisystemtime.h"
//! \ingroup StateMachine
//! \~\brief
//! \~english
//! \~russian
//! \~english Base class for state machine states
//! \~russian Базовый класс для состояний машины состояний
class PIP_EXPORT PIStateBase {
friend class PIStateMachine;
friend class PITransitionBase;
friend class PIStateFinal;
public:
//! \~english Creates state with name
//! \~russian Создает состояние с именем
PIStateBase(const PIString & n = {}): name_(n) { ; }
virtual ~PIStateBase();
//! \~english Called when state is \~russian Вы entered
//! зывается при входе в состояние
virtual void onEnter() {}
//! \~english Called when state is exited
//! \~russian Вызывается при выходе из состояния
virtual void onExit() {}
//! \~english Called when state machine finishes (for final states)
//! \~russian Вызывается при завершении машины состояний (для финальных состояний)
virtual void onFinish() {}
//! \~english Returns state machine this state belongs to
//! \~russian Возвращает машину состояний, которой принадлежит это состояние
PIStateMachine * machine() const { return root; }
//! \~english Returns parent state
//! \~russian Возвращает родительское состояние
PIStateBase * parent() const { return parent_state; }
//! \~english Returns active child state
//! \~russian Возвращает активное дочернее состояние
PIStateBase * activeChild() const { return active_state; }
//! \~english Returns all active child states
//! \~russian Возвращает все активные дочерние состояния
PIVector<PIStateBase *> activeChildren() const;
//! \~english Returns all active atomic states
//! \~russian Возвращает все активные атомарные состояния
PIVector<PIStateBase *> activeAtomics() const;
//! \~english Adds child state
//! \~russian Добавляет дочернее состояние
void addState(PIStateBase * s);
//! \~english Adds multiple child states
//! \~russian Добавляет несколько дочерних состояний
void addStates(PIVector<PIStateBase *> s);
//! \~english Sets initial state for compound state
//! \~russian Устанавливает начальное состояние для составного состояния
void setInitialState(PIStateBase * s);
//! \~english Adds transition to target state on event
//! \~russian Добавляет переход к целевому состоянию по событию
PITransitionBase * addTransition(PIStateBase * target, int event_id);
//! \~english Adds timeout transition to target state
//! \~russian Добавляет переход по таймауту к целевому состоянию
PITransitionTimeout * addTimeoutTransition(PIStateBase * target, PISystemTime timeout);
//! \~english Sets parallel mode for state
//! \~russian Устанавливает параллельный режим для состояния
void setParallel(bool yes) { is_parallel = yes; }
//! \~english Returns state name
//! \~russian Возвращает имя состояния
const PIString & getName() const { return name_; }
//! \~english Checks if this is root state machine
//! \~russian Проверяет является ли это корневой машиной состояний
bool isStateMachine() const { return is_root; }
//! \~english Checks if state is active
//! \~russian Проверяет активно ли состояние
bool isActive() const { return is_active; }
//! \~english Checks if state is in parallel mode
//! \~russian Проверяет находится ли состояние в параллельном режиме
bool isParallel() const { return is_parallel; }
//! \~english Checks if state is final
//! \~russian Проверяет является ли состояние финальным
bool isFinal() const { return is_final; }
//! \~english Checks if state is atomic (no children)
//! \~russian Проверяет является ли состояние атомарным (нет потомков)
bool isAtomic() const { return children.isEmpty(); }
//! \~english Checks if state is compound (has children)
//! \~russian Проверяет является ли состояние составным (есть потомки)
bool isCompound() const { return children.isNotEmpty(); }
//! \~english Returns child states
//! \~russian Возвращает дочерние состояния
const PIVector<PIStateBase *> & getChildren() const { return children; }
//! \~english Returns transitions from this state
//! \~russian Возвращает переходы из этого состояния
const PIVector<PITransitionBase *> & getTransitions() const { return transitions; }
//! \~english Prints state tree to string
//! \~russian Выводит дерево состояний в строку
void print(PIString prefix = {});
//! \~english Returns all states in machine
//! \~russian Возвращает все состояния в машине
PIVector<PIStateBase *> gatherStates();
private:
@@ -100,8 +171,12 @@ private:
};
//! \~english State with lambda callbacks
//! \~russian Состояние с lambda коллбэками
class PIP_EXPORT PIStateLambda: public PIStateBase {
public:
//! \~english Creates lambda state with callbacks
//! \~russian Создает lambda состояние с коллбэками
PIStateLambda(std::function<void()> on_enter, std::function<void()> on_exit = nullptr, const PIString & n = {}): PIStateBase(n) {
enter = on_enter;
exit = on_exit;
@@ -118,8 +193,12 @@ private:
};
//! \~english Final state of state machine
//! \~russian Финальное состояние машины состояний
class PIP_EXPORT PIStateFinal: public PIStateBase {
public:
//! \~english Creates final state with finish callback
//! \~russian Создает финальное состояние с коллбэком завершения
PIStateFinal(std::function<void()> on_finish = nullptr, const PIString & n = {}): PIStateBase(n) {
is_final = true;
enter = on_finish;
@@ -133,6 +212,11 @@ private:
};
//! \~english Output stream operator for PIStateBase
//! \~russian Оператор вывода потока для PIStateBase
//! \param c Output stream
//! \param s State to output
//! \return Reference to output stream
inline PICout operator<<(PICout c, PIStateBase * s) {
if (!s)
c << "state(nullptr)";

View File

@@ -1,9 +1,13 @@
/*! \file pistatemachine_transition.h
* \ingroup StateMachine
* \~\brief
* \~english State machine transition
* \~russian Переход машины состояний
*/
//! \addtogroup StateMachine
//! \{
//! \file pistatemachine_transition.h
//! \brief
//! \~english State machine transition
//! \~russian Переход машины состояний
//! \details
//! \~english Contains transition classes for state machine
//! \~russian Содержит классы переходов для машины состояний
//! \}
/*
PIP - Platform Independent Primitives
State machine transition
@@ -30,22 +34,38 @@
#include "pitimer.h"
//! \ingroup StateMachine
//! \~\brief
//! \~english
//! \~russian
//! \~english Base class for state machine transitions
//! \~russian Базовый класс для переходов машины состояний
class PIP_EXPORT PITransitionBase {
friend class PIStateMachine;
friend class PIStateBase;
public:
//! \~english Creates transition from source to target on event
//! \~russian Создает переход от source к target по событию
//! \param source Source state
//! \param target Target state
//! \param event_id Event identifier that triggers this transition
PITransitionBase(PIStateBase * source, PIStateBase * target, int event_id);
//! \~english Virtual destructor
//! \~russian Виртуальный деструктор
virtual ~PITransitionBase();
//! \~english Returns state machine this transition belongs to
//! \~russian Возвращает машину состояний, которой принадлежит этот переход
PIStateMachine * machine() const { return root; }
//! \~english Returns source state
//! \~russian Возвращает исходное состояние
PIStateBase * source() const { return source_state; }
//! \~english Returns target state
//! \~russian Возвращает целевое состояние
PIStateBase * target() const { return target_state; }
//! \~english Adds guard function to transition
//! \~russian Добавляет сторожевую функцию к переходу
template<typename R, typename... Args>
PITransitionBase * addGuard(std::function<R(Args...)> f) {
static_assert(std::is_same<R, bool>::value, "guard function should return bool!");
@@ -54,11 +74,15 @@ public:
return this;
}
//! \~english Adds guard function to transition (callable)
//! \~russian Добавляет сторожевую функцию к переходу (callable)
template<typename L>
PITransitionBase * addGuard(L f) {
return addGuard(toStdFunction(f));
}
//! \~english Tests guard function with arguments
//! \~russian Тестирует сторожевую функцию с аргументами
template<typename... Args>
bool testGuard(Args... args) {
if (!guard) return true;
@@ -69,13 +93,25 @@ public:
return reinterpret_cast<PIStateMachineHelpers::Function<Args...> *>(guard)->func(args...);
}
//! \~english Adds action to transition
//! \~russian Добавляет действие к переходу
PITransitionBase * addAction(std::function<void()> a);
//! \~english Executes transition action
//! \~russian Выполняет действие перехода
void makeAction();
//! \~english Triggers transition
//! \~russian Запускает переход
void trigger();
protected:
//! \~english Called when transition becomes enabled
//! \~russian Вызывается когда переход становится доступным
virtual void enabled() {}
//! \~english Called when transition becomes disabled
//! \~russian Вызывается когда переход становится недоступным
virtual void disabled() {}
int eventID = 0;
@@ -86,9 +122,22 @@ protected:
};
//! \~english Timeout transition
//! \~russian Переход по таймауту
//! \details
//! \~english Transition that triggers after specified timeout
//! \~russian Переход, который срабатывает после указанного таймаута
class PIP_EXPORT PITransitionTimeout: public PITransitionBase {
public:
//! \~english Creates timeout transition
//! \~russian Создает переход по таймауту
//! \param source Source state
//! \param target Target state
//! \param timeout Timeout duration
PITransitionTimeout(PIStateBase * source, PIStateBase * target, PISystemTime timeout);
//! \~english Destructor
//! \~russian Деструктор
~PITransitionTimeout();
private:
@@ -98,4 +147,6 @@ private:
PITimer timer;
};
//! \}
#endif

View File

@@ -122,6 +122,10 @@ bool PIHIDevice::open(const PIHIDeviceInfo & device) {
return false;
}
HidD_GetPreparsedData(PRIVATE->deviceHandle, &PRIVATE->preparsed);
if (!PRIVATE->preparsed) {
close();
return false;
}
return true;
#endif
}
@@ -338,12 +342,12 @@ PIVector<PIHIDeviceInfo> PIHIDevice::allDevices(bool try_open) {
PIDir hid_dir("/sys/bus/hid/devices"_a);
auto hid_devs = hid_dir.entries();
for (auto hd: hid_devs) {
for (const auto & hd: hid_devs) {
// piCout << d.path;
if (!isDir(hd)) continue;
PIDir dir_input(hd.path + "/input"_a);
auto hid_inputs = dir_input.entries();
for (auto hd_i: hid_inputs) {
for (const auto & hd_i: hid_inputs) {
if (!isDir(hd_i)) continue;
// now in /sys/bus/hid/devices/<dev>/input/input<N>
// piCout << hd_i.path;
@@ -360,7 +364,7 @@ PIVector<PIHIDeviceInfo> PIHIDevice::allDevices(bool try_open) {
PIDir dir_e(hd_i.path);
PIStringList devs;
auto dl_e = dir_e.entries();
for (auto d_e: dl_e) {
for (const auto & d_e: dl_e) {
if (!d_e.isDir() || d_e.flags[PIFile::FileInfo::Dot] || d_e.flags[PIFile::FileInfo::DotDot]) continue;
devs << d_e.name();
}
@@ -388,7 +392,7 @@ PIVector<PIHIDeviceInfo> PIHIDevice::allDevices(bool try_open) {
if (test_f.isClosed()) continue;
}
ullong ev = readFile(hd_i.path + "/capabilities/ev"_a).toULLong(16);
// ullong ev = readFile(hd_i.path + "/capabilities/ev"_a).toULLong(16);
auto readAxes = [readFile, checkBit, &hd_i, &dev](const PIString & file, bool is_relative) {
PIVector<PIHIDeviceInfo::AxisInfo> ret;
@@ -524,6 +528,7 @@ PIVector<PIHIDeviceInfo> PIHIDevice::allDevices(bool try_open) {
PHIDP_PREPARSED_DATA preparsed = nullptr;
if (HidD_GetPreparsedData(deviceHandle, &preparsed) == FALSE) {
piCout << "HidD_GetPreparsedData error:" << errorString();
CloseHandle(deviceHandle);
continue;
}
// auto pp = PIByteArray(preparsed, 64);

View File

@@ -29,37 +29,98 @@
#include "pithread.h"
//! \~english HID device information
//! \~russian Информация об HID устройстве
struct PIP_EXPORT PIHIDeviceInfo {
friend class PIHIDevice;
//! \~english Base class for value info
//! \~russian Базовый класс для информации о значении
struct PIP_EXPORT ValueInfoBase {
//! \~english Checks if info is valid
//! \~russian Проверяет валидна ли информация
bool isValid() const { return index >= 0; }
int index = -1;
int data_index = -1;
};
//! \~english Axis information
//! \~russian Информация об оси
struct PIP_EXPORT AxisInfo: public ValueInfoBase {
int bits = 0;
int min = 0;
int max = 1;
bool is_relative = false;
};
//! \~english Button information
//! \~russian Информация о кнопке
struct PIP_EXPORT ButtonInfo: public ValueInfoBase {
int code = 0;
};
//! \~english Device path
//! \~russian Путь к устройству
PIString path;
//! \~english Manufacturer name
//! \~russian Имя производителя
PIString manufacturer;
//! \~english Product name
//! \~russian Название продукта
PIString product;
//! \~english Serial number
//! \~russian Серийный номер
PIString serial;
//! \~english Version
//! \~russian Версия
PIString version;
//! \~english Vendor ID
//! \~russian Идентификатор производителя
PIString VID;
//! \~english Product ID
//! \~russian Идентификатор продукта
PIString PID;
//! \~english List of axes
//! \~russian Список осей
PIVector<AxisInfo> axes;
//! \~english List of buttons
//! \~russian Список кнопок
PIVector<ButtonInfo> buttons;
//! \~english Checks if info is null
//! \~russian Проверяет является ли информация пустой
bool isNull() const { return path.isEmpty(); }
//! \~english Checks if info is not null
//! \~russian Проверяет является ли информация не пустой
bool isNotNull() const { return !isNull(); }
//! \~english Matches device by string
//! \~russian Сопоставляет устройство по строке
bool match(const PIString & str) const;
//! \~english Returns axes count
//! \~russian Возвращает количество осей
int axesCount() const { return axes.size_s(); }
//! \~english Returns absolute axes count
//! \~russian Возвращает количество абсолютных осей
int axesAbsoluteCount() const;
//! \~english Returns relative axes count
//! \~russian Возвращает количество относительных осей
int axesRelativeCount() const;
//! \~english Returns buttons count
//! \~russian Возвращает количество кнопок
int buttonsCount() const { return buttons.size_s(); }
private:
@@ -74,13 +135,19 @@ private:
PIP_EXPORT PICout operator<<(PICout s, const PIHIDeviceInfo & v);
//! \~english HID device
//! \~russian HID устройство
class PIP_EXPORT PIHIDevice: public PIThread {
PIOBJECT_SUBCLASS(PIHIDevice, PIThread)
public:
~PIHIDevice();
//! \~english HID event
//! \~russian Событие HID
struct PIP_EXPORT Event {
//! \~english Event type
//! \~russian Тип события
enum Type {
tNone,
tButton,
@@ -92,19 +159,48 @@ public:
float value = 0.;
};
//! \~english Checks if device is opened
//! \~russian Проверяет открыто ли устройство
bool isOpened() const;
//! \~english Opens device by info
//! \~russian Открывает устройство по информации
bool open(const PIHIDeviceInfo & device);
//! \~english Opens device
//! \~russian Открывает устройство
bool open();
//! \~english Closes device
//! \~russian Закрывает устройство
void close();
//! \~english Starts reading device
//! \~russian Начинает чтение устройства
void start();
//! \~english Stops reading device
//! \~russian Останавливает чтение устройства
void stop();
//! \~english Sets dead zone for axes
//! \~russian Устанавливает мёртвую зону для осей
void setDeadZone(float v) { dead_zone = v; }
//! \~english Returns dead zone
//! \~russian Возвращает мёртвую зону
float deadZone() const { return dead_zone; }
//! \~english Event fired on device event
//! \~russian Событие при событии устройства
EVENT1(event, PIHIDevice::Event, e);
//! \~english Returns all available HID devices
//! \~russian Возвращает все доступные HID устройства
static PIVector<PIHIDeviceInfo> allDevices(bool try_open = true);
//! \~english Finds device by name
//! \~russian Находит устройство по имени
static PIHIDeviceInfo findDevice(const PIString & name);
private:

View File

@@ -102,9 +102,7 @@
# define PIP_PLUGIN \
extern "C" { \
PIP_PLUGIN_EXPORT int __PIP_PLUGIN_LOADER_VERSION_FUNC__() { \
return __PIP_PLUGIN_LOADER_VERSION__; \
} \
PIP_PLUGIN_EXPORT int __PIP_PLUGIN_LOADER_VERSION_FUNC__() { return __PIP_PLUGIN_LOADER_VERSION__; } \
}
# define PIP_PLUGIN_STATIC_SECTION_MERGE \
@@ -117,13 +115,28 @@
# endif
//! \~english Plugin information
//! \~russian Информация о плагине
class PIP_EXPORT PIPluginInfo {
public:
//! \~english Constructs empty plugin info
//! \~russian Создает пустую информацию о плагине
PIPluginInfo();
//! \~english Sets user version
//! \~russian Устанавливает пользовательскую версию
void setUserVersion(const PIString & v);
//! \~english Sets static section pointer
//! \~russian Устанавливает указатель статической секции
void setStaticSection(int type, void * ptr);
//! \~english Returns user version
//! \~russian Возвращает пользовательскую версию
PIString userVersion() const;
//! \~english Returns all static sections
//! \~russian Возвращает все статические секции
PIMap<int, void *> staticSections() const;
private:
@@ -132,16 +145,36 @@ private:
};
//! \~english Plugin info storage
//! \~russian Хранилище информации о плагинах
class PIP_EXPORT PIPluginInfoStorage {
public:
//! \~english Constructs storage
//! \~russian Создает хранилище
PIPluginInfoStorage();
//! \~english Returns current plugin info
//! \~russian Возвращает информацию о текущем плагине
PIPluginInfo * currentInfo();
//! \~english Returns plugin info by pointer
//! \~russian Возвращает информацию о плагине по указателю
PIPluginInfo * pluginInfo(void * p);
//! \~english Returns application plugin info
//! \~russian Возвращает информацию о приложении
PIPluginInfo * applicationInfo();
//! \~english Enters plugin context
//! \~russian Входит в контекст плагина
PIPluginInfo * enterPlugin(void * p);
//! \~english Unloads plugin
//! \~russian Выгружает плагин
void unloadPlugin(void * p);
//! \~english Returns singleton instance
//! \~russian Возвращает синглтон
static PIPluginInfoStorage * instance();
private:
@@ -223,6 +256,8 @@ public:
//! \~russian Вызывает у плагина \a PIP_PLUGIN_STATIC_SECTION_MERGE
void mergeStatic();
//! \~english Returns directories where plugins can be located
//! \~russian Возвращает директории где могут располагаться плагины
static PIStringList pluginsDirectories(const PIString & name);
private:

View File

@@ -30,36 +30,54 @@
#include <functional>
//! \~english System signals handler
//! \~russian Обработчик системных сигналов
class PIP_EXPORT PISignals {
public:
//! \~english Signal types
//! \~russian Типы сигналов
enum Signal {
Interrupt /** Interrupt from keyboard */ = 0x01, // Term Interrupt from keyboard
Illegal /** Illegal Instruction */ = 0x02, // Core Illegal Instruction
Abort /** Abort signal */ = 0x04, // Core Abort signal from abort
FPE /** Floating point exception */ = 0x08, // Core Floating point exception
SegFault /** Invalid memory reference */ = 0x10, // Core Invalid memory reference
Termination /** Termination signal */ = 0x20, // Term Termination signal
Hangup = 0x40, // Term Hangup detected on controlling terminal or death of controlling process
Quit = 0x80, // Core Quit from keyboard
Kill = 0x100, // Term Kill signal
BrokenPipe = 0x200, // Term Broken pipe: write to pipe with no readers
Timer = 0x400, // Term Timer signal from alarm
UserDefined1 = 0x800, // Term User-defined signal 1
UserDefined2 = 0x1000, // Term User-defined signal 2
ChildStopped = 0x2000, // Ign Child stopped or terminated
Continue = 0x4000, // Cont Continue if stopped
StopProcess = 0x8000, // Stop Stop process
StopTTY = 0x10000, // Stop Stop typed at tty
StopTTYInput = 0x20000, // Stop tty input for background process
StopTTYOutput = 0x40000, // Stop tty output for background process
All = 0xFFFFF
Interrupt = 0x01, //!< Interrupt from keyboard
Illegal = 0x02, //!< Illegal Instruction
Abort = 0x04, //!< Abort signal
FPE = 0x08, //!< Floating point exception
SegFault = 0x10, //!< Invalid memory reference
Termination = 0x20, //!< Termination signal
Hangup = 0x40, //!< Hangup detected
Quit = 0x80, //!< Quit from keyboard
Kill = 0x100, //!< Kill signal
BrokenPipe = 0x200, //!< Broken pipe
Timer = 0x400, //!< Timer signal
UserDefined1 = 0x800, //!< User-defined signal 1
UserDefined2 = 0x1000, //!< User-defined signal 2
ChildStopped = 0x2000, //!< Child stopped or terminated
Continue = 0x4000, //!< Continue if stopped
StopProcess = 0x8000, //!< Stop process
StopTTY = 0x10000, //!< Stop typed at tty
StopTTYInput = 0x20000, //!< Stop tty input
StopTTYOutput = 0x40000, //!< Stop tty output
All = 0xFFFFF //!< All signals
};
//! \~english Signal handler callback type
//! \~russian Тип коллбэка обработчика сигналов
//! \note slot is any function with format "void(PISignals::Signal)"
typedef std::function<void(PISignals::Signal)> SignalEvent;
// slot is any function format "void(PISignals::Signal)"
//! \~english Sets signal handler callback
//! \~russian Устанавливает коллбэк обработчика сигналов
static void setSlot(SignalEvent slot) { ret_func = slot; }
//! \~english Grabs specified signals
//! \~russian Перехватывает указанные сигналы
static void grabSignals(PIFlags<PISignals::Signal> signals_);
//! \~english Releases specified signals
//! \~russian Освобождает указанные сигналы
static void releaseSignals(PIFlags<PISignals::Signal> signals_);
//! \~english Raises signal
//! \~russian Генерирует сигнал
static void raiseSignal(PISignals::Signal signal);
private:

View File

@@ -17,22 +17,48 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! \addtogroup System
//! \{
//! \file pisystemtests.h
//! \brief System tests results
//! \~english Provides access to system test results and timing information
//! \~russian Предоставляет доступ к результатам системных тестов и информации о времени
//! \details
//! \~english This module contains functionality for reading system test results, including timing information
//! \~russian Этот модуль содержит функциональность для чтения результатов системных тестов, включая информацию о времени
#ifndef PISYSTEMTESTS_H
# define PISYSTEMTESTS_H
# include "pibase.h"
namespace PISystemTests {
//! \~english Time resolution in nanoseconds
//! \~russian Разрешение времени в наносекундах
extern PIP_EXPORT long time_resolution_ns;
//! \~english Time elapsed in nanoseconds
//! \~russian Прошедшее время в наносекундах
extern PIP_EXPORT long time_elapsed_ns;
//! \~english Usleep offset in microseconds
//! \~russian Смещение usleep в микросекундах
extern PIP_EXPORT long usleep_offset_us;
//! \~english System test reader class
//! \~russian Класс чтения системных тестов
class PIP_EXPORT PISystemTestReader {
public:
//! \~english Constructor
//! \~russian Конструктор
PISystemTestReader();
};
//! \~english Global system test reader instance
//! \~russian Глобальный экземпляр чтеца системных тестов
extern PIP_EXPORT PISystemTestReader pisystestreader;
} // namespace PISystemTests
#endif // PISYSTEMTESTS_H
//! \}

Some files were not shown because too many files have changed in this diff Show More