3 Commits

Author SHA1 Message Date
Бычков Анлрей
e85b11a233 PIMap via pipair, fix tests 2022-05-26 18:07:44 +03:00
Бычков Анлрей
831adf3fc9 some new tests 2022-05-25 18:52:19 +03:00
Бычков Анлрей
a18f461ce3 pimap tests 2022-05-24 18:40:06 +03:00
387 changed files with 32968 additions and 50560 deletions

View File

@@ -1,224 +0,0 @@
---
Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignArrayOfStructures: Left
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignCompound: false
PadOperators: true
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCompound: false
PadOperators: true
AlignConsecutiveDeclarations:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignCompound: false
PadOperators: true
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortEnumsOnASingleLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Inline
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Attach
BreakInheritanceList: BeforeComma
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 140
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: false
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: Always
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: CurrentLine
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: true
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
- piForeach
- piForeachC
- piForeachR
- piForeachRC
- piForeachCR
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: false
IndentPPDirectives: AfterHash
IndentExternBlock: NoIndent
IndentRequiresClause: true
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertBraces: false
InsertTrailingCommas: Wrapped
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: Signature
MacroBlockBegin: "PRIVATE_DEFINITION_START|STATIC_INITIALIZER_BEGIN"
MacroBlockEnd: "PRIVATE_DEFINITION_END|STATIC_INITIALIZER_END"
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyIndentedWhitespace: 0
PointerAlignment: Middle
PPIndentWidth: 2
ReferenceAlignment: Middle
ReflowComments: true
RemoveBracesLLVM: false
RequiresClausePosition: OwnLine
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: false
SpaceBeforeInheritanceColon: false
SpaceBeforeParens: ControlStatementsExceptControlMacros
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: false
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: false
AfterOverloadedOperator: false
AfterRequiresInClause: false
AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Both
SpaceBeforeRangeBasedForLoopColon: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: After
Standard: c++11
StatementAttributeLikeMacros:
- Q_EMIT
- PIMETA
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
- PRIVATE_DECLARATION
- NO_COPY_CLASS
- FOREVER_WAIT
- WAIT_FOREVER
TabWidth: 4
UseCRLF: false
UseTab: AlignWithSpaces
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
- PIMETA
...

View File

@@ -1,6 +0,0 @@
root = true
[*.{h,c,cpp}]
charset = utf-8
indent_style = tab
tab_width = 4

3
.gitignore vendored
View File

@@ -3,5 +3,4 @@
/doc/rtf /doc/rtf
_unsused _unsused
CMakeLists.txt.user* CMakeLists.txt.user*
/include /include
/release

View File

@@ -5,7 +5,7 @@
*/ */
#define lapi_c #define lapi_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lcode_c #define lcode_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lctype_c #define lctype_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define ldebug_c #define ldebug_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define ldo_c #define ldo_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define ldump_c #define ldump_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lfunc_c #define lfunc_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lgc_c #define lgc_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define llex_c #define llex_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lmem_c #define lmem_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lobject_c #define lobject_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lopcodes_c #define lopcodes_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lparser_c #define lparser_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lstate_c #define lstate_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lstring_c #define lstring_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define ltable_c #define ltable_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define ltm_c #define ltm_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lundump_c #define lundump_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lvm_c #define lvm_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -5,7 +5,7 @@
*/ */
#define lzio_c #define lzio_c
#define LUA_CORE
#include "lprefix.h" #include "lprefix.h"

View File

@@ -1,12 +1,12 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0017 NEW) # need include() with .cmake cmake_policy(SET CMP0017 NEW) # need include() with .cmake
project(PIP) project(pip)
set(PIP_MAJOR 4) set(pip_MAJOR 2)
set(PIP_MINOR 3) set(pip_MINOR 39)
set(PIP_REVISION 0) set(pip_REVISION 0)
set(PIP_SUFFIX ) set(pip_SUFFIX )
set(PIP_COMPANY SHS) set(pip_COMPANY SHS)
set(PIP_DOMAIN org.SHS) set(pip_DOMAIN org.SHS)
set(GIT_CMAKE_DIR) set(GIT_CMAKE_DIR)
if (NOT DEFINED SHSTKPROJECT) if (NOT DEFINED SHSTKPROJECT)
@@ -17,7 +17,7 @@ cmake_minimum_required(VERSION 2.8.2)
project(cmake-download NONE) project(cmake-download NONE)
include(ExternalProject) include(ExternalProject)
ExternalProject_Add(cmake ExternalProject_Add(cmake
GIT_REPOSITORY https://git.shstk.ru/SHS/cmake.git GIT_REPOSITORY https://git.shs.tools/SHS/cmake.git
GIT_TAG \"origin/master\" GIT_TAG \"origin/master\"
GIT_CONFIG \"advice.detachedHead=false\" GIT_CONFIG \"advice.detachedHead=false\"
SOURCE_DIR \"${CMAKE_CURRENT_BINARY_DIR}/cmake-src\" SOURCE_DIR \"${CMAKE_CURRENT_BINARY_DIR}/cmake-src\"
@@ -53,8 +53,7 @@ include(CheckFunctionExists)
include(PIPMacros) include(PIPMacros)
include(SHSTKMacros) include(SHSTKMacros)
shstk_begin_project(PIP) shstk_begin_project(pip PIP)
set(PIP_VERSION "${PIP_VERSION}" CACHE STRING "")
set(_ICU_DEFAULT OFF) set(_ICU_DEFAULT OFF)
if((NOT DEFINED WIN32) AND (NOT DEFINED ANDROID_PLATFORM) AND (NOT DEFINED APPLE)) if((NOT DEFINED WIN32) AND (NOT DEFINED ANDROID_PLATFORM) AND (NOT DEFINED APPLE))
@@ -62,28 +61,17 @@ if((NOT DEFINED WIN32) AND (NOT DEFINED ANDROID_PLATFORM) AND (NOT DEFINED APPLE
endif() endif()
set(PIP_DLL_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE STRING "") set(PIP_DLL_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE STRING "")
if (CMAKE_BUILD_TYPE MATCHES Debug)
set(PIP_BUILD_DEBUG ON)
else()
set(PIP_BUILD_DEBUG OFF)
endif()
# Options # Options
option(ICU "ICU support for convert codepages" ${_ICU_DEFAULT}) option(ICU "ICU support for convert codepages" ${_ICU_DEFAULT})
option(STD_IOSTREAM "Building with std iostream operators support" OFF) option(STD_IOSTREAM "Building with std iostream operators support" OFF)
option(INTROSPECTION "Build with introspection" OFF) option(INTROSPECTION "Build with introspection" OFF)
option(TESTS "Build tests and perform their before install step" ${PIP_BUILD_DEBUG}) option(TESTS "Build tests and perform their before install step" OFF)
option(COVERAGE "Build project with coverage info" OFF) option(COVERAGE "Build project with coverage info" OFF)
set(PIP_UTILS 1) set(PIP_UTILS 1)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE) set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
shstk_is_parent_exists(_pe)
if (_pe)
set(BUILDING_pip 1 PARENT_SCOPE)
set(pip_ROOT_SRC "${CMAKE_CURRENT_SOURCE_DIR}" PARENT_SCOPE)
endif()
# Basic # Basic
set(PIP_MODULES) set(PIP_MODULES)
@@ -96,7 +84,7 @@ set(PIP_UTILS_LIST)
set(PIP_TESTS_LIST) set(PIP_TESTS_LIST)
set(PIP_EXPORTS) set(PIP_EXPORTS)
set(PIP_SRC_MODULES "console;crypt;compress;usb;fftw;opencl;io_utils;client_server;cloud;lua") set(PIP_SRC_MODULES "console;crypt;compress;usb;fftw;opencl;io_utils;cloud;lua")
foreach(_m ${PIP_SRC_MODULES}) foreach(_m ${PIP_SRC_MODULES})
set(PIP_MSG_${_m} "no") set(PIP_MSG_${_m} "no")
endforeach() endforeach()
@@ -108,7 +96,7 @@ macro(pip_module NAME LIBS LABEL INCLUDES SOURCES MSG)
set(CRES) set(CRES)
file(GLOB_RECURSE CPPS "libs/${NAME}/*.cpp" "libs/${NAME}/*.c") file(GLOB_RECURSE CPPS "libs/${NAME}/*.cpp" "libs/${NAME}/*.c")
file(GLOB_RECURSE HS "libs/${NAME}/*.h") file(GLOB_RECURSE HS "libs/${NAME}/*.h")
file(GLOB_RECURSE PHS "libs/${NAME}/*_p.h" "libs/${NAME}/3rd/*.h") file(GLOB_RECURSE PHS "libs/${NAME}/*_p.h")
file(GLOB_RECURSE RES "libs/${NAME}/*.conf") file(GLOB_RECURSE RES "libs/${NAME}/*.conf")
if (NOT "x${PHS}" STREQUAL "x") if (NOT "x${PHS}" STREQUAL "x")
list(REMOVE_ITEM HS ${PHS}) list(REMOVE_ITEM HS ${PHS})
@@ -117,14 +105,9 @@ macro(pip_module NAME LIBS LABEL INCLUDES SOURCES MSG)
file(GLOB_RECURSE ASRC "${SOURCES}/*.cpp" "${SOURCES}/*.c") file(GLOB_RECURSE ASRC "${SOURCES}/*.cpp" "${SOURCES}/*.c")
list(APPEND CPPS ${ASRC}) list(APPEND CPPS ${ASRC})
endif() endif()
#message("${NAME} HS = ${HS}")
list(APPEND HDRS ${HS}) list(APPEND HDRS ${HS})
list(APPEND PHDRS ${PHS}) list(APPEND PHDRS ${PHS})
if(${CMAKE_VERSION} VERSION_LESS "3.8.0")
else()
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${CPPS} ${HS} ${PHS})
endif()
set(_target "pip_${NAME}") set(_target "pip_${NAME}")
set(_libs "${LIBS}") set(_libs "${LIBS}")
if ("${NAME}" STREQUAL "main") if ("${NAME}" STREQUAL "main")
@@ -135,11 +118,11 @@ macro(pip_module NAME LIBS LABEL INCLUDES SOURCES MSG)
string(TOUPPER "${_target}" DEF_NAME) string(TOUPPER "${_target}" DEF_NAME)
set(PIP_MSG_${NAME} "yes${MSG}") set(PIP_MSG_${NAME} "yes${MSG}")
import_version(${_target} PIP) import_version(${_target} pip)
set_deploy_property(${_target} ${PIP_LIB_TYPE} set_deploy_property(${_target} ${pip_LIB_TYPE}
LABEL "${LABEL}" LABEL "${LABEL}"
FULLNAME "${PIP_DOMAIN}.${_target}" FULLNAME "${pip_DOMAIN}.${_target}"
COMPANY "${PIP_COMPANY}" COMPANY "${pip_COMPANY}"
INFO "Platform-Independent Primitives") INFO "Platform-Independent Primitives")
make_rc(${_target} _RC) make_rc(${_target} _RC)
@@ -156,7 +139,7 @@ macro(pip_module NAME LIBS LABEL INCLUDES SOURCES MSG)
pip_resources(CRES "${RES}") pip_resources(CRES "${RES}")
endif() endif()
add_definitions(-D${DEF_NAME}) add_definitions(-D${DEF_NAME})
add_library(${_target} ${PIP_LIB_TYPE} ${CPPS} ${CRES} ${_RC} ${HS} ${PHS}) add_library(${_target} ${pip_LIB_TYPE} ${CPPS} ${CRES} ${_RC})
target_include_directories(${_target} PUBLIC ${PIP_INCLUDES}) target_include_directories(${_target} PUBLIC ${PIP_INCLUDES})
if (NOT "x${RES}" STREQUAL "x") if (NOT "x${RES}" STREQUAL "x")
add_dependencies(${_target} pip_rc) add_dependencies(${_target} pip_rc)
@@ -251,11 +234,26 @@ if(PIP_MATH_YN)
add_definitions(-DPIP_MATH_YN) add_definitions(-DPIP_MATH_YN)
endif() endif()
# Check if RT timers exists
set(CMAKE_REQUIRED_INCLUDES time.h)
set(CMAKE_REQUIRED_LIBRARIES )
if((NOT DEFINED ENV{QNX_HOST}) AND (NOT APPLE) AND (NOT WIN32) AND (NOT DEFINED ANDROID_PLATFORM) AND (NOT PIP_FREERTOS))
list(APPEND LIBS_MAIN rt)
set(CMAKE_REQUIRED_LIBRARIES rt)
endif()
CHECK_FUNCTION_EXISTS(timer_create PIP_TIMER_RT_0)
CHECK_FUNCTION_EXISTS(timer_settime PIP_TIMER_RT_1)
CHECK_FUNCTION_EXISTS(timer_delete PIP_TIMER_RT_2)
# Check if build debug version # Check if build debug version
if (PIP_BUILD_DEBUG) if (CMAKE_BUILD_TYPE MATCHES Debug)
set(PIP_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3 -Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3 -Wall")
add_definitions(-DPIP_DEBUG) add_definitions(-DPIP_DEBUG)
else() else()
set(PIP_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall")
endif() endif()
@@ -309,6 +307,15 @@ list(APPEND HDRS ${_PIP_DEFS_FILE})
#message("${_PIP_DEFS_CHANGED}") #message("${_PIP_DEFS_CHANGED}")
# Check if RT timers exists
if(PIP_TIMER_RT_0 AND PIP_TIMER_RT_1 AND PIP_TIMER_RT_2)
set(PIP_TIMERS "Thread, ThreadRT, Pool")
add_definitions(-DPIP_TIMER_RT)
else()
set(PIP_TIMERS "Thread, Pool")
endif()
# Add main library # Add main library
if(APPLE) if(APPLE)
@@ -319,27 +326,23 @@ if ((NOT DEFINED SHSTKPROJECT) AND (DEFINED ANDROID_PLATFORM))
#message("${ANDROID_SYSTEM_LIBRARY_PATH}/usr/include") #message("${ANDROID_SYSTEM_LIBRARY_PATH}/usr/include")
#message("${ANDROID_NDK}/sysroot/usr/include") #message("${ANDROID_NDK}/sysroot/usr/include")
endif() endif()
if(NOT PIP_FREERTOS) if(NOT PIP_FREERTOS)
if(WIN32) if(WIN32)
if(${C_COMPILER} STREQUAL "cl.exe") if(${C_COMPILER} STREQUAL "cl.exe")
else()
list(APPEND LIBS_MAIN ws2_32 iphlpapi psapi cfgmgr32 setupapi)
endif()
else() else()
list(APPEND LIBS_MAIN dl) list(APPEND LIBS_MAIN ws2_32 iphlpapi psapi cfgmgr32 setupapi)
if(DEFINED ENV{QNX_HOST}) endif()
list(APPEND LIBS_MAIN socket) else()
else() list(APPEND LIBS_MAIN dl)
if (NOT DEFINED ANDROID_PLATFORM) if(DEFINED ENV{QNX_HOST})
list(APPEND LIBS_MAIN pthread util) list(APPEND LIBS_MAIN socket)
if (NOT APPLE) else()
list(APPEND LIBS_MAIN rt) if (NOT DEFINED ANDROID_PLATFORM)
endif() list(APPEND LIBS_MAIN pthread util)
endif()
endif() endif()
endif() endif()
endif() endif()
endif()
set(PIP_LIBS) set(PIP_LIBS)
if(PIP_FREERTOS) if(PIP_FREERTOS)
set(PIP_LIBS ${LIBS_MAIN}) set(PIP_LIBS ${LIBS_MAIN})
@@ -396,7 +399,6 @@ if (NOT CROSSTOOLS)
pip_find_lib(sodium) pip_find_lib(sodium)
if(sodium_FOUND) if(sodium_FOUND)
pip_module(crypt "sodium" "PIP crypt support" "" "" "") pip_module(crypt "sodium" "PIP crypt support" "" "" "")
pip_module(client_server "pip_io_utils" "PIP client-server helper" "" "" "")
pip_module(cloud "pip_io_utils" "PIP cloud support" "" "" "") pip_module(cloud "pip_io_utils" "PIP cloud support" "" "" "")
endif() endif()
@@ -446,16 +448,17 @@ if (NOT CROSSTOOLS)
endif() endif()
if (NOT "x${MINGW_INCLUDE}" STREQUAL "x")
list(APPEND CMAKE_INCLUDE_PATH "${MINGW_INCLUDE}")
endif()
find_package(OpenCL QUIET) #OpenCL_VERSION_STRING find_package(OpenCL QUIET) #OpenCL_VERSION_STRING
if(OpenCL_FOUND) if(OpenCL_FOUND)
set(_opencl_lib OpenCL::OpenCL)
if(${CMAKE_VERSION} VERSION_LESS "3.7.0")
target_link_libraries(_opencl_lib OpenCL)
endif()
set(_opencl_inc "${OpenCL_INCLUDE_DIRS}") set(_opencl_inc "${OpenCL_INCLUDE_DIRS}")
if(APPLE) if(APPLE)
set(_opencl_inc "${OpenCL_INCLUDE_DIRS}/Headers") set(_opencl_inc "${OpenCL_INCLUDE_DIRS}/Headers")
endif() endif()
pip_module(opencl "OpenCL" "PIP OpenCL support" "${_opencl_inc}" "" " (${OpenCL_VERSION_STRING})") pip_module(opencl "${_opencl_lib}" "PIP OpenCL support" "${_opencl_inc}" "" " (${OpenCL_VERSION_STRING})")
endif() endif()
@@ -477,19 +480,15 @@ if (NOT CROSSTOOLS)
endif() endif()
list(APPEND HDR_DIRS "${_lua_bri_dir}/LuaBridge") list(APPEND HDR_DIRS "${_lua_bri_dir}/LuaBridge")
list(APPEND HDRS ${_lua_src_hdr}) list(APPEND HDRS ${_lua_src_hdr})
# Test program # Test program
if(PIP_UTILS) if(PIP_UTILS)
#add_library(pip_plugin SHARED "test_plugin.h" "test_plugin.cpp")
#add_library(pip_plugin SHARED "test_plugin.h" "test_plugin.cpp" "ccm.h" "ccm.cpp")
#target_link_libraries(pip_plugin pip) #target_link_libraries(pip_plugin pip)
add_executable(pip_test "main.cpp") add_executable(pip_test "main.cpp")
target_link_libraries(pip_test pip pip_io_utils pip_client_server) target_link_libraries(pip_test pip)
if(sodium_FOUND)
add_executable(pip_cloud_test "main_picloud_test.cpp")
target_link_libraries(pip_cloud_test pip_cloud)
endif()
endif() endif()
else() else()
@@ -563,7 +562,6 @@ if(NOT PIP_FREERTOS)
add_subdirectory("utils/code_model_generator") add_subdirectory("utils/code_model_generator")
add_subdirectory("utils/resources_compiler") add_subdirectory("utils/resources_compiler")
add_subdirectory("utils/deploy_tool") add_subdirectory("utils/deploy_tool")
add_subdirectory("utils/value_tree_translator")
if(PIP_UTILS AND (NOT CROSSTOOLS)) if(PIP_UTILS AND (NOT CROSSTOOLS))
add_subdirectory("utils/system_test") add_subdirectory("utils/system_test")
add_subdirectory("utils/udp_file_transfer") add_subdirectory("utils/udp_file_transfer")
@@ -590,18 +588,18 @@ if ((NOT PIP_FREERTOS) AND (NOT CROSSTOOLS))
find_package(Doxygen) find_package(Doxygen)
if(DOXYGEN_FOUND) if(DOXYGEN_FOUND)
set(DOXY_DEFINES "${PIP_EXPORTS}") set(DOXY_DEFINES "${PIP_EXPORTS}")
foreach (_m "console" "usb" "compress" "crypt" "client_server" "cloud" "fftw" "opencl" "io_utils" "lua") foreach (_m "console" "usb" "compress" "crypt" "cloud" "fftw" "opencl" "io_utils" "lua")
string(TOUPPER "${_m}" _mdef) string(TOUPPER "${_m}" _mdef)
list(APPEND DOXY_DEFINES "PIP_${_mdef}_EXPORT") list(APPEND DOXY_DEFINES "PIP_${_mdef}_EXPORT")
endforeach() endforeach()
set(DOXY_PROJECT_NUMBER "${PIP_VERSION}") set(DOXY_PROJECT_NUMBER "${pip_VERSION}")
set(DOXY_QHP_CUST_FILTER_ATTRS "\"PIP ${PIP_VERSION}\"") set(DOXY_QHP_CUST_FILTER_ATTRS "\"PIP ${pip_VERSION}\"")
set(DOXY_QHP_SECT_FILTER_ATTRS "\"PIP ${PIP_VERSION}\"") set(DOXY_QHP_SECT_FILTER_ATTRS "\"PIP ${pip_VERSION}\"")
set(DOXY_EXAMPLE_PATH "\"${CMAKE_CURRENT_SOURCE_DIR}/doc/examples\"") set(DOXY_EXAMPLE_PATH "\"${CMAKE_CURRENT_SOURCE_DIR}/doc/examples\"")
set(DOXY_IMAGE_PATH "\"${CMAKE_CURRENT_SOURCE_DIR}/doc/images\"") set(DOXY_IMAGE_PATH "\"${CMAKE_CURRENT_SOURCE_DIR}/doc/images\"")
set(DOXY_LOGO_PATH "\"${CMAKE_CURRENT_SOURCE_DIR}/doc/pip.png\"") set(DOXY_LOGO_PATH "\"${CMAKE_CURRENT_SOURCE_DIR}/doc/pip.png\"")
set(DOXY_EXCLUDE "\"${CMAKE_CURRENT_SOURCE_DIR}/libs/lua/3rd\"") set(DOXY_EXCLUDE "\"${CMAKE_CURRENT_SOURCE_DIR}/libs/lua/3rd\"")
set(DOXY_DOMAIN "${PIP_DOMAIN}.${PROJECT_NAME}.doc") set(DOXY_DOMAIN "${pip_DOMAIN}.${PROJECT_NAME}.doc")
if ("x${DOC_LANG}" STREQUAL "x") if ("x${DOC_LANG}" STREQUAL "x")
set(DOXY_OUTPUT_LANGUAGE English) set(DOXY_OUTPUT_LANGUAGE English)
set(DOXY_OUTPUT_DIR en) set(DOXY_OUTPUT_DIR en)
@@ -647,9 +645,9 @@ endmacro()
list(REMOVE_ITEM LIBS_STATUS ${PIP_MODULES}) list(REMOVE_ITEM LIBS_STATUS ${PIP_MODULES})
message("----------PIP----------") message("----------PIP----------")
message(" Version: ${PIP_VERSION} ") message(" Version: ${pip_VERSION} ")
message(" Linkage: ${PIP_LIB_TYPE_MSG}") message(" Linkage: ${pip_LIB_TYPE_MSG}")
message(" Type : ${CMAKE_BUILD_TYPE}") message(" Type : ${pip_BUILD_TYPE}")
if (NOT LOCAL) if (NOT LOCAL)
message(" Install: \"${CMAKE_INSTALL_PREFIX}\"") message(" Install: \"${CMAKE_INSTALL_PREFIX}\"")
else() else()
@@ -661,6 +659,7 @@ message("")
message(" Options:") message(" Options:")
message(" std::iostream: ${PIP_STD_IOSTREAM}") message(" std::iostream: ${PIP_STD_IOSTREAM}")
message(" ICU strings : ${PIP_ICU}") message(" ICU strings : ${PIP_ICU}")
message(" Timer types : ${PIP_TIMERS}")
message(" Introspection: ${PIP_INTROSPECTION}") message(" Introspection: ${PIP_INTROSPECTION}")
message(" Coverage : ${PIP_COVERAGE}") message(" Coverage : ${PIP_COVERAGE}")
if(INTROSPECTION) if(INTROSPECTION)

View File

@@ -33,10 +33,10 @@ You should add ${<out_var>} to your target.
## Documentation ## Documentation
[🇺🇸 Online documentation](https://shstk.ru/pip/html/en/index.html) [🇺🇸 Online documentation](https://shs.tools/pip/html/en/index.html)
[🇺🇸 Qt-help](https://shstk.ru/pip/pip_en.qch) [🇺🇸 Qt-help](https://shs.tools/pip/pip_en.qch)
[🇷🇺 Онлайн документация](https://shstk.ru/pip/html/ru/index.html) [🇷🇺 Онлайн документация](https://shs.tools/pip/html/ru/index.html)
[🇷🇺 Qt-help](https://shstk.ru/pip/pip_ru.qch) [🇷🇺 Qt-help](https://shs.tools/pip/pip_ru.qch)

View File

@@ -9,7 +9,6 @@ Create imported targets:
* PIP::FFTW * PIP::FFTW
* PIP::OpenCL * PIP::OpenCL
* PIP::IOUtils * PIP::IOUtils
* PIP::ClientServer
* PIP::Cloud * PIP::Cloud
* PIP::Lua * PIP::Lua
@@ -21,35 +20,40 @@ main library
cmake_policy(SET CMP0011 NEW) # don`t affect includer policies cmake_policy(SET CMP0011 NEW) # don`t affect includer policies
include(SHSTKMacros) include(SHSTKMacros)
shstk_set_find_dirs(PIP) shstk_set_find_dirs(pip)
if(PIP_DIR)
list(APPEND pip_LIBDIR "${PIP_DIR}/lib")
list(APPEND pip_INCDIR "${PIP_DIR}/include/pip")
list(APPEND pip_BINDIR "${PIP_DIR}/bin")
endif()
set(__libs "usb;crypt;console;fftw;compress;opencl;io_utils;client_server;cloud;lua") set(__libs "usb;crypt;console;fftw;compress;io_utils;opencl;cloud;lua")
if (BUILDING_PIP) if (BUILDING_pip)
#set(_libs "pip;pip_usb;pip_console;pip_crypt;pip_fftw;pip_compress;pip_opencl;pip_io_utils;pip_cloud;pip_lua") #set(_libs "pip;pip_usb;pip_console;pip_crypt;pip_fftw;pip_compress;pip_opencl;pip_io_utils;pip_cloud;pip_lua")
#set(_bins "pip_cmg;pip_rc;deploy_tool") #set(_bins "pip_cmg;pip_rc;deploy_tool")
#get_target_property(_path pip BINARY_DIR) #get_target_property(_path pip BINARY_DIR)
#get_target_property(_path pip LIBRARY_OUTPUT_NAME) #get_target_property(_path pip LIBRARY_OUTPUT_NAME)
#message("${_path}") #message("${_path}")
#set(PIP_LIBRARY "$<TARGET_FILE_DIR:pip>/$<TARGET_FILE_NAME:pip>" CACHE STRING "") #set(PIP_LIBRARY "$<TARGET_FILE_DIR:pip>/$<TARGET_FILE_NAME:pip>" CACHE STRING "")
set(PIP_LIBRARY pip CACHE STRING "") set(PIP_LIBRARY pip CACHE STRING "")
set(PIP_FOUND ON CACHE BOOL "") set(PIP_FOUND ON CACHE BOOL "")
else() else()
find_library(PIP_LIBRARY pip HINTS ${PIP_LIBDIR}) find_library(PIP_LIBRARY pip HINTS ${pip_LIBDIR})
foreach (_l ${__libs}) foreach (_l ${__libs})
find_library(PIP_LIBRARY_${_l} pip_${_l} HINTS ${PIP_LIBDIR}) find_library(PIP_LIBRARY_${_l} pip_${_l} HINTS ${pip_LIBDIR})
endforeach() endforeach()
endif() endif()
if (BUILDING_PIP AND (NOT CMAKE_CROSSCOMPILING)) if (BUILDING_pip AND (NOT CMAKE_CROSSCOMPILING))
set(PIP_CMG "$<TARGET_FILE_DIR:pip_cmg>/$<TARGET_FILE_NAME:pip_cmg>" CACHE STRING "") set(PIP_CMG "$<TARGET_FILE_DIR:pip_cmg>/$<TARGET_FILE_NAME:pip_cmg>" CACHE STRING "")
set(PIP_RC "$<TARGET_FILE_DIR:pip_rc>/$<TARGET_FILE_NAME:pip_rc>" CACHE STRING "") set(PIP_RC "$<TARGET_FILE_DIR:pip_rc>/$<TARGET_FILE_NAME:pip_rc>" CACHE STRING "")
set(PIP_DEPLOY_TOOL "$<TARGET_FILE_DIR:deploy_tool>/$<TARGET_FILE_NAME:deploy_tool>" CACHE STRING "") set(PIP_DEPLOY_TOOL "$<TARGET_FILE_DIR:deploy_tool>/$<TARGET_FILE_NAME:deploy_tool>" CACHE STRING "")
else() else()
find_program(PIP_CMG pip_cmg${PIP_BINEXT} HINTS ${PIP_BINDIR} ${PIP_FIND_PROGRAM_ARG}) find_program(PIP_CMG pip_cmg${pip_BINEXT} HINTS ${pip_BINDIR} ${pip_FIND_PROGRAM_ARG})
find_program(PIP_RC pip_rc${PIP_BINEXT} HINTS ${PIP_BINDIR} ${PIP_FIND_PROGRAM_ARG}) find_program(PIP_RC pip_rc${pip_BINEXT} HINTS ${pip_BINDIR} ${pip_FIND_PROGRAM_ARG})
find_program(PIP_DEPLOY_TOOL deploy_tool${PIP_BINEXT} HINTS ${PIP_BINDIR} ${PIP_FIND_PROGRAM_ARG}) find_program(PIP_DEPLOY_TOOL deploy_tool${pip_BINEXT} HINTS ${pip_BINDIR} ${pip_FIND_PROGRAM_ARG})
endif() endif()
if (NOT PIP_LIBRARY) if (NOT PIP_LIBRARY)
if(PIP_FIND_REQUIRED) if(PIP_FIND_REQUIRED)
@@ -59,7 +63,7 @@ if (NOT PIP_LIBRARY)
endif() endif()
set(_PIP_LIBRARY_PATH_ "${PIP_LIBRARY}") set(_PIP_LIBRARY_PATH_ "${PIP_LIBRARY}")
set(_PIP_ADD_LIBS_ "") set(_PIP_ADD_LIBS_ "")
if (NOT BUILDING_PIP) if (NOT BUILDING_pip)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
find_library(DL_LIBRARY dl) find_library(DL_LIBRARY dl)
list(APPEND PIP_LIBRARY ${DL_LIBRARY}) list(APPEND PIP_LIBRARY ${DL_LIBRARY})
@@ -76,24 +80,23 @@ if (NOT BUILDING_PIP)
endif() endif()
endif() endif()
if (NOT BUILDING_PIP) if (NOT BUILDING_pip)
shstk_find_header(PIP "pip_version.h" "${_PIP_LIBRARY_PATH_}") shstk_find_header(pip PIP "pip_version.h" "${_PIP_LIBRARY_PATH_}")
set(PIP_INCLUDES "${PIP_INCLUDES}" CACHE STRING "") set(PIP_INCLUDES "${pip_INCLUDES}" CACHE STRING "")
endif() endif()
if(PIP_FIND_VERSION VERSION_GREATER PIP_VERSION) if(PIP_FIND_VERSION VERSION_GREATER PIP_VERSION)
message(FATAL_ERROR "PIP version ${PIP_VERSION} is available, but ${PIP_FIND_VERSION} requested!") message(FATAL_ERROR "PIP version ${PIP_VERSION} is available, but ${PIP_FIND_VERSION} requested!")
endif() endif()
set(__module_usb USB ) set(__module_usb USB )
set(__module_console Console ) set(__module_console Console )
set(__module_crypt Crypt ) set(__module_crypt Crypt )
set(__module_fftw FFTW ) set(__module_fftw FFTW )
set(__module_compress Compress ) set(__module_compress Compress )
set(__module_opencl OpenCL ) set(__module_opencl OpenCL )
set(__module_io_utils IOUtils ) set(__module_io_utils IOUtils )
set(__module_client_server ClientServer) set(__module_cloud Cloud )
set(__module_cloud Cloud ) set(__module_lua Lua )
set(__module_lua Lua )
foreach (_l ${__libs}) foreach (_l ${__libs})
set( __inc_${_l} "") set( __inc_${_l} "")
@@ -101,15 +104,14 @@ foreach (_l ${__libs})
set(__libs_${_l} "") set(__libs_${_l} "")
endforeach() endforeach()
set(__deps_io_utils "PIP::Crypt" ) set(__deps_io_utils "PIP::Crypt")
set(__deps_client_server "PIP::IOUtils") set(__deps_cloud "PIP::IOUtils")
set(__deps_cloud "PIP::IOUtils")
if (BUILDING_PIP) if (BUILDING_pip)
if (NOT SET_TARGETS_PIP) if (NOT SET_TARGETS_pip)
set(SET_TARGETS_PIP ON CACHE BOOL "") set(SET_TARGETS_pip ON CACHE BOOL "")
#message("create aliases") #message("create aliases")
if((NOT TARGET PIP) AND PIP_LIBRARY) if((NOT TARGET PIP) AND PIP_LIBRARY)
#message("alias PIP = pip") #message("alias PIP = pip")
@@ -131,7 +133,7 @@ else()
add_library(PIP UNKNOWN IMPORTED) add_library(PIP UNKNOWN IMPORTED)
set_target_properties(PIP PROPERTIES set_target_properties(PIP PROPERTIES
IMPORTED_LOCATION "${_PIP_LIBRARY_PATH_}" IMPORTED_LOCATION "${_PIP_LIBRARY_PATH_}"
INTERFACE_INCLUDE_DIRECTORIES "${PIP_INCLUDES}" INTERFACE_INCLUDE_DIRECTORIES "${pip_INCLUDES}"
INTERFACE_LINK_LIBRARIES "${_PIP_ADD_LIBS_}") INTERFACE_LINK_LIBRARIES "${_PIP_ADD_LIBS_}")
#message("imported PIP = ${PIP_LIBRARY}") #message("imported PIP = ${PIP_LIBRARY}")
endif() endif()

View File

@@ -2282,7 +2282,7 @@ EXTERNAL_GROUPS = YES
# be listed. # be listed.
# The default value is: YES. # The default value is: YES.
EXTERNAL_PAGES = NO EXTERNAL_PAGES = YES
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
# Configuration options related to the dot tool # Configuration options related to the dot tool

View File

@@ -9,14 +9,8 @@ struct S {
}; };
// Operators // Operators
PIByteArray & operator<<(PIByteArray & b, const S & s) { PIByteArray & operator <<(PIByteArray & b, const S & s) {b << s.i << s.f << s.s; return b;}
b << s.i << s.f << s.s; PIByteArray & operator >>(PIByteArray & b, S & s) {b >> s.i >> s.f >> s.s; return b;}
return b;
}
PIByteArray & operator>>(PIByteArray & b, S & s) {
b >> s.i >> s.f >> s.s;
return b;
}
//! [struct] //! [struct]
//! [write] //! [write]
// Write chunk stream // Write chunk stream
@@ -28,7 +22,10 @@ PIVector<float> f;
f << -1. << 2.5 << 11.; f << -1. << 2.5 << 11.;
// write some data to empty stream // write some data to empty stream
PIChunkStream cs; PIChunkStream cs;
cs << cs.chunk(1, int(10)) << cs.chunk(2, PIString("text")) << cs.chunk(4, f) << cs.chunk(3, s); cs << cs.chunk(1, int(10))
<< cs.chunk(2, PIString("text"))
<< cs.chunk(4, f)
<< cs.chunk(3, s);
// now you can take cs.data() and send or place it somewhere ... // now you can take cs.data() and send or place it somewhere ...
//! [write] //! [write]
//! [read] //! [read]
@@ -45,14 +42,14 @@ while (!cs2.atEnd()) {
case 1: i = cs2.getData<int>(); break; case 1: i = cs2.getData<int>(); break;
case 2: str = cs2.getData<PIString>(); break; case 2: str = cs2.getData<PIString>(); break;
case 3: s = cs2.getData<S>(); break; case 3: s = cs2.getData<S>(); break;
case 4: f = cs2.getData<PIVector<float>>(); break; case 4: f = cs2.getData<PIVector<float> >(); break;
} }
} }
piCout << i << str << f << s.i << s.f << s.s; piCout << i << str << f << s.i << s.f << s.s;
//! [read] //! [read]
//! [write_new] //! [write_new]
PIByteArray & operator<<(PIByteArray & s, const S & value) { PIByteArray & operator <<(PIByteArray & s, const S & value) {
PIChunkStream cs; PIChunkStream cs;
cs.add(1, value.i).add(2, value.f).add(3, value.s); cs.add(1, value.i).add(2, value.f).add(3, value.s);
s << cs.data(); s << cs.data();
@@ -60,11 +57,11 @@ PIByteArray & operator<<(PIByteArray & s, const S & value) {
} }
//! [write_new] //! [write_new]
//! [read_new] //! [read_new]
PIByteArray & operator>>(PIByteArray & s, S & value) { PIByteArray & operator >>(PIByteArray & s, S & value) {
PIChunkStream cs; PIChunkStream cs;
if (!cs.extract(s)) return s; if (!cs.extract(s)) return s;
cs.readAll(); cs.readAll();
cs.get(1, value.i).get(2, value.f).get(3, value.s); cs.get(1, value.i).get(2, value.f).get(3, value.s);
return s; return b;
} }
//! [read_new] //! [read_new]

View File

@@ -4,7 +4,7 @@
inline PICout operator <<(PICout s, const PIByteArray & ba) { inline PICout operator <<(PICout s, const PIByteArray & ba) {
s.space(); // insert space after previous output s.space(); // insert space after previous output
s.quote(); // ONLY if you want to quoted your type s.quote(); // ONLY if you want to quoted your type
s.saveAndSetControls(0); // clear all features and s.setControl(0, true); // clear all features and
// save them to stack, // save them to stack,
// now it`s behavior similar to std::cout // now it`s behavior similar to std::cout
@@ -12,7 +12,7 @@ inline PICout operator <<(PICout s, const PIByteArray & ba) {
for (uint i = 0; i < ba.size(); ++i) for (uint i = 0; i < ba.size(); ++i)
s << ba[i]; s << ba[i];
s.restoreControls(); // restore features from stack s.restoreControl(); // restore features from stack
s.quote(); // ONLY if you want to quoted your type s.quote(); // ONLY if you want to quoted your type
return s; return s;
} }

View File

@@ -7,19 +7,19 @@ class SomeIO: public PIIODevice {
public: public:
SomeIO(): PIIODevice() {} SomeIO(): PIIODevice() {}
protected: protected:
bool openDevice() override { bool openDevice() {
// open your device here // open your device here
return if_success; return if_success;
} }
ssize_t readDevice(void * read_to, ssize_t max_size) override { int read(void * read_to, int max_size) {
// read from your device here // read from your device here
return readed_bytes; return readed_bytes;
} }
ssize_t writeDevice(const void * data, ssize_t max_size) override { int write(const void * data, int max_size) {
// write to your device here // write to your device here
return written_bytes; return written_bytes;
} }
void configureFromFullPathDevice(const PIString & full_path) override { void configureFromFullPath(const PIString & full_path) {
// parse full_path and configure device here // parse full_path and configure device here
} }
}; };
@@ -38,7 +38,7 @@ ser.configure("example.conf", "dev");
//! [configureDevice] //! [configureDevice]
class SomeIO: public PIIODevice { class SomeIO: public PIIODevice {
... ...
bool configureDevice(const void * e_main, const void * e_parent) override { bool configureDevice(const void * e_main, const void * e_parent) {
PIConfig::Entry * em = (PIConfig::Entry * )e_main; PIConfig::Entry * em = (PIConfig::Entry * )e_main;
PIConfig::Entry * ep = (PIConfig::Entry * )e_parent; PIConfig::Entry * ep = (PIConfig::Entry * )e_parent;
setStringParam(readDeviceSetting<PIString>("stringParam", stringParam(), em, ep)); setStringParam(readDeviceSetting<PIString>("stringParam", stringParam(), em, ep));

View File

@@ -1,10 +1,10 @@
#include "pip.h" #include "pip.h"
//! [delimiter] //! [delimiter]
void tfunc(int delim) { void tfunc(void * , int delim) {
piCout << "tick with delimiter" << delim; piCout << "tick with delimiter" << delim;
}; };
void tfunc4(int delim) { void tfunc4(void * , int delim) {
piCout << "tick4 with delimiter" << delim; piCout << "tick4 with delimiter" << delim;
}; };
int main() { int main() {
@@ -13,7 +13,8 @@ int main() {
timer.addDelimiter(4, tfunc4); timer.addDelimiter(4, tfunc4);
timer.start(50); timer.start(50);
piMSleep(200); piMSleep(200);
timer.stopAndWait(); timer.stop();
timer.waitForFinish();
return 0; return 0;
}; };
/* Result: /* Result:
@@ -28,14 +29,14 @@ tick4 with delimiter 4
//! [delimiter] //! [delimiter]
//! [elapsed] //! [elapsed]
int main() { int main() {
PITimeMeasurer tm; PITimer timer;
piMSleep(100); piMSleep(100);
piCout << "elapsed" << tm.elapsed_m() << "ms"; piCout << "elapsed" << timer.elapsed_m() << "ms";
piMSleep(100); piMSleep(100);
piCout << "elapsed" << tm.elapsed_m() << "ms"; piCout << "elapsed" << timer.elapsed_m() << "ms";
tm.reset(); timer.reset();
piMSleep(150); piMSleep(150);
piCout << "elapsed" << tm.elapsed_s() << "s"; piCout << "elapsed" << timer.elapsed_s() << "s";
return 0; return 0;
}; };
/* Result: /* Result:
@@ -46,21 +47,22 @@ elapsed 0.15 s
//! [elapsed] //! [elapsed]
//! [system_time] //! [system_time]
int main() { int main() {
PISystemTime t0; // s = ns = 0 PISystemTime t0; // s = ns = 0
t0.addMilliseconds(200); // s = 0, ns = 200000000 t0.addMilliseconds(200); // s = 0, ns = 200000000
t0.addMilliseconds(900); // s = 1, ns = 100000000 t0.addMilliseconds(900); // s = 1, ns = 100000000
t0 -= PISystemTime::fromSeconds(0.1); // s = 1, ns = 0 t0 -= PISystemTime::fromSeconds(0.1); // s = 1, ns = 0
t0.sleep(); // sleep for 1 second t0.sleep(); // sleep for 1 second
PISystemTime t1; PISystemTime t1;
t0 = PISystemTime::current(); t0 = currentSystemTime();
piMSleep(500); piMSleep(500);
t1 = PISystemTime::current(); t1 = currentSystemTime();
(t1 - t0).sleep(); // sleep for 500 milliseconds (t1 - t0).sleep(); // sleep for 500 milliseconds
return 0; return 0;
}; };
//! [system_time] //! [system_time]
void _(){ void _() {
}; };

View File

@@ -1,297 +0,0 @@
\~english \page code_model Code generation
\~russian \page code_model Генерация кода
\~english
\~russian
# Введение
Кодогенерация помогает в случаях, когда нужен доступ к строковому представлению
сущностей (классов, перечислений, ...), либо автоматизированная де/сериализация
структур и классов.
Например, необходимо для создания интерфейса получить в готовом виде список
пар "имя" = "значение" от какого-либо перечисления, либо обойти список
вложенных в класс структур с дополнительными метками. Или просто описать
структуру любой сложности, пометить поля номерами и получить готовые операторы
де/сериализации для PIBinaryStream с возможностью будущих изменений и сохранением
обратной совместимости.
# pip_cmg
PIP предоставляет утилиту, которая берет на вход файлы исходного кода, пути
включения, параметры и макросы, и на выходе создает h/cpp пару файлов с
необходимым функционалом. В зависимости от параметров, в этих файлах будут
присутствовать секции:
* метаинформации о сущностях;
* операторы де/сериализации;
* возможность получить PIVariant любого члена структуры по имени.
Параметры обработки:
* -s - не следовать "#include" внутри файлов;
* -I<include_dir> - добавить папку включения (например, -I.. -I../some_dir -I/usr/include);
* -D<define> - добавить макрос, PICODE всегда объявлен (например, -DMY_DEFINE добавит макрос MY_DEFINE).
Параметры создания:
* -A - создать всё;
* -M - создать метаинформацию (имена и типы всех членов, иерархия включения);
* -E - создать перечисления (списки перечислений);
* -S - создать операторы де/сериализации;
* -G - создать методы получения значений переменных по именам;
* -o <output_file> - имя файлов модели без расширения (например, "ccm" - создадутся файлы "ccm.h" и "ccm.cpp")
# CMake
Для автоматизации кодогенерации существует CMake макрос pip_code_model, который сам вызывает pip_cmg и
следит за актуальностью модели.
Формат вызова макроса:
\code{.cmake}
pip_code_model(<out_var> file0 [file1 ...] [OPTIONS opt0 [opt1 ...] ] [NAME name])
\endcode
Параметры:
* out_var - имя переменной, куда будут записаны абсолютные пути сгенерённых файлов;
* file... - файлы для генерации, допускаются относительные или абсолютные пути;
* OPTIONS - передаваемые в pip_cmg параметры, например, "-Es";
* NAME - базовое имя файлов модели, если не указано, то используется "ccm_${PROJECT_NAME}".
Этот макрос сам включает все пути для PIP.
Для получения актуальных параметров pip_cmg можно вызывать "pip_cmg -v".
# Подробности
## Метаинформация
Метаинформация - это текстовое представление всех членов и методов структуры или класса C++.
Для доступа к ним используется PICODEINFO::classes().value("name"), который возвращает
указатель на структуру PICodeInfo::ClassInfo, содержащую всю информацию о сущности.
В любой структуре PICodeInfo есть поле "MetaMap meta", содержащее произвольные
данные, видимые для кодогенератора, но невидимые для компилятора.
Для этого используется макрос PIMETA(), который необходимо вписать после объявления
переменной, метода либо сущности, например:
\code{.cpp}
struct MyStruct: Header PIMETA(type=in,port=5005) {
ushort calcChecksum() const PIMETA(show=true);
bool checkChecksum() const;
void setChecksum();
uchar block_id PIMETA(type=int) = 0;
};
enum FOV { // Поле зрения
fovWide PIMETA(label="Широкое",angle=90),
fovNormal PIMETA(label="Нормальное",angle=60),
fovNarrow PIMETA(label="Узкое",angle=30)
};
\endcode
В этом примере в каждом месте, где указана PIMETA, её можно будет получить через "MetaMap meta".
## Перечисления
Перечисления записываются отдельно, для доступа к ним используется PICODEINFO::enums().value("name"),
который возвращает указатель на структуру PICodeInfo::EnumInfo, содержащую всю информацию о перечеслении.
## Операторы де/сериализации
Эти операторы создаются в h файле для всех сутрктур и классов, в которых есть хотя бы один член,
доступный для работы. Операторы работают с PIBinaryStream в двух вариантах - простом или через PIChunkStream.
Для каждой структуры можно указать режим де/сериализации с помощью фиксированного поля в PIMETA:
* нет указаний - работа через PIChunkStream;
* simple-stream - работа просто через PIBinaryStream;
* no-stream - не создавать операторы.
Например, для структуры
\code{.cpp}
struct DateTime {
uchar seconds;
uchar minutes;
uchar hours;
uchar days;
uchar months;
uchar years;
};
\endcode
создадутся операторы
\code{.cpp}
BINARY_STREAM_WRITE(DateTime) {
PIChunkStream cs;
cs << cs.chunk(1, v.seconds);
cs << cs.chunk(2, v.minutes);
cs << cs.chunk(3, v.hours);
cs << cs.chunk(4, v.days);
cs << cs.chunk(5, v.months);
cs << cs.chunk(6, v.years);
s << cs.data();
return s;
}
BINARY_STREAM_READ (DateTime) {
PIByteArray csba; s >> csba;
PIChunkStream cs(csba);
while (!cs.atEnd()) {
switch (cs.read()) {
case 1: cs.get(v.seconds); break;
case 2: cs.get(v.minutes); break;
case 3: cs.get(v.hours); break;
case 4: cs.get(v.days); break;
case 5: cs.get(v.months); break;
case 6: cs.get(v.years); break;
}
}
return s;
}
\endcode
, где порядок id последовательнен.
Для структуры
\code{.cpp}
struct DateTime PIMETA(simple-stream) {
uchar seconds;
uchar minutes;
uchar hours;
uchar days;
uchar months;
uchar years;
};
\endcode
создадутся операторы
\code{.cpp}
BINARY_STREAM_WRITE(DateTime) {
s << v.seconds;
s << v.minutes;
s << v.hours;
s << v.days;
s << v.months;
s << v.years;
return s;
}
BINARY_STREAM_READ (DateTime) {
s >> v.seconds;
s >> v.minutes;
s >> v.hours;
s >> v.days;
s >> v.months;
s >> v.years;
return s;
}
\endcode
Для структуры
\code{.cpp}
struct DateTime PIMETA(no-stream) {
uchar seconds;
uchar minutes;
uchar hours;
uchar days;
uchar months;
uchar years;
};
\endcode
не создадутся операторы
В режиме работы через PIChunkStream также можно указать индивидуальные id,
что очень полезно для сохранения обратной совместимости структур разных версий:
Для структуры
\code{.cpp}
struct DateTime {
PIMETA(id=10) uchar seconds;
PIMETA(id=11) uchar minutes;
PIMETA(id=12) uchar hours;
PIMETA(id=20) uchar days;
PIMETA(id=21) uchar months;
PIMETA(id=22) uchar years;
};
\endcode
\code{.cpp}
BINARY_STREAM_WRITE(DateTime) {
PIChunkStream cs;
cs << cs.chunk(10, v.seconds);
cs << cs.chunk(11, v.minutes);
cs << cs.chunk(12, v.hours);
cs << cs.chunk(20, v.days);
cs << cs.chunk(21, v.months);
cs << cs.chunk(22, v.years);
s << cs.data();
return s;
}
BINARY_STREAM_READ (DateTime) {
PIByteArray csba; s >> csba;
PIChunkStream cs(csba);
while (!cs.atEnd()) {
switch (cs.read()) {
case 10: cs.get(v.seconds); break;
case 11: cs.get(v.minutes); break;
case 12: cs.get(v.hours); break;
case 20: cs.get(v.days); break;
case 21: cs.get(v.months); break;
case 22: cs.get(v.years); break;
}
}
return s;
}
\endcode
Если в этом режиме какую-либо переменную надо проигнорировать, то вместо
числа id можно указать "-":
\code{.cpp}
struct DateTime {
PIMETA(id=10) uchar seconds;
PIMETA(id=11) uchar minutes;
PIMETA(id=-) uchar hours;
PIMETA(id=20) uchar days;
PIMETA(id=21) uchar months;
PIMETA(id=-) uchar years;
};
\endcode
\code{.cpp}
BINARY_STREAM_WRITE(DateTime) {
PIChunkStream cs;
cs << cs.chunk(10, v.seconds);
cs << cs.chunk(11, v.minutes);
cs << cs.chunk(20, v.days);
cs << cs.chunk(21, v.months);
s << cs.data();
return s;
}
BINARY_STREAM_READ (DateTime) {
PIByteArray csba; s >> csba;
PIChunkStream cs(csba);
while (!cs.atEnd()) {
switch (cs.read()) {
case 10: cs.get(v.seconds); break;
case 11: cs.get(v.minutes); break;
case 20: cs.get(v.days); break;
case 21: cs.get(v.months); break;
}
}
return s;
}
\endcode
# Интеграция в проект
При использовании CMake достаточно включить содержимое переменной out_var в приложение
или библиотеку, и включить через "#include" сгенерированный заголовочный файл в нужном месте.
После этого перечисления и метаинформация будут загружены в момент запуска, до int main(),
а операторы станут доступны через заголовочный файл.
CMakeLists.txt:
\code{.cmake}
project(myapp)
pip_code_model(CCM "structures.h" OPTIONS "-EMs")
add_executable(${PROJECT_NAME} ${CPPS} ${CCM})
\endcode
C++:
\code{.cpp}
#include "ccm_myapp.h"
...
PICodeInfo::EnumInfo * ei = PICODEINFO::enums().value("MyEnum", 0);
if (ei) {
ei->members.forEach([](const PICodeInfo::EnumeratorInfo & e){piCout << e.name << "=" << e.value;});
}
\endcode

View File

@@ -1,125 +0,0 @@
\~english \page iostream Input/Output stream
\~russian \page iostream Поток ввода/вывода
\~english
\~russian
%PIBinaryStream представляет собой интерфейс бинарной сериализации.
Не может быть использован в чистом виде, только в виде миксина или
готовых классов: PIByteArray и PIIOBinaryStream.
Используется для сохранения или чтения любых данных. Простые типы читаются/пишутся
как блоки памяти, если не созданы конкретные операторы. Сложные типы
([нетривиальные](https://ru.cppreference.com/w/cpp/types/is_trivially_copyable))
обязаны иметь операторы ввода/вывода, иначе возникнет ошибка компиляции.
Также поддерживаются контейнеры с типами по таким же правилам.
Перечисления интерпретируются как int, логические типы как один байт.
Операторы сохранения добавляют данные в конец потока, а операторы извлечения
берут данные из его начала.
Для облегчения написания операторов есть макросы:
* BINARY_STREAM_FRIEND(T) - объявить операторы с доступом к приватному
содержимому типа T, необязателен;
* BINARY_STREAM_WRITE(T) - запись в поток, "s" - объект потока, "v" - объект типа T;
* BINARY_STREAM_READ(T) - чтение из потока, "s" - объект потока, "v" - объект типа T.
Пример:
\~\code{.cpp}
#include <pibytearray.h>
class MyType {
BINARY_STREAM_FRIEND(MyType);
public:
void setInt(int v) {m_i = v;}
int getInt() const {return m_i;}
void setString(PIString v) {m_s = v;}
PIString getString() const {return m_s;}
private:
int m_i = 0;
PIString m_s;
};
BINARY_STREAM_WRITE(MyType) {s << v.m_i << v.m_s; return s;}
BINARY_STREAM_READ (MyType) {s >> v.m_i >> v.m_s; return s;}
int main(int argc, char * argv[]) {
MyType t_read, t_write;
t_write.setInt(10);
t_write.setString("text");
PIByteArray data;
data << t_write;
piCout << data.toHex();
data >> t_read;
piCout << t_read.getInt() << t_read.getString();
piCout << data.toHex();
}
\endcode
\~english Result:
\~russian Результат:
\~\code{.cpp}
0a000000040000007400650078007400
10 text
\endcode
\~english
For store/restore custom data blocks this is PIMemoryBlock class. Stream
operators of this class simply store/restore data block to/from stream:
\~russian
Для сохранения/извлечения блоков произвольных данных используется класс PIMemoryBlock.
Потоковые операторы для него просто сохраняют/извлекают блоки байтов в/из потока:
\~\code{.cpp}
float a_read[10], a_write[10];
for (int i = 0; i < 10; ++i) {
a_read [i] = 0.f;
a_write[i] = i / 10.f;
}
PIByteArray data;
data << PIMemoryBlock(a_write, 10 * sizeof(float));
piCout << data.toHex();
data >> PIMemoryBlock(a_read, 10 * sizeof(float));
for (int i = 0; i < 10; ++i)
piCout << a_read[i];
\endcode
\~english Result:
\~russian Результат:
\~\code{.cpp}
00000000cdcccc3dcdcc4c3e9a99993ecdcccc3e0000003f9a99193f3333333fcdcc4c3f6666663f
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
\endcode
\~english
\~russian
Если при чтении из потока не хватило данных (например, закончился массив или файл), то проверка
объекта потока на wasReadError() вернёт true. Рекомендуется делать эту проверку после чтения
данных для корректной обработки ошибки.

View File

@@ -6,7 +6,7 @@
* direct output to console (\a PICout) * direct output to console (\a PICout)
* containers (\a PIVector, \a PIDeque, \a PIVector2D, \a PIStack, \a PIQueue, \a PIMap, \a PISet) * containers (\a PIVector, \a PIDeque, \a PIVector2D, \a PIStack, \a PIQueue, \a PIMap, \a PISet)
* byte array (\a PIByteArray) * byte array (\a PIByteArray)
* serialization (\a PIBinaryStream, \a PITextStream, \a PIIOBinaryStream, \a PIIOTextStream, \a PIChunkStream, \a PIJSON) * serialization (\a PIChunkStream)
* string (\a PIConstChars, \a PIString, \a PIStringList) * string (\a PIConstChars, \a PIString, \a PIStringList)
* base object (events and handlers) (\a PIObject) * base object (events and handlers) (\a PIObject)
* multithreading * multithreading
@@ -57,7 +57,7 @@
* общение с консолью (\a PICout) * общение с консолью (\a PICout)
* контейнеры (\a PIVector, \a PIDeque, \a PIVector2D, \a PIStack, \a PIQueue, \a PIMap, \a PISet) * контейнеры (\a PIVector, \a PIDeque, \a PIVector2D, \a PIStack, \a PIQueue, \a PIMap, \a PISet)
* байтовый массив (\a PIByteArray) * байтовый массив (\a PIByteArray)
* сериализация (\a PIBinaryStream, \a PITextStream, \a PIIOBinaryStream, \a PIIOTextStream, \a PIChunkStream) * сериализация (\a PIChunkStream)
* строка (\a PIConstChars, \a PIString, \a PIStringList) * строка (\a PIConstChars, \a PIString, \a PIStringList)
* базовый объект (события и обработчики) (\a PIObject) * базовый объект (события и обработчики) (\a PIObject)
* многопоточность * многопоточность

View File

@@ -56,7 +56,7 @@ class MainClass: public PITimer {
public: public:
MainClass() {} MainClass() {}
protected: protected:
void tick(int delimiter) { void tick(void * data, int delimiter) {
piCout << "timer tick"; piCout << "timer tick";
// timer tick // timer tick
} }

View File

@@ -1 +1,2 @@
#include "pip.h" #include "pip.h"

View File

@@ -1,54 +0,0 @@
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include "piclientserver_client.h"
#include "piclientserver_server.h"
#include "piethernet.h"
void PIClientServer::ServerClient::createForServer(Server * parent, PIEthernet * tcp_) {
tcp = tcp_;
tcp->setParameter(PIEthernet::KeepConnection, false);
init();
CONNECTL(tcp, disconnected, ([this, parent](bool) { parent->clientDisconnected(this); }));
}
PIClientServer::Client::Client() {
tcp = new PIEthernet(PIEthernet::TCP_Client);
tcp->setParameter(PIEthernet::KeepConnection, true);
own_tcp = true;
init();
}
PIClientServer::Client::~Client() {
if (tcp) tcp->setDebug(false);
close();
stopAndWait();
}
void PIClientServer::Client::connect(PINetworkAddress addr) {
if (!tcp || !own_tcp) return;
close();
config.apply(this);
tcp->connect(addr, true);
tcp->startThreadedRead();
}

View File

@@ -1,117 +0,0 @@
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include "piclientserver_client_base.h"
#include "piethernet.h"
#include "piliterals_time.h"
PIClientServer::ClientBase::ClientBase() {}
PIClientServer::ClientBase::~ClientBase() {
close();
stopAndWait();
if (own_tcp) piDeleteSafety(tcp);
piDeleteSafety(diag);
}
void PIClientServer::ClientBase::close() {
if (!tcp) return;
can_write = false;
tcp->stop();
stream.clear();
}
void PIClientServer::ClientBase::stopAndWait() {
if (!tcp) return;
tcp->stopAndWait(10_s);
if (tcp->isThreadedRead()) tcp->terminateThreadedRead();
tcp->close();
stream.clear();
}
int PIClientServer::ClientBase::write(const void * d, const size_t s) {
if (!tcp) return -1;
if (!can_write) return 0;
PIMutexLocker guard(write_mutex);
// piCout << "... send ...";
stream.send(PIByteArray(d, s));
// piCout << "... send ok";
return s;
}
void PIClientServer::ClientBase::enableDiagnostics() {
if (diag) return;
diag = new PIDiagnostics();
}
PIDiagnostics::State PIClientServer::ClientBase::diagnostics() const {
if (!diag) return {};
return diag->state();
}
int PIClientServer::ClientBase::receivePacketProgress() const {
return stream.receivePacketProgress();
}
void PIClientServer::ClientBase::init() {
if (!tcp) return;
CONNECTL(&stream, sendRequest, [this](const PIByteArray & ba) {
if (!can_write) return;
tcp->send(ba);
if (diag) diag->sended(ba.size_s());
// piMSleep(1);
});
CONNECTL(&stream, packetReceiveEvent, [this](PIByteArray & ba) { readed(ba); });
CONNECTL(&stream, startPacketReceive, [this](int size) { receivePacketStart(size); });
CONNECTL(&stream, endPacketReceive, [this]() { receivePacketEnd(); });
CONNECTL(tcp, threadedReadEvent, [this](const uchar * readed, ssize_t size) {
if (!can_write) return;
stream.received(readed, size);
if (diag) diag->received(size);
});
CONNECTL(tcp, connected, [this]() {
can_write = true;
// piCout << "Connected";
connected();
});
CONNECTL(tcp, disconnected, [this](bool) {
can_write = false;
stream.clear();
// piCout << "Disconnected";
disconnected();
});
}
void PIClientServer::ClientBase::destroy() {
write_mutex.lock();
close();
piDeleteSafety(tcp);
write_mutex.unlock();
// piCout << "Destroyed";
}

View File

@@ -1,50 +0,0 @@
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include "piclientserver_config.h"
#include "piclientserver_client_base.h"
void PIClientServer::Config::setPacketSign(ushort sign) {
packet_sign = sign;
}
void PIClientServer::Config::setPacketSize(int bytes) {
packet_size = bytes;
}
void PIClientServer::Config::enableSymmetricEncryption(const PIByteArray & key) {
crypt_key = key;
}
void PIClientServer::Config::apply(ClientBase * client) {
auto & s(client->stream);
s.setPacketSign(packet_sign);
s.setMaxPacketSize(packet_size);
if (crypt_key.isNotEmpty()) {
s.setCryptEnabled(true);
s.setCryptKey(crypt_key);
} else
s.setCryptEnabled(false);
}

View File

@@ -1,143 +0,0 @@
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include "piclientserver_server.h"
#include "piclientserver_client.h"
#include "piethernet.h"
#include "piliterals_time.h"
PIClientServer::Server::Server() {
tcp_server = new PIEthernet(PIEthernet::TCP_Server);
clean_thread = new PIThread();
client_factory = [] { return new ServerClient(); };
CONNECTL(tcp_server, newConnection, [this](PIEthernet * c) {
PIMutexLocker guard(clients_mutex);
if (clients.size_s() >= max_clients) {
piCout << "Server::newConnection overflow clients count";
delete c;
return;
}
auto sc = client_factory();
if (!sc) {
piCout << "ClientFactory returns nullptr!";
return;
}
sc->createForServer(this, c);
newClient(sc);
});
clean_thread->start([this]() {
clean_notifier.wait();
PIVector<ServerClient *> to_delete;
clients_mutex.lock();
for (auto c: clients) {
const PIEthernet * eth = c->getTCP();
if (!eth) continue;
if (eth->isConnected()) continue;
c->can_write = false;
to_delete << c;
}
for (auto c: to_delete)
clients.removeOne(c);
clients_mutex.unlock();
for (auto c: to_delete) {
c->aboutDelete();
c->destroy();
delete c;
}
});
}
PIClientServer::Server::~Server() {
clean_thread->stop();
clean_notifier.notify();
clean_thread->waitForFinish();
piDeleteSafety(clean_thread);
stopServer();
for (auto c: clients) {
c->aboutDelete();
c->destroy();
delete c;
}
piDeleteSafety(tcp_server);
}
void PIClientServer::Server::listen(PINetworkAddress addr) {
if (!tcp_server) return;
stopServer();
is_closing = false;
tcp_server->listen(addr, true);
// piCout << "Listen on" << addr.toString();
}
void PIClientServer::Server::closeAll() {
clients_mutex.lock();
for (auto c: clients) {
c->aboutDelete();
c->destroy();
delete c;
}
clients.clear();
clients_mutex.unlock();
}
void PIClientServer::Server::setMaxClients(int new_max_clients) {
max_clients = new_max_clients;
}
int PIClientServer::Server::clientsCount() const {
PIMutexLocker guard(clients_mutex);
return clients.size_s();
}
void PIClientServer::Server::forEachClient(std::function<void(ServerClient *)> func) {
PIMutexLocker guard(clients_mutex);
for (auto * c: clients) {
func(c);
if (is_closing) break;
}
}
void PIClientServer::Server::stopServer() {
if (!tcp_server) return;
is_closing = true;
tcp_server->stopThreadedListen();
tcp_server->stopAndWait();
}
void PIClientServer::Server::newClient(ServerClient * c) {
clients << c;
config.apply(c);
c->tcp->startThreadedRead();
c->connected();
}
void PIClientServer::Server::clientDisconnected(ServerClient * c) {
clean_notifier.notify();
}

View File

@@ -1,7 +1,7 @@
#include "picloudbase.h" #include "picloudbase.h"
PICloudBase::PICloudBase(): eth(PIEthernet::TCP_Client), streampacker(&eth), tcp(&streampacker) { PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client), streampacker(&eth), tcp(&streampacker) {
eth.setDebug(false); eth.setDebug(false);
} }

View File

@@ -1,191 +1,178 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
PICloud Client PICloud Client
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "picloudclient.h" #include "picloudclient.h"
#include "picloudtcp.h"
#include "picloudtcp.h"
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() {
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode), PICloudBase() { tcp.setRole(PICloud::TCP::Client);
tcp.setRole(PICloud::TCP::Client); setName("cloud_client");
setThreadedReadBufferSize(eth.threadedReadBufferSize()); is_connected = false;
setName("cloud_client"); is_deleted = false;
is_connected = false; // setReopenEnabled(false);
is_deleted = false; CONNECTL(&eth, connected, [this](){opened_ = true; tcp.sendStart();});
// setReopenEnabled(false); CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, connected, [this]() { CONNECTL(&eth, disconnected, [this](bool){
opened_ = true; if (is_deleted) return;
tcp.sendStart(); bool need_disconn = is_connected;
}); //piCoutObj << "eth disconnected";
CONNECT1(void, PIByteArray, &streampacker, packetReceiveEvent, this, _readed); static_cast<PIThread*>(&eth)->stop();
CONNECTL(&eth, disconnected, [this](bool) { opened_ = false;
if (is_deleted) return; internalDisconnect();
bool need_disconn = is_connected; if (need_disconn)
// piCoutObj << "eth disconnected"; disconnected();
eth.stop(); //piCoutObj << "eth disconnected done";
opened_ = false; });
internalDisconnect(); }
if (need_disconn) disconnected();
// piCoutObj << "eth disconnected done";
}); PICloudClient::~PICloudClient() {
} //piCoutObj << "~PICloudClient()";
PIThread::stop();
//eth.close();
PICloudClient::~PICloudClient() { //if (is_connected) disconnected();
// piCoutObj << "~PICloudClient() ..." << this; close();
is_deleted = true; //piCoutObj << "~PICloudClient() closed";
stopAndWait(); internalDisconnect();
close(); // stop(false);
internalDisconnect(); is_deleted = true;
// piCoutObj << "~PICloudClient() done" << this; internalDisconnect();
} //piCoutObj << "~PICloudClient() done";
}
void PICloudClient::setServerName(const PIString & server_name) {
setName("cloud_client__" + server_name); void PICloudClient::setServerName(const PIString & server_name) {
tcp.setServerName(server_name); setName("cloud_client__" + server_name);
} tcp.setServerName(server_name);
}
void PICloudClient::setKeepConnection(bool on) {
eth.setParameter(PIEthernet::KeepConnection, on); void PICloudClient::setKeepConnection(bool on) {
} eth.setParameter(PIEthernet::KeepConnection, on);
}
void PICloudClient::interrupt() {
cond_buff.notifyOne(); bool PICloudClient::openDevice() {
cond_connect.notifyOne(); //piCoutObj << "open";// << path();
eth.interrupt(); bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
} if (op) {
mutex_connect.lock();
eth.startThreadedRead();
bool PICloudClient::openDevice() { //piCoutObj << "connecting...";
// piCoutObj << "open";// << path(); bool conn_ok = cond_connect.waitFor(mutex_connect, (int)eth.readTimeout());
bool op = eth.connect(PINetworkAddress::resolve(path()), false); //piCoutObj << "conn_ok" << conn_ok << is_connected;
if (op) { mutex_connect.unlock();
mutex_connect.lock(); if (!conn_ok) {
eth.startThreadedRead(); mutex_connect.lock();
// piCoutObj << "connecting..."; eth.stop();
bool conn_ok = cond_connect.waitFor(mutex_connect, eth.readTimeout()); eth.close();
// piCoutObj << "conn_ok" << conn_ok << is_connected; mutex_connect.unlock();
mutex_connect.unlock(); }
if (!conn_ok) { return is_connected;
mutex_connect.lock(); } else {
eth.stopAndWait(); //eth.close();
eth.close(); return false;
mutex_connect.unlock(); }
} }
return is_connected;
} else {
// eth.close(); bool PICloudClient::closeDevice() {
return false; //PIThread::stop();
} if (is_connected) {
} internalDisconnect();
}
eth.stop();
bool PICloudClient::closeDevice() { eth.close();
// PIThread::stop(); return true;
if (is_connected) { }
internalDisconnect();
}
eth.stopAndWait(); int PICloudClient::readDevice(void * read_to, int max_size) {
eth.close(); if (is_deleted) return -1;
return true; //piCoutObj << "readDevice";
} if (!is_connected && eth.isClosed()) openDevice();
int sz = -1;
mutex_buff.lock();
ssize_t PICloudClient::readDevice(void * read_to, ssize_t max_size) { cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;});
if (is_deleted || max_size <= 0) return -1; if (is_connected) {
// piCoutObj << "readDevice ..."; sz = piMini(max_size, buff.size());
if (!is_connected && eth.isClosed()) openDevice(); memcpy(read_to, buff.data(), sz);
ssize_t sz = -1; buff.remove(0, sz);
mutex_buff.lock(); }
if (is_connected) { mutex_buff.unlock();
if (buff.isEmpty()) { if (!is_connected) opened_ = false;
sz = 0; //piCoutObj << "readDevice done" << sz;
} else { return sz;
sz = piMin<ssize_t>(max_size, buff.size_s()); }
memcpy(read_to, buff.data(), sz);
buff.remove(0, sz);
} int PICloudClient::writeDevice(const void * data, int size) {
if (sz == 0) cond_buff.wait(mutex_buff); if (is_deleted) return -1;
} // piCoutObj << "writeDevice";
mutex_buff.unlock(); return tcp.sendData(PIByteArray(data, size));
if (!is_connected) opened_ = false; }
// piCoutObj << "readDevice done" << sz;
return sz;
} void PICloudClient::internalDisconnect() {
is_connected = false;
cond_buff.notifyOne();
ssize_t PICloudClient::writeDevice(const void * data, ssize_t size) { cond_connect.notifyOne();
if (is_deleted || !is_connected) return -1; streampacker.clear();
// piCoutObj << "writeDevice" << size; buff.clear();
return tcp.sendData(PIByteArray(data, size)); }
}
void PICloudClient::_readed(PIByteArray & ba) {
void PICloudClient::internalDisconnect() { if (is_deleted) return;
// piCoutObj << "internalDisconnect"; PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
is_connected = false; //piCoutObj << "_readed" << ba.size() << hdr.first << hdr.second;
cond_buff.notifyOne(); if (hdr.second == tcp.role()) {
cond_connect.notifyOne(); switch (hdr.first) {
streampacker.clear(); case PICloud::TCP::Connect:
buff.clear(); if (tcp.parseConnect(ba) == 1) {
} mutex_connect.lock();
is_connected = true;
mutex_connect.unlock();
void PICloudClient::_readed(PIByteArray & ba) { cond_connect.notifyOne();
if (is_deleted) return; connected();
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba); }
// piCoutObj << "_readed" << ba.size() << hdr.first << hdr.second; break;
if (hdr.second == tcp.role()) { case PICloud::TCP::Disconnect:
switch (hdr.first) { static_cast<PIThread*>(&eth)->stop();
case PICloud::TCP::Connect: opened_ = false;
if (tcp.parseConnect(ba) == 1) { eth.close();
mutex_connect.lock(); break;
is_connected = true; case PICloud::TCP::Data:
mutex_connect.unlock(); if (is_connected) {
cond_connect.notifyOne(); mutex_buff.lock();
connected(); buff.append(ba);
} mutex_buff.unlock();
break; cond_buff.notifyOne();
case PICloud::TCP::Disconnect: }
eth.stop(); break;
opened_ = false; default:
eth.close(); break;
break; }
case PICloud::TCP::Data: //piCoutObj << "readed" << ba.toHex();
if (is_connected) { }
mutex_buff.lock(); while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
if (buff.size_s() > threadedReadBufferSize()) { //piCoutObj << "_readed done";
piCoutObj << "Error: buffer overflow, drop" << ba.size() << "bytes"; }
mutex_buff.unlock();
return;
}
buff.append(ba);
mutex_buff.unlock();
cond_buff.notifyOne();
}
break;
default: break;
}
// piCoutObj << "readed" << ba.toHex();
}
// piCoutObj << "_readed done";
}

View File

@@ -1,300 +1,238 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
PICloud Server PICloud Server
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "picloudserver.h" #include "picloudserver.h"
#include "piliterals_time.h"
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() {
PIString server_name = "PCS_" + PIString::fromNumber(randomi()%1000);
PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode), PICloudBase() { tcp.setRole(PICloud::TCP::Server);
PIString server_name = "PCS_" + PIString::fromNumber(randomi() % 1000); tcp.setServerName(server_name);
tcp.setRole(PICloud::TCP::Server); setName("cloud_server__" + server_name);
tcp.setServerName(server_name); CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
setName("cloud_server__" + server_name); CONNECTL(&eth, connected, [this](){opened_ = true; piCoutObj << "connected"; tcp.sendStart();});
is_deleted = false; CONNECTL(&eth, disconnected, [this](bool){
eth.setReopenEnabled(false); piCoutObj << "disconnected";
setThreadedReadBufferSize(eth.threadedReadBufferSize()); static_cast<PIThread*>(&eth)->stop();
CONNECT1(void, PIByteArray, &streampacker, packetReceiveEvent, this, _readed); opened_ = false;
CONNECTL(&eth, connected, [this]() { ping_timer.stop(false);
open_mutex.lock(); piMSleep(100);
opened_ = true; });
cvar.notifyOne(); CONNECTL(&ping_timer, tickEvent, [this] (void *, int){
open_mutex.unlock(); if (eth.isConnected()) tcp.sendPing();
piCoutObj << "connected"; });
tcp.sendStart(); }
});
CONNECTL(&eth, disconnected, [this](bool) {
if (is_deleted) return; PICloudServer::~PICloudServer() {
piCoutObj << "disconnected"; stop();
clients_mutex.lock(); close();
for (auto c: clients_) { }
c->is_connected = false;
c->close();
} void PICloudServer::setServerName(const PIString & server_name) {
removed_clients_.append(clients_); setName("cloud_server__" + server_name);
clients_.clear(); tcp.setServerName(server_name);
index_clients.clear(); }
clients_mutex.unlock();
open_mutex.lock();
opened_ = false; PIVector<PICloudServer::Client *> PICloudServer::clients() const {
cvar.notifyOne(); PIMutexLocker _ml(clients_mutex);
open_mutex.unlock(); return clients_;
ping_timer.stop(); }
});
ping_timer.setSlot([this]() {
if (eth.isConnected()) tcp.sendPing(); bool PICloudServer::openDevice() {
}); //piCout << "PICloudServer open device" << path();
} bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) {
eth.startThreadedRead();
PICloudServer::~PICloudServer() { ping_timer.start(5000);
// piCoutObj << "~PICloudServer ..." << this; return true;
is_deleted = true; }
stop(); ping_timer.stop(false);
close(); eth.close();
waitThreadedReadFinished(); return false;
// piCout << "wait"; }
while (removed_clients_.isNotEmpty()) {
Client * c = removed_clients_.take_back();
delete c; bool PICloudServer::closeDevice() {
} eth.stop();
// piCoutObj << "~PICloudServer done" << this; ping_timer.stop(false);
} clients_mutex.lock();
for (auto c : clients_) {
c->close();
void PICloudServer::setServerName(const PIString & server_name) { c->stop();
setName("cloud_server__" + server_name); }
tcp.setServerName(server_name); clients_mutex.unlock();
} eth.close();
for (auto c : clients_)
delete c;
PIVector<PICloudServer::Client *> PICloudServer::clients() const { return true;
PIMutexLocker _ml(clients_mutex); }
return clients_;
}
int PICloudServer::readDevice(void * read_to, int max_size) {
//piCoutObj << "readDevice";
bool PICloudServer::openDevice() { if (!opened_) openDevice();
piCoutObj << "open device" << path(); else piMSleep(eth.readTimeout());
if (is_deleted) return false; return -1;
bool op = eth.connect(PINetworkAddress::resolve(path()), false); }
if (op) {
eth.startThreadedRead();
ping_timer.start(5_s); int PICloudServer::writeDevice(const void * data, int max_size) {
return true; //piCoutObj << "writeDevice";
} else { return -1;
ping_timer.stop(); }
eth.close();
return false;
} void PICloudServer::clientDisconnect(uint client_id) {
} tcp.sendDisconnected(client_id);
}
bool PICloudServer::closeDevice() {
// piCoutObj << "closeDevice" << this; int PICloudServer::sendData(const PIByteArray & data, uint client_id) {
eth.stopAndWait(); return tcp.sendData(data, client_id);
ping_timer.stop(); }
eth.close();
cvar.notifyOne();
clients_mutex.lock(); PICloudServer::Client::Client(PICloudServer * srv, uint id) : server(srv), client_id(id) {
for (auto c: clients_) { setMode(PIIODevice::ReadWrite);
c->is_connected = false; setReopenEnabled(false);
c->close(); is_connected = true;
} }
removed_clients_.append(clients_);
clients_.clear();
index_clients.clear(); PICloudServer::Client::~Client() {
clients_mutex.unlock(); if (is_connected) {
return true; is_connected = false;
} cond_buff.notifyOne();
}
close();
ssize_t PICloudServer::readDevice(void * read_to, ssize_t max_size) { stop();
if (is_deleted) return -1; }
// piCoutObj << "readDevice";
open_mutex.lock();
if (isOpened()) cvar.wait(open_mutex); bool PICloudServer::Client::openDevice() {
open_mutex.unlock(); return is_connected;
// piCoutObj << "opened_ = " << opened_; }
// else piMSleep(eth.readTimeout());
return -1;
} bool PICloudServer::Client::closeDevice() {
PIThread::stop(false);
if (is_connected) {
ssize_t PICloudServer::writeDevice(const void * data, ssize_t max_size) { server->clientDisconnect(client_id);
// piCoutObj << "writeDevice"; is_connected = false;
return -1; }
} cond_buff.notifyOne();
return true;
}
void PICloudServer::interrupt() {
eth.interrupt();
cvar.notifyOne(); int PICloudServer::Client::readDevice(void * read_to, int max_size) {
} if (!is_connected) return -1;
int sz = -1;
mutex_buff.lock();
void PICloudServer::clientDisconnect(uint client_id) { cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;});
tcp.sendDisconnected(client_id); if (is_connected) {
} sz = piMini(max_size, buff.size());
memcpy(read_to, buff.data(), sz);
buff.remove(0, sz);
int PICloudServer::sendData(const PIByteArray & data, uint client_id) { }
if (!opened_) return -1; mutex_buff.unlock();
return tcp.sendData(data, client_id); return sz;
} }
PICloudServer::Client::Client(PICloudServer * srv, uint id): server(srv), client_id(id) { int PICloudServer::Client::writeDevice(const void * data, int size) {
setMode(PIIODevice::ReadWrite); return server->sendData(PIByteArray(data, size), client_id);
setReopenEnabled(false); }
setThreadedReadBufferSize(server->threadedReadBufferSize());
is_connected = true;
} void PICloudServer::Client::pushBuffer(const PIByteArray & ba) {
if (!is_connected) return;
mutex_buff.lock();
PICloudServer::Client::~Client() { buff.append(ba);
// piCoutObj << "~PICloudServer::Client..." << this; cond_buff.notifyOne();
close(); mutex_buff.unlock();
stopAndWait(10_s); while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
// piCoutObj << "~PICloudServer::Client done" << this; }
}
void PICloudServer::_readed(PIByteArray & ba) {
bool PICloudServer::Client::openDevice() { PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
return is_connected; if (hdr.second == tcp.role()) {
} switch (hdr.first) {
case PICloud::TCP::Connect: {
uint id = tcp.parseConnect(ba);
bool PICloudServer::Client::closeDevice() { clients_mutex.lock();
// piCoutObj << "closeDevice" << this; Client * oc = index_clients.value(id, nullptr);
if (is_connected) { clients_mutex.unlock();
server->clientDisconnect(client_id); if (oc) {
is_connected = false; tcp.sendDisconnected(id);
} } else {
cond_buff.notifyOne(); //piCoutObj << "new Client" << id;
return true; Client * c = new Client(this, id);
} CONNECTU(c, deleted, this, clientDeleted);
clients_mutex.lock();
clients_ << c;
ssize_t PICloudServer::Client::readDevice(void * read_to, ssize_t max_size) { index_clients.insert(id, c);
if (!is_connected) return -1; clients_mutex.unlock();
ssize_t sz = -1; newConnection(c);
mutex_buff.lock(); }
if (is_connected) { } break;
if (buff.isEmpty()) { case PICloud::TCP::Disconnect: {
sz = 0; uint id = tcp.parseDisconnect(ba);
} else { //piCoutObj << "remove Client" << id;
sz = piMini(max_size, buff.size()); clients_mutex.lock();
memcpy(read_to, buff.data(), sz); Client * oc = index_clients.value(id, nullptr);
buff.remove(0, sz); clients_mutex.unlock();
} if (oc) {
if (sz == 0) cond_buff.wait(mutex_buff); oc->is_connected = false;
} oc->close();
mutex_buff.unlock(); }
return sz; } break;
} case PICloud::TCP::Data: {
PIPair<uint, PIByteArray> d = tcp.parseDataServer(ba);
clients_mutex.lock();
ssize_t PICloudServer::Client::writeDevice(const void * data, ssize_t size) { Client * oc = index_clients.value(d.first, nullptr);
if (!is_connected) return -1; clients_mutex.unlock();
return server->sendData(PIByteArray(data, size), client_id); //piCoutObj << "data for" << d.first << d.second.toHex();
} if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
} break;
default: break;
void PICloudServer::Client::interrupt() { }
cond_buff.notifyOne(); }
} }
void PICloudServer::Client::pushBuffer(const PIByteArray & ba) { void PICloudServer::clientDeleted(PIObject * o) {
if (!is_connected) return; PICloudServer::Client * c = (PICloudServer::Client*)o;
mutex_buff.lock(); clients_mutex.lock();
if (buff.size_s() > threadedReadBufferSize()) { clients_.removeOne(c);
piCoutObj << "Error: buffer overflow, drop" << ba.size() << "bytes"; auto it = index_clients.makeIterator();
mutex_buff.unlock(); while (it.hasNext()) {
return; it.next();
} if (it.value() == c) {
buff.append(ba); index_clients.remove(it.key());
cond_buff.notifyOne(); break;
mutex_buff.unlock(); }
} }
clients_mutex.unlock();
}
void PICloudServer::_readed(PIByteArray & ba) {
if (is_deleted) return;
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
if (hdr.second == tcp.role()) {
switch (hdr.first) {
case PICloud::TCP::Connect: {
uint id = tcp.parseConnect(ba);
clients_mutex.lock();
Client * oc = index_clients.value(id, nullptr);
clients_mutex.unlock();
if (oc) {
piCoutObj << "Warning: reject client with duplicated ID";
tcp.sendDisconnected(id);
} else {
Client * c = new Client(this, id);
// piCoutObj << "new Client" << id << c;
CONNECT1(void, PIObject *, c, deleted, this, clientDeleted);
clients_mutex.lock();
clients_ << c;
index_clients.insert(id, c);
clients_mutex.unlock();
newConnection(c);
}
} break;
case PICloud::TCP::Disconnect: {
uint id = tcp.parseDisconnect(ba);
// piCoutObj << "Close on logic";
clients_mutex.lock();
Client * oc = index_clients.take(id, nullptr);
clients_.removeOne(oc);
clients_mutex.unlock();
if (oc) {
oc->stopAndWait();
oc->is_connected = false;
oc->close();
removed_clients_ << oc;
// delete oc;
}
} break;
case PICloud::TCP::Data: {
PIPair<uint, PIByteArray> d = tcp.parseDataServer(ba);
clients_mutex.lock();
Client * oc = index_clients.value(d.first, nullptr);
clients_mutex.unlock();
// piCoutObj << "data for" << d.first << d.second.size();
if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
} break;
default: break;
}
}
}
void PICloudServer::clientDeleted(PIObject * o) {
PICloudServer::Client * c = (PICloudServer::Client *)o;
// piCoutObj << "clientDeleted" << c;
clients_mutex.lock();
clients_.removeOne(c);
removed_clients_.removeAll(c);
index_clients.removeWhere([c](uint, Client * v) { return v == c; });
clients_mutex.unlock();
}

View File

@@ -1,184 +1,181 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
PICloud TCP transport PICloud TCP transport
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "picloudtcp.h" #include "picloudtcp.h"
#include "picrypt.h"
#include "pichunkstream.h" #include "pichunkstream.h"
#include "picrypt.h" #include "piethernet.h"
#include "piethernet.h" #include "pistreampacker.h"
#include "pistreampacker.h"
const char hash_cloud_key[] = "_picloud_";
const char hash_cloud_key[] = "_picloud_";
PICloud::TCP::Header::Header() {
PICloud::TCP::Header::Header() { version = Version_2;
version = Version_2; }
}
PICloud::TCP::TCP(PIStreamPacker * s) : streampacker(s) {
PICloud::TCP::TCP(PIStreamPacker * s): streampacker(s) { streampacker->setMaxPacketSize(63*1024);
streampacker->setMaxPacketSize(63 * 1024); }
}
void PICloud::TCP::setRole(PICloud::TCP::Role r) {
void PICloud::TCP::setRole(PICloud::TCP::Role r) { header.role = r;
header.role = r; }
}
void PICloud::TCP::setServerName(const PIString & server_name_) {
void PICloud::TCP::setServerName(const PIString & server_name_) { server_name = server_name_;
server_name = server_name_; suuid = PICrypt::hash(PIByteArray(server_name_.data(), server_name_.size()), (const unsigned char *)hash_cloud_key, sizeof(hash_cloud_key));
suuid = }
PICrypt::hash(PIByteArray(server_name_.data(), server_name_.size()), (const unsigned char *)hash_cloud_key, sizeof(hash_cloud_key));
}
PIString PICloud::TCP::serverName() const {
return server_name;
PIString PICloud::TCP::serverName() const { }
return server_name;
}
void PICloud::TCP::sendStart() {
//piCout << "sendStart";
void PICloud::TCP::sendStart() { if (suuid.size() != PICrypt::sizeHash()) {
// piCout << "sendStart"; piCout << "PICloud ERROR, server not set, invoke setServerName first";
if (suuid.size() != PICrypt::sizeHash()) { return;
piCout << "PICloud ERROR, server not set, invoke setServerName first"; }
return; header.type = PICloud::TCP::Connect;
} PIByteArray ba;
header.type = PICloud::TCP::Connect; ba << header;
PIByteArray ba; ba.append(suuid);
ba << header; //mutex_send.lock();
ba.append(suuid); streampacker->send(ba);
// mutex_send.lock(); //mutex_send.unlock();
streampacker->send(ba); }
// mutex_send.unlock();
}
void PICloud::TCP::sendConnected(uint client_id) {
header.type = PICloud::TCP::Connect;
void PICloud::TCP::sendConnected(uint client_id) { PIByteArray ba;
header.type = PICloud::TCP::Connect; ba << header << client_id;
PIByteArray ba; // mutex_send.lock();
ba << header << client_id; streampacker->send(ba);
// mutex_send.lock(); // mutex_send.unlock();
streampacker->send(ba); }
// mutex_send.unlock();
}
void PICloud::TCP::sendDisconnected(uint client_id) {
header.type = PICloud::TCP::Disconnect;
void PICloud::TCP::sendDisconnected(uint client_id) { PIByteArray ba;
header.type = PICloud::TCP::Disconnect; ba << header << client_id;
PIByteArray ba; // mutex_send.lock();
ba << header << client_id; streampacker->send(ba);
// mutex_send.lock(); // mutex_send.unlock();
streampacker->send(ba); }
// mutex_send.unlock();
}
int PICloud::TCP::sendData(const PIByteArray & data) {
header.type = PICloud::TCP::Data;
int PICloud::TCP::sendData(const PIByteArray & data) { PIByteArray ba;
header.type = PICloud::TCP::Data; ba << header;
PIByteArray ba; ba.append(data);
ba << header; // piCout << "[PICloud::TCP] sendData" << ba.toHex();
ba.append(data); mutex_send.lock();
// piCout << "[PICloud::TCP] sendData" << ba.toHex(); streampacker->send(ba);
mutex_send.lock(); mutex_send.unlock();
streampacker->send(ba); return data.size_s();
mutex_send.unlock(); }
return data.size_s();
}
int PICloud::TCP::sendData(const PIByteArray & data, uint client_id) {
header.type = PICloud::TCP::Data;
int PICloud::TCP::sendData(const PIByteArray & data, uint client_id) { PIByteArray ba;
header.type = PICloud::TCP::Data; ba << header << client_id;
PIByteArray ba; ba.append(data);
ba << header << client_id; mutex_send.lock();
ba.append(data); streampacker->send(ba);
mutex_send.lock(); mutex_send.unlock();
streampacker->send(ba); return data.size_s();
mutex_send.unlock(); }
return data.size_s();
}
void PICloud::TCP::sendPing() {
header.type = PICloud::TCP::Ping;
void PICloud::TCP::sendPing() { PIByteArray ba;
header.type = PICloud::TCP::Ping; ba << header;
PIByteArray ba; ba.append(suuid);
ba << header; mutex_send.lock();
ba.append(suuid); streampacker->send(ba);
mutex_send.lock(); mutex_send.unlock();
streampacker->send(ba); }
mutex_send.unlock();
}
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) { ret.first = InvalidType;
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret; ret.second = InvalidRole;
ret.first = InvalidType; if (ba.size() < sizeof(Header)) return ret;
ret.second = InvalidRole; PICloud::TCP::Header hdr;
if (ba.size() < sizeof(Header)) return ret; ba >> hdr;
PICloud::TCP::Header hdr; if (hdr.version != header.version) {
ba >> hdr; piCout << "[PICloud]" << "invalid PICloud::TCP version!";
if (hdr.version != header.version) { return ret;
piCout << "[PICloud]" }
<< "invalid PICloud::TCP version!"; ret.first = (Type)hdr.type;
return ret; ret.second = (Role)hdr.role;
} return ret;
ret.first = (Type)hdr.type; }
ret.second = (Role)hdr.role;
return ret;
} bool PICloud::TCP::canParseData(PIByteArray & ba) {
return header.role == Client;
}
bool PICloud::TCP::canParseData(PIByteArray & ba) {
return header.role == Client;
} PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
PIPair<uint, PIByteArray> ret;
ret.first = 0;
PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) { if (header.role == Server) {
PIPair<uint, PIByteArray> ret; ba >> ret.first;
ret.first = 0; ret.second.swap(ba);
if (header.role == Server) { }
ba >> ret.first; return ret;
ret.second.swap(ba); }
}
return ret;
} PIByteArray PICloud::TCP::parseConnect_d(PIByteArray & ba) {
if (ba.size() != PICrypt::sizeHash()) {
piCout << "PICloud ERROR, invalid server uuid";
PIByteArray PICloud::TCP::parseConnect_d(PIByteArray & ba) { return PIByteArray();
if (ba.size() != PICrypt::sizeHash()) { } else {
piCout << "PICloud ERROR, invalid server uuid"; return ba;
return PIByteArray(); }
} else { }
return ba;
}
} uint PICloud::TCP::parseConnect(PIByteArray & ba) {
uint ret;
ba >> ret;
uint PICloud::TCP::parseConnect(PIByteArray & ba) { return ret;
uint ret = 0; }
ba >> ret;
return ret;
} uint PICloud::TCP::parseDisconnect(PIByteArray & ba) {
uint ret;
ba >> ret;
uint PICloud::TCP::parseDisconnect(PIByteArray & ba) { return ret;
uint ret = 0; }
ba >> ret;
return ret;
}

View File

@@ -1,81 +1,76 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Compress class using zlib Compress class using zlib
Andrey Bychkov work.a.b@yandex.ru Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "picompress.h" #include "picompress.h"
#ifdef PIP_COMPRESS #ifdef PIP_COMPRESS
# ifdef ESP_PLATFORM # ifdef ESP_PLATFORM
# include "esp32/rom/miniz.h" # include "esp32/rom/miniz.h"
# define compress2 mz_compress2 # define compress2 mz_compress2
# define Z_OK MZ_OK # define Z_OK MZ_OK
# define uncompress mz_uncompress # define uncompress mz_uncompress
# else # else
# include <zlib.h> # include <zlib.h>
# endif # endif
#endif #endif
PIByteArray piCompress(const PIByteArray & ba, int level) { PIByteArray piCompress(const PIByteArray & ba, int level) {
#ifdef PIP_COMPRESS #ifdef PIP_COMPRESS
PIByteArray zba; PIByteArray zba;
zba.resize(ba.size() + 128); zba.resize(ba.size() + 128);
int ret = 0; int ret = 0;
ulong sz = zba.size(); ulong sz = zba.size();
ret = compress2(zba.data(), &sz, ba.data(), ba.size(), level); ret = compress2(zba.data(), &sz, ba.data(), ba.size(), level);
if (ret != Z_OK) { if (ret != Z_OK) {
piCout << "[PICompress]" piCout << "[PICompress]" << "Error: invalid input or not enought memory";
<< "Error: invalid input or not enought memory"; return ba;
return ba; }
} zba.resize(sz);
zba.resize(sz); zba << ullong(ba.size());
zba << ullong(ba.size()); return zba;
return zba; #else
#else piCout << "[PICompress]" << "Warning: PICompress is disabled, to enable install zlib library and build pip_compress library";
piCout << "[PICompress]" #endif
<< "Warning: PICompress is disabled, to enable install zlib library and build pip_compress library"; return ba;
#endif }
return ba;
}
PIByteArray piDecompress(const PIByteArray & zba) {
#ifdef PIP_COMPRESS
PIByteArray piDecompress(const PIByteArray & zba) { ullong sz;
#ifdef PIP_COMPRESS if (zba.size() < sizeof(ullong)) {
ullong sz = 0; piCout << "[PICompress]" << "Error: invalid input";
if (zba.size() < sizeof(ullong)) { return zba;
piCout << "[PICompress]" }
<< "Error: invalid input"; PIByteArray ba(zba.data(zba.size() - sizeof(ullong)), sizeof(ullong));
return zba; ba >> sz;
} ba.resize(sz);
PIByteArray ba(zba.data(zba.size() - sizeof(ullong)), sizeof(ullong)); int ret = 0;
ba >> sz; ulong s = sz;
ba.resize(sz); ret = uncompress(ba.data(), &s, zba.data(), zba.size());
int ret = 0; if (ret != Z_OK) {
ulong s = sz; piCout << "[PICompress]" << "Error: invalid input or not enought memory";
ret = uncompress(ba.data(), &s, zba.data(), zba.size()); return zba;
if (ret != Z_OK) { }
piCout << "[PICompress]" return ba;
<< "Error: invalid input or not enought memory"; #else
return zba; piCout << "[PICompress]" << "Warning: PICompress is disabled, to enable install zlib library and build pip_compress library";
} #endif
return ba; return zba;
#else }
piCout << "[PICompress]"
<< "Warning: PICompress is disabled, to enable install zlib library and build pip_compress library";
#endif
return zba;
}

View File

@@ -1,34 +1,32 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Console output/input Console output/input
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piscreen.h" #include "piscreen.h"
#include "piincludes_p.h" #include "piincludes_p.h"
#include "piliterals_time.h"
#ifndef WINDOWS #ifndef WINDOWS
# include <fcntl.h>
# include <sys/ioctl.h> # include <sys/ioctl.h>
# include <fcntl.h>
# include <termios.h> # include <termios.h>
#else #else
# include <wincon.h>
# include <wingdi.h> # include <wingdi.h>
# include <wincon.h>
# ifndef COMMON_LVB_UNDERSCORE # ifndef COMMON_LVB_UNDERSCORE
# define COMMON_LVB_UNDERSCORE 0x8000 # define COMMON_LVB_UNDERSCORE 0x8000
# endif # endif
#endif #endif
@@ -53,26 +51,14 @@ PRIVATE_DEFINITION_END(PIScreen::SystemConsole)
PIScreen::SystemConsole::SystemConsole() { PIScreen::SystemConsole::SystemConsole() {
width = height = pwidth = pheight = 0; width = height = pwidth = pheight = 0;
mouse_x = mouse_y = -1; mouse_x = mouse_y = -1;
}
PIScreen::SystemConsole::~SystemConsole() {
#ifdef WINDOWS
SetConsoleMode(PRIVATE->hOut, PRIVATE->smode);
SetConsoleTextAttribute(PRIVATE->hOut, PRIVATE->dattr);
#endif
}
void PIScreen::SystemConsole::begin() {
int w, h; int w, h;
#ifdef WINDOWS #ifdef WINDOWS
PRIVATE->ulcoord.X = 0; PRIVATE->ulcoord.X = 0;
PRIVATE->hOut = GetStdHandle(STD_OUTPUT_HANDLE); PRIVATE->hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->sbi); GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->sbi);
PRIVATE->dattr = PRIVATE->sbi.wAttributes; PRIVATE->dattr = PRIVATE->sbi.wAttributes;
w = PRIVATE->sbi.srWindow.Right - PRIVATE->sbi.srWindow.Left; w = PRIVATE->sbi.srWindow.Right - PRIVATE->sbi.srWindow.Left;
h = PRIVATE->sbi.srWindow.Bottom - PRIVATE->sbi.srWindow.Top; h = PRIVATE->sbi.srWindow.Bottom - PRIVATE->sbi.srWindow.Top;
PRIVATE->ulcoord.Y = PRIVATE->sbi.srWindow.Top; PRIVATE->ulcoord.Y = PRIVATE->sbi.srWindow.Top;
GetConsoleMode(PRIVATE->hOut, &PRIVATE->smode); GetConsoleMode(PRIVATE->hOut, &PRIVATE->smode);
GetConsoleCursorInfo(PRIVATE->hOut, &PRIVATE->curinfo); GetConsoleCursorInfo(PRIVATE->hOut, &PRIVATE->curinfo);
@@ -88,6 +74,18 @@ void PIScreen::SystemConsole::begin() {
# endif # endif
#endif #endif
resize(w, h); resize(w, h);
}
PIScreen::SystemConsole::~SystemConsole() {
#ifdef WINDOWS
SetConsoleMode(PRIVATE->hOut, PRIVATE->smode);
SetConsoleTextAttribute(PRIVATE->hOut, PRIVATE->dattr);
#endif
}
void PIScreen::SystemConsole::begin() {
#ifdef WINDOWS #ifdef WINDOWS
SetConsoleMode(PRIVATE->hOut, ENABLE_WRAP_AT_EOL_OUTPUT); SetConsoleMode(PRIVATE->hOut, ENABLE_WRAP_AT_EOL_OUTPUT);
GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->sbi); GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->sbi);
@@ -95,7 +93,6 @@ void PIScreen::SystemConsole::begin() {
PRIVATE->bc.Y = 0; PRIVATE->bc.Y = 0;
#endif #endif
clear(); clear();
clearScreen();
hideCursor(); hideCursor();
} }
@@ -137,9 +134,9 @@ void PIScreen::SystemConsole::clear() {
void PIScreen::SystemConsole::resize(int w, int h) { void PIScreen::SystemConsole::resize(int w, int h) {
if (w == pwidth && h == pheight) return; if (w == pwidth && h == pheight) return;
width = piMaxi(w, 0); width = piMaxi(w, 0);
height = piMaxi(h, 0); height = piMaxi(h, 0);
pwidth = width; pwidth = width;
pheight = height; pheight = height;
cells.resize(height); cells.resize(height);
pcells.resize(height); pcells.resize(height);
@@ -160,7 +157,7 @@ void PIScreen::SystemConsole::resize(int w, int h) {
void PIScreen::SystemConsole::print() { void PIScreen::SystemConsole::print() {
if (mouse_x >= 0 && mouse_x < width && mouse_y >= 0 && mouse_y < height) { if (mouse_x >= 0 && mouse_x < width && mouse_y >= 0 && mouse_y < height) {
/// cells[mouse_y][mouse_x].format.flags ^= Inverse; ///cells[mouse_y][mouse_x].format.flags ^= Inverse;
} }
#ifdef WINDOWS #ifdef WINDOWS
PRIVATE->srect = PRIVATE->sbi.srWindow; PRIVATE->srect = PRIVATE->sbi.srWindow;
@@ -188,10 +185,10 @@ void PIScreen::SystemConsole::print() {
int k = j * dw + i; int k = j * dw + i;
Cell & c(cells[j + dy0][i + dx0]); Cell & c(cells[j + dy0][i + dx0]);
PRIVATE->chars[k].Char.UnicodeChar = 0; PRIVATE->chars[k].Char.UnicodeChar = 0;
PRIVATE->chars[k].Char.AsciiChar = c.symbol.toConsole1Byte(); PRIVATE->chars[k].Char.AsciiChar = c.symbol.toConsole1Byte();
PRIVATE->chars[k].Attributes = attributes(c); PRIVATE->chars[k].Attributes = attributes(c);
} }
// piCout << "draw" << dw << dh; //piCout << "draw" << dw << dh;
PRIVATE->bs.X = dw; PRIVATE->bs.X = dw;
PRIVATE->bs.Y = dh; PRIVATE->bs.Y = dh;
PRIVATE->srect.Left += dx0; PRIVATE->srect.Left += dx0;
@@ -230,7 +227,7 @@ void PIScreen::SystemConsole::print() {
if (!s.isEmpty()) { if (!s.isEmpty()) {
moveTo(si, sj); moveTo(si, sj);
PICout::stdoutPIString(s); PICout::stdoutPIString(s);
// printf("%s", s.data()); //printf("%s", s.data());
s.clear(); s.clear();
} }
} }
@@ -242,37 +239,33 @@ void PIScreen::SystemConsole::print() {
#ifdef WINDOWS #ifdef WINDOWS
# define FOREGROUND_MASK (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) #define FOREGROUND_MASK (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
# define BACKGROUND_MASK (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE) #define BACKGROUND_MASK (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
ushort PIScreen::SystemConsole::attributes(const PIScreenTypes::Cell & c) { ushort PIScreen::SystemConsole::attributes(const PIScreenTypes::Cell & c) {
WORD attr = PRIVATE->dattr; WORD attr = PRIVATE->dattr;
if (c.format.flags & Bold) if (c.format.flags & Bold) attr |= FOREGROUND_INTENSITY;
attr |= FOREGROUND_INTENSITY; else attr &= ~FOREGROUND_INTENSITY;
else if (c.format.flags & Underline) attr |= COMMON_LVB_UNDERSCORE;
attr &= ~FOREGROUND_INTENSITY; else attr &= ~COMMON_LVB_UNDERSCORE;
if (c.format.flags & Underline)
attr |= COMMON_LVB_UNDERSCORE;
else
attr &= ~COMMON_LVB_UNDERSCORE;
switch (c.format.color_char) { switch (c.format.color_char) {
case Black: attr = (attr & ~FOREGROUND_MASK); break; case Black: attr = (attr & ~FOREGROUND_MASK); break;
case Red: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_RED; break; case Red: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_RED; break;
case Green: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_GREEN; break; case Green: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_GREEN; break;
case Blue: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_BLUE; break; case Blue: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_BLUE; break;
case Cyan: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_GREEN | FOREGROUND_BLUE; break; case Cyan: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
case Magenta: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_RED | FOREGROUND_BLUE; break; case Magenta: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_RED | FOREGROUND_BLUE; break;
case Yellow: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_RED | FOREGROUND_GREEN; break; case Yellow: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_RED | FOREGROUND_GREEN; break;
case White: attr = attr | FOREGROUND_MASK; break; case White: attr = attr | FOREGROUND_MASK; break;
} }
switch (c.format.color_back) { switch (c.format.color_back) {
case Black: attr = (attr & ~BACKGROUND_MASK); break; case Black: attr = (attr & ~BACKGROUND_MASK); break;
case Red: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_RED; break; case Red: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_RED; break;
case Green: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_GREEN; break; case Green: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_GREEN; break;
case Blue: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_BLUE; break; case Blue: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_BLUE; break;
case Cyan: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_GREEN | BACKGROUND_BLUE; break; case Cyan: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_GREEN | BACKGROUND_BLUE; break;
case Magenta: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_RED | BACKGROUND_BLUE; break; case Magenta: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_RED | BACKGROUND_BLUE; break;
case Yellow: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_RED | BACKGROUND_GREEN; break; case Yellow: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_RED | BACKGROUND_GREEN; break;
case White: attr = attr | BACKGROUND_MASK; break; case White: attr = attr | BACKGROUND_MASK; break;
} }
if ((c.format.flags & Inverse) == Inverse) { if ((c.format.flags & Inverse) == Inverse) {
uchar f = attr & 0xFF; uchar f = attr & 0xFF;
@@ -282,8 +275,8 @@ ushort PIScreen::SystemConsole::attributes(const PIScreenTypes::Cell & c) {
} }
return attr; return attr;
} }
# undef FOREGROUND_MASK #undef FOREGROUND_MASK
# undef BACKGROUND_MASK #undef BACKGROUND_MASK
void PIScreen::SystemConsole::getWinCurCoord() { void PIScreen::SystemConsole::getWinCurCoord() {
GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->csbi); GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->csbi);
@@ -298,37 +291,36 @@ void PIScreen::SystemConsole::clearLine() {
void PIScreen::SystemConsole::newLine() { void PIScreen::SystemConsole::newLine() {
getWinCurCoord(); getWinCurCoord();
PRIVATE->ccoord.X = 0; PRIVATE->ccoord.X = 0; PRIVATE->ccoord.Y++;
PRIVATE->ccoord.Y++;
SetConsoleCursorPosition(PRIVATE->hOut, PRIVATE->ccoord); SetConsoleCursorPosition(PRIVATE->hOut, PRIVATE->ccoord);
} }
#else // WINDOWS #else // WINDOWS
PIString PIScreen::SystemConsole::formatString(const PIScreenTypes::Cell & c) { PIString PIScreen::SystemConsole::formatString(const PIScreenTypes::Cell & c) {
PIString ts = PIStringAscii("\e[0"); PIString ts = PIStringAscii("\e[0");
switch (c.format.color_char) { switch (c.format.color_char) {
case Black: ts += PIStringAscii(";30"); break; case Black: ts += PIStringAscii(";30"); break;
case Red: ts += PIStringAscii(";31"); break; case Red: ts += PIStringAscii(";31"); break;
case Green: ts += PIStringAscii(";32"); break; case Green: ts += PIStringAscii(";32"); break;
case Blue: ts += PIStringAscii(";34"); break; case Blue: ts += PIStringAscii(";34"); break;
case Cyan: ts += PIStringAscii(";36"); break; case Cyan: ts += PIStringAscii(";36"); break;
case Magenta: ts += PIStringAscii(";35"); break; case Magenta: ts += PIStringAscii(";35"); break;
case Yellow: ts += PIStringAscii(";33"); break; case Yellow: ts += PIStringAscii(";33"); break;
case White: ts += PIStringAscii(";37"); break; case White: ts += PIStringAscii(";37"); break;
} }
switch (c.format.color_back) { switch (c.format.color_back) {
case Black: ts += PIStringAscii(";40"); break; case Black: ts += PIStringAscii(";40"); break;
case Red: ts += PIStringAscii(";41"); break; case Red: ts += PIStringAscii(";41"); break;
case Green: ts += PIStringAscii(";42"); break; case Green: ts += PIStringAscii(";42"); break;
case Blue: ts += PIStringAscii(";44"); break; case Blue: ts += PIStringAscii(";44"); break;
case Cyan: ts += PIStringAscii(";46"); break; case Cyan: ts += PIStringAscii(";46"); break;
case Magenta: ts += PIStringAscii(";45"); break; case Magenta: ts += PIStringAscii(";45"); break;
case Yellow: ts += PIStringAscii(";43"); break; case Yellow: ts += PIStringAscii(";43"); break;
case White: ts += PIStringAscii(";47"); break; case White: ts += PIStringAscii(";47"); break;
} }
if ((c.format.flags & Bold) == Bold) ts += PIStringAscii(";1"); if ((c.format.flags & Bold ) == Bold ) ts += PIStringAscii(";1");
if ((c.format.flags & Underline) == Underline) ts += PIStringAscii(";4"); if ((c.format.flags & Underline) == Underline) ts += PIStringAscii(";4");
if ((c.format.flags & Blink) == Blink) ts += PIStringAscii(";5"); if ((c.format.flags & Blink ) == Blink ) ts += PIStringAscii(";5");
if ((c.format.flags & Inverse) == Inverse) ts += PIStringAscii(";7"); if ((c.format.flags & Inverse ) == Inverse ) ts += PIStringAscii(";7");
return ts + 'm'; return ts + 'm';
} }
#endif // WINDOWS #endif // WINDOWS
@@ -365,16 +357,8 @@ void PIScreen::SystemConsole::clearScreen() {
void PIScreen::SystemConsole::clearScreenLower() { void PIScreen::SystemConsole::clearScreenLower() {
#ifdef WINDOWS #ifdef WINDOWS
getWinCurCoord(); getWinCurCoord();
FillConsoleOutputAttribute(PRIVATE->hOut, FillConsoleOutputAttribute(PRIVATE->hOut, PRIVATE->dattr, width * height - width * PRIVATE->ccoord.Y + PRIVATE->ccoord.X, PRIVATE->ccoord, &PRIVATE->written);
PRIVATE->dattr, FillConsoleOutputCharacter(PRIVATE->hOut, ' ', width * height - width * PRIVATE->ccoord.Y + PRIVATE->ccoord.X, PRIVATE->ccoord, &PRIVATE->written);
width * height - width * PRIVATE->ccoord.Y + PRIVATE->ccoord.X,
PRIVATE->ccoord,
&PRIVATE->written);
FillConsoleOutputCharacter(PRIVATE->hOut,
' ',
width * height - width * PRIVATE->ccoord.Y + PRIVATE->ccoord.X,
PRIVATE->ccoord,
&PRIVATE->written);
#else #else
printf("\e[0m\e[J"); printf("\e[0m\e[J");
#endif #endif
@@ -399,33 +383,34 @@ void PIScreen::SystemConsole::showCursor() {
} }
// PIScreen
PIScreen::PIScreen(bool startNow, PIKbdListener::KBFunc slot): PIThread(), drawer_(console.cells), root("rootTile") { PIScreen::PIScreen(bool startNow, PIKbdListener::KBFunc slot): PIThread(), drawer_(console.cells), root("rootTile") {
setName("screen"); setName("screen");
setPriority(piLow); setPriority(piLow);
needLockRun(true); needLockRun(true);
mouse_ = false; mouse_ = false;
ret_func = slot; ret_func = slot;
tile_focus = tile_dialog = nullptr; tile_focus = tile_dialog = 0;
root.screen = this; root.screen = this;
listener = new PIKbdListener(key_eventS, this, startNow); listener = new PIKbdListener(key_eventS, this, startNow);
CONNECT1(void, PIKbdListener::MouseEvent, listener, mouseEvent, this, mouse_event); CONNECTU(listener, mouseEvent, this, mouse_event);
CONNECT1(void, PIKbdListener::WheelEvent, listener, wheelEvent, this, wheel_event); CONNECTU(listener, wheelEvent, this, wheel_event);
if (startNow) start(); if (startNow) start();
} }
PIScreen::~PIScreen() { PIScreen::~PIScreen() {
if (isRunning()) stop(); if (isRunning())
PIThread::waitForFinish(100_ms); stop();
listener->waitForFinish(100_ms); PIThread::waitForFinish(10);
listener->waitForFinish(10);
delete listener; delete listener;
} }
void PIScreen::setMouseEnabled(bool on) { void PIScreen::setMouseEnabled(bool on) {
mouse_ = on; mouse_ = on;
console.mouse_x = console.mouse_y = -1; console.mouse_x = console.mouse_y = -1;
} }
@@ -437,12 +422,14 @@ void PIScreen::key_event(PIKbdListener::KeyEvent key) {
return; return;
*/ */
PIScreenTile * rtile = rootTile(); PIScreenTile * rtile = rootTile();
if (tile_dialog) rtile = tile_dialog; if (tile_dialog)
rtile = tile_dialog;
bool used = nextFocus(rtile, key); bool used = nextFocus(rtile, key);
if (used) return; if (used) return;
if (!used && tile_focus) { if (!used && tile_focus) {
if (tile_focus->visible) { if (tile_focus->visible) {
if (tile_focus->keyEvent(key)) return; if (tile_focus->keyEvent(key))
return;
} }
} }
if (ret_func != 0) ret_func(key, data_); if (ret_func != 0) ret_func(key, data_);
@@ -450,14 +437,14 @@ void PIScreen::key_event(PIKbdListener::KeyEvent key) {
} }
PIVector<PIScreenTile *> PIScreen::prepareMouse(PIKbdListener::MouseEvent * e) { PIVector<PIScreenTile * > PIScreen::prepareMouse(PIKbdListener::MouseEvent * e) {
PIVector<PIScreenTile *> ret; PIVector<PIScreenTile * > ret;
if (!mouse_ || !e) return ret; if (!mouse_ || !e) return ret;
console.mouse_x = e->x; console.mouse_x = e->x;
console.mouse_y = e->y; console.mouse_y = e->y;
PIVector<PIScreenTile *> tl = tilesUnderMouse(e->x, e->y); PIVector<PIScreenTile * > tl = tilesUnderMouse(e->x, e->y);
bool ff = false; bool ff = false;
piForeachR(PIScreenTile * t, tl) { piForeachR (PIScreenTile * t, tl) {
if (!ff) { if (!ff) {
if (t->focus_flags[FocusOnMouse] && (e->action == PIKbdListener::MouseButtonPress)) { if (t->focus_flags[FocusOnMouse] && (e->action == PIKbdListener::MouseButtonPress)) {
t->setFocus(); t->setFocus();
@@ -468,19 +455,20 @@ PIVector<PIScreenTile *> PIScreen::prepareMouse(PIKbdListener::MouseEvent * e) {
ff = true; ff = true;
} }
} }
} }
return tl; return tl;
} }
PIVector<PIScreenTile *> PIScreen::tilesUnderMouse(int x, int y) { PIVector<PIScreenTile * > PIScreen::tilesUnderMouse(int x, int y) {
PIVector<PIScreenTile *> ret; PIVector<PIScreenTile * > ret;
if (x < 0 || x >= console.width || y < 0 || y >= console.height) return ret; if (x < 0 || x >= console.width || y < 0 || y >= console.height) return ret;
PIScreenTile * ct = tile_dialog ? tile_dialog : rootTile(); PIScreenTile * ct = tile_dialog ? tile_dialog : rootTile();
bool f = true; bool f = true;
while (ct) { while (ct) {
if (!f) ret << ct; if (!f) ret << ct;
f = false; f = false;
ct = ct->childUnderMouse(x, y); ct = ct->childUnderMouse(x, y);
} }
return ret; return ret;
@@ -488,25 +476,27 @@ PIVector<PIScreenTile *> PIScreen::tilesUnderMouse(int x, int y) {
void PIScreen::mouse_event(PIKbdListener::MouseEvent me) { void PIScreen::mouse_event(PIKbdListener::MouseEvent me) {
PIVector<PIScreenTile *> tl = prepareMouse(&me); PIVector<PIScreenTile * > tl = prepareMouse(&me);
if (tl.isEmpty()) return; if (tl.isEmpty()) return;
piForeachR(PIScreenTile * t, tl) piForeachR (PIScreenTile * t, tl)
if (t->mouseEvent(me)) break; if (t->mouseEvent(me)) break;
} }
void PIScreen::wheel_event(PIKbdListener::WheelEvent we) { void PIScreen::wheel_event(PIKbdListener::WheelEvent we) {
PIVector<PIScreenTile *> tl = prepareMouse(&we); PIVector<PIScreenTile * > tl = prepareMouse(&we);
if (tl.isEmpty()) return; if (tl.isEmpty()) return;
piForeachR(PIScreenTile * t, tl) piForeachR (PIScreenTile * t, tl)
if (t->wheelEvent(we)) break; if (t->wheelEvent(we)) break;
} }
bool PIScreen::nextFocus(PIScreenTile * rt, PIKbdListener::KeyEvent key) { bool PIScreen::nextFocus(PIScreenTile * rt, PIKbdListener::KeyEvent key) {
PIVector<PIScreenTile *> vtl = rt->children(true), ftl; PIVector<PIScreenTile*> vtl = rt->children(true), ftl;
piForeach(PIScreenTile * t, vtl) { piForeach (PIScreenTile * t, vtl) {
if (t->focus_flags[CanHasFocus]) ftl << t; if (t->focus_flags[CanHasFocus])
ftl << t;
} }
int ind = -1; int ind = -1;
for (int i = 0; i < ftl.size_s(); ++i) for (int i = 0; i < ftl.size_s(); ++i)
@@ -514,15 +504,18 @@ bool PIScreen::nextFocus(PIScreenTile * rt, PIKbdListener::KeyEvent key) {
ind = i; ind = i;
break; break;
} }
if (ind < 0) tile_focus = 0; if (ind < 0)
tile_focus = 0;
if (ftl.isEmpty()) if (ftl.isEmpty())
tile_focus = 0; tile_focus = 0;
else { else {
if (tile_focus) if (tile_focus)
if (!tile_focus->visible) tile_focus = 0; if (!tile_focus->visible)
tile_focus = 0;
int next = tile_focus ? 0 : 1; int next = tile_focus ? 0 : 1;
if (tile_focus) { if (tile_focus) {
if (tile_focus->focus_flags[NextByTab] && key.key == PIKbdListener::Tab) next = 1; if (tile_focus->focus_flags[NextByTab] && key.key == PIKbdListener::Tab)
next = 1;
if (tile_focus->focus_flags[NextByArrowsHorizontal]) { if (tile_focus->focus_flags[NextByArrowsHorizontal]) {
if (key.key == PIKbdListener::LeftArrow) next = -1; if (key.key == PIKbdListener::LeftArrow) next = -1;
if (key.key == PIKbdListener::RightArrow) next = 1; if (key.key == PIKbdListener::RightArrow) next = 1;
@@ -532,16 +525,16 @@ bool PIScreen::nextFocus(PIScreenTile * rt, PIKbdListener::KeyEvent key) {
if (key.key == PIKbdListener::DownArrow) next = 1; if (key.key == PIKbdListener::DownArrow) next = 1;
} }
} }
// piCout << ftl.size() << ind << next; //piCout << ftl.size() << ind << next;
if (next != 0) { if (next != 0) {
PIVector<PIScreenTile *> tl = rt->children(); PIVector<PIScreenTile*> tl = rt->children();
piForeach(PIScreenTile * t, tl) piForeach (PIScreenTile * t, tl)
t->has_focus = false; t->has_focus = false;
if (!ftl.isEmpty()) { if (!ftl.isEmpty()) {
ind += next; ind += next;
if (ind >= ftl.size_s()) ind = 0; if (ind >= ftl.size_s()) ind = 0;
if (ind < 0) ind = ftl.size_s() - 1; if (ind < 0) ind = ftl.size_s() - 1;
tile_focus = ftl[ind]; tile_focus = ftl[ind];
tile_focus->has_focus = true; tile_focus->has_focus = true;
} }
return true; return true;
@@ -557,19 +550,22 @@ void PIScreen::tileEventInternal(PIScreenTile * t, TileEvent e) {
void PIScreen::tileRemovedInternal(PIScreenTile * t) { void PIScreen::tileRemovedInternal(PIScreenTile * t) {
if (tile_dialog == t) tile_dialog = 0; if (tile_dialog == t)
tile_dialog = 0;
} }
void PIScreen::tileSetFocusInternal(PIScreenTile * t) { void PIScreen::tileSetFocusInternal(PIScreenTile * t) {
PIScreenTile * rt = rootTile(); PIScreenTile * rt = rootTile();
if (tile_dialog) rt = tile_dialog; if (tile_dialog)
PIVector<PIScreenTile *> tl = rt->children(), ftl; rt = tile_dialog;
piForeach(PIScreenTile * i, tl) PIVector<PIScreenTile*> tl = rt->children(), ftl;
piForeach (PIScreenTile * i, tl)
i->has_focus = false; i->has_focus = false;
tile_focus = t; tile_focus = t;
if (!tile_focus) return; if (!tile_focus) return;
if (tile_focus->focus_flags[CanHasFocus]) tile_focus->has_focus = true; if (tile_focus->focus_flags[CanHasFocus])
tile_focus->has_focus = true;
} }
@@ -591,14 +587,8 @@ void PIScreen::waitForFinish() {
} }
void PIScreen::start(bool wait) {
PIThread::start(25_Hz);
if (wait) waitForFinish();
}
void PIScreen::stop(bool clear) { void PIScreen::stop(bool clear) {
PIThread::stopAndWait(); PIThread::stop(true);
if (clear) console.clearScreen(); if (clear) console.clearScreen();
#ifndef WINDOWS #ifndef WINDOWS
fflush(0); fflush(0);
@@ -622,21 +612,16 @@ void PIScreen::run() {
if (tile_dialog) { if (tile_dialog) {
int sw(0), sh(0); int sw(0), sh(0);
tile_dialog->sizeHint(sw, sh); tile_dialog->sizeHint(sw, sh);
sw = piClampi(sw, tile_dialog->minimumWidth, tile_dialog->maximumWidth); sw = piClampi(sw, tile_dialog->minimumWidth, tile_dialog->maximumWidth);
sh = piClampi(sh, tile_dialog->minimumHeight, tile_dialog->maximumHeight); sh = piClampi(sh, tile_dialog->minimumHeight, tile_dialog->maximumHeight);
tile_dialog->x_ = (console.width - sw) / 2; tile_dialog->x_ = (console.width - sw) / 2;
tile_dialog->y_ = (console.height - sh) / 2; tile_dialog->y_ = (console.height - sh) / 2;
tile_dialog->width_ = sw; tile_dialog->width_ = sw;
tile_dialog->height_ = sh; tile_dialog->height_ = sh;
tile_dialog->layout(); tile_dialog->layout();
int dx = tile_dialog->x_ - 1, dy = tile_dialog->y_ - 1, dw = tile_dialog->width_, dh = tile_dialog->height_; int dx = tile_dialog->x_ - 1, dy = tile_dialog->y_ - 1, dw = tile_dialog->width_, dh = tile_dialog->height_;
drawer_.drawFrame(dx, drawer_.drawFrame(dx, dy, dx + dw + 1, dy + dh + 1, (Color)tile_dialog->back_format.color_char,
dy, (Color)tile_dialog->back_format.color_back, (CharFlags)tile_dialog->back_format.flags);
dx + dw + 1,
dy + dh + 1,
(Color)tile_dialog->back_format.color_char,
(Color)tile_dialog->back_format.color_back,
(CharFlags)tile_dialog->back_format.flags);
tile_dialog->drawEventInternal(&drawer_); tile_dialog->drawEventInternal(&drawer_);
} }
console.print(); console.print();
@@ -650,8 +635,10 @@ void PIScreen::end() {
PIScreenTile * PIScreen::tileByName(const PIString & name) { PIScreenTile * PIScreen::tileByName(const PIString & name) {
PIVector<PIScreenTile *> tl(tiles()); PIVector<PIScreenTile*> tl(tiles());
piForeach(PIScreenTile * t, tl) piForeach (PIScreenTile * t, tl)
if (t->name() == name) return t; if (t->name() == name)
return t;
return 0; return 0;
} }

View File

@@ -1,20 +1,20 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Tile for PIScreen with PIConsole API Tile for PIScreen with PIConsole API
Andrey Bychkov work.a.b@yandex.ru Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piscreenconsole.h" #include "piscreenconsole.h"
@@ -22,14 +22,21 @@
using namespace PIScreenTypes; using namespace PIScreenTypes;
TileVars::TileVars(const PIString & n): PIScreenTile(n) { TileVars::TileVars(const PIString &n) : PIScreenTile(n) {
alignment = Left; alignment = Left;
} }
void TileVars::sizeHint(int & w, int & h) const {} void TileVars::sizeHint(int &w, int &h) const {
void TileVars::drawEvent(PIScreenDrawer * d) {} }
void TileVars::drawEvent(PIScreenDrawer *d) {
}
PIScreenConsoleTile::PIScreenConsoleTile() {} PIScreenConsoleTile::PIScreenConsoleTile() {
}

View File

@@ -1,20 +1,20 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Console output/input Console output/input
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piscreendrawer.h" #include "piscreendrawer.h"
@@ -25,68 +25,68 @@
using namespace PIScreenTypes; using namespace PIScreenTypes;
PIScreenDrawer::PIScreenDrawer(PIVector<PIVector<Cell>> & c): cells(c) { PIScreenDrawer::PIScreenDrawer(PIVector<PIVector<Cell> > & c): cells(c) {
arts_[LineVertical] = arts_[LineVertical] =
#ifdef USE_UNICODE #ifdef USE_UNICODE
PIChar::fromUTF8(""); PIChar::fromUTF8("");
#else #else
PIChar('|'); PIChar('|');
#endif #endif
arts_[LineHorizontal] = arts_[LineHorizontal] =
#ifdef USE_UNICODE #ifdef USE_UNICODE
PIChar::fromUTF8(""); PIChar::fromUTF8("");
#else #else
PIChar('-'); PIChar('-');
#endif #endif
arts_[Cross] = arts_[Cross] =
#ifdef USE_UNICODE #ifdef USE_UNICODE
PIChar::fromUTF8(""); PIChar::fromUTF8("");
#else #else
PIChar('+'); PIChar('+');
#endif #endif
arts_[CornerTopLeft] = arts_[CornerTopLeft] =
#ifdef USE_UNICODE #ifdef USE_UNICODE
PIChar::fromUTF8(""); PIChar::fromUTF8("");
#else #else
PIChar('+'); PIChar('+');
#endif #endif
arts_[CornerTopRight] = arts_[CornerTopRight] =
#ifdef USE_UNICODE #ifdef USE_UNICODE
PIChar::fromUTF8(""); PIChar::fromUTF8("");
#else #else
PIChar('+'); PIChar('+');
#endif #endif
arts_[CornerBottomLeft] = arts_[CornerBottomLeft] =
#ifdef USE_UNICODE #ifdef USE_UNICODE
PIChar::fromUTF8(""); PIChar::fromUTF8("");
#else #else
PIChar('+'); PIChar('+');
#endif #endif
arts_[CornerBottomRight] = arts_[CornerBottomRight] =
#ifdef USE_UNICODE #ifdef USE_UNICODE
PIChar::fromUTF8(""); PIChar::fromUTF8("");
#else #else
PIChar('+'); PIChar('+');
#endif #endif
arts_[Unchecked] = arts_[Unchecked] =
#ifdef USE_UNICODE #ifdef USE_UNICODE
PIChar::fromUTF8(""); PIChar::fromUTF8("");
#else #else
PIChar('O'); PIChar('O');
#endif #endif
arts_[Checked] = arts_[Checked] =
#ifdef USE_UNICODE #ifdef USE_UNICODE
PIChar::fromUTF8(""); PIChar::fromUTF8("");
#else #else
PIChar('0'); PIChar('0');
#endif #endif
} }
@@ -98,43 +98,45 @@ void PIScreenDrawer::clear() {
void PIScreenDrawer::drawPixel(int x, int y, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) { void PIScreenDrawer::drawPixel(int x, int y, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) {
if (x < 0 || x >= width || y < 0 || y >= height) return; if (x < 0 || x >= width || y < 0 || y >= height) return;
cells[y][x].symbol = c; cells[y][x].symbol = c;
cells[y][x].format.color_char = col_char; cells[y][x].format.color_char = col_char;
cells[y][x].format.color_back = col_back; cells[y][x].format.color_back = col_back;
cells[y][x].format.flags = flags_char; cells[y][x].format.flags = flags_char;
} }
void PIScreenDrawer::drawLine(int x0, int y0, int x1, int y1, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) { void PIScreenDrawer::drawLine(int x0, int y0, int x1, int y1, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) {
if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char); if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char);
Cell cc; Cell cc;
cc.symbol = c; cc.symbol = c;
cc.format.color_char = col_char; cc.format.color_char = col_char;
cc.format.color_back = col_back; cc.format.color_back = col_back;
cc.format.flags = flags_char; cc.format.flags = flags_char;
int x = 0, y = 0; int x = 0, y = 0;
if (piAbsi(x1 - x0) >= piAbsi(y1 - y0)) { if (piAbsi(x1 - x0) >= piAbsi(y1 - y0)) {
float dy = (y1 - y0) / float(piAbsi(x1 - x0)), cy = y0; float dy = (y1 - y0) / float(piAbsi(x1 - x0)), cy = y0;
int dx = x0 < x1 ? 1 : -1; int dx = x0 < x1 ? 1 : -1;
for (int i = x0; i != x1; i += dx) { for (int i = x0; i != x1; i += dx) {
x = i; x = i; y = piRound(cy);
y = piRound(cy); if (x >= 0 && x < width && y >= 0 && y < height)
if (x >= 0 && x < width && y >= 0 && y < height) cells[y][x] = cc; cells[y][x] = cc;
cy += dy; cy += dy;
} }
y = piRound(cy); y = piRound(cy);
if (x1 >= 0 && x1 < width && y >= 0 && y < height) cells[y][x1] = cc; if (x1 >= 0 && x1 < width && y >= 0 && y < height)
cells[y][x1] = cc;
} else { } else {
float dx = (x1 - x0) / float(piAbsi(y1 - y0)), cx = x0; float dx = (x1 - x0) / float(piAbsi(y1 - y0)), cx = x0;
int dy = y0 < y1 ? 1 : -1; int dy = y0 < y1 ? 1 : -1;
for (int i = y0; i != y1; i += dy) { for (int i = y0; i != y1; i += dy) {
x = piRound(cx); x = piRound(cx); y = i;
y = i; if (x >= 0 && x < width && y >= 0 && y < height)
if (x >= 0 && x < width && y >= 0 && y < height) cells[y][x] = cc; cells[y][x] = cc;
cx += dx; cx += dx;
} }
x = piRound(cx); x = piRound(cx);
if (x >= 0 && x < width && y1 >= 0 && y1 < height) cells[y1][x] = cc; if (x >= 0 && x < width && y1 >= 0 && y1 < height)
cells[y1][x] = cc;
} }
} }
@@ -142,29 +144,32 @@ void PIScreenDrawer::drawLine(int x0, int y0, int x1, int y1, const PIChar & c,
void PIScreenDrawer::drawRect(int x0, int y0, int x1, int y1, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) { void PIScreenDrawer::drawRect(int x0, int y0, int x1, int y1, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) {
if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char); if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char);
Cell cc; Cell cc;
cc.symbol = c; cc.symbol = c;
cc.format.color_char = col_char; cc.format.color_char = col_char;
cc.format.color_back = col_back; cc.format.color_back = col_back;
cc.format.flags = flags_char; cc.format.flags = flags_char;
int dx = x0 < x1 ? 1 : -1; int dx = x0 < x1 ? 1 : -1;
int dy = y0 < y1 ? 1 : -1; int dy = y0 < y1 ? 1 : -1;
int xs[2] = {x0, x1}; int xs[2] = {x0, x1};
int ys[2] = {y0, y1}; int ys[2] = {y0, y1};
for (int k = 0; k < 2; ++k) { for (int k = 0; k < 2; ++k) {
int j = ys[k]; int j = ys[k];
if (j >= 0 && j < height) { if (j >= 0 && j < height) {
PIVector<Cell> & cv(cells[j]); PIVector<Cell> & cv(cells[j]);
for (int i = x0; i != x1; i += dx) for (int i = x0; i != x1; i += dx)
if (i >= 0 && i < width) cv[i] = cc; if (i >= 0 && i < width)
cv[i] = cc;
} }
j = xs[k]; j = xs[k];
if (j >= 0 && j < width) { if (j >= 0 && j < width) {
for (int i = y0; i != y1; i += dy) for (int i = y0; i != y1; i += dy)
if (i >= 0 && i < height) cells[i][j] = cc; if (i >= 0 && i < height)
cells[i][j] = cc;
} }
} }
int i = x1, j = y1; int i = x1, j = y1;
if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc; if (i >= 0 && i < width && j >= 0 && j < height)
cells[j][i] = cc;
} }
@@ -173,37 +178,35 @@ void PIScreenDrawer::drawFrame(int x0, int y0, int x1, int y1, Color col_char, C
Cell cc; Cell cc;
cc.format.color_char = col_char; cc.format.color_char = col_char;
cc.format.color_back = col_back; cc.format.color_back = col_back;
cc.format.flags = flags_char; cc.format.flags = flags_char;
int dx = x0 < x1 ? 1 : -1; int dx = x0 < x1 ? 1 : -1;
int dy = y0 < y1 ? 1 : -1; int dy = y0 < y1 ? 1 : -1;
int xs[2] = {x0, x1}; int xs[2] = {x0, x1};
int ys[2] = {y0, y1}; int ys[2] = {y0, y1};
for (int k = 0; k < 2; ++k) { for (int k = 0; k < 2; ++k) {
int j = ys[k]; int j = ys[k];
if (j >= 0 && j < height) { if (j >= 0 && j < height) {
PIVector<Cell> & cv(cells[j]); PIVector<Cell> & cv(cells[j]);
cc.symbol = artChar(LineHorizontal); cc.symbol = artChar(LineHorizontal);
for (int i = x0 + 1; i != x1; i += dx) for (int i = x0 + 1; i != x1; i += dx)
if (i >= 0 && i < width) cv[i] = cc; if (i >= 0 && i < width)
cv[i] = cc;
} }
j = xs[k]; j = xs[k];
if (j >= 0 && j < width) { if (j >= 0 && j < width) {
cc.symbol = artChar(LineVertical); cc.symbol = artChar(LineVertical);
for (int i = y0 + 1; i != y1; i += dy) for (int i = y0 + 1; i != y1; i += dy)
if (i >= 0 && i < height) cells[i][j] = cc; if (i >= 0 && i < height)
cells[i][j] = cc;
} }
} }
int i = x0, j = y0; int i = x0, j = y0; cc.symbol = artChar(CornerTopLeft);
cc.symbol = artChar(CornerTopLeft);
if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc; if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc;
i = x1, j = y0; i = x1, j = y0; cc.symbol = artChar(CornerTopRight);
cc.symbol = artChar(CornerTopRight);
if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc; if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc;
i = x0, j = y1; i = x0, j = y1; cc.symbol = artChar(CornerBottomLeft);
cc.symbol = artChar(CornerBottomLeft);
if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc; if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc;
i = x1, j = y1; i = x1, j = y1; cc.symbol = artChar(CornerBottomRight);
cc.symbol = artChar(CornerBottomRight);
if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc; if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc;
} }
@@ -211,22 +214,23 @@ void PIScreenDrawer::drawFrame(int x0, int y0, int x1, int y1, Color col_char, C
void PIScreenDrawer::fillRect(int x0, int y0, int x1, int y1, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) { void PIScreenDrawer::fillRect(int x0, int y0, int x1, int y1, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) {
if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char); if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char);
Cell cc; Cell cc;
cc.symbol = c; cc.symbol = c;
cc.format.color_char = col_char; cc.format.color_char = col_char;
cc.format.color_back = col_back; cc.format.color_back = col_back;
cc.format.flags = flags_char; cc.format.flags = flags_char;
int dx = x0 < x1 ? 1 : -1; int dx = x0 < x1 ? 1 : -1;
int dy = y0 < y1 ? 1 : -1; int dy = y0 < y1 ? 1 : -1;
for (int j = y0; j != y1; j += dy) for (int j = y0; j != y1; j += dy)
if (j >= 0 && j < height) { if (j >= 0 && j < height) {
PIVector<Cell> & cv(cells[j]); PIVector<Cell> & cv(cells[j]);
for (int i = x0; i != x1; i += dx) for (int i = x0; i != x1; i += dx)
if (i >= 0 && i < width) cv[i] = cc; if (i >= 0 && i < width)
cv[i] = cc;
} }
} }
void PIScreenDrawer::fillRect(int x0, int y0, int x1, int y1, PIVector<PIVector<Cell>> & content) { void PIScreenDrawer::fillRect(int x0, int y0, int x1, int y1, PIVector<PIVector<Cell> > & content) {
if (x0 > x1) piSwap(x0, x1); if (x0 > x1) piSwap(x0, x1);
if (y0 > y1) piSwap(y0, y1); if (y0 > y1) piSwap(y0, y1);
int w = x1 - x0; int w = x1 - x0;
@@ -237,13 +241,14 @@ void PIScreenDrawer::fillRect(int x0, int y0, int x1, int y1, PIVector<PIVector<
PIVector<Cell> & cv(cells[y0 + j]); PIVector<Cell> & cv(cells[y0 + j]);
PIVector<Cell> & contv(content[j]); PIVector<Cell> & contv(content[j]);
for (int i = 0; i < piMini(w, contv.size_s()); ++i) for (int i = 0; i < piMini(w, contv.size_s()); ++i)
if ((i + x0) >= 0 && (i + x0) < width) cv[x0 + i] = contv[i]; if ((i + x0) >= 0 && (i + x0) < width)
cv[x0 + i] = contv[i];
} }
} }
} }
void PIScreenDrawer::clear(PIVector<PIVector<Cell>> & cells) { void PIScreenDrawer::clear(PIVector<PIVector<Cell> > & cells) {
for (int i = 0; i < cells.size_s(); ++i) for (int i = 0; i < cells.size_s(); ++i)
cells[i].fill(Cell()); cells[i].fill(Cell());
} }
@@ -255,12 +260,12 @@ void PIScreenDrawer::drawText(int x, int y, const PIString & s, Color col_char,
Cell cc; Cell cc;
cc.format.color_char = col_char; cc.format.color_char = col_char;
cc.format.color_back = col_back; cc.format.color_back = col_back;
cc.format.flags = flags_char; cc.format.flags = flags_char;
for (int i = 0; i < s.size_s(); ++i) { for (int i = 0; i < s.size_s(); ++i) {
int j = i + x; int j = i + x;
if (j >= 0 && j < width) { if (j >= 0 && j < width) {
cc.symbol = s[i]; cc.symbol = s[i];
cv[j] = cc; cv[j] = cc;
} }
} }
} }

View File

@@ -1,24 +1,23 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Basic PIScreen tile Basic PIScreen tile
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piscreentile.h" #include "piscreentile.h"
#include "piscreendrawer.h" #include "piscreendrawer.h"
@@ -26,23 +25,24 @@ using namespace PIScreenTypes;
PIScreenTile::PIScreenTile(const PIString & n, Direction d, SizePolicy p): PIObject(n) { PIScreenTile::PIScreenTile(const PIString & n, Direction d, SizePolicy p): PIObject(n) {
direction = d; direction = d;
size_policy = p; size_policy = p;
focus_flags = 0; focus_flags = 0;
screen = 0; screen = 0;
minimumWidth = minimumHeight = x_ = y_ = width_ = height_ = pw = ph = 0; minimumWidth = minimumHeight = x_ = y_ = width_ = height_ = pw = ph = 0;
maximumWidth = maximumHeight = 65535; maximumWidth = maximumHeight = 65535;
marginLeft = marginRight = marginTop = marginBottom = spacing = 0; marginLeft = marginRight = marginTop = marginBottom = spacing = 0;
parent = 0; parent = 0;
back_symbol = ' '; back_symbol = ' ';
visible = true; visible = true;
has_focus = false; has_focus = false;
} }
PIScreenTile::~PIScreenTile() { PIScreenTile::~PIScreenTile() {
// piCout << this << "~"; //piCout << this << "~";
if (screen) screen->tileRemovedInternal(this); if (screen)
screen->tileRemovedInternal(this);
setScreen(0); setScreen(0);
deleteChildren(); deleteChildren();
if (!parent) return; if (!parent) return;
@@ -76,18 +76,20 @@ void PIScreenTile::removeTile(PIScreenTile * t) {
} }
PIVector<PIScreenTile *> PIScreenTile::children(bool only_visible) { PIVector<PIScreenTile * > PIScreenTile::children(bool only_visible) {
PIVector<PIScreenTile *> ret; PIVector<PIScreenTile * > ret;
piForeach(PIScreenTile * t, tiles) piForeach (PIScreenTile * t, tiles)
if (t->visible || !only_visible) ret << t << t->children(only_visible); if (t->visible || !only_visible)
ret << t << t->children(only_visible);
return ret; return ret;
} }
PIScreenTile * PIScreenTile::childUnderMouse(int x, int y) { PIScreenTile * PIScreenTile::childUnderMouse(int x, int y) {
piForeach(PIScreenTile * t, tiles) { piForeach (PIScreenTile * t, tiles) {
if (!t->visible) continue; if (!t->visible) continue;
if (x >= t->x_ && (x - t->x_) < t->width_ && y >= t->y_ && (y - t->y_) < t->height_) { if (x >= t->x_ && (x - t->x_) < t->width_ &&
y >= t->y_ && (y - t->y_) < t->height_) {
return t; return t;
} }
} }
@@ -103,13 +105,13 @@ void PIScreenTile::raiseEvent(TileEvent e) {
void PIScreenTile::setScreen(PIScreenBase * s) { void PIScreenTile::setScreen(PIScreenBase * s) {
screen = s; screen = s;
piForeach(PIScreenTile * t, tiles) piForeach (PIScreenTile * t, tiles)
t->setScreen(s); t->setScreen(s);
} }
void PIScreenTile::deleteChildren() { void PIScreenTile::deleteChildren() {
piForeach(PIScreenTile * t, tiles) { piForeach (PIScreenTile * t, tiles) {
t->parent = 0; t->parent = 0;
delete t; delete t;
} }
@@ -127,16 +129,9 @@ void PIScreenTile::drawEventInternal(PIScreenDrawer * d) {
if (!visible) { if (!visible) {
return; return;
} }
d->fillRect(x_, d->fillRect(x_, y_, x_ + width_, y_ + height_, back_symbol, (Color)back_format.color_char, (Color)back_format.color_back, back_format.flags);
y_,
x_ + width_,
y_ + height_,
back_symbol,
(Color)back_format.color_char,
(Color)back_format.color_back,
back_format.flags);
drawEvent(d); drawEvent(d);
piForeach(PIScreenTile * t, tiles) piForeach (PIScreenTile * t, tiles)
t->drawEventInternal(d); t->drawEventInternal(d);
} }
@@ -146,22 +141,18 @@ void PIScreenTile::sizeHint(int & w, int & h) const {
h = 0; h = 0;
if (tiles.isEmpty()) return; if (tiles.isEmpty()) return;
int sl = spacing * (tiles.size_s() - 1); int sl = spacing * (tiles.size_s() - 1);
if (direction == Horizontal) if (direction == Horizontal) w += sl;
w += sl; else h += sl;
else piForeachC (PIScreenTile * t, tiles) {
h += sl;
piForeachC(PIScreenTile * t, tiles) {
if (!t->visible) continue; if (!t->visible) continue;
int cw(0), ch(0); int cw(0), ch(0);
t->sizeHint(cw, ch); t->sizeHint(cw, ch);
cw = piClampi(cw, t->minimumWidth, t->maximumWidth); cw = piClampi(cw, t->minimumWidth, t->maximumWidth);
ch = piClampi(ch, t->minimumHeight, t->maximumHeight); ch = piClampi(ch, t->minimumHeight, t->maximumHeight);
if (direction == Horizontal) { if (direction == Horizontal) {
w += cw; w += cw; h = piMaxi(h, ch);
h = piMaxi(h, ch);
} else { } else {
h += ch; h += ch; w = piMaxi(w, cw);
w = piMaxi(w, cw);
} }
} }
w += marginLeft + marginRight; w += marginLeft + marginRight;
@@ -172,7 +163,7 @@ void PIScreenTile::sizeHint(int & w, int & h) const {
void PIScreenTile::layout() { void PIScreenTile::layout() {
if (tiles.isEmpty() || !visible) return; if (tiles.isEmpty() || !visible) return;
int as(0), ts(0), ts2(0), ecnt(0), pcnt(0); int as(0), ts(0), ts2(0), ecnt(0), pcnt(0);
ts = (direction == Horizontal) ? (width_ - marginLeft - marginRight) : (height_ - marginTop - marginBottom); ts = (direction == Horizontal) ? (width_ - marginLeft - marginRight) : (height_ - marginTop - marginBottom);
ts2 = (direction != Horizontal) ? (width_ - marginLeft - marginRight) : (height_ - marginTop - marginBottom); ts2 = (direction != Horizontal) ? (width_ - marginLeft - marginRight) : (height_ - marginTop - marginBottom);
ts -= spacing * (tiles.size_s() - 1); ts -= spacing * (tiles.size_s() - 1);
PIVector<int> hints(tiles.size_s()); PIVector<int> hints(tiles.size_s());
@@ -189,7 +180,7 @@ void PIScreenTile::layout() {
cs = (direction == Horizontal) ? cw : ch; cs = (direction == Horizontal) ? cw : ch;
as += cs; as += cs;
} }
hints[i] = cs; hints[i] = cs;
asizes[i] = 0.f; asizes[i] = 0.f;
} }
if (as <= ts) { if (as <= ts) {
@@ -197,10 +188,10 @@ void PIScreenTile::layout() {
SizePolicy pol = Fixed; SizePolicy pol = Fixed;
if (ecnt > 0) { if (ecnt > 0) {
acnt = ecnt; acnt = ecnt;
pol = Expanding; pol = Expanding;
} else if (pcnt > 0) { } else if (pcnt > 0) {
acnt = pcnt; acnt = pcnt;
pol = Preferred; pol = Preferred;
} }
if (acnt > 0) { if (acnt > 0) {
float add_a = float(ts - as), add_s = add_a / acnt, add_da(0.); float add_a = float(ts - as), add_s = add_a / acnt, add_da(0.);
@@ -219,7 +210,8 @@ void PIScreenTile::layout() {
for (int j = 0; j < tiles.size_s(); ++j) { for (int j = 0; j < tiles.size_s(); ++j) {
if (i == j) continue; if (i == j) continue;
if (max_tl[j]) continue; if (max_tl[j]) continue;
if (tiles[j]->size_policy == pol && tiles[j]->visible && tiles[j]->needLayout()) asizes[j] += pas; if (tiles[j]->size_policy == pol && tiles[j]->visible && tiles[j]->needLayout())
asizes[j] += pas;
} }
} }
} }
@@ -241,15 +233,16 @@ void PIScreenTile::layout() {
t->x_ = cx; t->x_ = cx;
t->y_ = cy; t->y_ = cy;
if (direction == Horizontal) { if (direction == Horizontal) {
t->width_ = hints[i]; t->width_ = hints[i];
t->height_ = ts2; t->height_ = ts2;
cx += hints[i] + spacing; cx += hints[i] + spacing;
} else { } else {
t->width_ = ts2; t->width_ = ts2;
t->height_ = hints[i]; t->height_ = hints[i];
cy += hints[i] + spacing; cy += hints[i] + spacing;
} }
if (t->pw != t->width_ || t->ph != t->height_) t->resizeEvent(t->width_, t->height_); if (t->pw != t->width_ || t->ph != t->height_)
t->resizeEvent(t->width_, t->height_);
t->pw = t->width_; t->pw = t->width_;
t->ph = t->height_; t->ph = t->height_;
t->layout(); t->layout();

View File

@@ -1,24 +1,23 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Various tiles for PIScreen Various tiles for PIScreen
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piscreentiles.h" #include "piscreentiles.h"
#include "piscreendrawer.h" #include "piscreendrawer.h"
@@ -38,7 +37,7 @@ TileSimple::TileSimple(const TileSimple::Row & r): PIScreenTile() {
void TileSimple::sizeHint(int & w, int & h) const { void TileSimple::sizeHint(int & w, int & h) const {
w = h = 0; w = h = 0;
piForeachC(Row & r, content) piForeachC (Row & r, content)
w = piMaxi(w, r.first.size_s()); w = piMaxi(w, r.first.size_s());
h = content.size_s(); h = content.size_s();
} }
@@ -58,13 +57,13 @@ void TileSimple::drawEvent(PIScreenDrawer * d) {
} }
// TileScrollBar
TileScrollBar::TileScrollBar(const PIString & n) { TileScrollBar::TileScrollBar(const PIString & n) {
direction = Vertical; direction = Vertical;
thickness = 1; thickness = 1;
minimum_ = value_ = 0; minimum_ = value_ = 0;
maximum_ = 100; maximum_ = 100;
} }
@@ -123,15 +122,15 @@ bool TileScrollBar::mouseEvent(PIKbdListener::MouseEvent me) {
} }
// TileList
TileList::TileList(const PIString & n, SelectionMode sm): PIScreenTile(n) { TileList::TileList(const PIString & n, SelectionMode sm): PIScreenTile(n) {
alignment = Left; alignment = Left;
focus_flags = CanHasFocus | NextByArrowsHorizontal | NextByTab | FocusOnMouseOrWheel; focus_flags = CanHasFocus | NextByArrowsHorizontal | NextByTab | FocusOnMouseOrWheel;
lhei = offset = cur = 0; lhei = offset = cur = 0;
mouse_sel = false; mouse_sel = false;
selection_mode = sm; selection_mode = sm;
scroll = new TileScrollBar(); scroll = new TileScrollBar();
scroll->size_policy = Ignore; scroll->size_policy = Ignore;
addTile(scroll); addTile(scroll);
} }
@@ -139,26 +138,26 @@ TileList::TileList(const PIString & n, SelectionMode sm): PIScreenTile(n) {
void TileList::sizeHint(int & w, int & h) const { void TileList::sizeHint(int & w, int & h) const {
w = h = 0; w = h = 0;
piForeachC(Row & r, content) piForeachC (Row & r, content)
w = piMaxi(w, r.first.size_s()); w = piMaxi(w, r.first.size_s());
h = 3; h = 3;
} }
void TileList::resizeEvent(int w, int h) { void TileList::resizeEvent(int w, int h) {
scroll->x_ = x_ + width_ - 1; scroll->x_ = x_ + width_ - 1;
scroll->y_ = y_; scroll->y_ = y_;
scroll->width_ = 1; scroll->width_ = 1;
scroll->height_ = height_; scroll->height_ = height_;
} }
void TileList::drawEvent(PIScreenDrawer * d) { void TileList::drawEvent(PIScreenDrawer * d) {
lhei = height_ - 2; lhei = height_ - 2;
int is = piClampi(offset, 0, piMaxi(0, content.size_s() - 1)), ie = piClampi(offset + lhei, 0, content.size_s()); int is = piClampi(offset, 0, piMaxi(0, content.size_s() - 1)), ie = piClampi(offset + lhei, 0, content.size_s());
if (is > 0) d->drawText(x_, y_, PIString(" /\\ ").repeat(width_ / 4), Green, Default, Bold); if (is > 0) d->drawText(x_, y_, PIString(" /\\ ").repeat(width_ / 4), Green, Default, Bold);
if (ie < content.size_s()) d->drawText(x_, y_ + height_ - 1, PIString(" \\/ ").repeat(width_ / 4), Green, Default, Bold); if (ie < content.size_s()) d->drawText(x_, y_ + height_ - 1, PIString(" \\/ ").repeat(width_ / 4), Green, Default, Bold);
// piCout << is << ie << offset << lhei << content.size_s(); //piCout << is << ie << offset << lhei << content.size_s();
for (int i = is; i < ie; ++i) { for (int i = is; i < ie; ++i) {
Row & r(content[i]); Row & r(content[i]);
bool sel = i == cur && has_focus; bool sel = i == cur && has_focus;
@@ -173,7 +172,7 @@ void TileList::drawEvent(PIScreenDrawer * d) {
case Right: rx = x_ + width_ - 1 - r.first.size_s(); break; case Right: rx = x_ + width_ - 1 - r.first.size_s(); break;
}; };
CharFlags cf = r.second.flags; CharFlags cf = r.second.flags;
Color cc = (Color)r.second.color_char; Color cc = (Color)r.second.color_char;
if (selected[i]) { if (selected[i]) {
cf |= Bold; cf |= Bold;
cc = Yellow; cc = Yellow;
@@ -189,7 +188,9 @@ bool TileList::keyEvent(PIKbdListener::KeyEvent key) {
lhei = height_ - 2; lhei = height_ - 2;
int oo(0), osp = piMini(3, lhei / 4); int oo(0), osp = piMini(3, lhei / 4);
switch (key.key) { switch (key.key) {
case PIKbdListener::PageUp: cur -= lhei / 2; oo -= lhei / 2; case PIKbdListener::PageUp:
cur -= lhei / 2;
oo -= lhei / 2;
case PIKbdListener::UpArrow: case PIKbdListener::UpArrow:
cur--; cur--;
oo--; oo--;
@@ -206,8 +207,7 @@ bool TileList::keyEvent(PIKbdListener::KeyEvent key) {
switch (selection_mode) { switch (selection_mode) {
case NoSelection: return false; case NoSelection: return false;
case SingleSelection: case SingleSelection:
if (selected.isEmpty()) if (selected.isEmpty()) selected << cur;
selected << cur;
else { else {
bool add = !selected[cur]; bool add = !selected[cur];
selected.clear(); selected.clear();
@@ -216,10 +216,8 @@ bool TileList::keyEvent(PIKbdListener::KeyEvent key) {
raiseEvent(TileEvent(SelectionChanged)); raiseEvent(TileEvent(SelectionChanged));
return true; return true;
case MultiSelection: case MultiSelection:
if (selected[cur]) if (selected[cur]) selected.remove(cur);
selected.remove(cur); else selected << cur;
else
selected << cur;
raiseEvent(TileEvent(SelectionChanged)); raiseEvent(TileEvent(SelectionChanged));
break; break;
} }
@@ -240,14 +238,17 @@ bool TileList::keyEvent(PIKbdListener::KeyEvent key) {
if (offset >= content.size_s() - lhei) offset = content.size_s() - lhei; if (offset >= content.size_s() - lhei) offset = content.size_s() - lhei;
if (offset < 0) offset = 0; if (offset < 0) offset = 0;
return true; return true;
case PIKbdListener::Home: cur = offset = 0; return true; case PIKbdListener::Home:
cur = offset = 0;
return true;
case PIKbdListener::End: case PIKbdListener::End:
cur = content.size_s() - 1; cur = content.size_s() - 1;
offset = content.size_s() - lhei; offset = content.size_s() - lhei;
if (offset < 0) offset = 0; if (offset < 0) offset = 0;
return true; return true;
case PIKbdListener::Return: case PIKbdListener::Return:
if (cur >= 0 && cur < content.size_s()) raiseEvent(TileEvent(RowPressed, cur)); if (cur >= 0 && cur < content.size_s())
raiseEvent(TileEvent(RowPressed, cur));
return true; return true;
case '*': case '*':
if (selection_mode == TileList::MultiSelection) { if (selection_mode == TileList::MultiSelection) {
@@ -277,8 +278,12 @@ bool TileList::mouseEvent(PIKbdListener::MouseEvent me) {
if (mp < 0 || mp >= content.size_s()) return true; if (mp < 0 || mp >= content.size_s()) return true;
cur = mp; cur = mp;
switch (me.action) { switch (me.action) {
case PIKbdListener::MouseButtonPress: mouse_sel = !selected.contains(cur); break; case PIKbdListener::MouseButtonPress:
case PIKbdListener::MouseButtonDblClick: keyEvent(PIKbdListener::KeyEvent(PIKbdListener::Return)); return true; mouse_sel = !selected.contains(cur);
break;
case PIKbdListener::MouseButtonDblClick:
keyEvent(PIKbdListener::KeyEvent(PIKbdListener::Return));
return true;
default: break; default: break;
} }
if (me.buttons[PIKbdListener::MouseRight]) { if (me.buttons[PIKbdListener::MouseRight]) {
@@ -289,10 +294,8 @@ bool TileList::mouseEvent(PIKbdListener::MouseEvent me) {
raiseEvent(TileEvent(SelectionChanged)); raiseEvent(TileEvent(SelectionChanged));
break; break;
case MultiSelection: case MultiSelection:
if (mouse_sel) if (mouse_sel) selected << cur;
selected << cur; else selected.remove(cur);
else
selected.remove(cur);
raiseEvent(TileEvent(SelectionChanged)); raiseEvent(TileEvent(SelectionChanged));
break; break;
default: break; default: break;
@@ -308,7 +311,7 @@ bool TileList::wheelEvent(PIKbdListener::WheelEvent we) {
} }
// TileButton
TileButton::TileButton(const PIString & n): PIScreenTile(n) { TileButton::TileButton(const PIString & n): PIScreenTile(n) {
focus_flags = CanHasFocus | NextByTab | NextByArrowsAll | FocusOnMouse; focus_flags = CanHasFocus | NextByTab | NextByArrowsAll | FocusOnMouse;
@@ -324,7 +327,7 @@ void TileButton::sizeHint(int & w, int & h) const {
void TileButton::drawEvent(PIScreenDrawer * d) { void TileButton::drawEvent(PIScreenDrawer * d) {
Color cb = has_focus ? Blue : Cyan; Color cb = has_focus ? Blue : Cyan;
Color ct = has_focus ? White : Black; Color ct = has_focus ? White : Black;
int ff = has_focus ? Bold : 0; int ff = has_focus ? Bold : 0;
d->fillRect(x_, y_, x_ + width_, y_ + 1, ' ', Default, cb); d->fillRect(x_, y_, x_ + width_, y_ + 1, ' ', Default, cb);
d->drawText(x_, y_, "[", ct, Transparent, ff); d->drawText(x_, y_, "[", ct, Transparent, ff);
d->drawText(x_ + (width_ - text.size_s()) / 2, y_, text, ct, Transparent, ff); d->drawText(x_ + (width_ - text.size_s()) / 2, y_, text, ct, Transparent, ff);
@@ -348,25 +351,25 @@ bool TileButton::mouseEvent(PIKbdListener::MouseEvent me) {
} }
// TileButtons
TileButtons::TileButtons(const PIString & n): PIScreenTile(n) { TileButtons::TileButtons(const PIString & n): PIScreenTile(n) {
focus_flags = CanHasFocus | NextByTab | FocusOnMouse; focus_flags = CanHasFocus | NextByTab | FocusOnMouse;
direction = Horizontal; direction = Horizontal;
alignment = PIScreenTypes::Center; alignment = PIScreenTypes::Center;
cur = 0; cur = 0;
} }
void TileButtons::sizeHint(int & w, int & h) const { void TileButtons::sizeHint(int & w, int & h) const {
w = h = 0; w = h = 0;
if (direction == Horizontal) { if (direction == Horizontal) {
piForeachC(Button & b, content) piForeachC (Button & b, content)
w += b.first.size_s() + 4; w += b.first.size_s() + 4;
w += piMaxi(0, content.size_s() - 1) * 2; w += piMaxi(0, content.size_s() - 1) * 2;
h += 1; h += 1;
} else { } else {
piForeachC(Button & b, content) piForeachC (Button & b, content)
w = piMaxi(w, b.first.size_s() + 4); w = piMaxi(w, b.first.size_s() + 4);
h += content.size_s(); h += content.size_s();
h += piMaxi(0, content.size_s() - 1); h += piMaxi(0, content.size_s() - 1);
@@ -384,11 +387,12 @@ void TileButtons::drawEvent(PIScreenDrawer * d) {
case PIScreenTypes::Right: dx = width_ - shw; break; case PIScreenTypes::Right: dx = width_ - shw; break;
default: break; default: break;
} }
if (direction == PIScreenTypes::Horizontal) cx += dx; if (direction == PIScreenTypes::Horizontal)
cx += dx;
for (int i = 0; i < content.size_s(); ++i) { for (int i = 0; i < content.size_s(); ++i) {
Color cb = Cyan; Color cb = Cyan;
Color ct = Black; Color ct = Black;
int ff = 0; int ff = 0;
if (i == cur && has_focus) { if (i == cur && has_focus) {
cb = Blue; cb = Blue;
ct = White; ct = White;
@@ -426,7 +430,9 @@ bool TileButtons::keyEvent(PIKbdListener::KeyEvent key) {
if (cur >= content.size_s()) cur = content.size_s() - 1; if (cur >= content.size_s()) cur = content.size_s() - 1;
return true; return true;
case PIKbdListener::Space: case PIKbdListener::Space:
case PIKbdListener::Return: raiseEvent(TileEvent(ButtonSelected, cur)); return true; case PIKbdListener::Return:
raiseEvent(TileEvent(ButtonSelected, cur));
return true;
}; };
return PIScreenTile::keyEvent(key); return PIScreenTile::keyEvent(key);
} }
@@ -435,7 +441,8 @@ bool TileButtons::keyEvent(PIKbdListener::KeyEvent key) {
bool TileButtons::mouseEvent(PIKbdListener::MouseEvent me) { bool TileButtons::mouseEvent(PIKbdListener::MouseEvent me) {
if (me.action == PIKbdListener::MouseMove || me.action == PIKbdListener::MouseButtonPress) { if (me.action == PIKbdListener::MouseMove || me.action == PIKbdListener::MouseButtonPress) {
for (int i = 0; i < btn_rects.size_s(); ++i) for (int i = 0; i < btn_rects.size_s(); ++i)
if (me.x >= btn_rects[i].x0 && me.x < btn_rects[i].x1 && me.y >= btn_rects[i].y0 && me.y < btn_rects[i].y1) { if (me.x >= btn_rects[i].x0 && me.x < btn_rects[i].x1 &&
me.y >= btn_rects[i].y0 && me.y < btn_rects[i].y1) {
cur = i; cur = i;
break; break;
} }
@@ -447,11 +454,11 @@ bool TileButtons::mouseEvent(PIKbdListener::MouseEvent me) {
} }
// TileCheck
TileCheck::TileCheck(const PIString & n): PIScreenTile(n) { TileCheck::TileCheck(const PIString & n): PIScreenTile(n) {
focus_flags = CanHasFocus | NextByTab | NextByArrowsAll | FocusOnMouse; focus_flags = CanHasFocus | NextByTab | NextByArrowsAll | FocusOnMouse;
toggled = false; toggled = false;
} }
@@ -464,7 +471,7 @@ void TileCheck::sizeHint(int & w, int & h) const {
void TileCheck::drawEvent(PIScreenDrawer * d) { void TileCheck::drawEvent(PIScreenDrawer * d) {
Color cb = has_focus ? Blue : Cyan; Color cb = has_focus ? Blue : Cyan;
Color ct = has_focus ? White : Black; Color ct = has_focus ? White : Black;
int ff = has_focus ? Bold : 0; int ff = has_focus ? Bold : 0;
PIString cs("[ ]"); PIString cs("[ ]");
if (toggled) cs[1] = '*'; if (toggled) cs[1] = '*';
d->fillRect(x_, y_, x_ + width_, y_ + 1, ' ', Default, cb); d->fillRect(x_, y_, x_ + width_, y_ + 1, ' ', Default, cb);
@@ -492,12 +499,12 @@ bool TileCheck::mouseEvent(PIKbdListener::MouseEvent me) {
} }
// TileProgress
TileProgress::TileProgress(const PIString & n): PIScreenTile(n) { TileProgress::TileProgress(const PIString & n): PIScreenTile(n) {
maximum = 100.; maximum = 100.;
value = 0.; value = 0.;
suffix = " %"; suffix = " %";
} }
@@ -508,7 +515,7 @@ void TileProgress::sizeHint(int & w, int & h) const {
void TileProgress::drawEvent(PIScreenDrawer * d) { void TileProgress::drawEvent(PIScreenDrawer * d) {
int v = maximum == 0. ? 0 : piClampd(piRoundd(value / maximum * 100.), 0, 100); int v = maximum == 0. ? 0 : piClampd(piRoundd(value / maximum * 100.), 0, 100);
PIString s = prefix + PIString::fromNumber(piRoundd(value)) + suffix; PIString s = prefix + PIString::fromNumber(piRoundd(value)) + suffix;
int w = piRoundd(v / 100. * width_), sx = (width_ - s.size_s()) / 2; int w = piRoundd(v / 100. * width_), sx = (width_ - s.size_s()) / 2;
d->fillRect(x_, y_, x_ + width_, y_ + 1, ' ', Default, Cyan); d->fillRect(x_, y_, x_ + width_, y_ + 1, ' ', Default, Cyan);
@@ -525,26 +532,28 @@ void TileProgress::drawEvent(PIScreenDrawer * d) {
} }
// TilePICout
TilePICout::TilePICout(const PIString & n): TileList(n) { TilePICout::TilePICout(const PIString & n): TileList(n) {
max_lines = 1024; max_lines = 1024;
selection_mode = TileList::SingleSelection; selection_mode = TileList::SingleSelection;
PICout::setOutputDevices(PICout::Buffer); PICout::setOutputDevices(PICout::Buffer);
PICout::setBufferActive(true);
} }
void TilePICout::drawEvent(PIScreenDrawer * d) { void TilePICout::drawEvent(PIScreenDrawer * d) {
PIString out = PICout::getBufferAndClear(); PIString out = PICout::buffer(true);
if (!out.isEmpty()) { if (!out.isEmpty()) {
PIStringList l = out.split("\n"); PIStringList l = out.split("\n");
bool scroll = (cur == content.size_s() - 1) || !has_focus; bool scroll = (cur == content.size_s() - 1) || !has_focus;
piForeachC(PIString & s, l) piForeachC (PIString & s, l)
content << TileList::Row(s.trimmed(), format); content << TileList::Row(s.trimmed(), format);
if (content.size_s() > max_lines) content.remove(0, content.size_s() - max_lines); if (content.size_s() > max_lines)
content.remove(0, content.size_s() - max_lines);
if (scroll) { if (scroll) {
offset = piMaxi(0, content.size_s() - lhei); offset = piMaxi(0, content.size_s() - lhei);
cur = content.size_s() - 1; cur = content.size_s() - 1;
} }
} }
TileList::drawEvent(d); TileList::drawEvent(d);
@@ -561,16 +570,16 @@ bool TilePICout::keyEvent(PIKbdListener::KeyEvent key) {
} }
// TileInput
TileInput::TileInput(const PIString & n): PIScreenTile(n) { TileInput::TileInput(const PIString & n): PIScreenTile(n) {
focus_flags = CanHasFocus | NextByTab | FocusOnMouse; focus_flags = CanHasFocus | NextByTab | FocusOnMouse;
back_format.color_back = White; back_format.color_back = White;
format.color_char = Black; format.color_char = Black;
format.color_back = White; format.color_back = White;
max_length = 1024; max_length = 1024;
cur = offset = 0; cur = offset = 0;
inv = false; inv = false;
} }
@@ -583,8 +592,10 @@ void TileInput::sizeHint(int & w, int & h) const {
void TileInput::drawEvent(PIScreenDrawer * d) { void TileInput::drawEvent(PIScreenDrawer * d) {
PIString ps = text.mid(offset, width_ - 2); PIString ps = text.mid(offset, width_ - 2);
d->drawText(x_ + 1, y_, ps, (Color)format.color_char, Transparent, (CharFlags)format.flags); d->drawText(x_ + 1, y_, ps, (Color)format.color_char, Transparent, (CharFlags)format.flags);
if (offset > 0) d->drawText(x_, y_, "<", Green, Black, Bold); if (offset > 0)
if (text.size_s() - offset >= width_ - 2) d->drawText(x_ + width_ - 1, y_, ">", Green, Black, Bold); d->drawText(x_, y_, "<", Green, Black, Bold);
if (text.size_s() - offset >= width_ - 2)
d->drawText(x_ + width_ - 1, y_, ">", Green, Black, Bold);
if (!has_focus) return; if (!has_focus) return;
Color cb = (Color)format.color_char, cc = (Color)format.color_back; Color cb = (Color)format.color_char, cc = (Color)format.color_back;
if (tm_blink.elapsed_m() >= 650) { if (tm_blink.elapsed_m() >= 650) {
@@ -631,13 +642,14 @@ bool TileInput::keyEvent(PIKbdListener::KeyEvent key) {
reserCursor(); reserCursor();
return true; return true;
case PIKbdListener::End: case PIKbdListener::End:
cur = text.size_s(); cur = text.size_s();
offset = text.size_s() - lwid; offset = text.size_s() - lwid;
if (offset < 0) offset = 0; if (offset < 0) offset = 0;
reserCursor(); reserCursor();
return true; return true;
case PIKbdListener::Backspace: case PIKbdListener::Backspace:
if (cur > text.size_s() || text.isEmpty()) return true; if (cur > text.size_s() || text.isEmpty())
return true;
text.remove(cur - 1, 1); text.remove(cur - 1, 1);
cur--; cur--;
if (cur > text.size_s()) cur = text.size_s(); if (cur > text.size_s()) cur = text.size_s();
@@ -647,7 +659,8 @@ bool TileInput::keyEvent(PIKbdListener::KeyEvent key) {
reserCursor(); reserCursor();
return true; return true;
case PIKbdListener::Delete: case PIKbdListener::Delete:
if (cur >= text.size_s() || text.isEmpty()) return true; if (cur >= text.size_s() || text.isEmpty())
return true;
text.remove(cur, 1); text.remove(cur, 1);
if (cur < 0) cur = 0; if (cur < 0) cur = 0;
if (cur > text.size_s()) cur = text.size_s(); if (cur > text.size_s()) cur = text.size_s();
@@ -673,7 +686,8 @@ bool TileInput::keyEvent(PIKbdListener::KeyEvent key) {
case PIKbdListener::F9: case PIKbdListener::F9:
case PIKbdListener::F10: case PIKbdListener::F10:
case PIKbdListener::F11: case PIKbdListener::F11:
case PIKbdListener::F12: break; case PIKbdListener::F12:
break;
default: default:
text.insert(cur, PIChar((ushort)key.key)); text.insert(cur, PIChar((ushort)key.key));
cur++; cur++;

View File

@@ -1,61 +1,58 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Virtual terminal Virtual terminal
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piterminal.h"
#include "piincludes_p.h" #include "piincludes_p.h"
#include "piliterals_time.h" #include "piterminal.h"
#include "pisharedmemory.h" #include "pisharedmemory.h"
#ifndef MICRO_PIP #ifndef MICRO_PIP
# ifdef WINDOWS #ifdef WINDOWS
# include <wincon.h> # include <windows.h>
# include <windows.h> # include <wingdi.h>
# include <wingdi.h> # include <wincon.h>
# include <winuser.h> # include <winuser.h>
#else
# include "piprocess.h"
# include <csignal>
# include <fcntl.h>
# include <sys/ioctl.h>
# if defined(QNX) || defined(BLACKBERRY)
# include <unix.h>
# else # else
# include "piprocess.h" # ifdef MAC_OS
# include <util.h>
# include <csignal> # else
# include <fcntl.h> # include <pty.h>
# include <sys/ioctl.h> # endif
# if defined(QNX) || defined(BLACKBERRY)
# include <unix.h>
# else
# ifdef MAC_OS
# include <util.h>
# else
# include <pty.h>
# endif
# endif
# ifdef ANDROID
# if __ANDROID_API__ >= 23
# define HAS_FORKPTY
# endif
# else
# define HAS_FORKPTY
# endif
# endif # endif
# ifdef ANDROID
# if __ANDROID_API__ >= 23
# define HAS_FORKPTY
# endif
# else
# define HAS_FORKPTY
# endif
#endif
// extern PIMutex PICout::__mutex__; //extern PIMutex PICout::__mutex__;
# ifdef WINDOWS #ifdef WINDOWS
# define PIPE_BUFFER_SIZE 1024 # define PIPE_BUFFER_SIZE 1024
enum PITerminalAuxMessageType { enum PITerminalAuxMessageType {
mtKey = 1, mtKey = 1,
mtResize, mtResize,
@@ -68,22 +65,22 @@ struct PITerminalAuxData {
int size_y; int size_y;
int cells_size; int cells_size;
}; };
# else #else
# define BUFFER_SIZE 4096 # define BUFFER_SIZE 4096
enum DECType { enum DECType {
CKM = 1 CKM = 1
}; };
# endif #endif
PRIVATE_DEFINITION_START(PITerminal) PRIVATE_DEFINITION_START(PITerminal)
# ifdef WINDOWS #ifdef WINDOWS
PISharedMemory * shm; PISharedMemory * shm;
HANDLE hConBuf; HANDLE hConBuf;
STARTUPINFOA si; STARTUPINFOA si;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
HANDLE pipe; HANDLE pipe;
# else #else
PIString shell; PIString shell;
PIByteArray read_buf, tmp_buf; PIByteArray read_buf, tmp_buf;
PIScreenTypes::CellFormat cur_format, line_format; PIScreenTypes::CellFormat cur_format, line_format;
@@ -95,21 +92,21 @@ PRIVATE_DEFINITION_START(PITerminal)
PIString esc_seq; PIString esc_seq;
bool is_esc_seq, last_read; bool is_esc_seq, last_read;
PIMap<int, bool> DEC; PIMap<int, bool> DEC;
PIVector<PIVector<PIScreenTypes::Cell>> cells_save; PIVector<PIVector<PIScreenTypes::Cell> > cells_save;
# endif #endif
PRIVATE_DEFINITION_END(PITerminal) PRIVATE_DEFINITION_END(PITerminal)
# ifdef WINDOWS #ifdef WINDOWS
int writePipe(HANDLE pipe, const PIByteArray & ba) { int writePipe(HANDLE pipe, const PIByteArray & ba) {
DWORD wrote[2]; DWORD wrote[2];
int sz = ba.size_s(); int sz = ba.size_s();
WriteFile(pipe, &sz, 4, &(wrote[0]), 0); WriteFile(pipe, &sz, 4, &(wrote[0]), 0);
WriteFile(pipe, ba.data(), ba.size_s(), &(wrote[1]), 0); WriteFile(pipe, ba.data(), ba.size_s(), &(wrote[1]), 0);
// piCout << "send" << ba.size_s(); //piCout << "send" << ba.size_s();
return int(wrote[0] + wrote[1]); return int(wrote[0] + wrote[1]);
} }
# endif #endif
PITerminal::PITerminal(): PIThread() { PITerminal::PITerminal(): PIThread() {
@@ -117,41 +114,42 @@ PITerminal::PITerminal(): PIThread() {
initPrivate(); initPrivate();
cursor_blink = false; cursor_blink = false;
cursor_x = cursor_y = 0; cursor_x = cursor_y = 0;
dsize_x = 80; dsize_x = 80;
dsize_y = 24; dsize_y = 24;
# ifdef WINDOWS #ifdef WINDOWS
PRIVATE->shm = 0; PRIVATE->shm = 0;
# endif #endif
} }
PITerminal::~PITerminal() { PITerminal::~PITerminal() {
if (isRunning()) stop(); if (isRunning())
PIThread::waitForFinish(1_s); stop();
PIThread::waitForFinish(10);
destroy(); destroy();
# ifdef WINDOWS #ifdef WINDOWS
if (PRIVATE->shm) delete PRIVATE->shm; if (PRIVATE->shm) delete PRIVATE->shm;
# endif #endif
} }
void PITerminal::write(const PIByteArray & d) { void PITerminal::write(const PIByteArray & d) {
# ifdef WINDOWS #ifdef WINDOWS
PIByteArray msg; PIByteArray msg;
PIVector<PIKbdListener::KeyEvent> ke; PIVector<PIKbdListener::KeyEvent> ke;
for (int i = 0; i < d.size_s(); ++i) for (int i = 0; i < d.size_s(); ++i)
ke << PIKbdListener::KeyEvent(d[i]); ke << PIKbdListener::KeyEvent(d[i]);
msg << int(mtKey) << ke; msg << int(mtKey) << ke;
writePipe(PRIVATE->pipe, msg); writePipe(PRIVATE->pipe, msg);
# else #else
# ifdef HAS_FORKPTY # ifdef HAS_FORKPTY
if (PRIVATE->fd == 0) return; if (PRIVATE->fd == 0) return;
// ssize_t wrote = 0; //ssize_t wrote = 0;
// wrote = //wrote =
::write(PRIVATE->fd, d.data(), d.size_s()); ::write(PRIVATE->fd, d.data(), d.size_s());
// piCout << "wrote" << wrote << d; //piCout << "wrote" << wrote << d;
# endif
# endif # endif
#endif
cursor_tm.reset(); cursor_tm.reset();
cursor_blink = true; cursor_blink = true;
} }
@@ -159,16 +157,15 @@ void PITerminal::write(const PIByteArray & d) {
void PITerminal::write(PIKbdListener::SpecialKey k, PIKbdListener::KeyModifiers m) { void PITerminal::write(PIKbdListener::SpecialKey k, PIKbdListener::KeyModifiers m) {
PIByteArray ba; PIByteArray ba;
# ifdef WINDOWS #ifdef WINDOWS
switch (k) { switch (k) {
case PIKbdListener::Tab: ba << uchar('\t'); break; case PIKbdListener::Tab: ba << uchar('\t'); break;
case PIKbdListener::Return: ba << uchar('\r') << uchar('\n'); break; case PIKbdListener::Return: ba << uchar('\r') << uchar('\n'); break;
case PIKbdListener::Space: ba << uchar(' '); break; case PIKbdListener::Space: ba << uchar(' '); break;
default: break; default: break;
} }
// piCout << "write" << ba.size(); //piCout << "write" << ba.size();
if (!ba.isEmpty()) if (!ba.isEmpty()) write(ba);
write(ba);
else { else {
PIByteArray msg; PIByteArray msg;
PIVector<PIKbdListener::KeyEvent> ke; PIVector<PIKbdListener::KeyEvent> ke;
@@ -176,20 +173,19 @@ void PITerminal::write(PIKbdListener::SpecialKey k, PIKbdListener::KeyModifiers
msg << int(mtKey) << ke; msg << int(mtKey) << ke;
writePipe(PRIVATE->pipe, msg); writePipe(PRIVATE->pipe, msg);
} }
# else #else
int term = PRIVATE->term_type; int term = PRIVATE->term_type;
int flags = 0; int flags = 0;
switch (k) { switch (k) {
case PIKbdListener::Tab: ba << uchar('\t'); break; case PIKbdListener::Tab: ba << uchar('\t'); break;
case PIKbdListener::Return: ba << uchar('\n'); break; case PIKbdListener::Return: ba << uchar('\n'); break;
case PIKbdListener::Esc: ba << uchar('\e'); break; case PIKbdListener::Esc: ba << uchar('\e'); break;
case PIKbdListener::Space: ba << uchar(' '); break; case PIKbdListener::Space: ba << uchar(' '); break;
case PIKbdListener::Backspace: ba << uchar(0x7f); break; case PIKbdListener::Backspace: ba << uchar(0x7f); break;
case PIKbdListener::UpArrow: case PIKbdListener::UpArrow:
case PIKbdListener::DownArrow: case PIKbdListener::DownArrow:
case PIKbdListener::RightArrow: case PIKbdListener::RightArrow:
case PIKbdListener::LeftArrow: case PIKbdListener::LeftArrow: if (PRIVATE->DEC.value(CKM, false)) flags = 1;
if (PRIVATE->DEC.value(CKM, false)) flags = 1;
/*case PIKbdListener::Home: //break; /*case PIKbdListener::Home: //break;
case PIKbdListener::End: //break; case PIKbdListener::End: //break;
case PIKbdListener::PageUp: //ba << uchar('\e') << uchar('[') << uchar('5') << uchar('~'); break; case PIKbdListener::PageUp: //ba << uchar('\e') << uchar('[') << uchar('5') << uchar('~'); break;
@@ -210,55 +206,55 @@ void PITerminal::write(PIKbdListener::SpecialKey k, PIKbdListener::KeyModifiers
case PIKbdListener::F12: //break; case PIKbdListener::F12: //break;
*/ */
default: { default: {
// piCout << flags; //piCout << flags;
// int mod = 0; //int mod = 0;
if (m[PIKbdListener::Shift]) m |= 1; if (m[PIKbdListener::Shift]) m |= 1;
if (m[PIKbdListener::Alt]) m |= 2; if (m[PIKbdListener::Alt]) m |= 2;
if (m[PIKbdListener::Ctrl]) m |= 4; if (m[PIKbdListener::Ctrl]) m |= 4;
for (int i = 0;; ++i) { for (int i = 0; ; ++i) {
const PIKbdListener::EscSeq & e(PIKbdListener::esc_seq[i]); const PIKbdListener::EscSeq & e(PIKbdListener::esc_seq[i]);
if (!e.seq) break; if (!e.seq) break;
// piCout << "search" << rc[1] << esc_seq[i].seq; //piCout << "search" << rc[1] << esc_seq[i].seq;
if (e.key == k && e.mod == m) { if (e.key == k && e.mod == m) {
if (((e.vt & term) == term) || (((e.flags & flags) == flags) && (flags != 0))) { if (((e.vt & term) == term) || (((e.flags & flags) == flags) && (flags != 0))) {
// piCout << "found key" << PIString(e.seq).replaceAll("\e", "\\e"); //piCout << "found key" << PIString(e.seq).replaceAll("\e", "\\e");
PIByteArray d = ("\e" + PIString(e.seq)).toByteArray(); PIByteArray d = ("\e" + PIString(e.seq)).toByteArray();
write(d); write(d);
break; break;
} }
} }
} }
} break; } break;
} }
// piCout << "write" << ba.size(); //piCout << "write" << ba.size();
if (!ba.isEmpty()) write(ba); if (!ba.isEmpty()) write(ba);
# endif #endif
cursor_tm.reset(); cursor_tm.reset();
cursor_blink = true; cursor_blink = true;
} }
void PITerminal::write(PIKbdListener::KeyEvent ke) { void PITerminal::write(PIKbdListener::KeyEvent ke) {
if (isSpecialKey(ke.key)) if (isSpecialKey(ke.key)) write((PIKbdListener::SpecialKey)ke.key, ke.modifiers);
write((PIKbdListener::SpecialKey)ke.key, ke.modifiers);
else { else {
PIByteArray ba; PIByteArray ba;
# ifdef WINDOWS #ifdef WINDOWS
ba << uchar(PIChar((ushort)ke.key).toConsole1Byte()); ba << uchar(PIChar((ushort)ke.key).toConsole1Byte());
# else #else
ba = PIString(PIChar((ushort)ke.key)).toUTF8(); ba = PIString(PIChar((ushort)ke.key)).toUTF8();
# endif #endif
write(ba); write(ba);
} }
} }
PIVector<PIVector<PIScreenTypes::Cell>> PITerminal::content() { PIVector<PIVector<PIScreenTypes::Cell> > PITerminal::content() {
readConsole(); readConsole();
PIVector<PIVector<PIScreenTypes::Cell>> ret = cells; PIVector<PIVector<PIScreenTypes::Cell> > ret = cells;
if (cursor_blink && cursor_visible) if (cursor_blink && cursor_visible)
if (cursor_x >= 0 && cursor_x < size_x) if (cursor_x >= 0 && cursor_x < size_x)
if (cursor_y >= 0 && cursor_y < size_y) ret[cursor_y][cursor_x].format.flags ^= PIScreenTypes::Inverse; if (cursor_y >= 0 && cursor_y < size_y)
ret[cursor_y][cursor_x].format.flags ^= PIScreenTypes::Inverse;
return ret; return ret;
} }
@@ -299,56 +295,56 @@ bool PITerminal::isSpecialKey(int k) {
void PITerminal::initPrivate() { void PITerminal::initPrivate() {
# ifdef WINDOWS #ifdef WINDOWS
PRIVATE->hConBuf = INVALID_HANDLE_VALUE; PRIVATE->hConBuf = INVALID_HANDLE_VALUE;
PRIVATE->pipe = INVALID_HANDLE_VALUE; PRIVATE->pipe = INVALID_HANDLE_VALUE;
PRIVATE->pi.hProcess = 0; PRIVATE->pi.hProcess = 0;
# else #else
PRIVATE->shell = "/bin/bash"; PRIVATE->shell = "/bin/bash";
PRIVATE->read_buf.reserve(BUFFER_SIZE); PRIVATE->read_buf.reserve(BUFFER_SIZE);
PRIVATE->read_buf.fill(0); PRIVATE->read_buf.fill(0);
PRIVATE->fd = PRIVATE->cur_x = PRIVATE->cur_y = 0; PRIVATE->fd = PRIVATE->cur_x = PRIVATE->cur_y = 0;
PRIVATE->save_cur_x = PRIVATE->save_cur_y = 0; PRIVATE->save_cur_x = PRIVATE->save_cur_y = 0;
PRIVATE->pid = 0; PRIVATE->pid = 0;
PRIVATE->term_type = 0; PRIVATE->term_type = 0;
PRIVATE->is_esc_seq = false; PRIVATE->is_esc_seq = false;
PRIVATE->last_read = true; PRIVATE->last_read = true;
PRIVATE->esc_seq.clear(); PRIVATE->esc_seq.clear();
PRIVATE->cur_format = PIScreenTypes::CellFormat(); PRIVATE->cur_format = PIScreenTypes::CellFormat();
# endif #endif
cursor_blink = cursor_visible = true; cursor_blink = cursor_visible = true;
size_x = size_y = 0; size_x = size_y = 0;
} }
void PITerminal::readConsole() { void PITerminal::readConsole() {
# ifdef WINDOWS #ifdef WINDOWS
if (!PRIVATE->shm) return; if (!PRIVATE->shm) return;
PITerminalAuxData data; PITerminalAuxData data;
PRIVATE->shm->read(&data, sizeof(data)); PRIVATE->shm->read(&data, sizeof(data));
if (data.cells_size <= 4) return; if (data.cells_size <= 4) return;
cursor_x = data.cursor_x; cursor_x = data.cursor_x;
cursor_y = data.cursor_y; cursor_y = data.cursor_y;
size_x = data.size_x; size_x = data.size_x;
size_y = data.size_y; size_y = data.size_y;
PIByteArray ba; PIByteArray ba;
ba.resize(data.cells_size); ba.resize(data.cells_size);
PRIVATE->shm->read(ba.data(), ba.size_s(), sizeof(data)); PRIVATE->shm->read(ba.data(), ba.size_s(), sizeof(data));
ba >> cells; ba >> cells;
# endif #endif
// piCout << cursor_x << cursor_y; //piCout << cursor_x << cursor_y;
} }
void PITerminal::getCursor(int & x, int & y) { void PITerminal::getCursor(int & x, int & y) {
# ifdef WINDOWS #ifdef WINDOWS
if (!PRIVATE->shm) return; if (!PRIVATE->shm) return;
int sz = 0; int sz = 0;
PRIVATE->shm->read(&sz, 4); PRIVATE->shm->read(&sz, 4);
# else #else
x = PRIVATE->cur_x; x = PRIVATE->cur_x;
y = PRIVATE->cur_y; y = PRIVATE->cur_y;
# endif #endif
} }
@@ -374,16 +370,16 @@ void PITerminal::run() {
cursor_tm.reset(); cursor_tm.reset();
cursor_blink = !cursor_blink; cursor_blink = !cursor_blink;
} }
# ifndef WINDOWS #ifndef WINDOWS
# ifdef HAS_FORKPTY # ifdef HAS_FORKPTY
if (PRIVATE->fd == 0) return; if (PRIVATE->fd == 0) return;
PRIVATE->tmp_buf.resize(BUFFER_SIZE); PRIVATE->tmp_buf.resize(BUFFER_SIZE);
int readed = ::read(PRIVATE->fd, PRIVATE->tmp_buf.data(), BUFFER_SIZE - PRIVATE->read_buf.size_s()); int readed = ::read(PRIVATE->fd, PRIVATE->tmp_buf.data(), BUFFER_SIZE - PRIVATE->read_buf.size_s());
bool used = false; bool used = false;
if (readed > 0) { if (readed > 0) {
PRIVATE->last_read = true; PRIVATE->last_read = true;
// piCoutObj << "readed" << readed << PIString(PRIVATE->tmp_buf.resized(readed)).replaceAll("\e", "\\e"); //piCoutObj << "readed" << readed << PIString(PRIVATE->tmp_buf.resized(readed)).replaceAll("\e", "\\e");
// piCoutObj << "readed" << readed << (PRIVATE->tmp_buf.resized(readed)); //piCoutObj << "readed" << readed << (PRIVATE->tmp_buf.resized(readed));
PRIVATE->read_buf.append(PRIVATE->tmp_buf.resized(readed)); PRIVATE->read_buf.append(PRIVATE->tmp_buf.resized(readed));
for (;;) { for (;;) {
int ind = -1; int ind = -1;
@@ -399,27 +395,28 @@ void PITerminal::run() {
} }
bool parse = PRIVATE->read_buf.size_s() >= BUFFER_SIZE; bool parse = PRIVATE->read_buf.size_s() >= BUFFER_SIZE;
if (PRIVATE->read_buf.size_s() == 1) if (PRIVATE->read_buf.size_s() == 1)
if (PRIVATE->read_buf[0] < 0x80) parse = true; if (PRIVATE->read_buf[0] < 0x80)
parse = true;
if (parse) { if (parse) {
parseInput(PIString::fromUTF8(PRIVATE->read_buf)); parseInput(PIString(PRIVATE->read_buf));
PRIVATE->read_buf.clear(); PRIVATE->read_buf.clear();
} }
// printf("%s", PRIVATE->read_buf.data()); //printf("%s", PRIVATE->read_buf.data());
} }
if (!used && !PRIVATE->last_read && !PRIVATE->read_buf.isEmpty()) { if (!used && !PRIVATE->last_read && !PRIVATE->read_buf.isEmpty()) {
parseInput(PIString::fromUTF8(PRIVATE->read_buf)); parseInput(PIString(PRIVATE->read_buf));
PRIVATE->read_buf.clear(); PRIVATE->read_buf.clear();
} }
PRIVATE->last_read = false; PRIVATE->last_read = false;
# endif
# endif # endif
#endif
} }
# ifndef WINDOWS #ifndef WINDOWS
void PITerminal::parseInput(const PIString & s) { void PITerminal::parseInput(const PIString & s) {
// piCoutObj << s.replaceAll("\e", "\\e"); //piCoutObj << s.replaceAll("\e", "\\e");
// printf("%s", s.data()); //printf("%s", s.data());
for (int i = 0; i < s.size_s(); ++i) { for (int i = 0; i < s.size_s(); ++i) {
if (s[i].unicode16Code() == 0) break; if (s[i].unicode16Code() == 0) break;
if (PRIVATE->is_esc_seq) { if (PRIVATE->is_esc_seq) {
@@ -432,7 +429,7 @@ void PITerminal::parseInput(const PIString & s) {
if (isCompleteEscSeq(PRIVATE->esc_seq)) { if (isCompleteEscSeq(PRIVATE->esc_seq)) {
PRIVATE->is_esc_seq = false; PRIVATE->is_esc_seq = false;
applyEscSeq(PRIVATE->esc_seq); applyEscSeq(PRIVATE->esc_seq);
// piCoutObj << PRIVATE->esc_seq; //piCoutObj << PRIVATE->esc_seq;
} }
} }
} else { } else {
@@ -447,15 +444,14 @@ void PITerminal::parseInput(const PIString & s) {
} }
if (s[i] == '\r') continue; if (s[i] == '\r') continue;
if (s[i] == '\n') { if (s[i] == '\n') {
// piCoutObj << "new line"; //piCoutObj << "new line";
for (int i = PRIVATE->cur_x; i < size_x; ++i) for (int i = PRIVATE->cur_x; i < size_x; ++i) cells[PRIVATE->cur_y][i].format = PRIVATE->cur_format;
cells[PRIVATE->cur_y][i].format = PRIVATE->cur_format;
PRIVATE->line_format = PRIVATE->cur_format; PRIVATE->line_format = PRIVATE->cur_format;
PRIVATE->cur_x = 0; PRIVATE->cur_x = 0;
moveCursor(0, 1); moveCursor(0, 1);
continue; continue;
} }
// piCoutObj << "char" << s[i] << s[i].unicode16Code() << "at" << PRIVATE->cur_x << PRIVATE->cur_y; //piCoutObj << "char" << s[i] << s[i].unicode16Code() << "at" << PRIVATE->cur_x << PRIVATE->cur_y;
cells[PRIVATE->cur_y][PRIVATE->cur_x].symbol = s[i]; cells[PRIVATE->cur_y][PRIVATE->cur_x].symbol = s[i];
cells[PRIVATE->cur_y][PRIVATE->cur_x].format = PRIVATE->cur_format; cells[PRIVATE->cur_y][PRIVATE->cur_x].format = PRIVATE->cur_format;
moveCursor(1, 0); moveCursor(1, 0);
@@ -479,10 +475,10 @@ bool PITerminal::isCompleteEscSeq(const PIString & es) {
void PITerminal::applyEscSeq(PIString es) { void PITerminal::applyEscSeq(PIString es) {
piCoutObj << es; piCoutObj << es;
if (es.size_s() < 2) return; if (es.size_s() < 2) return;
// PIScreenTypes::Cell line_cell = PIScreenTypes::Cell(' ', PRIVATE->line_format); // PIScreenTypes::Cell line_cell = PIScreenTypes::Cell(' ', PRIVATE->line_format);
PIScreenTypes::Cell def_cell = PIScreenTypes::Cell(' ', PRIVATE->cur_format); PIScreenTypes::Cell def_cell = PIScreenTypes::Cell(' ', PRIVATE->cur_format);
if (es[1] == '?' && es.size_s() >= 2) { if (es[1] == '?' && es.size_s() >= 2) {
char a = es.takeRight(1)[0].toAscii(); char a = es.takeRight(1)[0].toAscii();
bool val = false; bool val = false;
if (a == 'l') val = false; if (a == 'l') val = false;
if (a == 'h') val = true; if (a == 'h') val = true;
@@ -502,8 +498,7 @@ void PITerminal::applyEscSeq(PIString es) {
case 1047: case 1047:
if (val) { if (val) {
PRIVATE->cells_save = cells; PRIVATE->cells_save = cells;
for (int i = 0; i < size_y; ++i) for (int i = 0; i < size_y; ++i) cells[i].fill(def_cell);
cells[i].fill(def_cell);
} else { } else {
cells = PRIVATE->cells_save; cells = PRIVATE->cells_save;
} }
@@ -518,7 +513,7 @@ void PITerminal::applyEscSeq(PIString es) {
return; return;
} }
PIStringList args = es.split(";"); PIStringList args = es.split(";");
piForeachC(PIString & a, args) { piForeachC (PIString & a, args) {
int av = a.toInt(); int av = a.toInt();
switch (av) { switch (av) {
case 0: PRIVATE->cur_format = PIScreenTypes::CellFormat(); break; case 0: PRIVATE->cur_format = PIScreenTypes::CellFormat(); break;
@@ -529,14 +524,8 @@ void PITerminal::applyEscSeq(PIString es) {
default: { default: {
bool col = false, target = false; bool col = false, target = false;
int cid = av % 10; int cid = av % 10;
if (av >= 30 && av <= 37) { if (av >= 30 && av <= 37) {col = true; target = false;}
col = true; if (av >= 40 && av <= 47) {col = true; target = true;}
target = false;
}
if (av >= 40 && av <= 47) {
col = true;
target = true;
}
if (col) { if (col) {
int cfl = 0; int cfl = 0;
switch (cid) { switch (cid) {
@@ -555,13 +544,13 @@ void PITerminal::applyEscSeq(PIString es) {
PRIVATE->cur_format.color_char = cfl; PRIVATE->cur_format.color_char = cfl;
break; break;
} }
} break; } break;
} }
} }
/*if ((PRIVATE->cur_format.flags & PIScreenTypes::Inverse) == PIScreenTypes::Inverse) { /*if ((PRIVATE->cur_format.flags & PIScreenTypes::Inverse) == PIScreenTypes::Inverse) {
uchar t = PRIVATE->cur_format.color_char; uchar t = PRIVATE->cur_format.color_char;
PRIVATE->cur_format.color_char = PRIVATE->cur_format.color_back; PRIVATE->cur_format.color_char = PRIVATE->cur_format.color_back;
PRIVATE->cur_format.color_back = t; PRIVATE->cur_format.color_back = t;
}*/ }*/
} }
if (es.back() == 'r') { if (es.back() == 'r') {
@@ -590,37 +579,33 @@ void PITerminal::applyEscSeq(PIString es) {
int x(0), y(0); int x(0), y(0);
if (!args[0].isEmpty()) y = args[0].toInt() - 1; if (!args[0].isEmpty()) y = args[0].toInt() - 1;
if (!args[1].isEmpty()) x = args[1].toInt() - 1; if (!args[1].isEmpty()) x = args[1].toInt() - 1;
// piCoutObj << x << y; //piCoutObj << x << y;
PRIVATE->cur_x = piClamp(x, 0, size_x - 1); PRIVATE->cur_x = piClamp(x, 0, size_x - 1);
PRIVATE->cur_y = piClamp(y, 0, size_y - 1); PRIVATE->cur_y = piClamp(y, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format; PRIVATE->line_format = PRIVATE->cur_format;
} }
if (es.back() == 'A') { // cursor up if (es.back() == 'A') { // cursor up
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; PRIVATE->cur_y = piClamp(PRIVATE->cur_y - v, 0, size_y - 1);
PRIVATE->cur_y = piClamp(PRIVATE->cur_y - v, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format; PRIVATE->line_format = PRIVATE->cur_format;
} }
if (es.back() == 'B') { // cursor down if (es.back() == 'B') { // cursor down
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; PRIVATE->cur_y = piClamp(PRIVATE->cur_y + v, 0, size_y - 1);
PRIVATE->cur_y = piClamp(PRIVATE->cur_y + v, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format; PRIVATE->line_format = PRIVATE->cur_format;
} }
if (es.back() == 'C' || es.back() == 'a') { // cursor forward, next column if (es.back() == 'C' || es.back() == 'a') { // cursor forward, next column
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; PRIVATE->cur_x = piClamp(PRIVATE->cur_x + v, 0, size_x - 1);
PRIVATE->cur_x = piClamp(PRIVATE->cur_x + v, 0, size_x - 1);
PRIVATE->line_format = PRIVATE->cur_format; PRIVATE->line_format = PRIVATE->cur_format;
} }
if (es.back() == 'D') { // cursor back if (es.back() == 'D') { // cursor back
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; PRIVATE->cur_x = piClamp(PRIVATE->cur_x - v, 0, size_x - 1);
PRIVATE->cur_x = piClamp(PRIVATE->cur_x - v, 0, size_x - 1);
PRIVATE->line_format = PRIVATE->cur_format; PRIVATE->line_format = PRIVATE->cur_format;
} }
if (es.back() == 'G' || es.back() == '`') { // goto column if (es.back() == 'G' || es.back() == '`') { // goto column
@@ -631,92 +616,66 @@ void PITerminal::applyEscSeq(PIString es) {
} }
if (es.back() == 'd') { // goto line if (es.back() == 'd') { // goto line
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; PRIVATE->cur_x = 0;
PRIVATE->cur_x = 0; PRIVATE->cur_y = piClamp(v - 1, 0, size_y - 1);
PRIVATE->cur_y = piClamp(v - 1, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format; PRIVATE->line_format = PRIVATE->cur_format;
} }
if (es.back() == 'E' || es.back() == 'e') { // next line if (es.back() == 'E' || es.back() == 'e') { // next line
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; PRIVATE->cur_x = 0;
PRIVATE->cur_x = 0; PRIVATE->cur_y = piClamp(PRIVATE->cur_y + v, 0, size_y - 1);
PRIVATE->cur_y = piClamp(PRIVATE->cur_y + v, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format; PRIVATE->line_format = PRIVATE->cur_format;
} }
if (es.back() == 'F') { // previous line if (es.back() == 'F') { // previous line
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; PRIVATE->cur_x = 0;
PRIVATE->cur_x = 0; PRIVATE->cur_y = piClamp(PRIVATE->cur_y - v, 0, size_y - 1);
PRIVATE->cur_y = piClamp(PRIVATE->cur_y - v, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format; PRIVATE->line_format = PRIVATE->cur_format;
} }
if (es.back() == 'L') { // insert lines if (es.back() == 'L') { // insert lines
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; for (int i = piClamp(size_y - 1, PRIVATE->win_y0, PRIVATE->win_y1); i >= piClamp(PRIVATE->cur_y + v, PRIVATE->win_y0, PRIVATE->win_y1); --i) cells[i] = cells[i - v];
for (int i = piClamp(size_y - 1, PRIVATE->win_y0, PRIVATE->win_y1); for (int j = piClamp(PRIVATE->cur_y, PRIVATE->win_y0, PRIVATE->win_y1); j < piClamp(PRIVATE->cur_y + v, PRIVATE->win_y0, PRIVATE->win_y1); ++j)
i >= piClamp(PRIVATE->cur_y + v, PRIVATE->win_y0, PRIVATE->win_y1); for (int i = 0; i < PRIVATE->cur_x; ++i) cells[j][i] = def_cell;
--i)
cells[i] = cells[i - v];
for (int j = piClamp(PRIVATE->cur_y, PRIVATE->win_y0, PRIVATE->win_y1);
j < piClamp(PRIVATE->cur_y + v, PRIVATE->win_y0, PRIVATE->win_y1);
++j)
for (int i = 0; i < PRIVATE->cur_x; ++i)
cells[j][i] = def_cell;
} }
if (es.back() == 'M') { // delete lines if (es.back() == 'M') { // delete lines
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; for (int i = piClamp(PRIVATE->cur_y, PRIVATE->win_y0, PRIVATE->win_y1); i < piClamp(size_y - v, PRIVATE->win_y0, PRIVATE->win_y1); ++i) cells[i] = cells[i + v];
for (int i = piClamp(PRIVATE->cur_y, PRIVATE->win_y0, PRIVATE->win_y1);
i < piClamp(size_y - v, PRIVATE->win_y0, PRIVATE->win_y1);
++i)
cells[i] = cells[i + v];
for (int j = piClamp(size_y - v, PRIVATE->win_y0, PRIVATE->win_y1); j < piClamp(size_y, PRIVATE->win_y0, PRIVATE->win_y1); ++j) for (int j = piClamp(size_y - v, PRIVATE->win_y0, PRIVATE->win_y1); j < piClamp(size_y, PRIVATE->win_y0, PRIVATE->win_y1); ++j)
for (int i = 0; i < PRIVATE->cur_x; ++i) for (int i = 0; i < PRIVATE->cur_x; ++i) cells[j][i] = def_cell;
cells[j][i] = def_cell;
} }
if (es.back() == 'P') { // delete characters if (es.back() == 'P') { // delete characters
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; for (int i = PRIVATE->cur_x; i < size_x - v; ++i) cells[PRIVATE->cur_y][i] = cells[PRIVATE->cur_y][i + v];
for (int i = PRIVATE->cur_x; i < size_x - v; ++i) for (int i = size_x - v; i < size_x; ++i) cells[PRIVATE->cur_y][i] = def_cell;
cells[PRIVATE->cur_y][i] = cells[PRIVATE->cur_y][i + v];
for (int i = size_x - v; i < size_x; ++i)
cells[PRIVATE->cur_y][i] = def_cell;
} }
if (es.back() == '@') { // delete characters if (es.back() == '@') { // delete characters
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt(); if (v == 0) v = 1;
if (v == 0) v = 1; for (int i = size_x - 1; i >= PRIVATE->cur_x + v; --i) cells[PRIVATE->cur_y][i] = cells[PRIVATE->cur_y][i - v];
for (int i = size_x - 1; i >= PRIVATE->cur_x + v; --i) for (int i = PRIVATE->cur_x; i < PRIVATE->cur_x + v; ++i) cells[PRIVATE->cur_y][i] = def_cell;
cells[PRIVATE->cur_y][i] = cells[PRIVATE->cur_y][i - v];
for (int i = PRIVATE->cur_x; i < PRIVATE->cur_x + v; ++i)
cells[PRIVATE->cur_y][i] = def_cell;
} }
if (es.back() == 'J') { // erase data if (es.back() == 'J') { // erase data
es.cutLeft(1).cutRight(1); es.cutLeft(1).cutRight(1);
int v = es.toInt(); int v = es.toInt();
switch (v) { switch (v) {
case 0: case 0:
for (int i = PRIVATE->cur_x; i < size_x; ++i) for (int i = PRIVATE->cur_x; i < size_x; ++i) cells[PRIVATE->cur_y][i] = def_cell;
cells[PRIVATE->cur_y][i] = def_cell; for (int i = PRIVATE->cur_y + 1; i < size_y; ++i) cells[i].fill(def_cell);
for (int i = PRIVATE->cur_y + 1; i < size_y; ++i)
cells[i].fill(def_cell);
break; break;
case 1: case 1:
for (int i = 0; i <= PRIVATE->cur_x; ++i) for (int i = 0; i <= PRIVATE->cur_x; ++i) cells[PRIVATE->cur_y][i] = def_cell;
cells[PRIVATE->cur_y][i] = def_cell; for (int i = 0; i < PRIVATE->cur_y; ++i) cells[i].fill(def_cell);
for (int i = 0; i < PRIVATE->cur_y; ++i)
cells[i].fill(def_cell);
break; break;
case 2: case 2:
for (int i = 0; i < size_y; ++i) for (int i = 0; i < size_y; ++i) cells[i].fill(def_cell);
cells[i].fill(def_cell); //PRIVATE->cur_x = PRIVATE->cur_y = 0;
// PRIVATE->cur_x = PRIVATE->cur_y = 0;
break; break;
} }
} }
@@ -725,14 +684,14 @@ void PITerminal::applyEscSeq(PIString es) {
int v = es.toInt(); int v = es.toInt();
switch (v) { switch (v) {
case 0: case 0:
for (int i = PRIVATE->cur_x; i < size_x; ++i) for (int i = PRIVATE->cur_x; i < size_x; ++i) cells[PRIVATE->cur_y][i] = def_cell;
cells[PRIVATE->cur_y][i] = def_cell;
break; break;
case 1: case 1:
for (int i = 0; i <= PRIVATE->cur_x; ++i) for (int i = 0; i <= PRIVATE->cur_x; ++i) cells[PRIVATE->cur_y][i] = def_cell;
cells[PRIVATE->cur_y][i] = def_cell; break;
case 2:
cells[PRIVATE->cur_y].fill(def_cell);
break; break;
case 2: cells[PRIVATE->cur_y].fill(def_cell); break;
} }
} }
} }
@@ -746,12 +705,12 @@ void PITerminal::moveCursor(int dx, int dy) {
if (PRIVATE->cur_y < 0) PRIVATE->cur_y = 0; if (PRIVATE->cur_y < 0) PRIVATE->cur_y = 0;
if (PRIVATE->cur_x >= size_x) { if (PRIVATE->cur_x >= size_x) {
PRIVATE->line_format = PRIVATE->cur_format; PRIVATE->line_format = PRIVATE->cur_format;
PRIVATE->cur_x = 0; PRIVATE->cur_x = 0;
PRIVATE->cur_y++; PRIVATE->cur_y++;
} }
if (PRIVATE->cur_y >= size_y) { if (PRIVATE->cur_y >= size_y) {
int scroll = piMini(PRIVATE->cur_y - size_y + 1, size_y - 1); int scroll = piMini(PRIVATE->cur_y - size_y + 1, size_y - 1);
// piCout << "scroll" << size_x << size_y << size_y - scroll - 1; //piCout << "scroll" << size_x << size_y << size_y - scroll - 1;
PRIVATE->cur_y = size_y - 1; PRIVATE->cur_y = size_y - 1;
for (int y = 0; y < size_y - scroll; ++y) for (int y = 0; y < size_y - scroll; ++y)
cells[y] = cells[y + scroll]; cells[y] = cells[y + scroll];
@@ -762,76 +721,58 @@ void PITerminal::moveCursor(int dx, int dy) {
int PITerminal::termType(const PIString & t) { int PITerminal::termType(const PIString & t) {
if (t == "xterm") if (t == "xterm") return PIKbdListener::vt_xterm;
return PIKbdListener::vt_xterm; else if (t == "linux") return PIKbdListener::vt_linux;
else if (t == "linux")
return PIKbdListener::vt_linux;
return PIKbdListener::vt_none; return PIKbdListener::vt_none;
} }
# endif #endif
bool PITerminal::initialize() { bool PITerminal::initialize() {
destroy(); destroy();
# ifdef WINDOWS #ifdef WINDOWS
/*SECURITY_ATTRIBUTES sa; /*SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = true; sa.bInheritHandle = true;
sa.lpSecurityDescriptor = 0; sa.lpSecurityDescriptor = 0;
if (!CreatePipe(&(PRIVATE->pipe_in[0]), &(PRIVATE->pipe_in[1]), &sa, 0)) { if (!CreatePipe(&(PRIVATE->pipe_in[0]), &(PRIVATE->pipe_in[1]), &sa, 0)) {
piCoutObj << "CreatePipe error," << errorString(); piCoutObj << "CreatePipe error," << errorString();
initPrivate(); initPrivate();
return false; return false;
} }
PRIVATE->hConBuf = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, &sa, CONSOLE_TEXTMODE_BUFFER, 0); PRIVATE->hConBuf = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, &sa, CONSOLE_TEXTMODE_BUFFER, 0);
if (PRIVATE->hConBuf == INVALID_HANDLE_VALUE) { if (PRIVATE->hConBuf == INVALID_HANDLE_VALUE) {
piCoutObj << "CreateConsoleScreenBuffer error," << errorString(); piCoutObj << "CreateConsoleScreenBuffer error," << errorString();
destroy(); destroy();
return false; return false;
}*/ }*/
// CreatePipe(&(PRIVATE->pipe_out[0]), &(PRIVATE->pipe_out[1]), &sa, 0); //CreatePipe(&(PRIVATE->pipe_out[0]), &(PRIVATE->pipe_out[1]), &sa, 0);
// SetHandleInformation(PRIVATE->pipe_in[1], HANDLE_FLAG_INHERIT, 0); //SetHandleInformation(PRIVATE->pipe_in[1], HANDLE_FLAG_INHERIT, 0);
// SetHandleInformation(PRIVATE->hConBuf, HANDLE_FLAG_INHERIT, 0); //SetHandleInformation(PRIVATE->hConBuf, HANDLE_FLAG_INHERIT, 0);
// GetStartupInfoA(&PRIVATE->si); //GetStartupInfoA(&PRIVATE->si);
memset(&PRIVATE->si, 0, sizeof(PRIVATE->si)); memset(&PRIVATE->si, 0, sizeof(PRIVATE->si));
PRIVATE->si.cb = sizeof(STARTUPINFO); PRIVATE->si.cb = sizeof(STARTUPINFO);
// PRIVATE->si.dwFlags |= STARTF_USESTDHANDLES; //PRIVATE->si.dwFlags |= STARTF_USESTDHANDLES;
PRIVATE->si.dwFlags |= STARTF_USESHOWWINDOW; PRIVATE->si.dwFlags |= STARTF_USESHOWWINDOW;
PRIVATE->si.dwFlags |= STARTF_USECOUNTCHARS; PRIVATE->si.dwFlags |= STARTF_USECOUNTCHARS;
// PRIVATE->si.hStdInput = PRIVATE->pipe; //PRIVATE->si.hStdInput = PRIVATE->pipe;
// PRIVATE->si.hStdOutput = PRIVATE->hConBuf; //PRIVATE->si.hStdOutput = PRIVATE->hConBuf;
// PRIVATE->si.hStdError = PRIVATE->hConBuf; //PRIVATE->si.hStdError = PRIVATE->hConBuf;
PRIVATE->si.wShowWindow = SW_HIDE; PRIVATE->si.wShowWindow = SW_HIDE;
PRIVATE->si.dwXCountChars = 80; PRIVATE->si.dwXCountChars = 80;
PRIVATE->si.dwYCountChars = 24; PRIVATE->si.dwYCountChars = 24;
memset(&PRIVATE->pi, 0, sizeof(PRIVATE->pi)); memset(&PRIVATE->pi, 0, sizeof(PRIVATE->pi));
PIString shmh = PIString::fromNumber(randomi() % 10000); PIString shmh = PIString::fromNumber(randomi() % 10000);
PIString pname = "\\\\.\\pipe\\piterm" + shmh; PIString pname = "\\\\.\\pipe\\piterm" + shmh;
PIString cmd = "piterminal \"" + shmh + "\" \"" + pname + "\""; PIString cmd = "piterminal \"" + shmh + "\" \"" + pname + "\"";
if (!CreateProcessA(0, if(!CreateProcessA(0, (LPSTR)cmd.dataAscii(), 0, 0, false, CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP, 0, 0, &PRIVATE->si, &PRIVATE->pi)) {
(LPSTR)cmd.dataAscii(),
0,
0,
false,
CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP,
0,
0,
&PRIVATE->si,
&PRIVATE->pi)) {
piCoutObj << "CreateProcess error," << errorString(); piCoutObj << "CreateProcess error," << errorString();
destroy(); destroy();
return false; return false;
} }
PRIVATE->pipe = CreateNamedPipeA((LPSTR)pname.dataAscii(), PRIVATE->pipe = CreateNamedPipe((LPSTR)pname.dataAscii(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 2, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, 1000, NULL);
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
2,
PIPE_BUFFER_SIZE,
PIPE_BUFFER_SIZE,
1000,
NULL);
if (PRIVATE->pipe == INVALID_HANDLE_VALUE) { if (PRIVATE->pipe == INVALID_HANDLE_VALUE) {
piCoutObj << "CreateNamedPipe error," << errorString(); piCoutObj << "CreateNamedPipe error," << errorString();
destroy(); destroy();
@@ -851,32 +792,31 @@ bool PITerminal::initialize() {
return false; return false;
} }
if (PRIVATE->shm) delete PRIVATE->shm; if (PRIVATE->shm) delete PRIVATE->shm;
PRIVATE->shm = new PISharedMemory("piterm_aux" + shmh, 1024 * 1024); PRIVATE->shm = new PISharedMemory("piterm_aux" + shmh, 1024*1024);
CloseHandle(PRIVATE->pi.hThread); CloseHandle(PRIVATE->pi.hThread);
resize(dsize_x, dsize_y); resize(dsize_x, dsize_y);
# else #else
# ifdef HAS_FORKPTY # ifdef HAS_FORKPTY
char pty[256]; char pty[256]; memset(pty, 0, 256);
memset(pty, 0, 256);
winsize ws; winsize ws;
ws.ws_col = dsize_x; ws.ws_col = dsize_x;
ws.ws_row = dsize_y; ws.ws_row = dsize_y;
PIStringList env = PIProcess::currentEnvironment(); PIStringList env = PIProcess::currentEnvironment();
piForeachC(PIString & e, env) piForeachC (PIString & e, env)
if (e.startsWith("TERM=")) { if (e.startsWith("TERM=")) {
PRIVATE->term_type = termType(e.mid(5).trim().toLowerCase()); PRIVATE->term_type = termType(e.mid(5).trim().toLowerCase());
// piCout << PRIVATE->term_type; //piCout << PRIVATE->term_type;
break; break;
} }
pid_t fr = forkpty(&(PRIVATE->fd), pty, 0, &ws); pid_t fr = forkpty(&(PRIVATE->fd), pty, 0, &ws);
// piCoutObj << fr << PRIVATE->fd << pty; //piCoutObj << fr << PRIVATE->fd << pty;
if (fr == 0) { if (fr == 0) {
char ** argv = new char *[2]; char ** argv = new char*[2];
PIByteArray shell = PRIVATE->shell.toByteArray(); PIByteArray shell = PRIVATE->shell.toByteArray();
argv[0] = new char[shell.size() + 1]; argv[0] = new char[shell.size() + 1];
memcpy(argv[0], shell.data(), shell.size()); memcpy(argv[0], shell.data(), shell.size());
argv[0][shell.size()] = 0; argv[0][shell.size()] = 0;
argv[1] = 0; argv[1] = 0;
execvp(argv[0], argv); execvp(argv[0], argv);
delete[] argv[0]; delete[] argv[0];
delete[] argv; delete[] argv;
@@ -889,77 +829,79 @@ bool PITerminal::initialize() {
} }
PRIVATE->pid = fr; PRIVATE->pid = fr;
fcntl(PRIVATE->fd, F_SETFL, O_NONBLOCK); fcntl(PRIVATE->fd, F_SETFL, O_NONBLOCK);
/* /*
tcgetattr(PRIVATE->fd, &PRIVATE->desc); tcgetattr(PRIVATE->fd, &PRIVATE->desc);
PRIVATE->desc.c_oflag = PRIVATE->desc.c_lflag = PRIVATE->desc.c_cflag = 0; PRIVATE->desc.c_oflag = PRIVATE->desc.c_lflag = PRIVATE->desc.c_cflag = 0;
PRIVATE->desc.c_iflag = IGNBRK; PRIVATE->desc.c_iflag = IGNBRK;
PRIVATE->desc.c_cflag = CLOCAL | HUPCL; PRIVATE->desc.c_cflag = CLOCAL | HUPCL;
PRIVATE->desc.c_cflag |= (CSIZE & CS8); PRIVATE->desc.c_cflag |= (CSIZE & CS8);
PRIVATE->desc.c_cflag |= CREAD; PRIVATE->desc.c_cflag |= CREAD;
PRIVATE->desc.c_cc[VMIN] = 1; PRIVATE->desc.c_cc[VMIN] = 1;
PRIVATE->desc.c_cc[VTIME] = 1; PRIVATE->desc.c_cc[VTIME] = 1;
cfsetispeed(&PRIVATE->desc, B38400); cfsetispeed(&PRIVATE->desc, B38400);
cfsetospeed(&PRIVATE->desc, B38400); cfsetospeed(&PRIVATE->desc, B38400);
if (tcsetattr(PRIVATE->fd, TCSANOW, &PRIVATE->desc) < 0) { if (tcsetattr(PRIVATE->fd, TCSANOW, &PRIVATE->desc) < 0) {
piCoutObj << "Can`t set attributes for \"" << pty << "\""; piCoutObj << "Can`t set attributes for \"" << pty << "\"";
destroy(); destroy();
return false; return false;
} }
*/ */
size_x = dsize_x; size_x = dsize_x;
size_y = dsize_y; size_y = dsize_y;
resize(size_x, size_y); resize(size_x, size_y);
} }
# endif
# endif # endif
#endif
cursor_blink = false; cursor_blink = false;
cursor_tm.reset(); cursor_tm.reset();
start(25_Hz); start(40);
return true; return true;
} }
void PITerminal::destroy() { void PITerminal::destroy() {
// piCout << "destroy ..."; //piCout << "destroy ...";
stop(); stop();
waitForFinish(1_s); waitForFinish(1000);
# ifdef WINDOWS #ifdef WINDOWS
if (PRIVATE->pi.hProcess) { if (PRIVATE->pi.hProcess) {
// piCout << "term"; //piCout << "term";
// TerminateProcess(PRIVATE->pi.hProcess, 0); //TerminateProcess(PRIVATE->pi.hProcess, 0);
GenerateConsoleCtrlEvent(CTRL_C_EVENT, PRIVATE->pi.dwProcessId); GenerateConsoleCtrlEvent(CTRL_C_EVENT, PRIVATE->pi.dwProcessId);
CloseHandle(PRIVATE->pi.hProcess); CloseHandle(PRIVATE->pi.hProcess);
} }
if (PRIVATE->pipe != INVALID_HANDLE_VALUE) CloseHandle(PRIVATE->pipe); if (PRIVATE->pipe != INVALID_HANDLE_VALUE) CloseHandle(PRIVATE->pipe);
if (PRIVATE->hConBuf != INVALID_HANDLE_VALUE) CloseHandle(PRIVATE->hConBuf); if (PRIVATE->hConBuf != INVALID_HANDLE_VALUE) CloseHandle(PRIVATE->hConBuf);
// piCout << "destroy" << size_y; //piCout << "destroy" << size_y;
# else #else
# ifdef HAS_FORKPTY # ifdef HAS_FORKPTY
if (PRIVATE->pid != 0) kill(PRIVATE->pid, SIGKILL); if (PRIVATE->pid != 0)
if (PRIVATE->fd != 0) ::close(PRIVATE->fd); kill(PRIVATE->pid, SIGKILL);
# endif if (PRIVATE->fd != 0)
::close(PRIVATE->fd);
# endif # endif
#endif
initPrivate(); initPrivate();
} }
bool PITerminal::resize(int cols, int rows) { bool PITerminal::resize(int cols, int rows) {
bool ret = true; bool ret = true;
dsize_x = cols; dsize_x = cols;
dsize_y = rows; dsize_y = rows;
# ifdef WINDOWS #ifdef WINDOWS
if (PRIVATE->pipe == INVALID_HANDLE_VALUE) return false; if (PRIVATE->pipe == INVALID_HANDLE_VALUE) return false;
PIByteArray msg; PIByteArray msg;
msg << int(mtResize) << dsize_x << dsize_y; msg << int(mtResize) << dsize_x << dsize_y;
writePipe(PRIVATE->pipe, msg); writePipe(PRIVATE->pipe, msg);
# else #else
# ifdef HAS_FORKPTY # ifdef HAS_FORKPTY
if (PRIVATE->fd == 0) return false; if (PRIVATE->fd == 0) return false;
size_x = dsize_x; size_x = dsize_x;
size_y = dsize_y; size_y = dsize_y;
// piCout << "resize" << PRIVATE->fd << size_x << size_y; //piCout << "resize" << PRIVATE->fd << size_x << size_y;
winsize ws; winsize ws;
ws.ws_col = cols; ws.ws_col = cols;
ws.ws_row = rows; ws.ws_row = rows;
@@ -969,8 +911,8 @@ bool PITerminal::resize(int cols, int rows) {
PRIVATE->cells_save.resize(size_y); PRIVATE->cells_save.resize(size_y);
for (int i = 0; i < size_y; ++i) for (int i = 0; i < size_y; ++i)
PRIVATE->cells_save[i].resize(size_x); PRIVATE->cells_save[i].resize(size_x);
# endif
# endif # endif
#endif
cells.resize(size_y); cells.resize(size_y);
for (int i = 0; i < size_y; ++i) for (int i = 0; i < size_y; ++i)
cells[i].resize(size_x); cells[i].resize(size_x);

View File

@@ -1,297 +1,297 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
PIP Authentication API PIP Authentication API
Andrey Bychkov work.a.b@yandex.ru Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piauth.h" #include "piauth.h"
#define PIAUTH_NOISE_MAX_SIZE 256 #define PIAUTH_NOISE_MAX_SIZE 256
PIAuth::PIAuth(const PIByteArray & sign): PIObject() { PIAuth::PIAuth(const PIByteArray & sign) : PIObject() {
setName("Client"); setName("Client");
role = Client; role = Client;
state = NotConnected; state = NotConnected;
sign_sk = sign; sign_sk = sign;
sign_pk = crypt.extractSignPublicKey(sign); sign_pk = crypt.extractSignPublicKey(sign);
} }
void PIAuth::setServerPassword(const PIString & ps) { void PIAuth::setServerPassword(const PIString & ps) {
pass_hash = crypt.passwordHash(ps, PIString("PIAuth").toByteArray()); pass_hash = crypt.passwordHash(ps, PIString("PIAuth").toByteArray());
} }
void PIAuth::stop() { void PIAuth::stop() {
role = Client; role = Client;
state = NotConnected; state = NotConnected;
auth_sign.clear(); auth_sign.clear();
box_sk.clear(); box_sk.clear();
box_pk.clear(); box_pk.clear();
my_pk.clear(); my_pk.clear();
} }
void PIAuth::startClient() { void PIAuth::startClient() {
role = Client; role = Client;
state = AuthProbe; state = AuthProbe;
} }
PIByteArray PIAuth::startServer() { PIByteArray PIAuth::startServer() {
setName("Server"); setName("Server");
role = Server; role = Server;
state = AuthProbe; state = AuthProbe;
PIByteArray ba; PIByteArray ba;
crypt.generateKeypair(my_pk, box_sk); crypt.generateKeypair(my_pk, box_sk);
PIByteArray noise = crypt.generateRandomBuff(randomi() % PIAUTH_NOISE_MAX_SIZE + 128); PIByteArray noise = crypt.generateRandomBuff(randomi()%PIAUTH_NOISE_MAX_SIZE+128);
ba << (int)state << custom_info << sign_pk << my_pk << noise; ba << (int)state << custom_info << sign_pk << my_pk << noise;
PIByteArray sign = crypt.signMessage(ba, sign_sk); PIByteArray sign = crypt.signMessage(ba, sign_sk);
ba << sign; ba << sign;
return ba; return ba;
} }
PIAuth::State PIAuth::receive(PIByteArray & ba) { PIAuth::State PIAuth::receive(PIByteArray & ba) {
if (ba.size() < sizeof(int)) return disconnect(ba, "invalid data size"); if (ba.size() < sizeof(int)) return disconnect(ba, "invalid data size");
State rstate; State rstate;
int s; int s;
ba >> s; ba >> s;
rstate = (State)s; rstate = (State)s;
// if (state != rstate) return disconect(ba); // if (state != rstate) return disconect(ba);
// client side //client side
if (role == Client) { if (role == Client) {
if (state == AuthProbe && rstate == AuthProbe) { if (state == AuthProbe && rstate == AuthProbe) {
if (ba.size() < sizeof(int) * 5) return disconnect(ba, "invalid data size"); if (ba.size() < sizeof(int)*5) return disconnect(ba, "invalid data size");
PIByteArray rinfo; PIByteArray rinfo;
PIByteArray rsign; PIByteArray rsign;
PIByteArray rsign_pk; PIByteArray rsign_pk;
PIByteArray noise; PIByteArray noise;
ba >> rinfo >> rsign_pk >> box_pk >> noise >> rsign; ba >> rinfo >> rsign_pk >> box_pk >> noise >> rsign;
if (rsign_pk.isEmpty() || box_pk.isEmpty() || rsign.isEmpty()) return disconnect(ba, "invalid key size"); if (rsign_pk.isEmpty() || box_pk.isEmpty() || rsign.isEmpty()) return disconnect(ba, "invalid key size");
PIByteArray tba; PIByteArray tba;
tba << (int)rstate << rinfo << rsign_pk << box_pk << noise; tba << (int)rstate << rinfo << rsign_pk << box_pk << noise;
if (!crypt.verifySign(tba, rsign, rsign_pk)) return disconnect(ba, "Incorrect sign"); if (!crypt.verifySign(tba, rsign, rsign_pk)) return disconnect(ba, "Incorrect sign");
bool auth = false; bool auth = false;
if (isAuthorizedKey(rsign_pk)) { if (isAuthorizedKey(rsign_pk)) {
auth = true; auth = true;
} else { } else {
authorize(rinfo, &auth); authorize(rinfo, &auth);
if (auth) auth_pkeys << rsign_pk; if (auth) auth_pkeys << rsign_pk;
} }
if (!auth) return disconnect(ba, "Unauthorised"); if (!auth) return disconnect(ba, "Unauthorised");
ba.clear(); ba.clear();
auth_sign = rsign_pk; auth_sign = rsign_pk;
crypt.generateKeypair(my_pk, box_sk); crypt.generateKeypair(my_pk, box_sk);
tba.clear(); tba.clear();
tba << sign_pk << my_pk << box_pk; tba << sign_pk << my_pk << box_pk;
PIByteArray sign = crypt.signMessage(tba, sign_sk); PIByteArray sign = crypt.signMessage(tba, sign_sk);
tba << sign; tba << sign;
tba = crypt.crypt(tba, box_pk, box_sk); tba = crypt.crypt(tba, box_pk, box_sk);
state = AuthReply; state = AuthReply;
noise = crypt.generateRandomBuff(randomi() % PIAUTH_NOISE_MAX_SIZE); noise = crypt.generateRandomBuff(randomi()%PIAUTH_NOISE_MAX_SIZE);
ba << (int)state << tba << my_pk << noise; ba << (int)state << tba << my_pk << noise;
sign = crypt.signMessage(ba, sign_sk); sign = crypt.signMessage(ba, sign_sk);
ba << sign; ba << sign;
return state; return state;
} }
if (state == AuthReply && rstate == PassRequest) { if (state == AuthReply && rstate == PassRequest) {
PIByteArray ctba, tba; PIByteArray ctba, tba;
PIByteArray noise; PIByteArray noise;
PIByteArray rsign, rsign_pk; PIByteArray rsign, rsign_pk;
ba >> ctba >> rsign; ba >> ctba >> rsign;
bool ok = false; bool ok = false;
tba = crypt.decrypt(ctba, box_pk, box_sk, &ok); tba = crypt.decrypt(ctba, box_pk, box_sk, &ok);
if (tba.isEmpty() || !ok) return disconnect(ba, "Message corrupted"); if (tba.isEmpty() || !ok) return disconnect(ba, "Message corrupted");
ba.clear(); ba.clear();
ba << (int)rstate << ctba; ba << (int)rstate << ctba;
if (!crypt.verifySign(ba, rsign, auth_sign)) return disconnect(ba, "Incorrect sign"); if (!crypt.verifySign(ba, rsign, auth_sign)) return disconnect(ba, "Incorrect sign");
ctba.clear(); ctba.clear();
tba >> rsign_pk >> noise >> ctba; tba >> rsign_pk >> noise >> ctba;
if (rsign_pk != auth_sign || ctba != my_pk) return disconnect(ba, "Invalid public key"); if (rsign_pk != auth_sign || ctba != my_pk) return disconnect(ba, "Invalid public key");
PIString ps; PIString ps;
PIByteArray ph; PIByteArray ph;
passwordRequest(&ps); passwordRequest(&ps);
if (ps.isEmpty()) return disconnect(ba, "Canceled by user"); if (ps.isEmpty()) return disconnect(ba, "Canceled by user");
ph = crypt.passwordHash(ps, PIString("PIAuth").toByteArray()); ph = crypt.passwordHash(ps, PIString("PIAuth").toByteArray());
ps.fill(PIChar()); ps.fill(PIChar());
tba.clear(); tba.clear();
tba << ph << auth_sign << sign_pk; tba << ph << auth_sign << sign_pk;
tba = crypt.crypt(tba, box_pk, box_sk); tba = crypt.crypt(tba, box_pk, box_sk);
ba.clear(); ba.clear();
state = PassRequest; state = PassRequest;
ba << (int)state << tba; ba << (int)state << tba;
rsign = crypt.signMessage(ba, sign_sk); rsign = crypt.signMessage(ba, sign_sk);
ba << rsign; ba << rsign;
return state; return state;
} }
if ((state == AuthReply && rstate == KeyExchange) || (state == PassRequest && rstate == KeyExchange)) { if ((state == AuthReply && rstate == KeyExchange) || (state == PassRequest && rstate == KeyExchange)) {
PIByteArray tba, ctba; PIByteArray tba, ctba;
PIByteArray rsign; PIByteArray rsign;
ba >> ctba >> rsign; ba >> ctba >> rsign;
bool ok = false; bool ok = false;
tba = crypt.decrypt(ctba, box_pk, box_sk, &ok); tba = crypt.decrypt(ctba, box_pk, box_sk, &ok);
if (tba.isEmpty() || !ok) return disconnect(ba, "Message corrupted"); if (tba.isEmpty() || !ok) return disconnect(ba, "Message corrupted");
ba.clear(); ba.clear();
ba << (int)rstate << ctba; ba << (int)rstate << ctba;
if (!crypt.verifySign(ba, rsign, auth_sign)) return disconnect(ba, "Incorrect sign"); if (!crypt.verifySign(ba, rsign, auth_sign)) return disconnect(ba, "Incorrect sign");
tba >> secret_key; tba >> secret_key;
if (secret_key.size() != crypt.sizeKey()) return disconnect(ba, "Invalid key"); if (secret_key.size() != crypt.sizeKey()) return disconnect(ba, "Invalid key");
ba.clear(); ba.clear();
state = Connected; state = Connected;
connected(PIString()); connected(PIString());
ba << (int)state << crypt.crypt(custom_info, secret_key) << crypt.generateRandomBuff(randomi() % PIAUTH_NOISE_MAX_SIZE); ba << (int)state << crypt.crypt(custom_info, secret_key) << crypt.generateRandomBuff(randomi()%PIAUTH_NOISE_MAX_SIZE);
return state; return state;
} }
if (state == Connected && rstate == Connected) { if (state == Connected && rstate == Connected) {
ba.clear(); ba.clear();
state = Connected; state = Connected;
connected(PIString()); connected(PIString());
ba << (int)state << crypt.crypt(custom_info, secret_key) << crypt.generateRandomBuff(randomi() % PIAUTH_NOISE_MAX_SIZE); ba << (int)state << crypt.crypt(custom_info, secret_key) << crypt.generateRandomBuff(randomi()%PIAUTH_NOISE_MAX_SIZE);
return state; return state;
} }
} }
// server side // server side
if (role == Server) { if (role == Server) {
if (state == AuthProbe && rstate == AuthReply) { if (state == AuthProbe && rstate == AuthReply) {
if (ba.size() < sizeof(int) * 4) return disconnect(ba, "invalid data size"); if (ba.size() < sizeof(int)*4) return disconnect(ba, "invalid data size");
PIByteArray ctba, tba; PIByteArray ctba, tba;
PIByteArray noise; PIByteArray noise;
PIByteArray rsign1, rsign2; PIByteArray rsign1, rsign2;
PIByteArray rsign_pk; PIByteArray rsign_pk;
PIByteArray pk, mpk; PIByteArray pk, mpk;
ba >> ctba >> pk >> noise >> rsign1; ba >> ctba >> pk >> noise >> rsign1;
bool ok = false; bool ok = false;
tba = crypt.decrypt(ctba, pk, box_sk, &ok); tba = crypt.decrypt(ctba, pk, box_sk, &ok);
if (tba.isEmpty() || !ok) return disconnect(ba, "Message corrupted"); if (tba.isEmpty() || !ok) return disconnect(ba, "Message corrupted");
if (tba.size() < sizeof(int) * 3) return disconnect(tba, "invalid data size"); if (tba.size() < sizeof(int)*3) return disconnect(tba, "invalid data size");
tba >> rsign_pk >> box_pk >> mpk >> rsign2; tba >> rsign_pk >> box_pk >> mpk >> rsign2;
if (pk != box_pk || mpk != my_pk) return disconnect(ba, "Invalid public key"); if (pk != box_pk || mpk != my_pk) return disconnect(ba, "Invalid public key");
ba.clear(); ba.clear();
ba << (int)rstate << ctba << box_pk << noise; ba << (int)rstate << ctba << box_pk << noise;
if (!crypt.verifySign(ba, rsign1, rsign_pk)) return disconnect(ba, "Incorrect sign"); if (!crypt.verifySign(ba, rsign1, rsign_pk)) return disconnect(ba, "Incorrect sign");
ba.clear(); ba.clear();
ba << rsign_pk << box_pk << my_pk; ba << rsign_pk << box_pk << my_pk;
if (!crypt.verifySign(ba, rsign2, rsign_pk)) return disconnect(ba, "Incorrect sign"); if (!crypt.verifySign(ba, rsign2, rsign_pk)) return disconnect(ba, "Incorrect sign");
auth_sign = rsign_pk; auth_sign = rsign_pk;
if (isAuthorizedKey(rsign_pk)) { if (isAuthorizedKey(rsign_pk)) {
state = KeyExchange; state = KeyExchange;
ba = createSKMessage(); ba = createSKMessage();
return state; return state;
} else { } else {
ba.clear(); ba.clear();
tba.clear(); tba.clear();
state = PassRequest; state = PassRequest;
noise = crypt.generateRandomBuff(randomi() % PIAUTH_NOISE_MAX_SIZE); noise = crypt.generateRandomBuff(randomi()%PIAUTH_NOISE_MAX_SIZE);
tba << sign_pk << noise << box_pk; tba << sign_pk << noise << box_pk;
tba = crypt.crypt(tba, box_pk, box_sk); tba = crypt.crypt(tba, box_pk, box_sk);
ba << (int)state << tba; ba << (int)state << tba;
rsign1 = crypt.signMessage(ba, sign_sk); rsign1 = crypt.signMessage(ba, sign_sk);
ba << rsign1; ba << rsign1;
return state; return state;
} }
} }
if (state == PassRequest && rstate == PassRequest) { if (state == PassRequest && rstate == PassRequest) {
PIByteArray tba, ctba; PIByteArray tba, ctba;
PIByteArray rsign_pk, rsign, mpk; PIByteArray rsign_pk, rsign, mpk;
ba >> ctba >> rsign; ba >> ctba >> rsign;
bool ok = false; bool ok = false;
tba = crypt.decrypt(ctba, box_pk, box_sk, &ok); tba = crypt.decrypt(ctba, box_pk, box_sk, &ok);
if (tba.isEmpty() || !ok) return disconnect(ba, "Message corrupted"); if (tba.isEmpty() || !ok) return disconnect(ba, "Message corrupted");
ba.clear(); ba.clear();
ba << (int)rstate << ctba; ba << (int)rstate << ctba;
if (!crypt.verifySign(ba, rsign, auth_sign)) return disconnect(ba, "Incorrect sign"); if (!crypt.verifySign(ba, rsign, auth_sign)) return disconnect(ba, "Incorrect sign");
ctba.clear(); ctba.clear();
tba >> ctba >> mpk >> rsign_pk; tba >> ctba >> mpk >> rsign_pk;
if (rsign_pk != auth_sign || mpk != sign_pk) return disconnect(ba, "Invalid public key"); if (rsign_pk != auth_sign || mpk != sign_pk) return disconnect(ba, "Invalid public key");
bool auth = (ctba == pass_hash); bool auth = (ctba == pass_hash);
if (ctba.isEmpty() || pass_hash.isEmpty()) auth = false; if (ctba.isEmpty() || pass_hash.isEmpty()) auth = false;
passwordCheck(auth); passwordCheck(auth);
if (!auth) { if (!auth) {
// piSleep(1); // piSleep(1);
return disconnect(ba, "Invalid password"); return disconnect(ba, "Invalid password");
} }
state = KeyExchange; state = KeyExchange;
ba = createSKMessage(); ba = createSKMessage();
return state; return state;
} }
if ((state == KeyExchange && rstate == Connected) || (state == Connected && rstate == Connected)) { if ((state == KeyExchange && rstate == Connected) || (state == Connected && rstate == Connected)) {
ba.clear(); ba.clear();
PIByteArray rinfo; PIByteArray rinfo;
ba >> rinfo; ba >> rinfo;
bool ok = false; bool ok = false;
rinfo = crypt.decrypt(rinfo, secret_key, &ok); rinfo = crypt.decrypt(rinfo, secret_key, &ok);
if (!ok) return disconnect(ba, "Error while exchange keys"); if (!ok) return disconnect(ba, "Error while exchange keys");
state = Connected; state = Connected;
connected(PIString::fromUTF8(rinfo)); connected(rinfo);
ba << (int)state << crypt.generateRandomBuff(randomi() % PIAUTH_NOISE_MAX_SIZE); ba << (int)state << crypt.generateRandomBuff(randomi()%PIAUTH_NOISE_MAX_SIZE);
return state; return state;
} }
} }
return disconnect(ba, "invalid state " + PIString::fromNumber((int)state)); return disconnect(ba, "invalid state " + PIString::fromNumber((int)state));
} }
PIByteArray PIAuth::getSecretKey() { PIByteArray PIAuth::getSecretKey() {
if (state == Connected) return secret_key; if (state == Connected) return secret_key;
return PIByteArray(); return PIByteArray();
} }
PIByteArray PIAuth::generateSign(const PIByteArray & seed) { PIByteArray PIAuth::generateSign(const PIByteArray & seed) {
PIByteArray pk, sk; PIByteArray pk, sk;
PICrypt::generateSignKeys(pk, sk, seed); PICrypt::generateSignKeys(pk, sk, seed);
return sk; return sk;
} }
PIAuth::State PIAuth::disconnect(PIByteArray & ba, const PIString & error) { PIAuth::State PIAuth::disconnect(PIByteArray & ba, const PIString & error) {
if (!error.isEmpty()) piCoutObj << error; if (!error.isEmpty()) piCoutObj << error;
auth_sign.clear(); auth_sign.clear();
box_sk.clear(); box_sk.clear();
box_pk.clear(); box_pk.clear();
my_pk.clear(); my_pk.clear();
secret_key.clear(); secret_key.clear();
ba.clear(); ba.clear();
state = NotConnected; state = NotConnected;
disconnected(error); disconnected(error);
return state; return state;
} }
bool PIAuth::isAuthorizedKey(const PIByteArray & pkey) { bool PIAuth::isAuthorizedKey(const PIByteArray & pkey) {
for (int i = 0; i < auth_pkeys.size_s(); ++i) { for (int i=0; i<auth_pkeys.size_s(); ++i) {
if (pkey == auth_pkeys[i]) return true; if (pkey == auth_pkeys[i]) return true;
} }
return false; return false;
} }
PIByteArray PIAuth::createSKMessage() { PIByteArray PIAuth::createSKMessage() {
secret_key = crypt.generateKey(); secret_key = crypt.generateKey();
PIByteArray tba; PIByteArray tba;
PIByteArray noise = crypt.generateRandomBuff(randomi() % PIAUTH_NOISE_MAX_SIZE); PIByteArray noise = crypt.generateRandomBuff(randomi()%PIAUTH_NOISE_MAX_SIZE);
tba << secret_key << noise; tba << secret_key << noise;
tba = crypt.crypt(tba, box_pk, box_sk); tba = crypt.crypt(tba, box_pk, box_sk);
PIByteArray ret; PIByteArray ret;
ret << (int)state << tba; ret << (int)state << tba;
PIByteArray sign = crypt.signMessage(ret, sign_sk); PIByteArray sign = crypt.signMessage(ret, sign_sk);
ret << sign; ret << sign;
return ret; return ret;
} }

View File

@@ -1,457 +1,448 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Cryptographic class using lib Sodium Cryptographic class using lib Sodium
Andrey Bychkov work.a.b@yandex.ru Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "picrypt.h" #include "picrypt.h"
#ifdef PIP_CRYPT #ifdef PIP_CRYPT
# include <sodium.h> # include <sodium.h>
#endif #endif
#define PICRYPT_DISABLED_WARNING \ #define PICRYPT_DISABLED_WARNING piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install sodium library and rebuild pip";
piCout << "[PICrypt]" \
<< "Warning: PICrypt is disabled, to enable install sodium library and rebuild pip"; const char hash_def_key[] = "_picrypt_\0\0\0\0\0\0\0";
const int hash_def_key_size = 9;
const char hash_def_key[] = "_picrypt_\0\0\0\0\0\0\0";
const int hash_def_key_size = 9;
PICrypt::PICrypt() {
#ifdef PIP_CRYPT
PICrypt::PICrypt() { if (!init()) piCout << "[PICrypt]" << "Error while initialize sodium!";
#ifdef PIP_CRYPT nonce_.resize(crypto_secretbox_NONCEBYTES);
if (!init()) key_.resize(crypto_secretbox_KEYBYTES);
piCout << "[PICrypt]" randombytes_buf(key_.data(), key_.size());
<< "Error while initialize sodium!"; randombytes_buf(nonce_.data(), nonce_.size());
nonce_.resize(crypto_secretbox_NONCEBYTES); #else
key_.resize(crypto_secretbox_KEYBYTES); PICRYPT_DISABLED_WARNING
randombytes_buf(key_.data(), key_.size()); #endif
randombytes_buf(nonce_.data(), nonce_.size()); }
#else
PICRYPT_DISABLED_WARNING
#endif bool PICrypt::setKey(const PIByteArray & _key) {
} if (_key.size() != key_.size()) return false;
key_ = _key;
return true;
bool PICrypt::setKey(const PIByteArray & _key) { }
if (_key.size() != key_.size()) return false;
key_ = _key;
return true; PIByteArray PICrypt::setKey(const PIString & secret) {
} PIByteArray hash;
#ifdef PIP_CRYPT
hash.resize(crypto_generichash_BYTES);
PIByteArray PICrypt::setKey(const PIString & secret) { PIByteArray s(secret.data(), secret.size());
PIByteArray hash; crypto_generichash(hash.data(), hash.size(), s.data(), s.size(), (const uchar*)hash_def_key, hash_def_key_size);
#ifdef PIP_CRYPT hash.resize(key_.size());
hash.resize(crypto_generichash_BYTES); setKey(hash);
PIByteArray s(secret.data(), secret.size()); #endif
crypto_generichash(hash.data(), hash.size(), s.data(), s.size(), (const uchar *)hash_def_key, hash_def_key_size); return hash;
hash.resize(key_.size()); }
setKey(hash);
#endif
return hash; PIByteArray PICrypt::crypt(const PIByteArray & data) {
} PIByteArray ret;
#ifdef PIP_CRYPT
ret.resize(data.size() + crypto_secretbox_MACBYTES);
PIByteArray PICrypt::crypt(const PIByteArray & data) { randombytes_buf(nonce_.data(), nonce_.size());
PIByteArray ret; crypto_secretbox_easy(ret.data(), data.data(), data.size(), nonce_.data(), key_.data());
#ifdef PIP_CRYPT ret.append(nonce_);
ret.resize(data.size() + crypto_secretbox_MACBYTES); #endif
randombytes_buf(nonce_.data(), nonce_.size()); return ret;
crypto_secretbox_easy(ret.data(), data.data(), data.size(), nonce_.data(), key_.data()); }
ret.append(nonce_);
#endif
return ret; PIByteArray PICrypt::crypt(const PIByteArray & data, PIByteArray key) {
} PIByteArray ret;
#ifdef PIP_CRYPT
if (key.size() != crypto_secretbox_KEYBYTES)
PIByteArray PICrypt::crypt(const PIByteArray & data, PIByteArray key) { key.resize(crypto_secretbox_KEYBYTES, ' ');
PIByteArray ret; //return PIByteArray();
#ifdef PIP_CRYPT if (!init()) return ret;
if (key.size() != crypto_secretbox_KEYBYTES) key.resize(crypto_secretbox_KEYBYTES, ' '); PIByteArray n;
// return PIByteArray(); ret.resize(data.size() + crypto_secretbox_MACBYTES);
if (!init()) return ret; n.resize(crypto_secretbox_NONCEBYTES);
PIByteArray n; randombytes_buf(n.data(), n.size());
ret.resize(data.size() + crypto_secretbox_MACBYTES); crypto_secretbox_easy(ret.data(), data.data(), data.size(), n.data(), key.data());
n.resize(crypto_secretbox_NONCEBYTES); ret.append(n);
randombytes_buf(n.data(), n.size()); #else
crypto_secretbox_easy(ret.data(), data.data(), data.size(), n.data(), key.data()); PICRYPT_DISABLED_WARNING
ret.append(n); #endif
#else return ret;
PICRYPT_DISABLED_WARNING }
#endif
return ret;
} PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, bool *ok) {
PIByteArray ret;
#ifdef PIP_CRYPT
PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, bool * ok) { if (crypt_data.size() < nonce_.size() + crypto_secretbox_MACBYTES) {
PIByteArray ret; if (ok) *ok = false;
#ifdef PIP_CRYPT return PIByteArray();
if (crypt_data.size() < nonce_.size() + crypto_secretbox_MACBYTES) { }
if (ok) *ok = false; ret.resize(crypt_data.size() - nonce_.size() - crypto_secretbox_MACBYTES);
return PIByteArray(); memcpy(nonce_.data(), crypt_data.data(crypt_data.size() - nonce_.size()), nonce_.size());
} if (crypto_secretbox_open_easy(ret.data(), crypt_data.data(), crypt_data.size() - nonce_.size(), nonce_.data(), key_.data()) != 0) {
ret.resize(crypt_data.size() - nonce_.size() - crypto_secretbox_MACBYTES); if (ok) *ok = false;
memcpy(nonce_.data(), crypt_data.data(crypt_data.size() - nonce_.size()), nonce_.size()); // piCout << "[PICrypt]" << "bad key_";
if (crypto_secretbox_open_easy(ret.data(), crypt_data.data(), crypt_data.size() - nonce_.size(), nonce_.data(), key_.data()) != 0) { return PIByteArray();
if (ok) *ok = false; }
// piCout << "[PICrypt]" << "bad key_"; #endif
return PIByteArray(); if (ok) *ok = true;
} return ret;
#endif }
if (ok) *ok = true;
return ret;
} PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, PIByteArray key, bool *ok) {
PIByteArray ret;
#ifdef PIP_CRYPT
PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, PIByteArray key, bool * ok) { if (key.size() != crypto_secretbox_KEYBYTES)
PIByteArray ret; key.resize(crypto_secretbox_KEYBYTES, ' ');
#ifdef PIP_CRYPT /*if (ok) *ok = false;
if (key.size() != crypto_secretbox_KEYBYTES) key.resize(crypto_secretbox_KEYBYTES, ' '); return PIByteArray();
/*if (ok) *ok = false; }*/
return PIByteArray(); if (crypt_data.size() < crypto_secretbox_NONCEBYTES + crypto_secretbox_MACBYTES) {
}*/ if (ok) *ok = false;
if (crypt_data.size() < crypto_secretbox_NONCEBYTES + crypto_secretbox_MACBYTES) { return PIByteArray();
if (ok) *ok = false; }
return PIByteArray(); if (!init()) return ret;
} PIByteArray n;
if (!init()) return ret; n.resize(crypto_secretbox_NONCEBYTES);
PIByteArray n; ret.resize(crypt_data.size() - n.size() - crypto_secretbox_MACBYTES);
n.resize(crypto_secretbox_NONCEBYTES); memcpy(n.data(), crypt_data.data(crypt_data.size() - n.size()), n.size());
ret.resize(crypt_data.size() - n.size() - crypto_secretbox_MACBYTES); if (crypto_secretbox_open_easy(ret.data(), crypt_data.data(), crypt_data.size() - n.size(), n.data(), key.data()) != 0) {
memcpy(n.data(), crypt_data.data(crypt_data.size() - n.size()), n.size()); if (ok) *ok = false;
if (crypto_secretbox_open_easy(ret.data(), crypt_data.data(), crypt_data.size() - n.size(), n.data(), key.data()) != 0) { // piCout << "[PICrypt]" << "bad key_";
if (ok) *ok = false; return PIByteArray();
// piCout << "[PICrypt]" << "bad key_"; } else if (ok) *ok = true;
return PIByteArray(); #else
} else if (ok) PICRYPT_DISABLED_WARNING
*ok = true; #endif
#else return ret;
PICRYPT_DISABLED_WARNING }
#endif
return ret;
} PIByteArray PICrypt::hash(const PIString & secret) {
PIByteArray hash;
#ifdef PIP_CRYPT
PIByteArray PICrypt::hash(const PIString & secret) { if (!init()) return hash;
PIByteArray hash; hash.resize(crypto_generichash_BYTES);
#ifdef PIP_CRYPT PIByteArray s(secret.data(), secret.size());
if (!init()) return hash; crypto_generichash(hash.data(), hash.size(), s.data(), s.size(),(const uchar*)hash_def_key, hash_def_key_size);
hash.resize(crypto_generichash_BYTES); #else
PIByteArray s(secret.data(), secret.size()); PICRYPT_DISABLED_WARNING
crypto_generichash(hash.data(), hash.size(), s.data(), s.size(), (const uchar *)hash_def_key, hash_def_key_size); #endif
#else return hash;
PICRYPT_DISABLED_WARNING }
#endif
return hash;
} PIByteArray PICrypt::hash(const PIByteArray & data) {
PIByteArray hash;
#ifdef PIP_CRYPT
PIByteArray PICrypt::hash(const PIByteArray & data) { if (!init()) return hash;
PIByteArray hash; hash.resize(crypto_generichash_BYTES);
#ifdef PIP_CRYPT crypto_generichash(hash.data(), hash.size(), data.data(), data.size(), (const uchar*)hash_def_key, hash_def_key_size);
if (!init()) return hash; #else
hash.resize(crypto_generichash_BYTES); PICRYPT_DISABLED_WARNING
crypto_generichash(hash.data(), hash.size(), data.data(), data.size(), (const uchar *)hash_def_key, hash_def_key_size); #endif
#else return hash;
PICRYPT_DISABLED_WARNING }
#endif
return hash;
} PIByteArray PICrypt::hash(const PIByteArray & data, const unsigned char *key, size_t keylen) {
PIByteArray hash;
#ifdef PIP_CRYPT
PIByteArray PICrypt::hash(const PIByteArray & data, const unsigned char * key, size_t keylen) { if (!init()) return hash;
PIByteArray hash; hash.resize(crypto_generichash_BYTES);
#ifdef PIP_CRYPT crypto_generichash(hash.data(), hash.size(), data.data(), data.size(), key, keylen);
if (!init()) return hash; #else
hash.resize(crypto_generichash_BYTES); PICRYPT_DISABLED_WARNING
crypto_generichash(hash.data(), hash.size(), data.data(), data.size(), key, keylen); #endif
#else return hash;
PICRYPT_DISABLED_WARNING }
#endif
return hash;
} size_t PICrypt::sizeHash() {
#ifdef PIP_CRYPT
return crypto_generichash_BYTES;
size_t PICrypt::sizeHash() { #else
#ifdef PIP_CRYPT PICRYPT_DISABLED_WARNING
return crypto_generichash_BYTES; #endif
#else return 0;
PICRYPT_DISABLED_WARNING }
#endif
return 0;
} ullong PICrypt::shorthash(const PIString& s, PIByteArray key) {
ullong hash = 0;
#ifdef PIP_CRYPT
ullong PICrypt::shorthash(const PIString & s, PIByteArray key) { if (crypto_shorthash_BYTES != sizeof(hash)) piCout << "[PICrypt]" << "internal error: bad hash size";
ullong hash = 0; if (!init()) return hash;
#ifdef PIP_CRYPT if (key.size() != crypto_shorthash_KEYBYTES) {
if (crypto_shorthash_BYTES != sizeof(hash)) piCout << "[PICrypt]" << "invalid key size" << key.size() << ", shoud be" << crypto_shorthash_KEYBYTES << ", filled zeros";
piCout << "[PICrypt]" key.resize(crypto_shorthash_KEYBYTES, 0);
<< "internal error: bad hash size"; }
if (!init()) return hash; PIByteArray in(s.data(), s.size());
if (key.size() != crypto_shorthash_KEYBYTES) { crypto_shorthash((uchar *)&hash, in.data(), in.size(), key.data());
piCout << "[PICrypt]" #else
<< "invalid key size" << key.size() << ", shoud be" << crypto_shorthash_KEYBYTES << ", filled zeros"; PICRYPT_DISABLED_WARNING
key.resize(crypto_shorthash_KEYBYTES, 0); #endif
} return hash;
PIByteArray in(s.data(), s.size()); }
crypto_shorthash((uchar *)&hash, in.data(), in.size(), key.data());
#else
PICRYPT_DISABLED_WARNING PIByteArray PICrypt::generateKey() {
#endif PIByteArray hash;
return hash; #ifdef PIP_CRYPT
} if (!init()) return hash;
hash.resize(crypto_secretbox_KEYBYTES);
randombytes_buf(hash.data(), hash.size());
PIByteArray PICrypt::generateKey() { #else
PIByteArray hash; PICRYPT_DISABLED_WARNING
#ifdef PIP_CRYPT #endif
if (!init()) return hash; return hash;
hash.resize(crypto_secretbox_KEYBYTES); }
randombytes_buf(hash.data(), hash.size());
#else
PICRYPT_DISABLED_WARNING PIByteArray PICrypt::generateRandomBuff(int size) {
#endif PIByteArray hash;
return hash; #ifdef PIP_CRYPT
} if (!init() || size <= 0) return hash;
hash.resize(size);
randombytes_buf(hash.data(), hash.size());
PIByteArray PICrypt::generateRandomBuff(int size) { #else
PIByteArray hash; PICRYPT_DISABLED_WARNING
#ifdef PIP_CRYPT #endif
if (!init() || size <= 0) return hash; return hash;
hash.resize(size); }
randombytes_buf(hash.data(), hash.size());
#else
PICRYPT_DISABLED_WARNING size_t PICrypt::sizeKey() {
#endif #ifdef PIP_CRYPT
return hash; return crypto_secretbox_KEYBYTES;
} #else
PICRYPT_DISABLED_WARNING
#endif
size_t PICrypt::sizeKey() { return 0;
#ifdef PIP_CRYPT }
return crypto_secretbox_KEYBYTES;
#else
PICRYPT_DISABLED_WARNING size_t PICrypt::sizeCrypt() {
#endif #ifdef PIP_CRYPT
return 0; return crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
} #else
PICRYPT_DISABLED_WARNING
#endif
size_t PICrypt::sizeCrypt() { return 0;
#ifdef PIP_CRYPT }
return crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
#else
PICRYPT_DISABLED_WARNING void PICrypt::generateSignKeys(PIByteArray & public_key, PIByteArray & secret_key) {
#endif #ifdef PIP_CRYPT
return 0; if (!init()) return;
} public_key.resize(crypto_sign_PUBLICKEYBYTES);
secret_key.resize(crypto_sign_SECRETKEYBYTES);
crypto_sign_keypair(public_key.data(), secret_key.data());
void PICrypt::generateSignKeys(PIByteArray & public_key, PIByteArray & secret_key) { #else
#ifdef PIP_CRYPT PICRYPT_DISABLED_WARNING
if (!init()) return; #endif
public_key.resize(crypto_sign_PUBLICKEYBYTES); }
secret_key.resize(crypto_sign_SECRETKEYBYTES);
crypto_sign_keypair(public_key.data(), secret_key.data());
#else void PICrypt::generateSignKeys(PIByteArray & public_key, PIByteArray & secret_key, const PIByteArray & seed) {
PICRYPT_DISABLED_WARNING #ifdef PIP_CRYPT
#endif if (!init() || seed.isEmpty()) return;
} public_key.resize(crypto_sign_PUBLICKEYBYTES);
secret_key.resize(crypto_sign_SECRETKEYBYTES);
crypto_sign_seed_keypair(public_key.data(), secret_key.data(), hash(seed).data());
void PICrypt::generateSignKeys(PIByteArray & public_key, PIByteArray & secret_key, const PIByteArray & seed) { #else
#ifdef PIP_CRYPT PICRYPT_DISABLED_WARNING
if (!init() || seed.isEmpty()) return; #endif
public_key.resize(crypto_sign_PUBLICKEYBYTES); }
secret_key.resize(crypto_sign_SECRETKEYBYTES);
crypto_sign_seed_keypair(public_key.data(), secret_key.data(), hash(seed).data());
#else PIByteArray PICrypt::extractSignPublicKey(const PIByteArray & secret_key) {
PICRYPT_DISABLED_WARNING PIByteArray pk;
#endif #ifdef PIP_CRYPT
} if (!init() || secret_key.size() != crypto_sign_SECRETKEYBYTES) return pk;
pk.resize(crypto_sign_PUBLICKEYBYTES);
crypto_sign_ed25519_sk_to_pk(pk.data(), secret_key.data());
PIByteArray PICrypt::extractSignPublicKey(const PIByteArray & secret_key) { #else
PIByteArray pk; PICRYPT_DISABLED_WARNING
#ifdef PIP_CRYPT #endif
if (!init() || secret_key.size() != crypto_sign_SECRETKEYBYTES) return pk; return pk;
pk.resize(crypto_sign_PUBLICKEYBYTES); }
crypto_sign_ed25519_sk_to_pk(pk.data(), secret_key.data());
#else
PICRYPT_DISABLED_WARNING PIByteArray PICrypt::signMessage(const PIByteArray & data, PIByteArray secret_key) {
#endif PIByteArray sign;
return pk; #ifdef PIP_CRYPT
} if (!init()) return sign;
sign.resize(crypto_sign_BYTES);
crypto_sign_detached(sign.data(), 0, data.data(), data.size(), secret_key.data());
PIByteArray PICrypt::signMessage(const PIByteArray & data, PIByteArray secret_key) { #else
PIByteArray sign; PICRYPT_DISABLED_WARNING
#ifdef PIP_CRYPT #endif
if (!init()) return sign; return sign;
sign.resize(crypto_sign_BYTES); }
crypto_sign_detached(sign.data(), 0, data.data(), data.size(), secret_key.data());
#else
PICRYPT_DISABLED_WARNING bool PICrypt::verifySign(const PIByteArray & data, const PIByteArray & signature, PIByteArray public_key) {
#endif #ifdef PIP_CRYPT
return sign; if (!init()) return false;
} return (crypto_sign_verify_detached(signature.data(), data.data(), data.size(), public_key.data()) == 0);
#else
PICRYPT_DISABLED_WARNING
bool PICrypt::verifySign(const PIByteArray & data, const PIByteArray & signature, PIByteArray public_key) { #endif
#ifdef PIP_CRYPT return false;
if (!init()) return false; }
return (crypto_sign_verify_detached(signature.data(), data.data(), data.size(), public_key.data()) == 0);
#else
PICRYPT_DISABLED_WARNING void PICrypt::generateKeypair(PIByteArray & public_key, PIByteArray & secret_key) {
#endif #ifdef PIP_CRYPT
return false; if (!init()) return;
} public_key.resize(crypto_box_PUBLICKEYBYTES);
secret_key.resize(crypto_box_SECRETKEYBYTES);
crypto_box_keypair(public_key.data(), secret_key.data());
void PICrypt::generateKeypair(PIByteArray & public_key, PIByteArray & secret_key) { #else
#ifdef PIP_CRYPT PICRYPT_DISABLED_WARNING
if (!init()) return; #endif
public_key.resize(crypto_box_PUBLICKEYBYTES); }
secret_key.resize(crypto_box_SECRETKEYBYTES);
crypto_box_keypair(public_key.data(), secret_key.data());
#else void PICrypt::generateKeypair(PIByteArray & public_key, PIByteArray & secret_key, const PIByteArray & seed) {
PICRYPT_DISABLED_WARNING #ifdef PIP_CRYPT
#endif if (!init()) return;
} public_key.resize(crypto_box_PUBLICKEYBYTES);
secret_key.resize(crypto_box_SECRETKEYBYTES);
crypto_box_seed_keypair(public_key.data(), secret_key.data(), hash(seed).data());
void PICrypt::generateKeypair(PIByteArray & public_key, PIByteArray & secret_key, const PIByteArray & seed) { #else
#ifdef PIP_CRYPT PICRYPT_DISABLED_WARNING
if (!init()) return; #endif
public_key.resize(crypto_box_PUBLICKEYBYTES); }
secret_key.resize(crypto_box_SECRETKEYBYTES);
crypto_box_seed_keypair(public_key.data(), secret_key.data(), hash(seed).data());
#else PIByteArray PICrypt::crypt(const PIByteArray & data, const PIByteArray & public_key, const PIByteArray & secret_key) {
PICRYPT_DISABLED_WARNING PIByteArray ret;
#endif #ifdef PIP_CRYPT
} if (!init()) return ret;
if (public_key.size() != crypto_box_PUBLICKEYBYTES)
return ret;
PIByteArray PICrypt::crypt(const PIByteArray & data, const PIByteArray & public_key, const PIByteArray & secret_key) { if (secret_key.size() != crypto_box_SECRETKEYBYTES)
PIByteArray ret; return ret;
#ifdef PIP_CRYPT PIByteArray n;
if (!init()) return ret; ret.resize(data.size() + crypto_box_MACBYTES);
if (public_key.size() != crypto_box_PUBLICKEYBYTES) return ret; n.resize(crypto_box_NONCEBYTES);
if (secret_key.size() != crypto_box_SECRETKEYBYTES) return ret; randombytes_buf(n.data(), n.size());
PIByteArray n; if (crypto_box_easy(ret.data(), data.data(), data.size(), n.data(), public_key.data(), secret_key.data()) != 0)
ret.resize(data.size() + crypto_box_MACBYTES); return PIByteArray();
n.resize(crypto_box_NONCEBYTES); ret.append(n);
randombytes_buf(n.data(), n.size()); #else
if (crypto_box_easy(ret.data(), data.data(), data.size(), n.data(), public_key.data(), secret_key.data()) != 0) return PIByteArray(); PICRYPT_DISABLED_WARNING
ret.append(n); #endif
#else return ret;
PICRYPT_DISABLED_WARNING }
#endif
return ret;
} PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, const PIByteArray & public_key, const PIByteArray & secret_key, bool * ok) {
PIByteArray ret;
#ifdef PIP_CRYPT
PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, const PIByteArray & public_key, const PIByteArray & secret_key, bool * ok) { if (!init()) return ret;
PIByteArray ret; if (public_key.size() != crypto_box_PUBLICKEYBYTES) {
#ifdef PIP_CRYPT if (ok) *ok = false;
if (!init()) return ret; return ret;
if (public_key.size() != crypto_box_PUBLICKEYBYTES) { }
if (ok) *ok = false; if (secret_key.size() != crypto_box_SECRETKEYBYTES) {
return ret; if (ok) *ok = false;
} return ret;
if (secret_key.size() != crypto_box_SECRETKEYBYTES) { }
if (ok) *ok = false; if (crypt_data.size() < crypto_box_NONCEBYTES + crypto_box_MACBYTES) {
return ret; if (ok) *ok = false;
} return ret;
if (crypt_data.size() < crypto_box_NONCEBYTES + crypto_box_MACBYTES) { }
if (ok) *ok = false; PIByteArray n;
return ret; n.resize(crypto_secretbox_NONCEBYTES);
} ret.resize(crypt_data.size() - n.size() - crypto_secretbox_MACBYTES);
PIByteArray n; memcpy(n.data(), crypt_data.data(crypt_data.size() - n.size()), n.size());
n.resize(crypto_secretbox_NONCEBYTES); if (crypto_box_open_easy(ret.data(), crypt_data.data(), crypt_data.size() - n.size(), n.data(), public_key.data(), secret_key.data()) != 0) {
ret.resize(crypt_data.size() - n.size() - crypto_secretbox_MACBYTES); if (ok) *ok = false;
memcpy(n.data(), crypt_data.data(crypt_data.size() - n.size()), n.size()); // piCout << "[PICrypt]" << "bad key_";
if (crypto_box_open_easy(ret.data(), crypt_data.data(), crypt_data.size() - n.size(), n.data(), public_key.data(), secret_key.data()) != return PIByteArray();
0) { } else if (ok) *ok = true;
if (ok) *ok = false; #else
// piCout << "[PICrypt]" << "bad key_"; PICRYPT_DISABLED_WARNING
return PIByteArray(); #endif
} else if (ok) return ret;
*ok = true; }
#else
PICRYPT_DISABLED_WARNING
#endif PIByteArray PICrypt::passwordHash(const PIString & password, const PIByteArray & seed) {
return ret; #ifdef crypto_pwhash_ALG_ARGON2I13
} // char out[crypto_pwhash_STRBYTES];
PIByteArray pass = password.toUTF8();
PIByteArray n = hash(seed);
PIByteArray PICrypt::passwordHash(const PIString & password, const PIByteArray & seed) { PIByteArray ph;
#ifdef crypto_pwhash_ALG_ARGON2I13 ph.resize(crypto_box_SEEDBYTES);
// char out[crypto_pwhash_STRBYTES]; n.resize(crypto_pwhash_SALTBYTES);
PIByteArray pass = password.toUTF8(); // randombytes_buf(n.data(), n.size());
PIByteArray n = hash(seed); // crypto_shorthash(n.data(), seed.data(), seed.size(), PIByteArray(crypto_shorthash_KEYBYTES).data());
PIByteArray ph; int r = crypto_pwhash(ph.data(), ph.size(), (const char*)pass.data(), pass.size(), n.data(), crypto_pwhash_argon2i_opslimit_moderate(), crypto_pwhash_argon2i_memlimit_moderate(), crypto_pwhash_ALG_ARGON2I13);
ph.resize(crypto_box_SEEDBYTES); //crypto_pwhash_str(out, (const char*)pass.data(), pass.size(), crypto_pwhash_argon2i_opslimit_moderate(), crypto_pwhash_argon2i_memlimit_moderate());
n.resize(crypto_pwhash_SALTBYTES); pass.fill(0);
// randombytes_buf(n.data(), n.size()); if (r != 0) return PIByteArray();
// crypto_shorthash(n.data(), seed.data(), seed.size(), PIByteArray(crypto_shorthash_KEYBYTES).data()); return ph;
int r = crypto_pwhash(ph.data(), // PIByteArray ret;
ph.size(), // ret << ph << n << crypto_pwhash_argon2i_opslimit_moderate() << crypto_pwhash_argon2i_memlimit_moderate();
(const char *)pass.data(), // return ret;
pass.size(), #else
n.data(), return PIByteArray();
crypto_pwhash_argon2i_opslimit_moderate(), #endif
crypto_pwhash_argon2i_memlimit_moderate(), }
crypto_pwhash_ALG_ARGON2I13);
// crypto_pwhash_str(out, (const char*)pass.data(), pass.size(), crypto_pwhash_argon2i_opslimit_moderate(),
// crypto_pwhash_argon2i_memlimit_moderate()); PIString PICrypt::version() {
pass.fill(0); #ifdef PIP_CRYPT
if (r != 0) return PIByteArray(); return SODIUM_VERSION_STRING;
return ph; #else
// PIByteArray ret; return PIString();
// ret << ph << n << crypto_pwhash_argon2i_opslimit_moderate() << crypto_pwhash_argon2i_memlimit_moderate(); #endif
// return ret; }
#else
return PIByteArray();
#endif bool PICrypt::init() {
} #ifdef PIP_CRYPT
static bool inited = false;
if (inited) return true;
PIString PICrypt::version() { //piCout << "[PICrypt]" << "init ...";
#ifdef PIP_CRYPT inited = sodium_init();
return SODIUM_VERSION_STRING; if (!inited)
#else inited = sodium_init();
return PIString(); //piCout << "[PICrypt]" << "init" << inited;
#endif return inited;
} #else
PICRYPT_DISABLED_WARNING
#endif
bool PICrypt::init() { return false;
#ifdef PIP_CRYPT }
static bool inited = false;
if (inited) return true;
// piCout << "[PICrypt]" << "init ...";
inited = sodium_init();
if (!inited) inited = sodium_init();
// piCout << "[PICrypt]" << "init" << inited;
return inited;
#else
PICRYPT_DISABLED_WARNING
#endif
return false;
}

View File

@@ -1,48 +1,34 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Class for FFT, IFFT and Hilbert transformations Class for FFT, IFFT and Hilbert transformations
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "pifft.h" #include "pifft.h"
#include "pifft_p.h"
#include "pifft_p.h"
#define _PIFFTW_CPP(type) \
#define _PIFFTW_CPP(type) \ _PIFFTW_P_##type##_::_PIFFTW_P_##type##_() {impl = new PIFFTW_Private<type>();;} \
_PIFFTW_P_##type##_::_PIFFTW_P_##type##_() { \ _PIFFTW_P_##type##_::~_PIFFTW_P_##type##_() {delete (PIFFTW_Private<type>*)impl;} \
impl = new PIFFTW_Private<type>(); \ const PIVector<complex<type> > & _PIFFTW_P_##type##_::calcFFT(const PIVector<complex<type> > & in) {return ((PIFFTW_Private<type>*)impl)->calcFFT(in);} \
; \ const PIVector<complex<type> > & _PIFFTW_P_##type##_::calcFFTR(const PIVector<type> & in) {return ((PIFFTW_Private<type>*)impl)->calcFFT(in);} \
} \ const PIVector<complex<type> > & _PIFFTW_P_##type##_::calcFFTI(const PIVector<complex<type> > & in) {return ((PIFFTW_Private<type>*)impl)->calcFFTinverse(in);} \
_PIFFTW_P_##type##_::~_PIFFTW_P_##type##_() { \ void _PIFFTW_P_##type##_::preparePlan(int size, int op) {return ((PIFFTW_Private<type>*)impl)->preparePlan(size, op);}
delete (PIFFTW_Private<type> *)impl; \
} \ _PIFFTW_CPP(float)
const PIVector<complex<type>> & _PIFFTW_P_##type##_::calcFFT(const PIVector<complex<type>> & in) { \ _PIFFTW_CPP(double)
return ((PIFFTW_Private<type> *)impl)->calcFFT(in); \ _PIFFTW_CPP(ldouble)
} \
const PIVector<complex<type>> & _PIFFTW_P_##type##_::calcFFTR(const PIVector<type> & in) { \
return ((PIFFTW_Private<type> *)impl)->calcFFT(in); \
} \
const PIVector<complex<type>> & _PIFFTW_P_##type##_::calcFFTI(const PIVector<complex<type>> & in) { \
return ((PIFFTW_Private<type> *)impl)->calcFFTinverse(in); \
} \
void _PIFFTW_P_##type##_::preparePlan(int size, int op) { \
return ((PIFFTW_Private<type> *)impl)->preparePlan(size, op); \
}
_PIFFTW_CPP(float)
_PIFFTW_CPP(double)
_PIFFTW_CPP(ldouble)

View File

@@ -1,247 +1,180 @@
/*! \file pifft_p.h /*! \file pifft_p.h
* \brief Class for FFT, IFFT and Hilbert transformations * \brief Class for FFT, IFFT and Hilbert transformations
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Private header for fftw3 Private header for fftw3
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PIFFT_P_H #ifndef PIFFT_P_H
#define PIFFT_P_H #define PIFFT_P_H
#include "picout.h" #include "pivector.h"
#include "pivector.h" #include "picout.h"
#if defined(PIP_FFTW) || defined(PIP_FFTWf) || defined(PIP_FFTWl) || defined(PIP_FFTWq) #if defined(PIP_FFTW) || defined(PIP_FFTWf) || defined(PIP_FFTWl) || defined(PIP_FFTWq)
# include "fftw3.h" # include "fftw3.h"
#else #else
# define FFTW_FORWARD 0 # define FFTW_FORWARD 0
# define FFTW_BACKWARD 0 # define FFTW_BACKWARD 0
# define FFTW_ESTIMATE 0 # define FFTW_ESTIMATE 0
# define FFTW_MEASURE 0 # define FFTW_MEASURE 0
#endif #endif
template<typename T> template <typename T>
class PIFFTW_Private { class PIFFTW_Private
public: {
explicit PIFFTW_Private() { public:
plan = 0; explicit PIFFTW_Private() {
// #ifndef PIP_FFTW plan = 0;
// piCout << "[PIFFTW]" << "Warning: PIFFTW is disabled, to enable install libfftw3-dev library and build pip with -DFFTW=1"; //#ifndef PIP_FFTW
// #endif // piCout << "[PIFFTW]" << "Warning: PIFFTW is disabled, to enable install libfftw3-dev library and build pip with -DFFTW=1";
p_makeThreadSafe(); //#endif
} p_makeThreadSafe();
~PIFFTW_Private() { p_destroyPlan(plan); } }
~PIFFTW_Private() {p_destroyPlan(plan);}
const PIVector<complex<T>> & calcFFT(const PIVector<complex<T>> & in) {
if (prepare != PlanParams(in.size(), fo_complex)) { const PIVector<complex<T> > & calcFFT(const PIVector<complex<T> > & in) {
p_out.resize(in.size()); if (prepare != PlanParams(in.size(), fo_complex)) {
// piCout << "[PIFFTW]" << "creating plan"; p_out.resize(in.size());
p_createPlan_c2c_1d(plan, in.size(), in.data(), p_out.data(), FFTW_FORWARD, FFTW_ESTIMATE | FFTW_UNALIGNED); piCout << "[PIFFTW]" << "creating plan";
prepare = PlanParams(in.size(), fo_complex); p_createPlan_c2c_1d(plan, in.size(), in.data(), p_out.data(), FFTW_FORWARD, FFTW_ESTIMATE | FFTW_UNALIGNED);
} prepare = PlanParams(in.size(), fo_complex);
p_executePlan_c2c(plan, in.data(), p_out.data()); }
return p_out; p_executePlan_c2c(plan, in.data(), p_out.data());
} return p_out;
const PIVector<complex<T>> & calcFFT(const PIVector<T> & in) { }
if (prepare != PlanParams(in.size(), fo_real)) { const PIVector<complex<T> > & calcFFT(const PIVector<T> & in) {
p_out.resize(in.size()); if (prepare != PlanParams(in.size(), fo_real)) {
// piCout << "[PIFFTW]" << "creating plan"; p_out.resize(in.size());
p_createPlan_r2c_1d(plan, in.size(), in.data(), p_out.data(), FFTW_ESTIMATE | FFTW_UNALIGNED); piCout << "[PIFFTW]" << "creating plan";
prepare = PlanParams(in.size(), fo_real); p_createPlan_r2c_1d(plan, in.size(), in.data(), p_out.data(), FFTW_ESTIMATE | FFTW_UNALIGNED);
} prepare = PlanParams(in.size(), fo_real);
p_executePlan_r2c(plan, in.data(), p_out.data()); }
return p_out; p_executePlan_r2c(plan, in.data(), p_out.data());
} return p_out;
const PIVector<complex<T>> & calcFFTinverse(const PIVector<complex<T>> & in) { }
if (prepare != PlanParams(in.size(), fo_inverse)) { const PIVector<complex<T> > & calcFFTinverse(const PIVector<complex<T> > & in) {
p_out.resize(in.size()); if (prepare != PlanParams(in.size(), fo_inverse)) {
// piCout << "[PIFFTW]" << "creating plan"; p_out.resize(in.size());
p_createPlan_c2c_1d(plan, in.size(), in.data(), p_out.data(), FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_UNALIGNED); piCout << "[PIFFTW]" << "creating plan";
prepare = PlanParams(in.size(), fo_inverse); p_createPlan_c2c_1d(plan, in.size(), in.data(), p_out.data(), FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_UNALIGNED);
} prepare = PlanParams(in.size(), fo_inverse);
p_executePlan_c2c(plan, in.data(), p_out.data()); }
return p_out; p_executePlan_c2c(plan, in.data(), p_out.data());
} return p_out;
}
enum FFT_Operation {
fo_real, enum FFT_Operation {fo_real, fo_complex, fo_inverse};
fo_complex,
fo_inverse void preparePlan(int size, int op) {
}; p_inr.clear();
p_in.clear();
void preparePlan(int size, int op) { p_out.clear();
p_inr.clear(); switch ((FFT_Operation)op) {
p_in.clear(); case fo_real:
p_out.clear(); p_inr.resize(size);
switch ((FFT_Operation)op) { p_out.resize(size);
case fo_real: p_createPlan_r2c_1d(plan, size, p_inr.data(), p_out.data(), FFTW_MEASURE | FFTW_UNALIGNED);
p_inr.resize(size); break;
p_out.resize(size); case fo_complex:
p_createPlan_r2c_1d(plan, size, p_inr.data(), p_out.data(), FFTW_MEASURE | FFTW_UNALIGNED); p_in.resize(size);
break; p_out.resize(size);
case fo_complex: p_createPlan_c2c_1d(plan, size, p_in.data(), p_out.data(), FFTW_FORWARD, FFTW_MEASURE | FFTW_UNALIGNED);
p_in.resize(size); break;
p_out.resize(size); case fo_inverse:
p_createPlan_c2c_1d(plan, size, p_in.data(), p_out.data(), FFTW_FORWARD, FFTW_MEASURE | FFTW_UNALIGNED); p_in.resize(size);
break; p_out.resize(size);
case fo_inverse: p_createPlan_c2c_1d(plan, size, p_in.data(), p_out.data(), FFTW_BACKWARD, FFTW_MEASURE | FFTW_UNALIGNED);
p_in.resize(size); break;
p_out.resize(size); default:
p_createPlan_c2c_1d(plan, size, p_in.data(), p_out.data(), FFTW_BACKWARD, FFTW_MEASURE | FFTW_UNALIGNED); size = 0;
break; break;
default: size = 0; break; }
} prepare = PlanParams(size, (FFT_Operation)op);
prepare = PlanParams(size, (FFT_Operation)op); }
}
inline void p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) {}
inline void p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) {} inline void p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {}
inline void p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {} inline void p_executePlan(void * plan) {}
inline void p_executePlan(void * plan) {} inline void p_executePlan_c2c(void * plan, const void * in, void * out) {}
inline void p_executePlan_c2c(void * plan, const void * in, void * out) {} inline void p_executePlan_r2c(void * plan, const void * in, void * out) {}
inline void p_executePlan_r2c(void * plan, const void * in, void * out) {} inline void p_destroyPlan(void *& plan) {}
inline void p_destroyPlan(void *& plan) {} inline void p_makeThreadSafe() {}
inline void p_makeThreadSafe() {}
struct PlanParams {
struct PlanParams { PlanParams() {size = 0; op = fo_complex;}
PlanParams() { PlanParams(int size_, FFT_Operation op_) {size = size_; op = op_;}
size = 0; bool isValid() {return size > 0;}
op = fo_complex; bool operator ==(const PlanParams & v) const {return (v.size == size) && (v.op == op);}
} bool operator !=(const PlanParams & v) const {return !(*this == v);}
PlanParams(int size_, FFT_Operation op_) { int size;
size = size_; FFT_Operation op;
op = op_; };
}
bool isValid() { return size > 0; } PIVector<complex<T> > p_in;
bool operator==(const PlanParams & v) const { return (v.size == size) && (v.op == op); } PIVector<T> p_inr;
bool operator!=(const PlanParams & v) const { return !(*this == v); } PIVector<complex<T> > p_out;
int size; void * plan;
FFT_Operation op; PlanParams prepare;
}; };
PIVector<complex<T>> p_in;
PIVector<T> p_inr; #ifdef PIP_FFTWf
PIVector<complex<T>> p_out; template<> inline void PIFFTW_Private<float>::p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) {
void * plan; plan = fftwf_plan_dft_1d(size, (fftwf_complex *)in, (fftwf_complex *)out, dir, flags);}
PlanParams prepare; template<> inline void PIFFTW_Private<float>::p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {
}; plan = fftwf_plan_dft_r2c_1d(size, (float *)in, (fftwf_complex *)out, flags);}
template<> inline void PIFFTW_Private<float>::p_executePlan(void * plan) {fftwf_execute((fftwf_plan)plan);}
template<> inline void PIFFTW_Private<float>::p_executePlan_c2c(void * plan, const void * in, void * out) {fftwf_execute_dft((fftwf_plan)plan, (fftwf_complex *)in, (fftwf_complex *)out);}
#ifdef PIP_FFTWf template<> inline void PIFFTW_Private<float>::p_executePlan_r2c(void * plan, const void * in, void * out) {fftwf_execute_dft_r2c((fftwf_plan)plan, (float *)in, (fftwf_complex *)out);}
template<> template<> inline void PIFFTW_Private<float>::p_destroyPlan(void *& plan) {if (plan) fftwf_destroy_plan((fftwf_plan)plan); plan = 0;}
inline void PIFFTW_Private<float>::p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) { # ifdef PIP_FFTWf_THREADSAFE
plan = fftwf_plan_dft_1d(size, (fftwf_complex *)in, (fftwf_complex *)out, dir, flags); template<> inline void PIFFTW_Private<float>::p_makeThreadSafe() {fftwf_make_planner_thread_safe();}
} # endif
template<> #endif // PIP_FFTWf
inline void PIFFTW_Private<float>::p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {
plan = fftwf_plan_dft_r2c_1d(size, (float *)in, (fftwf_complex *)out, flags); #ifdef PIP_FFTW
} template<> inline void PIFFTW_Private<double>::p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) {
template<> plan = fftw_plan_dft_1d(size, (fftw_complex *)in, (fftw_complex *)out, dir, flags);}
inline void PIFFTW_Private<float>::p_executePlan(void * plan) { template<> inline void PIFFTW_Private<double>::p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {
fftwf_execute((fftwf_plan)plan); plan = fftw_plan_dft_r2c_1d(size, (double *)in, (fftw_complex *)out, flags);}
} template<> inline void PIFFTW_Private<double>::p_executePlan(void * plan) {fftw_execute((fftw_plan)plan);}
template<> template<> inline void PIFFTW_Private<double>::p_executePlan_c2c(void * plan, const void * in, void * out) {fftw_execute_dft((fftw_plan)plan, (fftw_complex *)in, (fftw_complex *)out);}
inline void PIFFTW_Private<float>::p_executePlan_c2c(void * plan, const void * in, void * out) { template<> inline void PIFFTW_Private<double>::p_executePlan_r2c(void * plan, const void * in, void * out) {fftw_execute_dft_r2c((fftw_plan)plan, (double *)in, (fftw_complex *)out);}
fftwf_execute_dft((fftwf_plan)plan, (fftwf_complex *)in, (fftwf_complex *)out); template<> inline void PIFFTW_Private<double>::p_destroyPlan(void *& plan) {if (plan) fftw_destroy_plan((fftw_plan)plan); plan = 0;}
} # ifdef PIP_FFTW_THREADSAFE
template<> template<> inline void PIFFTW_Private<double>::p_makeThreadSafe() {fftw_make_planner_thread_safe();}
inline void PIFFTW_Private<float>::p_executePlan_r2c(void * plan, const void * in, void * out) { # endif
fftwf_execute_dft_r2c((fftwf_plan)plan, (float *)in, (fftwf_complex *)out); #endif // PIP_FFTW
}
template<> #ifdef PIP_FFTWl
inline void PIFFTW_Private<float>::p_destroyPlan(void *& plan) { template<> inline void PIFFTW_Private<ldouble>::p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) {
if (plan) fftwf_destroy_plan((fftwf_plan)plan); plan = fftwl_plan_dft_1d(size, (fftwl_complex *)in, (fftwl_complex *)out, dir, flags);}
plan = 0; template<> inline void PIFFTW_Private<ldouble>::p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {
} plan = fftwl_plan_dft_r2c_1d(size, (ldouble *)in, (fftwl_complex *)out, flags);}
# ifdef PIP_FFTWf_THREADSAFE template<> inline void PIFFTW_Private<ldouble>::p_executePlan(void * plan) {fftwl_execute((fftwl_plan)plan);}
template<> template<> inline void PIFFTW_Private<ldouble>::p_executePlan_c2c(void * plan, const void * in, void * out) {fftwl_execute_dft((fftwl_plan)plan, (fftwl_complex *)in, (fftwl_complex *)out);}
inline void PIFFTW_Private<float>::p_makeThreadSafe() { template<> inline void PIFFTW_Private<ldouble>::p_executePlan_r2c(void * plan, const void * in, void * out) {fftwl_execute_dft_r2c((fftwl_plan)plan, (ldouble *)in, (fftwl_complex *)out);}
fftwf_make_planner_thread_safe(); template<> inline void PIFFTW_Private<ldouble>::p_destroyPlan(void *& plan) {if (plan) fftwl_destroy_plan((fftwl_plan)plan); plan = 0;}
} # ifdef PIP_FFTWl_THREADSAFE
# endif template<> inline void PIFFTW_Private<ldouble>::p_makeThreadSafe() {fftwl_make_planner_thread_safe();}
#endif // PIP_FFTWf # endif
#endif // PIP_FFTWl
#ifdef PIP_FFTW
template<>
inline void PIFFTW_Private<double>::p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) { #endif // PIFFT_H
plan = fftw_plan_dft_1d(size, (fftw_complex *)in, (fftw_complex *)out, dir, flags);
}
template<>
inline void PIFFTW_Private<double>::p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {
plan = fftw_plan_dft_r2c_1d(size, (double *)in, (fftw_complex *)out, flags);
}
template<>
inline void PIFFTW_Private<double>::p_executePlan(void * plan) {
fftw_execute((fftw_plan)plan);
}
template<>
inline void PIFFTW_Private<double>::p_executePlan_c2c(void * plan, const void * in, void * out) {
fftw_execute_dft((fftw_plan)plan, (fftw_complex *)in, (fftw_complex *)out);
}
template<>
inline void PIFFTW_Private<double>::p_executePlan_r2c(void * plan, const void * in, void * out) {
fftw_execute_dft_r2c((fftw_plan)plan, (double *)in, (fftw_complex *)out);
}
template<>
inline void PIFFTW_Private<double>::p_destroyPlan(void *& plan) {
if (plan) fftw_destroy_plan((fftw_plan)plan);
plan = 0;
}
# ifdef PIP_FFTW_THREADSAFE
template<>
inline void PIFFTW_Private<double>::p_makeThreadSafe() {
fftw_make_planner_thread_safe();
}
# endif
#endif // PIP_FFTW
#ifdef PIP_FFTWl
template<>
inline void PIFFTW_Private<ldouble>::p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) {
plan = fftwl_plan_dft_1d(size, (fftwl_complex *)in, (fftwl_complex *)out, dir, flags);
}
template<>
inline void PIFFTW_Private<ldouble>::p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {
plan = fftwl_plan_dft_r2c_1d(size, (ldouble *)in, (fftwl_complex *)out, flags);
}
template<>
inline void PIFFTW_Private<ldouble>::p_executePlan(void * plan) {
fftwl_execute((fftwl_plan)plan);
}
template<>
inline void PIFFTW_Private<ldouble>::p_executePlan_c2c(void * plan, const void * in, void * out) {
fftwl_execute_dft((fftwl_plan)plan, (fftwl_complex *)in, (fftwl_complex *)out);
}
template<>
inline void PIFFTW_Private<ldouble>::p_executePlan_r2c(void * plan, const void * in, void * out) {
fftwl_execute_dft_r2c((fftwl_plan)plan, (ldouble *)in, (fftwl_complex *)out);
}
template<>
inline void PIFFTW_Private<ldouble>::p_destroyPlan(void *& plan) {
if (plan) fftwl_destroy_plan((fftwl_plan)plan);
plan = 0;
}
# ifdef PIP_FFTWl_THREADSAFE
template<>
inline void PIFFTW_Private<ldouble>::p_makeThreadSafe() {
fftwl_make_planner_thread_safe();
}
# endif
#endif // PIP_FFTWl
#endif // PIFFT_H

View File

@@ -1,270 +1,266 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Broadcast for all interfaces, including loopback Broadcast for all interfaces, including loopback
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "pibroadcast.h" #include "pibroadcast.h"
#include "piliterals_time.h" /** \class PIBroadcast
* \brief Broadcast for all interfaces, including loopback
/** \class PIBroadcast *
* \brief Broadcast for all interfaces, including loopback * \section PIBroadcast_synopsis Synopsis
* * %PIBroadcast used as multichannel IO device. It can use
* \section PIBroadcast_synopsis Synopsis * multicast, broadcast and loopback ethernet channels to
* %PIBroadcast used as multichannel IO device. It can use * send/receive packets. \a send() function send packet to
* multicast, broadcast and loopback ethernet channels to * all initialized ethernets. \a receiveEvent() raised on
* send/receive packets. \a send() function send packet to * packet received by any ethernet. All multi/broadcast
* all initialized ethernets. \a receiveEvent() raised on * ethernets created for all current addresses, obtained
* packet received by any ethernet. All multi/broadcast * by \a PIEthernets::allAddresses().
* ethernets created for all current addresses, obtained *
* by \a PIEthernets::allAddresses(). * * \a Multicast ethernets use \a multicastGroup() and \a multicastPort()
* * * \a Broadcast ethernets use \a broadcastPort()
* * \a Multicast ethernets use \a multicastGroup() and \a multicastPort() * * \a Loopback ethernet use \a loopbackPortsCount() started from \a loopbackPort()
* * \a Broadcast ethernets use \a broadcastPort() *
* * \a Loopback ethernet use \a loopbackPortsCount() started from \a loopbackPort() * %PIBroadcast starts thread, which every 3 seconds check if
* * current \a PIEthernet::allAddresses() was changed and call
* %PIBroadcast starts thread, which every 3 seconds check if * \a reinit() if it necessary.
* current \a PIEthernet::allAddresses() was changed and call *
* \a reinit() if it necessary. */
*
*/ #define MULTICAST_TTL 4
#define MULTICAST_TTL 4
PIBroadcast::PIBroadcast(bool send_only): PIThread(), PIEthUtilBase() {
_channels = All;
PIBroadcast::PIBroadcast(bool send_only): PIThread(), PIEthUtilBase() { eth_lo = 0;
_channels = All; mcast_address.set("232.13.3.14", 14100);
eth_lo = 0; lo_port = 14200;
mcast_address.set("232.13.3.14", 14100); lo_pcnt = 5;
lo_port = 14200; _started = false;
lo_pcnt = 5; _send_only = send_only;
_started = false; _reinit = true;
_send_only = send_only; }
_reinit = true;
}
PIBroadcast::~PIBroadcast() {
PIThread::stop();
PIBroadcast::~PIBroadcast() { mcast_mutex.unlock();
PIThread::stopAndWait(); destroyAll();
// mcast_mutex.unlock(); }
destroyAll();
}
void PIBroadcast::setChannels(PIBroadcast::Channels ch) {
PIMutexLocker ml(mcast_mutex);
void PIBroadcast::setChannels(PIBroadcast::Channels ch) { _channels = ch;
PIMutexLocker ml(mcast_mutex); _reinit = true;
_channels = ch; }
_reinit = true;
}
void PIBroadcast::setMulticastGroup(const PIString & mg) {
PIMutexLocker ml(mcast_mutex);
void PIBroadcast::setMulticastGroup(const PIString & mg) { mcast_address.setIP(mg);
PIMutexLocker ml(mcast_mutex); _reinit = true;
mcast_address.setIP(mg); }
_reinit = true;
}
void PIBroadcast::setMulticastPort(ushort port) {
PIMutexLocker ml(mcast_mutex);
void PIBroadcast::setMulticastPort(ushort port) { mcast_address.setPort(port);
PIMutexLocker ml(mcast_mutex); _reinit = true;
mcast_address.setPort(port); }
_reinit = true;
}
void PIBroadcast::setMulticastAddress(const PIEthernet::Address & addr) {
PIMutexLocker ml(mcast_mutex);
void PIBroadcast::setMulticastAddress(const PINetworkAddress & addr) { mcast_address = addr;
PIMutexLocker ml(mcast_mutex); _reinit = true;
mcast_address = addr; }
_reinit = true;
}
void PIBroadcast::setBroadcastPort(ushort port) {
PIMutexLocker ml(mcast_mutex);
void PIBroadcast::setBroadcastPort(ushort port) { bcast_port = port;
PIMutexLocker ml(mcast_mutex); _reinit = true;
bcast_port = port; }
_reinit = true;
}
void PIBroadcast::setLoopbackPort(ushort port) {
PIMutexLocker ml(mcast_mutex);
void PIBroadcast::setLoopbackPort(ushort port) { lo_port = port;
PIMutexLocker ml(mcast_mutex); _reinit = true;
lo_port = port; }
_reinit = true;
}
void PIBroadcast::setLoopbackPortsCount(int count) {
PIMutexLocker ml(mcast_mutex);
void PIBroadcast::setLoopbackPortsCount(int count) { lo_pcnt = count;
PIMutexLocker ml(mcast_mutex); _reinit = true;
lo_pcnt = count; }
_reinit = true;
}
void PIBroadcast::destroyAll() {
piForeach (PIEthernet * e, eth_mcast) {
void PIBroadcast::destroyAll() { e->stopThreadedRead();
for (auto * e: eth_mcast) { delete e;
e->stopAndWait(); }
piDeleteSafety(e); eth_mcast.clear();
} if (eth_lo) {
eth_mcast.clear(); eth_lo->stopThreadedRead();
if (eth_lo) { delete eth_lo;
eth_lo->stopAndWait(); eth_lo = 0;
piDeleteSafety(eth_lo); }
} }
}
void PIBroadcast::initAll(PIVector<PIEthernet::Address> al) {
void PIBroadcast::initAll(PIVector<PINetworkAddress> al) { PIMutexLocker ml(mcast_mutex);
PIMutexLocker ml(mcast_mutex); destroyAll();
destroyAll(); _reinit = false;
_reinit = false; prev_al = al;
prev_al = al; al.removeAll(PIEthernet::Address("127.0.0.1"));
al.removeAll(PINetworkAddress("127.0.0.1")); al << mcast_address;
al << mcast_address; eth_mcast.clear();
eth_mcast.clear(); PIEthernet::InterfaceList ifaces = PIEthernet::interfaces();
PIEthernet::InterfaceList ifaces = PIEthernet::interfaces(); piForeachC (PIEthernet::Address & a, al) {
for (const auto & a: al) { PIEthernet * ce = 0;
PIEthernet * ce = 0; //piCout << "mcast try" << a;
// piCout << "mcast try" << a; if (_channels[Multicast]) {
if (_channels[Multicast]) { ce = new PIEthernet();
ce = new PIEthernet(); ce->setDebug(false);
ce->setDebug(false); ce->setName("PIMulticast_" + a.toString());
ce->setName("PIMulticast_" + a.toString()); ce->setParameters(0);
ce->setParameters(0); ce->setSendAddress(mcast_address);
ce->setSendAddress(mcast_address); ce->setMulticastTTL(MULTICAST_TTL);
ce->setMulticastTTL(MULTICAST_TTL); if (!_send_only) {
if (!_send_only) { ce->setReadAddress(a.ipString(), mcast_address.port());
ce->setReadAddress(a.ipString(), mcast_address.port()); ce->joinMulticastGroup(mcast_address.ipString());
ce->joinMulticastGroup(mcast_address.ipString()); //piCout << "mcast " << ce->readAddress() << ce->sendAddress();
// piCout << "mcast " << ce->readAddress() << ce->sendAddress(); if (ce->open()) {
if (ce->open()) { eth_mcast << ce;
eth_mcast << ce; CONNECTU(ce, threadedReadEvent, this, mcastRead);
CONNECT2(void, const uchar *, ssize_t, ce, threadedReadEvent, this, mcastRead); } else {
} else { delete ce;
delete ce; }
} } else {
} else { eth_mcast << ce;
eth_mcast << ce; }
} }
}
if (_channels[Broadcast]) {
if (_channels[Broadcast]) { ce = new PIEthernet();
ce = new PIEthernet(); ce->setDebug(false);
ce->setDebug(false); ce->setName("PIMulticast_" + a.toString());
ce->setName("PIMulticast_" + a.toString()); ce->setParameters(PIEthernet::Broadcast);
ce->setParameters(PIEthernet::Broadcast); const PIEthernet::Interface * cint = ifaces.getByAddress(a.ipString());
const PIEthernet::Interface * cint = ifaces.getByAddress(a.ipString()); PIEthernet::Address nm((cint == 0) ? "255.255.255.0" : cint->netmask);
PINetworkAddress nm((cint == 0) ? "255.255.255.0" : cint->netmask); ce->setSendAddress(PIEthernet::getBroadcast(a, nm).ipString(), bcast_port);
ce->setSendAddress(PIEthernet::getBroadcast(a, nm).ipString(), bcast_port); if (!_send_only) {
if (!_send_only) { ce->setReadAddress(PIEthernet::Address(a.ip(), bcast_port));
ce->setReadAddress(PINetworkAddress(a.ip(), bcast_port)); //piCout << "bcast " << ce->readAddress() << ce->sendAddress();
// piCout << "bcast " << ce->readAddress() << ce->sendAddress(); if (ce->open()) {
if (ce->open()) { eth_mcast << ce;
eth_mcast << ce; CONNECTU(ce, threadedReadEvent, this, mcastRead);
CONNECT2(void, const uchar *, ssize_t, ce, threadedReadEvent, this, mcastRead); } else {
} else { delete ce;
delete ce; }
} } else {
} else { eth_mcast << ce;
eth_mcast << ce; }
} }
} }
}
if (_channels[Loopback]) {
if (_channels[Loopback]) { eth_lo = new PIEthernet();
eth_lo = new PIEthernet(); eth_lo->setDebug(false);
eth_lo->setDebug(false); eth_lo->setName("PIMulticast_loopback");
eth_lo->setName("PIMulticast_loopback"); if (!_send_only) {
if (!_send_only) { eth_lo->setParameter(PIEthernet::ReuseAddress, false);
eth_lo->setParameter(PIEthernet::ReuseAddress, false); CONNECTU(eth_lo, threadedReadEvent, this, mcastRead);
CONNECT2(void, const uchar *, ssize_t, eth_lo, threadedReadEvent, this, mcastRead); for (int i = 0; i < lo_pcnt; ++i) {
for (int i = 0; i < lo_pcnt; ++i) { eth_lo->setReadAddress("127.0.0.1", lo_port + i);
eth_lo->setReadAddress("127.0.0.1", lo_port + i); if (eth_lo->open()) {
if (eth_lo->open()) { //piCout << "bind local to" << (lo_port + i);
// piCout << "bind local to" << (lo_port + i); break;
break; }
} }
} }
} }
} }
}
void PIBroadcast::send(const PIByteArray & data) {
void PIBroadcast::send(const PIByteArray & data) { if (!isRunning()) {
/*if (!isRunning()) { reinit();
reinit(); PIThread::start(3000);
PIThread::start(3000); }
}*/ PIByteArray cd = cryptData(data);
PIByteArray cd = cryptData(data); if (cd.isEmpty()) return;
if (cd.isEmpty()) return; PIMutexLocker ml(mcast_mutex);
PIMutexLocker ml(mcast_mutex); piForeach (PIEthernet * e, eth_mcast) e->send(cd);
for (auto * e: eth_mcast) if (eth_lo) {
e->send(cd); for (int i = 0; i < lo_pcnt; ++i) {
if (eth_lo) { eth_lo->send("127.0.0.1", lo_port + i, cd);
for (int i = 0; i < lo_pcnt; ++i) { }
eth_lo->send("127.0.0.1", lo_port + i, cd); }
} }
}
}
void PIBroadcast::startRead() {
if (!isRunning()) {
void PIBroadcast::startRead() { _started = false;
if (!isRunning()) { reinit();
_started = false; PIThread::start(3000);
reinit(); }
PIThread::start(3_s); if (_send_only) return;
} PIMutexLocker ml(mcast_mutex);
if (_send_only) return; piForeach (PIEthernet * e, eth_mcast) e->startThreadedRead();
PIMutexLocker ml(mcast_mutex); if (eth_lo) eth_lo->startThreadedRead();
for (auto * e: eth_mcast) _started = true;
e->startThreadedRead(); }
if (eth_lo) eth_lo->startThreadedRead();
_started = true;
} void PIBroadcast::stopRead() {
if (isRunning()) stop();
PIMutexLocker ml(mcast_mutex);
void PIBroadcast::stopRead() { piForeach (PIEthernet * e, eth_mcast) e->stopThreadedRead();
if (isRunning()) stopAndWait(); if (eth_lo) eth_lo->stopThreadedRead();
PIMutexLocker ml(mcast_mutex); _started = false;
for (auto * e: eth_mcast) }
e->stopAndWait();
if (eth_lo) eth_lo->stopAndWait();
_started = false; void PIBroadcast::reinit() {
} initAll(PIEthernet::allAddresses());
if (_started) startRead();
}
void PIBroadcast::reinit() {
initAll(PIEthernet::allAddresses());
if (_started) startRead(); void PIBroadcast::mcastRead(uchar * data, int size) {
} PIByteArray cd = decryptData(PIByteArray(data, size));
if (cd.isEmpty()) return;
received(cd);
void PIBroadcast::mcastRead(const uchar * data, ssize_t size) { receiveEvent(cd);
PIByteArray cd = decryptData(PIByteArray(data, size)); }
if (cd.isEmpty()) return;
received(cd);
receiveEvent(cd); void PIBroadcast::run() {
} PIVector<PIEthernet::Address> al = PIEthernet::allAddresses();
mcast_mutex.lock();
bool r = _reinit, ac = (al != prev_al);
void PIBroadcast::run() { mcast_mutex.unlock();
PIVector<PINetworkAddress> al = PIEthernet::allAddresses(); if (ac || r) reinit();
mcast_mutex.lock(); if (ac) addressesChanged();
bool r = _reinit, ac = (al != prev_al); }
mcast_mutex.unlock();
if (ac || r) reinit();
if (ac) addressesChanged();
}

View File

@@ -1,120 +1,121 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Base class for ethernet utils Base class for ethernet utils
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piethutilbase.h" #include "piethutilbase.h"
#ifdef PIP_CRYPT #ifdef PIP_CRYPT
# include "picrypt.h" # include "picrypt.h"
#endif #endif
/** \class PIEthUtilBase /** \class PIEthUtilBase
* \brief Base class for ethernet utils * \brief Base class for ethernet utils
* *
* \section PIEthUtilBase_synopsis Synopsis * \section PIEthUtilBase_synopsis Synopsis
* %PIEthUtilBase provides crypt layer for derived classes: * %PIEthUtilBase provides crypt layer for derived classes:
* \a PIStreamPacker and \a PIBroadcast. All input and output * \a PIStreamPacker and \a PIBroadcast. All input and output
* (sended and received) data can be decrypted/encrypted by this layer. * (sended and received) data can be decrypted/encrypted by this layer.
* *
* By default crypt layer is disabled. * By default crypt layer is disabled.
* *
* You can separetely enable it and set ready-to-use * You can separetely enable it and set ready-to-use
* key by \a setCryptEnabled() and \a setCryptKey(). Or you can * key by \a setCryptEnabled() and \a setCryptKey(). Or you can
* use \a createCryptKey() to generate key from your passphrase * use \a createCryptKey() to generate key from your passphrase
* and automatic enable crypt layer. * and automatic enable crypt layer.
* *
* \note To use crypt layer, PIP should be built with crypt module, * \note To use crypt layer, PIP should be built with crypt module,
* otherwise your in/out data will be lost. * otherwise your in/out data will be lost.
* *
* You can use this class as base for your own classes. Use \a cryptData() * You can use this class as base for your own classes. Use \a cryptData()
* and \a decryptData() when send and receive your data. * and \a decryptData() when send and receive your data.
* *
*/ */
PIEthUtilBase::PIEthUtilBase() { PIEthUtilBase::PIEthUtilBase() {
_crypt = false; _crypt = false;
} }
PIEthUtilBase::~PIEthUtilBase() {} PIEthUtilBase::~PIEthUtilBase() {
}
void PIEthUtilBase::setCryptEnabled(bool on) {
_crypt = on; void PIEthUtilBase::setCryptEnabled(bool on) {
} _crypt = on;
}
void PIEthUtilBase::cryptEnable() {
setCryptEnabled(true); void PIEthUtilBase::cryptEnable() {
} setCryptEnabled(true);
}
void PIEthUtilBase::cryptDisable() {
setCryptEnabled(false); void PIEthUtilBase::cryptDisable() {
} setCryptEnabled(false);
}
bool PIEthUtilBase::isCryptEnabled() const {
return _crypt; bool PIEthUtilBase::isCryptEnabled() const {
} return _crypt;
}
void PIEthUtilBase::setCryptKey(const PIByteArray & k) {
_key = k; void PIEthUtilBase::setCryptKey(const PIByteArray & k) {
setCryptEnabled(true); _key = k;
} setCryptEnabled(true);
}
void PIEthUtilBase::createCryptKey(const PIString & k) {
#ifdef PIP_CRYPT void PIEthUtilBase::createCryptKey(const PIString & k) {
_key = PICrypt::hash("sodium_bug"); #ifdef PIP_CRYPT
_key = PICrypt::hash(k); _key = PICrypt::hash("sodium_bug");
#else _key = PICrypt::hash(k);
piCout << "[PIEthUtilBase] PICrypt wasn`t built!"; #else
#endif piCout << "[PIEthUtilBase] PICrypt wasn`t built!";
_crypt = true; #endif
} _crypt = true;
}
PIByteArray PIEthUtilBase::cryptKey() const {
return _key; PIByteArray PIEthUtilBase::cryptKey() const {
} return _key;
}
PIByteArray PIEthUtilBase::cryptData(const PIByteArray & data) {
if (!_crypt) return data; PIByteArray PIEthUtilBase::cryptData(const PIByteArray & data) {
return if (!_crypt) return data;
#ifdef PIP_CRYPT return
PICrypt::crypt(data, _key); #ifdef PIP_CRYPT
#else PICrypt::crypt(data, _key);
PIByteArray(); #else
#endif PIByteArray();
} #endif
}
PIByteArray PIEthUtilBase::decryptData(const PIByteArray & data) {
if (!_crypt) return data; PIByteArray PIEthUtilBase::decryptData(const PIByteArray & data) {
#ifdef PIP_CRYPT if (!_crypt) return data;
bool ok = false; #ifdef PIP_CRYPT
PIByteArray ret = PICrypt::decrypt(data, _key, &ok); bool ok = false;
if (!ok) return PIByteArray(); PIByteArray ret = PICrypt::decrypt(data, _key, &ok);
return ret; if (!ok) return PIByteArray();
#else return ret;
return PIByteArray(); #else
#endif return PIByteArray();
} #endif
}

View File

@@ -1,199 +0,0 @@
/*
PIP - Platform Independent Primitives
PIPackedTCP
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include "pipackedtcp.h"
#include "piethernet.h"
#include "piliterals.h"
/** \class PIPackedTCP pipackedtcp.h
* \brief
* TCP packed channel
*
* \details
* \section PITCP_sec0 Synopsis
* %PIEthernet designed to work with IPv4 network via two protocols:
* UDP and TCP. This class allow you send and receive packets to/from
* another computer through network. Also it supports broadcast and
* multicast extensions.
*
* */
PIPackedTCP::PIPackedTCP(Role role, const PINetworkAddress & addr): m_role(role) {
setMode(PIIODevice::ReadWrite);
packer.setCryptEnabled(false);
CONNECTL(&packer, packetReceiveEvent, [this](PIByteArray & data) {
PIMutexLocker ml(rec_mutex);
rec_queue.enqueue(data);
});
init();
setAddress(addr);
}
PIPackedTCP::~PIPackedTCP() {
stopAndWait();
if (client) client->stopAndWait();
if (eth) eth->stopAndWait();
piDeleteSafety(eth);
}
void PIPackedTCP::setAddress(const PINetworkAddress & addr) {
m_addr = addr;
setPath(m_addr.toString());
}
bool PIPackedTCP::isConnected() const {
if (m_role == Client) {
return eth->isConnected();
} else {
if (client) return client->isConnected();
}
return false;
}
bool PIPackedTCP::isConnecting() const {
if (m_role == Client) {
return eth->isConnecting();
} else {
if (client) return client->isConnecting();
}
return false;
}
void PIPackedTCP::init() {
if (client) client->stopAndWait();
if (eth) eth->stopAndWait();
piDeleteSafety(eth);
eth = new PIEthernet(m_role == Client ? PIEthernet::TCP_Client : PIEthernet::TCP_Server);
if (m_role == Client) {
eth->setReopenTimeout(100_ms);
packer.assignDevice(eth);
CONNECTL(eth, connected, [this]() {
packer.clear();
connected();
});
CONNECTL(eth, disconnected, [this](bool) {
packer.clear();
eth->connect(path(), true);
disconnected();
});
} else {
CONNECTL(eth, newConnection, [this](PIEthernet * c) {
if (client) client->stopAndWait();
piDeleteSafety(client);
client = c;
// piCout << "Server connected" << client;
packer.assignDevice(client);
CONNECTL(client, disconnected, [this](bool) {
packer.assignDevice(nullptr);
packer.clear();
disconnected();
});
client->startThreadedRead();
connected();
});
}
}
PIIODevice::DeviceInfoFlags PIPackedTCP::deviceInfoFlags() const {
return PIIODevice::Reliable;
}
PIString PIPackedTCP::constructFullPathDevice() const {
return PIString(m_role == Client ? "client" : "server") + ":" + path();
}
void PIPackedTCP::configureFromFullPathDevice(const PIString & full_path) {
PIStringList pl = full_path.split(":");
if (pl.size() >= 1) {
PIString p = pl[0].toLowerCase().left(1);
if (p == "c") m_role = Client;
if (p == "s") m_role = Server;
init();
}
PINetworkAddress addr("0.0.0.0", 13362);
if (pl.size() >= 2) {
if (pl[1].isNotEmpty()) addr.setIP(pl[1]);
}
if (pl.size() >= 3) {
if (pl[2].isNotEmpty()) addr.setPort(pl[2].toInt());
}
setAddress(addr);
}
ssize_t PIPackedTCP::readDevice(void * read_to, ssize_t max_size) {
PIMutexLocker ml(rec_mutex);
if (rec_queue.isNotEmpty()) {
auto d = rec_queue.dequeue();
auto sz = piMin(max_size, d.size_s());
if (read_to) memcpy(read_to, d.data(), sz);
return sz;
}
return 0;
}
ssize_t PIPackedTCP::writeDevice(const void * data, ssize_t max_size) {
if (!isConnected()) return 0;
packer.send(PIByteArray(data, max_size));
// piCout << m_role << "write" << eth;
return max_size;
/*if (m_role == Client) {
return eth->write(data, max_size);
} else {
if (client) return client->write(data, max_size);
}*/
}
bool PIPackedTCP::openDevice() {
if (m_role == Client) {
if (eth->isConnected()) return true;
if (eth->isConnecting()) return false;
packer.clear();
bool ret = eth->connect(path(), false);
eth->startThreadedRead();
return ret;
} else {
return eth->listen(path(), false);
}
return false;
}
bool PIPackedTCP::closeDevice() {
if (client) {
client->close();
client->stopAndWait();
piDeleteSafety(client);
packer.assignDevice(nullptr);
}
return eth->close();
}

View File

@@ -1,221 +1,202 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Simple packet wrap aroud any PIIODevice Simple packet wrap aroud any PIIODevice
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifdef __GNUC__ #ifdef __GNUC__
# pragma GCC diagnostic push # pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wnonnull" # pragma GCC diagnostic ignored "-Wnonnull"
#endif #endif
#include "pistreampacker.h" #include "pistreampacker.h"
#include "piiodevice.h"
#include "piiodevice.h" #ifdef __GNUC__
#include "piliterals_bytes.h" # pragma GCC diagnostic pop
#ifdef __GNUC__ #endif
# pragma GCC diagnostic pop
#endif /** \class PIStreamPacker
* \brief Simple packet wrap aroud any PIIODevice
/** \class PIStreamPacker *
* \brief Simple packet wrap aroud any PIIODevice * \section PIStreamPacker_synopsis Synopsis
* * %PIStreamPacker provides simple pack/unpack logic for any data packets.
* \section PIStreamPacker_synopsis Synopsis *
* %PIStreamPacker provides simple pack/unpack logic for any data packets. * When you call \a send() function data splited into several
* * parts, \a packetSign() prepended to first part and \a sendRequest()
* When you call \a send() function data splited into several * event raised several times.
* parts, \a packetSign() prepended to first part and \a sendRequest() *
* event raised several times. * When your device receive some data, call \a received() function.
* * \a packetReceiveEvent() event will be raised when packet will be
* When your device receive some data, call \a received() function. * collected.
* \a packetReceiveEvent() event will be raised when packet will be *
* collected. * Use \a assignDevice() to connect device to this %PIStreamPacker.
* *
* Use \a assignDevice() to connect device to this %PIStreamPacker. */
*
*/
PIStreamPacker::PIStreamPacker(PIIODevice * dev): PIObject() {
crypt_frag = crypt_size = false;
PIStreamPacker::PIStreamPacker(PIIODevice * dev): PIObject() { aggressive_optimization = true;
crypt_frag = crypt_size = false; packet_size = -1;
aggressive_optimization = true; size_crypted_size = sizeof(int);
packet_size = -1; crypt_frag_size = 1024*1024;
size_crypted_size = sizeof(int); max_packet_size = 1400;
crypt_frag_size = 1_MiB; packet_sign = 0xAFBE;
max_packet_size = 1400; assignDevice(dev);
packet_sign = 0xAFBE; }
assignDevice(dev);
}
void PIStreamPacker::setCryptSizeEnabled(bool on) {
crypt_size = on;
void PIStreamPacker::setCryptSizeEnabled(bool on) { if (crypt_size) {
crypt_size = on; PIByteArray ba; ba << int(0);
if (crypt_size) { size_crypted_size = cryptData(ba).size_s();
PIByteArray ba; } else
ba << int(0); size_crypted_size = sizeof(int);
size_crypted_size = cryptData(ba).size_s(); }
} else
size_crypted_size = sizeof(int);
} void PIStreamPacker::clear() {
packet.clear();
packet_size = -1;
void PIStreamPacker::clear() { stream.clear();
packet.clear(); }
packet_size = -1;
stream.clear();
} void PIStreamPacker::send(const PIByteArray & data) {
if (data.isEmpty()) return;
PIByteArray cd;
void PIStreamPacker::send(const PIByteArray & data) { if (crypt_frag) {
if (data.isEmpty()) return; int fcnt = (data.size_s() - 1) / crypt_frag_size + 1, fst = 0;
PIByteArray cd; //piCout << "crypt_frag send" << fcnt << "frags";
if (crypt_frag) { PIByteArray frag;
int fcnt = (data.size_s() - 1) / crypt_frag_size + 1, fst = 0; for (int i = 0; i < fcnt; ++i) {
// piCout << "crypt_frag send" << fcnt << "frags"; if (i == fcnt - 1) frag = PIByteArray(data.data(fst), data.size_s() - fst);
PIByteArray frag; else frag = PIByteArray(data.data(fst), crypt_frag_size);
for (int i = 0; i < fcnt; ++i) { fst += crypt_frag_size;
if (i == fcnt - 1) cd << cryptData(frag);
frag = PIByteArray(data.data(fst), data.size_s() - fst); }
else } else {
frag = PIByteArray(data.data(fst), crypt_frag_size); cd = cryptData(data);
fst += crypt_frag_size; }
cd << cryptData(frag); //piCout << "crypt" << data.size() << "->" << cd.size() << key().size();
} PIByteArray hdr, part;
} else { hdr << packet_sign;
cd = cryptData(data); if (crypt_size) {
} PIByteArray crsz; crsz << int(cd.size_s());
// piCout << "crypt" << data.size() << "->" << cd.size() << key().size(); hdr.append(cryptData(crsz));
PIByteArray hdr, part; } else
hdr << packet_sign; hdr << int(cd.size_s());
if (crypt_size) { cd.insert(0, hdr);
PIByteArray crsz; int pcnt = (cd.size_s() - 1) / max_packet_size + 1, pst = 0;
crsz << int(cd.size_s()); for (int i = 0; i < pcnt; ++i) {
hdr.append(cryptData(crsz)); if (i == pcnt - 1) part = PIByteArray(cd.data(pst), cd.size_s() - pst);
} else else part = PIByteArray(cd.data(pst), max_packet_size);
hdr << int(cd.size_s()); //piCout << "send" << part.size();
cd.insert(0, hdr); sendRequest(part);
int pcnt = (cd.size_s() - 1) / max_packet_size + 1, pst = 0; pst += max_packet_size;
for (int i = 0; i < pcnt; ++i) { }
if (i == pcnt - 1) }
part = PIByteArray(cd.data(pst), cd.size_s() - pst);
else
part = PIByteArray(cd.data(pst), max_packet_size); void PIStreamPacker::received(uchar * readed, int size) {
// piCout << "send" << part.size(); received(PIByteArray(readed, size));
sendRequest(part); }
pst += max_packet_size;
}
} void PIStreamPacker::received(const PIByteArray & data) {
stream.append(data);
//piCout << "rec" << data.size();
void PIStreamPacker::received(const uchar * readed, ssize_t size) { while (!stream.isEmpty()) {
if (size <= 0) return; int hdr_size = sizeof(packet_sign) + size_crypted_size;
received(PIByteArray(readed, size)); if (packet_size < 0) {
} if (stream.size_s() < hdr_size) return;
ushort sign(0);
memcpy(&sign, stream.data(), 2);
void PIStreamPacker::received(const PIByteArray & data) { if (sign != packet_sign) {
stream.append(data); if (aggressive_optimization) stream.clear();
// piCout << "rec" << data.size(); else stream.pop_front();
while (!stream.isEmpty()) { continue;
int hdr_size = sizeof(packet_sign) + size_crypted_size; }
if (packet_size < 0) { int sz = -1;
if (stream.size_s() < hdr_size) return; if (crypt_size) {
ushort sign(0); PIByteArray crsz((uint)size_crypted_size);
memcpy(&sign, stream.data(), 2); memcpy(crsz.data(), stream.data(2), size_crypted_size);
if (sign != packet_sign) { crsz = decryptData(crsz);
if (aggressive_optimization) if (crsz.size() < sizeof(sz)) {
stream.clear(); if (aggressive_optimization) stream.clear();
else else stream.pop_front();
stream.pop_front(); continue;
continue; }
} crsz >> sz;
int sz = -1; } else {
if (crypt_size) { memcpy(&sz, stream.data(2), size_crypted_size);
PIByteArray crsz((uint)size_crypted_size); }
memcpy(crsz.data(), stream.data(2), size_crypted_size); if (sz < 0) {
crsz = decryptData(crsz); if (aggressive_optimization) stream.clear();
if (crsz.size() < sizeof(sz)) { else stream.pop_front();
if (aggressive_optimization) continue;
stream.clear(); }
else stream.remove(0, hdr_size);
stream.pop_front(); packet.clear();
continue; packet_size = sz;
} if (packet_size == 0)
crsz >> sz; packet_size = -1;
} else { continue;
memcpy(&sz, stream.data(2), size_crypted_size); } else {
} int ps = piMini(stream.size_s(), packet_size - packet.size_s());
if (sz < 0) { packet.append(stream.data(), ps);
if (aggressive_optimization) stream.remove(0, ps);
stream.clear(); if (packet.size_s() == packet_size) {
else PIByteArray cd;
stream.pop_front(); if (crypt_frag) {
continue; //piCout << "decrypt frags ..." << packet_size;
} while (packet.size_s() >= 4) {
stream.remove(0, hdr_size); //piCout << "decrypt frags take data ...";
packet.clear(); PIByteArray frag;
packet_size = sz; //piCout << "decrypt frags take data done" << frag.size_s();
if (packet_size == 0) packet >> frag;
packet_size = -1; if (frag.isEmpty()) {
else //piCout << "decrypt frags corrupt, break";
startPacketReceive(packet_size); cd.clear();
continue; break;
} else { }
int ps = piMini(stream.size_s(), packet_size - packet.size_s()); cd.append(decryptData(frag));
packet.append(stream.data(), ps); //piCout << "decrypt frags add" << frag.size_s();
stream.remove(0, ps); }
if (packet.size_s() == packet_size) { //piCout << "decrypt frags done" << cd.size();
PIByteArray cd; } else {
if (crypt_frag) { cd = decryptData(packet);
// piCout << "decrypt frags ..." << packet_size; }
while (packet.size_s() >= 4) { //piCout << "decrypt" << packet.size() << "->" << cd.size() << key().size();
// piCout << "decrypt frags take data ..."; if (!cd.isEmpty()) {
PIByteArray frag; packetReceived(cd);
// piCout << "decrypt frags take data done" << frag.size_s(); packetReceiveEvent(cd);
packet >> frag; }
if (frag.isEmpty()) { packet.clear();
// piCout << "decrypt frags corrupt, break"; packet_size = -1;
cd.clear(); }
break; }
} }
cd.append(decryptData(frag)); }
// piCout << "decrypt frags add" << frag.size_s();
}
// piCout << "decrypt frags done" << cd.size(); void PIStreamPacker::assignDevice(PIIODevice * dev) {
} else { if (!dev) return;
cd = decryptData(packet); if (!dev->infoFlags()[PIIODevice::Reliable])
} piCoutObj << "Warning! Not recommended to use with non-reliable" << dev;
// piCout << "decrypt" << packet.size() << "->" << cd.size() << key().size(); CONNECTU(dev, threadedReadEvent, this, received);
if (!cd.isEmpty()) { CONNECTU(this, sendRequest, dev, write);
endPacketReceive(); }
packetReceived(cd);
packetReceiveEvent(cd);
}
packet.clear();
packet_size = -1;
}
}
}
}
void PIStreamPacker::assignDevice(PIIODevice * dev) {
if (!dev) return;
if (!dev->infoFlags()[PIIODevice::Reliable]) {
piCoutObj << "Warning! Not recommended to use with non-reliable" << dev;
}
CONNECT2(void, const uchar *, ssize_t, dev, threadedReadEvent, this, received);
CONNECT1(void, PIByteArray, this, sendRequest, dev, write);
}

View File

@@ -1,20 +1,20 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
PILuaProgram PILuaProgram
Andrey Bychkov work.a.b@yandex.ru Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "piluaprogram.h" #include "piluaprogram.h"
@@ -50,3 +50,4 @@ luabridge::LuaRef PILuaProgram::getGlobal(const PIString & name) {
luabridge::Namespace PILuaProgram::getGlobalNamespace() { luabridge::Namespace PILuaProgram::getGlobalNamespace() {
return luabridge::getGlobalNamespace(PRIVATE->lua_state); return luabridge::getGlobalNamespace(PRIVATE->lua_state);
} }

View File

@@ -1,58 +0,0 @@
/*
PIP - Platform Independent Primitives
Module includes
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
//! \defgroup Application Application
//! \~\brief
//! \~english Application-level classes.
//! \~russian Классы уровня "приложение".
//!
//! \~\details
//! \~english \section cmake_module_Application Building with CMake
//! \~russian \section cmake_module_Application Сборка с использованием CMake
//!
//! \~\code
//! find_package(PIP REQUIRED)
//! target_link_libraries([target] PIP)
//! \endcode
//!
//! \~english \par Common
//! \~russian \par Общее
//!
//! \~english
//! These files provides some classes for help to create application
//!
//! \~russian
//! Эти файлы предоставляют классы для облегчения создания приложения
//!
//! \~\authors
//! \~english
//! Ivan Pelipenko peri4ko@yandex.ru;
//! Andrey Bychkov work.a.b@yandex.ru;
//! \~russian
//! Иван Пелипенко peri4ko@yandex.ru;
//! Андрей Бычков work.a.b@yandex.ru;
//!
#ifndef PIAPPLICATIONMODULE_H
#define PIAPPLICATIONMODULE_H
#include "picli.h"
#include "pisingleapplication.h"
#include "pisystemmonitor.h"
#endif

View File

@@ -1,255 +0,0 @@
/*
PIP - Platform Independent Primitives
Command-Line Parser
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include "picli.h"
#include "pisysteminfo.h"
//! \class PICLI picli.h
//! \details
//! \~english \section PICLI_sec0 Synopsis
//! \~russian \section PICLI_sec0 Краткий обзор
//! \~english
//! This class provide handy parsing of command-line arguments. First you should add
//! arguments to %PICLI with function \a addArgument(). Then you can check if there
//! is some argument in application command-line with function \a hasArgument(),
//! or obtain argument value by \a argumentValue().
//!
//! \~russian
//! Этот класс предоставляет удобный механизм для разбора аргументов командной строки.
//! Сперва необходимо добавить аргументы в %PICLI с помощью методов \a addArgument().
//! Далее можно проверять аргументы на наличие в командной строке методом \a hasArgument(),
//! а также получать их значения при помощи \a argumentValue().
//!
//! \~english \section PICLI_sec1 Example
//! \~russian \section PICLI_sec1 Пример
//! \~\code
//! int main(int argc, char ** argv) {
//! PICLI cli(argc, argv);
//! cli.addArgument("console");
//! cli.addArgument("debug");
//! cli.addArgument("Value", "v", "value", true);
//! if (cli.hasArgument("console"))
//! piCout << "console active";
//! if (cli.hasArgument("debug"))
//! piCout << "debug active";
//! piCout << "Value =" << cli.argumentValue("Value");
//! return 0;
//! }
//! \endcode
//!
//! \~english These executions are similar:
//! \~russian Эти вызовы будут идентичны:
//!
//! \~\code
//! a.out -cd -v 10
//! a.out --value 10 -dc
//! a.out -c -v 10 -d
//! a.out --console -d -v 10
//! a.out --debug -c --value 10
//! \endcode
//!
PICLI::PICLI(int argc, char * argv[]) {
for (int i = 0; i < argc; ++i)
_args_raw << argv[i];
if (argc > 0) PISystemInfo::instance()->execCommand = argv[0];
}
void PICLI::parse() {
if (!needParse) return;
PIString cra, full;
Argument * last = 0;
for (int i = 1; i < _args_raw.size_s(); ++i) {
cra = _args_raw[i];
if (cra.left(2) == _prefix_full) {
last = 0;
full = cra.right(cra.length() - 2);
for (auto & a: _args) {
if (a.full_key == full) {
a.found = true;
last = &a;
break;
}
}
} else {
if (cra.left(1) == _prefix_short) {
last = 0;
for (int j = 1; j < cra.length(); ++j) {
bool found = false;
for (auto & a: _args) {
if ((a.short_key != '\0') && (a.short_key == cra[j])) {
a.found = true;
last = &a;
found = true;
break;
}
}
if (!found) break;
}
} else {
if (last == 0 ? true : !last->has_value) {
if (_args_mand.size_s() < _count_mand) {
_args_mand << cra;
continue;
}
if (_args_opt.size_s() < _count_opt || _count_opt < 0) {
_args_opt << cra;
continue;
}
piCoutObj << "Arguments overflow, \"" << cra << "\" ignored";
}
if (last == 0 ? false : last->has_value) {
last->value = cra;
last = 0;
}
}
}
}
needParse = false;
}
void PICLI::addArgument(const PIString & name, bool value) {
_args << Argument(name, name[0], name, value);
needParse = true;
}
void PICLI::addArgument(const PIString & name, const PIChar & shortKey, bool value) {
_args << Argument(name, shortKey, name, value);
needParse = true;
}
void PICLI::addArgument(const PIString & name, const char * shortKey, bool value) {
_args << Argument(name, PIChar::fromUTF8(shortKey), name, value);
needParse = true;
}
void PICLI::addArgument(const PIString & name, const PIChar & shortKey, const PIString & fullKey, bool value) {
_args << Argument(name, shortKey, fullKey, value);
needParse = true;
}
void PICLI::addArgument(const PIString & name, const char * shortKey, const PIString & fullKey, bool value) {
_args << Argument(name, PIChar::fromUTF8(shortKey), fullKey, value);
needParse = true;
}
PIString PICLI::rawArgument(int index) {
parse();
return _args_raw[index];
}
PIString PICLI::mandatoryArgument(int index) {
parse();
return _args_mand[index];
}
PIString PICLI::optionalArgument(int index) {
parse();
return _args_opt[index];
}
const PIStringList & PICLI::rawArguments() {
parse();
return _args_raw;
}
const PIStringList & PICLI::mandatoryArguments() {
parse();
return _args_mand;
}
const PIStringList & PICLI::optionalArguments() {
parse();
return _args_opt;
}
PIString PICLI::programCommand() {
parse();
return _args_raw.isNotEmpty() ? _args_raw.front() : PIString();
}
bool PICLI::hasArgument(const PIString & name) {
parse();
for (const auto & i: _args)
if (i.name == name && i.found) return true;
return false;
}
PIString PICLI::argumentValue(const PIString & name) {
parse();
for (const auto & i: _args)
if (i.name == name && i.found) return i.value;
return PIString();
}
PIString PICLI::argumentShortKey(const PIString & name) {
for (const auto & i: _args)
if (i.name == name) return PIString(i.short_key);
return PIString();
}
PIString PICLI::argumentFullKey(const PIString & name) {
for (const auto & i: _args)
if (i.name == name) return i.full_key;
return PIString();
}
void PICLI::setShortKeyPrefix(const PIString & prefix) {
_prefix_short = prefix;
needParse = true;
}
void PICLI::setFullKeyPrefix(const PIString & prefix) {
_prefix_full = prefix;
needParse = true;
}
void PICLI::setMandatoryArgumentsCount(const int count) {
_count_mand = count;
needParse = true;
}
void PICLI::setOptionalArgumentsCount(const int count) {
_count_opt = count;
needParse = true;
}

View File

@@ -1,136 +0,0 @@
/*! \file picli.h
* \ingroup Application
* \~\brief
* \~english Command-Line parser
* \~russian Парсер командной строки
*/
/*
PIP - Platform Independent Primitives
Command-Line Parser
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#ifndef PICLI_H
#define PICLI_H
#include "piset.h"
#include "pistringlist.h"
//! \ingroup Application
//! \~\brief
//! \~english Command-Line parser.
//! \~russian Парсер командной строки.
class PIP_EXPORT PICLI {
public:
//! \~english Constructs %PICLI from "argc" and "argv" from "int main()" method.
//! \~russian Создает %PICLI из "argc" и "argv" из метода "int main()".
PICLI(int argc, char * argv[]);
//! \~english Add argument with name "name", short key = name first letter and full key = name.
//! \~russian Добавляет аргумент с именем "name", коротким ключом = первой букве имени и полным ключом = имени.
void addArgument(const PIString & name, bool value = false);
//! \~english Add argument with name "name", short key = "shortKey" and full key = name.
//! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = имени.
void addArgument(const PIString & name, const PIChar & shortKey, bool value = false);
//! \~english Add argument with name "name", short key = "shortKey" and full key = name.
//! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = имени.
void addArgument(const PIString & name, const char * shortKey, bool value = false);
//! \~english Add argument with name "name", short key = "shortKey" and full key = "fullKey".
//! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = "fullKey".
void addArgument(const PIString & name, const PIChar & shortKey, const PIString & fullKey, bool value = false);
//! \~english Add argument with name "name", short key = "shortKey" and full key = "fullKey".
//! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = "fullKey".
void addArgument(const PIString & name, const char * shortKey, const PIString & fullKey, bool value = false);
//! \~english Returns unparsed command-line argument by index "index". Index 0 is program execute command.
//! \~russian Возвращает исходный аргумент командной строки по индексу "index". Индекс 0 это команда вызова программы.
PIString rawArgument(int index);
PIString mandatoryArgument(int index);
PIString optionalArgument(int index);
//! \~english Returns unparsed command-line arguments.
//! \~russian Возвращает исходные аргументы командной строки.
const PIStringList & rawArguments();
const PIStringList & mandatoryArguments();
const PIStringList & optionalArguments();
//! \~english Returns program execute command without arguments.
//! \~russian Возвращает команду вызова программы без аргументов.
PIString programCommand();
//! \~english Returns if argument "name" found.
//! \~russian Возвращает найден ли аргумент "name".
bool hasArgument(const PIString & name);
//! \~english Returns argument "name" value, or empty string if this is no value.
//! \~russian Возвращает значение аргумента "name" или пустую строку, если значения нет.
PIString argumentValue(const PIString & name);
//! \~english Returns short key of argument "name", or empty string if this is no argument.
//! \~russian Возвращает короткий ключ аргумента "name" или пустую строку, если аргумента нет.
PIString argumentShortKey(const PIString & name);
//! \~english Returns full key of argument "name", or empty string if this is no argument.
//! \~russian Возвращает полный ключ аргумента "name" или пустую строку, если аргумента нет.
PIString argumentFullKey(const PIString & name);
const PIString & shortKeyPrefix() const { return _prefix_short; }
const PIString & fullKeyPrefix() const { return _prefix_full; }
int mandatoryArgumentsCount() const { return _count_mand; }
int optionalArgumentsCount() const { return _count_opt; }
void setShortKeyPrefix(const PIString & prefix);
void setFullKeyPrefix(const PIString & prefix);
void setMandatoryArgumentsCount(const int count);
void setOptionalArgumentsCount(const int count);
bool debug() const { return debug_; }
void setDebug(bool debug) { debug_ = debug; }
PIConstChars className() const { return "PICLI"; }
PIString name() const { return PIStringAscii("CLI"); }
private:
struct Argument {
Argument() {}
Argument(const PIString & n, const PIChar & s, const PIString & f, bool v) {
name = n;
short_key = s;
full_key = f;
has_value = v;
}
PIString name;
PIChar short_key;
PIString full_key;
PIString value;
bool has_value = false, found = false;
};
void parse();
PIString _prefix_short = "-", _prefix_full = "--";
PIStringList _args_raw, _args_mand, _args_opt;
PISet<PIString> keys_full, keys_short;
PIVector<Argument> _args;
int _count_mand = 0, _count_opt = 0;
bool needParse = true, debug_ = true;
};
#endif // PICLI_H

View File

@@ -1,229 +0,0 @@
/*
PIP - Platform Independent Primitives
High-level log
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include "pilog.h"
#include "pidir.h"
#include "piliterals_string.h"
#include "piliterals_time.h"
#include "pitime.h"
//! \class PILog pilog.h
//! \details
//! \~english \section PILog_sec0 Synopsis
//! \~russian \section PILog_sec0 Краткий обзор
//! \~english
//! This class provide handy parsing of command-line arguments. First you should add
//! arguments to %PICLI with function \a addArgument(). Then you can check if there
//! is some argument in application command-line with function \a hasArgument(),
//! or obtain argument value by \a argumentValue().
//!
//! \~russian
//! Этот класс предоставляет удобный механизм для разбора аргументов командной строки.
//! Сперва необходимо добавить аргументы в %PICLI с помощью методов \a addArgument().
//! Далее можно проверять аргументы на наличие в командной строке методом \a hasArgument(),
//! а также получать их значения при помощи \a argumentValue().
//!
//! \~english \section PICLI_sec1 Example
//! \~russian \section PICLI_sec1 Пример
//! \~\code
//! int main(int argc, char ** argv) {
//! PICLI cli(argc, argv);
//! cli.addArgument("console");
//! cli.addArgument("debug");
//! cli.addArgument("Value", "v", "value", true);
//! if (cli.hasArgument("console"))
//! piCout << "console active";
//! if (cli.hasArgument("debug"))
//! piCout << "debug active";
//! piCout << "Value =" << cli.argumentValue("Value");
//! return 0;
//! }
//! \endcode
//!
//! \~english These executions are similar:
//! \~russian Эти вызовы будут идентичны:
//!
//! \~\code
//! a.out -cd -v 10
//! a.out --value 10 -dc
//! a.out -c -v 10 -d
//! a.out --console -d -v 10
//! a.out --debug -c --value 10
//! \endcode
//!
PILog::PILog(): PIThread(), log_ts(&log_file) {
setName("PILog");
split_time = 8_h;
timestamp_format = "yyyy-MM-dd hh:mm:ss.zzz";
setLineFormat("t - c: m");
id_by_cat[Level::Info] = PICout::registerExternalBufferID();
id_by_cat[Level::Debug] = PICout::registerExternalBufferID();
id_by_cat[Level::Warning] = PICout::registerExternalBufferID();
id_by_cat[Level::Error] = PICout::registerExternalBufferID();
CONNECTU(PICout::Notifier::object(), finished, this, coutDone);
}
PILog::~PILog() {
stop();
}
void PILog::setDir(const PIString & d) {
stopAndWait();
log_dir = d;
if (output[File]) {
PIDir::make(log_dir);
newFile();
}
start();
}
void PILog::setLineFormat(const PIString & f) {
line_format = f;
line_format_p = line_format;
line_format_p.replace("t", "${t}").replace("c", "${c}").replace("m", "${m}");
}
void PILog::setLevel(Level l) {
max_level = l;
}
PICout PILog::error(PIObject * context) {
return makePICout(context, Level::Error);
}
PICout PILog::warning(PIObject * context) {
return makePICout(context, Level::Warning);
}
PICout PILog::info(PIObject * context) {
return makePICout(context, Level::Info);
}
PICout PILog::debug(PIObject * context) {
return makePICout(context, Level::Debug);
}
void PILog::stop() {
while (true) {
log_mutex.lock();
bool done = queue.isEmpty();
log_mutex.unlock();
if (done) break;
piMinSleep();
}
PIThread::stopAndWait();
}
void PILog::coutDone(int id, PIString * buffer) {
if (!buffer) return;
if (!id_by_cat.containsValue(id)) return;
auto cat = id_by_cat.key(id, PILog::Level::Debug);
if (cat > max_level) return;
enqueue(*buffer, cat);
delete buffer;
}
PICout PILog::makePICout(PIObject * context, Level cat) {
auto buffer = new PIString();
if (context) {
*buffer = "["_a + context->className();
if (context->name().isNotEmpty()) *buffer += " \"" + context->name() + "\"";
*buffer += "] ";
}
return PICout::withExternalBuffer(buffer, id_by_cat.value(cat), PICoutManipulators::AddSpaces);
}
void PILog::enqueue(const PIString & msg, Level cat) {
auto t = PIDateTime::fromSystemTime(PISystemTime::current());
PIMutexLocker ml(log_mutex);
queue.enqueue({cat, t, msg});
}
PIString PILog::entryToString(const Entry & e) const {
static PIStringList categories{"error", "warn ", "info ", "debug"};
PIString t = e.time.toString(timestamp_format);
PIString ret = line_format_p;
ret.replace("${t}", t).replace("${c}", categories[static_cast<int>(e.cat)]).replace("${m}", e.msg);
return ret;
}
void PILog::newFile() {
PIString aname = log_name;
if (aname.isNotEmpty()) aname += "__";
log_file.open(log_dir + "/" + aname + PIDateTime::current().toString("yyyy_MM_dd__hh_mm_ss") + ".log." +
PIString::fromNumber(++part_number),
PIIODevice::ReadWrite);
}
void PILog::run() {
if (output[File]) {
if (split_tm.elapsed() >= split_time) {
split_tm.reset();
newFile();
}
}
log_mutex.lock();
if (queue.isEmpty()) {
log_mutex.unlock();
piMSleep(20);
return;
}
log_mutex.unlock();
while (true) {
log_mutex.lock();
if (queue.isEmpty()) {
log_mutex.unlock();
return;
}
auto qi = queue.dequeue();
log_mutex.unlock();
auto str = entryToString(qi);
if (log_file.isOpened()) log_ts << str << "\n";
if (output[Console]) {
PICout out(qi.cat == Level::Error ? piCerr : piCout);
if (color_console) {
switch (qi.cat) {
case Level::Error: out << PICoutManipulators::Red; break;
case Level::Warning: out << PICoutManipulators::Yellow; break;
default: break;
}
}
out << str;
}
}
}

View File

@@ -1,152 +0,0 @@
/*! \file pilog.h
* \ingroup Application
* \~\brief
* \~english High-level log
* \~russian Высокоуровневый лог
*/
/*
PIP - Platform Independent Primitives
High-level log
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#ifndef PIlog_H
#define PIlog_H
#include "pifile.h"
#include "piiostream.h"
#include "pithread.h"
//! \ingroup Application
//! \~\brief
//! \~english High-level log
//! \~russian Высокоуровневый лог
class PIP_EXPORT PILog: public PIThread {
PIOBJECT_SUBCLASS(PILog, PIThread)
public:
PILog();
~PILog();
enum class Level {
Error,
Warning,
Info,
Debug,
};
enum Output {
File = 0x1,
Console = 0x2,
All = 0xFF,
};
//! \~english Set output target \"o\" to \"on\".
void setOutput(Output o, bool on = true) { output.setFlag(o, on); }
//! \~english Returns prefix for filename.
PIString logName() const { return log_name; }
//! \~english Set prefix for filename. Should be set \b before \a setDir()!
void setLogName(const PIString & n) { log_name = n; }
//! \~english Returns if color for console output enabled.
bool colorConsole() const { return color_console; }
//! \~english Set color for console output enabled. True by default.
void setColorConsole(bool yes) { color_console = yes; }
//! \~english Returns directory for log files.
PIString dir() const { return log_dir; }
//! \~english Set directory for log files. Should be set \b after \a setApplicationName()!
void setDir(const PIString & d);
//! \~english Returns lifetime for file.
PISystemTime fileSplitTime() const { return split_time; }
//! \~english Set lifetime for file. Each "st" interval new file will be created.
void setFileSplitTime(PISystemTime st) { split_time = st; }
//! \~english Returns timestamp format for line.
PIString timestampFormat() const { return timestamp_format; }
//! \~english Set timestamp format for line. Default is "yyyy-MM-dd hh:mm:ss.zzz".
void setTimestampFormat(const PIString & f) { timestamp_format = f; }
//! \~english Returns line format.
PIString lineFormat() const { return line_format; }
//! \~english Set line format. "t" is timestamp, "c" is category and "m" is message. Default is "t - c: m".
void setLineFormat(const PIString & f);
//! \~english Returns maximum level.
Level level() const { return max_level; }
//! \~english Set maximum level. All levels greater than \"l\" will be ignored. Default if \a Level::Debug.
void setLevel(Level l);
//! \~english Returns PICout for Level::Error level.
PICout error(PIObject * context = nullptr);
//! \~english Returns PICout for Level::Warning level.
PICout warning(PIObject * context = nullptr);
//! \~english Returns PICout for Level::Info level.
PICout info(PIObject * context = nullptr);
//! \~english Returns PICout for Level::Debug level.
PICout debug(PIObject * context = nullptr);
//! \~english Write all queued lines and stop. Also called in destructor.
void stop();
private:
EVENT_HANDLER2(void, coutDone, int, id, PIString *, buff);
PICout makePICout(PIObject * context, Level cat);
void enqueue(const PIString & msg, Level cat = Level::Debug);
struct Entry {
Level cat;
PIDateTime time;
PIString msg;
};
PIString entryToString(const Entry & e) const;
void newFile();
void run() override;
PIMutex log_mutex;
PIFile log_file;
PIIOTextStream log_ts;
PITimeMeasurer split_tm;
PISystemTime split_time;
PIString log_dir, timestamp_format, line_format, line_format_p, log_name;
PIQueue<Entry> queue;
PIMap<Level, int> id_by_cat;
Level max_level = Level::Debug;
PIFlags<Output> output = All;
bool color_console = true;
int part_number = -1, cout_id = -1;
};
#endif

View File

@@ -1,69 +0,0 @@
/*! \file piclientserver_client.h
* \ingroup ClientServer
* \~\brief
* \~english
* \~russian
*/
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#ifndef piclientserver_client_H
#define piclientserver_client_H
#include "piclientserver_client_base.h"
namespace PIClientServer {
// ServerClient
class PIP_CLIENT_SERVER_EXPORT ServerClient: public ClientBase {
friend class Server;
NO_COPY_CLASS(ServerClient);
public:
ServerClient() {}
protected:
virtual void aboutDelete() {}
private:
void createForServer(Server * parent, PIEthernet * tcp_);
};
// Client
class PIP_CLIENT_SERVER_EXPORT Client: public ClientBase {
NO_COPY_CLASS(Client);
public:
Client();
~Client();
void connect(PINetworkAddress addr);
protected:
private:
};
} // namespace PIClientServer
#endif

View File

@@ -1,86 +0,0 @@
/*! \file piclientserver_client_base.h
* \ingroup ClientServer
* \~\brief
* \~english
* \~russian
*/
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#ifndef piclientserver_client_base_H
#define piclientserver_client_base_H
#include "piclientserver_config.h"
#include "pidiagnostics.h"
#include "pip_client_server_export.h"
#include "pistreampacker.h"
class PIEthernet;
namespace PIClientServer {
class Server;
class PIP_CLIENT_SERVER_EXPORT ClientBase {
friend class Config;
friend class Server;
NO_COPY_CLASS(ClientBase);
public:
ClientBase();
virtual ~ClientBase();
const PIEthernet * getTCP() const { return tcp; }
void close();
void stopAndWait();
int write(const void * d, const size_t s);
int write(const PIByteArray & ba) { return write(ba.data(), ba.size()); }
void enableDiagnostics();
PIDiagnostics::State diagnostics() const;
int receivePacketProgress() const;
Config & configuration() { return config; }
protected:
virtual void readed(PIByteArray data) {}
virtual void connected() {}
virtual void disconnected() {}
virtual void receivePacketStart(int size) {}
virtual void receivePacketEnd() {}
void init();
bool own_tcp = false;
std::atomic_bool can_write = {true};
PIEthernet * tcp = nullptr;
Config config;
private:
void destroy();
PIStreamPacker stream;
mutable PIMutex write_mutex;
PIDiagnostics * diag = nullptr;
};
} // namespace PIClientServer
#endif

View File

@@ -1,60 +0,0 @@
/*! \file piclientserver_config.h
* \ingroup ClientServer
* \~\brief
* \~english
* \~russian
*/
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#ifndef piclientserver_config_H
#define piclientserver_config_H
#include "pibytearray.h"
#include "pip_client_server_export.h"
namespace PIClientServer {
class Server;
class Client;
class ClientBase;
class PIP_CLIENT_SERVER_EXPORT Config {
friend class Server;
friend class Client;
public:
void setPacketSign(ushort sign);
void setPacketSize(int bytes);
void enableSymmetricEncryption(const PIByteArray & key);
protected:
void apply(ClientBase * client);
PIByteArray crypt_key;
ushort packet_sign = 0xAFBE;
int packet_size = 1400;
private:
};
} // namespace PIClientServer
#endif

View File

@@ -1,82 +0,0 @@
/*! \file piclientserver_server.h
* \ingroup ClientServer
* \~\brief
* \~english
* \~russian
*/
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#ifndef piclientserver_server_H
#define piclientserver_server_H
#include "piclientserver_config.h"
#include "pimutex.h"
#include "pinetworkaddress.h"
#include "pip_client_server_export.h"
#include "pithreadnotifier.h"
class PIEthernet;
class PIThread;
namespace PIClientServer {
class ServerClient;
class PIP_CLIENT_SERVER_EXPORT Server {
friend class ServerClient;
NO_COPY_CLASS(Server);
public:
Server();
virtual ~Server();
void listen(PINetworkAddress addr);
void listenAll(ushort port) { listen({0, port}); }
void closeAll();
int getMaxClients() const { return max_clients; }
void setMaxClients(int new_max_clients);
int clientsCount() const;
void forEachClient(std::function<void(ServerClient *)> func);
void setClientFactory(std::function<ServerClient *()> f) { client_factory = f; }
Config & configuration() { return config; }
private:
void stopServer();
void newClient(ServerClient * c);
void clientDisconnected(ServerClient * c);
std::function<ServerClient *()> client_factory;
std::atomic_bool is_closing = {false};
PIEthernet * tcp_server = nullptr;
PIThread * clean_thread = nullptr;
PIThreadNotifier clean_notifier;
Config config;
PIVector<ServerClient *> clients;
mutable PIMutex clients_mutex;
int max_clients = 1000;
};
} // namespace PIClientServer
#endif

View File

@@ -5,22 +5,22 @@
* \~russian Базовый класс для PICloudClient и PICloudServer * \~russian Базовый класс для PICloudClient и PICloudServer
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
PICloud Base - Base class for PICloudClient and PICloud Server PICloud Base - Base class for PICloudClient and PICloud Server
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PICLOUDBASE_H #ifndef PICLOUDBASE_H
@@ -31,7 +31,8 @@
#include "pistreampacker.h" #include "pistreampacker.h"
class PIP_CLOUD_EXPORT PICloudBase { class PIP_CLOUD_EXPORT PICloudBase
{
public: public:
PICloudBase(); PICloudBase();

View File

@@ -5,22 +5,22 @@
* \~russian Клиент PICloud * \~russian Клиент PICloud
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
PICloud Client PICloud Client
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PICLOUDCLIENT_H #ifndef PICLOUDCLIENT_H
@@ -32,30 +32,26 @@
//! \brief PICloudClient //! \brief PICloudClient
class PIP_CLOUD_EXPORT PICloudClient class PIP_CLOUD_EXPORT PICloudClient: public PIIODevice, public PICloudBase
: public PIIODevice {
, public PICloudBase { PIIODEVICE(PICloudClient, "")
PIIODEVICE(PICloudClient, "");
public: public:
explicit PICloudClient(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite); explicit PICloudClient(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
virtual ~PICloudClient(); virtual ~PICloudClient();
void setServerName(const PIString & server_name); void setServerName(const PIString & server_name);
void setKeepConnection(bool on); void setKeepConnection(bool on);
bool isConnected() const { return is_connected; } bool isConnected() const {return is_connected;}
ssize_t bytesAvailable() const override { return buff.size(); }
void interrupt() override;
EVENT(connected); EVENT(connected)
EVENT(disconnected); EVENT(disconnected)
protected: protected:
bool openDevice() override; bool openDevice();
bool closeDevice() override; bool closeDevice();
ssize_t readDevice(void * read_to, ssize_t max_size) override; int readDevice(void * read_to, int max_size);
ssize_t writeDevice(const void * data, ssize_t size) override; int writeDevice(const void * data, int size);
DeviceInfoFlags deviceInfoFlags() const override { return PIIODevice::Reliable; } DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;}
private: private:
EVENT_HANDLER1(void, _readed, PIByteArray &, data); EVENT_HANDLER1(void, _readed, PIByteArray &, data);

View File

@@ -1,20 +1,20 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Module includes Module includes
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
//! \defgroup Cloud Cloud //! \defgroup Cloud Cloud
//! \~\brief //! \~\brief
@@ -51,8 +51,8 @@
#ifndef PICLOUDMODULE_H #ifndef PICLOUDMODULE_H
#define PICLOUDMODULE_H #define PICLOUDMODULE_H
#include "picloudtcp.h"
#include "picloudclient.h" #include "picloudclient.h"
#include "picloudserver.h" #include "picloudserver.h"
#include "picloudtcp.h"
#endif // PICLOUDMODULE_H #endif // PICLOUDMODULE_H

View File

@@ -5,22 +5,22 @@
* \~russian Сервер PICloud * \~russian Сервер PICloud
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
PICloud Server PICloud Server
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PICLOUDSERVER_H #ifndef PICLOUDSERVER_H
@@ -30,32 +30,26 @@
#include "piconditionvar.h" #include "piconditionvar.h"
class PIP_CLOUD_EXPORT PICloudServer class PIP_CLOUD_EXPORT PICloudServer: public PIIODevice, public PICloudBase
: public PIIODevice {
, public PICloudBase { PIIODEVICE(PICloudServer, "")
PIIODEVICE(PICloudServer, "");
public: public:
//! PICloudServer //! PICloudServer
explicit PICloudServer(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite); explicit PICloudServer(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
virtual ~PICloudServer(); virtual ~PICloudServer();
class Client: public PIIODevice { class Client : public PIIODevice {
PIIODEVICE(PICloudServer::Client, ""); PIIODEVICE(PICloudServer::Client, "")
friend class PICloudServer; friend class PICloudServer;
public: public:
Client(PICloudServer * srv = nullptr, uint id = 0); Client(PICloudServer * srv = nullptr, uint id = 0);
virtual ~Client(); virtual ~Client();
protected: protected:
bool openDevice() override; bool openDevice();
bool closeDevice() override; bool closeDevice();
ssize_t readDevice(void * read_to, ssize_t max_size) override; int readDevice(void * read_to, int max_size);
ssize_t writeDevice(const void * data, ssize_t size) override; int writeDevice(const void * data, int size);
DeviceInfoFlags deviceInfoFlags() const override { return PIIODevice::Reliable; } DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;}
ssize_t bytesAvailable() const override { return buff.size(); }
void interrupt() override;
private: private:
void pushBuffer(const PIByteArray & ba); void pushBuffer(const PIByteArray & ba);
@@ -71,14 +65,13 @@ public:
PIVector<PICloudServer::Client *> clients() const; PIVector<PICloudServer::Client *> clients() const;
EVENT1(newConnection, PICloudServer::Client *, client); EVENT1(newConnection, PICloudServer::Client * , client)
protected: protected:
bool openDevice() override; bool openDevice();
bool closeDevice() override; bool closeDevice();
ssize_t readDevice(void * read_to, ssize_t max_size) override; int readDevice(void * read_to, int max_size);
ssize_t writeDevice(const void * data, ssize_t max_size) override; int writeDevice(const void * data, int max_size);
void interrupt() override;
private: private:
EVENT_HANDLER1(void, _readed, PIByteArray &, ba); EVENT_HANDLER1(void, _readed, PIByteArray &, ba);
@@ -87,13 +80,9 @@ private:
int sendData(const PIByteArray & data, uint client_id); int sendData(const PIByteArray & data, uint client_id);
PIVector<Client *> clients_; PIVector<Client *> clients_;
PIVector<Client *> removed_clients_;
PIMap<uint, Client *> index_clients; PIMap<uint, Client *> index_clients;
PIThread ping_timer; PITimer ping_timer;
PIConditionVariable cvar;
PIMutex open_mutex;
mutable PIMutex clients_mutex; mutable PIMutex clients_mutex;
std::atomic_bool is_deleted;
}; };
#endif // PICLOUDSERVER_H #endif // PICLOUDSERVER_H

View File

@@ -5,30 +5,30 @@
* \~russian TCP слой PICloud * \~russian TCP слой PICloud
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
PICloud TCP transport PICloud TCP transport
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PICLOUDTCP_H #ifndef PICLOUDTCP_H
#define PICLOUDTCP_H #define PICLOUDTCP_H
#include "pimutex.h"
#include "pip_cloud_export.h" #include "pip_cloud_export.h"
#include "pistring.h" #include "pistring.h"
#include "pimutex.h"
class PIEthernet; class PIEthernet;
@@ -37,6 +37,7 @@ class PIStreamPacker;
namespace PICloud { namespace PICloud {
class PIP_CLOUD_EXPORT TCP { class PIP_CLOUD_EXPORT TCP {
public: public:
enum Version { enum Version {
@@ -46,21 +47,21 @@ public:
enum Role { enum Role {
InvalidRole = 0, InvalidRole = 0,
Server = 1, Server = 1,
Client = 2, Client = 2,
}; };
enum Type { enum Type {
InvalidType = 0, InvalidType = 0,
Connect = 1, Connect = 1,
Disconnect = 2, Disconnect = 2,
Data = 3, Data = 3,
Ping = 4, Ping = 4,
}; };
TCP(PIStreamPacker * s); TCP(PIStreamPacker * s);
void setRole(Role r); void setRole(Role r);
Role role() const { return (Role)header.role; } Role role() const {return (Role)header.role;}
void setServerName(const PIString & server_name_); void setServerName(const PIString & server_name_);
PIString serverName() const; PIString serverName() const;
@@ -81,8 +82,8 @@ private:
struct Header { struct Header {
Header(); Header();
uchar version; // PICloud::Version uchar version; // PICloud::Version
uchar type; // PICloud::Type uchar type; // PICloud::Type
uchar role; // PICloud::Role uchar role; // PICloud::Role
}; };
Header header; Header header;
@@ -90,8 +91,9 @@ private:
PIString server_name; PIString server_name;
PIStreamPacker * streampacker; PIStreamPacker * streampacker;
PIMutex mutex_send; PIMutex mutex_send;
}; };
} // namespace PICloud }
#endif // PICLOUDTCP_H #endif // PICLOUDTCP_H

View File

@@ -1,85 +1,62 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
C++ code info structs C++ code info structs
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "picodeinfo.h" #include "picodeinfo.h"
#include "pivariant.h" #include "pivariant.h"
PIString PICodeInfo::EnumInfo::memberName(int value_) const { PIString PICodeInfo::EnumInfo::memberName(int value_) const {
piForeachC(PICodeInfo::EnumeratorInfo & e, members) piForeachC (PICodeInfo::EnumeratorInfo & e, members)
if (e.value == value_) return e.name.toString(); if (e.value == value_)
return e.name.toString();
return PIString(); return PIString();
} }
int PICodeInfo::EnumInfo::memberValue(const PIString & name_) const { int PICodeInfo::EnumInfo::memberValue(const PIString & name_) const {
piForeachC(PICodeInfo::EnumeratorInfo & e, members) piForeachC (PICodeInfo::EnumeratorInfo & e, members)
if (e.name.toString() == name_) return e.value; if (e.name.toString() == name_)
return e.value;
return -1; return -1;
} }
PIVariantTypes::Enum PICodeInfo::EnumInfo::toPIVariantEnum() { PIVariantTypes::Enum PICodeInfo::EnumInfo::toPIVariantEnum() {
PIVariantTypes::Enum en(name.toString()); PIVariantTypes::Enum en(name.toString());
for (auto m: members) for (auto m: members) en << m.toPIVariantEnumerator();
en << m.toPIVariantEnumerator();
if (!en.isEmpty()) en.selectValue(members.front().value); if (!en.isEmpty()) en.selectValue(members.front().value);
return en; return en;
} }
PIMap<PIConstChars, PICodeInfo::ClassInfo * > * PICodeInfo::classesInfo;
PIMap<PIConstChars, PICodeInfo::EnumInfo * > * PICodeInfo::enumsInfo;
PIMap<PIConstChars, PICodeInfo::AccessValueFunction> * PICodeInfo::accessValueFunctions;
PIMap<PIConstChars, PICodeInfo::AccessTypeFunction> * PICodeInfo::accessTypeFunctions;
bool __PICodeInfoInitializer__::_inited_ = false;
PIVariant PICodeInfo::getMemberAsVariant(const void * p, const char * class_name, const char * member_name) { PIVariant PICodeInfo::getMemberAsVariant(const void * p, const char * class_name, const char * member_name) {
if (!p || !class_name || !member_name) return PIVariant(); if (!p || !class_name || !member_name || !accessTypeFunctions || !accessValueFunctions) return PIVariant();
AccessTypeFunction atf = PICODEINFO::accessTypeFunctions().value(class_name, (AccessTypeFunction) nullptr); AccessTypeFunction atf = accessTypeFunctions->value(class_name, (AccessTypeFunction)0);
AccessValueFunction avf = PICODEINFO::accessValueFunctions().value(class_name, (AccessValueFunction) nullptr); AccessValueFunction avf = accessValueFunctions->value(class_name, (AccessValueFunction)0);
if (!atf || !avf) return PIVariant(); if (!atf || !avf) return PIVariant();
return PIVariant::fromValue(avf(p, member_name), PIStringAscii(atf(member_name))); return PIVariant::fromValue(avf(p, member_name), PIStringAscii(atf(member_name)));
} }
PICodeInfo::__Storage__::__Storage__() {
classesInfo = new PIMap<PIConstChars, ClassInfo *>;
enumsInfo = new PIMap<PIConstChars, EnumInfo *>;
accessValueFunctions = new PIMap<PIConstChars, AccessValueFunction>;
accessTypeFunctions = new PIMap<PIConstChars, AccessTypeFunction>;
(*enumsInfo)[""] = new EnumInfo();
}
PICodeInfo::__Storage__::~__Storage__() {
piDeleteAll(classesInfo->values());
piDeleteAll(enumsInfo->values());
piDeleteSafety(classesInfo);
piDeleteSafety(enumsInfo);
piDeleteSafety(accessValueFunctions);
piDeleteSafety(accessTypeFunctions);
}
PICodeInfo::__Storage__ * PICodeInfo::__Storage__::instance() {
static __Storage__ ret;
return &ret;
}
PICodeInfo::ClassInfoInterface classesInfo;
PICodeInfo::EnumsInfoInterface enumsInfo;
PICodeInfo::AccessValueFunctionInterface accessValueFunctions;
PICodeInfo::AccessTypeFunctionInterface accessTypeFunctions;

View File

@@ -1,26 +1,26 @@
/*! \file picodeinfo.h /*! \file picodeinfo.h
* \ingroup Code * \ingroup Code
* \~\brief * \~\brief
* \~english C++ code info structs. See \ref code_model. * \~english C++ code info structs
* \~russian Структуры для C++ кода. Подробнее \ref code_model. * \~russian Структуры для C++ кода
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
C++ code info structs C++ code info structs
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@@ -34,181 +34,72 @@
class PIVariant; class PIVariant;
//! \~english Namespace contains structures for code generation. See \ref code_model.
//! \~russian Пространство имен содержит структуры для кодогенерации. Подробнее \ref code_model.
namespace PICodeInfo { namespace PICodeInfo {
//! \~english
//! Type modifiers
//! \~russian
//! Модификаторы типа
enum TypeFlag { enum TypeFlag {
NoFlag, NoFlag,
Const /** const */ = 0x01, Const = 0x01,
Static /** static */ = 0x02, Static = 0x02,
Mutable /** mutable */ = 0x04, Mutable = 0x04,
Volatile /** volatile */ = 0x08, Volatile = 0x08,
Inline /** inline */ = 0x10, Inline = 0x10,
Virtual /** virtual */ = 0x20, Virtual = 0x20,
Extern /** extern */ = 0x40 Extern = 0x40
}; };
typedef PIFlags<PICodeInfo::TypeFlag> TypeFlags; typedef PIFlags<PICodeInfo::TypeFlag> TypeFlags;
typedef PIMap<PIString, PIString> MetaMap; typedef PIMap<PIString, PIString> MetaMap;
typedef PIByteArray (*AccessValueFunction)(const void *, const char *); typedef PIByteArray(*AccessValueFunction)(const void *, const char *);
typedef const char * (*AccessTypeFunction)(const char *); typedef const char*(*AccessTypeFunction)(const char *);
//! \~english Type information
//! \~russian Информация о типе
struct PIP_EXPORT TypeInfo { struct PIP_EXPORT TypeInfo {
TypeInfo(const PIConstChars & n = PIConstChars(), const PIConstChars & t = PIConstChars(), PICodeInfo::TypeFlags f = 0, int b = -1) { TypeInfo(const PIConstChars & n = PIConstChars(), const PIConstChars & t = PIConstChars(), PICodeInfo::TypeFlags f = 0, int b = -1) {name = n; type = t; flags = f; bits = b;}
name = n; bool isBitfield() const {return bits > 0;}
type = t;
flags = f;
bits = b;
}
//! \~english Returns if variable if bitfield
//! \~russian Возвращает битовым ли полем является переменная
bool isBitfield() const { return bits > 0; }
//! \~english Custom PIMETA content
//! \~russian Произвольное содержимое PIMETA
MetaMap meta; MetaMap meta;
//! \~english Name
//! \~russian Имя
PIConstChars name; PIConstChars name;
//! \~english Type
//! \~russian Тип
PIConstChars type; PIConstChars type;
//! \~english Modifiers
//! \~russian Модификаторы
PICodeInfo::TypeFlags flags; PICodeInfo::TypeFlags flags;
//! \~english Bitfield variable bit count
//! \~russian Количество бит битового поля
int bits; int bits;
}; };
//! \~english Method information
//! \~russian Информация о методе
struct PIP_EXPORT FunctionInfo { struct PIP_EXPORT FunctionInfo {
//! \~english Custom PIMETA content
//! \~russian Произвольное содержимое PIMETA
MetaMap meta; MetaMap meta;
//! \~english Name
//! \~russian Имя
PIConstChars name; PIConstChars name;
//! \~english Return type
//! \~russian Возвращаемые тип
TypeInfo return_type; TypeInfo return_type;
//! \~english Arguments types
//! \~russian Типы аргументов
PIVector<PICodeInfo::TypeInfo> arguments; PIVector<PICodeInfo::TypeInfo> arguments;
}; };
//! \~english Class or struct information
//! \~russian Информация о классе или структуре
struct PIP_EXPORT ClassInfo { struct PIP_EXPORT ClassInfo {
ClassInfo() { has_name = true; } ClassInfo() {has_name = true;}
//! \~english Custom PIMETA content
//! \~russian Произвольное содержимое PIMETA
MetaMap meta; MetaMap meta;
//! \~english Has name or not
//! \~russian Имеет или нет имя
bool has_name; bool has_name;
//! \~english Type
//! \~russian Тип
PIConstChars type; PIConstChars type;
//! \~english Name
//! \~russian Имя
PIConstChars name; PIConstChars name;
//! \~english Parent names
//! \~russian Имена родителей
PIVector<PIConstChars> parents; PIVector<PIConstChars> parents;
//! \~english Variables
//! \~russian Переменные
PIVector<PICodeInfo::TypeInfo> variables; PIVector<PICodeInfo::TypeInfo> variables;
//! \~english Methods
//! \~russian Методы
PIVector<PICodeInfo::FunctionInfo> functions; PIVector<PICodeInfo::FunctionInfo> functions;
PIVector<PICodeInfo::ClassInfo * > children_info;
//! \~english Subclass list
//! \~russian Список наследников
PIVector<PICodeInfo::ClassInfo *> children_info;
}; };
//! \~english Enumerator information
//! \~russian Информация об элементе перечисления
struct PIP_EXPORT EnumeratorInfo { struct PIP_EXPORT EnumeratorInfo {
EnumeratorInfo(const PIConstChars & n = PIConstChars(), int v = 0) { EnumeratorInfo(const PIConstChars & n = PIConstChars(), int v = 0) {name = n; value = v;}
name = n; PIVariantTypes::Enumerator toPIVariantEnumerator() {return PIVariantTypes::Enumerator(value, name.toString());}
value = v;
}
PIVariantTypes::Enumerator toPIVariantEnumerator() { return PIVariantTypes::Enumerator(value, name.toString()); }
//! \~english Custom PIMETA content
//! \~russian Произвольное содержимое PIMETA
MetaMap meta; MetaMap meta;
//! \~english Name
//! \~russian Имя
PIConstChars name; PIConstChars name;
//! \~english Value
//! \~russian Значение
int value; int value;
}; };
//! \~english Enum information
//! \~russian Информация о перечислении
struct PIP_EXPORT EnumInfo { struct PIP_EXPORT EnumInfo {
//! \~english Returns member name with value "value"
//! \~russian Возвращает имя элемента со значением "value"
PIString memberName(int value) const; PIString memberName(int value) const;
//! \~english Returns member value with name "name"
//! \~russian Возвращает значение элемента с именем "name"
int memberValue(const PIString & name) const; int memberValue(const PIString & name) const;
//! \~english Returns as PIVariantTypes::Enum
//! \~russian Возвращает как PIVariantTypes::Enum
PIVariantTypes::Enum toPIVariantEnum(); PIVariantTypes::Enum toPIVariantEnum();
//! \~english Custom PIMETA content
//! \~russian Произвольное содержимое PIMETA
MetaMap meta; MetaMap meta;
//! \~english Name
//! \~russian Имя
PIConstChars name; PIConstChars name;
//! \~english Members
//! \~russian Элементы
PIVector<PICodeInfo::EnumeratorInfo> members; PIVector<PICodeInfo::EnumeratorInfo> members;
}; };
inline PICout operator<<(PICout s, const PICodeInfo::TypeInfo & v) { inline PICout operator <<(PICout s, const PICodeInfo::TypeInfo & v) {
if (v.flags[Inline]) s << "inline "; if (v.flags[Inline]) s << "inline ";
if (v.flags[Virtual]) s << "virtual "; if (v.flags[Virtual]) s << "virtual ";
if (v.flags[Mutable]) s << "mutable "; if (v.flags[Mutable]) s << "mutable ";
@@ -216,26 +107,22 @@ inline PICout operator<<(PICout s, const PICodeInfo::TypeInfo & v) {
if (v.flags[Static]) s << "static "; if (v.flags[Static]) s << "static ";
if (v.flags[Const]) s << "const "; if (v.flags[Const]) s << "const ";
s << v.type; s << v.type;
if (!v.name.isEmpty()) s << " " << v.name; if (!v.name.isEmpty())
s << " " << v.name;
return s; return s;
} }
inline PICout operator<<(PICout s, const PICodeInfo::EnumeratorInfo & v) { inline PICout operator <<(PICout s, const PICodeInfo::EnumeratorInfo & v) {s << v.name << " = " << v.value << " Meta" << v.meta; return s;}
s << v.name << " = " << v.value << " Meta" << v.meta;
return s;
}
inline PICout operator<<(PICout s, const PICodeInfo::ClassInfo & v) { inline PICout operator <<(PICout s, const PICodeInfo::ClassInfo & v) {
s.saveAndSetControls(0); s.setControl(0, true);
s << "class " << v.name; s << "class " << v.name;
if (!v.parents.isEmpty()) { if (!v.parents.isEmpty()) {
s << ": "; s << ": ";
bool first = true; bool first = true;
for (const auto & i: v.parents) { for (const auto & i: v.parents) {
if (first) if (first) first = false;
first = false; else s << ", ";
else
s << ", ";
s << i; s << i;
} }
} }
@@ -244,132 +131,51 @@ inline PICout operator<<(PICout s, const PICodeInfo::ClassInfo & v) {
s << PICoutManipulators::Tab << i.return_type << " " << i.name << "("; s << PICoutManipulators::Tab << i.return_type << " " << i.name << "(";
bool fa = true; bool fa = true;
for (const auto & a: i.arguments) { for (const auto & a: i.arguments) {
if (fa) if (fa) fa = false;
fa = false; else s << ", ";
else
s << ", ";
s << a; s << a;
} }
s << ") Meta" << i.meta << ";\n"; s << ") Meta" << i.meta << ";\n";
} }
if (!v.functions.isEmpty() && !v.variables.isEmpty()) s << "\n"; if (!v.functions.isEmpty() && !v.variables.isEmpty())
s << "\n";
for (const auto & i: v.variables) { for (const auto & i: v.variables) {
s << PICoutManipulators::Tab << i << " Meta" << i.meta << ";\n"; s << PICoutManipulators::Tab << i << " Meta" << i.meta << ";\n";
} }
s << "}\n"; s << "}\n";
s.restoreControls(); s.restoreControl();
return s; return s;
} }
inline PICout operator<<(PICout s, const PICodeInfo::EnumInfo & v) { inline PICout operator <<(PICout s, const PICodeInfo::EnumInfo & v) {
s.saveAndSetControls(0); s.setControl(0, true);
s << "enum " << v.name << " Meta" << v.meta << " {\n"; s << "enum " << v.name << " Meta" << v.meta << " {\n";
for (const auto & i: v.members) { for (const auto & i: v.members) {
bool f = true; bool f = true;
if (f) if (f) f = false;
f = false; else s << ", ";
else
s << ", ";
s << PICoutManipulators::Tab << i << "\n"; s << PICoutManipulators::Tab << i << "\n";
} }
s << "}\n"; s << "}\n";
s.restoreControls(); s.restoreControl();
return s; return s;
} }
extern PIP_EXPORT PIMap<PIConstChars, PICodeInfo::ClassInfo * > * classesInfo;
class PIP_EXPORT __Storage__ { extern PIP_EXPORT PIMap<PIConstChars, PICodeInfo::EnumInfo * > * enumsInfo;
__Storage__(); extern PIP_EXPORT PIMap<PIConstChars, PICodeInfo::AccessValueFunction> * accessValueFunctions;
~__Storage__(); extern PIP_EXPORT PIMap<PIConstChars, PICodeInfo::AccessTypeFunction> * accessTypeFunctions;
public:
static __Storage__ * instance();
PIMap<PIConstChars, PICodeInfo::ClassInfo *> * classesInfo;
PIMap<PIConstChars, PICodeInfo::EnumInfo *> * enumsInfo;
PIMap<PIConstChars, PICodeInfo::AccessValueFunction> * accessValueFunctions;
PIMap<PIConstChars, PICodeInfo::AccessTypeFunction> * accessTypeFunctions;
private:
NO_COPY_CLASS(__Storage__)
};
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); }
//! \~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::AccessValueFunction> & accessValueFunctions() {
return *(__Storage__::instance()->accessValueFunctions);
}
static const PIMap<PIConstChars, PICodeInfo::AccessTypeFunction> & accessTypeFunctions() {
return *(__Storage__::instance()->accessTypeFunctions);
}
};
#define PICODEINFO PICodeInfo::__StorageAccess__
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()") {
return __Storage__::instance()->enumsInfo;
}
};
static EnumsInfoInterface enumsInfo;
class PIP_EXPORT AccessValueFunctionInterface {
public:
const PIMap<PIConstChars, PICodeInfo::AccessValueFunction> * operator->() const DEPRECATEDM("use PICODEINFO::accessValueFunctions()") {
return __Storage__::instance()->accessValueFunctions;
}
};
static AccessValueFunctionInterface accessValueFunctions;
class PIP_EXPORT AccessTypeFunctionInterface {
public:
const PIMap<PIConstChars, PICodeInfo::AccessTypeFunction> * operator->() const DEPRECATEDM("use PICODEINFO::accessTypeFunctions()") {
return __Storage__::instance()->accessTypeFunctions;
}
};
static AccessTypeFunctionInterface accessTypeFunctions;
STATIC_INITIALIZER_BEGIN
NO_UNUSED(classesInfo);
NO_UNUSED(enumsInfo);
NO_UNUSED(accessValueFunctions);
NO_UNUSED(accessTypeFunctions);
STATIC_INITIALIZER_END
inline PIByteArray getMemberValue(const void * p, const char * class_name, const char * member_name) { inline PIByteArray getMemberValue(const void * p, const char * class_name, const char * member_name) {
if (!p || !class_name || !member_name) return PIByteArray(); if (!p || !class_name || !member_name || !accessValueFunctions) return PIByteArray();
AccessValueFunction af = PICODEINFO::accessValueFunctions().value(class_name, (AccessValueFunction)0); AccessValueFunction af = accessValueFunctions->value(class_name, (AccessValueFunction)0);
if (!af) return PIByteArray(); if (!af) return PIByteArray();
return af(p, member_name); return af(p, member_name);
} }
inline const char * getMemberType(const char * class_name, const char * member_name) { inline const char * getMemberType(const char * class_name, const char * member_name) {
if (!class_name || !member_name) return ""; if (!class_name || !member_name || !accessTypeFunctions) return "";
AccessTypeFunction af = PICODEINFO::accessTypeFunctions().value(class_name, (AccessTypeFunction)0); AccessTypeFunction af = accessTypeFunctions->value(class_name, (AccessTypeFunction)0);
if (!af) return ""; if (!af) return "";
return af(member_name); return af(member_name);
} }
@@ -377,15 +183,27 @@ inline const char * getMemberType(const char * class_name, const char * member_n
PIP_EXPORT PIVariant getMemberAsVariant(const void * p, const char * class_name, const char * member_name); PIP_EXPORT PIVariant getMemberAsVariant(const void * p, const char * class_name, const char * member_name);
template<typename T, typename std::enable_if<std::is_assignable<T &, const T &>::value, int>::type = 0> template<typename T, typename std::enable_if< std::is_assignable<T&, const T&>::value, int>::type = 0>
void serialize(PIByteArray & ret, const T & v) { void serialize(PIByteArray & ret, const T & v) {ret << v;}
ret << v;
}
template<typename T, typename std::enable_if<!std::is_assignable<T &, const T &>::value, int>::type = 0> template<typename T, typename std::enable_if<!std::is_assignable<T&, const T&>::value, int>::type = 0>
void serialize(PIByteArray & ret, const T & v) {} void serialize(PIByteArray & ret, const T & v) {}
} // namespace PICodeInfo }
class PIP_EXPORT __PICodeInfoInitializer__ {
public:
__PICodeInfoInitializer__() {
if (_inited_) return;
_inited_ = true;
PICodeInfo::classesInfo = new PIMap<PIConstChars, PICodeInfo::ClassInfo * >;
PICodeInfo::enumsInfo = new PIMap<PIConstChars, PICodeInfo::EnumInfo * >;
PICodeInfo::accessValueFunctions = new PIMap<PIConstChars, PICodeInfo::AccessValueFunction>;
PICodeInfo::accessTypeFunctions = new PIMap<PIConstChars, PICodeInfo::AccessTypeFunction>;
}
static bool _inited_;
};
static __PICodeInfoInitializer__ __picodeinfoinitializer__;
#endif // PICODEINFO_H #endif // PICODEINFO_H

View File

@@ -1,20 +1,20 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Module includes Module includes
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
//! \defgroup Code Code //! \defgroup Code Code
//! \~\brief //! \~\brief
@@ -35,11 +35,9 @@
//! //!
//! \~english //! \~english
//! These files provides parsing C++ code and storage to use results of \a pip_cmg utility. //! These files provides parsing C++ code and storage to use results of \a pip_cmg utility.
//! See \ref code_model.
//! //!
//! \~russian //! \~russian
//! Эти файлы обеспечивают разбор C++ кода и хранение результатов работы утилиты \a pip_cmg. //! Эти файлы обеспечивают разбор C++ кода и хранение результатов работы утилиты \a pip_cmg.
//! Подробнее \ref code_model.
//! //!
//! \~\authors //! \~\authors
//! \~english //! \~english

View File

@@ -1,25 +1,26 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
C++ code parser C++ code parser
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "picodeparser.h" #include "picodeparser.h"
PIString PICodeParser::Macro::expand(PIString args_, bool * ok) const { PIString PICodeParser::Macro::expand(PIString args_, bool * ok) const {
PIStringList arg_vals; PIStringList arg_vals;
while (!args_.isEmpty()) { while (!args_.isEmpty()) {
@@ -31,32 +32,29 @@ PIString PICodeParser::Macro::expand(PIString args_, bool * ok) const {
PIString ca; PIString ca;
if (bi >= 0 && bi < ci) { if (bi >= 0 && bi < ci) {
ca = args_.left(args_.takeLeft(bi).toInt()); ca = args_.left(args_.takeLeft(bi).toInt());
ci -= ca.size_s(); ci -= ca.size_s(); bi -= ca.size_s();
bi -= ca.size_s();
ca += '(' + args_.takeRange('(', ')') + ')'; ca += '(' + args_.takeRange('(', ')') + ')';
} else { } else {
ca = args_.takeLeft(ci); ca = args_.takeLeft(ci);
} }
arg_vals << ca; arg_vals << ca;
args_.trim(); args_.trim(); args_.takeLeft(1); args_.trim();
args_.takeLeft(1);
args_.trim();
} }
if (args.size() != arg_vals.size()) { if (args.size() != arg_vals.size()) {
piCout << ("Error: in expansion of macro \"" + name + '(' + args.join(", ") + ")\": expect") << args.size() << "arguments but takes" piCout << ("Error: in expansion of macro \"" + name + '(' + args.join(", ") + ")\": expect")
<< arg_vals.size() << "!"; << args.size() << "arguments but takes" << arg_vals.size() << "!";
if (ok != 0) *ok = false; if (ok != 0) *ok = false;
return PIString(); return PIString();
} }
PIString ret = value; PIString ret = value;
for (int i = 0; i < args.size_s(); ++i) { for (int i = 0; i < args.size_s(); ++i) {
const PIString &an(args[i]), av(arg_vals[i]); const PIString & an(args[i]), av(arg_vals[i]);
int ind(-1); int ind(-1);
while ((ind = ret.find(an, ind + 1)) >= 0) { while ((ind = ret.find(an, ind + 1)) >= 0) {
PIChar ppc, pc, nc; PIChar ppc, pc, nc;
if (ind > 1) ppc = ret[ind - 2]; if (ind > 1) ppc = ret[ind - 2];
if (ind > 0) pc = ret[ind - 1]; if (ind > 0) pc = ret[ind - 1];
if (ind + an.size_s() < ret.size_s()) nc = ret.mid(ind + an.size_s(), 1)[0]; if (ind + an.size_s() < ret.size_s()) nc = ret.mid(ind + an.size_s(),1)[0];
if (ppc != '#' && pc == '#' && !_isCChar(nc)) { // to chars if (ppc != '#' && pc == '#' && !_isCChar(nc)) { // to chars
ind--; ind--;
ret.replace(ind, an.size_s() + 1, '\"' + av + '\"'); ret.replace(ind, an.size_s() + 1, '\"' + av + '\"');
@@ -74,8 +72,9 @@ PIString PICodeParser::Macro::expand(PIString args_, bool * ok) const {
} }
PICodeParser::PICodeParser() { PICodeParser::PICodeParser() {
macros_iter = 32; macros_iter = 32;
with_includes = true; with_includes = true;
clear(); clear();
includes << ""; includes << "";
@@ -87,66 +86,62 @@ void PICodeParser::parseFile(const PIString & file, bool follow_includes) {
parseFileInternal(file, follow_includes); parseFileInternal(file, follow_includes);
/*piCout << "\n\n"; /*piCout << "\n\n";
piForeachC (Entity * c, entities) { piForeachC (Entity * c, entities) {
piCout << ""; piCout << "";
piCout << c->type << c->name << c->parent_scope << c->parents << c->children << c->meta; piCout << c->type << c->name << c->parent_scope << c->parents << c->children << c->meta;
if (c->parent_scope) if (c->parent_scope)
piCout << "parent" << c->parent_scope->name; piCout << "parent" << c->parent_scope->name;
piCout << "Functions:"; piCout << "Functions:";
piForeachC (Member & m, c->functions) piForeachC (Member & m, c->functions)
piCout << m.type << m.name << m.meta; piCout << m.type << m.name << m.meta;
piCout << "Members:"; piCout << "Members:";
piForeachC (Member & m, c->members) piForeachC (Member & m, c->members)
piCout << m.type << m.name << m.meta; piCout << m.type << m.name << m.meta;
} }
piCout << "\n\nDefines:"; piCout << "\n\nDefines:";
piForeachC (Define & m, defines) piForeachC (Define & m, defines)
piCout << PIStringAscii("define") << m.first << m.second; piCout << PIStringAscii("define") << m.first << m.second;
piCout << "\n\nMacros:"; piCout << "\n\nMacros:";
piForeachC (Macro & m, macros) piForeachC (Macro & m, macros)
piCout << "Macro:" << m.name << m.args << m.value; piCout << "Macro:" << m.name << m.args << m.value;
piCout << "\n\nClasses:"; piCout << "\n\nClasses:";
piCout << "\n\nEnums:"; piCout << "\n\nEnums:";
piForeachC (Enum & c, enums) { piForeachC (Enum & c, enums) {
piCout << PIStringAscii("enum") << c.name << c.meta; piCout << PIStringAscii("enum") << c.name << c.meta;
piForeachC (EnumeratorInfo & e, c.members) piForeachC (EnumeratorInfo & e, c.members)
piCout << " " << e.name << '=' << e.value << e.meta; piCout << " " << e.name << '=' << e.value << e.meta;
} }
piCout << "\n\nTypedefs:"; piCout << "\n\nTypedefs:";
piForeachC (Typedef & c, typedefs)
piCout << PIStringAscii("typedef") << c;*/
}
void PICodeParser::parseFiles(const PIStringList & files, bool follow_includes) {
clear();
piForeachC(PIString & f, files)
parseFileInternal(f, follow_includes);
/*piCout << "\n\nDefines:";
piForeachC (Define & m, defines)
piCout << PIStringAscii("define") << m.first << m.second;
piCout << "\n\nMacros:";
piForeachC (Macro & m, macros)
piCout << "Macro:" << m.name << m.args << m.value;
piCout << "\n\nClasses:";
piForeachC (Entity * c, entities)
piCout << PIStringAscii("class") << c->name << c->parents;
piCout << "\n\nEnums:";
piForeachC (Enum & c, enums)
piCout << PIStringAscii("enum") << c.name << c.members;
piCout << "\n\nTypedefs:";
piForeachC (Typedef & c, typedefs) piForeachC (Typedef & c, typedefs)
piCout << PIStringAscii("typedef") << c;*/ piCout << PIStringAscii("typedef") << c;*/
} }
void PICodeParser::parseFileContent(PIString fc) { void PICodeParser::parseFiles(const PIStringList & files, bool follow_includes) {
parseFileContent(fc, false); clear();
piForeachC (PIString & f, files)
parseFileInternal(f, follow_includes);
/*piCout << "\n\nDefines:";
piForeachC (Define & m, defines)
piCout << PIStringAscii("define") << m.first << m.second;
piCout << "\n\nMacros:";
piForeachC (Macro & m, macros)
piCout << "Macro:" << m.name << m.args << m.value;
piCout << "\n\nClasses:";
piForeachC (Entity * c, entities)
piCout << PIStringAscii("class") << c->name << c->parents;
piCout << "\n\nEnums:";
piForeachC (Enum & c, enums)
piCout << PIStringAscii("enum") << c.name << c.members;
piCout << "\n\nTypedefs:";
piForeachC (Typedef & c, typedefs)
piCout << PIStringAscii("typedef") << c;*/
} }
bool PICodeParser::isEnum(const PIString & name) { bool PICodeParser::isEnum(const PIString & name) {
piForeachC(Enum & e, enums) piForeachC (Enum & e, enums)
if (e.name == name) return true; if (e.name == name)
return true;
return false; return false;
} }
@@ -154,19 +149,19 @@ bool PICodeParser::isEnum(const PIString & name) {
bool PICodeParser::parseFileInternal(const PIString & file, bool follow_includes) { bool PICodeParser::parseFileInternal(const PIString & file, bool follow_includes) {
if (proc_files[file]) return true; if (proc_files[file]) return true;
with_includes = follow_includes; with_includes = follow_includes;
cur_file = file; cur_file = file;
PIFile f(file, PIIODevice::ReadOnly); PIFile f(file, PIIODevice::ReadOnly);
int ii = 0; int ii = 0;
while (!f.isOpened() && ii < (includes.size_s() - 1)) { while (!f.isOpened() && ii < (includes.size_s() - 1)) {
f.setPath(includes[++ii] + '/' + file); f.setPath(includes[++ii] + '/' + file);
// piCout << "try" << f.path(); //piCout << "try" << f.path();
f.open(PIIODevice::ReadOnly); f.open(PIIODevice::ReadOnly);
} }
if (!f.isOpened()) { if (!f.isOpened()) {
piCout << ("Error: can`t open file \"" + file + "\"!"); piCout << ("Error: can`t open file \"" + file + "\"!");
return false; return false;
} }
// piCout << "add" << file; //piCout << "add" << file;
proc_files << f.path(); proc_files << f.path();
PIString fc = PIString::fromUTF8(f.readAll()); PIString fc = PIString::fromUTF8(f.readAll());
piCout << "parsing" << f.path() << "..."; piCout << "parsing" << f.path() << "...";
@@ -179,8 +174,7 @@ bool PICodeParser::parseFileInternal(const PIString & file, bool follow_includes
void PICodeParser::clear() { void PICodeParser::clear() {
piForeach(Entity * i, entities) piForeach (Entity * i, entities) delete i;
delete i;
defines.clear(); defines.clear();
macros.clear(); macros.clear();
enums.clear(); enums.clear();
@@ -190,145 +184,40 @@ void PICodeParser::clear() {
cur_namespace.clear(); cur_namespace.clear();
main_file.clear(); main_file.clear();
evaluator.clearCustomVariables(); evaluator.clearCustomVariables();
cur_def_vis = Global; cur_def_vis = Global;
anon_num = 0; anon_num = 0;
PIStringList defs = PIStringAscii(PICODE_DEFINES).split(","); PIStringList defs = PIStringAscii(PICODE_DEFINES).split(",");
piForeachC(PIString & d, defs) piForeachC (PIString & d, defs)
defines << Define(d, ""); defines << Define(d, "");
defines << Define(PIStringAscii("PICODE"), "") << custom_defines; defines << Define(PIStringAscii("PICODE"), "") << custom_defines;
macros << Macro(PIStringAscii("PIOBJECT"), "", PIStringList() << "name") macros << Macro(PIStringAscii("PIOBJECT"), "", PIStringList() << "name")
<< Macro(PIStringAscii("PIOBJECT_PARENT"), "", PIStringList() << "parent") << Macro(PIStringAscii("PIOBJECT_PARENT"), "", PIStringList() << "parent")
<< Macro(PIStringAscii("PIOBJECT_SUBCLASS"), << Macro(PIStringAscii("PIOBJECT_SUBCLASS"), "", PIStringList() << "name" << "parent")
"",
PIStringList() << "name"
<< "parent")
<< Macro(PIStringAscii("PIIODEVICE"), "", PIStringList() << "name") << Macro(PIStringAscii("PIIODEVICE"), "", PIStringList() << "name")
<< Macro(PIStringAscii("NO_COPY_CLASS"), "", PIStringList() << "name") << Macro(PIStringAscii("PRIVATE_DECLARATION")) << Macro(PIStringAscii("NO_COPY_CLASS"), "", PIStringList() << "name")
<< Macro(PIStringAscii("PRIVATE_DECLARATION"))
<< Macro(PIStringAscii("EVENT"), "void name();", PIStringList() << "name") << Macro(PIStringAscii("EVENT" ), "void name();", PIStringList() << "name")
<< Macro(PIStringAscii("EVENT0"), "void name();", PIStringList() << "name") << Macro(PIStringAscii("EVENT0"), "void name();", PIStringList() << "name")
<< Macro(PIStringAscii("EVENT1"), << Macro(PIStringAscii("EVENT1"), "void name(a0 n0);", PIStringList() << "name" << "a0" << "n0")
"void name(a0 n0);", << Macro(PIStringAscii("EVENT2"), "void name(a0 n0, a1 n1);", PIStringList() << "name" << "a0" << "n0" << "a1" << "n1")
PIStringList() << "name" << Macro(PIStringAscii("EVENT3"), "void name(a0 n0, a1 n1, a2 n2);", PIStringList() << "name" << "a0" << "n0" << "a1" << "n1" << "a2" << "n2")
<< "a0" << Macro(PIStringAscii("EVENT4"), "void name(a0 n0, a1 n1, a2 n2, a3 n3);", PIStringList() << "name" << "a0" << "n0" << "a1" << "n1" << "a2" << "n2" << "a3" << "n3")
<< "n0")
<< Macro(PIStringAscii("EVENT2"),
"void name(a0 n0, a1 n1);",
PIStringList() << "name"
<< "a0"
<< "n0"
<< "a1"
<< "n1")
<< Macro(PIStringAscii("EVENT3"),
"void name(a0 n0, a1 n1, a2 n2);",
PIStringList() << "name"
<< "a0"
<< "n0"
<< "a1"
<< "n1"
<< "a2"
<< "n2")
<< Macro(PIStringAscii("EVENT4"),
"void name(a0 n0, a1 n1, a2 n2, a3 n3);",
PIStringList() << "name"
<< "a0"
<< "n0"
<< "a1"
<< "n1"
<< "a2"
<< "n2"
<< "a3"
<< "n3")
<< Macro(PIStringAscii("EVENT_HANDLER"), << Macro(PIStringAscii("EVENT_HANDLER" ), "ret name()", PIStringList() << "ret" << "name")
"ret name()", << Macro(PIStringAscii("EVENT_HANDLER0"), "ret name()", PIStringList() << "ret" << "name")
PIStringList() << "ret" << Macro(PIStringAscii("EVENT_HANDLER1"), "ret name(a0 n0)", PIStringList() << "ret" << "name" << "a0" << "n0")
<< "name") << Macro(PIStringAscii("EVENT_HANDLER2"), "ret name(a0 n0, a1 n1)", PIStringList() << "ret" << "name" << "a0" << "n0" << "a1" << "n1")
<< Macro(PIStringAscii("EVENT_HANDLER0"), << Macro(PIStringAscii("EVENT_HANDLER3"), "ret name(a0 n0, a1 n1, a2 n2)", PIStringList() << "ret" << "name" << "a0" << "n0" << "a1" << "n1" << "a2" << "n2")
"ret name()", << Macro(PIStringAscii("EVENT_HANDLER4"), "ret name(a0 n0, a1 n1, a2 n2, a3 n3)", PIStringList() << "ret" << "name" << "a0" << "n0" << "a1" << "n1" << "a2" << "n2" << "a3" << "n3")
PIStringList() << "ret"
<< "name")
<< Macro(PIStringAscii("EVENT_HANDLER1"),
"ret name(a0 n0)",
PIStringList() << "ret"
<< "name"
<< "a0"
<< "n0")
<< Macro(PIStringAscii("EVENT_HANDLER2"),
"ret name(a0 n0, a1 n1)",
PIStringList() << "ret"
<< "name"
<< "a0"
<< "n0"
<< "a1"
<< "n1")
<< Macro(PIStringAscii("EVENT_HANDLER3"),
"ret name(a0 n0, a1 n1, a2 n2)",
PIStringList() << "ret"
<< "name"
<< "a0"
<< "n0"
<< "a1"
<< "n1"
<< "a2"
<< "n2")
<< Macro(PIStringAscii("EVENT_HANDLER4"),
"ret name(a0 n0, a1 n1, a2 n2, a3 n3)",
PIStringList() << "ret"
<< "name"
<< "a0"
<< "n0"
<< "a1"
<< "n1"
<< "a2"
<< "n2"
<< "a3"
<< "n3")
<< Macro(PIStringAscii("EVENT_VHANDLER"), << Macro(PIStringAscii("EVENT_VHANDLER" ), "virtual ret name()", PIStringList() << "ret" << "name")
"virtual ret name()", << Macro(PIStringAscii("EVENT_VHANDLER0"), "virtual ret name()", PIStringList() << "ret" << "name")
PIStringList() << "ret" << Macro(PIStringAscii("EVENT_VHANDLER1"), "virtual ret name(a0 n0)", PIStringList() << "ret" << "name" << "a0" << "n0")
<< "name") << Macro(PIStringAscii("EVENT_VHANDLER2"), "virtual ret name(a0 n0, a1 n1)", PIStringList() << "ret" << "name" << "a0" << "n0" << "a1" << "n1")
<< Macro(PIStringAscii("EVENT_VHANDLER0"), << Macro(PIStringAscii("EVENT_VHANDLER3"), "virtual ret name(a0 n0, a1 n1, a2 n2)", PIStringList() << "ret" << "name" << "a0" << "n0" << "a1" << "n1" << "a2" << "n2")
"virtual ret name()", << Macro(PIStringAscii("EVENT_VHANDLER4"), "virtual ret name(a0 n0, a1 n1, a2 n2, a3 n3)", PIStringList() << "ret" << "name" << "a0" << "n0" << "a1" << "n1" << "a2" << "n2" << "a3" << "n3")
PIStringList() << "ret" ;
<< "name")
<< Macro(PIStringAscii("EVENT_VHANDLER1"),
"virtual ret name(a0 n0)",
PIStringList() << "ret"
<< "name"
<< "a0"
<< "n0")
<< Macro(PIStringAscii("EVENT_VHANDLER2"),
"virtual ret name(a0 n0, a1 n1)",
PIStringList() << "ret"
<< "name"
<< "a0"
<< "n0"
<< "a1"
<< "n1")
<< Macro(PIStringAscii("EVENT_VHANDLER3"),
"virtual ret name(a0 n0, a1 n1, a2 n2)",
PIStringList() << "ret"
<< "name"
<< "a0"
<< "n0"
<< "a1"
<< "n1"
<< "a2"
<< "n2")
<< Macro(PIStringAscii("EVENT_VHANDLER4"),
"virtual ret name(a0 n0, a1 n1, a2 n2, a3 n3)",
PIStringList() << "ret"
<< "name"
<< "a0"
<< "n0"
<< "a1"
<< "n1"
<< "a2"
<< "n2"
<< "a3"
<< "n3");
} }
@@ -349,7 +238,7 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
char c = 0, pc = 0; char c = 0, pc = 0;
PIString pfc, line, ccmn, tmp; PIString pfc, line, ccmn, tmp;
PIMap<PIString, PIString> cchars; PIMap<PIString, PIString> cchars;
/// Remove comments, join multiline '*' and replace '*' to $n (cchars) /// Remove comments, join multiline '*' and replace '*' to $n (cchars)
fc.replaceAll("\r\n", '\n'); fc.replaceAll("\r\n", '\n');
fc.replaceAll('\r', '\n'); fc.replaceAll('\r', '\n');
@@ -358,8 +247,7 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
if (i > 0) pc = c; if (i > 0) pc = c;
c = fc[i].toAscii(); c = fc[i].toAscii();
if (c == '"' && !mlc && pc != '\'') { if (c == '"' && !mlc && pc != '\'') {
if (i > 0) if (i > 0) if (fc[i - 1] == '\\') continue;
if (fc[i - 1] == '\\') continue;
cc = !cc; cc = !cc;
continue; continue;
} }
@@ -370,62 +258,46 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
continue; continue;
} }
if (cc) continue; if (cc) continue;
if (fc.mid(i, 2) == "/*") { if (fc.mid(i, 2) == "/*") {mlc = true; mls = i; ++i; continue;}
mlc = true; if (fc.mid(i, 2) == "*/" && mlc) {mlc = false; fc.cutMid(mls, i - mls + 2); i = mls - 1; continue;}
mls = i; if (fc.mid(i, 2) == "//" && !mlc) {ole = fc.find('\n', i); fc.cutMid(i, ole < 0 ? -1 : ole - i); --i; continue;}
++i;
continue;
}
if (fc.mid(i, 2) == "*/" && mlc) {
mlc = false;
fc.cutMid(mls, i - mls + 2);
i = mls - 1;
continue;
}
if (fc.mid(i, 2) == "//" && !mlc) {
ole = fc.find('\n', i);
fc.cutMid(i, ole < 0 ? -1 : ole - i);
--i;
continue;
}
} }
pfc = procMacros(fc); pfc = procMacros(fc);
if (main) return true; if (main) return true;
bool replaced = true; bool replaced = true;
int replaced_cnt = 0; int replaced_cnt = 0;
while (replaced) { while (replaced) {
// piCout << "MACRO iter" << replaced_cnt; //piCout << "MACRO iter" << replaced_cnt;
if (replaced_cnt >= macros_iter) { if (replaced_cnt >= macros_iter) {
piCout << "Error: recursive macros detected!"; piCout << "Error: recursive macros detected!";
break; // return false; break;//return false;
} }
replaced_cnt++; replaced_cnt++;
replaced = false; replaced = false;
piForeachC(Define & d, defines) { piForeachC (Define & d, defines) {
int ind(-1); int ind(-1);
while ((ind = pfc.find(d.first, ind + 1)) >= 0) { while ((ind = pfc.find(d.first, ind + 1)) >= 0) {
PIChar pc, nc; PIChar pc, nc;
if (ind > 0) pc = pfc[ind - 1]; if (ind > 0) pc = pfc[ind - 1];
if (ind + d.first.size_s() < pfc.size_s()) nc = pfc.mid(ind + d.first.size_s(), 1)[0]; if (ind + d.first.size_s() < pfc.size_s()) nc = pfc.mid(ind + d.first.size_s(),1)[0];
if (_isCChar(pc) || _isCChar(nc) || nc.isDigit()) continue; if (_isCChar(pc) || _isCChar(nc) || nc.isDigit()) continue;
pfc.replace(ind, d.first.size_s(), d.second); pfc.replace(ind, d.first.size_s(), d.second);
ind -= d.first.size_s() - d.second.size_s(); ind -= d.first.size_s() - d.second.size_s();
replaced = true; replaced = true;
} }
} }
piForeachC(Macro & m, macros) { piForeachC (Macro & m, macros) {
int ind(-1); int ind(-1);
while ((ind = pfc.find(m.name, ind + 1)) >= 0) { while ((ind = pfc.find(m.name, ind + 1)) >= 0) {
PIChar pc, nc; PIChar pc, nc;
if (ind > 0) pc = pfc[ind - 1]; if (ind > 0) pc = pfc[ind - 1];
if (ind + m.name.size_s() < pfc.size_s()) nc = pfc.mid(ind + m.name.size_s(), 1)[0]; if (ind + m.name.size_s() < pfc.size_s()) nc = pfc.mid(ind + m.name.size_s(),1)[0];
if (_isCChar(pc) || _isCChar(nc) || nc.isDigit()) continue; if (_isCChar(pc) || _isCChar(nc) || nc.isDigit()) continue;
PIString ret, range; PIString ret, range; bool ok(false);
bool ok(false);
range = pfc.mid(ind + m.name.size_s()).takeRange('(', ')'); range = pfc.mid(ind + m.name.size_s()).takeRange('(', ')');
ret = m.expand(range, &ok); ret = m.expand(range, &ok);
if (!ok) return false; if (!ok) return false;
int rlen = pfc.find(range, ind + m.name.size_s()) + range.size_s() + 1 - ind; int rlen = pfc.find(range, ind + m.name.size_s()) + range.size_s() + 1 - ind;
pfc.replace(ind, rlen, ret); pfc.replace(ind, rlen, ret);
@@ -436,10 +308,9 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
} }
replaceMeta(pfc); replaceMeta(pfc);
// piCout << PICoutManipulators::NewLine << "file" << cur_file << pfc; //piCout << PICoutManipulators::NewLine << "file" << cur_file << pfc;
int pl = -1; int pl = -1;
cur_def_vis = Global;
while (!pfc.isEmpty()) { while (!pfc.isEmpty()) {
pfc.trim(); pfc.trim();
int nl = pfc.size_s(); int nl = pfc.size_s();
@@ -450,7 +321,6 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
PIString prev_namespace = cur_namespace, ccmn; PIString prev_namespace = cur_namespace, ccmn;
cur_namespace += pfc.takeCWord() + s_ns; cur_namespace += pfc.takeCWord() + s_ns;
ccmn = pfc.takeRange('{', '}'); ccmn = pfc.takeRange('{', '}');
// piCout << "namespace" << cur_namespace;
parseClass(0, ccmn, true); parseClass(0, ccmn, true);
cur_namespace = prev_namespace; cur_namespace = prev_namespace;
continue; continue;
@@ -460,25 +330,14 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
pfc.takeRange('<', '>'); pfc.takeRange('<', '>');
bool def = !isDeclaration(pfc, 0, &end); bool def = !isDeclaration(pfc, 0, &end);
pfc.cutLeft(end); pfc.cutLeft(end);
if (def) if (def) pfc.takeRange('{', '}');
pfc.takeRange('{', '}'); else pfc.takeSymbol();
else
pfc.takeSymbol();
continue; continue;
} }
if (pfc.left(5) == s_class || pfc.left(6) == s_struct || pfc.left(5) == s_union) { if (pfc.left(5) == s_class || pfc.left(6) == s_struct || pfc.left(5) == s_union) {
int dind = pfc.find('{', 0), find = pfc.find(';', 0); int dind = pfc.find('{', 0), find = pfc.find(';', 0);
// piCout << pfc.left(6) << dind << find; if (dind < 0 && find < 0) {pfc.cutLeft(6); continue;}
if (dind < 0 && find < 0) { if (dind < 0 || find < dind) {pfc.cutLeft(6); continue;}
pfc.cutLeft(6);
continue;
}
if (dind < 0 || find < dind) {
// piCout << "skip FC" << (find + 1) << pfc.left(find + 1);
pfc.cutLeft(find + 1);
// pfc.cutLeft(6);
continue;
}
ccmn = pfc.left(dind) + s_bo + pfc.mid(dind).takeRange('{', '}') + s_bc; ccmn = pfc.left(dind) + s_bo + pfc.mid(dind).takeRange('{', '}') + s_bc;
pfc.remove(0, ccmn.size()); pfc.remove(0, ccmn.size());
parseClass(0, ccmn, false); parseClass(0, ccmn, false);
@@ -489,12 +348,6 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
tmp = pfc.takeCWord(); tmp = pfc.takeCWord();
pfc.trim(); pfc.trim();
MetaMap meta = maybeMeta(pfc); MetaMap meta = maybeMeta(pfc);
if (tmp == s_class || tmp == s_struct) {
tmp = pfc.takeCWord();
pfc.trim();
MetaMap smeta = maybeMeta(pfc);
meta << smeta;
}
parseEnum(0, cur_namespace + tmp, pfc.takeRange('{', '}'), meta); parseEnum(0, cur_namespace + tmp, pfc.takeRange('{', '}'), meta);
pfc.takeSymbol(); pfc.takeSymbol();
continue; continue;
@@ -502,10 +355,8 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
if (pfc.left(7) == s_typedef) { if (pfc.left(7) == s_typedef) {
pfc.cutLeft(7); pfc.cutLeft(7);
typedefs << parseTypedef(pfc.takeLeft(pfc.find(';'))); typedefs << parseTypedef(pfc.takeLeft(pfc.find(';')));
if (typedefs.back().first.isEmpty()) if (typedefs.back().first.isEmpty()) typedefs.pop_back();
typedefs.pop_back(); else root_.typedefs << typedefs.back();
else
root_.typedefs << typedefs.back();
pfc.takeSymbol(); pfc.takeSymbol();
continue; continue;
} }
@@ -523,7 +374,7 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
} }
parseMember(&root_, str); parseMember(&root_, str);
} }
return true; return true;
} }
@@ -532,49 +383,43 @@ PICodeParser::Entity * PICodeParser::parseClassDeclaration(const PIString & fc)
static const PIString s_ss = PIStringAscii(" "); static const PIString s_ss = PIStringAscii(" ");
static const PIString s_M = PIStringAscii("$M"); static const PIString s_M = PIStringAscii("$M");
static const PIString s_class = PIStringAscii("class"); static const PIString s_class = PIStringAscii("class");
PIString cd = fc.trimmed().removeAll('\n').replaceAll('\t', ' ').replaceAll(s_ss, ' '), pn; PIString cd = fc.trimmed().removeAll('\n').replaceAll('\t', ' ').replaceAll(s_ss, ' '), pn;
MetaMap meta; MetaMap meta;
int ind = cd.find(s_M); int ind = cd.find(s_M);
if (ind >= 0) { if (ind >= 0) {
meta = tmp_meta.value(cd.takeMid(ind, 5)); meta = tmp_meta.value(cd.takeMid(ind, 5));
cd.replaceAll(s_ss, ' '); cd.replaceAll(s_ss, ' ');
} }
// piCout << "found class <****\n" << cd << "\n****>"; //piCout << "found class <****\n" << cd << "\n****>";
ind = cd.find(':'); ind = cd.find(':');
PIVector<Entity *> parents; PIVector<Entity * > parents;
if (ind > 0) { if (ind > 0) {
PIStringList pl = cd.takeMid(ind + 1).trim().split(','); PIStringList pl = cd.takeMid(ind + 1).trim().split(',');
cd.cutRight(1); cd.cutRight(1);
Entity * pe = 0; Entity * pe = 0;
piForeachC(PIString & p, pl) { piForeachC (PIString & p, pl) {
if (p.contains(' ')) if (p.contains(' ')) pn = p.mid(p.find(' ') + 1);
pn = p.mid(p.find(' ') + 1); else pn = p;
else
pn = p;
pe = findEntityByName(pn); pe = findEntityByName(pn);
if (pe == 0) if (pe == 0) ;//{piCout << "Error: can`t find" << pn;}
; //{piCout << "Error: can`t find" << pn;} else parents << pe;
else
parents << pe;
} }
} }
PIString typename_ = cd.left(6).trim(); PIString typename_ = cd.left(6).trim();
bool is_class = typename_ == s_class; bool is_class = typename_ == s_class;
Visibility vis = cur_def_vis; cur_def_vis = (is_class ? Private : Public);
cur_def_vis = (is_class ? Private : Public); PIString cn = cd.mid(6).trim();
PIString cn = cd.mid(6).trim(); bool has_name = !cn.isEmpty();
bool has_name = !cn.isEmpty();
if (cn.isEmpty()) cn = PIStringAscii("<unnamed_") + PIString::fromNumber(anon_num++) + '>'; if (cn.isEmpty()) cn = PIStringAscii("<unnamed_") + PIString::fromNumber(anon_num++) + '>';
// piCout << "found " << typename_ << cn; //piCout << "found " << typename_ << cn;
if (cn.isEmpty()) return nullptr; if (cn.isEmpty()) return 0;
Entity * e = new Entity(); Entity * e = new Entity();
e->meta = meta; e->meta = meta;
e->name = cur_namespace + cn; e->name = cur_namespace + cn;
e->type = typename_; e->type = typename_;
e->has_name = has_name; e->has_name = has_name;
e->parents = parents; e->parents = parents;
e->visibility = vis; e->file = cur_file;
e->file = cur_file;
entities << e; entities << e;
return e; return e;
} }
@@ -593,44 +438,37 @@ void PICodeParser::parseClass(Entity * parent, PIString & fc, bool is_namespace)
static const PIString s_typedef = PIStringAscii("typedef"); static const PIString s_typedef = PIStringAscii("typedef");
static const PIString s_namespace = PIStringAscii("namespace"); static const PIString s_namespace = PIStringAscii("namespace");
static const PIString s_template = PIStringAscii("template"); static const PIString s_template = PIStringAscii("template");
Visibility prev_vis = cur_def_vis; Visibility prev_vis = cur_def_vis;
int dind = fc.find('{'), find = fc.find(';'), end = 0; int dind = fc.find('{'), find = fc.find(';'), end = 0;
if (dind < 0 && find < 0) return; if (dind < 0 && find < 0) return;
// piCout << "parse class <****\n" << fc << "\n****>"; if (dind < 0 || find < dind) {
fc.left(find);
return;
}
//piCout << "parse class <****\n" << fc << "\n****>";
Entity * ce = parent; Entity * ce = parent;
if (!is_namespace) { if (!is_namespace) {
ce = parseClassDeclaration(fc.takeLeft(dind)); ce = parseClassDeclaration(fc.takeLeft(dind));
fc.trim().cutLeft(1).cutRight(1).trim(); fc.trim().cutLeft(1).cutRight(1).trim();
} }
// piCout << "found class <****\n" << fc << "\n****>"; //piCout << "found class <****\n" << fc << "\n****>";
///if (!ce) return PIString();
if (ce) { if (ce) {
if (parent) parent->children << ce; if (parent) parent->children << ce;
ce->parent_scope = parent; ce->parent_scope = parent;
} }
int ps = -1; int ps = -1;
bool def = false; bool def = false;
PIString prev_namespace = cur_namespace, stmp; PIString prev_namespace = cur_namespace, stmp;
if (ce) cur_namespace += ce->name + s_ns; if (ce) cur_namespace += ce->name + s_ns;
// piCout << "parse class" << ce->name << "namespace" << cur_namespace; //piCout << "parse class" << ce->name << "namespace" << cur_namespace;
// piCout << "\nparse class" << ce->name << "namespace" << cur_namespace; //piCout << "\nparse class" << ce->name << "namespace" << cur_namespace;
while (!fc.isEmpty()) { while (!fc.isEmpty()) {
PIString cw = fc.takeCWord(), tmp; PIString cw = fc.takeCWord(), tmp;
// piCout << "\ntaked word" << cw; //piCout << "\ntaked word" << cw;
if (cw == s_public) { if (cw == s_public ) {cur_def_vis = Public; fc.cutLeft(1); continue;}
cur_def_vis = Public; if (cw == s_protected) {cur_def_vis = Protected; fc.cutLeft(1); continue;}
fc.cutLeft(1); if (cw == s_private ) {cur_def_vis = Private; fc.cutLeft(1); continue;}
continue;
}
if (cw == s_protected) {
cur_def_vis = Protected;
fc.cutLeft(1);
continue;
}
if (cw == s_private) {
cur_def_vis = Private;
fc.cutLeft(1);
continue;
}
if (cw == s_namespace) { if (cw == s_namespace) {
PIString prev_namespace = cur_namespace, ccmn; PIString prev_namespace = cur_namespace, ccmn;
cur_namespace += fc.takeCWord() + s_ns; cur_namespace += fc.takeCWord() + s_ns;
@@ -640,16 +478,15 @@ void PICodeParser::parseClass(Entity * parent, PIString & fc, bool is_namespace)
continue; continue;
} }
if (cw == s_class || cw == s_struct || cw == s_union) { if (cw == s_class || cw == s_struct || cw == s_union) {
// piCout << cw << isDeclaration(fc, 0, &end);
if (isDeclaration(fc, 0, &end)) { if (isDeclaration(fc, 0, &end)) {
fc.cutLeft(end); fc.cutLeft(end);
fc.takeSymbol(); fc.takeSymbol();
continue; continue;
} }
tmp = fc.takeLeft(fc.find('{')); tmp = fc.takeLeft(fc.find('{'));
stmp = fc.takeRange('{', '}'); stmp = fc.takeRange('{', '}');
fc.takeSymbol(); fc.takeSymbol();
stmp = cw + ' ' + tmp + '{' + stmp + '}'; stmp = cw + ' ' + tmp + '{' + stmp + '}';
parseClass(ce, stmp, false); parseClass(ce, stmp, false);
continue; continue;
} }
@@ -657,26 +494,18 @@ void PICodeParser::parseClass(Entity * parent, PIString & fc, bool is_namespace)
tmp = fc.takeCWord(); tmp = fc.takeCWord();
fc.trim(); fc.trim();
MetaMap meta = maybeMeta(fc); MetaMap meta = maybeMeta(fc);
if (tmp == s_class || tmp == s_struct) {
tmp = fc.takeCWord();
fc.trim();
MetaMap smeta = maybeMeta(fc);
meta << smeta;
}
parseEnum(ce, cur_namespace + tmp, fc.takeRange('{', '}'), meta); parseEnum(ce, cur_namespace + tmp, fc.takeRange('{', '}'), meta);
fc.takeSymbol(); fc.takeSymbol();
continue; continue;
} }
if (cw == s_friend) { if (cw == s_friend) {fc.cutLeft(fc.find(';') + 1); continue;}
fc.cutLeft(fc.find(';') + 1);
continue;
}
if (cw == s_typedef) { if (cw == s_typedef) {
if (ce) { if (ce) {
ce->typedefs << parseTypedef(fc.takeLeft(fc.find(';'))); ce->typedefs << parseTypedef(fc.takeLeft(fc.find(';')));
typedefs << ce->typedefs.back(); typedefs << ce->typedefs.back();
typedefs.back().first.insert(0, cur_namespace); typedefs.back().first.insert(0, cur_namespace);
if (ce->typedefs.back().first.isEmpty()) ce->typedefs.pop_back(); if (ce->typedefs.back().first.isEmpty())
ce->typedefs.pop_back();
} }
fc.takeSymbol(); fc.takeSymbol();
continue; continue;
@@ -685,25 +514,20 @@ void PICodeParser::parseClass(Entity * parent, PIString & fc, bool is_namespace)
fc.takeRange('<', '>'); fc.takeRange('<', '>');
def = !isDeclaration(fc, 0, &end); def = !isDeclaration(fc, 0, &end);
fc.cutLeft(end); fc.cutLeft(end);
if (def) if (def) fc.takeRange('{', '}');
fc.takeRange('{', '}'); else fc.takeSymbol();
else
fc.takeSymbol();
continue; continue;
} }
def = !isDeclaration(fc, 0, &end); def = !isDeclaration(fc, 0, &end);
tmp = (cw + fc.takeLeft(end)).trim(); tmp = (cw + fc.takeLeft(end)).trim();
if (!tmp.isEmpty() && ce) parseMember(ce, tmp); if (!tmp.isEmpty() && ce)
if (def) parseMember(ce, tmp);
fc.takeRange('{', '}'); if (def) fc.takeRange('{', '}');
else else fc.takeSymbol();
fc.takeSymbol(); if (ps == fc.size_s()) {fc.cutLeft(1);}
if (ps == fc.size_s()) {
fc.cutLeft(1);
}
ps = fc.size_s(); ps = fc.size_s();
} }
cur_def_vis = prev_vis; cur_def_vis = prev_vis;
cur_namespace = prev_namespace; cur_namespace = prev_namespace;
} }
@@ -712,7 +536,7 @@ PICodeParser::MetaMap PICodeParser::parseMeta(PIString & fc) {
PICodeParser::MetaMap ret; PICodeParser::MetaMap ret;
if (fc.isEmpty()) return ret; if (fc.isEmpty()) return ret;
PIStringList ml = fc.split(','); PIStringList ml = fc.split(',');
piForeachC(PIString & m, ml) { piForeachC (PIString & m, ml) {
int i = m.find('='); int i = m.find('=');
if (i < 0) { if (i < 0) {
ret[m.trimmed()] = PIString(); ret[m.trimmed()] = PIString();
@@ -723,61 +547,55 @@ PICodeParser::MetaMap PICodeParser::parseMeta(PIString & fc) {
ret[m.left(i).trim()] = mv; ret[m.left(i).trim()] = mv;
} }
} }
// piCout << ms << ret; //piCout << ms << ret;
return ret; return ret;
} }
bool PICodeParser::parseEnum(Entity * parent, const PIString & name, PIString fc, const MetaMap & meta) { bool PICodeParser::parseEnum(Entity * parent, const PIString & name, PIString fc, const MetaMap & meta) {
static const PIString s_ss = PIStringAscii(" "); static const PIString s_ss = PIStringAscii(" ");
static const PIString s_M = PIStringAscii("$M"); static const PIString s_M = PIStringAscii("$M");
// piCout << PIStringAscii("enum") << name << fc; //piCout << PIStringAscii("enum") << name << fc;
Enum e(name); Enum e(name);
e.meta = meta; e.meta = meta;
PIStringList vl(fc.split(',')); PIStringList vl(fc.split(','));
PIString vn; PIString vn;
int cv = -1, ind = 0; int cv = -1, ind = 0;
piForeach(PIString & v, vl) { piForeach (PIString & v, vl) {
MetaMap meta; MetaMap meta;
int mi = v.find(s_M); int mi = v.find(s_M);
if (mi >= 0) { if (mi >= 0) {
meta = tmp_meta.value(v.takeMid(mi, 5)); meta = tmp_meta.value(v.takeMid(mi, 5));
v.replaceAll(s_ss, ' '); v.replaceAll(s_ss, ' ');
} }
vn = v; vn = v; ind = v.find('=');
ind = v.find('='); if (ind > 0) {cv = v.right(v.size_s() - ind - 1).toInt(); vn = v.left(ind);}
if (ind > 0) {
cv = v.right(v.size_s() - ind - 1).toInt();
vn = v.left(ind);
}
if (ind < 0) ++cv; if (ind < 0) ++cv;
e.members << EnumeratorInfo(vn.trim(), cv, meta); e.members << EnumeratorInfo(vn.trim(), cv, meta);
} }
if (!e.members.isEmpty()) if (!e.members.isEmpty())
if (e.members.back().name.isEmpty()) e.members.pop_back(); if (e.members.back().name.isEmpty())
e.members.pop_back();
enums << e; enums << e;
return true; return true;
} }
PICodeParser::Typedef PICodeParser::parseTypedef(PIString fc) { PICodeParser::Typedef PICodeParser::parseTypedef(PIString fc) {
// piCout << "parse typedef" << fc; //piCout << "parse typedef" << fc;
Typedef td; Typedef td;
fc.replaceAll('\t', ' '); fc.replaceAll('\t', ' ');
if (fc.contains('(')) { if (fc.contains('(')) {
int start = fc.find('('), end = fc.find(')'); int start = fc.find('('), end = fc.find(')');
td.first = fc.takeMid(start + 1, end - start - 1).trim(); td.first = fc.takeMid(start + 1, end - start - 1).trim();
if (td.first.left(1) == PIChar('*')) { if (td.first.left(1) == PIChar('*')) {td.first.cutLeft(1).trim(); fc.insert(start + 1, '*');}
td.first.cutLeft(1).trim();
fc.insert(start + 1, '*');
}
td.second = fc.trim(); td.second = fc.trim();
} else { } else {
td.first = fc.takeMid(fc.findLast(' ')).trim(); td.first = fc.takeMid(fc.findLast(' ')).trim();
td.second = fc.trim(); td.second = fc.trim();
} }
// piCout << "found typedef" << td; //piCout << "found typedef" << td;
return td; return td;
} }
@@ -813,26 +631,22 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
if (fc.trim().isEmpty()) return true; if (fc.trim().isEmpty()) return true;
if (fc.find(s_operator) >= 0) return true; if (fc.find(s_operator) >= 0) return true;
tmp_temp.clear(); tmp_temp.clear();
// piCout << "parse member" << fc; //piCout << "parse member" << fc;
int ts = fc.find('<'), te = 0; int ts = fc.find('<'), te = 0;
PIString ctemp, crepl; PIString ctemp, crepl;
while (ts >= 0) { while (ts >= 0) {
ctemp = fc.mid(ts).takeRange('<', '>'); ctemp = fc.mid(ts).takeRange('<', '>');
if (ctemp.isEmpty()) { if (ctemp.isEmpty()) {te = ts + 1; ts = fc.find('<', te); continue;}
te = ts + 1;
ts = fc.find('<', te);
continue;
}
crepl = s_T + PIString::fromNumber(tmp_temp.size_s()).expandLeftTo(3, '0'); crepl = s_T + PIString::fromNumber(tmp_temp.size_s()).expandLeftTo(3, '0');
fc.replace(ts, ctemp.size_s() + 2, crepl); fc.replace(ts, ctemp.size_s() + 2, crepl);
tmp_temp[crepl] = '<' + ctemp + '>'; tmp_temp[crepl] = '<' + ctemp + '>';
ts = fc.find('<', te); ts = fc.find('<', te);
} }
fc.replaceAll('\n', ' ').replaceAll('\t', ' ').replaceAll(s_ss, ' ').replaceAll(s_cs, ',').replaceAll(s_sb, '(').replaceAll(s_sM, s_M); fc.replaceAll('\n', ' ').replaceAll('\t', ' ').replaceAll(s_ss, ' ').replaceAll(s_cs, ',').replaceAll(s_sb, '(').replaceAll(s_sM, s_M);
// piCout << "parse member" << fc; //piCout << "parse member" << fc;
PIStringList tl, al; PIStringList tl, al;
Member me; Member me;
// piCout << fc; //piCout << fc;
if (fc.contains('(')) { if (fc.contains('(')) {
MetaMap meta; MetaMap meta;
int ind = fc.find(s_M); int ind = fc.find(s_M);
@@ -842,16 +656,16 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
} }
fc.cutRight(fc.size_s() - fc.findLast(')') - 1); fc.cutRight(fc.size_s() - fc.findLast(')') - 1);
te = fc.find('('); te = fc.find('(');
// piCout << fc; //piCout << fc;
for (ts = te - 1; ts >= 0; --ts) for (ts = te - 1; ts >= 0; --ts)
if (!_isCChar(fc[ts]) && !(fc[ts].isDigit())) break; if (!_isCChar(fc[ts]) && !(fc[ts].isDigit())) break;
// piCout << "takeMid" << ts + 1 << te - ts - 1; //piCout << "takeMid" << ts + 1 << te - ts - 1;
me.meta = meta; me.meta = meta;
me.name = fc.takeMid(ts + 1, te - ts - 1); me.name = fc.takeMid(ts + 1, te - ts - 1);
if (me.name == parent->name) return true; if (me.name == parent->name) return true;
me.arguments_full = fc.takeMid(ts + 2).cutRight(1).split(','); me.arguments_full = fc.takeMid(ts + 2).cutRight(1).split(',');
me.type = fc.cutRight(1).trim(); me.type = fc.cutRight(1).trim();
me.visibility = cur_def_vis; me.visibility = cur_def_vis;
if (me.type.find(s_inline_s) >= 0) { if (me.type.find(s_inline_s) >= 0) {
me.attributes |= Inline; me.attributes |= Inline;
me.type.removeAll(s_inline_s); me.type.removeAll(s_inline_s);
@@ -866,18 +680,20 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
} }
normalizeEntityNamespace(me.type); normalizeEntityNamespace(me.type);
int i = 0; int i = 0;
// piCout << me.arguments_full; //piCout << me.arguments_full;
piForeach(PIString & a, me.arguments_full) piForeach (PIString & a, me.arguments_full)
if ((i = a.find('=')) > 0) a.cutRight(a.size_s() - i).trim(); if ((i = a.find('=')) > 0)
a.cutRight(a.size_s() - i).trim();
for (int j = 0; j < me.arguments_full.size_s(); ++j) for (int j = 0; j < me.arguments_full.size_s(); ++j)
if (me.arguments_full[j] == s_void) { if (me.arguments_full[j] == s_void) {
me.arguments_full.remove(j); me.arguments_full.remove(j);
--j; --j;
} }
me.arguments_type = me.arguments_full; me.arguments_type = me.arguments_full;
piForeach(PIString & a, me.arguments_type) { piForeach (PIString & a, me.arguments_type) {
crepl.clear(); crepl.clear();
if (a.contains('[')) crepl = a.takeMid(a.find('['), a.findLast(']') - a.find('[') + 1); if (a.contains('['))
crepl = a.takeMid(a.find('['), a.findLast(']') - a.find('[') + 1);
for (ts = a.size_s() - 1; ts >= 0; --ts) for (ts = a.size_s() - 1; ts >= 0; --ts)
if (!_isCChar(a[ts]) && !(a[ts].isDigit())) break; if (!_isCChar(a[ts]) && !(a[ts].isDigit())) break;
a.cutRight(a.size_s() - ts - 1); a.cutRight(a.size_s() - ts - 1);
@@ -886,34 +702,31 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
a.trim(); a.trim();
} }
restoreTmpTemp(&me); restoreTmpTemp(&me);
// piCout << "func" << me.type << me.name << me.arguments_full << me.arguments_type; //piCout << "func" << me.type << me.name << me.arguments_full << me.arguments_type;
parent->functions << me; parent->functions << me;
} else { } else {
if (fc.endsWith(';')) fc.cutRight(1); if (fc.endsWith(';')) fc.cutRight(1);
// piCout << "member" << fc; //piCout << "member" << fc;
if (fc.startsWith(s_using) || !(fc.contains(' ') || fc.contains('\t') || fc.contains('\n'))) return true; if (fc.startsWith(s_using) || !(fc.contains(' ') || fc.contains('\t') || fc.contains('\n'))) return true;
int bits = extractMemberBits(fc); int bits = extractMemberBits(fc);
tl = fc.split(','); tl = fc.split(',');
// piCout << "member" << fc << tl; //piCout << "member" << fc << tl;
// piCout << "member after eb" << fc << ", bits =" << bits; //piCout << "member after eb" << fc << ", bits =" << bits;
if (tl.isEmpty()) return true; if (tl.isEmpty()) return true;
piForeach(PIString & v, tl) piForeach (PIString & v, tl)
removeAssignment(v); removeAssignment(v);
bool vn = true; bool vn = true;
ctemp = tl.front().trim(); ctemp = tl.front().trim();
PIString meta_t; PIString meta_t;
if (ctemp.contains(s_M)) { if (ctemp.contains(s_M)) {
meta_t = ctemp.takeMid(ctemp.find(s_M), 5); meta_t = ctemp.takeMid(ctemp.find(s_M), 5);
ctemp.trim(); ctemp.trim();
} }
for (ts = ctemp.size_s() - 1; ts > 0; --ts) { for (ts = ctemp.size_s() - 1; ts > 0; --ts) {
if (vn) { if (vn) {if (!_isCChar(ctemp[ts]) && !ctemp[ts].isDigit() && ctemp[ts] != '[' && ctemp[ts] != ']') vn = false;}
if (!_isCChar(ctemp[ts]) && !ctemp[ts].isDigit() && ctemp[ts] != '[' && ctemp[ts] != ']') vn = false; else {if (_isCChar(ctemp[ts]) || ctemp[ts].isDigit()) break;}
} else {
if (_isCChar(ctemp[ts]) || ctemp[ts].isDigit()) break;
}
} }
me.type = ctemp.takeLeft(ts + 1); me.type = ctemp.takeLeft(ts + 1);
me.visibility = cur_def_vis; me.visibility = cur_def_vis;
ctemp += meta_t; ctemp += meta_t;
restoreTmpTemp(&me); restoreTmpTemp(&me);
@@ -941,21 +754,21 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
type.trim(); type.trim();
normalizeEntityNamespace(type); normalizeEntityNamespace(type);
tl[0] = ctemp.trim(); tl[0] = ctemp.trim();
// piCout << "vars" << tl; //piCout << "vars" << tl;
piForeachC(PIString & v, tl) { piForeachC (PIString & v, tl) {
crepl.clear(); crepl.clear();
me.name = v.trimmed(); me.name = v.trimmed();
me.type = type; me.type = type;
restoreTmpMeta(&me); restoreTmpMeta(&me);
if (me.name.isEmpty()) continue; if (me.name.isEmpty()) continue;
if (me.name.contains('[')) crepl = me.name.takeMid(me.name.find('['), me.name.findLast(']') - me.name.find('[') + 1); if (me.name.contains('['))
crepl = me.name.takeMid(me.name.find('['), me.name.findLast(']') - me.name.find('[') + 1);
while (!me.name.isEmpty()) { while (!me.name.isEmpty()) {
if (me.name.front() == PIChar('*') || me.name.front() == PIChar('&')) { if (me.name.front() == PIChar('*') || me.name.front() == PIChar('&')) {
me.type += me.name.takeLeft(1); me.type += me.name.takeLeft(1);
me.name.trim(); me.name.trim();
} else } else break;
break;
} }
me.is_type_ptr = (me.type.right(1) == PIChar(']') || me.type.right(1) == PIChar('*')); me.is_type_ptr = (me.type.right(1) == PIChar(']') || me.type.right(1) == PIChar('*'));
me.type += crepl; me.type += crepl;
@@ -965,12 +778,12 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
if (cdim.isEmpty()) break; if (cdim.isEmpty()) break;
me.dims << cdim; me.dims << cdim;
} }
// PICout(PICoutManipulators::AddAll) << "var" << me.type << me.name << me.bits; //PICout(PICoutManipulators::AddAll) << "var" << me.type << me.name << me.bits;
// piCout << "var" << v << me.type << me.name << me.bits; //piCout << "var" << v << me.type << me.name << me.bits;
parent->members << me; parent->members << me;
} }
} }
// piCout << "parse member" << fc; //piCout << "parse member" << fc;
return true; return true;
} }
@@ -1002,25 +815,13 @@ void PICodeParser::normalizeEntityNamespace(PIString & n) {
break; break;
} }
n.push_front(' '); n.push_front(' ');
if (n.find(s_s_const_s) >= 0) { if (n.find(s_s_const_s) >= 0) {n.replaceAll(s_s_const_s, ""); pref += s_const_s;}
n.replaceAll(s_s_const_s, ""); if (n.find(s_s_static_s) >= 0) {n.replaceAll(s_s_static_s, ""); pref += s_static_s;}
pref += s_const_s; if (n.find(s_s_mutable_s) >= 0) {n.replaceAll(s_s_mutable_s, ""); pref += s_mutable_s;}
} if (n.find(s_s_volatile_s) >= 0) {n.replaceAll(s_s_volatile_s, ""); pref += s_volatile_s;}
if (n.find(s_s_static_s) >= 0) {
n.replaceAll(s_s_static_s, "");
pref += s_static_s;
}
if (n.find(s_s_mutable_s) >= 0) {
n.replaceAll(s_s_mutable_s, "");
pref += s_mutable_s;
}
if (n.find(s_s_volatile_s) >= 0) {
n.replaceAll(s_s_volatile_s, "");
pref += s_volatile_s;
}
n.trim(); n.trim();
int f = 0; int f = 0;
piForeachC(Entity * e, entities) { piForeachC (Entity * e, entities) {
if (e->name == n) { if (e->name == n) {
n = (pref + n + suff).trim(); n = (pref + n + suff).trim();
return; return;
@@ -1032,20 +833,20 @@ void PICodeParser::normalizeEntityNamespace(PIString & n) {
return; return;
} }
} }
piForeachC(Enum & e, enums) { piForeachC (Enum & e, enums) {
if ((f = e.name.find(n)) >= 0) if ((f = e.name.find(n)) >= 0)
if (e.name.at(f - 1) == PIChar(':')) if (e.name.at(f - 1) == PIChar(':'))
if (e.name.find(cur_namespace) >= 0) { if (e.name.find(cur_namespace) >= 0) {
// piCout << "change" << n << "to" << e.name + suff; //piCout << "change" << n << "to" << e.name + suff;
n = pref + e.name + suff; n = pref + e.name + suff;
return; return;
} }
} }
piForeachC(Typedef & e, typedefs) { piForeachC (Typedef & e, typedefs) {
if ((f = e.first.find(n)) >= 0) if ((f = e.first.find(n)) >= 0)
if (e.first.at(f - 1) == PIChar(':')) if (e.first.at(f - 1) == PIChar(':'))
if (e.first.find(cur_namespace) >= 0) { if (e.first.find(cur_namespace) >= 0) {
// piCout << "change" << n << "to" << e.name + suff; //piCout << "change" << n << "to" << e.name + suff;
n = pref + e.first + suff; n = pref + e.first + suff;
return; return;
} }
@@ -1056,12 +857,12 @@ void PICodeParser::normalizeEntityNamespace(PIString & n) {
void PICodeParser::restoreTmpTemp(Member * e) { void PICodeParser::restoreTmpTemp(Member * e) {
static const PIString s_T = PIStringAscii("$T"); static const PIString s_T = PIStringAscii("$T");
int i = 0; int i = 0;
piForeach(PIString & a, e->arguments_full) { piForeach (PIString & a, e->arguments_full) {
while ((i = a.find(s_T)) >= 0) while ((i = a.find(s_T)) >= 0)
a.replace(i, 5, tmp_temp[a.mid(i, 5)]); a.replace(i, 5, tmp_temp[a.mid(i, 5)]);
} }
piForeach(PIString & a, e->arguments_type) { piForeach (PIString & a, e->arguments_type) {
while ((i = a.find(s_T)) >= 0) while ((i = a.find(s_T)) >= 0)
a.replace(i, 5, tmp_temp[a.mid(i, 5)]); a.replace(i, 5, tmp_temp[a.mid(i, 5)]);
} }
@@ -1072,7 +873,7 @@ void PICodeParser::restoreTmpTemp(Member * e) {
void PICodeParser::restoreTmpMeta(PICodeParser::Member * e) { void PICodeParser::restoreTmpMeta(PICodeParser::Member * e) {
static const PIString s_M = PIStringAscii("$M"); static const PIString s_M = PIStringAscii("$M");
int i = e->name.find(s_M); int i = e->name.find(s_M);
if (i < 0) return; if (i < 0) return;
e->meta = tmp_meta[e->name.takeMid(i, 5)]; e->meta = tmp_meta[e->name.takeMid(i, 5)];
} }
@@ -1094,7 +895,7 @@ bool PICodeParser::macroCondition(const PIString & mif, PIString mifcond) {
static const PIString s_ifndef = PIStringAscii("ifndef"); static const PIString s_ifndef = PIStringAscii("ifndef");
static const PIString s_if = PIStringAscii("if"); static const PIString s_if = PIStringAscii("if");
static const PIString s_elif = PIStringAscii("elif"); static const PIString s_elif = PIStringAscii("elif");
// piCout << "macroCondition" << mif << mifcond; //piCout << "macroCondition" << mif << mifcond;
if (mif == s_ifdef) return isDefineExists(mifcond); if (mif == s_ifdef) return isDefineExists(mifcond);
if (mif == s_ifndef) return !isDefineExists(mifcond); if (mif == s_ifndef) return !isDefineExists(mifcond);
if (mif == s_if || mif == s_elif) { if (mif == s_if || mif == s_elif) {
@@ -1113,65 +914,52 @@ double PICodeParser::procMacrosCond(PIString fc) {
char cc, nc; char cc, nc;
PIString ce; PIString ce;
fc.removeAll(s_defined); fc.removeAll(s_defined);
// piCout << "procMacrosCond" << fc; //piCout << "procMacrosCond" << fc;
while (!fc.isEmpty()) { while (!fc.isEmpty()) {
cc = fc[0].toAscii(); cc = fc[0].toAscii();
nc = (fc.size() > 1 ? fc[1].toAscii() : 0); nc = (fc.size() > 1 ? fc[1].toAscii() : 0);
if (cc == '!') { if (cc == '!') {neg = true; fc.pop_front(); continue;}
neg = true; if (cc == '(') {br = true; brv = procMacrosCond(fc.takeRange('(', ')'));}
fc.pop_front(); if (cc == '&' && nc == '&') {fc.remove(0, 2); oper = 1; continue;}
continue; if (cc == '|' && nc == '|') {fc.remove(0, 2); oper = 2; continue;}
}
if (cc == '(') {
br = true;
brv = procMacrosCond(fc.takeRange('(', ')'));
}
if (cc == '&' && nc == '&') {
fc.remove(0, 2);
oper = 1;
continue;
}
if (cc == '|' && nc == '|') {
fc.remove(0, 2);
oper = 2;
continue;
}
if (!br) { if (!br) {
ce = fc.takeCWord(); ce = fc.takeCWord();
if (ce.isEmpty()) ce = fc.takeNumber(); if (ce.isEmpty()) ce = fc.takeNumber();
} }
if (first) { if (first) {
first = false; first = false;
ret = br ? brv : defineValue(ce); ret = br ? brv : defineValue(ce);
if (neg) ret = -ret; if (neg) ret = -ret;
} else { } else {
// piCout << "oper" << oper << "with" << ce; //piCout << "oper" << oper << "with" << ce;
if (!br) brv = defineValue(ce); if (!br) brv = defineValue(ce);
switch (oper) { switch (oper) {
case 1: ret = ret && (neg ? -brv : brv); break; case 1: ret = ret && (neg ? -brv : brv); break;
case 2: ret = ret || (neg ? -brv : brv); break; case 2: ret = ret || (neg ? -brv : brv); break;
} }
} }
if (ps == fc.size_s()) fc.cutLeft(1); if (ps == fc.size_s()) fc.cutLeft(1);
ps = fc.size_s(); ps = fc.size_s();
br = neg = false; br = neg = false;
} }
// piCout << "return" << ret; //piCout << "return" << ret;
return ret; return ret;
} }
bool PICodeParser::isDefineExists(const PIString & dn) { bool PICodeParser::isDefineExists(const PIString & dn) {
piForeachC(Define & d, defines) { piForeachC (Define & d, defines) {
if (d.first == dn) return true; if (d.first == dn)
return true;
} }
return false; return false;
} }
double PICodeParser::defineValue(const PIString & dn) { double PICodeParser::defineValue(const PIString & dn) {
piForeachC(Define & d, defines) { piForeachC (Define & d, defines) {
if (d.first == dn) return d.second.isEmpty() ? 1. : d.second.toDouble(); if (d.first == dn)
return d.second.isEmpty() ? 1. : d.second.toDouble();
} }
return dn.toDouble(); return dn.toDouble();
} }
@@ -1188,33 +976,28 @@ void PICodeParser::replaceMeta(PIString & dn) {
ms = dn.findRange('(', ')', '\\', s + 6, &ml); ms = dn.findRange('(', ')', '\\', s + 6, &ml);
if (ms < 0) return; if (ms < 0) return;
PIString meta = dn.mid(ms, ml).trim(); PIString meta = dn.mid(ms, ml).trim();
PIString rm = s_M + PIString::fromNumber(tmp_meta.size_s()).expandLeftTo(3, '0'); PIString rm = s_M + PIString::fromNumber(tmp_meta.size_s()).expandLeftTo(3, '0');
dn.replace(s, ms + ml + 1 - s, rm); dn.replace(s, ms + ml + 1 - s, rm);
// piCout << "FOUND META \"" << meta << '\"'; //piCout << "FOUND META \"" << meta << '\"';
tmp_meta[rm] = parseMeta(meta); tmp_meta[rm] = parseMeta(meta);
s = dn.find(s_PIMETA); s = dn.find(s_PIMETA);
} }
} }
PICodeParser::Entity * PICodeParser::findEntityByName(const PIString & en) { PICodeParser::Entity * PICodeParser::findEntityByName(const PIString & en) {
piForeach(Entity * e, entities) piForeach (Entity * e, entities)
if (e->name == en) return e; if (e->name == en)
return e;
return 0; return 0;
} }
bool PICodeParser::isDeclaration(const PIString & fc, int start, int * end) { bool PICodeParser::isDeclaration(const PIString & fc, int start, int * end) {
int dind = fc.find('{', start), find = fc.find(';', start); int dind = fc.find('{', start), find = fc.find(';', start);
// piCout << "isDeclaration" << dind << find << fc.left(10); //piCout << "isDeclaration" << dind << find << fc.left(10);
if (dind < 0 && find < 0) { if (dind < 0 && find < 0) {if (end) *end = -1; return true;}
if (end) *end = -1; if (dind < 0 || find < dind) {if (end) *end = find; return true;}
return true;
}
if (dind < 0 || find < dind) {
if (end) *end = find;
return true;
}
if (end) *end = dind; if (end) *end = dind;
return false; return false;
} }
@@ -1227,7 +1010,7 @@ bool PICodeParser::isMainFile(const PIString & fc) {
if (csi < 0) csi = fc.find(PIStringAscii("\tmain"), si); if (csi < 0) csi = fc.find(PIStringAscii("\tmain"), si);
if (csi < 0) csi = fc.find(PIStringAscii("\nmain"), si); if (csi < 0) csi = fc.find(PIStringAscii("\nmain"), si);
if (csi < 0) return false; if (csi < 0) return false;
si = csi; si = csi;
int fi = fc.find('(', si + 5); int fi = fc.find('(', si + 5);
if (fi < 0) return false; if (fi < 0) return false;
if (fi - si < 10) { if (fi - si < 10) {
@@ -1252,80 +1035,68 @@ PIString PICodeParser::procMacros(PIString fc) {
int ifcnt = 0; int ifcnt = 0;
bool grab = false, skip = false, cond_ok = false; bool grab = false, skip = false, cond_ok = false;
PIString pfc, nfc, line, mif, mifcond; PIString pfc, nfc, line, mif, mifcond;
// piCout << "procMacros\n<******" << fc << "\n******>"; //piCout << "procMacros\n<******" << fc << "\n******>";
fc += '\n'; fc += '\n';
while (!fc.isEmpty()) { while (!fc.isEmpty()) {
line = fc.takeLine().trimmed(); line = fc.takeLine().trimmed();
if (line.left(1) == PIChar('#')) { if (line.left(1) == PIChar('#')) {
mifcond = line.mid(1); mifcond = line.mid(1);
mif = mifcond.takeCWord(); mif = mifcond.takeCWord();
// piCout << mif; //piCout << mif;
// piCout << "mif mifcond" << mif << mifcond << ifcnt; //piCout << "mif mifcond" << mif << mifcond << ifcnt;
if (skip || grab) { if (skip || grab) {
if (mif.left(2) == s_if) ifcnt++; if (mif.left(2) == s_if) ifcnt++;
if (mif.left(5) == s_endif) { if (mif.left(5) == s_endif) {
if (ifcnt > 0) if (ifcnt > 0) ifcnt--;
ifcnt--;
else { else {
// piCout << "main endif" << skip << grab; //piCout << "main endif" << skip << grab;
if (grab) pfc += procMacros(nfc); if (grab) pfc << procMacros(nfc);
skip = grab = false; skip = grab = false;
continue; continue;
} }
} }
if (mif.left(4) == s_elif && ifcnt == 0) { if (mif.left(4) == s_elif && ifcnt == 0) {
// piCout << "main elif" << skip << grab << cond_ok; //piCout << "main elif" << skip << grab << cond_ok;
if (cond_ok) { if (cond_ok) {
if (grab) { if (grab) {
pfc += procMacros(nfc); pfc << procMacros(nfc);
skip = true; skip = true; grab = false;
grab = false;
} }
continue; continue;
} }
if (skip) { if (skip) {
// piCout << "check elif" << skip << grab << cond_ok; //piCout << "check elif" << skip << grab << cond_ok;
if (!macroCondition(mif, mifcond.trimmed())) continue; if (!macroCondition(mif, mifcond.trimmed())) continue;
// piCout << "check elif ok"; //piCout << "check elif ok";
skip = false; skip = false; grab = cond_ok = true;
grab = cond_ok = true;
continue; continue;
} }
continue; continue;
} }
if (mif.left(4) == s_else && ifcnt == 0) { if (mif.left(4) == s_else && ifcnt == 0) {
// piCout << "main else" << skip << grab; //piCout << "main else" << skip << grab;
if (grab) pfc += procMacros(nfc); if (grab) pfc << procMacros(nfc);
if (skip && !cond_ok) { if (skip && !cond_ok) {skip = false; grab = true;}
skip = false; else {skip = true; grab = false;}
grab = true;
} else {
skip = true;
grab = false;
}
continue; continue;
} }
if (grab) nfc += line + '\n'; if (grab) nfc << line << '\n';
continue; continue;
} }
if (mif.left(2) == s_if) { if (mif.left(2) == s_if) {
// piCout << "main if"; //piCout << "main if";
skip = grab = cond_ok = false; skip = grab = cond_ok = false;
if (macroCondition(mif, mifcond.trimmed())) if (macroCondition(mif, mifcond.trimmed())) grab = cond_ok = true;
grab = cond_ok = true; else skip = true;
else
skip = true;
ifcnt = 0; ifcnt = 0;
nfc.clear(); nfc.clear();
} else { } else {
parseDirective(line.cutLeft(1).trim()); parseDirective(line.cutLeft(1).trim());
// return false; /// WARNING: now skip errors //return false; /// WARNING: now skip errors
} }
} else { } else {
if (grab) if (grab) nfc << line << '\n';
nfc += line + '\n'; else if (!skip) pfc << line << '\n';
else if (!skip)
pfc += line + '\n';
} }
} }
return pfc; return pfc;
@@ -1333,13 +1104,13 @@ PIString PICodeParser::procMacros(PIString fc) {
bool PICodeParser::parseDirective(PIString d) { bool PICodeParser::parseDirective(PIString d) {
static const PIString s_include = PIStringAscii("include"); static const PIString s_include = PIStringAscii("include");
static const PIString s_define = PIStringAscii("define"); static const PIString s_define = PIStringAscii("define");
static const PIString s_undef = PIStringAscii("undef"); static const PIString s_undef = PIStringAscii("undef");
static const PIString s_PIMETA = PIStringAscii("PIMETA"); static const PIString s_PIMETA = PIStringAscii("PIMETA");
if (d.isEmpty()) return true; if (d.isEmpty()) return true;
PIString dname = d.takeCWord(); PIString dname = d.takeCWord();
// piCout << "parseDirective" << d; //piCout << "parseDirective" << d;
if (dname == s_include) { if (dname == s_include) {
d.replaceAll('<', '\"').replaceAll('>', '\"'); d.replaceAll('<', '\"').replaceAll('>', '\"');
PIString cf = cur_file, ifc = d.takeRange('\"', '\"'); PIString cf = cur_file, ifc = d.takeRange('\"', '\"');
@@ -1351,7 +1122,7 @@ bool PICodeParser::parseDirective(PIString d) {
} }
if (dname == s_define) { if (dname == s_define) {
PIString mname = d.takeCWord(); PIString mname = d.takeCWord();
// piCout << mname; //piCout << mname;
if (mname == s_PIMETA) return true; if (mname == s_PIMETA) return true;
if (d.left(1) == PIChar('(')) { // macro if (d.left(1) == PIChar('(')) { // macro
PIStringList args = d.takeRange('(', ')').split(',').trim(); PIStringList args = d.takeRange('(', ')').split(',').trim();
@@ -1376,10 +1147,7 @@ bool PICodeParser::parseDirective(PIString d) {
if (dname == s_undef) { if (dname == s_undef) {
PIString mname = d.takeCWord(); PIString mname = d.takeCWord();
for (int i = 0; i < defines.size_s(); ++i) for (int i = 0; i < defines.size_s(); ++i)
if (defines[i].first == mname) { if (defines[i].first == mname) {defines.remove(i); --i;}
defines.remove(i);
--i;
}
return true; return true;
} }
return true; return true;

View File

@@ -5,59 +5,49 @@
* \~russian Разбор C++ кода * \~russian Разбор C++ кода
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
C++ code parser C++ code parser
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PICODEPARSER_H #ifndef PICODEPARSER_H
#define PICODEPARSER_H #define PICODEPARSER_H
#include "pievaluator.h"
#include "pifile.h" #include "pifile.h"
#include "pievaluator.h"
inline bool _isCChar(const PIChar & c) { inline bool _isCChar(const PIChar & c) {return (c.isAlpha() || (c.toAscii() == '_'));}
return (c.isAlpha() || (c.toAscii() == '_')); inline bool _isCChar(const PIString & c) {if (c.isEmpty()) return false; return _isCChar(c[0]);}
}
inline bool _isCChar(const PIString & c) {
if (c.isEmpty()) return false;
return _isCChar(c[0]);
}
class PIP_EXPORT PICodeParser { class PIP_EXPORT PICodeParser {
public: public:
PICodeParser(); PICodeParser();
enum Visibility { enum Visibility {Global, Public, Protected, Private};
Global,
Public,
Protected,
Private
};
enum Attribute { enum Attribute {
NoAttributes = 0x0, NoAttributes = 0x0,
Const = 0x01, Const = 0x01,
Static = 0x02, Static = 0x02,
Mutable = 0x04, Mutable = 0x04,
Volatile = 0x08, Volatile = 0x08,
Inline = 0x10, Inline = 0x10,
Virtual = 0x20, Virtual = 0x20,
Extern = 0x40 Extern = 0x40
}; };
typedef PIFlags<Attribute> Attributes; typedef PIFlags<Attribute> Attributes;
typedef PIPair<PIString, PIString> Define; typedef PIPair<PIString, PIString> Define;
typedef PIPair<PIString, PIString> Typedef; typedef PIPair<PIString, PIString> Typedef;
@@ -65,25 +55,25 @@ public:
struct PIP_EXPORT Macro { struct PIP_EXPORT Macro {
Macro(const PIString & n = PIString(), const PIString & v = PIString(), const PIStringList & a = PIStringList()) { Macro(const PIString & n = PIString(), const PIString & v = PIString(), const PIStringList & a = PIStringList()) {
name = n; name = n;
value = v; value = v;
args = a; args = a;
} }
PIString expand(PIString args_, bool * ok = 0) const; PIString expand(PIString args_, bool * ok = 0) const;
PIString name; PIString name;
PIString value; PIString value;
PIStringList args; PIStringList args;
}; };
struct PIP_EXPORT Member { struct PIP_EXPORT Member {
Member() { Member() {
visibility = Global; visibility = Global;
size = 0; size = 0;
bits = -1; bits = -1;
is_type_ptr = false; is_type_ptr = false;
attributes = NoAttributes; attributes = NoAttributes;
} }
bool isBitfield() const { return bits > 0; } bool isBitfield() const {return bits > 0;}
MetaMap meta; MetaMap meta;
PIString type; PIString type;
PIString name; PIString name;
@@ -96,12 +86,12 @@ public:
int size; int size;
int bits; int bits;
}; };
struct PIP_EXPORT Entity { struct PIP_EXPORT Entity {
Entity() { Entity() {
visibility = Global; visibility = Global;
has_name = true; has_name = true;
size = 0; size = 0;
parent_scope = 0; parent_scope = 0;
} }
MetaMap meta; MetaMap meta;
@@ -112,52 +102,49 @@ public:
int size; int size;
bool has_name; bool has_name;
Entity * parent_scope; Entity * parent_scope;
PIVector<Entity *> parents; PIVector<Entity * > parents;
PIVector<Entity *> children; PIVector<Entity * > children;
PIVector<Member> functions; PIVector<Member> functions;
PIVector<Member> members; PIVector<Member> members;
PIVector<Typedef> typedefs; PIVector<Typedef> typedefs;
}; };
struct PIP_EXPORT EnumeratorInfo { struct PIP_EXPORT EnumeratorInfo {
EnumeratorInfo(const PIString & n = PIString(), int v = 0, const MetaMap & m = MetaMap()) { EnumeratorInfo(const PIString & n = PIString(), int v = 0, const MetaMap & m = MetaMap()) {name = n; value = v; meta = m;}
name = n;
value = v;
meta = m;
}
MetaMap meta; MetaMap meta;
PIString name; PIString name;
int value; int value;
}; };
struct PIP_EXPORT Enum { struct PIP_EXPORT Enum {
Enum(const PIString & n = PIString()) { name = n; } Enum(const PIString & n = PIString()) {
name = n;
}
MetaMap meta; MetaMap meta;
PIString name; PIString name;
PIVector<EnumeratorInfo> members; PIVector<EnumeratorInfo> members;
}; };
void parseFile(const PIString & file, bool follow_includes = true); void parseFile(const PIString & file, bool follow_includes = true);
void parseFiles(const PIStringList & files, bool follow_includes = true); void parseFiles(const PIStringList & files, bool follow_includes = true);
void parseFileContent(PIString fc);
void includeDirectory(const PIString & dir) {includes << dir;}
void includeDirectory(const PIString & dir) { includes << dir; } void addDefine(const PIString & def_name, const PIString & def_value) {custom_defines << Define(def_name, def_value);}
void addDefine(const PIString & def_name, const PIString & def_value) { custom_defines << Define(def_name, def_value); }
bool isEnum(const PIString & name); bool isEnum(const PIString & name);
Entity * findEntityByName(const PIString & en); Entity * findEntityByName(const PIString & en);
PIStringList parsedFiles() const { return PIStringList(proc_files.toVector()); } PIStringList parsedFiles() const {return PIStringList(proc_files.toVector());}
PIString mainFile() const { return main_file; } PIString mainFile() const {return main_file;}
const PICodeParser::Entity * global() const { return &root_; } const PICodeParser::Entity * global() const {return &root_;}
int macrosSubstitutionMaxIterations() const { return macros_iter; } int macrosSubstitutionMaxIterations() const {return macros_iter;}
void setMacrosSubstitutionMaxIterations(int value) { macros_iter = value; } void setMacrosSubstitutionMaxIterations(int value) {macros_iter = value;}
PIVector<Define> defines, custom_defines; PIVector<Define> defines, custom_defines;
PIVector<Macro> macros; PIVector<Macro> macros;
PIVector<Enum> enums; PIVector<Enum> enums;
PIVector<Typedef> typedefs; PIVector<Typedef> typedefs;
PIVector<Entity *> entities; PIVector<Entity * > entities;
private: private:
void clear(); void clear();
bool parseFileInternal(const PIString & file, bool follow_includes); bool parseFileInternal(const PIString & file, bool follow_includes);
@@ -182,7 +169,7 @@ private:
bool isDeclaration(const PIString & fc, int start, int * end); bool isDeclaration(const PIString & fc, int start, int * end);
bool isMainFile(const PIString & fc); bool isMainFile(const PIString & fc);
void normalizeEntityNamespace(PIString & n); void normalizeEntityNamespace(PIString & n);
int macros_iter, anon_num; int macros_iter, anon_num;
bool with_includes; bool with_includes;
PIEvaluator evaluator; PIEvaluator evaluator;
@@ -194,6 +181,7 @@ private:
PIString cur_namespace; PIString cur_namespace;
PIMap<PIString, PIString> tmp_temp; PIMap<PIString, PIString> tmp_temp;
PIMap<PIString, MetaMap> tmp_meta; PIMap<PIString, MetaMap> tmp_meta;
}; };
#endif // PICODEPARSER_H #endif // PICODEPARSER_H

View File

@@ -6,22 +6,22 @@
* \~russian Сжатие с помощью zlib * \~russian Сжатие с помощью zlib
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Compress class using zlib Compress class using zlib
Andrey Bychkov work.a.b@yandex.ru Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
//! \defgroup Compress Compress //! \defgroup Compress Compress
//! \~\brief //! \~\brief
@@ -58,8 +58,8 @@
#ifndef PICOMPRESS_H #ifndef PICOMPRESS_H
#define PICOMPRESS_H #define PICOMPRESS_H
#include "pibytearray.h"
#include "pip_compress_export.h" #include "pip_compress_export.h"
#include "pibytearray.h"
//! \~english Compress "ba" with compression level "level", return empty %PIByteArray if no compression supports //! \~english Compress "ba" with compression level "level", return empty %PIByteArray if no compression supports
//! \~russian Сжимает "ba" с уровнем сжатия "level", возвращает пустой %PIByteArray если нет поддержки //! \~russian Сжимает "ba" с уровнем сжатия "level", возвращает пустой %PIByteArray если нет поддержки

View File

@@ -1,20 +1,20 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Module includes Module includes
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
//! \defgroup Console Console //! \defgroup Console Console
//! \~\brief //! \~\brief

View File

@@ -1,30 +1,28 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Keyboard grabber for console Keyboard grabber for console
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "pikbdlistener.h"
#include "piincludes_p.h" #include "piincludes_p.h"
#include "piliterals.h" #include "pikbdlistener.h"
#ifndef WINDOWS #ifndef WINDOWS
# include <termios.h> # include <termios.h>
#else #else
# include <wincon.h>
# include <wingdi.h> # include <wingdi.h>
# include <wincon.h>
#endif #endif
/** \class PIKbdListener /** \class PIKbdListener
@@ -49,82 +47,80 @@ PIKbdListener * PIKbdListener::_object = 0;
#ifndef WINDOWS #ifndef WINDOWS
// unix // unix
const PIKbdListener::EscSeq PIKbdListener::esc_seq[] = { const PIKbdListener::EscSeq PIKbdListener::esc_seq[] = {
{"OA", PIKbdListener::UpArrow, 0, 0, 1}, {"OA", PIKbdListener::UpArrow, 0, 0 , 1 },
{"OA", PIKbdListener::UpArrow, 4, 0, 0}, {"OA", PIKbdListener::UpArrow, 4, 0 , 0 },
{"[1A", PIKbdListener::UpArrow, 0, 0, 0}, {"[1A", PIKbdListener::UpArrow, 0, 0 , 0 },
{"[A", PIKbdListener::UpArrow, 0, vt_all, 0}, {"[A", PIKbdListener::UpArrow, 0, vt_all , 0 },
{"OB", PIKbdListener::DownArrow, 0, 0, 1}, {"OB", PIKbdListener::DownArrow, 0, 0 , 1 },
{"OB", PIKbdListener::DownArrow, 4, 0, 0}, {"OB", PIKbdListener::DownArrow, 4, 0 , 0 },
{"[1B", PIKbdListener::DownArrow, 0, 0, 0}, {"[1B", PIKbdListener::DownArrow, 0, 0 , 0 },
{"[B", PIKbdListener::DownArrow, 0, vt_all, 0}, {"[B", PIKbdListener::DownArrow, 0, vt_all , 0 },
{"OC", PIKbdListener::RightArrow, 0, 0, 1}, {"OC", PIKbdListener::RightArrow, 0, 0 , 1 },
{"OC", PIKbdListener::RightArrow, 4, 0, 0}, {"OC", PIKbdListener::RightArrow, 4, 0 , 0 },
{"[1C", PIKbdListener::RightArrow, 0, 0, 0}, {"[1C", PIKbdListener::RightArrow, 0, 0 , 0 },
{"[C", PIKbdListener::RightArrow, 0, vt_all, 0}, {"[C", PIKbdListener::RightArrow, 0, vt_all , 0 },
{"OD", PIKbdListener::LeftArrow, 0, 0, 1}, {"OD", PIKbdListener::LeftArrow, 0, 0 , 1 },
{"OD", PIKbdListener::LeftArrow, 4, 0, 0}, {"OD", PIKbdListener::LeftArrow, 4, 0 , 0 },
{"[1D", PIKbdListener::LeftArrow, 0, 0, 0}, {"[1D", PIKbdListener::LeftArrow, 0, 0 , 0 },
{"[D", PIKbdListener::LeftArrow, 0, vt_all, 0}, {"[D", PIKbdListener::LeftArrow, 0, vt_all , 0 },
{"[H", PIKbdListener::Home, 0, vt_xterm, 0}, {"[H", PIKbdListener::Home, 0, vt_xterm , 0 },
{"[1H", PIKbdListener::Home, 0, 0, 0}, {"[1H", PIKbdListener::Home, 0, 0 , 0 },
{"OH", PIKbdListener::Home, 0, 0, 1}, {"OH", PIKbdListener::Home, 0, 0 , 1 },
{"[1~", PIKbdListener::Home, 0, vt_linux, 0}, {"[1~", PIKbdListener::Home, 0, vt_linux , 0 },
{"[F", PIKbdListener::End, 0, vt_xterm, 0}, {"[F", PIKbdListener::End, 0, vt_xterm , 0 },
{"[1F", PIKbdListener::End, 0, 0, 0}, {"[1F", PIKbdListener::End, 0, 0 , 0 },
{"OF", PIKbdListener::End, 0, 0, 1}, {"OF", PIKbdListener::End, 0, 0 , 1 },
{"[4~", PIKbdListener::End, 0, vt_linux, 0}, {"[4~", PIKbdListener::End, 0, vt_linux , 0 },
{"[2~", PIKbdListener::Insert, 0, vt_all, 0}, {"[2~", PIKbdListener::Insert, 0, vt_all , 0 },
{"[3~", PIKbdListener::Delete, 0, vt_all, 0}, {"[3~", PIKbdListener::Delete, 0, vt_all , 0 },
{"[5~", PIKbdListener::PageUp, 0, vt_all, 0}, {"[5~", PIKbdListener::PageUp, 0, vt_all , 0 },
{"[6~", PIKbdListener::PageDown, 0, vt_all, 0}, {"[6~", PIKbdListener::PageDown, 0, vt_all , 0 },
{"[Z", PIKbdListener::Tab, 1, vt_xterm, 0}, {"[Z", PIKbdListener::Tab, 1, vt_xterm , 0 },
{"OP", PIKbdListener::F1, 0, vt_xterm, 0}, {"OP", PIKbdListener::F1, 0, vt_xterm , 0 },
{"[[A", PIKbdListener::F1, 0, vt_linux, 0}, {"[[A", PIKbdListener::F1, 0, vt_linux , 0 },
{"[11~", PIKbdListener::F1, 0, 0, 0}, {"[11~", PIKbdListener::F1, 0, 0 , 0 },
{"[25~", PIKbdListener::F1, 1, vt_linux, 0}, {"[25~", PIKbdListener::F1, 1, vt_linux , 0 },
{"OQ", PIKbdListener::F2, 0, vt_xterm, 0}, {"OQ", PIKbdListener::F2, 0, vt_xterm , 0 },
{"[[B", PIKbdListener::F2, 0, vt_linux, 0}, {"[[B", PIKbdListener::F2, 0, vt_linux , 0 },
{"[12~", PIKbdListener::F2, 0, 0, 0}, {"[12~", PIKbdListener::F2, 0, 0 , 0 },
{"[26~", PIKbdListener::F2, 1, vt_linux, 0}, {"[26~", PIKbdListener::F2, 1, vt_linux , 0 },
{"OR", PIKbdListener::F3, 0, vt_xterm, 0}, {"OR", PIKbdListener::F3, 0, vt_xterm , 0 },
{"[[C", PIKbdListener::F3, 0, vt_linux, 0}, {"[[C", PIKbdListener::F3, 0, vt_linux , 0 },
{"[13~", PIKbdListener::F3, 0, 0, 0}, {"[13~", PIKbdListener::F3, 0, 0 , 0 },
{"[28~", PIKbdListener::F3, 1, vt_linux, 0}, {"[28~", PIKbdListener::F3, 1, vt_linux , 0 },
{"OS", PIKbdListener::F4, 0, vt_xterm, 0}, {"OS", PIKbdListener::F4, 0, vt_xterm , 0 },
{"[[D", PIKbdListener::F4, 0, vt_linux, 0}, {"[[D", PIKbdListener::F4, 0, vt_linux , 0 },
{"[14~", PIKbdListener::F4, 0, 0, 0}, {"[14~", PIKbdListener::F4, 0, 0 , 0 },
{"[29~", PIKbdListener::F4, 1, vt_linux, 0}, {"[29~", PIKbdListener::F4, 1, vt_linux , 0 },
{"[[E", PIKbdListener::F5, 0, vt_linux, 0}, {"[[E", PIKbdListener::F5, 0, vt_linux , 0 },
{"OT", PIKbdListener::F5, 0, 0, 0}, {"OT", PIKbdListener::F5, 0, 0 , 0 },
{"[15~", PIKbdListener::F5, 0, vt_xterm, 0}, {"[15~", PIKbdListener::F5, 0, vt_xterm , 0 },
{"[31~", PIKbdListener::F5, 1, vt_linux, 0}, {"[31~", PIKbdListener::F5, 1, vt_linux , 0 },
{"OU", PIKbdListener::F6, 0, 0, 0}, {"OU", PIKbdListener::F6, 0, 0 , 0 },
{"[17~", PIKbdListener::F6, 0, vt_all, 0}, {"[17~", PIKbdListener::F6, 0, vt_all , 0 },
{"[32~", PIKbdListener::F6, 1, vt_linux, 0}, {"[32~", PIKbdListener::F6, 1, vt_linux , 0 },
{"OV", PIKbdListener::F7, 0, 0, 0}, {"OV", PIKbdListener::F7, 0, 0 , 0 },
{"[18~", PIKbdListener::F7, 0, vt_all, 0}, {"[18~", PIKbdListener::F7, 0, vt_all , 0 },
{"[33~", PIKbdListener::F7, 1, vt_linux, 0}, {"[33~", PIKbdListener::F7, 1, vt_linux , 0 },
{"OW", PIKbdListener::F8, 0, 0, 0}, {"OW", PIKbdListener::F8, 0, 0 , 0 },
{"[19~", PIKbdListener::F8, 0, vt_all, 0}, {"[19~", PIKbdListener::F8, 0, vt_all , 0 },
{"[34~", PIKbdListener::F8, 1, vt_linux, 0}, {"[34~", PIKbdListener::F8, 1, vt_linux , 0 },
{"OX", PIKbdListener::F9, 0, 0, 0}, {"OX", PIKbdListener::F9, 0, 0 , 0 },
{"[20~", PIKbdListener::F9, 0, vt_all, 0}, {"[20~", PIKbdListener::F9, 0, vt_all , 0 },
{"[35~", PIKbdListener::F9, 1, vt_linux, 0}, {"[35~", PIKbdListener::F9, 1, vt_linux , 0 },
{"OY", PIKbdListener::F10, 0, 0, 0}, {"OY", PIKbdListener::F10, 0, 0 , 0 },
{"[21~", PIKbdListener::F10, 0, vt_all, 0}, {"[21~", PIKbdListener::F10, 0, vt_all , 0 },
{"[36~", PIKbdListener::F10, 1, vt_linux, 0}, {"[36~", PIKbdListener::F10, 1, vt_linux , 0 },
{"OZ", PIKbdListener::F11, 0, 0, 0}, {"OZ", PIKbdListener::F11, 0, 0 , 0 },
{"[23~", PIKbdListener::F11, 0, vt_all, 0}, {"[23~", PIKbdListener::F11, 0, vt_all , 0 },
{"O[", PIKbdListener::F12, 0, 0, 0}, {"O[", PIKbdListener::F12, 0, 0 , 0 },
{"[24~", PIKbdListener::F12, 0, vt_all, 0}, {"[24~", PIKbdListener::F12, 0, vt_all , 0 },
// End // End
{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0},
}; };
void setupTerminal(bool on) { void setupTerminal(bool on) {
printf("\e[?1000"); printf("\e[?1000"); printf(on ? "h" : "l");
printf(on ? "h" : "l"); printf("\e[?1002"); printf(on ? "h" : "l");
printf("\e[?1002");
printf(on ? "h" : "l");
fflush(0); fflush(0);
} }
#endif #endif
@@ -132,7 +128,7 @@ void setupTerminal(bool on) {
PRIVATE_DEFINITION_START(PIKbdListener) PRIVATE_DEFINITION_START(PIKbdListener)
#ifdef WINDOWS #ifdef WINDOWS
void *hIn, *hOut; void * hIn, * hOut;
DWORD smode, tmode; DWORD smode, tmode;
CONSOLE_SCREEN_BUFFER_INFO sbi; CONSOLE_SCREEN_BUFFER_INFO sbi;
#else #else
@@ -143,32 +139,31 @@ PRIVATE_DEFINITION_START(PIKbdListener)
#else #else
int int
#endif #endif
ret; ret;
PRIVATE_DEFINITION_END(PIKbdListener) PRIVATE_DEFINITION_END(PIKbdListener)
PIKbdListener::PIKbdListener(KBFunc slot, void * _d, bool startNow): PIThread() { PIKbdListener::PIKbdListener(KBFunc slot, void * _d, bool startNow): PIThread() {
setName("keyboard_listener"_a); setName("keyboard_listener");
_object = this; _object = this;
#ifdef WINDOWS #ifdef WINDOWS
PRIVATE->hIn = GetStdHandle(STD_INPUT_HANDLE); PRIVATE->hIn = GetStdHandle(STD_INPUT_HANDLE);
PRIVATE->hOut = GetStdHandle(STD_OUTPUT_HANDLE); PRIVATE->hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(PRIVATE->hIn, &PRIVATE->smode); GetConsoleMode(PRIVATE->hIn, &PRIVATE->smode);
#else #else
tcgetattr(0, &PRIVATE->sterm); tcgetattr(0, &PRIVATE->sterm);
#endif #endif
is_active = true; is_active = true;
ret_func = slot; ret_func = slot;
kbddata_ = _d; kbddata_ = _d;
dbl_interval = 400; dbl_interval = 400;
PIKbdListener::exiting = exit_enabled = false; PIKbdListener::exiting = exit_enabled = false;
if (startNow) start(); if (startNow) start();
} }
PIKbdListener::~PIKbdListener() { PIKbdListener::~PIKbdListener() {
stop(); terminate();
if (!waitForFinish(100_ms)) terminate();
end(); end();
} }
@@ -196,9 +191,9 @@ PIKbdListener::KeyModifiers getModifiers(DWORD v, bool * shift = 0) {
bool shi = v & SHIFT_PRESSED; bool shi = v & SHIFT_PRESSED;
bool alt = v & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED); bool alt = v & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED);
if (ctrl) ret |= PIKbdListener::Ctrl; if (ctrl) ret |= PIKbdListener::Ctrl;
if (shi) ret |= PIKbdListener::Shift; if (shi) ret |= PIKbdListener::Shift;
if (alt) ret |= PIKbdListener::Alt; if (alt) ret |= PIKbdListener::Alt;
// if (meta) ret |= PIKbdListener::Meta; //if (meta) ret |= PIKbdListener::Meta;
if (v & CAPSLOCK_ON) shi = !shi; if (v & CAPSLOCK_ON) shi = !shi;
if (shift) *shift = shi; if (shift) *shift = shi;
return ret; return ret;
@@ -206,7 +201,7 @@ PIKbdListener::KeyModifiers getModifiers(DWORD v, bool * shift = 0) {
PIKbdListener::MouseButtons getButtons(DWORD v) { PIKbdListener::MouseButtons getButtons(DWORD v) {
PIKbdListener::MouseButtons ret; PIKbdListener::MouseButtons ret;
if (v & FROM_LEFT_1ST_BUTTON_PRESSED) ret |= PIKbdListener::MouseLeft; if (v & FROM_LEFT_1ST_BUTTON_PRESSED) ret |= PIKbdListener::MouseLeft;
if (v & RIGHTMOST_BUTTON_PRESSED) ret |= PIKbdListener::MouseRight; if (v & RIGHTMOST_BUTTON_PRESSED) ret |= PIKbdListener::MouseRight;
if (v & FROM_LEFT_2ND_BUTTON_PRESSED) ret |= PIKbdListener::MouseMiddle; if (v & FROM_LEFT_2ND_BUTTON_PRESSED) ret |= PIKbdListener::MouseMiddle;
return ret; return ret;
} }
@@ -214,159 +209,77 @@ PIKbdListener::MouseButtons getButtons(DWORD v) {
void PIKbdListener::readKeyboard() { void PIKbdListener::readKeyboard() {
ke.key = 0; ke.key = 0;
ke.modifiers = 0; ke.modifiers = 0;
memset(rc, 0, 8); memset(rc, 0, 8);
#ifdef WINDOWS #ifdef WINDOWS
INPUT_RECORD ir; INPUT_RECORD ir;
ReadConsoleInput(PRIVATE->hIn, &ir, 1, &(PRIVATE->ret)); ReadConsoleInput(PRIVATE->hIn, &ir, 1, &(PRIVATE->ret));
//piCout << ir.EventType;
switch (ir.EventType) { switch (ir.EventType) {
case KEY_EVENT: { case KEY_EVENT: {
KEY_EVENT_RECORD ker = ir.Event.KeyEvent; KEY_EVENT_RECORD ker = ir.Event.KeyEvent;
if (ker.bKeyDown) { if (ker.bKeyDown) {
bool shift = false; bool shift = false;
ke.modifiers = getModifiers(ker.dwControlKeyState, &shift); ke.modifiers = getModifiers(ker.dwControlKeyState, &shift);
// piCout << "key" << int(ker.wVirtualKeyCode) << int(ker.uChar.AsciiChar); //piCout << "key" << int(ker.wVirtualKeyCode) << int(ker.uChar.AsciiChar);
switch (ker.wVirtualKeyCode) { switch (ker.wVirtualKeyCode) {
case 8: case 8: PRIVATE->ret = 1; ke.key = Backspace; break;
PRIVATE->ret = 1; case 33: PRIVATE->ret = 1; ke.key = PageUp; break;
ke.key = Backspace; case 34: PRIVATE->ret = 1; ke.key = PageDown; break;
break; case 35: PRIVATE->ret = 1; ke.key = End; break;
case 33: case 36: PRIVATE->ret = 1; ke.key = Home; break;
PRIVATE->ret = 1; case 37: PRIVATE->ret = 1; ke.key = LeftArrow; break;
ke.key = PageUp; case 38: PRIVATE->ret = 1; ke.key = UpArrow; break;
break; case 39: PRIVATE->ret = 1; ke.key = RightArrow; break;
case 34: case 40: PRIVATE->ret = 1; ke.key = DownArrow; break;
PRIVATE->ret = 1; case 45: PRIVATE->ret = 1; ke.key = Insert; break;
ke.key = PageDown; case 46: PRIVATE->ret = 1; ke.key = Delete; break;
break;
case 35:
PRIVATE->ret = 1;
ke.key = End;
break;
case 36:
PRIVATE->ret = 1;
ke.key = Home;
break;
case 37:
PRIVATE->ret = 1;
ke.key = LeftArrow;
break;
case 38:
PRIVATE->ret = 1;
ke.key = UpArrow;
break;
case 39:
PRIVATE->ret = 1;
ke.key = RightArrow;
break;
case 40:
PRIVATE->ret = 1;
ke.key = DownArrow;
break;
case 45:
PRIVATE->ret = 1;
ke.key = Insert;
break;
case 46:
PRIVATE->ret = 1;
ke.key = Delete;
break;
case '\r': case '\r':
case '\n': case '\n': PRIVATE->ret = 1; ke.key = Return; break;
PRIVATE->ret = 1; case ' ': PRIVATE->ret = 1; ke.key = Space; break;
ke.key = Return; case '\t': PRIVATE->ret = 1; ke.key = Tab; break;
break; case 112: PRIVATE->ret = 1; ke.key = F1; break;
case ' ': case 113: PRIVATE->ret = 1; ke.key = F2; break;
PRIVATE->ret = 1; case 114: PRIVATE->ret = 1; ke.key = F3; break;
ke.key = Space; case 115: PRIVATE->ret = 1; ke.key = F4; break;
break; case 116: PRIVATE->ret = 1; ke.key = F5; break;
case '\t': case 117: PRIVATE->ret = 1; ke.key = F6; break;
PRIVATE->ret = 1; case 118: PRIVATE->ret = 1; ke.key = F7; break;
ke.key = Tab; case 119: PRIVATE->ret = 1; ke.key = F8; break;
break; case 120: PRIVATE->ret = 1; ke.key = F9; break;
case 112: case 121: PRIVATE->ret = 1; ke.key = F10; break;
PRIVATE->ret = 1; case 122: PRIVATE->ret = 1; ke.key = F11; break;
ke.key = F1; case 123: PRIVATE->ret = 1; ke.key = F12; break;
break;
case 113:
PRIVATE->ret = 1;
ke.key = F2;
break;
case 114:
PRIVATE->ret = 1;
ke.key = F3;
break;
case 115:
PRIVATE->ret = 1;
ke.key = F4;
break;
case 116:
PRIVATE->ret = 1;
ke.key = F5;
break;
case 117:
PRIVATE->ret = 1;
ke.key = F6;
break;
case 118:
PRIVATE->ret = 1;
ke.key = F7;
break;
case 119:
PRIVATE->ret = 1;
ke.key = F8;
break;
case 120:
PRIVATE->ret = 1;
ke.key = F9;
break;
case 121:
PRIVATE->ret = 1;
ke.key = F10;
break;
case 122:
PRIVATE->ret = 1;
ke.key = F11;
break;
case 123:
PRIVATE->ret = 1;
ke.key = F12;
break;
default: default:
PRIVATE->ret = 1; PRIVATE->ret = 1;
rc[0] = 1; rc[0] = 1;
{ {
PIChar ch = PIChar::fromConsole(ker.uChar.AsciiChar); PIChar ch = PIChar::fromConsole(ker.uChar.AsciiChar);
if (shift) ch = ch.toUpper(); if (shift) ch = ch.toUpper();
ke.key = ch.unicode16Code(); ke.key = ch.unicode16Code();
} }
break; break;
} }
if (ke.key == 0) { if (ke.key == 0) {piMSleep(10); return;}
piMSleep(10); } else {piMSleep(10); return;}
return;
}
} else {
piMSleep(10);
return;
} }
} break; break;
case MOUSE_EVENT: { case MOUSE_EVENT: {
MOUSE_EVENT_RECORD mer = ir.Event.MouseEvent; MOUSE_EVENT_RECORD mer = ir.Event.MouseEvent;
GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->sbi); GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->sbi);
me.modifiers = getModifiers(mer.dwControlKeyState); me.modifiers = getModifiers(mer.dwControlKeyState);
MouseButtons mb = getButtons(mer.dwButtonState); MouseButtons mb = getButtons(mer.dwButtonState);
if (mer.dwEventFlags & MOUSE_WHEELED) { if (mer.dwEventFlags & MOUSE_WHEELED) {
memcpy((void *)(&we), (const void *)(&me), sizeof(me)); memcpy((void*)(&we), (const void*)(&me), sizeof(me));
we.action = MouseWheel; we.action = MouseWheel;
we.direction = short((mer.dwButtonState >> 8) & 0xFFFF) > 0; we.direction = short((mer.dwButtonState >> 8) & 0xFFFF) > 0;
// piCout << "wheel" << we.direction; //piCout << "wheel" << we.direction;
wheelEvent(we, kbddata_); wheelEvent(we, kbddata_);
break; break;
} else { } else {
me.x = mer.dwMousePosition.X; me.x = mer.dwMousePosition.X;
me.y = mer.dwMousePosition.Y - PRIVATE->sbi.srWindow.Top; me.y = mer.dwMousePosition.Y - PRIVATE->sbi.srWindow.Top;
bool move = mer.dwEventFlags & MOUSE_MOVED; bool move = mer.dwEventFlags & MOUSE_MOVED;
if (move) { if (move) {
if (me.action == MouseButtonRelease) { if (me.action == MouseButtonRelease) {
@@ -384,43 +297,38 @@ void PIKbdListener::readKeyboard() {
prev_p_me = me; prev_p_me = me;
} }
tm_dbl.reset(); tm_dbl.reset();
} else if (mb < me.buttons)
me.action = MouseButtonRelease;
else {
if (mb != 0) piCoutObj << "WTF";
break;
} }
else if (mb < me.buttons) me.action = MouseButtonRelease;
else {if (mb != 0) piCoutObj << "WTF"; break;}
} }
me.buttons = mb; me.buttons = mb;
if (piCompareBinary(&prev_me, &me, sizeof(me))) break; if (piCompareBinary(&prev_me, &me, sizeof(me)))
memcpy((void *)(&prev_me), (const void *)(&me), sizeof(me)); break;
// PIString _s[] = {"press", "release", "dbl click", "move"}; memcpy((void*)(&prev_me), (const void*)(&me), sizeof(me));
// piCoutObj << _s[me.action] << me.buttons << ":" << me.x << me.y; //PIString _s[] = {"press", "release", "dbl click", "move"};
//piCoutObj << _s[me.action] << me.buttons << ":" << me.x << me.y;
mouseEvent(me, kbddata_); mouseEvent(me, kbddata_);
break; break;
} }
} break; }
default: piMSleep(10); return; break;
default:
piMSleep(10);
return;
} }
#else #else
tcsetattr(0, TCSANOW, &PRIVATE->tterm); tcsetattr(0, TCSANOW, &PRIVATE->tterm);
PRIVATE->ret = read(0, rc, 8); PRIVATE->ret = read(0, rc, 8);
/*piCout << "key" << PIString(rc).replaceAll("\e", "\\e"); /*piCout << "key" << PIString(rc).replaceAll("\e", "\\e");
for (int i = 0; i < PRIVATE->ret; ++i) for (int i = 0; i < PRIVATE->ret; ++i)
PICout(0) << PICoutManipulators::Hex << int(((uchar * )&rc)[i]) << ' '; PICout(0) << PICoutManipulators::Hex << int(((uchar * )&rc)[i]) << ' ';
PICout(0) << "\n"; PICout(0) << "\n";
std::cout << PRIVATE->ret << " chars "; std::cout << PRIVATE->ret << " chars ";
for (int i = 0; i < PRIVATE->ret; ++i) for (int i = 0; i < PRIVATE->ret; ++i)
std::cout << "'" << (char)(rc[i]) << "' " << (int)(uchar)(rc[i]); std::cout << "'" << (char)(rc[i]) << "' " << (int)(uchar)(rc[i]);
std::cout << std::endl;*/ std::cout << std::endl;*/
if (rc[0] == 0) { if (rc[0] == 0) {piMSleep(10); return;}
piMSleep(10); if (PRIVATE->ret < 0 || PRIVATE->ret > 7) {piMSleep(10); return;}
return;
}
if (PRIVATE->ret < 0 || PRIVATE->ret > 7) {
piMSleep(10);
return;
}
if (PRIVATE->ret == 1) { if (PRIVATE->ret == 1) {
if (rc[0] == 8) if (rc[0] == 8)
ke.key = Backspace; ke.key = Backspace;
@@ -435,26 +343,25 @@ void PIKbdListener::readKeyboard() {
// 8 - ctrl+alt+shift 7 // 8 - ctrl+alt+shift 7
if (rc[0] == '\e') { // search for Alt if (rc[0] == '\e') { // search for Alt
if (PRIVATE->ret == 2) { if (PRIVATE->ret == 2) {
mod = 2; mod = 2;
ke.key = PIChar::fromConsole(rc[1]).unicode16Code(); ke.key = PIChar::fromConsole(rc[1]).unicode16Code();
} else { // escape-seq } else {// escape-seq
if (rc[1] == '\e') { // search for Alt if (rc[1] == '\e') { // search for Alt
for (int i = 1; i < 7; ++i) for (int i = 1; i < 7; ++i) rc[i] = rc[i + 1];
rc[i] = rc[i + 1];
mod = 2; mod = 2;
PRIVATE->ret--; PRIVATE->ret--;
} }
if (rc[1] == '[') { if (rc[1] == '[') {
if (rc[2] == 'M') { // mouse if (rc[2] == 'M') { // mouse
if (PRIVATE->ret >= 5) { if (PRIVATE->ret >= 5) {
me.x = uchar(rc[4] - '!'); me.x = uchar(rc[4] - '!');
me.y = uchar(rc[5] - '!'); me.y = uchar(rc[5] - '!');
int b = rc[3] & 0x7, a = rc[3] & 0x60; int b = rc[3] & 0x7, a = rc[3] & 0x60;
if (a == 0x60) { if (a == 0x60) {
memcpy((void *)(&we), (const void *)(&me), sizeof(me)); memcpy((void*)(&we), (const void*)(&me), sizeof(me));
we.action = MouseWheel; we.action = MouseWheel;
we.direction = (b == 0); we.direction = (b == 0);
// piCout << "wheel" << we.direction; //piCout << "wheel" << we.direction;
wheelEvent(we, kbddata_); wheelEvent(we, kbddata_);
} else { } else {
switch (b) { switch (b) {
@@ -465,7 +372,7 @@ void PIKbdListener::readKeyboard() {
} }
if (a == 0x20) { if (a == 0x20) {
if (b == 3) { if (b == 3) {
me.action = MouseButtonRelease; me.action = MouseButtonRelease;
me.buttons = 0; me.buttons = 0;
} else { } else {
if (tm_dbl.elapsed_m() <= dbl_interval && prev_p_me.x == me.x && prev_p_me.y == me.y) if (tm_dbl.elapsed_m() <= dbl_interval && prev_p_me.x == me.x && prev_p_me.y == me.y)
@@ -478,19 +385,18 @@ void PIKbdListener::readKeyboard() {
} }
} }
if (a == 0x40) me.action = MouseMove; if (a == 0x40) me.action = MouseMove;
// PIString _s[] = {"press", "release", "dbl click", "move"}; //PIString _s[] = {"press", "release", "dbl click", "move"};
// piCoutObj << _s[me.action] << me.buttons << ":" << me.x << me.y; //piCoutObj << _s[me.action] << me.buttons << ":" << me.x << me.y;
mouseEvent(me, kbddata_); mouseEvent(me, kbddata_);
} }
// piCout << me.x << me.y << PICoutManipulators::Hex << b << a; //piCout << me.x << me.y << PICoutManipulators::Hex << b << a;
} }
return; return;
} else { } else {
for (int i = 2; i < 7; ++i) // search for modifier for (int i = 2; i < 7; ++i) // search for modifier
if (rc[i] == ';') { if (rc[i] == ';') {
mod |= rc[i + 1] - '0' - 1; mod |= rc[i + 1] - '0' - 1;
for (int j = i; j < 6; ++j) for (int j = i; j < 6; ++j) rc[j] = rc[j + 2];
rc[j] = rc[j + 2];
rc[6] = rc[7] = 0; rc[6] = rc[7] = 0;
PRIVATE->ret -= 2; PRIVATE->ret -= 2;
break; break;
@@ -500,15 +406,14 @@ void PIKbdListener::readKeyboard() {
if (PRIVATE->ret >= 3 && rc[1] == 'O') { // search for modifier (F1-F4) if (PRIVATE->ret >= 3 && rc[1] == 'O') { // search for modifier (F1-F4)
if (rc[2] >= '1' && rc[2] <= '8') { if (rc[2] >= '1' && rc[2] <= '8') {
mod |= rc[2] - '0' - 1; mod |= rc[2] - '0' - 1;
for (int j = 2; j < 6; ++j) for (int j = 2; j < 6; ++j) rc[j] = rc[j + 1];
rc[j] = rc[j + 1];
rc[7] = 0; rc[7] = 0;
PRIVATE->ret--; PRIVATE->ret--;
} }
} }
for (int i = 0;; ++i) { for (int i = 0; ; ++i) {
if (!esc_seq[i].seq) break; if (!esc_seq[i].seq) break;
// piCout << "search" << rc[1] << esc_seq[i].seq; //piCout << "search" << rc[1] << esc_seq[i].seq;
if (strcmp(esc_seq[i].seq, &(rc[1])) == 0) { if (strcmp(esc_seq[i].seq, &(rc[1])) == 0) {
ke.key = esc_seq[i].key; ke.key = esc_seq[i].key;
mod |= esc_seq[i].mod; mod |= esc_seq[i].mod;
@@ -520,16 +425,18 @@ void PIKbdListener::readKeyboard() {
if (mod & 0x1) ke.modifiers |= Shift; if (mod & 0x1) ke.modifiers |= Shift;
if (mod & 0x2) ke.modifiers |= Alt; if (mod & 0x2) ke.modifiers |= Alt;
if (mod & 0x4) ke.modifiers |= Ctrl; if (mod & 0x4) ke.modifiers |= Ctrl;
// if (mod & 0x8) ke.modifiers |= Meta; //if (mod & 0x8) ke.modifiers |= Meta;
} }
/*cout << "wo mods (" << mod << ")\n"; /*cout << "wo mods (" << mod << ")\n";
for (int i = 0; i < PRIVATE->ret; ++i) for (int i = 0; i < PRIVATE->ret; ++i)
cout << "'" << (char)(rc[i]) << "' "; cout << "'" << (char)(rc[i]) << "' ";
cout << endl;*/ cout << endl;*/
} }
if (ke.key == 0 && PRIVATE->ret > 1) ke.key = PIChar::fromSystem(rc).unicode16Code(); if (ke.key == 0 && PRIVATE->ret > 1)
ke.key = PIChar(rc).unicode16Code();
#endif #endif
if ((rc[0] == '\n' || rc[0] == '\r') && PRIVATE->ret == 1) ke.key = Return; if ((rc[0] == '\n' || rc[0] == '\r') && PRIVATE->ret == 1)
ke.key = Return;
if (exit_enabled && ke.key == exit_key) { if (exit_enabled && ke.key == exit_key) {
PIKbdListener::exiting = true; PIKbdListener::exiting = true;
return; return;
@@ -541,13 +448,8 @@ void PIKbdListener::readKeyboard() {
} }
void PIKbdListener::stop() {
PIThread::stop();
}
void PIKbdListener::end() { void PIKbdListener::end() {
// cout << "list end" << endl; //cout << "list end" << endl;
#ifdef WINDOWS #ifdef WINDOWS
SetConsoleMode(PRIVATE->hIn, PRIVATE->smode); SetConsoleMode(PRIVATE->hIn, PRIVATE->smode);
#else #else

View File

@@ -5,89 +5,83 @@
* \~russian Консольный захват клавиатуры * \~russian Консольный захват клавиатуры
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Keyboard grabber for console Keyboard grabber for console
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PIKBDLISTENER_H #ifndef PIKBDLISTENER_H
#define PIKBDLISTENER_H #define PIKBDLISTENER_H
#include "pithread.h" #include "pithread.h"
#include "pitime.h"
#define WAIT_FOR_EXIT \ #define WAIT_FOR_EXIT while (!PIKbdListener::exiting) piMSleep(PIP_MIN_MSLEEP*5); // TODO: rewrite with condvar
while (!PIKbdListener::exiting) \
piMSleep(PIP_MIN_MSLEEP * 5); \
if (PIKbdListener::instance()) PIKbdListener::instance()->stopAndWait();
class PIP_EXPORT PIKbdListener: public PIThread { class PIP_EXPORT PIKbdListener: public PIThread
PIOBJECT_SUBCLASS(PIKbdListener, PIThread); {
PIOBJECT_SUBCLASS(PIKbdListener, PIThread)
friend class PIConsole; friend class PIConsole;
friend class PITerminal; friend class PITerminal;
public: public:
//! Special keyboard keys //! Special keyboard keys
enum SpecialKey { enum SpecialKey {
Tab /** Tab key */ = 0x09, Tab /** Tab key */ = 0x09,
Return /** Enter key */ = 0x0a, Return /** Enter key */ = 0x0a,
Esc /** Escape key */ = 0x1b, Esc /** Escape key */ = 0x1b,
Space /** Space key */ = 0x20, Space /** Space key */ = 0x20,
Backspace /** Backspace key */ = 0x7f, Backspace /** Backspace key */ = 0x7f,
UpArrow /** Up arrow key */ = -1, UpArrow /** Up arrow key */ = -1,
DownArrow /** Down arrow key */ = -2, DownArrow /** Down arrow key */ = -2,
RightArrow /** Right arrow key */ = -3, RightArrow /** Right arrow key */ = -3,
LeftArrow /** Left arrow key */ = -4, LeftArrow /** Left arrow key */ = -4,
Home /** Home key */ = -5, Home /** Home key */ = -5,
End /** End key */ = -6, End /** End key */ = -6,
PageUp /** Page up key */ = -7, PageUp /** Page up key */ = -7,
PageDown /** Page down key */ = -8, PageDown /** Page down key */ = -8,
Insert /** Delete key */ = -9, Insert /** Delete key */ = -9,
Delete /** Delete key */ = -10, Delete /** Delete key */ = -10,
F1 /** F1 key */ = -11, F1 /** F1 key */ = -11,
F2 /** F2 key */ = -12, F2 /** F2 key */ = -12,
F3 /** F3 key */ = -13, F3 /** F3 key */ = -13,
F4 /** F4 key */ = -14, F4 /** F4 key */ = -14,
F5 /** F5 key */ = -15, F5 /** F5 key */ = -15,
F6 /** F6 key */ = -16, F6 /** F6 key */ = -16,
F7 /** F7 key */ = -17, F7 /** F7 key */ = -17,
F8 /** F8 key */ = -18, F8 /** F8 key */ = -18,
F9 /** F9 key */ = -19, F9 /** F9 key */ = -19,
F10 /** F10 key */ = -20, F10 /** F10 key */ = -20,
F11 /** F11 key */ = -21, F11 /** F11 key */ = -21,
F12 /** F12 key */ = -22 F12 /** F12 key */ = -22
}; };
//! Keyboard modifiers //! Keyboard modifiers
enum KeyModifier { enum KeyModifier {
Ctrl /** Control key */ = 0x1, Ctrl /** Control key */ = 0x1,
Shift /** Shift key */ = 0x2, Shift /** Shift key */ = 0x2,
Alt /** Alt key */ = 0x4 Alt /** Alt key */ = 0x4
// Meta /** Meta (windows) key */ = 0x8 //Meta /** Meta (windows) key */ = 0x8
}; };
typedef PIFlags<KeyModifier> KeyModifiers; typedef PIFlags<KeyModifier> KeyModifiers;
//! This struct contains information about pressed keyboard key //! This struct contains information about pressed keyboard key
struct PIP_EXPORT KeyEvent { struct PIP_EXPORT KeyEvent {
KeyEvent(int k = 0, KeyModifiers m = 0) { KeyEvent(int k = 0, KeyModifiers m = 0) {key = k; modifiers = m;}
key = k;
modifiers = m;
}
//! Pressed key. It can be simple \b char or special key (see PIKbdListener::SpecialKey) //! Pressed key. It can be simple \b char or special key (see PIKbdListener::SpecialKey)
int key; int key;
@@ -98,8 +92,8 @@ public:
//! Mouse buttons //! Mouse buttons
enum MouseButton { enum MouseButton {
MouseLeft /** Left button */ = 0x01, MouseLeft /** Left button */ = 0x01,
MouseRight /** Right button */ = 0x02, MouseRight /** Right button */ = 0x02,
MouseMiddle /** Middle button */ = 0x04 MouseMiddle /** Middle button */ = 0x04
}; };
@@ -116,12 +110,7 @@ public:
//! This struct contains information about mouse action //! This struct contains information about mouse action
struct PIP_EXPORT MouseEvent { struct PIP_EXPORT MouseEvent {
MouseEvent(MouseAction a = MouseButtonPress, MouseButtons b = 0, KeyModifiers m = 0) { MouseEvent(MouseAction a = MouseButtonPress, MouseButtons b = 0, KeyModifiers m = 0) {x = y = 0; action = a; buttons = b; modifiers = m;}
x = y = 0;
action = a;
buttons = b;
modifiers = m;
}
//! Event X coordinate in view-space, from 0 //! Event X coordinate in view-space, from 0
int x; int x;
@@ -141,7 +130,7 @@ public:
//! This struct contains information about mouse wheel action //! This struct contains information about mouse wheel action
struct PIP_EXPORT WheelEvent: public MouseEvent { struct PIP_EXPORT WheelEvent: public MouseEvent {
WheelEvent(): MouseEvent() { direction = false; } WheelEvent(): MouseEvent() {direction = false;}
//! Wheel direction, /b true - up, /b fasle - down //! Wheel direction, /b true - up, /b fasle - down
bool direction; bool direction;
@@ -156,50 +145,43 @@ public:
//! Returns custom data //! Returns custom data
void * data() { return kbddata_; } void * data() {return kbddata_;}
//! Set custom data to "_data" //! Set custom data to "_data"
void setData(void * _data) { kbddata_ = _data; } void setData(void * _data) {kbddata_ = _data;}
//! Set external function to "slot" //! Set external function to "slot"
void setSlot(KBFunc slot) { ret_func = slot; } void setSlot(KBFunc slot) {ret_func = slot;}
//! Set external function to "slot" //! Set external function to "slot"
void setSlot(std::function<void(KeyEvent)> slot) { void setSlot(std::function<void(KeyEvent)> slot) {ret_func = [slot](KeyEvent e, void *){slot(e);};}
ret_func = [slot](KeyEvent e, void *) { slot(e); };
}
//! Returns if exit key if awaiting //! Returns if exit key if awaiting
bool exitCaptured() const { return exit_enabled; } bool exitCaptured() const {return exit_enabled;}
//! Returns exit key, default 'Q' //! Returns exit key, default 'Q'
int exitKey() const { return exit_key; } int exitKey() const {return exit_key;}
double doubleClickInterval() const { return dbl_interval; } double doubleClickInterval() const {return dbl_interval;}
void setDoubleClickInterval(double v) { dbl_interval = v; } void setDoubleClickInterval(double v) {dbl_interval = v;}
void readKeyboard(); void readKeyboard();
void stop();
//! Returns if keyboard listening is active (not running!) //! Returns if keyboard listening is active (not running!)
bool isActive() { return is_active; } bool isActive() {return is_active;}
EVENT_HANDLER(void, enableExitCapture) { enableExitCapture('Q'); } EVENT_HANDLER( void, enableExitCapture) {enableExitCapture('Q');}
EVENT_HANDLER1(void, enableExitCapture, int, key) { EVENT_HANDLER1(void, enableExitCapture, int, key) {exit_enabled = true; exit_key = key;}
exit_enabled = true; EVENT_HANDLER(void, disableExitCapture) {exit_enabled = false;}
exit_key = key; EVENT_HANDLER(void, setActive) {setActive(true);}
}
EVENT_HANDLER(void, disableExitCapture) { exit_enabled = false; }
EVENT_HANDLER(void, setActive) { setActive(true); }
EVENT_HANDLER1(void, setActive, bool, yes); EVENT_HANDLER1(void, setActive, bool, yes);
EVENT2(keyPressed, PIKbdListener::KeyEvent, key, void *, data); EVENT2(keyPressed, PIKbdListener::KeyEvent, key, void * , data)
EVENT2(mouseEvent, PIKbdListener::MouseEvent, mouse, void *, data); EVENT2(mouseEvent, PIKbdListener::MouseEvent, mouse, void * , data)
EVENT2(wheelEvent, PIKbdListener::WheelEvent, wheel, void *, data); EVENT2(wheelEvent, PIKbdListener::WheelEvent, wheel, void * , data)
//! \handlers //! \handlers
//! \{ //! \{
//! \fn void enableExitCapture(int key = 'Q') //! \fn void enableExitCapture(int key = 'Q')
//! \brief Enable exit key "key" awaiting //! \brief Enable exit key "key" awaiting
@@ -210,22 +192,22 @@ public:
//! \fn void setActive(bool yes = true) //! \fn void setActive(bool yes = true)
//! \brief Set keyboard listening is active or not //! \brief Set keyboard listening is active or not
//! \} //! \}
//! \events //! \events
//! \{ //! \{
//! \fn void keyPressed(PIKbdListener::KeyEvent key, void * data) //! \fn void keyPressed(PIKbdListener::KeyEvent key, void * data)
//! \brief Raise on key "key" pressed, "data" is custom data //! \brief Raise on key "key" pressed, "data" is custom data
//! \} //! \}
static bool exiting; static bool exiting;
static PIKbdListener * instance() { return _object; } static PIKbdListener * instance() {return _object;}
private: private:
void begin() override; void begin();
void run() override { readKeyboard(); } void run() {readKeyboard();}
void end() override; void end();
#ifndef WINDOWS #ifndef WINDOWS
struct PIP_EXPORT EscSeq { struct PIP_EXPORT EscSeq {
@@ -243,7 +225,7 @@ private:
vt_none, vt_none,
vt_xterm = 0x1, vt_xterm = 0x1,
vt_linux = 0x2, vt_linux = 0x2,
vt_all = 0xFF vt_all = 0xFF
}; };
static const EscSeq esc_seq[]; static const EscSeq esc_seq[];
@@ -261,42 +243,17 @@ private:
MouseEvent me, prev_me, prev_p_me; MouseEvent me, prev_me, prev_p_me;
WheelEvent we; WheelEvent we;
static PIKbdListener * _object; static PIKbdListener * _object;
}; };
//! \relatesalso PIBinaryStream inline PIByteArray & operator <<(PIByteArray & s, const PIKbdListener::KeyEvent & v) {s << v.key << v.modifiers; return s;}
//! \~english Store operator inline PIByteArray & operator <<(PIByteArray & s, const PIKbdListener::MouseEvent & v) {s << v.x << v.y << (int)v.action << v.buttons << v.modifiers; return s;}
//! \~russian Оператор сохранения inline PIByteArray & operator <<(PIByteArray & s, const PIKbdListener::WheelEvent & v) {s << (*(PIKbdListener::MouseEvent*)&v) << (uchar)v.direction; return s;}
BINARY_STREAM_WRITE(PIKbdListener::MouseEvent) {
s << v.x << v.y << v.action << v.buttons << v.modifiers;
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Restore operator
//! \~russian Оператор извлечения
BINARY_STREAM_READ(PIKbdListener::MouseEvent) {
s >> v.x >> v.y >> v.action >> v.buttons >> v.modifiers;
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Store operator
//! \~russian Оператор сохранения
BINARY_STREAM_WRITE(PIKbdListener::WheelEvent) {
s << (*(PIKbdListener::MouseEvent *)&v) << v.direction;
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Restore operator
//! \~russian Оператор извлечения
BINARY_STREAM_READ(PIKbdListener::WheelEvent) {
s >> (*(PIKbdListener::MouseEvent *)&v) >> v.direction;
return s;
}
inline PIByteArray & operator >>(PIByteArray & s, PIKbdListener::KeyEvent & v) {s >> v.key >> v.modifiers; return s;}
inline PIByteArray & operator >>(PIByteArray & s, PIKbdListener::MouseEvent & v) {int a(0); s >> v.x >> v.y >> a >> v.buttons >> v.modifiers; v.action = (PIKbdListener::MouseAction)a; return s;}
inline PIByteArray & operator >>(PIByteArray & s, PIKbdListener::WheelEvent & v) {uchar d(0); s >> (*(PIKbdListener::MouseEvent*)&v) >> d; v.direction = d; return s;}
REGISTER_PIVARIANTSIMPLE(PIKbdListener::KeyEvent) REGISTER_PIVARIANTSIMPLE(PIKbdListener::KeyEvent)
REGISTER_PIVARIANTSIMPLE(PIKbdListener::MouseEvent) REGISTER_PIVARIANTSIMPLE(PIKbdListener::MouseEvent)

View File

@@ -5,105 +5,104 @@
* \~russian Консольный тайловый менеджер * \~russian Консольный тайловый менеджер
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Console GUI Console GUI
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PISCREEN_H #ifndef PISCREEN_H
#define PISCREEN_H #define PISCREEN_H
#include "pip_console_export.h" #include "pip_console_export.h"
#include "piscreendrawer.h"
#include "piscreentile.h" #include "piscreentile.h"
#include "piscreendrawer.h"
class PIP_CONSOLE_EXPORT PIScreen class PIP_CONSOLE_EXPORT PIScreen: public PIThread, public PIScreenTypes::PIScreenBase
: public PIThread {
, public PIScreenTypes::PIScreenBase { PIOBJECT_SUBCLASS(PIScreen, PIThread)
PIOBJECT_SUBCLASS(PIScreen, PIThread);
class SystemConsole; class SystemConsole;
public: public:
//! Constructs %PIScreen with key handler "slot" and if "startNow" start it //! Constructs %PIScreen with key handler "slot" and if "startNow" start it
PIScreen(bool startNow = true, PIKbdListener::KBFunc slot = 0); PIScreen(bool startNow = true, PIKbdListener::KBFunc slot = 0);
~PIScreen(); ~PIScreen();
//! Directly call function from \a PIKbdListener //! Directly call function from \a PIKbdListener
void enableExitCapture(int key = 'Q') { listener->enableExitCapture(key); } void enableExitCapture(int key = 'Q') {listener->enableExitCapture(key);}
//! Directly call function from \a PIKbdListener //! Directly call function from \a PIKbdListener
void disableExitCapture() { listener->disableExitCapture(); } void disableExitCapture() {listener->disableExitCapture();}
//! Directly call function from \a PIKbdListener //! Directly call function from \a PIKbdListener
bool exitCaptured() const { return listener->exitCaptured(); } bool exitCaptured() const {return listener->exitCaptured();}
//! Directly call function from \a PIKbdListener //! Directly call function from \a PIKbdListener
int exitKey() const { return listener->exitKey(); } int exitKey() const {return listener->exitKey();}
int windowWidth() const {return console.width;}
int windowHeight() const {return console.height;}
int windowWidth() const { return console.width; } bool isMouseEnabled() const {return mouse_;}
int windowHeight() const { return console.height; }
bool isMouseEnabled() const { return mouse_; }
void setMouseEnabled(bool on); void setMouseEnabled(bool on);
PIScreenTile * rootTile() { return &root; } PIScreenTile * rootTile() {return &root;}
PIScreenTile * tileByName(const PIString & name); PIScreenTile * tileByName(const PIString & name);
void setDialogTile(PIScreenTile * t); void setDialogTile(PIScreenTile * t);
PIScreenTile * dialogTile() const { return tile_dialog; } PIScreenTile * dialogTile() const {return tile_dialog;}
PIScreenDrawer * drawer() { return &drawer_; } PIScreenDrawer * drawer() {return &drawer_;}
void clear() { drawer_.clear(); } void clear() {drawer_.clear();}
void resize(int w, int h) { console.resize(w, h); } void resize(int w, int h) {console.resize(w, h);}
EVENT_HANDLER0(void, waitForFinish); EVENT_HANDLER0(void, waitForFinish);
EVENT_HANDLER0(void, start) { start(false); } EVENT_HANDLER0(void, start) {start(false);}
EVENT_HANDLER1(void, start, bool, wait); EVENT_HANDLER1(void, start, bool, wait) {PIThread::start(40); if (wait) waitForFinish();}
EVENT_HANDLER0(void, stop) { stop(false); } EVENT_HANDLER0(void, stop) {stop(false);}
EVENT_HANDLER1(void, stop, bool, clear); EVENT_HANDLER1(void, stop, bool, clear);
EVENT2(keyPressed, PIKbdListener::KeyEvent, key, void *, data); EVENT2(keyPressed, PIKbdListener::KeyEvent, key, void * , data)
EVENT2(tileEvent, PIScreenTile *, tile, PIScreenTypes::TileEvent, e); EVENT2(tileEvent, PIScreenTile * , tile, PIScreenTypes::TileEvent, e)
//! \handlers //! \handlers
//! \{ //! \{
//! \fn void waitForFinish() //! \fn void waitForFinish()
//! \brief block until finished (exit key will be pressed) //! \brief block until finished (exit key will be pressed)
//! \fn void start(bool wait = false) //! \fn void start(bool wait = false)
//! \brief Start console output and if "wait" block until finished (exit key will be pressed) //! \brief Start console output and if "wait" block until finished (exit key will be pressed)
//! \fn void stop(bool clear = false) //! \fn void stop(bool clear = false)
//! \brief Stop console output and if "clear" clear the screen //! \brief Stop console output and if "clear" clear the screen
//! \} //! \}
//! \events //! \events
//! \{ //! \{
//! \fn void keyPressed(PIKbdListener::KeyEvent key, void * data) //! \fn void keyPressed(PIKbdListener::KeyEvent key, void * data)
//! \brief Raise on key "key" pressed, "data" is pointer to %PIConsole object //! \brief Raise on key "key" pressed, "data" is pointer to %PIConsole object
//! \fn void tileEvent(PIScreenTile * tile, PIScreenTypes::TileEvent e) //! \fn void tileEvent(PIScreenTile * tile, PIScreenTypes::TileEvent e)
//! \brief Raise on some event "e" from tile "tile" //! \brief Raise on some event "e" from tile "tile"
//! \} //! \}
private: private:
class PIP_CONSOLE_EXPORT SystemConsole { class PIP_CONSOLE_EXPORT SystemConsole {
public: public:
@@ -132,23 +131,23 @@ private:
PRIVATE_DECLARATION(PIP_CONSOLE_EXPORT) PRIVATE_DECLARATION(PIP_CONSOLE_EXPORT)
int width, height, pwidth, pheight; int width, height, pwidth, pheight;
int mouse_x, mouse_y; int mouse_x, mouse_y;
PIVector<PIVector<PIScreenTypes::Cell>> cells, pcells; PIVector<PIVector<PIScreenTypes::Cell> > cells, pcells;
}; };
void begin() override; void begin();
void run() override; void run();
void end() override; void end();
void key_event(PIKbdListener::KeyEvent key); void key_event(PIKbdListener::KeyEvent key);
EVENT_HANDLER1(void, mouse_event, PIKbdListener::MouseEvent, me); EVENT_HANDLER1(void, mouse_event, PIKbdListener::MouseEvent, me);
EVENT_HANDLER1(void, wheel_event, PIKbdListener::WheelEvent, we); EVENT_HANDLER1(void, wheel_event, PIKbdListener::WheelEvent, we);
static void key_eventS(PIKbdListener::KeyEvent key, void * t) { ((PIScreen *)t)->key_event(key); } static void key_eventS(PIKbdListener::KeyEvent key, void * t) {((PIScreen*)t)->key_event(key);}
PIVector<PIScreenTile *> tiles() { return root.children(); } PIVector<PIScreenTile*> tiles() {return root.children();}
PIVector<PIScreenTile *> prepareMouse(PIKbdListener::MouseEvent * e); PIVector<PIScreenTile*> prepareMouse(PIKbdListener::MouseEvent * e);
PIVector<PIScreenTile *> tilesUnderMouse(int x, int y); PIVector<PIScreenTile*> tilesUnderMouse(int x, int y);
bool nextFocus(PIScreenTile * rt, PIKbdListener::KeyEvent key = PIKbdListener::KeyEvent()); bool nextFocus(PIScreenTile * rt, PIKbdListener::KeyEvent key = PIKbdListener::KeyEvent());
void tileEventInternal(PIScreenTile * t, PIScreenTypes::TileEvent e) override; void tileEventInternal(PIScreenTile * t, PIScreenTypes::TileEvent e);
void tileRemovedInternal(PIScreenTile * t) override; void tileRemovedInternal(PIScreenTile * t);
void tileSetFocusInternal(PIScreenTile * t) override; void tileSetFocusInternal(PIScreenTile * t);
bool mouse_; bool mouse_;
SystemConsole console; SystemConsole console;
@@ -156,7 +155,8 @@ private:
PIKbdListener * listener; PIKbdListener * listener;
PIKbdListener::KBFunc ret_func; PIKbdListener::KBFunc ret_func;
PIScreenTile root; PIScreenTile root;
PIScreenTile *tile_focus, *tile_dialog; PIScreenTile * tile_focus, * tile_dialog;
}; };

View File

@@ -5,22 +5,22 @@
* \~russian Тайл для PIScreen с API PIConsole * \~russian Тайл для PIScreen с API PIConsole
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Tile for PIScreen with PIConsole API Tile for PIScreen with PIConsole API
Andrey Bychkov work.a.b@yandex.ru Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PISCREENCONSOLE_H #ifndef PISCREENCONSOLE_H
@@ -35,15 +35,10 @@
class PIP_CONSOLE_EXPORT TileVars: public PIScreenTile { class PIP_CONSOLE_EXPORT TileVars: public PIScreenTile {
public: public:
TileVars(const PIString & n = PIString()); TileVars(const PIString & n = PIString());
protected: protected:
struct PIP_CONSOLE_EXPORT Variable { struct PIP_CONSOLE_EXPORT Variable {
Variable() { Variable() {nx = ny = type = offset = bitFrom = bitCount = size = 0; format = PIScreenTypes::CellFormat(); ptr = 0;}
nx = ny = type = offset = bitFrom = bitCount = size = 0; bool isEmpty() const {return (ptr == 0);}
format = PIScreenTypes::CellFormat();
ptr = 0;
}
bool isEmpty() const { return (ptr == 0); }
PIString name; PIString name;
PIScreenTypes::CellFormat format; PIScreenTypes::CellFormat format;
int nx; int nx;
@@ -55,26 +50,28 @@ protected:
int size; int size;
const void * ptr; const void * ptr;
/*void operator =(const Variable & src) { /*void operator =(const Variable & src) {
name = src.name; name = src.name;
format = src.format; format = src.format;
nx = src.nx; nx = src.nx;
ny = src.ny; ny = src.ny;
type = src.type; type = src.type;
offset = src.offset; offset = src.offset;
bitFrom = src.bitFrom; bitFrom = src.bitFrom;
bitCount = src.bitCount; bitCount = src.bitCount;
size = src.size; size = src.size;
ptr = src.ptr; ptr = src.ptr;
}*/ }*/
}; };
PIVector<Variable> variables; PIVector<Variable> variables;
PIScreenTypes::Alignment alignment; PIScreenTypes::Alignment alignment;
void sizeHint(int & w, int & h) const override; void sizeHint(int & w, int & h) const;
void drawEvent(PIScreenDrawer * d) override; void drawEvent(PIScreenDrawer * d);
}; };
class PIP_CONSOLE_EXPORT PIScreenConsoleTile: public PIScreenTile {
class PIP_CONSOLE_EXPORT PIScreenConsoleTile : public PIScreenTile
{
public: public:
PIScreenConsoleTile(); PIScreenConsoleTile();
}; };

View File

@@ -5,22 +5,22 @@
* \~russian Отрисовщик для PIScreen * \~russian Отрисовщик для PIScreen
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Drawer for PIScreen Drawer for PIScreen
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PISCREENDRAWER_H #ifndef PISCREENDRAWER_H
@@ -30,10 +30,10 @@
#include "piscreentypes.h" #include "piscreentypes.h"
#include "pistring.h" #include "pistring.h"
class PIP_CONSOLE_EXPORT PIScreenDrawer { class PIP_CONSOLE_EXPORT PIScreenDrawer
{
friend class PIScreen; friend class PIScreen;
PIScreenDrawer(PIVector<PIVector<PIScreenTypes::Cell>> & c); PIScreenDrawer(PIVector<PIVector<PIScreenTypes::Cell> > & c);
public: public:
enum ArtChar { enum ArtChar {
LineVertical = 1, LineVertical = 1,
@@ -46,62 +46,26 @@ public:
Unchecked, Unchecked,
Checked Checked
}; };
void clear(); void clear();
void clearRect(int x0, int y0, int x1, int y1) { fillRect(x0, y0, x1, y1, ' '); } void clearRect(int x0, int y0, int x1, int y1) {fillRect(x0, y0, x1, y1, ' ');}
void drawPixel(int x, 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);
int y, void drawLine(int x0, int y0, int x1, int y1, const PIChar & c, PIScreenTypes::Color col_char = PIScreenTypes::Default, PIScreenTypes::Color col_back = PIScreenTypes::Default, PIScreenTypes::CharFlags flags_char = 0);
const PIChar & c, void drawRect(int x0, int y0, int x1, int y1, const PIChar & c, PIScreenTypes::Color col_char = PIScreenTypes::Default, PIScreenTypes::Color col_back = PIScreenTypes::Default, PIScreenTypes::CharFlags flags_char = 0);
PIScreenTypes::Color col_char = PIScreenTypes::Default, void drawFrame(int x0, int y0, int x1, int y1, PIScreenTypes::Color col_char = PIScreenTypes::Default, PIScreenTypes::Color col_back = PIScreenTypes::Default, PIScreenTypes::CharFlags flags_char = 0);
PIScreenTypes::Color col_back = PIScreenTypes::Default, 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);
PIScreenTypes::CharFlags flags_char = 0); void fillRect(int x0, int y0, int x1, int y1, const PIChar & c, PIScreenTypes::Color col_char = PIScreenTypes::Default, PIScreenTypes::Color col_back = PIScreenTypes::Default, PIScreenTypes::CharFlags flags_char = 0);
void drawLine(int x0, void fillRect(int x0, int y0, int x1, int y1, PIVector<PIVector<PIScreenTypes::Cell> > & content);
int y0,
int x1, PIChar artChar(const ArtChar type) const {return arts_.value(type, PIChar(' '));}
int y1,
const PIChar & c,
PIScreenTypes::Color col_char = PIScreenTypes::Default,
PIScreenTypes::Color col_back = PIScreenTypes::Default,
PIScreenTypes::CharFlags flags_char = 0);
void drawRect(int x0,
int y0,
int x1,
int y1,
const PIChar & c,
PIScreenTypes::Color col_char = PIScreenTypes::Default,
PIScreenTypes::Color col_back = PIScreenTypes::Default,
PIScreenTypes::CharFlags flags_char = 0);
void drawFrame(int x0,
int y0,
int x1,
int y1,
PIScreenTypes::Color col_char = PIScreenTypes::Default,
PIScreenTypes::Color col_back = PIScreenTypes::Default,
PIScreenTypes::CharFlags flags_char = 0);
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);
void fillRect(int x0,
int y0,
int x1,
int y1,
const PIChar & c,
PIScreenTypes::Color col_char = PIScreenTypes::Default,
PIScreenTypes::Color col_back = PIScreenTypes::Default,
PIScreenTypes::CharFlags flags_char = 0);
void fillRect(int x0, int y0, int x1, int y1, PIVector<PIVector<PIScreenTypes::Cell>> & content);
PIChar artChar(const ArtChar type) const { return arts_.value(type, PIChar(' ')); } static void clear(PIVector<PIVector<PIScreenTypes::Cell> > & cells);
static void clear(PIVector<PIVector<PIScreenTypes::Cell>> & cells);
private: private:
PIVector<PIVector<PIScreenTypes::Cell>> & cells; PIVector<PIVector<PIScreenTypes::Cell> > & cells;
int width, height; int width, height;
PIMap<ArtChar, PIChar> arts_; PIMap<ArtChar, PIChar> arts_;
}; };

View File

@@ -5,66 +5,58 @@
* \~russian Базовый тайл для PIScreen * \~russian Базовый тайл для PIScreen
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Basic PIScreen tile Basic PIScreen tile
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PISCREENTILE_H #ifndef PISCREENTILE_H
#define PISCREENTILE_H #define PISCREENTILE_H
#include "pikbdlistener.h"
#include "pip_console_export.h" #include "pip_console_export.h"
#include "piscreentypes.h" #include "piscreentypes.h"
#include "pikbdlistener.h"
class PIScreenDrawer; class PIScreenDrawer;
class PIP_CONSOLE_EXPORT PIScreenTile: public PIObject { class PIP_CONSOLE_EXPORT PIScreenTile: public PIObject {
friend class PIScreen; friend class PIScreen;
PIOBJECT_SUBCLASS(PIScreenTile, PIObject); PIOBJECT_SUBCLASS(PIScreenTile, PIObject)
public: public:
PIScreenTile(const PIString & n = PIString(), PIScreenTile(const PIString & n = PIString(), PIScreenTypes::Direction d = PIScreenTypes::Vertical, PIScreenTypes::SizePolicy p = PIScreenTypes::Preferred);
PIScreenTypes::Direction d = PIScreenTypes::Vertical,
PIScreenTypes::SizePolicy p = PIScreenTypes::Preferred);
virtual ~PIScreenTile(); virtual ~PIScreenTile();
void addTile(PIScreenTile * t); void addTile(PIScreenTile * t);
void takeTile(PIScreenTile * t); void takeTile(PIScreenTile * t);
void removeTile(PIScreenTile * t); void removeTile(PIScreenTile * t);
PIScreenTile * parentTile() const { return parent; } PIScreenTile * parentTile() const {return parent;}
PIVector<PIScreenTile *> children(bool only_visible = false); PIVector<PIScreenTile * > children(bool only_visible = false);
PIScreenTile * childUnderMouse(int x, int y); PIScreenTile * childUnderMouse(int x, int y);
void show() { visible = true; } void show() {visible = true;}
void hide() { visible = false; } void hide() {visible = false;}
void setFocus(); void setFocus();
bool hasFocus() const { return has_focus; } bool hasFocus() const {return has_focus;}
void setMargins(int m) { marginLeft = marginRight = marginTop = marginBottom = m; } void setMargins(int m) {marginLeft = marginRight = marginTop = marginBottom = m;}
void setMargins(int l, int r, int t, int b) { void setMargins(int l, int r, int t, int b) {marginLeft = l; marginRight = r; marginTop = t; marginBottom = b;}
marginLeft = l;
marginRight = r;
marginTop = t;
marginBottom = b;
}
int x() const { return x_; }
int y() const { return y_; }
int width() const { return width_; }
int height() const { return height_; }
int x() const {return x_;}
int y() const {return y_;}
int width() const {return width_;}
int height() const {return height_;}
PIScreenTypes::Direction direction; PIScreenTypes::Direction direction;
PIScreenTypes::SizePolicy size_policy; PIScreenTypes::SizePolicy size_policy;
PIScreenTypes::FocusFlags focus_flags; PIScreenTypes::FocusFlags focus_flags;
@@ -75,41 +67,43 @@ public:
int marginLeft, marginRight, marginTop, marginBottom; int marginLeft, marginRight, marginTop, marginBottom;
int spacing; int spacing;
bool visible; bool visible;
protected: protected:
//! Returns desired tile size in "w" and "h" //! Returns desired tile size in "w" and "h"
virtual void sizeHint(int & w, int & h) const; virtual void sizeHint(int & w, int & h) const;
//! Tile has been resized to "w"x"h" //! Tile has been resized to "w"x"h"
virtual void resizeEvent(int w, int h) {} virtual void resizeEvent(int w, int h) {}
//! Draw tile with drawer "d" in world-space coordinates //! Draw tile with drawer "d" in world-space coordinates
virtual void drawEvent(PIScreenDrawer * d) {} virtual void drawEvent(PIScreenDrawer * d) {}
//! Return "true" if you process key //! Return "true" if you process key
virtual bool keyEvent(PIKbdListener::KeyEvent key) { return false; } virtual bool keyEvent(PIKbdListener::KeyEvent key) {return false;}
//! Return "true" if you process event //! Return "true" if you process event
virtual bool mouseEvent(PIKbdListener::MouseEvent me) { return false; } virtual bool mouseEvent(PIKbdListener::MouseEvent me) {return false;}
//! Return "true" if you process wheel //! Return "true" if you process wheel
virtual bool wheelEvent(PIKbdListener::WheelEvent we) { return false; } virtual bool wheelEvent(PIKbdListener::WheelEvent we) {return false;}
void raiseEvent(PIScreenTypes::TileEvent e); void raiseEvent(PIScreenTypes::TileEvent e);
void setScreen(PIScreenTypes::PIScreenBase * s); void setScreen(PIScreenTypes::PIScreenBase * s);
void deleteChildren(); void deleteChildren();
void drawEventInternal(PIScreenDrawer * d); void drawEventInternal(PIScreenDrawer * d);
void layout(); void layout();
bool needLayout() { return size_policy != PIScreenTypes::Ignore; } bool needLayout() {return size_policy != PIScreenTypes::Ignore;}
PIVector<PIScreenTile *> tiles; PIVector<PIScreenTile * > tiles;
PIScreenTile * parent; PIScreenTile * parent;
PIScreenTypes::PIScreenBase * screen; PIScreenTypes::PIScreenBase * screen;
int x_, y_, width_, height_; int x_, y_, width_, height_;
bool has_focus; bool has_focus;
private: private:
int pw, ph; int pw, ph;
}; };

View File

@@ -5,22 +5,22 @@
* \~russian Различные тайлы для PIScreen * \~russian Различные тайлы для PIScreen
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Various tiles for PIScreen Various tiles for PIScreen
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PISCREENTILES_H #ifndef PISCREENTILES_H
@@ -31,8 +31,7 @@
class PIP_CONSOLE_EXPORT TileSimple: public PIScreenTile { class PIP_CONSOLE_EXPORT TileSimple: public PIScreenTile {
PIOBJECT_SUBCLASS(TileSimple, PIScreenTile); PIOBJECT_SUBCLASS(TileSimple, PIScreenTile)
public: public:
typedef PIPair<PIString, PIScreenTypes::CellFormat> Row; typedef PIPair<PIString, PIScreenTypes::CellFormat> Row;
TileSimple(const PIString & n = PIString()); TileSimple(const PIString & n = PIString());
@@ -40,43 +39,39 @@ public:
virtual ~TileSimple() {} virtual ~TileSimple() {}
PIVector<Row> content; PIVector<Row> content;
PIScreenTypes::Alignment alignment; PIScreenTypes::Alignment alignment;
protected: protected:
void sizeHint(int & w, int & h) const override; void sizeHint(int & w, int & h) const;
void drawEvent(PIScreenDrawer * d) override; void drawEvent(PIScreenDrawer * d);
}; };
class TileList; class TileList;
class PIP_CONSOLE_EXPORT TileScrollBar: public PIScreenTile { class PIP_CONSOLE_EXPORT TileScrollBar: public PIScreenTile {
PIOBJECT_SUBCLASS(TileScrollBar, PIScreenTile); PIOBJECT_SUBCLASS(TileScrollBar, PIScreenTile)
friend class TileList; friend class TileList;
public: public:
TileScrollBar(const PIString & n = PIString()); TileScrollBar(const PIString & n = PIString());
virtual ~TileScrollBar() {} virtual ~TileScrollBar() {}
void setMinimum(int v); void setMinimum(int v);
void setMaximum(int v); void setMaximum(int v);
void setValue(int v); void setValue(int v);
int minimum() const { return minimum_; } int minimum() const {return minimum_;}
int maximum() const { return maximum_; } int maximum() const {return maximum_;}
int value() const { return value_; } int value() const {return value_;}
int thickness; int thickness;
protected: protected:
void _check(); void _check();
void sizeHint(int & w, int & h) const override; void sizeHint(int & w, int & h) const;
void drawEvent(PIScreenDrawer * d) override; void drawEvent(PIScreenDrawer * d);
bool mouseEvent(PIKbdListener::MouseEvent me) override; bool mouseEvent(PIKbdListener::MouseEvent me);
int minimum_, maximum_, value_; int minimum_, maximum_, value_;
PIChar line_char; PIChar line_char;
}; };
class PIP_CONSOLE_EXPORT TileList: public PIScreenTile { class PIP_CONSOLE_EXPORT TileList: public PIScreenTile {
PIOBJECT_SUBCLASS(TileList, PIScreenTile); PIOBJECT_SUBCLASS(TileList, PIScreenTile)
public: public:
enum SelectionMode { enum SelectionMode {
NoSelection, NoSelection,
@@ -97,22 +92,20 @@ public:
SelectionMode selection_mode; SelectionMode selection_mode;
PISet<int> selected; PISet<int> selected;
int lhei, cur, offset; int lhei, cur, offset;
protected: protected:
void sizeHint(int & w, int & h) const override; void sizeHint(int & w, int & h) const;
void resizeEvent(int w, int h) override; void resizeEvent(int w, int h);
void drawEvent(PIScreenDrawer * d) override; void drawEvent(PIScreenDrawer * d);
bool keyEvent(PIKbdListener::KeyEvent key) override; bool keyEvent(PIKbdListener::KeyEvent key);
bool mouseEvent(PIKbdListener::MouseEvent me) override; bool mouseEvent(PIKbdListener::MouseEvent me);
bool wheelEvent(PIKbdListener::WheelEvent we) override; bool wheelEvent(PIKbdListener::WheelEvent we);
TileScrollBar * scroll; TileScrollBar * scroll;
bool mouse_sel; bool mouse_sel;
}; };
class PIP_CONSOLE_EXPORT TileButton: public PIScreenTile { class PIP_CONSOLE_EXPORT TileButton: public PIScreenTile {
PIOBJECT_SUBCLASS(TileButton, PIScreenTile); PIOBJECT_SUBCLASS(TileButton, PIScreenTile)
public: public:
TileButton(const PIString & n = PIString()); TileButton(const PIString & n = PIString());
virtual ~TileButton() {} virtual ~TileButton() {}
@@ -121,18 +114,18 @@ public:
}; };
PIScreenTypes::CellFormat format; PIScreenTypes::CellFormat format;
PIString text; PIString text;
protected: protected:
void sizeHint(int & w, int & h) const override; void sizeHint(int & w, int & h) const;
void drawEvent(PIScreenDrawer * d) override; void drawEvent(PIScreenDrawer * d);
bool keyEvent(PIKbdListener::KeyEvent key) override; bool keyEvent(PIKbdListener::KeyEvent key);
bool mouseEvent(PIKbdListener::MouseEvent me) override; bool mouseEvent(PIKbdListener::MouseEvent me);
}; };
class PIP_CONSOLE_EXPORT TileButtons: public PIScreenTile {
PIOBJECT_SUBCLASS(TileButtons, PIScreenTile);
class PIP_CONSOLE_EXPORT TileButtons: public PIScreenTile {
PIOBJECT_SUBCLASS(TileButtons, PIScreenTile)
public: public:
TileButtons(const PIString & n = PIString()); TileButtons(const PIString & n = PIString());
virtual ~TileButtons() {} virtual ~TileButtons() {}
@@ -143,23 +136,21 @@ public:
PIScreenTypes::Alignment alignment; PIScreenTypes::Alignment alignment;
PIVector<Button> content; PIVector<Button> content;
int cur; int cur;
protected: protected:
void sizeHint(int & w, int & h) const override; void sizeHint(int & w, int & h) const;
void drawEvent(PIScreenDrawer * d) override; void drawEvent(PIScreenDrawer * d);
bool keyEvent(PIKbdListener::KeyEvent key) override; bool keyEvent(PIKbdListener::KeyEvent key);
bool mouseEvent(PIKbdListener::MouseEvent me) override; bool mouseEvent(PIKbdListener::MouseEvent me);
struct Rect { struct Rect {
Rect(int _x0 = 0, int _y0 = 0, int _x1 = 0, int _y1 = 0): x0(_x0), y0(_y0), x1(_x1), y1(_y1) {} 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; int x0,y0,x1,y1;
}; };
PIVector<Rect> btn_rects; PIVector<Rect> btn_rects;
}; };
class PIP_CONSOLE_EXPORT TileCheck: public PIScreenTile { class PIP_CONSOLE_EXPORT TileCheck: public PIScreenTile {
PIOBJECT_SUBCLASS(TileCheck, PIScreenTile); PIOBJECT_SUBCLASS(TileCheck, PIScreenTile)
public: public:
TileCheck(const PIString & n = PIString()); TileCheck(const PIString & n = PIString());
virtual ~TileCheck() {} virtual ~TileCheck() {}
@@ -169,18 +160,16 @@ public:
PIScreenTypes::CellFormat format; PIScreenTypes::CellFormat format;
PIString text; PIString text;
bool toggled; bool toggled;
protected: protected:
void sizeHint(int & w, int & h) const override; void sizeHint(int & w, int & h) const;
void drawEvent(PIScreenDrawer * d) override; void drawEvent(PIScreenDrawer * d);
bool keyEvent(PIKbdListener::KeyEvent key) override; bool keyEvent(PIKbdListener::KeyEvent key);
bool mouseEvent(PIKbdListener::MouseEvent me) override; bool mouseEvent(PIKbdListener::MouseEvent me);
}; };
class PIP_CONSOLE_EXPORT TileProgress: public PIScreenTile { class PIP_CONSOLE_EXPORT TileProgress: public PIScreenTile {
PIOBJECT_SUBCLASS(TileProgress, PIScreenTile); PIOBJECT_SUBCLASS(TileProgress, PIScreenTile)
public: public:
TileProgress(const PIString & n = PIString()); TileProgress(const PIString & n = PIString());
virtual ~TileProgress() {} virtual ~TileProgress() {}
@@ -189,42 +178,37 @@ public:
PIString suffix; PIString suffix;
double maximum; double maximum;
double value; double value;
protected: protected:
void sizeHint(int & w, int & h) const override; void sizeHint(int & w, int & h) const;
void drawEvent(PIScreenDrawer * d) override; void drawEvent(PIScreenDrawer * d);
}; };
class PIP_CONSOLE_EXPORT TilePICout: public TileList { class PIP_CONSOLE_EXPORT TilePICout: public TileList {
PIOBJECT_SUBCLASS(TilePICout, PIScreenTile); PIOBJECT_SUBCLASS(TilePICout, PIScreenTile)
public: public:
TilePICout(const PIString & n = PIString()); TilePICout(const PIString & n = PIString());
virtual ~TilePICout() {} virtual ~TilePICout() {}
PIScreenTypes::CellFormat format; PIScreenTypes::CellFormat format;
int max_lines; int max_lines;
protected: protected:
void drawEvent(PIScreenDrawer * d) override; void drawEvent(PIScreenDrawer * d);
bool keyEvent(PIKbdListener::KeyEvent key) override; bool keyEvent(PIKbdListener::KeyEvent key);
}; };
class PIP_CONSOLE_EXPORT TileInput: public PIScreenTile { class PIP_CONSOLE_EXPORT TileInput: public PIScreenTile {
PIOBJECT_SUBCLASS(TileInput, PIScreenTile); PIOBJECT_SUBCLASS(TileInput, PIScreenTile)
public: public:
TileInput(const PIString & n = PIString()); TileInput(const PIString & n = PIString());
virtual ~TileInput() {} virtual ~TileInput() {}
PIScreenTypes::CellFormat format; PIScreenTypes::CellFormat format;
PIString text; PIString text;
int max_length; int max_length;
protected: protected:
void sizeHint(int & w, int & h) const override; void sizeHint(int & w, int & h) const;
void drawEvent(PIScreenDrawer * d) override; void drawEvent(PIScreenDrawer * d);
bool keyEvent(PIKbdListener::KeyEvent key) override; bool keyEvent(PIKbdListener::KeyEvent key);
void reserCursor(); void reserCursor();
int cur, offset; int cur, offset;
bool inv; bool inv;

View File

@@ -5,22 +5,22 @@
* \~russian Типы для PIScreen * \~russian Типы для PIScreen
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Types for PIScreen Types for PIScreen
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PISCREENTYPES_H #ifndef PISCREENTYPES_H
@@ -33,156 +33,121 @@
class PIScreenTile; class PIScreenTile;
namespace PIScreenTypes { namespace PIScreenTypes {
//! Color for chars or background //! Color for chars or background
enum Color { enum Color {
Default /** Default */, Default /** Default */,
Black /** Black */, Black /** Black */,
Red /** Red */, Red /** Red */,
Green /** Green */, Green /** Green */,
Blue /** Blue */, Blue /** Blue */,
Cyan /** Cyan */, Cyan /** Cyan */,
Magenta /** Magenta */, Magenta /** Magenta */,
Yellow /** Yellow */, Yellow /** Yellow */,
White /** White */, White /** White */,
Transparent /** Save previous color */ Transparent /** Save previous color */
}; };
//! Flags for chars //! Flags for chars
enum CharFlag { enum CharFlag {
Bold /** Bold or bright */ = 0x1, Bold /** Bold or bright */ = 0x1,
Blink /** Blink text */ = 0x2, Blink /** Blink text */ = 0x2,
Underline /** Underline text */ = 0x4, Underline /** Underline text */ = 0x4,
Inverse = 0x08 Inverse = 0x08
}; };
//! Alignment //! Alignment
enum Alignment { enum Alignment {
Left /** Left */, Left /** Left */ ,
Center /** Center */, Center /** Center */ ,
Right /** Right */ Right /** Right */
}; };
//! Size policy //! Size policy
enum SizePolicy { enum SizePolicy {
Fixed /** Fixed size */, Fixed /** Fixed size */ ,
Preferred /** Preferred size */, Preferred /** Preferred size */ ,
Expanding /** Maximum available size */, Expanding /** Maximum available size */ ,
Ignore /** Ignore layout logic */ Ignore /** Ignore layout logic */
}; };
//! Direction //! Direction
enum Direction { enum Direction {
Horizontal /** Horizontal */, Horizontal /** Horizontal */ ,
Vertical /** Vertical */ Vertical /** Vertical */
};
//! Focus flags
enum FocusFlag {
CanHasFocus /** Tile can has focus */ = 0x1,
NextByTab /** Focus passed to next tile by tab key */ = 0x2,
NextByArrowsHorizontal /** Focus passed to next tile by arrow keys left or right */ = 0x4,
NextByArrowsVertical /** Focus passed to next tile by arrow keys up or down */ = 0x8,
NextByArrowsAll /** Focus passed to next tile by any arrow key */ = NextByArrowsHorizontal | NextByArrowsVertical,
FocusOnMouse /** Tile focused on mouse press */ = 0x10,
FocusOnWheel /** Tile focused on wheel */ = 0x20,
FocusOnMouseOrWheel /** Tile focused on mouse press or wheel */ = FocusOnMouse | FocusOnWheel
};
typedef PIFlags<CharFlag> CharFlags;
typedef PIFlags<FocusFlag> FocusFlags;
union PIP_CONSOLE_EXPORT CellFormat {
CellFormat(ushort f = 0) { raw_format = f; }
CellFormat(Color col_char, Color col_back = Default, CharFlags flags_ = 0) {
color_char = col_char;
color_back = col_back;
flags = flags_;
}
ushort raw_format;
struct {
ushort color_char: 4;
ushort color_back: 4;
ushort flags : 8;
}; };
bool operator==(const CellFormat & c) const { return raw_format == c.raw_format; }
bool operator!=(const CellFormat & c) const { return raw_format != c.raw_format; }
};
struct PIP_CONSOLE_EXPORT Cell { //! Focus flags
Cell(PIChar c = PIChar(' '), CellFormat f = CellFormat()) { enum FocusFlag {
symbol = c; CanHasFocus /** Tile can has focus */ = 0x1,
format = f; NextByTab /** Focus passed to next tile by tab key */ = 0x2,
} NextByArrowsHorizontal /** Focus passed to next tile by arrow keys left or right */ = 0x4,
CellFormat format; NextByArrowsVertical /** Focus passed to next tile by arrow keys up or down */ = 0x8,
PIChar symbol; NextByArrowsAll /** Focus passed to next tile by any arrow key */ = NextByArrowsHorizontal | NextByArrowsVertical,
bool operator==(const Cell & c) const { return format == c.format && symbol == c.symbol; } FocusOnMouse /** Tile focused on mouse press */ = 0x10,
bool operator!=(const Cell & c) const { return format != c.format || symbol != c.symbol; } FocusOnWheel /** Tile focused on wheel */ = 0x20,
Cell & operator=(const Cell & c) { FocusOnMouseOrWheel /** Tile focused on mouse press or wheel */ = FocusOnMouse | FocusOnWheel
symbol = c.symbol; };
if (c.format.color_back == Transparent) {
format.color_char = c.format.color_char; typedef PIFlags<CharFlag> CharFlags;
format.flags = c.format.flags; typedef PIFlags<FocusFlag> FocusFlags;
} else
format = c.format; union PIP_CONSOLE_EXPORT CellFormat {
return *this; CellFormat(ushort f = 0) {raw_format = f;}
} CellFormat(Color col_char, Color col_back = Default, CharFlags flags_ = 0) {
}; color_char = col_char;
color_back = col_back;
flags = flags_;
}
ushort raw_format;
struct {
ushort color_char : 4;
ushort color_back : 4;
ushort flags : 8;
};
bool operator ==(const CellFormat & c) const {return raw_format == c.raw_format;}
bool operator !=(const CellFormat & c) const {return raw_format != c.raw_format;}
};
struct PIP_CONSOLE_EXPORT Cell {
Cell(PIChar c = PIChar(' '), CellFormat f = CellFormat()) {symbol = c; format = f;}
CellFormat format;
PIChar symbol;
bool operator ==(const Cell & c) const {return format == c.format && symbol == c.symbol;}
bool operator !=(const Cell & c) const {return format != c.format || symbol != c.symbol;}
Cell & operator =(const Cell & c) {
symbol = c.symbol;
if (c.format.color_back == Transparent) {
format.color_char = c.format.color_char;
format.flags = c.format.flags;
} else format = c.format;
return *this;
}
};
struct PIP_CONSOLE_EXPORT TileEvent { struct PIP_CONSOLE_EXPORT TileEvent {
TileEvent(int t = -1, const PIVariant & d = PIVariant()): type(t), data(d) {} TileEvent(int t = -1, const PIVariant & d = PIVariant()): type(t), data(d) {}
int type; int type;
PIVariant data; PIVariant data;
}; };
class PIP_CONSOLE_EXPORT PIScreenBase {
public:
PIScreenBase() {}
virtual ~PIScreenBase() {}
virtual void tileEventInternal(PIScreenTile * , TileEvent) {}
virtual void tileRemovedInternal(PIScreenTile * ) {}
virtual void tileSetFocusInternal(PIScreenTile * ) {}
};
class PIP_CONSOLE_EXPORT PIScreenBase {
public:
PIScreenBase() {}
virtual ~PIScreenBase() {}
virtual void tileEventInternal(PIScreenTile *, TileEvent) {}
virtual void tileRemovedInternal(PIScreenTile *) {}
virtual void tileSetFocusInternal(PIScreenTile *) {}
};
} // namespace PIScreenTypes
// inline PIByteArray & operator <<(PIByteArray & s, const PIScreenTypes::Cell & v) {s << v.format.raw_format << v.symbol; return s;}
// inline PIByteArray & operator >>(PIByteArray & s, PIScreenTypes::Cell & v) {s >> v.format.raw_format >> v.symbol; return s;}
//! \relatesalso PIBinaryStream
//! \~english Store operator
//! \~russian Оператор сохранения
BINARY_STREAM_WRITE(PIScreenTypes::Cell) {
s << v.format.raw_format << v.symbol;
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Restore operator
//! \~russian Оператор извлечения
BINARY_STREAM_READ(PIScreenTypes::Cell) {
s >> v.format.raw_format >> v.symbol;
return s;
} }
//! \relatesalso PIBinaryStream inline PIByteArray & operator <<(PIByteArray & s, const PIScreenTypes::Cell & v) {s << v.format.raw_format << v.symbol; return s;}
//! \~english Store operator inline PIByteArray & operator >>(PIByteArray & s, PIScreenTypes::Cell & v) {s >> v.format.raw_format >> v.symbol; return s;}
//! \~russian Оператор сохранения
BINARY_STREAM_WRITE(PIScreenTypes::TileEvent) {
s << v.type << v.data;
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Restore operator
//! \~russian Оператор извлечения
BINARY_STREAM_READ(PIScreenTypes::TileEvent) {
s >> v.type >> v.data;
return s;
}
inline PIByteArray & operator <<(PIByteArray & s, const PIScreenTypes::TileEvent & v) {s << v.type << v.data; return s;}
inline PIByteArray & operator >>(PIByteArray & s, PIScreenTypes::TileEvent & v) {s >> v.type >> v.data; return s;}
REGISTER_PIVARIANTSIMPLE(PIScreenTypes::TileEvent) REGISTER_PIVARIANTSIMPLE(PIScreenTypes::TileEvent)

View File

@@ -5,60 +5,60 @@
* \~russian Виртуальный терминал * \~russian Виртуальный терминал
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Virtual terminal Virtual terminal
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PITERMINAL_H #ifndef PITERMINAL_H
#define PITERMINAL_H #define PITERMINAL_H
#include "pikbdlistener.h"
#include "pip_console_export.h" #include "pip_console_export.h"
#include "pikbdlistener.h"
#include "piscreentypes.h" #include "piscreentypes.h"
class PIP_CONSOLE_EXPORT PITerminal: public PIThread { class PIP_CONSOLE_EXPORT PITerminal: public PIThread
PIOBJECT_SUBCLASS(PITerminal, PIThread); {
PIOBJECT_SUBCLASS(PITerminal, PIThread)
public: public:
//! Constructs %PITerminal //! Constructs %PITerminal
PITerminal(); PITerminal();
~PITerminal(); ~PITerminal();
int columns() const { return size_x; } int columns() const {return size_x;}
int rows() const { return size_y; } int rows() const {return size_y;}
bool resize(int cols, int rows); bool resize(int cols, int rows);
void write(const PIByteArray & d); void write(const PIByteArray & d);
void write(PIKbdListener::SpecialKey k, PIKbdListener::KeyModifiers m); void write(PIKbdListener::SpecialKey k, PIKbdListener::KeyModifiers m);
void write(PIKbdListener::KeyEvent ke); void write(PIKbdListener::KeyEvent ke);
PIVector<PIVector<PIScreenTypes::Cell>> content(); PIVector<PIVector<PIScreenTypes::Cell> > content();
static bool isSpecialKey(int k); static bool isSpecialKey(int k);
bool initialize(); bool initialize();
void destroy(); void destroy();
private: private:
void initPrivate(); void initPrivate();
void readConsole(); void readConsole();
void getCursor(int & x, int & y); void getCursor(int & x, int & y);
uchar invertColor(uchar c); uchar invertColor(uchar c);
void run() override; void run();
#ifndef WINDOWS #ifndef WINDOWS
void parseInput(const PIString & s); void parseInput(const PIString & s);
bool isCompleteEscSeq(const PIString & es); bool isCompleteEscSeq(const PIString & es);
@@ -72,7 +72,8 @@ private:
int size_x, size_y, cursor_x, cursor_y; int size_x, size_y, cursor_x, cursor_y;
bool cursor_blink, cursor_visible; bool cursor_blink, cursor_visible;
PITimeMeasurer cursor_tm; PITimeMeasurer cursor_tm;
PIVector<PIVector<PIScreenTypes::Cell>> cells; PIVector<PIVector<PIScreenTypes::Cell> > cells;
}; };

View File

@@ -1,35 +1,34 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Base macros for generic containers Base macros for generic containers
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "picontainers.h" #include "picontainers.h"
const size_t minAlloc = 64; const ssize_t minAlloc = 64;
size_t _PIContainerConstantsBase::calcMinCountPoT(size_t szof) { ssize_t _PIContainerConstantsBase::calcMinCountPoT(ssize_t szof) {
size_t ret = 0; ssize_t ret = 0, elc = 1;
size_t elc = 1;
while (elc * szof < minAlloc) { while (elc * szof < minAlloc) {
elc *= 2; elc *= 2;
++ret; ++ret;
} }
// printf("calcMinCount sizeof = %d, min_count = %d, pot = %d\n", szof, elc, ret); //printf("calcMinCount sizeof = %d, min_count = %d, pot = %d\n", szof, elc, ret);
return ret; return ret;
} }

View File

@@ -13,22 +13,22 @@
//! Андрей Бычков work.a.b@yandex.ru; //! Андрей Бычков work.a.b@yandex.ru;
//! \~\} //! \~\}
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Base macros for generic containers Base macros for generic containers
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PICONTAINERS_H #ifndef PICONTAINERS_H
@@ -41,24 +41,23 @@
#else #else
# include <malloc.h> # include <malloc.h>
#endif #endif
#include <initializer_list>
#include <type_traits>
#include <string.h>
#include <new>
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <initializer_list>
#include <new>
#include <string.h>
#include <type_traits>
template<typename C> template <typename C>
class _PIReverseWrapper { class _PIReverseWrapper {
public: public:
_PIReverseWrapper(C & c): c_(c) {} _PIReverseWrapper(C & c): c_(c) {}
_PIReverseWrapper(const C & c): c_(const_cast<C &>(c)) {} _PIReverseWrapper(const C & c): c_(const_cast<C&>(c)) {}
typename C::reverse_iterator begin() { return c_.rbegin(); } typename C::reverse_iterator begin() {return c_.rbegin();}
typename C::reverse_iterator end() { return c_.rend(); } typename C::reverse_iterator end() {return c_.rend(); }
typename C::const_reverse_iterator begin() const { return c_.rbegin(); } typename C::const_reverse_iterator begin() const {return c_.rbegin();}
typename C::const_reverse_iterator end() const { return c_.rend(); } typename C::const_reverse_iterator end() const {return c_.rend(); }
private: private:
C & c_; C & c_;
}; };
@@ -66,30 +65,21 @@ private:
class PIP_EXPORT _PIContainerConstantsBase { class PIP_EXPORT _PIContainerConstantsBase {
public: public:
static size_t calcMinCountPoT(size_t szof); static ssize_t calcMinCountPoT(ssize_t szof);
}; };
template<typename T> template<typename T>
class _PIContainerConstants { class _PIContainerConstants {
public: public:
static size_t minCountPoT() { static ssize_t minCountPoT() {static ssize_t ret = _PIContainerConstantsBase::calcMinCountPoT(sizeof(T)); return ret;}
static const size_t ret = _PIContainerConstantsBase::calcMinCountPoT(sizeof(T));
return ret;
}
}; };
//! \brief //! \brief
//! \~english Template reverse wrapper over any container //! \~english Template reverse wrapper over any container
//! \~russian Шаблонная функция обертки любого контейнера для обратного доступа через итераторы //! \~russian Шаблонная функция обертки любого контейнера для обратного доступа через итераторы
template<typename C> template <typename C> _PIReverseWrapper<C> PIReverseWrap(C & c) {return _PIReverseWrapper<C>(c);}
_PIReverseWrapper<C> PIReverseWrap(C & c) { template <typename C> _PIReverseWrapper<C> PIReverseWrap(const C & c) {return _PIReverseWrapper<C>(c);}
return _PIReverseWrapper<C>(c);
}
template<typename C>
_PIReverseWrapper<C> PIReverseWrap(const C & c) {
return _PIReverseWrapper<C>(c);
}
//! \brief //! \brief
@@ -98,7 +88,7 @@ _PIReverseWrapper<C> PIReverseWrap(const C & c) {
//! \~\param c //! \~\param c
//! \~english Iteration times in loop //! \~english Iteration times in loop
//! \~russian Количество итераций цикла //! \~russian Количество итераций цикла
#define piForTimes(c) for (int _i##c = 0; _i##c < c; ++_i##c) #define piForTimes(c) for(int _i##c = 0; _i##c < c; ++_i##c)
//! \brief //! \brief
@@ -130,7 +120,7 @@ _PIReverseWrapper<C> PIReverseWrap(const C & c) {
//! // 4 //! // 4
//! \endcode //! \endcode
//! \sa \a piForeachC, \a piForeachR, \a piForeachRC //! \sa \a piForeachC, \a piForeachR, \a piForeachRC
#define piForeach(i, c) for (i: c) #define piForeach(i, c) for(i : c)
//! \brief //! \brief
//! \~english Macro for iterate any container //! \~english Macro for iterate any container
@@ -146,7 +136,7 @@ _PIReverseWrapper<C> PIReverseWrap(const C & c) {
//! \~russian Перебор всех элементов контейнера с доступом только на чтение. //! \~russian Перебор всех элементов контейнера с доступом только на чтение.
//! Перебор осуществляется в прямом порядке. //! Перебор осуществляется в прямом порядке.
//! \~ \sa \a piForeach, \a piForeachR, \a piForeachRC //! \~ \sa \a piForeach, \a piForeachR, \a piForeachRC
#define piForeachC(i, c) for (const i: c) #define piForeachC(i, c) for(const i : c)
//! \brief //! \brief
//! \~english Macro for iterate any container //! \~english Macro for iterate any container
@@ -162,7 +152,7 @@ _PIReverseWrapper<C> PIReverseWrap(const C & c) {
//! \~russian Перебор всех элементов контейнера с доступом на чтение и запись. //! \~russian Перебор всех элементов контейнера с доступом на чтение и запись.
//! Перебор осуществляется в обратном порядке. //! Перебор осуществляется в обратном порядке.
//! \~ \sa \a piForeach, \a piForeachC, \a piForeachRC //! \~ \sa \a piForeach, \a piForeachC, \a piForeachRC
#define piForeachR(i, c) for (i: PIReverseWrap(c)) #define piForeachR(i, c) for(i : PIReverseWrap(c))
//! \brief //! \brief
//! \~english Macro for iterate any container //! \~english Macro for iterate any container
@@ -178,8 +168,8 @@ _PIReverseWrapper<C> PIReverseWrap(const C & c) {
//! \~russian Перебор всех элементов контейнера с доступом только на чтение. //! \~russian Перебор всех элементов контейнера с доступом только на чтение.
//! Перебор осуществляется в обратном порядке. Также можно писать **piForeachCR** //! Перебор осуществляется в обратном порядке. Также можно писать **piForeachCR**
//! \~ \sa \a piForeach, \a piForeachC, \a piForeachR //! \~ \sa \a piForeach, \a piForeachC, \a piForeachR
#define piForeachRC(i, c) for (const i: PIReverseWrap(c)) #define piForeachRC(i, c) for(const i : PIReverseWrap(c))
#define piForeachCR piForeachRC #define piForeachCR piForeachRC
//! \~\brief //! \~\brief
@@ -187,10 +177,8 @@ _PIReverseWrapper<C> PIReverseWrap(const C & c) {
//! \~russian Порядок обхода для функции изменения размерности reshape(). //! \~russian Порядок обхода для функции изменения размерности reshape().
//! \~ \sa \a PIVector::reshape(), \a PIDeque::reshape() //! \~ \sa \a PIVector::reshape(), \a PIDeque::reshape()
enum ReshapeOrder { enum ReshapeOrder {
ReshapeByRow /*! \~english Traversing elements by line, just as they stay in memory \~russian Обход элементов построчно, так же как они ReshapeByRow /*! \~english Traversing elements by line, just as they stay in memory \~russian Обход элементов построчно, так же как они находятся в памяти */,
находятся в памяти */ ReshapeByColumn /*! \~english Traversing elements by column \~russian Обход элементов по столбцам */,
,
ReshapeByColumn /*! \~english Traversing elements by column \~russian Обход элементов по столбцам */,
}; };
#endif // PICONTAINERS_H #endif // PICONTAINERS_H

View File

@@ -1,20 +1,20 @@
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Containers Containers
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
//! \defgroup Containers Containers //! \defgroup Containers Containers
//! \~\brief //! \~\brief
@@ -180,12 +180,12 @@
#ifndef PICONTAINERSMODULE_H #ifndef PICONTAINERSMODULE_H
#define PICONTAINERSMODULE_H #define PICONTAINERSMODULE_H
#include "pivector.h"
#include "pideque.h" #include "pideque.h"
#include "pimap.h" #include "pimap.h"
#include "piqueue.h" #include "piqueue.h"
#include "piset.h" #include "piset.h"
#include "pistack.h" #include "pistack.h"
#include "pivector.h"
#include "pivector2d.h" #include "pivector2d.h"

View File

@@ -13,22 +13,22 @@
//! Андрей Бычков work.a.b@yandex.ru; //! Андрей Бычков work.a.b@yandex.ru;
//! \~\} //! \~\}
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Dynamic array of any type Dynamic array of any type
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PIDEQUE_H #ifndef PIDEQUE_H
@@ -75,9 +75,9 @@
//! if the number of elements is known beforehand. //! if the number of elements is known beforehand.
//! //!
//! The complexity (efficiency) of common operations on PIDeque is as follows: //! The complexity (efficiency) of common operations on PIDeque is as follows:
//! - Random access - constant O(1) //! - Random access - constant 𝓞(1)
//! - Insertion or removal of elements at the end or begin - amortized constant O(1) //! - Insertion or removal of elements at the end or begin - amortized constant 𝓞(1)
//! - Insertion or removal of elements - linear in the distance to the end of the array O(n) //! - Insertion or removal of elements - linear in the distance to the end of the array 𝓞(n)
//! //!
//! \~russian //! \~russian
//! Элементы хранятся непрерывно, а значит доступны не только через итераторы, //! Элементы хранятся непрерывно, а значит доступны не только через итераторы,
@@ -109,29 +109,31 @@
//! если количество элементов известно заранее. //! если количество элементов известно заранее.
//! //!
//! Сложность (эффективность) обычных операций над PIDeque следующая: //! Сложность (эффективность) обычных операций над PIDeque следующая:
//! - Произвольный доступ — постоянная O(1) //! - Произвольный доступ — постоянная 𝓞(1)
//! - Вставка и удаление элементов в конце или начале — амортизированная постоянная O(1) //! - Вставка и удаление элементов в конце или начале — амортизированная постоянная 𝓞(1)
//! - Вставка и удаление элементов — линейная по расстоянию до конца массива O(n) //! - Вставка и удаление элементов — линейная по расстоянию до конца массива 𝓞(n)
//! //!
//! \~\sa \a PIVector, \a PIMap //! \~\sa \a PIVector, \a PIMap
template<typename T> template <typename T>
class PIDeque { class PIDeque {
public: public:
typedef bool (*CompareFunc)(const T &, const T &); typedef bool (*CompareFunc)(const T & , const T & );
typedef T value_type; typedef T value_type;
typedef T * pointer; typedef T* pointer;
typedef const T * const_pointer; typedef const T* const_pointer;
typedef T & reference; typedef T& reference;
typedef const T & const_reference; typedef const T& const_reference;
typedef size_t size_type; typedef size_t size_type;
//! \~english Constructs an empty array. //! \~english Constructs an empty array.
//! \~russian Создает пустой массив. //! \~russian Создает пустой массив.
inline PIDeque() { PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) } inline PIDeque(): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
}
//! \~english Copy constructor. //! \~english Copy constructor.
//! \~russian Копирующий конструктор. //! \~russian Копирующий конструктор.
inline PIDeque(const PIDeque<T> & other) { inline PIDeque(const PIDeque<T> & other): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
alloc_forward(other.pid_size); alloc_forward(other.pid_size);
newT(pid_data + pid_start, other.pid_data + other.pid_start, pid_size); newT(pid_data + pid_start, other.pid_data + other.pid_start, pid_size);
@@ -146,7 +148,7 @@ public:
//! PIDeque <int> v{1,2,3}; //! PIDeque <int> v{1,2,3};
//! piCout << v; // {1, 2, 3} //! piCout << v; // {1, 2, 3}
//! \endcode //! \endcode
inline PIDeque(std::initializer_list<T> init_list) { inline PIDeque(std::initializer_list<T> init_list): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
alloc_forward(init_list.size()); alloc_forward(init_list.size());
newT(pid_data, init_list.begin(), init_list.size()); newT(pid_data, init_list.begin(), init_list.size());
@@ -156,7 +158,7 @@ public:
//! This constructor reserve `size` and copy from `data` pointer. //! This constructor reserve `size` and copy from `data` pointer.
//! \~russian Создает массив из указателя на данные `data` и размер `size`. //! \~russian Создает массив из указателя на данные `data` и размер `size`.
//! То есть выделяет память для `size` элементов и копирует данные из указателя `data`. //! То есть выделяет память для `size` элементов и копирует данные из указателя `data`.
inline PIDeque(const T * data, size_t size) { inline PIDeque(const T * data, size_t size): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
alloc_forward(size); alloc_forward(size);
newT(pid_data + pid_start, data, pid_size); newT(pid_data + pid_start, data, pid_size);
@@ -164,9 +166,9 @@ public:
//! \~english Contructs array with size `size` filled elements `e`. //! \~english Contructs array with size `size` filled elements `e`.
//! \~russian Создает массив из `size` элементов заполненных `e`. //! \~russian Создает массив из `size` элементов заполненных `e`.
inline explicit PIDeque(size_t pid_size, const T & e = T()) { inline PIDeque(size_t pid_size, const T & e = T()): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
expand(pid_size, e); resize(pid_size, e);
} }
//! \~english Contructs array with size `size` and elements created by function `f(size_t i)`. //! \~english Contructs array with size `size` and elements created by function `f(size_t i)`.
@@ -182,34 +184,31 @@ public:
//! PIDeque <int> v(5, [](size_t i){return i*2;}); //! PIDeque <int> v(5, [](size_t i){return i*2;});
//! piCout << v; // {0, 2, 4, 6, 8} //! piCout << v; // {0, 2, 4, 6, 8}
//! \endcode //! \endcode
inline PIDeque(size_t piv_size, std::function<T(size_t i)> f) { inline PIDeque(size_t piv_size, std::function<T(size_t i)> f): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
expand(piv_size, f); resize(piv_size, f);
} }
//! \~english Move constructor. //! \~english Move constructor.
//! \~russian Перемещающий конструктор. //! \~russian Перемещающий конструктор.
inline PIDeque(PIDeque<T> && other) { inline PIDeque(PIDeque<T> && other): pid_data(other.pid_data), pid_size(other.pid_size), pid_rsize(other.pid_rsize), pid_start(other.pid_start) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
pid_data = other.pid_data;
pid_size = other.pid_size;
pid_rsize = other.pid_rsize;
pid_start = other.pid_start;
other._reset(); other._reset();
} }
inline ~PIDeque() { inline virtual ~PIDeque() {
PIINTROSPECTION_CONTAINER_DELETE(T) PIINTROSPECTION_CONTAINER_DELETE(T)
PIINTROSPECTION_CONTAINER_FREE(T, (pid_rsize)) PIINTROSPECTION_CONTAINER_FREE(T, (pid_rsize))
deleteT(pid_data + pid_start, pid_size); deleteT(pid_data + pid_start, pid_size);
dealloc(); dealloc();
_reset();
} }
//! \~english Assign operator. //! \~english Assign operator.
//! \~russian Оператор присваивания. //! \~russian Оператор присваивания.
inline PIDeque<T> & operator=(const PIDeque<T> & other) { inline PIDeque<T> & operator =(const PIDeque<T> & other) {
if (this == &other) return *this; if (this == &other) return *this;
clear(); deleteT(pid_data + pid_start, pid_size);
alloc_forward(other.pid_size); alloc_forward(other.pid_size);
newT(pid_data + pid_start, other.pid_data + other.pid_start, pid_size); newT(pid_data + pid_start, other.pid_data + other.pid_start, pid_size);
return *this; return *this;
@@ -217,330 +216,360 @@ public:
//! \~english Assign move operator. //! \~english Assign move operator.
//! \~russian Оператор перемещающего присваивания. //! \~russian Оператор перемещающего присваивания.
inline PIDeque<T> & operator=(PIDeque<T> && other) { inline PIDeque<T> & operator =(PIDeque<T> && other) {
swap(other); swap(other);
return *this; return *this;
} }
class iterator { class iterator {
friend class PIDeque<T>; friend class PIDeque<T>;
private: private:
inline iterator(PIDeque<T> * v, ssize_t p): parent(v), pos(p) {} inline iterator(PIDeque<T> * v, ssize_t p): parent(v), pos(p) {}
PIDeque<T> * parent; PIDeque<T> * parent;
ssize_t pos; ssize_t pos;
public: public:
typedef T value_type; typedef T value_type;
typedef T * pointer; typedef T* pointer;
typedef T & reference; typedef T& reference;
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
typedef std::random_access_iterator_tag iterator_category; typedef std::random_access_iterator_tag iterator_category;
inline iterator(): parent(0), pos(0) {} inline iterator(): parent(0), pos(0) {}
inline T & operator*() { return (*parent)[pos]; } inline T & operator *() {return (*parent)[pos];}
inline const T & operator*() const { return (*parent)[pos]; } inline const T & operator *() const {return (*parent)[pos];}
inline T & operator->() { return (*parent)[pos]; } inline T & operator ->() {return (*parent)[pos];}
inline const T & operator->() const { return (*parent)[pos]; } inline const T & operator ->() const {return (*parent)[pos];}
inline iterator & operator++() { inline iterator & operator ++() {
++pos; ++pos;
return *this; return *this;
} }
inline iterator operator++(int) { inline iterator operator ++(int) {
const auto tmp = *this; auto tmp = *this;
++*this; ++*this;
return tmp; return tmp;
} }
inline iterator & operator--() { inline iterator & operator --() {
--pos; --pos;
return *this; return *this;
} }
inline iterator operator--(int) { inline iterator operator --(int) {
const auto tmp = *this; auto tmp = *this;
--*this; --*this;
return tmp; return tmp;
} }
inline iterator & operator+=(const iterator & it) { inline iterator & operator +=(const iterator & it) {
pos += it.pos; pos += it.pos;
return *this; return *this;
} }
inline iterator & operator+=(size_t p) { inline iterator & operator +=(size_t p) {
pos += p; pos += p;
return *this; return *this;
} }
inline iterator & operator-=(const iterator & it) { inline iterator & operator -=(const iterator & it) {
pos -= it.pos; pos -= it.pos;
return *this; return *this;
} }
inline iterator & operator-=(size_t p) { inline iterator & operator -=(size_t p) {
pos -= p; pos -= p;
return *this; return *this;
} }
friend inline iterator operator-(size_t p, const iterator & it) { return it - p; } friend inline iterator operator -(size_t p, const iterator & it) {return it - p;}
friend inline iterator operator-(const iterator & it, size_t p) { friend inline iterator operator -(const iterator & it, size_t p) {
auto tmp = it; auto tmp = it;
tmp -= p; tmp -= p;
return tmp; return tmp;
} }
friend inline std::ptrdiff_t operator-(const iterator & it1, const iterator & it2) { return it1.pos - it2.pos; } friend inline std::ptrdiff_t operator -(const iterator & it1, const iterator & it2) {
return it1.pos - it2.pos;
}
friend inline iterator operator+(size_t p, const iterator & it) { return it + p; } friend inline iterator operator +(size_t p, const iterator & it) {return it + p;}
friend inline iterator operator+(const iterator & it, size_t p) { friend inline iterator operator +(const iterator & it, size_t p) {
auto tmp = it; auto tmp = it;
tmp += p; tmp += p;
return tmp; return tmp;
} }
inline bool operator==(const iterator & it) const { return (pos == it.pos); } inline bool operator ==(const iterator & it) const {return (pos == it.pos);}
inline bool operator!=(const iterator & it) const { return (pos != it.pos); } inline bool operator !=(const iterator & it) const {return (pos != it.pos);}
friend inline bool operator<(const iterator & it1, const iterator & it2) { return it1.pos < it2.pos; } friend inline bool operator <(const iterator & it1, const iterator & it2) {
friend inline bool operator<=(const iterator & it1, const iterator & it2) { return it1.pos <= it2.pos; } return it1.pos < it2.pos;
friend inline bool operator>(const iterator & it1, const iterator & it2) { return it1.pos > it2.pos; } }
friend inline bool operator>=(const iterator & it1, const iterator & it2) { return it1.pos >= it2.pos; } friend inline bool operator <=(const iterator & it1, const iterator & it2) {
return it1.pos <= it2.pos;
}
friend inline bool operator >(const iterator & it1, const iterator & it2) {
return it1.pos > it2.pos;
}
friend inline bool operator >=(const iterator & it1, const iterator & it2) {
return it1.pos >= it2.pos;
}
}; };
class const_iterator { class const_iterator {
friend class PIDeque<T>; friend class PIDeque<T>;
private: private:
inline const_iterator(const PIDeque<T> * v, ssize_t p): parent(v), pos(p) {} inline const_iterator(const PIDeque<T> * v, ssize_t p): parent(v), pos(p) {}
const PIDeque<T> * parent; const PIDeque<T> * parent;
ssize_t pos; ssize_t pos;
public: public:
typedef T value_type; typedef T value_type;
typedef T * pointer; typedef T* pointer;
typedef T & reference; typedef T& reference;
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
typedef std::random_access_iterator_tag iterator_category; typedef std::random_access_iterator_tag iterator_category;
inline const_iterator(): parent(0), pos(0) {} inline const_iterator(): parent(0), pos(0) {}
inline const T & operator*() const { return (*parent)[pos]; } inline const T & operator *() const {return (*parent)[pos];}
inline const T & operator->() const { return (*parent)[pos]; } inline const T & operator ->() const {return (*parent)[pos];}
inline const_iterator & operator++() { inline const_iterator & operator ++() {
++pos; ++pos;
return *this; return *this;
} }
inline const_iterator operator++(int) { inline const_iterator operator ++(int) {
const auto tmp = *this; auto tmp = *this;
++*this; ++*this;
return tmp; return tmp;
} }
inline const_iterator & operator--() { inline const_iterator & operator --() {
--pos; --pos;
return *this; return *this;
} }
inline const_iterator operator--(int) { inline const_iterator operator --(int) {
const auto tmp = *this; auto tmp = *this;
--*this; --*this;
return tmp; return tmp;
} }
inline const_iterator & operator+=(const const_iterator & it) { inline const_iterator & operator +=(const const_iterator & it) {
pos += it.pos; pos += it.pos;
return *this; return *this;
} }
inline const_iterator & operator+=(size_t p) { inline const_iterator & operator +=(size_t p) {
pos += p; pos += p;
return *this; return *this;
} }
inline const_iterator & operator-=(const const_iterator & it) { inline const_iterator & operator -=(const const_iterator & it) {
pos -= it.pos; pos -= it.pos;
return *this; return *this;
} }
inline const_iterator & operator-=(size_t p) { inline const_iterator & operator -=(size_t p) {
pos -= p; pos -= p;
return *this; return *this;
} }
friend inline const_iterator operator-(size_t p, const const_iterator & it) { return it - p; } friend inline const_iterator operator -(size_t p, const const_iterator & it) {return it - p;}
friend inline const_iterator operator-(const const_iterator & it, size_t p) { friend inline const_iterator operator -(const const_iterator & it, size_t p) {
auto tmp = it; auto tmp = it;
tmp -= p; tmp -= p;
return tmp; return tmp;
} }
friend inline std::ptrdiff_t operator-(const const_iterator & it1, const const_iterator & it2) { return it1.pos - it2.pos; } friend inline std::ptrdiff_t operator -(const const_iterator & it1, const const_iterator & it2) {
return it1.pos - it2.pos;
}
friend inline const_iterator operator+(size_t p, const const_iterator & it) { return it + p; } friend inline const_iterator operator +(size_t p, const const_iterator & it) {return it + p;}
friend inline const_iterator operator+(const const_iterator & it, size_t p) { friend inline const_iterator operator +(const const_iterator & it, size_t p) {
auto tmp = it; auto tmp = it;
tmp += p; tmp += p;
return tmp; return tmp;
} }
inline bool operator==(const const_iterator & it) const { return (pos == it.pos); } inline bool operator ==(const const_iterator & it) const {return (pos == it.pos);}
inline bool operator!=(const const_iterator & it) const { return (pos != it.pos); } inline bool operator !=(const const_iterator & it) const {return (pos != it.pos);}
friend inline bool operator<(const const_iterator & it1, const const_iterator & it2) { return it1.pos < it2.pos; } friend inline bool operator <(const const_iterator & it1, const const_iterator & it2) {
friend inline bool operator<=(const const_iterator & it1, const const_iterator & it2) { return it1.pos <= it2.pos; } return it1.pos < it2.pos;
friend inline bool operator>(const const_iterator & it1, const const_iterator & it2) { return it1.pos > it2.pos; } }
friend inline bool operator>=(const const_iterator & it1, const const_iterator & it2) { return it1.pos >= it2.pos; } friend inline bool operator <=(const const_iterator & it1, const const_iterator & it2) {
return it1.pos <= it2.pos;
}
friend inline bool operator >(const const_iterator & it1, const const_iterator & it2) {
return it1.pos > it2.pos;
}
friend inline bool operator >=(const const_iterator & it1, const const_iterator & it2) {
return it1.pos >= it2.pos;
}
}; };
class reverse_iterator { class reverse_iterator {
friend class PIDeque<T>; friend class PIDeque<T>;
private: private:
inline reverse_iterator(PIDeque<T> * v, ssize_t p): parent(v), pos(p) {} inline reverse_iterator(PIDeque<T> * v, ssize_t p): parent(v), pos(p) {}
PIDeque<T> * parent; PIDeque<T> * parent;
ssize_t pos; ssize_t pos;
public: public:
typedef T value_type; typedef T value_type;
typedef T * pointer; typedef T* pointer;
typedef T & reference; typedef T& reference;
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
typedef std::random_access_iterator_tag iterator_category; typedef std::random_access_iterator_tag iterator_category;
inline reverse_iterator(): parent(0), pos(0) {} inline reverse_iterator(): parent(0), pos(0) {}
inline T & operator*() { return (*parent)[pos]; } inline T & operator *() {return (*parent)[pos];}
inline const T & operator*() const { return (*parent)[pos]; } inline const T & operator *() const {return (*parent)[pos];}
inline T & operator->() { return (*parent)[pos]; } inline T & operator ->() {return (*parent)[pos];}
inline const T & operator->() const { return (*parent)[pos]; } inline const T & operator ->() const {return (*parent)[pos];}
inline reverse_iterator & operator++() { inline reverse_iterator & operator ++() {
--pos; --pos;
return *this; return *this;
} }
inline reverse_iterator operator++(int) { inline reverse_iterator operator ++(int) {
const auto tmp = *this; auto tmp = *this;
--*this; --*this;
return tmp; return tmp;
} }
inline reverse_iterator & operator--() { inline reverse_iterator & operator --() {
++pos; ++pos;
return *this; return *this;
} }
inline reverse_iterator operator--(int) { inline reverse_iterator operator --(int) {
const auto tmp = *this; auto tmp = *this;
++*this; ++*this;
return tmp; return tmp;
} }
inline reverse_iterator & operator+=(const reverse_iterator & it) { inline reverse_iterator & operator +=(const reverse_iterator & it) {
pos -= it.pos; pos -= it.pos;
return *this; return *this;
} }
inline reverse_iterator & operator+=(size_t p) { inline reverse_iterator & operator +=(size_t p) {
pos -= p; pos -= p;
return *this; return *this;
} }
inline reverse_iterator & operator-=(const reverse_iterator & it) { inline reverse_iterator & operator -=(const reverse_iterator & it) {
pos += it.pos; pos += it.pos;
return *this; return *this;
} }
inline reverse_iterator & operator-=(size_t p) { inline reverse_iterator & operator -=(size_t p) {
pos += p; pos += p;
return *this; return *this;
} }
friend inline reverse_iterator operator-(size_t p, const reverse_iterator & it) { return it - p; } friend inline reverse_iterator operator -(size_t p, const reverse_iterator & it) {return it - p;}
friend inline reverse_iterator operator-(const reverse_iterator & it, size_t p) { friend inline reverse_iterator operator -(const reverse_iterator & it, size_t p) {
auto tmp = it; auto tmp = it;
tmp -= p; tmp -= p;
return tmp; return tmp;
} }
friend inline std::ptrdiff_t operator-(const reverse_iterator & it1, const reverse_iterator & it2) { return it2.pos - it1.pos; } friend inline std::ptrdiff_t operator -(const reverse_iterator & it1, const reverse_iterator & it2) {
return it2.pos - it1.pos;
}
friend inline reverse_iterator operator+(size_t p, const reverse_iterator & it) { return it + p; } friend inline reverse_iterator operator +(size_t p, const reverse_iterator & it) {return it + p;}
friend inline reverse_iterator operator+(const reverse_iterator & it, size_t p) { friend inline reverse_iterator operator +(const reverse_iterator & it, size_t p) {
auto tmp = it; auto tmp = it;
tmp += p; tmp += p;
return tmp; return tmp;
} }
inline bool operator==(const reverse_iterator & it) const { return (pos == it.pos); } inline bool operator ==(const reverse_iterator & it) const {return (pos == it.pos);}
inline bool operator!=(const reverse_iterator & it) const { return (pos != it.pos); } inline bool operator !=(const reverse_iterator & it) const {return (pos != it.pos);}
friend inline bool operator<(const reverse_iterator & it1, const reverse_iterator & it2) { return it1.pos < it2.pos; } friend inline bool operator <(const reverse_iterator & it1, const reverse_iterator & it2) {
friend inline bool operator<=(const reverse_iterator & it1, const reverse_iterator & it2) { return it1.pos <= it2.pos; } return it1.pos < it2.pos;
friend inline bool operator>(const reverse_iterator & it1, const reverse_iterator & it2) { return it1.pos > it2.pos; } }
friend inline bool operator>=(const reverse_iterator & it1, const reverse_iterator & it2) { return it1.pos >= it2.pos; } friend inline bool operator <=(const reverse_iterator & it1, const reverse_iterator & it2) {
return it1.pos <= it2.pos;
}
friend inline bool operator >(const reverse_iterator & it1, const reverse_iterator & it2) {
return it1.pos > it2.pos;
}
friend inline bool operator >=(const reverse_iterator & it1, const reverse_iterator & it2) {
return it1.pos >= it2.pos;
}
}; };
class const_reverse_iterator { class const_reverse_iterator {
friend class PIDeque<T>; friend class PIDeque<T>;
private: private:
inline const_reverse_iterator(const PIDeque<T> * v, ssize_t p): parent(v), pos(p) {} inline const_reverse_iterator(const PIDeque<T> * v, ssize_t p): parent(v), pos(p) {}
const PIDeque<T> * parent; const PIDeque<T> * parent;
ssize_t pos; ssize_t pos;
public: public:
typedef T value_type; typedef T value_type;
typedef T * pointer; typedef T* pointer;
typedef T & reference; typedef T& reference;
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
typedef std::random_access_iterator_tag iterator_category; typedef std::random_access_iterator_tag iterator_category;
inline const_reverse_iterator(): parent(0), pos(0) {} inline const_reverse_iterator(): parent(0), pos(0) {}
inline const T & operator*() const { return (*parent)[pos]; } inline const T & operator *() const {return (*parent)[pos];}
inline const T & operator->() const { return (*parent)[pos]; } inline const T & operator ->() const {return (*parent)[pos];}
inline const_reverse_iterator & operator++() { inline const_reverse_iterator & operator ++() {
--pos; --pos;
return *this; return *this;
} }
inline const_reverse_iterator operator++(int) { inline const_reverse_iterator operator ++(int) {
const auto tmp = *this; auto tmp = *this;
--*this; --*this;
return tmp; return tmp;
} }
inline const_reverse_iterator & operator--() { inline const_reverse_iterator & operator --() {
++pos; ++pos;
return *this; return *this;
} }
inline const_reverse_iterator operator--(int) { inline const_reverse_iterator operator --(int) {
const auto tmp = *this; auto tmp = *this;
++*this; ++*this;
return tmp; return tmp;
} }
inline const_reverse_iterator & operator+=(const const_reverse_iterator & it) { inline const_reverse_iterator & operator +=(const const_reverse_iterator & it) {
pos -= it.pos; pos -= it.pos;
return *this; return *this;
} }
inline const_reverse_iterator & operator+=(size_t p) { inline const_reverse_iterator & operator +=(size_t p) {
pos -= p; pos -= p;
return *this; return *this;
} }
inline const_reverse_iterator & operator-=(const const_reverse_iterator & it) { inline const_reverse_iterator & operator -=(const const_reverse_iterator & it) {
pos += it.pos; pos += it.pos;
return *this; return *this;
} }
inline const_reverse_iterator & operator-=(size_t p) { inline const_reverse_iterator & operator -=(size_t p) {
pos += p; pos += p;
return *this; return *this;
} }
friend inline const_reverse_iterator operator-(size_t p, const const_reverse_iterator & it) { return it - p; } friend inline const_reverse_iterator operator -(size_t p, const const_reverse_iterator & it) {return it - p;}
friend inline const_reverse_iterator operator-(const const_reverse_iterator & it, size_t p) { friend inline const_reverse_iterator operator -(const const_reverse_iterator & it, size_t p) {
auto tmp = it; auto tmp = it;
tmp -= p; tmp -= p;
return tmp; return tmp;
} }
friend inline std::ptrdiff_t operator-(const const_reverse_iterator & it1, const const_reverse_iterator & it2) { friend inline std::ptrdiff_t operator -(const const_reverse_iterator & it1, const const_reverse_iterator & it2) {
return it2.pos - it1.pos; return it2.pos - it1.pos;
} }
friend inline const_reverse_iterator operator+(size_t p, const const_reverse_iterator & it) { return it + p; } friend inline const_reverse_iterator operator +(size_t p, const const_reverse_iterator & it) {return it + p;}
friend inline const_reverse_iterator operator+(const const_reverse_iterator & it, size_t p) { friend inline const_reverse_iterator operator +(const const_reverse_iterator & it, size_t p) {
auto tmp = it; auto tmp = it;
tmp += p; tmp += p;
return tmp; return tmp;
} }
inline bool operator==(const const_reverse_iterator & it) const { return (pos == it.pos); } inline bool operator ==(const const_reverse_iterator & it) const {return (pos == it.pos);}
inline bool operator!=(const const_reverse_iterator & it) const { return (pos != it.pos); } inline bool operator !=(const const_reverse_iterator & it) const {return (pos != it.pos);}
friend inline bool operator<(const const_reverse_iterator & it1, const const_reverse_iterator & it2) { return it1.pos < it2.pos; } friend inline bool operator <(const const_reverse_iterator & it1, const const_reverse_iterator & it2) {
friend inline bool operator<=(const const_reverse_iterator & it1, const const_reverse_iterator & it2) { return it1.pos <= it2.pos; } return it1.pos < it2.pos;
friend inline bool operator>(const const_reverse_iterator & it1, const const_reverse_iterator & it2) { return it1.pos > it2.pos; } }
friend inline bool operator>=(const const_reverse_iterator & it1, const const_reverse_iterator & it2) { return it1.pos >= it2.pos; } friend inline bool operator <=(const const_reverse_iterator & it1, const const_reverse_iterator & it2) {
return it1.pos <= it2.pos;
}
friend inline bool operator >(const const_reverse_iterator & it1, const const_reverse_iterator & it2) {
return it1.pos > it2.pos;
}
friend inline bool operator >=(const const_reverse_iterator & it1, const const_reverse_iterator & it2) {
return it1.pos >= it2.pos;
}
}; };
@@ -552,7 +581,7 @@ public:
//! \~russian Если массив - пуст, возвращаемый итератор будет равен \a end(). //! \~russian Если массив - пуст, возвращаемый итератор будет равен \a end().
//! \~\return \ref stl_iterators //! \~\return \ref stl_iterators
//! \~\sa \a end(), \a rbegin(), \a rend() //! \~\sa \a end(), \a rbegin(), \a rend()
inline iterator begin() { return iterator(this, 0); } inline iterator begin() {return iterator(this, 0);}
//! \~english Iterator to the element following the last element. //! \~english Iterator to the element following the last element.
//! \~russian Итератор на элемент, следующий за последним элементом. //! \~russian Итератор на элемент, следующий за последним элементом.
@@ -564,10 +593,10 @@ public:
//! попытка доступа к нему приведёт к выходу за разрешенную память. //! попытка доступа к нему приведёт к выходу за разрешенную память.
//! \~\return \ref stl_iterators //! \~\return \ref stl_iterators
//! \~\sa \a begin(), \a rbegin(), \a rend() //! \~\sa \a begin(), \a rbegin(), \a rend()
inline iterator end() { return iterator(this, pid_size); } inline iterator end() {return iterator(this, pid_size);}
inline const_iterator begin() const { return const_iterator(this, 0); } inline const_iterator begin() const {return const_iterator(this, 0); }
inline const_iterator end() const { return const_iterator(this, pid_size); } inline const_iterator end() const {return const_iterator(this, pid_size);}
//! \~english Returns a reverse iterator to the first element of the reversed array. //! \~english Returns a reverse iterator to the first element of the reversed array.
//! \~russian Обратный итератор на первый элемент. //! \~russian Обратный итератор на первый элемент.
@@ -580,12 +609,11 @@ public:
//! Если массив пустой, то совпадает с итератором \a rend(). //! Если массив пустой, то совпадает с итератором \a rend().
//! \~\return \ref stl_iterators //! \~\return \ref stl_iterators
//! \~\sa \a rend(), \a begin(), \a end() //! \~\sa \a rend(), \a begin(), \a end()
inline reverse_iterator rbegin() { return reverse_iterator(this, pid_size - 1); } inline reverse_iterator rbegin() {return reverse_iterator(this, pid_size - 1);}
//! \~english Returns a reverse iterator to the element. //! \~english Returns a reverse iterator to the element.
//! following the last element of the reversed array. //! following the last element of the reversed array.
//! \~russian Обратный итератор на элемент, //! \~russian Обратный итератор на элемент, следующий за последним элементом.
//! следующий за последним элементом.
//! \~\details ![rbegin, rend](doc/images/pivector_rbegin.png) //! \~\details ![rbegin, rend](doc/images/pivector_rbegin.png)
//! //!
//! \~english It corresponds to the element preceding the first element of the non-reversed array. //! \~english It corresponds to the element preceding the first element of the non-reversed array.
@@ -596,51 +624,51 @@ public:
//! попытка доступа к нему приведёт к выходу за разрешенную память. //! попытка доступа к нему приведёт к выходу за разрешенную память.
//! \~\return \ref stl_iterators //! \~\return \ref stl_iterators
//! \~\sa \a rbegin(), \a begin(), \a end() //! \~\sa \a rbegin(), \a begin(), \a end()
inline reverse_iterator rend() { return reverse_iterator(this, -1); } inline reverse_iterator rend() {return reverse_iterator(this, -1);}
inline const_reverse_iterator rbegin() const { return const_reverse_iterator(this, pid_size - 1); } inline const_reverse_iterator rbegin() const {return const_reverse_iterator(this, pid_size - 1);}
inline const_reverse_iterator rend() const { return const_reverse_iterator(this, -1); } inline const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);}
//! \~english Number of elements in the container. //! \~english Number of elements in the container.
//! \~russian Количество элементов массива. //! \~russian Количество элементов массива.
//! \~\sa \a size_s(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve() //! \~\sa \a size_s(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline size_t size() const { return pid_size; } inline size_t size() const {return pid_size;}
//! \~english Number of elements in the container as signed value. //! \~english Number of elements in the container as signed value.
//! \~russian Количество элементов массива в виде знакового числа. //! \~russian Количество элементов массива в виде знакового числа.
//! \~\sa \a size(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve() //! \~\sa \a size(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline ssize_t size_s() const { return pid_size; } inline ssize_t size_s() const {return pid_size;}
//! \~english Same as \a size(). //! \~english Same as \a size().
//! \~russian Синоним \a size(). //! \~russian Синоним \a size().
//! \~\sa \a size(), \a size_s(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve() //! \~\sa \a size(), \a size_s(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline size_t length() const { return pid_size; } inline size_t length() const {return pid_size;}
//! \~english Number of elements that the container has currently allocated space for. //! \~english Number of elements that the container has currently allocated space for.
//! \~russian Количество элементов, для которого сейчас выделена память массивом. //! \~russian Количество элементов, для которого сейчас выделена память контейнером.
//! \~\details //! \~\details
//! \~english To find out the actual number of items, use the function \a size(). //! \~english To find out the actual number of items, use the function \a size().
//! \~russian Чтобы узнать фактическое количество элементов используйте функцию \a size(). //! \~russian Чтобы узнать фактическое количество элементов используйте функцию \a size().
//! \~\sa \a reserve(), \a size(), \a size_s() //! \~\sa \a reserve(), \a size(), \a size_s()
inline size_t capacity() const { return pid_rsize; } inline size_t capacity() const {return pid_rsize;}
inline size_t _start() const { return pid_start; } inline size_t _start() const {return pid_start;}
//! \~english Checks if the container has no elements. //! \~english Checks if the container has no elements.
//! \~russian Проверяет пуст ли массив. //! \~russian Проверяет пуст ли контейнер.
//! \~\return //! \~\return
//! \~english **true** if the container is empty, **false** otherwise //! \~english **true** if the container is empty, **false** otherwise
//! \~russian **true** если массив пуст, **false** иначе. //! \~russian **true** если контейнер пуст, **false** иначе.
//! \~\sa \a size(), \a size_s(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve() //! \~\sa \a size(), \a size_s(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline bool isEmpty() const { return (pid_size == 0); } inline bool isEmpty() const {return (pid_size == 0);}
//! \~english Checks if the container has elements. //! \~english Checks if the container has elements.
//! \~russian Проверяет не пуст ли массив. //! \~russian Проверяет не пуст ли контейнер.
//! \~\return //! \~\return
//! \~english **true** if the container is not empty, **false** otherwise //! \~english **true** if the container is not empty, **false** otherwise
//! \~russian **true** если массив не пуст, **false** иначе. //! \~russian **true** если контейнер не пуст, **false** иначе.
//! \~\sa \a size(), \a size_s(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve() //! \~\sa \a size(), \a size_s(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline bool isNotEmpty() const { return (pid_size > 0); } inline bool isNotEmpty() const {return (pid_size > 0);}
//! \~english Tests whether at least one element in the array //! \~english Tests whether at least one element in the array
//! passes the test implemented by the provided function `test`. //! passes the test implemented by the provided function `test`.
@@ -661,7 +689,7 @@ public:
//! \endcode //! \endcode
//! \~\sa \a every(), \a contains(), \a entries(), \a forEach() //! \~\sa \a every(), \a contains(), \a entries(), \a forEach()
inline bool any(std::function<bool(const T & e)> test) const { inline bool any(std::function<bool(const T & e)> test) const {
for (size_t i = pid_start; i < pid_start + pid_size; ++i) { for (ssize_t i = pid_start; i < pid_start + (ssize_t)pid_size; ++i) {
if (test(pid_data[i])) return true; if (test(pid_data[i])) return true;
} }
return false; return false;
@@ -686,7 +714,7 @@ public:
//! \endcode //! \endcode
//! \~\sa \a any(), \a contains(), \a entries(), \a forEach() //! \~\sa \a any(), \a contains(), \a entries(), \a forEach()
inline bool every(std::function<bool(const T & e)> test) const { inline bool every(std::function<bool(const T & e)> test) const {
for (size_t i = pid_start; i < pid_start + pid_size; ++i) { for (ssize_t i = pid_start; i < pid_start + (ssize_t)pid_size; ++i) {
if (!test(pid_data[i])) return false; if (!test(pid_data[i])) return false;
} }
return true; return true;
@@ -708,8 +736,8 @@ public:
//! piCout << v; // {1, 2, 5, 9} //! piCout << v; // {1, 2, 5, 9}
//! \endcode //! \endcode
//! \~\sa \a at() //! \~\sa \a at()
inline T & operator[](size_t index) { return pid_data[pid_start + index]; } inline T & operator [](size_t index) {return pid_data[pid_start + index];}
inline const T & operator[](size_t index) const { return pid_data[pid_start + index]; } inline const T & operator [](size_t index) const {return pid_data[pid_start + index];}
//! \~english Read only access to element by `index`. //! \~english Read only access to element by `index`.
//! \~russian Доступ исключительно на чтение к элементу по индексу `index`. //! \~russian Доступ исключительно на чтение к элементу по индексу `index`.
@@ -720,35 +748,7 @@ public:
//! \~russian Индекс элемента считается от `0`. //! \~russian Индекс элемента считается от `0`.
//! Индекс элемента должен лежать в пределах от `0` до `size()-1`. //! Индекс элемента должен лежать в пределах от `0` до `size()-1`.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти. //! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
inline const T & at(size_t index) const { return pid_data[pid_start + index]; } inline const T & at(size_t index) const {return pid_data[pid_start + index];}
//! \~english Returns the first element of the array that
//! passes the test implemented by the provided function `test`
//! or `def` if there is no such element.
//! \~russian Возвращает первый элемент массива, проходящего по условию,
//! заданному в передаваемой функции `test`, или `def` если такого элемента нет.
//! \~\sa \a indexWhere()
inline const T & atWhere(std::function<bool(const T & e)> test, ssize_t start = 0, const T & def = T()) const {
const ssize_t i = indexWhere(test, start);
if (i < 0)
return def;
else
return at(i);
}
//! \~english Returns the last element of the array that
//! passes the test implemented by the provided function `test`
//! or `def` if there is no such element.
//! \~russian Возвращает последний элемент массива, проходящего по условию,
//! заданному в передаваемой функции `test`, или `def` если такого элемента нет.
//! \~\sa \a lastIndexWhere()
inline const T & lastAtWhere(std::function<bool(const T & e)> test, ssize_t start = -1, const T & def = T()) const {
const ssize_t i = lastIndexWhere(test, start);
if (i < 0)
return def;
else
return at(i);
}
//! \~english Last element. //! \~english Last element.
//! \~russian Последний элемент массива. //! \~russian Последний элемент массива.
@@ -759,8 +759,8 @@ public:
//! \~russian Возвращает ссылку на последний элемент в массиве. //! \~russian Возвращает ссылку на последний элемент в массиве.
//! Эта функция предполагает, что массив не пустой. //! Эта функция предполагает, что массив не пустой.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти. //! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
inline T & back() { return pid_data[pid_start + pid_size - 1]; } inline T & back() {return pid_data[pid_start + pid_size - 1];}
inline const T & back() const { return pid_data[pid_start + pid_size - 1]; } inline const T & back() const {return pid_data[pid_start + pid_size - 1];}
//! \~english Last element. //! \~english Last element.
//! \~russian Первый элемент массива. //! \~russian Первый элемент массива.
@@ -771,12 +771,12 @@ public:
//! \~russian Возвращает ссылку на пенрвый элемент в массиве. //! \~russian Возвращает ссылку на пенрвый элемент в массиве.
//! Эта функция предполагает, что массив не пустой. //! Эта функция предполагает, что массив не пустой.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти. //! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
inline T & front() { return pid_data[pid_start]; } inline T & front() {return pid_data[pid_start];}
inline const T & front() const { return pid_data[pid_start]; } inline const T & front() const {return pid_data[pid_start];}
//! \~english Compare operator with array `v`. //! \~english Compare operator with array `v`.
//! \~russian Оператор сравнения с массивом `v`. //! \~russian Оператор сравнения с массивом `v`.
inline bool operator==(const PIDeque<T> & v) const { inline bool operator ==(const PIDeque<T> & v) const {
if (pid_size != v.pid_size) return false; if (pid_size != v.pid_size) return false;
for (size_t i = 0; i < pid_size; ++i) { for (size_t i = 0; i < pid_size; ++i) {
if (v[i] != (*this)[i]) return false; if (v[i] != (*this)[i]) return false;
@@ -786,7 +786,7 @@ public:
//! \~english Compare operator with array `v`. //! \~english Compare operator with array `v`.
//! \~russian Оператор сравнения с массивом `v`. //! \~russian Оператор сравнения с массивом `v`.
inline bool operator!=(const PIDeque<T> & v) const { return !(*this == v); } inline bool operator !=(const PIDeque<T> & v) const {return !(*this == v);}
//! \~english Tests if element `e` exists in the array. //! \~english Tests if element `e` exists in the array.
//! \~russian Проверяет наличие элемента `e` в массиве. //! \~russian Проверяет наличие элемента `e` в массиве.
@@ -804,7 +804,7 @@ public:
//! возвращается **false**, что означает, что массив даже не просматривается. //! возвращается **false**, что означает, что массив даже не просматривается.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива. //! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив. //! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу (слева на право). //! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу.
//! Значение по умолчанию равно 0, что означает, что просматривается весь массив. //! Значение по умолчанию равно 0, что означает, что просматривается весь массив.
//! \~\code //! \~\code
//! PIDeque<int> v{1, 2, 3, 4}; //! PIDeque<int> v{1, 2, 3, 4};
@@ -825,39 +825,12 @@ public:
start = pid_size + start; start = pid_size + start;
if (start < 0) start = 0; if (start < 0) start = 0;
} }
for (size_t i = pid_start + size_t(start); i < pid_start + pid_size; ++i) { for (ssize_t i = pid_start + start; i < pid_start + (ssize_t)pid_size; ++i) {
if (e == pid_data[i]) return true; if (e == pid_data[i]) return true;
} }
return false; return false;
} }
//! \~english Tests if all elements of `v` exists in the array.
//! \~russian Проверяет наличие всех элементов `v` в массиве.
//! \~\details
//! \~\code
//! PIDeque<int> v{1, 2, 3, 4};
//! 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 PIDeque<T> & v, ssize_t start = 0) const {
if (start < 0) {
start = pid_size + start;
if (start < 0) start = 0;
}
for (const T & e: v) {
bool c = false;
for (size_t i = pid_start + size_t(start); i < pid_start + pid_size; ++i) {
if (e == pid_data[i]) {
c = true;
break;
}
}
if (!c) return false;
}
return true;
}
//! \~english Count elements equal `e` in the array. //! \~english Count elements equal `e` in the array.
//! \~russian Подсчитывает количество элементов, совпадающих с элементом `e` в массиве. //! \~russian Подсчитывает количество элементов, совпадающих с элементом `e` в массиве.
//! \~\details //! \~\details
@@ -874,7 +847,7 @@ public:
//! возвращается 0, что означает, что массив даже не просматривается. //! возвращается 0, что означает, что массив даже не просматривается.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива. //! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив. //! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу (слева на право). //! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу.
//! Значение по умолчанию равно 0, что означает, что просматривается весь массив. //! Значение по умолчанию равно 0, что означает, что просматривается весь массив.
//! \~\code //! \~\code
//! PIDeque<int> v{2, 2, 4, 2, 6}; //! PIDeque<int> v{2, 2, 4, 2, 6};
@@ -883,13 +856,13 @@ public:
//! piCout << v.entries(2, -4); // 2 //! piCout << v.entries(2, -4); // 2
//! \endcode //! \endcode
//! \~\sa \a every(), \a any(), \a contains(), \a forEach(), \a indexOf() //! \~\sa \a every(), \a any(), \a contains(), \a forEach(), \a indexOf()
inline int entries(const T & e, ssize_t start = 0) const { inline int entries(const T & e, size_t start = 0) const {
int ec = 0; int ec = 0;
if (start < 0) { if (start < 0) {
start = pid_size + start; start = pid_size + start;
if (start < 0) start = 0; if (start < 0) start = 0;
} }
for (size_t i = pid_start + size_t(start); i < pid_start + pid_size; ++i) { for (ssize_t i = pid_start + start; i < pid_start + (ssize_t)pid_size; ++i) {
if (e == pid_data[i]) ++ec; if (e == pid_data[i]) ++ec;
} }
return ec; return ec;
@@ -914,16 +887,16 @@ public:
//! возвращается 0, что означает, что массив даже не просматривается. //! возвращается 0, что означает, что массив даже не просматривается.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива. //! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив. //! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу (слева на право). //! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу.
//! Значение по умолчанию равно 0, что означает, что просматривается весь массив. //! Значение по умолчанию равно 0, что означает, что просматривается весь массив.
//! \~\sa \a every(), \a any(), \a contains(), \a forEach(), \a indexWhere() //! \~\sa \a every(), \a any(), \a contains(), \a forEach(), \a indexWhere()
inline int entries(std::function<bool(const T & e)> test, ssize_t start = 0) const { inline int entries(std::function<bool(const T & e)> test, size_t start = 0) const {
int ec = 0; int ec = 0;
if (start < 0) { if (start < 0) {
start = pid_size + start; start = pid_size + start;
if (start < 0) start = 0; if (start < 0) start = 0;
} }
for (size_t i = pid_start + size_t(start); i < pid_start + pid_size; ++i) { for (ssize_t i = pid_start + start; i < pid_start + (ssize_t)pid_size; ++i) {
if (test(pid_data[i])) ++ec; if (test(pid_data[i])) ++ec;
} }
return ec; return ec;
@@ -947,7 +920,7 @@ public:
//! возвращается `-1`, что означает, что массив даже не просматривается. //! возвращается `-1`, что означает, что массив даже не просматривается.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива. //! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив. //! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу (слева на право). //! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу.
//! Значение по умолчанию равно 0, что означает, что просматривается весь массив. //! Значение по умолчанию равно 0, что означает, что просматривается весь массив.
//! \~\code //! \~\code
//! PIDeque<int> v{2, 5, 9}; //! PIDeque<int> v{2, 5, 9};
@@ -958,14 +931,14 @@ public:
//! piCout << v.indexOf(2, -3); // 0 //! piCout << v.indexOf(2, -3); // 0
//! \endcode //! \endcode
//! \~\sa \a indexWhere(), \a lastIndexOf(), \a lastIndexWhere(), \a contains() //! \~\sa \a indexWhere(), \a lastIndexOf(), \a lastIndexWhere(), \a contains()
inline ssize_t indexOf(const T & e, ssize_t start = 0) const { inline ssize_t indexOf(const T & e, size_t start = 0) const {
if (start < 0) { if (start < 0) {
start = pid_size + start; start = pid_size + start;
if (start < 0) start = 0; if (start < 0) start = 0;
} }
for (size_t i = pid_start + size_t(start); i < pid_start + pid_size; ++i) { for (ssize_t i = pid_start + start; i < pid_start + (ssize_t)pid_size; ++i) {
if (e == pid_data[i]) { if (e == pid_data[i]) {
return ssize_t(i) - pid_start; return i - pid_start;
} }
} }
return -1; return -1;
@@ -990,7 +963,7 @@ public:
//! возвращается `-1`, что означает, что массив даже не просматривается. //! возвращается `-1`, что означает, что массив даже не просматривается.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива. //! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив. //! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу (слева на право). //! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу.
//! Значение по умолчанию равно 0, что означает, что просматривается весь массив. //! Значение по умолчанию равно 0, что означает, что просматривается весь массив.
//! \~\code //! \~\code
//! PIDeque<PIString> v{"do", "re", "mi", "re"}; //! PIDeque<PIString> v{"do", "re", "mi", "re"};
@@ -999,14 +972,14 @@ public:
//! piCout << v.indexWhere([](const PIString & s){return s.startsWith('k');}); // -1 //! piCout << v.indexWhere([](const PIString & s){return s.startsWith('k');}); // -1
//! \endcode //! \endcode
//! \~\sa \a indexOf(), \a lastIndexOf(), \a lastIndexWhere(), \a contains() //! \~\sa \a indexOf(), \a lastIndexOf(), \a lastIndexWhere(), \a contains()
inline ssize_t indexWhere(std::function<bool(const T & e)> test, ssize_t start = 0) const { inline ssize_t indexWhere(std::function<bool(const T & e)> test, size_t start = 0) const {
if (start < 0) { if (start < 0) {
start = pid_size + start; start = pid_size + start;
if (start < 0) start = 0; if (start < 0) start = 0;
} }
for (size_t i = pid_start + size_t(start); i < pid_start + pid_size; ++i) { for (ssize_t i = pid_start + start; i < pid_start + (ssize_t)pid_size; ++i) {
if (test(pid_data[i])) { if (test(pid_data[i])) {
return ssize_t(i) - pid_start; return i - pid_start;
} }
} }
return -1; return -1;
@@ -1032,7 +1005,7 @@ public:
//! c которого начинать поиск в обратном направлении. //! c которого начинать поиск в обратном направлении.
//! Если индекс больше или равен длине массива, просматривается весь массив. //! Если индекс больше или равен длине массива, просматривается весь массив.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива. //! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от конца к началу (справа на лево). //! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от конца к началу.
//! Если рассчитанный индекс оказывается меньше 0, массив даже не просматривается. //! Если рассчитанный индекс оказывается меньше 0, массив даже не просматривается.
//! Значение по умолчанию равно `-1`, что равно индексу последнего элемента //! Значение по умолчанию равно `-1`, что равно индексу последнего элемента
//! и означает, что просматривается весь массив. //! и означает, что просматривается весь массив.
@@ -1049,9 +1022,9 @@ public:
inline ssize_t lastIndexOf(const T & e, ssize_t start = -1) const { inline ssize_t lastIndexOf(const T & e, ssize_t start = -1) const {
if (start >= size_s()) start = pid_size - 1; if (start >= size_s()) start = pid_size - 1;
if (start < 0) start = pid_size + start; if (start < 0) start = pid_size + start;
for (ssize_t i = ssize_t(pid_start) + start; i >= ssize_t(pid_start); --i) { for (ssize_t i = pid_start + start; i >= pid_start; --i) {
if (e == pid_data[i]) { if (e == pid_data[i]) {
return i - ssize_t(pid_start); return i - pid_start;
} }
} }
return -1; return -1;
@@ -1077,7 +1050,7 @@ public:
//! c которого начинать поиск в обратном направлении. //! c которого начинать поиск в обратном направлении.
//! Если индекс больше или равен длине массива, просматривается весь массив. //! Если индекс больше или равен длине массива, просматривается весь массив.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива. //! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от конца к началу (справа на лево). //! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от конца к началу.
//! Если рассчитанный индекс оказывается меньше 0, массив даже не просматривается. //! Если рассчитанный индекс оказывается меньше 0, массив даже не просматривается.
//! Значение по умолчанию равно `-1`, что равно индексу последнего элемента //! Значение по умолчанию равно `-1`, что равно индексу последнего элемента
//! и означает, что просматривается весь массив. //! и означает, что просматривается весь массив.
@@ -1085,9 +1058,9 @@ public:
inline ssize_t lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const { inline ssize_t lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const {
if (start >= size_s()) start = pid_size - 1; if (start >= size_s()) start = pid_size - 1;
if (start < 0) start = pid_size + start; if (start < 0) start = pid_size + start;
for (ssize_t i = ssize_t(pid_start) + start; i >= ssize_t(pid_start); --i) { for (ssize_t i = pid_start + start; i >= pid_start; --i) {
if (test(pid_data[i])) { if (test(pid_data[i])) {
return i - ssize_t(pid_start); return i - pid_start;
} }
} }
return -1; return -1;
@@ -1106,7 +1079,7 @@ public:
//! memcpy(vec.data(1), a, 2 * sizeof(int)); //! memcpy(vec.data(1), a, 2 * sizeof(int));
//! piCout << v; // {2, 12, 13, 2} //! piCout << v; // {2, 12, 13, 2}
//! \endcode //! \endcode
inline T * data(size_t index = 0) { return &(pid_data[pid_start + index]); } inline T * data(size_t index = 0) {return &(pid_data[pid_start + index]);}
//! \~english Read only pointer to array //! \~english Read only pointer to array
//! \~russian Указатель на память массива только для чтения. //! \~russian Указатель на память массива только для чтения.
@@ -1125,7 +1098,7 @@ public:
//! memcpy(a, v.data(), a.size() * sizeof(int)); //! memcpy(a, v.data(), a.size() * sizeof(int));
//! piCout << a[0] << a[1] << a[2]; // 1 3 5 //! piCout << a[0] << a[1] << a[2]; // 1 3 5
//! \endcode //! \endcode
inline const T * data(size_t index = 0) const { return &(pid_data[pid_start + index]); } inline const T * data(size_t index = 0) const {return &(pid_data[pid_start + index]);}
//! \~english Creates sub-array of this array. //! \~english Creates sub-array of this array.
//! \~russian Создает подмассив, то есть кусок из текущего массива. //! \~russian Создает подмассив, то есть кусок из текущего массива.
@@ -1146,7 +1119,7 @@ public:
PIDeque<T> getRange(size_t index, size_t count) const { PIDeque<T> getRange(size_t index, size_t count) const {
if (index >= pid_size || count == 0) return PIDeque<T>(); if (index >= pid_size || count == 0) return PIDeque<T>();
if (index + count > pid_size) count = pid_size - index; if (index + count > pid_size) count = pid_size - index;
return PIDeque(pid_data + pid_start + index, count); return PIDeque(&(pid_data[pid_start + index]), count);
} }
//! \~english Clear array, remove all elements. //! \~english Clear array, remove all elements.
@@ -1156,17 +1129,19 @@ public:
//! \~english Reserved memory will not be released. //! \~english Reserved memory will not be released.
//! \~russian Зарезервированная память не освободится. //! \~russian Зарезервированная память не освободится.
//! \~\sa \a resize() //! \~\sa \a resize()
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable<T1>::value, int>::type = 0> template<typename T1 = T, typename std::enable_if<
!std::is_trivially_copyable<T1>::value
, int>::type = 0>
inline PIDeque<T> & clear() { inline PIDeque<T> & clear() {
deleteT(pid_data + pid_start, pid_size); resize(0);
pid_size = 0;
pid_start = (pid_rsize - pid_size) / 2;
return *this; return *this;
} }
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0> template<typename T1 = T, typename std::enable_if<
std::is_trivially_copyable<T1>::value
, int>::type = 0>
inline PIDeque<T> & clear() { inline PIDeque<T> & clear() {
PIINTROSPECTION_CONTAINER_UNUSED(T, pid_size) PIINTROSPECTION_CONTAINER_UNUSED(T, pid_size)
pid_size = 0; pid_size = 0;
pid_start = (pid_rsize - pid_size) / 2; pid_start = (pid_rsize - pid_size) / 2;
return *this; return *this;
} }
@@ -1210,17 +1185,21 @@ public:
//! \~english Same as \a fill(). //! \~english Same as \a fill().
//! \~russian Тоже самое что и \a fill(). //! \~russian Тоже самое что и \a fill().
//! \~\sa \a fill(), \a resize() //! \~\sa \a fill(), \a resize()
inline PIDeque<T> & assign(const T & e = T()) { return fill(e); } inline PIDeque<T> & assign(const T & e = T()) {return fill(e);}
//! \~english First does `resize(new_size)` then `fill(e)`. //! \~english First does `resize(new_size)` then `fill(e)`.
//! \~russian Сначала делает `resize(new_size)`, затем `fill(e)`. //! \~russian Сначала делает `resize(new_size)`, затем `fill(e)`.
//! \~\sa \a fill(), \a resize() //! \~\sa \a fill(), \a resize()
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable<T1>::value, int>::type = 0> template<typename T1 = T, typename std::enable_if<
!std::is_trivially_copyable<T1>::value
, int>::type = 0>
inline PIDeque<T> & assign(size_t new_size, const T & e) { inline PIDeque<T> & assign(size_t new_size, const T & e) {
resize(new_size); resize(new_size);
return fill(e); return fill(e);
} }
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0> template<typename T1 = T, typename std::enable_if<
std::is_trivially_copyable<T1>::value
, int>::type = 0>
inline PIDeque<T> & assign(size_t new_size, const T & e) { inline PIDeque<T> & assign(size_t new_size, const T & e) {
_resizeRaw(new_size); _resizeRaw(new_size);
return fill(e); return fill(e);
@@ -1238,12 +1217,20 @@ public:
//! лишние элементы удаляются с конца массива. //! лишние элементы удаляются с конца массива.
//! \~\sa \a size(), \a clear() //! \~\sa \a size(), \a clear()
inline PIDeque<T> & resize(size_t new_size, const T & e = T()) { inline PIDeque<T> & resize(size_t new_size, const T & e = T()) {
if (new_size == 0) return clear();
if (new_size < pid_size) { if (new_size < pid_size) {
deleteT(pid_data + pid_start + new_size, pid_size - new_size); deleteT(&(pid_data[new_size + pid_start]), pid_size - new_size);
pid_size = new_size; pid_size = new_size;
} else if (new_size > pid_size) { if (new_size == 0) {
expand(new_size, e); pid_start = (pid_rsize - pid_size) / 2;
}
}
if (new_size > pid_size) {
size_t os = pid_size;
alloc_forward(new_size);
PIINTROSPECTION_CONTAINER_USED(T, (new_size-os))
for (size_t i = os + pid_start; i < new_size + pid_start; ++i) {
elementNew(pid_data + i, e);
}
} }
return *this; return *this;
} }
@@ -1260,31 +1247,41 @@ public:
//! лишние элементы удаляются с конца массива. //! лишние элементы удаляются с конца массива.
//! \~\sa \a size(), \a clear() //! \~\sa \a size(), \a clear()
inline PIDeque<T> & resize(size_t new_size, std::function<T(size_t i)> f) { inline PIDeque<T> & resize(size_t new_size, std::function<T(size_t i)> f) {
if (new_size == 0) return clear();
if (new_size < pid_size) { if (new_size < pid_size) {
deleteT(pid_data + pid_start + new_size, pid_size - new_size); deleteT(&(pid_data[new_size + pid_start]), pid_size - new_size);
pid_size = new_size; pid_size = new_size;
} else if (new_size > pid_size) { if (new_size == 0) {
expand(new_size, f); pid_start = (pid_rsize - pid_size) / 2;
}
}
if (new_size > pid_size) {
size_t os = pid_size;
alloc_forward(new_size);
PIINTROSPECTION_CONTAINER_USED(T, (new_size-os))
for (size_t i = os + pid_start; i < new_size + pid_start; ++i) {
elementNew(pid_data + i, f(i));
}
} }
return *this; return *this;
} }
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0> template<typename T1 = T, typename std::enable_if<
std::is_trivially_copyable<T1>::value
, int>::type = 0>
inline PIDeque<T> & _resizeRaw(size_t new_size) { inline PIDeque<T> & _resizeRaw(size_t new_size) {
#if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
if (new_size > pid_size) { if (new_size > pid_size) {
PIINTROSPECTION_CONTAINER_USED(T, (new_size - pid_size)); PIINTROSPECTION_CONTAINER_USED(T, (new_size-pid_size));
} }
if (new_size < pid_size) { if (new_size < pid_size) {
PIINTROSPECTION_CONTAINER_UNUSED(T, (pid_size - new_size)); PIINTROSPECTION_CONTAINER_UNUSED(T, (pid_size-new_size));
} }
#endif
alloc_forward(new_size); alloc_forward(new_size);
return *this; return *this;
} }
inline void _copyRaw(T * dst, const T * src, size_t size) { newT(dst, src, size); } inline void _copyRaw(T * dst, const T * src, size_t size) {
newT(dst, src, size);
}
//! \~english Attempts to allocate memory for at least `new_size` elements. //! \~english Attempts to allocate memory for at least `new_size` elements.
//! \~russian Резервируется память под как минимум `new_size` элементов. //! \~russian Резервируется память под как минимум `new_size` элементов.
@@ -1302,7 +1299,7 @@ public:
//! \~\sa \a size(), \a capacity(), \a resize() //! \~\sa \a size(), \a capacity(), \a resize()
inline PIDeque<T> & reserve(size_t new_size) { inline PIDeque<T> & reserve(size_t new_size) {
if (new_size <= pid_rsize) return *this; if (new_size <= pid_rsize) return *this;
const size_t os = pid_size; size_t os = pid_size;
alloc_forward(new_size); alloc_forward(new_size);
pid_size = os; pid_size = os;
return *this; return *this;
@@ -1322,21 +1319,17 @@ public:
inline PIDeque<T> & insert(size_t index, const T & e = T()) { inline PIDeque<T> & insert(size_t index, const T & e = T()) {
if (index == pid_size) return push_back(e); if (index == pid_size) return push_back(e);
PIINTROSPECTION_CONTAINER_USED(T, 1) PIINTROSPECTION_CONTAINER_USED(T, 1)
const bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false); bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
if (dir) { if (dir) {
alloc_forward(pid_size + 1); alloc_forward(pid_size + 1);
if (index < pid_size - 1) { if (index < pid_size - 1) {
const size_t os = pid_size - index - 1; size_t os = pid_size - index - 1;
memmove(reinterpret_cast<void *>(pid_data + pid_start + index + 1), memmove((void*)(&(pid_data[index + pid_start + 1])), (const void*)(&(pid_data[index + pid_start])), os * sizeof(T));
reinterpret_cast<const void *>(pid_data + pid_start + index),
os * sizeof(T));
} }
} else { } else {
alloc_backward(pid_size + 1, -1); alloc_backward(pid_size + 1, -1);
if (index > 0) { if (index > 0) {
memmove(reinterpret_cast<void *>(pid_data + pid_start), memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + 1])), index * sizeof(T));
reinterpret_cast<const void *>(pid_data + pid_start + 1),
index * sizeof(T));
} }
} }
elementNew(pid_data + pid_start + index, e); elementNew(pid_data + pid_start + index, e);
@@ -1352,21 +1345,17 @@ public:
inline PIDeque<T> & insert(size_t index, T && e) { inline PIDeque<T> & insert(size_t index, T && e) {
if (index == pid_size) return push_back(e); if (index == pid_size) return push_back(e);
PIINTROSPECTION_CONTAINER_USED(T, 1) PIINTROSPECTION_CONTAINER_USED(T, 1)
const bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false); bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
if (dir) { if (dir) {
alloc_forward(pid_size + 1); alloc_forward(pid_size + 1);
if (index < pid_size - 1) { if (index < pid_size - 1) {
const size_t os = pid_size - index - 1; size_t os = pid_size - index - 1;
memmove(reinterpret_cast<void *>(pid_data + pid_start + index + 1), memmove((void*)(&(pid_data[index + pid_start + 1])), (const void*)(&(pid_data[index + pid_start])), os * sizeof(T));
reinterpret_cast<const void *>(pid_data + pid_start + index),
os * sizeof(T));
} }
} else { } else {
alloc_backward(pid_size + 1, -1); alloc_backward(pid_size + 1, -1);
if (index > 0) { if (index > 0) {
memmove(reinterpret_cast<void *>(pid_data + pid_start), memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + 1])), index * sizeof(T));
reinterpret_cast<const void *>(pid_data + pid_start + 1),
index * sizeof(T));
} }
} }
elementNew(pid_data + pid_start + index, std::move(e)); elementNew(pid_data + pid_start + index, std::move(e));
@@ -1383,25 +1372,21 @@ public:
if (v.isEmpty()) return *this; if (v.isEmpty()) return *this;
#ifndef NDEBUG #ifndef NDEBUG
if (&v == this) { if (&v == this) {
fprintf(stderr, "error with PIDeque<%s>::insert\n", __PIP_TYPENAME__(T)); printf("error with PIDeque<%s>::insert\n", __PIP_TYPENAME__(T));
} }
#endif #endif
assert(&v != this); assert(&v != this);
const bool dir = v.size() > pid_size ? true : (index >= pid_rsize / 2 ? true : false); bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
if (dir) { if (dir) {
const ssize_t os = pid_size - index; ssize_t os = pid_size - index;
alloc_forward(pid_size + v.pid_size); alloc_forward(pid_size + v.pid_size);
if (os > 0) { if (os > 0) {
memmove(reinterpret_cast<void *>(pid_data + pid_start + index + v.pid_size), memmove((void*)(&(pid_data[index + pid_start + v.pid_size])), (const void*)(&(pid_data[index + pid_start])), os * sizeof(T));
reinterpret_cast<const void *>(pid_data + pid_start + index),
os * sizeof(T));
} }
} else { } else {
alloc_backward(pid_size + v.pid_size, -v.pid_size); alloc_backward(pid_size + v.pid_size, -v.pid_size);
if (index > 0) { if (index > 0) {
memmove(reinterpret_cast<void *>(pid_data + pid_start), memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + v.pid_size])), index * sizeof(T));
reinterpret_cast<const void *>(pid_data + pid_start + v.pid_size),
index * sizeof(T));
} }
} }
newT(pid_data + pid_start + index, v.pid_data + v.pid_start, v.pid_size); newT(pid_data + pid_start + index, v.pid_data + v.pid_start, v.pid_size);
@@ -1419,22 +1404,17 @@ public:
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list). //! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append(), \a prepend(), \a remove() //! \~\sa \a append(), \a prepend(), \a remove()
inline PIDeque<T> & insert(size_t index, std::initializer_list<T> init_list) { inline PIDeque<T> & insert(size_t index, std::initializer_list<T> init_list) {
if (init_list.size() == 0) return *this; bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
const bool dir = init_list.size() > pid_size ? true : (index >= pid_rsize / 2 ? true : false);
if (dir) { if (dir) {
const ssize_t os = ssize_t(pid_size) - index; ssize_t os = pid_size - index;
alloc_forward(pid_size + init_list.size()); alloc_forward(pid_size + init_list.size());
if (os > 0) { if (os > 0) {
memmove(reinterpret_cast<void *>(pid_data + pid_start + index + init_list.size()), memmove((void*)(&(pid_data[index + pid_start + init_list.size()])), (const void*)(&(pid_data[index + pid_start])), os * sizeof(T));
reinterpret_cast<const void *>(pid_data + pid_start + index),
os * sizeof(T));
} }
} else { } else {
alloc_backward(pid_size + init_list.size(), -init_list.size()); alloc_backward(pid_size + init_list.size(), -init_list.size());
if (index > 0) { if (index > 0) {
memmove(reinterpret_cast<void *>(pid_data + pid_start), memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + init_list.size()])), index * sizeof(T));
reinterpret_cast<const void *>(pid_data + pid_start + init_list.size()),
index * sizeof(T));
} }
} }
newT(pid_data + pid_start + index, init_list.begin(), init_list.size()); newT(pid_data + pid_start + index, init_list.begin(), init_list.size());
@@ -1453,27 +1433,22 @@ public:
inline PIDeque<T> & remove(size_t index, size_t count = 1) { inline PIDeque<T> & remove(size_t index, size_t count = 1) {
if (count == 0) return *this; if (count == 0) return *this;
if (index + count >= pid_size) { if (index + count >= pid_size) {
if (index < pid_size) { resize(index);
deleteT(pid_data + pid_start + index, pid_size - index); return *this;
pid_size = index; }
size_t os = pid_size - index - count;
deleteT(&(pid_data[index + pid_start]), count);
if (os <= index) {
if (os > 0) {
memmove((void*)(&(pid_data[index + pid_start])), (const void*)(&(pid_data[index + pid_start + count])), os * sizeof(T));
} }
} else { } else {
const size_t os = pid_size - index - count; if (index > 0) {
deleteT(pid_data + pid_start + index, count); memmove((void*)(&(pid_data[pid_start + count])), (const void*)(&(pid_data[pid_start])), index * sizeof(T));
if (os <= index) {
memmove(reinterpret_cast<void *>(pid_data + pid_start + index),
reinterpret_cast<const void *>(pid_data + pid_start + index + count),
os * sizeof(T));
} else {
if (index > 0) {
memmove(reinterpret_cast<void *>(pid_data + pid_start + count),
reinterpret_cast<const void *>(pid_data + pid_start),
index * sizeof(T));
}
pid_start += count;
} }
pid_size -= count; pid_start += count;
} }
pid_size -= count;
return *this; return *this;
} }
@@ -1483,10 +1458,10 @@ public:
//! \~english This operation is very fast and never fails. //! \~english This operation is very fast and never fails.
//! \~russian Эта операция выполняется мгновенно без копирования памяти и никогда не дает сбоев. //! \~russian Эта операция выполняется мгновенно без копирования памяти и никогда не дает сбоев.
inline void swap(PIDeque<T> & other) { inline void swap(PIDeque<T> & other) {
piSwap<T *>(pid_data, other.pid_data); piSwap<T*>(pid_data, other.pid_data);
piSwap<size_t>(pid_size, other.pid_size); piSwap<size_t>(pid_size, other.pid_size);
piSwap<size_t>(pid_rsize, other.pid_rsize); piSwap<size_t>(pid_rsize, other.pid_rsize);
piSwap<size_t>(pid_start, other.pid_start); piSwap<ssize_t>(pid_start, other.pid_start);
} }
//! \~english Sorts the elements in non-descending order. //! \~english Sorts the elements in non-descending order.
@@ -1528,18 +1503,23 @@ public:
//! Complexity `O(N·log(N))`. //! Complexity `O(N·log(N))`.
//! \~russian Сохранность порядка элементов, имеющих одинаковое значение, не гарантируется. //! \~russian Сохранность порядка элементов, имеющих одинаковое значение, не гарантируется.
//! Для сравнения элементов используется функция сравнения `comp`. //! Для сравнения элементов используется функция сравнения `comp`.
//! Функция сравнения, возвращает `true` если первый аргумент меньше //! Функция сравнения, возвращает `true` если первый аргумент меньше второго.
//! второго. Сигнатура функции сравнения должна быть эквивалентна следующей: \code bool comp(const T &a, const T &b); \endcode Сигнатура //! Сигнатура функции сравнения должна быть эквивалентна следующей:
//! не обязана содержать const &, однако, функция не может изменять переданные объекты. Функция обязана возвращать `false` для //! \code
//! одинаковых элементов, иначе это приведёт к неопределённому поведению программы и ошибкам памяти. Для сортировки используется функция //! bool comp(const T &a, const T &b);
//! [std::sort](https://ru.cppreference.com/w/cpp/algorithm/sort). Сложность сортировки `O(N·log(N))`. //! \endcode
//! Сигнатура не обязана содержать const &, однако, функция не может изменять переданные объекты.
//! Функция обязана возвращать `false` для одинаковых элементов,
//! иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
//! Для сортировки используется функция [std::sort](https://ru.cppreference.com/w/cpp/algorithm/sort).
//! Сложность сортировки `O(N·log(N))`.
//! \~\code //! \~\code
//! PIDeque<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; //! PIDeque<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
//! v.sort([](const int & a, const int & b){return a > b;}); //! v.sort([](const int & a, const int & b){return a > b;});
//! piCout << v; // 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 //! piCout << v; // 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
//! \endcode //! \endcode
//! \~\sa \a sort() //! \~\sa \a sort()
inline PIDeque<T> & sort(std::function<bool(const T & a, const T & b)> comp) { inline PIDeque<T> & sort(std::function<bool(const T &a, const T &b)> comp) {
std::sort(begin(), end(), comp); std::sort(begin(), end(), comp);
return *this; return *this;
} }
@@ -1562,9 +1542,9 @@ public:
//! \endcode //! \endcode
//! \~\sa \a reversed() //! \~\sa \a reversed()
inline PIDeque<T> & reverse() { inline PIDeque<T> & reverse() {
const size_t s2 = pid_size / 2; size_t s2 = pid_size/2;
for (size_t i = 0; i < s2; ++i) { for (size_t i = 0; i < s2; ++i) {
piSwap<T>(pid_data[pid_start + i], pid_data[pid_start + pid_size - i - 1]); piSwap<T>(pid_data[pid_start+i], pid_data[pid_start+pid_size-i-1]);
} }
return *this; return *this;
} }
@@ -1592,12 +1572,10 @@ public:
//! Если `add_size < 0`, то с конца массива удаляются элементы. //! Если `add_size < 0`, то с конца массива удаляются элементы.
//! Если `add_size < 0` и в массиве меньше элементов чем указано, то массив становится пустым. //! Если `add_size < 0` и в массиве меньше элементов чем указано, то массив становится пустым.
//! \~\sa \a resize() //! \~\sa \a resize()
inline PIDeque<T> & enlarge(ssize_t add_size, const T & e = T()) { inline PIDeque<T> & enlarge(llong pid_size) {
const ssize_t ns = size_s() + add_size; llong ns = size_s() + pid_size;
if (ns <= 0) if (ns <= 0) clear();
clear(); else resize(size_t(ns));
else
resize(size_t(ns), e);
return *this; return *this;
} }
@@ -1630,14 +1608,11 @@ public:
//! \endcode //! \endcode
//! \~\sa \a remove(), \a removeOne(), \a removeWhere() //! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIDeque<T> & removeAll(const T & e) { inline PIDeque<T> & removeAll(const T & e) {
ssize_t j = indexOf(e); for (ssize_t i = 0; i < ssize_t(pid_size); ++i) {
if (j != -1) { if (pid_data[i + pid_start] == e) {
for (size_t i = j + 1; i < pid_size; ++i) { remove(i);
if (pid_data[i + pid_start] != e) { --i;
pid_data[j++ + pid_start] = std::move(pid_data[i + pid_start]);
}
} }
remove(j, pid_size - j);
} }
return *this; return *this;
} }
@@ -1654,14 +1629,11 @@ public:
//! \endcode //! \endcode
//! \~\sa \a remove(), \a removeOne(), \a removeWhere() //! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIDeque<T> & removeWhere(std::function<bool(const T & e)> test) { inline PIDeque<T> & removeWhere(std::function<bool(const T & e)> test) {
ssize_t j = indexWhere(test); for (ssize_t i = 0; i < ssize_t(pid_size); ++i) {
if (j != -1) { if (test(pid_data[i + pid_start])) {
for (size_t i = j + 1; i < pid_size; ++i) { remove(i);
if (!test(pid_data[i + pid_start])) { --i;
pid_data[j++ + pid_start] = std::move(pid_data[i + pid_start]);
}
} }
remove(j, pid_size - j);
} }
return *this; return *this;
} }
@@ -1721,8 +1693,7 @@ public:
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list). //! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a push_back() //! \~\sa \a push_back()
inline PIDeque<T> & push_back(std::initializer_list<T> init_list) { inline PIDeque<T> & push_back(std::initializer_list<T> init_list) {
if (init_list.size() == 0) return *this; size_t ps = pid_size;
const size_t ps = pid_size;
alloc_forward(pid_size + init_list.size()); alloc_forward(pid_size + init_list.size());
newT(pid_data + pid_start + ps, init_list.begin(), init_list.size()); newT(pid_data + pid_start + ps, init_list.begin(), init_list.size());
return *this; return *this;
@@ -1735,14 +1706,13 @@ public:
//! \~russian Перегруженая функция. //! \~russian Перегруженая функция.
//! \~\sa \a push_back() //! \~\sa \a push_back()
inline PIDeque<T> & push_back(const PIDeque<T> & v) { inline PIDeque<T> & push_back(const PIDeque<T> & v) {
if (v.isEmpty()) return *this;
#ifndef NDEBUG #ifndef NDEBUG
if (&v == this) { if (&v == this) {
fprintf(stderr, "error with PIDeque<%s>::append\n", __PIP_TYPENAME__(T)); printf("error with PIDeque<%s>::append\n", __PIP_TYPENAME__(T));
} }
#endif #endif
assert(&v != this); assert(&v != this);
const size_t ps = pid_size; size_t ps = pid_size;
alloc_forward(pid_size + v.pid_size); alloc_forward(pid_size + v.pid_size);
newT(pid_data + ps + pid_start, v.pid_data + v.pid_start, v.pid_size); newT(pid_data + ps + pid_start, v.pid_data + v.pid_start, v.pid_size);
return *this; return *this;
@@ -1772,7 +1742,7 @@ public:
//! piCout << v; // {1, 2, 3, 4, 5} //! piCout << v; // {1, 2, 3, 4, 5}
//! \endcode //! \endcode
//! \~\sa \a prepend(), \a push_front(), \a push_back(), \a insert() //! \~\sa \a prepend(), \a push_front(), \a push_back(), \a insert()
inline PIDeque<T> & append(const T & e) { return push_back(e); } inline PIDeque<T> & append(const T & e) {return push_back(e);}
//! \~english Appends the given element `e` to the end of the array. //! \~english Appends the given element `e` to the end of the array.
//! \~russian Добавляет элемент `e` в конец массива. //! \~russian Добавляет элемент `e` в конец массива.
@@ -1780,7 +1750,7 @@ public:
//! \~english Overloaded function. //! \~english Overloaded function.
//! \~russian Перегруженая функция. //! \~russian Перегруженая функция.
//! \~\sa \a append() //! \~\sa \a append()
inline PIDeque<T> & append(T && e) { return push_back(std::move(e)); } inline PIDeque<T> & append(T && e) {return push_back(std::move(e));}
//! \~english Appends the given elements to the end of the array. //! \~english Appends the given elements to the end of the array.
//! \~russian Добавляет элементы в конец массива. //! \~russian Добавляет элементы в конец массива.
@@ -1792,7 +1762,7 @@ public:
//! Добавляет элементы из //! Добавляет элементы из
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list). //! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append() //! \~\sa \a append()
inline PIDeque<T> & append(std::initializer_list<T> init_list) { return push_back(init_list); } inline PIDeque<T> & append(std::initializer_list<T> init_list) {return push_back(init_list);}
//! \~english Appends the given array `v` to the end of the array. //! \~english Appends the given array `v` to the end of the array.
//! \~russian Добавляет массив `v` в конец массива. //! \~russian Добавляет массив `v` в конец массива.
@@ -1805,7 +1775,7 @@ public:
//! piCout << v; // {1, 2, 3, 4, 5} //! piCout << v; // {1, 2, 3, 4, 5}
//! \endcode //! \endcode
//! \~\sa \a append() //! \~\sa \a append()
inline PIDeque<T> & append(const PIDeque<T> & v) { return push_back(v); } inline PIDeque<T> & append(const PIDeque<T> & v) {return push_back(v);}
//! \~english Appends the given element `e` to the end of the array. //! \~english Appends the given element `e` to the end of the array.
//! \~russian Добавляет элемент `e` в конец массива. //! \~russian Добавляет элемент `e` в конец массива.
@@ -1816,7 +1786,7 @@ public:
//! piCout << v; // {1, 2, 3, 4, 5} //! piCout << v; // {1, 2, 3, 4, 5}
//! \endcode //! \endcode
//! \~\sa \a append() //! \~\sa \a append()
inline PIDeque<T> & operator<<(const T & e) { return push_back(e); } inline PIDeque<T> & operator <<(const T & e) {return push_back(e);}
//! \~english Appends the given element `e` to the end of the array. //! \~english Appends the given element `e` to the end of the array.
//! \~russian Добавляет элемент `e` в конец массива. //! \~russian Добавляет элемент `e` в конец массива.
@@ -1827,7 +1797,7 @@ public:
//! piCout << v; // {1, 2, 3, 4, 5} //! piCout << v; // {1, 2, 3, 4, 5}
//! \endcode //! \endcode
//! \~\sa \a append() //! \~\sa \a append()
inline PIDeque<T> & operator<<(T && e) { return push_back(std::move(e)); } inline PIDeque<T> & operator <<(T && e) {return push_back(std::move(e));}
//! \~english Appends the given array `v` to the end of the array. //! \~english Appends the given array `v` to the end of the array.
//! \~russian Добавляет массив `v` в конец массива. //! \~russian Добавляет массив `v` в конец массива.
@@ -1838,7 +1808,7 @@ public:
//! piCout << v; // {1, 2, 3, 4, 5} //! piCout << v; // {1, 2, 3, 4, 5}
//! \endcode //! \endcode
//! \~\sa \a append(), \a push_back() //! \~\sa \a append(), \a push_back()
inline PIDeque<T> & operator<<(const PIDeque<T> & v) { return append(v); } inline PIDeque<T> & operator <<(const PIDeque<T> & v) {return append(v);}
//! \~english Appends the given element `e` to the begin of the array. //! \~english Appends the given element `e` to the begin of the array.
//! \~russian Добавляет элемент `e` в начало массива. //! \~russian Добавляет элемент `e` в начало массива.
@@ -1865,9 +1835,7 @@ public:
//! \endcode //! \endcode
//! \~\sa \a push_back(), \a append(), \a prepend(), \a insert() //! \~\sa \a push_back(), \a append(), \a prepend(), \a insert()
inline PIDeque<T> & push_front(const T & e) { inline PIDeque<T> & push_front(const T & e) {
if (isEmpty()) return push_back(e); insert(0, e);
alloc_backward(pid_size + 1, -1);
elementNew(pid_data + pid_start, e);
return *this; return *this;
} }
@@ -1878,9 +1846,7 @@ public:
//! \~russian Перегруженая функция. //! \~russian Перегруженая функция.
//! \~\sa \a push_front() //! \~\sa \a push_front()
inline PIDeque<T> & push_front(T && e) { inline PIDeque<T> & push_front(T && e) {
if (isEmpty()) return push_back(std::move(e)); insert(0, std::move(e));
alloc_backward(pid_size + 1, -1);
elementNew(pid_data + pid_start, std::move(e));
return *this; return *this;
} }
@@ -1895,7 +1861,10 @@ public:
//! piCout << v; // {4, 5, 1, 2, 3} //! piCout << v; // {4, 5, 1, 2, 3}
//! \endcode //! \endcode
//! \~\sa \a push_front() //! \~\sa \a push_front()
inline PIDeque<T> & push_front(const PIDeque<T> & v) { return insert(0, v); } inline PIDeque<T> & push_front(const PIDeque<T> & v) {
insert(0, v);
return *this;
}
//! \~english Appends the given elements to the begin of the array. //! \~english Appends the given elements to the begin of the array.
//! \~russian Добавляет элементы в начало массива. //! \~russian Добавляет элементы в начало массива.
@@ -1907,7 +1876,10 @@ public:
//! Добавляет элементы из //! Добавляет элементы из
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list). //! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append() //! \~\sa \a append()
inline PIDeque<T> & push_front(std::initializer_list<T> init_list) { return insert(0, init_list); } inline PIDeque<T> & push_front(std::initializer_list<T> init_list) {
insert(0, init_list);
return *this;
}
//! \~english Appends the given element `e` to the begin of the array. //! \~english Appends the given element `e` to the begin of the array.
//! \~russian Добавляет элемент `e` в начало массива. //! \~russian Добавляет элемент `e` в начало массива.
@@ -1933,7 +1905,7 @@ public:
//! piCout << v; // {5, 4, 1, 2, 3} //! piCout << v; // {5, 4, 1, 2, 3}
//! \endcode //! \endcode
//! \~\sa \a push_back(), \a append(), \a prepend(), \a insert() //! \~\sa \a push_back(), \a append(), \a prepend(), \a insert()
inline PIDeque<T> & prepend(const T & e) { return push_front(e); } inline PIDeque<T> & prepend(const T & e) {return push_front(e);}
//! \~english Appends the given element `e` to the begin of the array. //! \~english Appends the given element `e` to the begin of the array.
//! \~russian Добавляет элемент `e` в начало массива. //! \~russian Добавляет элемент `e` в начало массива.
@@ -1941,7 +1913,7 @@ public:
//! \~english Overloaded function. //! \~english Overloaded function.
//! \~russian Перегруженая функция. //! \~russian Перегруженая функция.
//! \~\sa \a prepend() //! \~\sa \a prepend()
inline PIDeque<T> & prepend(T && e) { return push_front(std::move(e)); } inline PIDeque<T> & prepend(T && e) {return push_front(std::move(e));}
//! \~english Appends the given array `v` to the begin of the array. //! \~english Appends the given array `v` to the begin of the array.
//! \~russian Добавляет массив `v` в начало массива. //! \~russian Добавляет массив `v` в начало массива.
@@ -1954,7 +1926,7 @@ public:
//! piCout << v; // {4, 5, 1, 2, 3} //! piCout << v; // {4, 5, 1, 2, 3}
//! \endcode //! \endcode
//! \~\sa \a prepend() //! \~\sa \a prepend()
inline PIDeque<T> & prepend(const PIDeque<T> & v) { return push_front(v); } inline PIDeque<T> & prepend(const PIDeque<T> & v) {return push_front(v);}
//! \~english Appends the given elements to the begin of the array. //! \~english Appends the given elements to the begin of the array.
//! \~russian Добавляет элементы в начало массива. //! \~russian Добавляет элементы в начало массива.
@@ -1966,7 +1938,7 @@ public:
//! Добавляет элементы из //! Добавляет элементы из
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list). //! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append() //! \~\sa \a append()
inline PIDeque<T> & prepend(std::initializer_list<T> init_list) { return push_front(init_list); } inline PIDeque<T> & prepend(std::initializer_list<T> init_list) {return prepend(init_list);}
//! \~english Remove one element from the end of the array. //! \~english Remove one element from the end of the array.
//! \~russian Удаляет один элемент с конца массива. //! \~russian Удаляет один элемент с конца массива.
@@ -1983,8 +1955,7 @@ public:
//! \~\sa \a pop_front(), \a take_back(), \a take_front() //! \~\sa \a pop_front(), \a take_back(), \a take_front()
inline PIDeque<T> & pop_back() { inline PIDeque<T> & pop_back() {
if (pid_size == 0) return *this; if (pid_size == 0) return *this;
elementDelete(pid_data[pid_size + pid_start - 1]); resize(pid_size - 1);
pid_size = pid_size - 1;
return *this; return *this;
} }
@@ -2005,9 +1976,7 @@ public:
//! \~\sa \a pop_back(), \a take_back(), \a take_front() //! \~\sa \a pop_back(), \a take_back(), \a take_front()
inline PIDeque<T> & pop_front() { inline PIDeque<T> & pop_front() {
if (pid_size == 0) return *this; if (pid_size == 0) return *this;
elementDelete(pid_data[pid_start]); remove(0);
pid_start += 1;
pid_size -= 1;
return *this; return *this;
} }
@@ -2021,7 +1990,7 @@ public:
//! \endcode //! \endcode
//! \~\sa \a take_front(), \a pop_back(), \a pop_front() //! \~\sa \a take_front(), \a pop_back(), \a pop_front()
inline T take_back() { inline T take_back() {
const T e(back()); T e(back());
pop_back(); pop_back();
return e; return e;
} }
@@ -2036,7 +2005,7 @@ public:
//! \endcode //! \endcode
//! \~\sa \a take_front(), \a pop_back(), \a pop_front() //! \~\sa \a take_front(), \a pop_back(), \a pop_front()
inline T take_front() { inline T take_front() {
const T e(front()); T e(front());
pop_front(); pop_front();
return e; return e;
} }
@@ -2050,10 +2019,9 @@ public:
//! piCout << v2; // {1, 2, 3} //! piCout << v2; // {1, 2, 3}
//! \endcode //! \endcode
//! \~\sa \a map() //! \~\sa \a map()
template<typename ST> template <typename ST>
inline PIDeque<ST> toType() const { inline PIDeque<ST> toType() const {
PIDeque<ST> ret(pid_size); PIDeque<ST> ret(pid_size);
ret.reserve(pid_size);
for (size_t i = 0; i < pid_size; ++i) { for (size_t i = 0; i < pid_size; ++i) {
ret[i] = ST(pid_data[i + pid_start]); ret[i] = ST(pid_data[i + pid_start]);
} }
@@ -2061,9 +2029,9 @@ public:
} }
//! \~english Returns a new array with all elements //! \~english Returns a new array with all elements
//! that pass the test implemented by the provided function `bool test(const T & e)`. //! that pass the test implemented by the provided function `test`.
//! \~russian Возвращает новый массив со всеми элементами, //! \~russian Возвращает новый массив со всеми элементами,
//! прошедшими проверку, задаваемую в передаваемой функции `bool test(const T & e)`. //! прошедшими проверку, задаваемую в передаваемой функции `test`.
//! \~\details //! \~\details
//! \~\code //! \~\code
//! PIDeque<int> v{3, 2, 5, 2, 7}; //! PIDeque<int> v{3, 2, 5, 2, 7};
@@ -2079,39 +2047,6 @@ public:
return ret; return ret;
} }
//! \~english Same as \a filter() but with `index` parameter in `test`.
//! \~russian Аналогично \a filter() но с параметром индекса `index` в функции `test`.
//! \~\sa \a filter()
inline PIDeque<T> filterIndexed(std::function<bool(size_t index, const T & e)> test) const {
PIDeque<T> ret;
for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
if (test(i - pid_start, pid_data[i])) ret << pid_data[i];
}
return ret;
}
//! \~english Same as \a filter() but from end to begin (from right to left).
//! \~russian Аналогично \a filter() но от конца до начала (справа на лево).
//! \~\sa \a filter()
inline PIDeque<T> filterReverse(std::function<bool(const T & e)> test) const {
PIDeque<T> ret;
for (ssize_t i = ssize_t(pid_start + pid_size) - 1; i >= ssize_t(pid_start); --i) {
if (test(pid_data[i])) ret << pid_data[i];
}
return ret;
}
//! \~english Same as \a filterReverse() but with `index` parameter in `test`.
//! \~russian Аналогично \a filterReverse() но с параметром индекса `index` в функции `test`.
//! \~\sa \a filterReverse()
inline PIDeque<T> filterReverseIndexed(std::function<bool(size_t index, const T & e)> test) const {
PIDeque<T> ret;
for (ssize_t i = ssize_t(pid_start + pid_size) - 1; i >= ssize_t(pid_start); --i) {
if (test(i - pid_start, pid_data[i])) ret << pid_data[i];
}
return ret;
}
//! \~english Execute function `void f(const T & e)` for every element in array. //! \~english Execute function `void f(const T & e)` for every element in array.
//! \~russian Выполняет функцию `void f(const T & e)` для каждого элемента массива. //! \~russian Выполняет функцию `void f(const T & e)` для каждого элемента массива.
//! \~\details //! \~\details
@@ -2152,63 +2087,6 @@ public:
return *this; return *this;
} }
//! \~english Same as \a forEach() but with `index` parameter in `f`.
//! \~russian Аналогично \a forEach() но с параметром индекса `index` в функции `f`.
//! \~\sa \a forEach()
inline void forEachIndexed(std::function<void(size_t index, const T & e)> f) const {
for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
f(i - pid_start, pid_data[i]);
}
}
//! \~english Same as \a forEachIndexed(), but allows you to change the elements of the array.
//! \~russian Аналогично \a forEachIndexed(), но позволяет изменять элементы массива.
//! \~\sa \a forEach(), \a forEachIndexed()
inline PIDeque<T> & forEachIndexed(std::function<void(size_t index, T & e)> f) {
for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
f(i - pid_start, pid_data[i]);
}
return *this;
}
//! \~english Same as \a forEach() but from end to begin (from right to left).
//! \~russian Аналогично \a forEach() но от конца до начала (справа на лево).
//! \~\sa \a forEach()
inline void forEachReverse(std::function<void(const T & e)> f) const {
for (ssize_t i = ssize_t(pid_start + pid_size) - 1; i >= ssize_t(pid_start); --i) {
f(pid_data[i]);
}
}
//! \~english Same as \a forEachReverse(), but allows you to change the elements of the array.
//! \~russian Аналогично \a forEachReverse(), но позволяет изменять элементы массива.
//! \~\sa \a forEach(), \a forEachReverse()
inline PIDeque<T> & forEachReverse(std::function<void(T & e)> f) {
for (ssize_t i = ssize_t(pid_start + pid_size) - 1; i >= ssize_t(pid_start); --i) {
f(pid_data[i]);
}
return *this;
}
//! \~english Same as \a forEachIndexed() but from end to begin (from right to left).
//! \~russian Аналогично \a forEachIndexed() но от конца до начала (справа на лево).
//! \~\sa \a forEachIndexed(), \a forEachReverse(), \a forEach()
inline void forEachReverseIndexed(std::function<void(size_t index, const T & e)> f) const {
for (ssize_t i = ssize_t(pid_start + pid_size) - 1; i >= ssize_t(pid_start); --i) {
f(i - pid_start, pid_data[i]);
}
}
//! \~english Same as \a forEachReverseIndexed(), but allows you to change the elements of the array.
//! \~russian Аналогично \a forEachReverseIndexed(), но позволяет изменять элементы массива.
//! \~\sa \a forEachReverseIndexed(), \a forEachIndexed(), \a forEachReverse(), \a forEach()
inline PIDeque<T> & forEachReverseIndexed(std::function<void(size_t index, T & e)> f) {
for (ssize_t i = ssize_t(pid_start + pid_size) - 1; i >= ssize_t(pid_start); --i) {
f(i - pid_start, pid_data[i]);
}
return *this;
}
//! \~english Сreates a new array populated with the results //! \~english Сreates a new array populated with the results
//! of calling a provided function `ST f(const T & e)` on every element in the calling array. //! of calling a provided function `ST f(const T & e)` on every element in the calling array.
//! \~russian Создаёт новый массив с результатом вызова указанной функции //! \~russian Создаёт новый массив с результатом вызова указанной функции
@@ -2218,78 +2096,23 @@ public:
//! once for each element in an array, in order, //! once for each element in an array, in order,
//! and constructs a new array from the results. //! and constructs a new array from the results.
//! \~russian Метод `map` вызывает переданную функцию `ST f(const T & e)` //! \~russian Метод `map` вызывает переданную функцию `ST f(const T & e)`
//! один раз для каждого элемента в порядке их появления от начала к концу (слева на право) //! один раз для каждого элемента в порядке их появления
//! и конструирует новый массив из результатов её вызова. //! и конструирует новый массив из результатов её вызова.
//! \~\code //! \~\code
//! PIDeque<int> v{1, 2, 3}; //! PIDeque<int> v{1, 2, 3};
//! PIStringList sl = v.map<PIString>([](const int & i){return PIString::fromNumber(i);}); //! PIStringList sl = v.map<PIString>([](const int & i){return PIString::fromNumber(i);});
//! piCout << sl; // {"1", "2", "3"} //! piCout << sl; {"1", "2", "3"}
//! \endcode //! \endcode
//! \~\sa \a forEach(), \a reduce() //! \~\sa \a forEach(), \a reduce()
template<typename ST> template <typename ST>
inline PIDeque<ST> map(std::function<ST(const T & e)> f) const { inline PIDeque<ST> map(std::function<ST(const T & e)> f) const {
PIDeque<ST> ret; PIDeque<ST> ret; ret.reserve(pid_size);
ret.reserve(pid_size);
for (size_t i = pid_start; i < pid_start + pid_size; ++i) { for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
ret << f(pid_data[i]); ret << f(pid_data[i]);
} }
return ret; return ret;
} }
//! \~english Same as \a map() but with `index` parameter in `f`.
//! \~russian Аналогично \a map() но с параметром индекса `index` в функции `f`.
//! \~\code
//! PIDeque<int> v{1, 2, 3};
//! PIStringList sl = v.mapIndexed<PIString>([](size_t index, const int & i){return PIString::fromNumber(index);});
//! piCout << sl; // {"0", "1", "2"}
//! \endcode
//! \~\sa \a map()
template<typename ST>
inline PIDeque<ST> mapIndexed(std::function<ST(size_t index, const T & e)> f) const {
PIDeque<ST> ret;
ret.reserve(pid_size);
for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
ret << f(i - pid_start, pid_data[i]);
}
return ret;
}
//! \~english Same as \a map() but from end to begin (from right to left).
//! \~russian Аналогично \a map() но от конца до начала (справа на лево).
//! \~\code
//! PIDeque<int> v{1, 2, 3};
//! PIStringList sl = v.mapReverse<PIString>([](const int & i){return PIString::fromNumber(i);});
//! piCout << sl; // {"3", "2", "1"}
//! \endcode
//! \~\sa \a map()
template<typename ST>
inline PIDeque<ST> mapReverse(std::function<ST(const T & e)> f) const {
PIDeque<ST> ret;
ret.reserve(pid_size);
for (ssize_t i = ssize_t(pid_start + pid_size) - 1; i >= ssize_t(pid_start); --i) {
ret << f(pid_data[i]);
}
return ret;
}
//! \~english Same as \a mapReverse() but with `index` parameter in `f`.
//! \~russian Аналогично \a mapReverse() но с параметром индекса `index` в функции `f`.
//! \~\code
//! PIDeque<int> v{1, 2, 3};
//! PIStringList sl = v.mapReverseIndexed<PIString>([](size_t index, const int & i){return PIString::fromNumber(index);});
//! piCout << sl; // {"2", "1", "0"}
//! \endcode
//! \~\sa \a mapReverse()
template<typename ST>
inline PIDeque<ST> mapReverseIndexed(std::function<ST(size_t index, const T & e)> f) const {
PIDeque<ST> ret;
ret.reserve(pid_size);
for (ssize_t i = ssize_t(pid_start + pid_size) - 1; i >= ssize_t(pid_start); --i) {
ret << f(size_t(i) - pid_start, pid_data[i]);
}
return ret;
}
//! \~english Applies the function `ST f(const T & e, const ST & acc)` //! \~english Applies the function `ST f(const T & e, const ST & acc)`
//! to each element of the array (from left to right), returns one value. //! to each element of the array (from left to right), returns one value.
//! \~russian Применяет функцию `ST f(const T & e, const ST & acc)` //! \~russian Применяет функцию `ST f(const T & e, const ST & acc)`
@@ -2310,7 +2133,7 @@ public:
//! \param initial _optional_ Object used as the second argument //! \param initial _optional_ Object used as the second argument
//! when the `f` function is first called. //! when the `f` function is first called.
//! \~russian Метод reduce() выполняет функцию `f` //! \~russian Метод reduce() выполняет функцию `f`
//! один раз для каждого элемента, присутствующего в массиве от начала к концу (слева на право). //! один раз для каждого элемента, присутствующего в массиве.
//! Если при вызове reduce() передан аргумент `initial`, //! Если при вызове reduce() передан аргумент `initial`,
//! то при первом вызове функции `f` значение `acc` //! то при первом вызове функции `f` значение `acc`
//! будет равным значению `initial`. //! будет равным значению `initial`.
@@ -2330,8 +2153,8 @@ public:
//! int s = v.reduce<int>([](const int & e, const int & acc){return e + acc;}); //! int s = v.reduce<int>([](const int & e, const int & acc){return e + acc;});
//! piCout << s; // 15 //! piCout << s; // 15
//! \endcode //! \endcode
//! \~\sa \a reduceIndexed(), \a reduceReverse(), \a reduceReverseIndexed(), \a forEach(), \a map() //! \~\sa \a forEach(), \a map()
template<typename ST> template <typename ST>
inline ST reduce(std::function<ST(const T & e, const ST & acc)> f, const ST & initial = ST()) const { inline ST reduce(std::function<ST(const T & e, const ST & acc)> f, const ST & initial = ST()) const {
ST ret(initial); ST ret(initial);
for (size_t i = pid_start; i < pid_start + pid_size; ++i) { for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
@@ -2340,42 +2163,6 @@ public:
return ret; return ret;
} }
//! \~english Same as \a reduce() but with `index` parameter in `f`.
//! \~russian Аналогично \a reduce() но с параметром индекса `index` в функции `f`.
//! \~\sa \a reduce()
template<typename ST>
inline ST reduceIndexed(std::function<ST(size_t index, const T & e, const ST & acc)> f, const ST & initial = ST()) const {
ST ret(initial);
for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
ret = f(i - pid_start, pid_data[i], ret);
}
return ret;
}
//! \~english Same as \a reduce() but from end to begin (from right to left).
//! \~russian Аналогично \a reduce() но от конца до начала (справа на лево).
//! \~\sa \a reduce()
template<typename ST>
inline ST reduceReverse(std::function<ST(const T & e, const ST & acc)> f, const ST & initial = ST()) const {
ST ret(initial);
for (ssize_t i = ssize_t(pid_start + pid_size) - 1; i >= ssize_t(pid_start); --i) {
ret = f(pid_data[i], ret);
}
return ret;
}
//! \~english Same as \a reduceReverse() but with `index` parameter in `f`.
//! \~russian Аналогично \a reduceReverse() но с параметром индекса `index` в функции `f`.
//! \~\sa \a reduceReverse()
template<typename ST>
inline ST reduceReverseIndexed(std::function<ST(size_t index, const T & e, const ST & acc)> f, const ST & initial = ST()) const {
ST ret(initial);
for (ssize_t i = ssize_t(pid_start + pid_size) - 1; i >= ssize_t(pid_start); --i) {
ret = f(size_t(i) - pid_start, pid_data[i], ret);
}
return ret;
}
//! \~english Changes the dimension of the array, creates a two-dimensional array from a one-dimensional array. //! \~english Changes the dimension of the array, creates a two-dimensional array from a one-dimensional array.
//! \~russian Изменяет размерность массива, из одномерного массива создает двухмерный. //! \~russian Изменяет размерность массива, из одномерного массива создает двухмерный.
//! \~\details //! \~\details
@@ -2399,22 +2186,22 @@ public:
PIDeque<PIDeque<T>> ret; PIDeque<PIDeque<T>> ret;
if (isEmpty()) return ret; if (isEmpty()) return ret;
#ifndef NDEBUG #ifndef NDEBUG
if (rows * cols != pid_size) { if (rows*cols != pid_size) {
fprintf(stderr, "error with PIDeque<%s>::reshape\n", __PIP_TYPENAME__(T)); printf("error with PIDeque<%s>::reshape\n", __PIP_TYPENAME__(T));
} }
#endif #endif
assert(rows * cols == pid_size); assert(rows*cols == pid_size);
ret.expand(rows); ret.resize(rows);
if (order == ReshapeByRow) { if (order == ReshapeByRow) {
for (size_t r = 0; r < rows; r++) { for (size_t r = 0; r < rows; r++) {
ret[r] = PIDeque<T>(&(pid_data[r * cols]), cols); ret[r] = PIDeque<T>(&(pid_data[r*cols]), cols);
} }
} }
if (order == ReshapeByColumn) { if (order == ReshapeByColumn) {
for (size_t r = 0; r < rows; r++) { for (size_t r = 0; r < rows; r++) {
ret[r].expand(cols); ret[r].resize(cols);
for (size_t c = 0; c < cols; c++) { for (size_t c = 0; c < cols; c++) {
ret[r][c] = pid_data[c * rows + r]; ret[r][c] = pid_data[c*rows + r];
} }
} }
} }
@@ -2435,12 +2222,14 @@ public:
//! piCout << xv.flatten<int>(); // {1, 2, 3, 4, 5, 6} //! piCout << xv.flatten<int>(); // {1, 2, 3, 4, 5, 6}
//! \endcode //! \endcode
//! \~\sa \a map(), \a reduce(), \a reshape() //! \~\sa \a map(), \a reduce(), \a reshape()
template<typename C, typename std::enable_if<std::is_same<T, PIDeque<C>>::value, int>::type = 0> template<typename C, typename std::enable_if<
std::is_same<T, PIDeque<C>>::value
, int>::type = 0>
inline PIDeque<C> flatten(ReshapeOrder order = ReshapeByRow) const { inline PIDeque<C> flatten(ReshapeOrder order = ReshapeByRow) const {
PIDeque<C> ret; PIDeque<C> ret;
if (isEmpty()) return ret; if (isEmpty()) return ret;
const size_t rows = size(); size_t rows = size();
const size_t cols = at(0).size(); size_t cols = at(0).size();
ret.reserve(rows * cols); ret.reserve(rows * cols);
if (order == ReshapeByRow) { if (order == ReshapeByRow) {
for (size_t r = 0; r < rows; r++) { for (size_t r = 0; r < rows; r++) {
@@ -2476,269 +2265,153 @@ public:
//! piCout << xv.reshape<int>(2,3); // {{1, 2, 3}, {4, 5, 6}} //! piCout << xv.reshape<int>(2,3); // {{1, 2, 3}, {4, 5, 6}}
//! \endcode //! \endcode
//! \~\sa \a map(), \a reduce(), \a reshape() //! \~\sa \a map(), \a reduce(), \a reshape()
template<typename C, typename std::enable_if<std::is_same<T, PIDeque<C>>::value, int>::type = 0> template<typename C, typename std::enable_if<
std::is_same<T, PIDeque<C>>::value
, int>::type = 0>
inline PIDeque<PIDeque<C>> reshape(size_t rows, size_t cols, ReshapeOrder order = ReshapeByRow) const { inline PIDeque<PIDeque<C>> reshape(size_t rows, size_t cols, ReshapeOrder order = ReshapeByRow) const {
PIDeque<C> fl = flatten<C>(); PIDeque<C> fl = flatten<C>();
return fl.reshape(rows, cols, order); return fl.reshape(rows, cols, order);
} }
//! \~english Divides an array into a two-dimensional array using the separator `separator`.
//! \~russian Разделяет массив на двумерный массив с помощью разделителя`separator`.
//! \~\code
//! PIDeque<int> v{1, 2, 3, 99, 4, 5, 99, 6};
//! piCout << v.split(99); // {{1, 2, 3}, {4, 5}, {6}}
//! \endcode
//! \~\sa \a splitBySize()
inline PIDeque<PIDeque<T>> split(const T & separator) const {
PIDeque<PIDeque<T>> ret;
if (isEmpty()) return ret;
size_t start = 0;
ssize_t ci = indexOf(separator, start);
while (ci >= 0) {
ret << PIDeque<T>(pid_data + pid_start + start, ci - start);
start = ci + 1;
ci = indexOf(separator, start);
}
if (start < pid_size) {
ret << PIDeque<T>(pid_data + pid_start + start, pid_size - start);
}
return ret;
}
//! \~english Divides an array into a two-dimensional array in chunks of no more than `sz`.
//! \~russian Разделяет массив на двумерный массив по кускам не более чем `sz`.
//! \~\sa \a split()
inline PIDeque<PIDeque<T>> splitBySize(size_t sz) const {
PIDeque<PIDeque<T>> ret;
if (isEmpty() || sz == 0) return ret;
const size_t ch = pid_size / sz;
for (size_t i = 0; i < ch; ++i) {
ret << PIDeque<T>(pid_data + pid_start + sz * i, sz);
}
const size_t t = ch * sz;
if (t < pid_size) {
ret << PIDeque<T>(pid_data + pid_start + t, pid_size - t);
}
return ret;
}
//! \~english Cut sub-array of this array.
//! \~russian Вырезает подмассив, то есть кусок из текущего массива.
//! \~english
//! \param index - index of this array where sub-array starts
//! \param count - sub-array size
//! \~russian
//! \param index - индекс в текущем массиве, откуда начинётся подмассив
//! \param count - размер подмассива
//! \~\details
//! \~english
//! Index must be in range from `0` to `size()-1`.
//! If sub-array size more than this array size, than ends early.
//! \~russian
//! Индекс начала должен лежать в диапазоне от `0` до `size()-1`.
//! Если заданный размер подмассива превышает размер текущего массива,
//! то вернется подмассив меньшего размера (`size()-index-1`).
inline PIDeque<T> takeRange(size_t index, size_t count) {
PIDeque<T> ret;
if (index >= pid_size || count == 0) return ret;
if (index + count > pid_size) count = pid_size - index;
ret.alloc_forward(count);
memcpy(reinterpret_cast<void *>(ret.pid_data + ret.pid_start),
reinterpret_cast<const void *>(pid_data + pid_start + index),
count * sizeof(T));
const size_t os = pid_size - index - count;
if (os <= index) {
if (os > 0) {
memmove(reinterpret_cast<void *>(pid_data + pid_start + index),
reinterpret_cast<const void *>(pid_data + pid_start + index + count),
os * sizeof(T));
}
} else {
if (index > 0) {
memmove(reinterpret_cast<void *>(pid_data + pid_start + count),
reinterpret_cast<const void *>(pid_data + pid_start),
index * sizeof(T));
}
pid_start += count;
}
pid_size -= count;
return ret;
}
private: private:
inline void _reset() { inline void _reset() {pid_size = pid_rsize = pid_start = 0; pid_data = 0;}
pid_size = 0;
pid_rsize = 0;
pid_start = 0;
pid_data = nullptr;
}
inline size_t asize(ssize_t s) { inline size_t asize(ssize_t s) {
if (s <= 0) return 0; if (s <= 0) return 0;
if (pid_rsize * 2 >= size_t(s) && pid_rsize < size_t(s)) { if (pid_rsize + pid_rsize >= size_t(s) && pid_rsize < size_t(s)) {
return pid_rsize * 2; return pid_rsize + pid_rsize;
} }
size_t t = _PIContainerConstants<T>::minCountPoT(); ssize_t t = _PIContainerConstants<T>::minCountPoT(), s_ = s - 1;
s -= 1; while (s_ >> t)
while (s >> t) {
++t; ++t;
}
return (1 << t); return (1 << t);
} }
template<typename T1 = T, typename std::enable_if<
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable<T1>::value, int>::type = 0> !std::is_trivially_copyable<T1>::value
, int>::type = 0>
inline void newT(T * dst, const T * src, size_t s) { inline void newT(T * dst, const T * src, size_t s) {
PIINTROSPECTION_CONTAINER_USED(T, s) PIINTROSPECTION_CONTAINER_USED(T, s)
for (size_t i = 0; i < s; ++i) { for (size_t i = 0; i < s; ++i)
elementNew(dst + i, src[i]); elementNew(dst + i, src[i]);
}
} }
template<typename T1 = T, typename std::enable_if<
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0> std::is_trivially_copyable<T1>::value
, int>::type = 0>
inline void newT(T * dst, const T * src, size_t s) { inline void newT(T * dst, const T * src, size_t s) {
PIINTROSPECTION_CONTAINER_USED(T, s) PIINTROSPECTION_CONTAINER_USED(T, s)
memcpy(reinterpret_cast<void *>(dst), reinterpret_cast<const void *>(src), s * sizeof(T)); memcpy((void*)(dst), (const void*)(src), s * sizeof(T));
} }
template<typename T1 = T, typename std::enable_if<
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable<T1>::value, int>::type = 0> !std::is_trivially_copyable<T1>::value
, int>::type = 0>
inline void deleteT(T * d, size_t sz) { inline void deleteT(T * d, size_t sz) {
PIINTROSPECTION_CONTAINER_UNUSED(T, sz) PIINTROSPECTION_CONTAINER_UNUSED(T, sz)
if (d != nullptr) { if ((uchar*)d != 0) {
for (size_t i = 0; i < sz; ++i) { for (size_t i = 0; i < sz; ++i) {
elementDelete(d[i]); elementDelete(d[i]);
} }
} }
} }
template<typename T1 = T, typename std::enable_if<
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0> std::is_trivially_copyable<T1>::value
, int>::type = 0>
inline void deleteT(T * d, size_t sz) { inline void deleteT(T * d, size_t sz) {
PIINTROSPECTION_CONTAINER_UNUSED(T, sz) PIINTROSPECTION_CONTAINER_UNUSED(T, sz)
} }
template<typename T1 = T, typename std::enable_if<
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable<T1>::value, int>::type = 0> !std::is_trivially_copyable<T1>::value
inline void elementNew(T * to, const T & from) { , int>::type = 0>
new (to) T(from); inline void elementNew(T * to, const T & from) {new(to)T(from);}
} template<typename T1 = T, typename std::enable_if<
!std::is_trivially_copyable<T1>::value
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable<T1>::value, int>::type = 0> , int>::type = 0>
inline void elementNew(T * to, T && from) { inline void elementNew(T * to, T && from) {new(to)T(std::move(from));}
new (to) T(std::move(from)); template<typename T1 = T, typename std::enable_if<
} std::is_trivially_copyable<T1>::value
, int>::type = 0>
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0> inline void elementNew(T1 * to, const T & from) {(*to) = from;}
inline void elementNew(T1 * to, const T & from) { template<typename T1 = T, typename std::enable_if<
(*to) = from; std::is_trivially_copyable<T1>::value
} , int>::type = 0>
inline void elementNew(T * to, T && from) {(*to) = std::move(from);}
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0> template<typename T1 = T, typename std::enable_if<
inline void elementNew(T * to, T && from) { !std::is_trivially_copyable<T1>::value
(*to) = std::move(from); , int>::type = 0>
} inline void elementDelete(T & from) {from.~T();}
template<typename T1 = T, typename std::enable_if<
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable<T1>::value, int>::type = 0> std::is_trivially_copyable<T1>::value
inline void elementDelete(T & from) { , int>::type = 0>
from.~T();
}
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0>
inline void elementDelete(T & from) {} inline void elementDelete(T & from) {}
inline void dealloc() { inline void dealloc() {
if (pid_data != nullptr) { if ((uchar*)pid_data != 0) free((uchar*)pid_data);
free(reinterpret_cast<void *>(pid_data)); pid_data = 0;
pid_data = nullptr;
}
} }
inline void checkMove() { inline void checkMove() {
if (pid_size >= 4) { if (pid_size >= 4) {
if (pid_size < pid_rsize / 6) { if (pid_size < pid_rsize / 6) {
if (pid_start < (pid_size * 2) || ssize_t(pid_start) > (ssize_t(pid_rsize) - (ssize_t(pid_size) * 2))) { if (pid_start < ssize_t(pid_size + pid_size) || pid_start > (ssize_t(pid_rsize) - ssize_t(pid_size) - ssize_t(pid_size))) {
size_t ns = (pid_rsize - pid_size) / 2; ssize_t ns = (pid_rsize - pid_size) / 2;
if (pid_start != ns) { if (pid_start != ns) {
memmove(reinterpret_cast<void *>(pid_data + ns), memmove((void*)(pid_data + ns), (const void*)(pid_data + pid_start), pid_size * sizeof(T));
reinterpret_cast<const void *>(pid_data + pid_start),
pid_size * sizeof(T));
pid_start = ns; pid_start = ns;
} }
} }
} }
} else { } else {
const size_t ns = (pid_rsize - pid_size) / 2; ssize_t ns = (pid_rsize - pid_size) / 2;
if (pid_start != ns) { if (pid_start != ns) {
memmove(reinterpret_cast<void *>(pid_data + ns), memmove((void*)(pid_data + ns), (const void*)(pid_data + pid_start), pid_size * sizeof(T));
reinterpret_cast<const void *>(pid_data + pid_start),
pid_size * sizeof(T));
pid_start = ns; pid_start = ns;
} }
} }
} }
inline void alloc_forward(size_t new_size) { // direction == true -> alloc forward
inline void alloc_forward(size_t new_size) { if (pid_start + new_size <= pid_rsize) {
if (pid_start + new_size <= pid_rsize) { pid_size = new_size;
checkMove();
return;
}
pid_size = new_size; pid_size = new_size;
if (pid_start > 0) checkMove(); size_t as = asize(pid_start + new_size);
return; if (as != pid_rsize) {
} PIINTROSPECTION_CONTAINER_ALLOC(T, (as-pid_rsize))
pid_size = new_size; T * p_d = (T*)(realloc((void*)(pid_data), as*sizeof(T)));
const size_t as = asize(pid_start + new_size);
if (as != pid_rsize) {
PIINTROSPECTION_CONTAINER_ALLOC(T, (as - pid_rsize))
T * p_d = reinterpret_cast<T *>(realloc(reinterpret_cast<void *>(pid_data), as * sizeof(T)));
#ifndef NDEBUG #ifndef NDEBUG
if (!p_d) { if (!p_d) {
fprintf(stderr, "error with PIDeque<%s>::alloc\n", __PIP_TYPENAME__(T)); printf("error with PIDeque<%s>::alloc\n", __PIP_TYPENAME__(T));
} }
#endif #endif
assert(p_d); assert(p_d);
pid_data = p_d; pid_data = p_d;
pid_rsize = as; pid_rsize = as;
}
}
inline void alloc_backward(size_t new_size, ssize_t start_offset = 0) {
const size_t as = ssize_t(pid_start) + start_offset < 0 ? asize(pid_rsize - start_offset) : pid_rsize;
if (as > pid_rsize) {
T * td = reinterpret_cast<T *>(malloc(as * sizeof(T)));
const size_t ns = pid_start + as - pid_rsize;
PIINTROSPECTION_CONTAINER_ALLOC(T, (as - pid_rsize))
if (pid_rsize > 0 && pid_data != 0) {
memcpy(reinterpret_cast<void *>(td + ns), reinterpret_cast<const void *>(pid_data + pid_start), pid_size * sizeof(T));
dealloc();
} }
pid_data = td; }
pid_rsize = as; inline void alloc_backward(size_t new_size, ssize_t start_offset = 0) { //alloc backward
pid_start = ns; size_t as;
} if (pid_start + start_offset < 0) {
pid_start += start_offset; as = asize(pid_rsize - start_offset);
pid_size = new_size; } else {
checkMove(); as = pid_rsize;
}
if (as > pid_rsize) {
T * td = (T*)(malloc(as * sizeof(T)));
ssize_t ns = pid_start + as - pid_rsize;
PIINTROSPECTION_CONTAINER_ALLOC(T, (as-pid_rsize))
if (pid_rsize > 0 && pid_data != 0) {
memcpy((void*)(td + ns), (const void*)(pid_data + pid_start), pid_size * sizeof(T));
dealloc();
}
pid_data = td;
pid_rsize = as;
pid_start = ns;
}
pid_start += start_offset;
pid_size = new_size;
checkMove();
} }
inline void expand(size_t new_size, const T & e = T()) { T * pid_data;
const size_t os = pid_size; size_t pid_size, pid_rsize;
alloc_forward(new_size); ssize_t pid_start;
PIINTROSPECTION_CONTAINER_USED(T, (new_size - os))
for (size_t i = os + pid_start; i < new_size + pid_start; ++i) {
elementNew(pid_data + i, e);
}
}
inline void expand(size_t new_size, std::function<T(size_t i)> f) {
const size_t os = pid_size;
alloc_forward(new_size);
PIINTROSPECTION_CONTAINER_USED(T, (new_size - os))
for (size_t i = os + pid_start; i < new_size + pid_start; ++i) {
elementNew(pid_data + i, f(i));
}
}
T * pid_data = nullptr;
size_t pid_size = 0;
size_t pid_rsize = 0;
size_t pid_start = 0;
}; };
@@ -2746,7 +2419,7 @@ private:
//! \~english Output operator to [std::ostream](https://en.cppreference.com/w/cpp/io/basic_ostream). //! \~english Output operator to [std::ostream](https://en.cppreference.com/w/cpp/io/basic_ostream).
//! \~russian Оператор вывода в [std::ostream](https://ru.cppreference.com/w/cpp/io/basic_ostream). //! \~russian Оператор вывода в [std::ostream](https://ru.cppreference.com/w/cpp/io/basic_ostream).
template<typename T> template<typename T>
inline std::ostream & operator<<(std::ostream & s, const PIDeque<T> & v) { inline std::ostream & operator <<(std::ostream & s, const PIDeque<T> & v) {
s << "{"; s << "{";
for (size_t i = 0; i < v.size(); ++i) { for (size_t i = 0; i < v.size(); ++i) {
s << v[i]; s << v[i];
@@ -2762,23 +2435,21 @@ inline std::ostream & operator<<(std::ostream & s, const PIDeque<T> & v) {
//! \~english Output operator to \a PICout //! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout //! \~russian Оператор вывода в \a PICout
template<typename T> template<typename T>
inline PICout operator<<(PICout s, const PIDeque<T> & v) { inline PICout operator <<(PICout s, const PIDeque<T> & v) {
s.space(); s.space();
s.saveAndSetControls(0); s.setControl(0, true);
s << "{"; s << "{";
for (size_t i = 0; i < v.size(); ++i) { for (size_t i = 0; i < v.size(); ++i) {
s << v[i]; s << v[i];
if (i < v.size() - 1) s << ", "; if (i < v.size() - 1) s << ", ";
} }
s << "}"; s << "}";
s.restoreControls(); s.restoreControl();
return s; return s;
} }
template<typename T> template<typename T>
inline void piSwap(PIDeque<T> & f, PIDeque<T> & s) { inline void piSwap(PIDeque<T> & f, PIDeque<T> & s) {f.swap(s);}
f.swap(s);
}
#endif // PIDEQUE_H #endif // PIDEQUE_H

View File

@@ -0,0 +1,187 @@
/** \class PIMap
* \brief Associative array
* \details This class used to store Key = Value array of any
* type of data. \a value() returns value for key and leave map
* unchaged in any case. \a operator [] create entry in map if
* there is no entry for given key. You can retrieve all
* keys by method \a keys() and all values by methos \a values().
* To iterate all entries use class PIMapIterator, or methods
* \a makeIterator() and \a makeReverseIterator().
* \fn PIMap::PIMap();
* \brief Contructs an empty map
* \fn PIMap::PIMap(const PIMap & other);
* \brief Contructs a copy of "other"
* \fn PIMap & PIMap::operator =(const PIMap & other);
* \brief Copy operator
* \fn PIMap::PIMap(const PIMap & other);
* \brief Contructs a copy of "other"
* \fn PIMapIterator PIMap::makeIterator() const
* \brief Returns PIMapIterator for this map
* \fn PIMapIterator PIMap::makeReverseIterator() const
* \brief Returns reverse PIMapIterator for this map
* \fn size_t PIMap::size() const
* \brief Returns entries count
* \fn int PIMap::size_s() const
* \brief Returns entries count
* \fn size_t PIMap::length() const
* \brief Returns entries count
* \fn bool PIMap::isEmpty() const
* \brief Returns if map is empty
* \fn T & PIMap::operator [](const Key & key)
* \brief Returns value for key "key". If there is no key in map, create one.
* \fn const T PIMap::operator [](const Key & key) const
* \brief Returns value for key "key". If there is no key in map, returns default T().
* \fn T & PIMap::at(const Key & key)
* \brief Equivalent to operator []
* \fn const T PIMap::at(const Key & key) const
* \brief Equivalent to operator []
* \fn PIMap & PIMap::operator <<(const PIMap & other)
* \brief Insert all etries of "other" to this map. Override existing values.
* \fn bool PIMap::operator ==(const PIMap & t) const
* \brief Compare operator
* \fn bool PIMap::operator !=(const PIMap & t) const
* \brief Compare operator
* \fn bool PIMap::contains(const Key & key) const
* \brief Returns "true" if map contains entry with key "key"
* \fn PIMap & PIMap::reserve(size_t new_size)
* \brief Reserve space for "new_size" entries
* \fn PIMap & PIMap::removeOne(const Key & key)
* \brief Remove entry with key "key"
* \fn PIMap & PIMap::remove(const Key & key)
* \brief Equivalent \a removeOne(key)
* \fn PIMap & PIMap::erase(const Key & key)
* \brief Equivalent \a removeOne(key)
* \fn PIMap & PIMap::clear()
* \brief Clear map
* \fn void PIMap::swap(PIMap & other)
* \brief Swap map with "other"
* \fn PIMap & PIMap::insert(const Key & key, const T & value)
* \brief Insert or rewrite entry with key "key" and value "value"
* \fn const T PIMap::value(const Key & key, const T & default = T())
* \brief Returns value for key "key". If there is no key in map, returns "default".
* \fn PIVector<T> PIMap::values() const
* \brief Returns all values as PIVector
* \fn Key PIMap::key(const T & value, const Key & default = Key()) const
* \brief Returns key for first founded value "value". If there is no such value in map, returns "default".
* \fn PIVector<Key> PIMap::keys() const
* \brief Returns all keys as PIVector
* */
/** \class PIMapIterator
* \brief Helper class to iterate over PIMap
* \details This class used to access keys and values in PIMap.
* You can use constructor to create iterator, or use \a PIMap::makeIterator()
* and \a PIMap::makeReverseIterator() methods.
*
* First usage variant:
* \code
* PIMap<int, PIString> m;
* m[1] = "one";
* m[2] = "two";
* m[4] = "four";
*
* auto it = m.makeIterator();
* while (it.next()) {
* piCout << it.key() << it.value();
* }
* // 1 one
* // 2 two
* // 4 four
* \endcode
*
* Using hasNext():
* \code
* while (it.hasNext()) {
* it.next();
* \endcode
*
* Using constructor:
* \code
* PIMapIterator<int, PIString> it(m);
* \endcode
*
* Write access:
* \code
* while (it.next()) {
* it.valueRef().append("_!");
* piCout << it.key() << it.value();
* }
*
* // 1 one_!
* // 2 two_!
* // 4 four_!
* \endcode
*
* Reverse iterator:
* \code
* auto it = m.makeReverseIterator();
* while (it.next()) {
* piCout << it.key() << it.value();
* }
*
* // 4 four
* // 2 two
* // 1 one
* \endcode
* \fn PIMapIterator(const PIMap & map, bool reverse = false)
* \brief Contructs iterator for "map". Current position is invalid.
* \fn const Key & PIMapIterator::key() const
* \brief Returns current entry key
* \fn const T & PIMapIterator::value() const
* \brief Returns current entry value
* \fn T & PIMapIterator::valueRef() const
* \brief Returns reference to current entry value
* \fn bool PIMapIterator::hasNext()
* \brief Returns if iterator can jump to next entry
* \fn bool PIMapIterator::next()
* \brief Jump to next entry and return if new position is valid.
* \fn void PIMapIterator::reset()
* \brief Reset iterator to initial position.
* */

View File

@@ -1,1010 +1,390 @@
//! \addtogroup Containers /*! \file pimap.h
//! \{ * \brief Associative array with custom types of key and value
//! \file pimap.h *
//! \brief * This file declares PIMap
//! \~english Declares \a PIMap */
//! \~russian Объявление \a PIMap
//! \~\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 PIP - Platform Independent Primitives
Associative array with custom types of key and value Associative array with custom types of key and value
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PIMAP_H #ifndef PIMAP_H
#define PIMAP_H #define PIMAP_H
#include "pivector.h"
#include "pideque.h" #include "pideque.h"
#include "pipair.h" #include "pipair.h"
#include "pivector.h"
template<typename Key, typename T> template <typename Key, typename T>
class PIMapIteratorConst;
template<typename Key, typename T>
class PIMapIteratorConstReverse;
template<typename Key, typename T>
class PIMapIterator; class PIMapIterator;
template<typename Key, typename T>
class PIMapIteratorReverse;
//! \addtogroup Containers template <typename Key, typename T>
//! \{
//! \class PIMap
//! \brief
//! \~english Associative array.
//! \~russian Словарь.
//! \~\}
//! \details
//! \~english
//! A collection of key/value pairs, from which you retrieve a value using its associated key.
//! There is a finite number of keys in the map, and each key has exactly one value associated with it.
//! \a value() returns value for key and leave map
//! unchaged in any case. \a operator [] create entry in map if
//! there is no entry for given key. You can retrieve all
//! keys by method \a keys() and all values by methos \a values().
//! To iterate all entries use class PIMapIterator, or methods
//! \a makeIterator() and \a makeReverseIterator().
//! A key in the Map may only occur once.
//! \~russian
//! Словари, в принципе, похожи на обычные, используемые в повседневной жизни.
//! Они хранят элементы одного и того же типа, индексируемые ключевыми значениями.
//! Достоинство словаря в том, что он позволяет быстро получать значение,
//! ассоциированное с заданным ключом.
//! Ключи должны быть уникальными.
//! Элемент
//! В контейнеры этого типа заносятся элементы вместе с ключами,
//! по которым их можно найти, которыми могут выступать значения любого типа.
//! \a operator [] позволяет получить доступ к элементу по ключу,
//! и если такого эелемента не было, то он будет создан.
template<typename Key, typename T>
class PIMap { class PIMap {
template<typename Key1, typename T1> template <typename Key1, typename T1> friend PIByteArray & operator >>(PIByteArray & s, PIMap<Key1, T1> & v);
friend class PIMapIteratorConst; template <typename Key1, typename T1> friend PIByteArray & operator <<(PIByteArray & s, const PIMap<Key1, T1> & v);
template<typename Key1, typename T1> template <typename Key1, typename T1> friend class PIMapIterator;
friend class PIMapIteratorConstReverse;
template<typename Key1, typename T1>
friend class PIMapIterator;
template<typename Key1, typename T1>
friend class PIMapIteratorReverse;
template<typename P, typename Key1, typename T1>
friend PIBinaryStream<P> & operator<<(PIBinaryStream<P> & s, const PIMap<Key1, T1> & v);
template<typename P, typename Key1, typename T1>
friend PIBinaryStream<P> & operator>>(PIBinaryStream<P> & s, PIMap<Key1, T1> & v);
public: public:
typedef T mapped_type; PIMap() {}
typedef Key key_type; PIMap(const PIMap<Key, T> & other) {*this = other;}
typedef PIPair<Key, T> value_type; PIMap(PIMap<Key, T> && other) : pim_content(std::move(other.pim_content)) {}
PIMap(std::initializer_list<std::pair<Key, T>> init_list) {
//! \~english Constructs an empty map. for (auto i: init_list)
//! \~russian Создает пустой словарь.
inline PIMap() {}
//! \~english Copy constructor.
//! \~russian Копирующий конструктор.
inline PIMap(const PIMap<Key, T> & other): pim_content(other.pim_content), pim_index(other.pim_index) {}
//! \~english Move constructor.
//! \~russian Перемещающий конструктор.
inline PIMap(PIMap<Key, T> && other): pim_content(std::move(other.pim_content)), pim_index(std::move(other.pim_index)) {}
//! \~english Contructs map from
//! [C++11 initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list).
//! \~russian Создает словарь из
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\details
//! \~\code
//! PIMap <int, PIString> m{{1, "a"}, {2, "b"}};
//! piCout << m; // {1: a, 2: b}
//! \endcode
inline PIMap(std::initializer_list<std::pair<Key, T>> init_list) {
for (auto i: init_list) {
insert(std::get<0>(i), std::get<1>(i)); insert(std::get<0>(i), std::get<1>(i));
}
} }
virtual ~PIMap() {;}
//! \~english Assign operator. PIMap<Key, T> & operator =(const PIMap<Key, T> & other) {
//! \~russian Оператор присваивания.
inline PIMap<Key, T> & operator=(const PIMap<Key, T> & other) {
if (this == &other) return *this; if (this == &other) return *this;
clear(); clear();
pim_content = other.pim_content; pim_content = other.pim_content;
pim_index = other.pim_index;
return *this; return *this;
} }
//! \~english Assign move operator. PIMap<Key, T> & operator =(PIMap<Key, T> && other) {
//! \~russian Оператор перемещающего присваивания.
inline PIMap<Key, T> & operator=(PIMap<Key, T> && other) {
swap(other); swap(other);
return *this; return *this;
} }
typedef T mapped_type;
typedef Key key_type;
typedef PIPair<Key, T> value_type;
class iterator { class iterator {
friend class PIMap<Key, T>; friend class PIMap<Key, T>;
private: private:
iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {} iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
const PIMap<Key, T> * parent; const PIMap<Key, T> * parent;
ssize_t pos; ssize_t pos;
public: public:
iterator(): parent(nullptr), pos(0) {} iterator(): parent(0), pos(0) {}
const Key & key() const { return const_cast<PIMap<Key, T> *>(parent)->_key(pos); } const Key & key() const {return const_cast<PIMap<Key, T> * >(parent)->_key(pos);}
T & value() { return const_cast<PIMap<Key, T> *>(parent)->_value(pos); } T & value() {return const_cast<PIMap<Key, T> * >(parent)->_value(pos);}
inline PIPair<Key, T> operator*() const { inline PIPair<Key, T> operator *() const {return PIPair<Key, T>(const_cast<PIMap<Key, T> * >(parent)->_key(pos), const_cast<PIMap<Key, T> * >(parent)->_value(pos));}
return PIPair<Key, T>(const_cast<PIMap<Key, T> *>(parent)->_key(pos), const_cast<PIMap<Key, T> *>(parent)->_value(pos)); void operator ++() {++pos;}
} void operator ++(int) {++pos;}
void operator++() { ++pos; } void operator --() {--pos;}
void operator++(int) { ++pos; } void operator --(int) {--pos;}
void operator--() { --pos; } bool operator ==(const iterator & it) const {return (pos == it.pos);}
void operator--(int) { --pos; } bool operator !=(const iterator & it) const {return (pos != it.pos);}
bool operator==(const iterator & it) const { return (pos == it.pos); }
bool operator!=(const iterator & it) const { return (pos != it.pos); }
}; };
class reverse_iterator { class reverse_iterator {
friend class PIMap<Key, T>; friend class PIMap<Key, T>;
private: private:
reverse_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {} reverse_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
const PIMap<Key, T> * parent; const PIMap<Key, T> * parent;
ssize_t pos; ssize_t pos;
public: public:
reverse_iterator(): parent(nullptr), pos(0) {} reverse_iterator(): parent(0), pos(0) {}
const Key & key() const { return const_cast<PIMap<Key, T> *>(parent)->_key(pos); } const Key & key() const {return const_cast<PIMap<Key, T> * >(parent)->_key(pos);}
T & value() const { return const_cast<PIMap<Key, T> *>(parent)->_value(pos); } T & value() const {return const_cast<PIMap<Key, T> * >(parent)->_value(pos);}
inline PIPair<Key, T> operator*() const { inline PIPair<Key, T> operator *() const {return PIPair<Key, T>(const_cast<PIMap<Key, T> * >(parent)->_key(pos), const_cast<PIMap<Key, T> * >(parent)->_value(pos));}
return PIPair<Key, T>(const_cast<PIMap<Key, T> *>(parent)->_key(pos), const_cast<PIMap<Key, T> *>(parent)->_value(pos)); void operator ++() {--pos;}
} void operator ++(int) {--pos;}
void operator++() { --pos; } void operator --() {++pos;}
void operator++(int) { --pos; } void operator --(int) {++pos;}
void operator--() { ++pos; } bool operator ==(const reverse_iterator & it) const {return (pos == it.pos);}
void operator--(int) { ++pos; } bool operator !=(const reverse_iterator & it) const {return (pos != it.pos);}
bool operator==(const reverse_iterator & it) const { return (pos == it.pos); }
bool operator!=(const reverse_iterator & it) const { return (pos != it.pos); }
}; };
class const_iterator { class const_iterator {
friend class PIMap<Key, T>; friend class PIMap<Key, T>;
private: private:
const_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {} const_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
const PIMap<Key, T> * parent; const PIMap<Key, T> * parent;
ssize_t pos; ssize_t pos;
public: public:
const_iterator(): parent(nullptr), pos(0) {} const_iterator(): parent(0), pos(0) {}
const value_type operator*() const { return parent->_pair(pos); } const value_type operator *() const {return parent->_pair(pos);}
const Key & key() const { return const_cast<PIMap<Key, T> *>(parent)->_key(pos); } const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;}
const T & value() const { return const_cast<PIMap<Key, T> *>(parent)->_value(pos); } const Key & key() const {return const_cast<PIMap<Key, T> * >(parent)->_key(pos);}
void operator++() { ++pos; } const T & value() const {return const_cast<PIMap<Key, T> * >(parent)->_value(pos);}
void operator++(int) { ++pos; } void operator ++() {++pos;}
void operator--() { --pos; } void operator ++(int) {++pos;}
void operator--(int) { --pos; } void operator --() {--pos;}
bool operator==(const const_iterator & it) const { return (pos == it.pos); } void operator --(int) {--pos;}
bool operator!=(const const_iterator & it) const { return (pos != it.pos); } bool operator ==(const const_iterator & it) const {return (pos == it.pos);}
bool operator !=(const const_iterator & it) const {return (pos != it.pos);}
mutable value_type cval;
}; };
class const_reverse_iterator { class const_reverse_iterator {
friend class PIMap<Key, T>; friend class PIMap<Key, T>;
private: private:
const_reverse_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {} const_reverse_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
const PIMap<Key, T> * parent; const PIMap<Key, T> * parent;
ssize_t pos; ssize_t pos;
public: public:
const_reverse_iterator(): parent(nullptr), pos(0) {} const_reverse_iterator(): parent(0), pos(0) {}
const value_type operator*() const { return parent->_pair(pos); } const value_type operator *() const {return parent->_pair(pos);}
void operator++() { --pos; } const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;}
void operator++(int) { --pos; } void operator ++() {--pos;}
void operator--() { ++pos; } void operator ++(int) {--pos;}
void operator--(int) { ++pos; } void operator --() {++pos;}
bool operator==(const const_reverse_iterator & it) const { return (pos == it.pos); } void operator --(int) {++pos;}
bool operator!=(const const_reverse_iterator & it) const { return (pos != it.pos); } bool operator ==(const const_reverse_iterator & it) const {return (pos == it.pos);}
bool operator !=(const const_reverse_iterator & it) const {return (pos != it.pos);}
mutable value_type cval;
}; };
iterator begin() {return iterator(this, 0);}
iterator end() {return iterator(this, size());}
const_iterator begin() const {return const_iterator(this, 0);}
const_iterator end() const {return const_iterator(this, size());}
const_iterator constBegin() const {return const_iterator(this, 0);}
const_iterator constEnd() const {return const_iterator(this, size());}
reverse_iterator rbegin() {return reverse_iterator(this, size() - 1);}
reverse_iterator rend() {return reverse_iterator(this, -1);}
const_reverse_iterator rbegin() const {return const_reverse_iterator(this, size() - 1);}
const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);}
const_reverse_iterator constRbegin() const {return const_reverse_iterator(this, size() - 1);}
const_reverse_iterator constRend() const {return const_reverse_iterator(this, -1);}
//! \~english Iterator to the first element. PIMapIterator<Key, T> makeIterator() const {return PIMapIterator<Key, T>(*this);}
//! \~russian Итератор на первый элемент. PIMapIterator<Key, T> makeReverseIterator() const {return PIMapIterator<Key, T>(*this, true);}
inline iterator begin() { return iterator(this, 0); }
//! \~english Iterator to the element following the last element. size_t size() const {return pim_content.size();}
//! \~russian Итератор на элемент, следующий за последним элементом. int size_s() const {return pim_content.size_s();}
inline iterator end() { return iterator(this, size()); } size_t length() const {return pim_content.size();}
bool isEmpty() const {return (pim_content.size() == 0);}
inline const_iterator begin() const { return const_iterator(this, 0); }
inline const_iterator end() const { return const_iterator(this, size()); } T & operator [](const Key & key) {
//! \~english Returns a reverse iterator to the first element of the reversed array.
//! \~russian Обратный итератор на первый элемент.
inline reverse_iterator rbegin() { return reverse_iterator(this, size() - 1); }
//! \~english Returns a reverse iterator to the element.
//! following the last element of the reversed array.
//! \~russian Обратный итератор на элемент,
//! следующий за последним элементом.
inline reverse_iterator rend() { return reverse_iterator(this, -1); }
inline const_reverse_iterator rbegin() const { return const_reverse_iterator(this, size() - 1); }
inline const_reverse_iterator rend() const { return const_reverse_iterator(this, -1); }
//! \relatesalso PIMapIteratorConst
inline PIMapIteratorConst<Key, T> makeIterator() const { return PIMapIteratorConst<Key, T>(*this); }
//! \relatesalso PIMapIterator
inline PIMapIterator<Key, T> makeIterator() { return PIMapIterator<Key, T>(*this); }
//! \relatesalso PIMapIteratorConstReverse
inline PIMapIteratorConstReverse<Key, T> makeReverseIterator() const { return PIMapIteratorConstReverse<Key, T>(*this); }
//! \relatesalso PIMapIteratorReverse
inline PIMapIteratorReverse<Key, T> makeReverseIterator() { return PIMapIteratorReverse<Key, T>(*this); }
//! \~english Number of elements in the container.
//! \~russian Количество элементов массива.
//! \~\sa \a size_s(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline size_t size() const { return pim_content.size(); }
//! \~english Number of elements in the container as signed value.
//! \~russian Количество элементов массива в виде знакового числа.
//! \~\sa \a size(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline int size_s() const { return pim_content.size_s(); }
//! \~english Same as \a size().
//! \~russian Синоним \a size().
//! \~\sa \a size(), \a size_s(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline size_t length() const { return pim_content.size(); }
//! \~english Checks if the container has no elements.
//! \~russian Проверяет пуст ли массив.
//! \~\return
//! \~english **true** if the container is empty, **false** otherwise
//! \~russian **true** если массив пуст, **false** иначе.
//! \~\sa \a size(), \a size_s(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline bool isEmpty() const { return (pim_content.size() == 0); }
//! \~english Checks if the container has elements.
//! \~russian Проверяет не пуст ли массив.
//! \~\return
//! \~english **true** if the container is not empty, **false** otherwise
//! \~russian **true** если массив не пуст, **false** иначе.
//! \~\sa \a size(), \a size_s(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline bool isNotEmpty() const { return (pim_content.size() > 0); }
//! \~english Full access to element key `key`.
//! \~russian Полный доступ к элементу по ключу `key`.
//! \~\details
//! \~english If the map contains no item with key `key`,
//! the function inserts a default-constructed value into the map with key `key`,
//! and returns a reference to it.
//! \~russian Если элемента с таким ключом `key` не существует,
//! то он будет создан конструктором по умолчанию и добавлен в массив
//! по ключу `key`, а затем возвращена ссылка на этот новый элемент.
//! \~\code
//! PIMap <PIString, int> m;
//! m["огурец"] = 500;
//! piCout << m; // {огурец: 500}
//! m["лук"] = 25;
//! piCout << m; // {огурец: 500, лук: 25}
//! m["огурец"] = 350;
//! piCout << m; // {огурец: 350, лук: 25}
//! \endcode
//! \~\sa \a insert(), \a value(), \a key()
inline T & operator[](const Key & key) {
bool f(false); bool f(false);
const ssize_t i = _find(key, f); ssize_t i = _find(key, f);
if (f) return _value(i); if (!f) pim_content.insert(i, PIPair<Key, T>(key, T()));
pim_content.push_back(T()); return pim_content[i].second;
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
return pim_content.back();
} }
const T operator [](const Key & key) const {
//! \~english Read only access to element by `key`.
//! \~russian Доступ исключительно на чтение к элементу по ключу `key`.
//! \~\note
//! \~english Element with key `key` must exists,
//! otherwise it will lead to undefined program behavior and memory errors.
//! \~russian Элемент по ключу `key` должен существовать,
//! иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
//! \~\sa \a operator[](), \a value(), \a key()
inline const T & at(const Key & key) const {
bool f(false); bool f(false);
const ssize_t i = _find(key, f); ssize_t i = _find(key, f);
return _value(i); if (f) return pim_content[i].second;
return T();
} }
const T at(const Key & key) const {return (*this)[key];}
//! \~english Remove element with key `key` from the array and return it.
//! \~russian Удаляет элемент с ключом `key` из массива и возвращает его. PIMap<Key, T> & operator <<(const PIMap<Key, T> & other) {
inline T take(const Key & key, const T & default_ = T()) {
bool f(false);
const ssize_t i = _find(key, f);
if (!f) return default_;
const T ret = _value(i);
_remove(i);
return ret;
}
//! \~english Inserts all elements in array `other` to this array with overwrite.
//! \~russian Вставляет все элементы `other` этот массив с перезаписью.
inline PIMap<Key, T> & operator<<(const PIMap<Key, T> & other) {
#ifndef NDEBUG #ifndef NDEBUG
if (&other == this) { if (&other == this) {
fprintf(stderr, "error with PIMap<%s, %s>::<<\n", __PIP_TYPENAME__(Key), __PIP_TYPENAME__(T)); printf("error with PIMap<%s, %s>::<<\n", __PIP_TYPENAME__(Key), __PIP_TYPENAME__(T));
} }
#endif #endif
assert(&other != this); assert(&other != this);
if (other.isEmpty()) return *this; if (other.isEmpty()) return *this;
if (other.size() == 1) { // if (other.size() == 1) {insert(other.pim_index[0].key, other.pim_content[0]); return *this;}
insert(other.pim_index[0].key, other.pim_content[0]); // if (other.size() == 2) {insert(other.pim_index[0].key, other.pim_content[0]); insert(other.pim_index[1].key, other.pim_content[1]); return *this;}
return *this; for (int i = 0; i < other.pim_content.size_s(); ++i)
} insert(other.pim_content[i].first, other.pim_content[i].second);
if (other.size() == 2) {
insert(other.pim_index[0].key, other.pim_content[0]);
insert(other.pim_index[1].key, other.pim_content[1]);
return *this;
}
for (int i = 0; i < other.pim_index.size_s(); ++i) {
insert(other.pim_index[i].key, other.pim_content[other.pim_index[i].index]);
}
return *this; return *this;
} }
//! \~english Compare operator with array `m`. bool operator ==(const PIMap<Key, T> & t) const {return (pim_content == t.pim_content);}
//! \~russian Оператор сравнения с массивом `m`. bool operator !=(const PIMap<Key, T> & t) const {return (pim_content != t.pim_content);}
inline bool operator==(const PIMap<Key, T> & m) const { return (pim_content == m.pim_content && pim_index == m.pim_index); } bool contains(const Key & key) const {bool f(false); _find(key, f); return f;}
//! \~english Compare operator with array `m`. PIMap<Key, T> & reserve(size_t new_size) {pim_content.reserve(new_size);return *this;}
//! \~russian Оператор сравнения с массивом `m`.
inline bool operator!=(const PIMap<Key, T> & m) const { return (pim_content != m.pim_content || pim_index != m.pim_index); }
//! \~english Tests if element with key `key` exists in the array. PIMap<Key, T> & removeOne(const Key & key) {bool f(false); ssize_t i = _find(key, f); if (f) _remove(i); return *this;}
//! \~russian Проверяет наличие элемента с ключом `key` в массиве. PIMap<Key, T> & remove(const Key & key) {return removeOne(key);}
inline bool contains(const Key & key) const { PIMap<Key, T> & erase(const Key & key) {return removeOne(key);}
bool f(false); PIMap<Key, T> & clear() {pim_content.clear(); return *this;}
_find(key, f);
return f; void swap(PIMap<Key, T> & other) {
}
//! \~english Tests if element with value `value` exists in the array.
//! \~russian Проверяет наличие элемента со значением `value` в массиве.
inline bool containsValue(const T & value) const { return pim_content.contains(value); }
//! \~english Attempts to allocate memory for at least `new_size` elements.
//! \~russian Резервируется память под как минимум `new_size` элементов.
inline PIMap<Key, T> & reserve(size_t new_size) {
pim_content.reserve(new_size);
pim_index.reserve(new_size);
return *this;
}
//! \~english Remove element with key `key` from the array.
//! \~russian Удаляет элемент с ключом `key` из массива.
inline PIMap<Key, T> & remove(const Key & key) {
bool f(false);
const ssize_t i = _find(key, f);
if (f) _remove(i);
return *this;
}
//! \~english Remove all elements in the array
//! passes the test implemented by the provided function `test`.
//! \~russian Удаляет все элементы, удовлетворяющие условию,
//! заданному в передаваемой функции `test`.
inline PIMap<Key, T> & removeWhere(std::function<bool(const Key & key, const T & value)> test) {
for (int i = 0; i < pim_index.size_s(); ++i) {
const auto & mi(pim_index[i]);
if (test(mi.key, pim_content[mi.index])) {
_remove(i);
--i;
}
}
return *this;
}
//! \~english Same as \a remove().
//! \~russian Синоним функции \a remove().
inline PIMap<Key, T> & erase(const Key & key) { return remove(key); }
//! \~english Clear array, remove all elements.
//! \~russian Очищает массив, удаляет все элементы.
//! \~\details
//! \~\note
//! \~english Reserved memory will not be released.
//! \~russian Зарезервированная память не освободится.
//! \~\sa \a resize()
inline PIMap<Key, T> & clear() {
pim_content.clear();
pim_index.clear();
return *this;
}
//! \~english Swaps array `v` other with this array.
//! \~russian Меняет местами массив `v` с этим массивом.
//! \~\details
//! \~english This operation is very fast and never fails.
//! \~russian Эта операция выполняется мгновенно без копирования памяти и никогда не дает сбоев.
inline void swap(PIMap<Key, T> & other) {
pim_content.swap(other.pim_content); pim_content.swap(other.pim_content);
pim_index.swap(other.pim_index);
} }
//! \~english Inserts value `value` with key `key` in the array. PIMap<Key, T> & insert(const Key & key, const T & value) {
//! \~russian Вставляет значение `value` с ключом `key` в массив.
//! \~\details
//! \~english If an element with the key `key` already exists, it will be overwritten with the value `value`.
//! \~russian Если элемент с ключом `key` уже существует, то он будет перезаписан на значение `value`.
inline PIMap<Key, T> & insert(const Key & key, const T & value) {
bool f(false); bool f(false);
const ssize_t i = _find(key, f); ssize_t i = _find(key, f);
//piCout << "insert key=" << key << "found=" << f << "index=" << i << "value=" << value;
if (f) { if (f) {
_value(i) = value; pim_content[i].second = value;
} else { } else {
pim_content.push_back(value); pim_content.insert(i, PIPair<Key, T>(key, value));
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
} }
return *this; return *this;
} }
PIMap<Key, T> & insert(const Key & key, T && value) {
inline PIMap<Key, T> & insert(const Key & key, T && value) {
bool f(false); bool f(false);
const ssize_t i = _find(key, f); ssize_t i = _find(key, f);
//piCout << "insert key=" << key << "found=" << f << "index=" << i << "value=" << value;
if (f) { if (f) {
_value(i) = std::move(value); pim_content[i].second = std::move(value);
} else { } else {
pim_content.push_back(std::move(value)); // pim_content.push_back(std::move(value));
pim_index.insert(i, MapIndex(key, pim_content.size() - 1)); // pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
pim_content.insert(i, PIPair<Key, T>(key, std::move(value)));
} }
return *this; return *this;
} }
const T value(const Key & key, const T & default_ = T()) const {
//! \~english Inserts value `pair` in the array.
//! \~russian Вставляет пару `pair` в массив.
//! \~\details
//! \~english The first element of the pair is the key, and the second is the value.
//! \~russian Первый элемент пары является ключом, а второй значением.
inline PIMap<Key, T> & insert(const PIPair<Key, T> & pair) {
bool f(false); bool f(false);
const ssize_t i = _find(pair.first, f); ssize_t i = _find(key, f);
if (f) {
_value(i) = pair.second;
} else {
pim_content.push_back(pair.second);
pim_index.insert(i, MapIndex(pair.first, pim_content.size() - 1));
}
return *this;
}
inline PIMap<Key, T> & insert(PIPair<Key, T> && pair) {
bool f(false);
Key k(std::move(pair.first));
const ssize_t i = _find(k, f);
if (f) {
_value(i) = std::move(pair.second);
} else {
pim_content.push_back(std::move(pair.second));
pim_index.insert(i, MapIndex(k, pim_content.size() - 1));
}
return *this;
}
//! \~english Returns the value of the element by the key `key`
//! or `default_` if there is no such element.
//! \~russian Возвращает значение элемента по ключу `key`
//! или `default_` если такого элемента нет.
inline T value(const Key & key, const T & default_ = T()) const {
bool f(false);
const ssize_t i = _find(key, f);
if (!f) return default_; if (!f) return default_;
return _value(i); return pim_content[i].second;
} }
PIVector<T> values() const {
//! \~english Returns an array of values of all elements PIVector<T> ret;
//! \~russian Возвращает массив значений всех эелметнов for (size_t i = 0; i < pim_content.size(); ++i) ret << pim_content[i].second;
inline PIVector<T> values() const { return pim_content; } return ret;
}
//! \~english Returns the key of the first element Key key(const T & value_, const Key & default_ = Key()) const {
//! whose value matches `value` or `default_` if there is no such element. for (int i = 0; i < pim_content.size_s(); ++i)
//! \~russian Возвращает ключ первого элемента, значение которого if (pim_content[i].second == value_)
//! совпадает с `value` или `default_` если такого элемента нет. return pim_content[i].first;
inline Key key(const T & value, const Key & default_ = Key()) const {
for (int i = 0; i < pim_index.size_s(); ++i) {
const auto & mi(pim_index[i]);
if (pim_content[mi.index] == value) {
return mi.key;
}
}
return default_; return default_;
} }
PIVector<Key> keys() const {
//! \~english Returns an array of keys of all elements
//! \~russian Возвращает массив ключей всех элементов
inline PIVector<Key> keys() const {
PIVector<Key> ret; PIVector<Key> ret;
ret.reserve(pim_index.size()); for (size_t i = 0; i < pim_content.size(); ++i) ret << pim_content[i].first;
for (int i = 0; i < pim_index.size_s(); ++i) {
ret << pim_index[i].key;
}
return ret; return ret;
} }
//! \~english Execute function `void f(const Key & key, const T & value)` for every element in array. void dump() {
//! \~russian Выполняет функцию `void f(const Key & key, const T & value)` для каждого элемента массива. piCout << "PIMap" << size() << "entries" << PICoutManipulators::NewLine << "content:";
inline void forEach(std::function<void(const Key & key, const T & value)> f) const { for (size_t i = 0; i < pim_content.size(); ++i)
for (int i = 0; i < pim_index.size_s(); ++i) { piCout << PICoutManipulators::Tab << i << ":" << pim_content[i];
const auto & mi(pim_index[i]); // piCout << "index:";
f(mi.key, pim_content[mi.index]); // for (size_t i = 0; i < pim_index.size(); ++i)
} // piCout << PICoutManipulators::Tab << i << ":" << pim_index[i].key << "->" << pim_index[i].index;
} }
//! \~english Сreates a new map PIMap<Key2, T2> populated with the results
//! of calling a provided function `PIPair<Key2, T2> f(const Key & key, const T & value)` on every element in the calling array.
//! \~russian Создаёт новый словарь PIMap<Key2, T2> с результатом вызова указанной функции
//! `PIPair<Key2, T2> f(const Key & key, const T & value)` для каждого элемента массива.
template<typename Key2, typename T2>
inline PIMap<Key2, T2> map(std::function<PIPair<Key2, T2>(const Key & key, const T & value)> f) const {
PIMap<Key2, T2> ret;
ret.reserve(size());
for (int i = 0; i < pim_index.size_s(); ++i) {
const auto & mi(pim_index[i]);
ret.insert(f(mi.key, pim_content[mi.index]));
}
return ret;
}
//! \~english Сreates a new array PIVector<ST> populated with the results
//! of calling a provided function `ST f(const Key & key, const T & value)` on every element in the calling array.
//! \~russian Создаёт новый массив PIVector<ST> с результатом вызова указанной функции
//! `ST f(const Key & key, const T & value)` для каждого элемента массива.
template<typename ST>
inline PIVector<ST> map(std::function<ST(const Key & key, const T & value)> f) const {
PIVector<ST> ret;
ret.reserve(size());
for (int i = 0; i < pim_index.size_s(); ++i) {
const auto & mi(pim_index[i]);
ret << f(mi.key, pim_content[mi.index]);
}
return ret;
}
//! \~english Returns a new array with all elements
//! that pass the test implemented by the provided function `bool test(const Key & key, const T & value)`.
//! \~russian Возвращает новый массив со всеми элементами,
//! прошедшими проверку, задаваемую в передаваемой функции `bool test(const Key & key, const T & value)`.
inline PIMap<Key, T> filter(std::function<bool(const Key & key, const T & value)> test) const {
PIMap<Key, T> ret;
for (int i = 0; i < pim_index.size_s(); ++i) {
const auto & mi(pim_index[i]);
const auto & v(pim_content[mi.index]);
if (test(mi.key, v)) {
ret.insert(mi.key, v);
}
}
return ret;
}
protected: protected:
struct MapIndex { // struct MapIndex {
MapIndex(const Key & k = Key(), size_t i = 0): key(k), index(i) {} // MapIndex(Key k = Key(), size_t i = 0): key(k), index(i) {;}
MapIndex(Key && k, size_t i = 0): key(std::move(k)), index(i) {} // Key key;
Key key; // size_t index;
size_t index; // bool operator ==(const MapIndex & s) const {return key == s.key;}
bool operator==(const MapIndex & s) const { return key == s.key; } // bool operator !=(const MapIndex & s) const {return key != s.key;}
bool operator!=(const MapIndex & s) const { return key != s.key; } // bool operator <(const MapIndex & s) const {return key < s.key;}
bool operator<(const MapIndex & s) const { return key < s.key; } // bool operator >(const MapIndex & s) const {return key > s.key;}
bool operator>(const MapIndex & s) const { return key > s.key; } // };
}; // template <typename Key1, typename T1> friend PIByteArray & operator >>(PIByteArray & s, PIDeque<typename PIMap<Key1, T1>::MapIndex> & v);
// template <typename Key1, typename T1> friend PIByteArray & operator <<(PIByteArray & s, const PIDeque<typename PIMap<Key1, T1>::MapIndex> & v);
template<typename P, typename Key1, typename T1> ssize_t binarySearch(ssize_t first, ssize_t last, const Key & key, bool & found) const {
friend PIBinaryStream<P> & operator>>(PIBinaryStream<P> & s, PIDeque<typename PIMap<Key1, T1>::MapIndex> & v); ssize_t mid;
template<typename P, typename Key1, typename T1>
friend PIBinaryStream<P> & operator<<(PIBinaryStream<P> & s, const PIDeque<typename PIMap<Key1, T1>::MapIndex> & v);
PIVector<T> pim_content;
PIDeque<MapIndex> pim_index;
private:
inline ssize_t _binarySearch(ssize_t first, ssize_t last, const Key & key, bool & found) const {
ssize_t mid = 0;
while (first <= last) { while (first <= last) {
mid = (first + last) / 2; mid = (first + last) / 2;
if (key > pim_index[mid].key) if (key > pim_content[mid].first) first = mid + 1;
first = mid + 1; else if (key < pim_content[mid].first) last = mid - 1;
else if (key < pim_index[mid].key) else {found = true; return mid;}
last = mid - 1;
else {
found = true;
return mid;
}
} }
found = false; found = false;
return first; return first;
} }
ssize_t _find(const Key & k, bool & found) const {
inline ssize_t _find(const Key & k, bool & found) const { if (pim_content.isEmpty()) {
if (pim_index.isEmpty()) {
found = false; found = false;
return 0; return 0;
} }
return _binarySearch(0, pim_index.size_s() - 1, k, found); return binarySearch(0, pim_content.size_s() - 1, k, found);
} }
void _remove(ssize_t index) {
inline void _remove(ssize_t index) { pim_content.remove(index);
const size_t ci = pim_index[index].index, bi = pim_index.size() - 1;
pim_index.remove(index);
for (size_t i = 0; i < pim_index.size(); ++i) {
if (pim_index[i].index == bi) {
pim_index[i].index = ci;
break;
}
}
piSwap<T>(pim_content[ci], pim_content.back());
pim_content.resize(pim_index.size());
} }
const value_type _pair(ssize_t index) const {
inline const value_type _pair(ssize_t index) const { if (index < 0 || index >= pim_content.size_s()) return value_type();
if (index < 0 || index >= pim_index.size_s()) return value_type(); return pim_content[index];
const auto & mi(pim_index[index]);
return value_type(mi.key, pim_content[mi.index]);
} }
Key & _key(ssize_t index) {return pim_content[index].first;}
inline Key & _key(ssize_t index) { return pim_index[index].key; } T & _value(ssize_t index) {return pim_content[index].second;}
inline const Key & _key(ssize_t index) const { return pim_index[index].key; } PIDeque<PIPair<Key, T>> pim_content;
inline T & _value(ssize_t index) { return pim_content[pim_index[index].index]; }
inline const T & _value(ssize_t index) const { return pim_content[pim_index[index].index]; }
}; };
//! \addtogroup Containers template <typename Key, typename T>
//! \{
//! \class PIMapIteratorConst
//! \brief
//! \~english Java-style iterator for \a PIMap.
//! \~russian Итератор Java стиля для \a PIMap.
//! \~\}
//! \details
//! \~english
//! This class used to easy serial access keys and values in PIMap with read only permitions.
//! Use constructor to create iterator, or use \a PIMap::makeIterator()
//! \~russian
//! Этот класс используется для удобного перебора ключей и значений всего словаря только для чтения.
//! Можно использовать конструктор, в который передаётся словарь, или функцию словаря \a PIMap::makeIterator().
//! \~
//! \code
//! PIMap<int, PIString> m;
//! m[1] = "one";
//! m[2] = "two";
//! m[4] = "four";
//! auto it = m.makeIterator();
//! while (it.next()) {
//! piCout << it.key() << it.value();
//! }
//! // 1 one
//! // 2 two
//! // 4 four
//! \endcode
template<typename Key, typename T>
class PIMapIteratorConst {
typedef PIMap<Key, T> MapType;
public:
inline PIMapIteratorConst(const PIMap<Key, T> & map): m(map), pos(-1) {}
//! \~english Returns current key.
//! \~russian Возвращает ключ текущего элемента.
//! \~\sa \a value()
inline const Key & key() const { return m._key(pos); }
//! \~english Returns current value.
//! \~russian Возвращает значение текущего элемента.
//! \~\sa \a key()
inline const T & value() const { return m._value(pos); }
//! \~english Returns true if iterator can jump to next entry
//! \~russian Возвращает true если итератор может перейти к следующему элементу.
//! \~\sa \a next()
inline bool hasNext() const { return pos < (m.size_s() - 1); }
//! \~english Jump to next entry and return true if new position is valid.
//! \~russian Переходит к следующему элементу и возвращает true если он существует.
//! \~\sa \a hasNext(), \a reset()
inline bool next() {
++pos;
return pos < m.size_s();
}
//! \~english Reset iterator to initial position.
//! \~russian Переходит на начало.
//! \~\sa \a next()
inline void reset() { pos = -1; }
private:
const MapType & m;
ssize_t pos;
};
//! \addtogroup Containers
//! \{
//! \class PIMapIteratorConstReverse
//! \brief
//! \~english Java-style reverse iterator for \a PIMap.
//! \~russian Итератор Java стиля для \a PIMap в обратном порядке.
//! \~\}
//! \details
//! \~english
//! This class used to easy serial reverse access keys and values in PIMap with read only permitions.
//! Use constructor to create iterator, or use \a PIMap::makeReverseIterator().
//! \~russian
//! Этот класс используется для удобного перебора ключей и значений всего словаря в обратном порядке только для чтения.
//! Можно использовать конструктор, в который передаётся словарь, или функцию словаря \a PIMap::makeReverseIterator().
//! \~
//! \code
//! PIMap<int, PIString> m;
//! m[1] = "one";
//! m[2] = "two";
//! m[4] = "four";
//! auto it = m.makeReverseIterator();
//! while (it.next()) {
//! piCout << it.key() << it.value();
//! }
//! // 4 four
//! // 2 two
//! // 1 one
//! \endcode
template<typename Key, typename T>
class PIMapIteratorConstReverse {
typedef PIMap<Key, T> MapType;
public:
inline PIMapIteratorConstReverse(const PIMap<Key, T> & map): m(map), pos(m.size_s()) {}
//! \~english Returns current key.
//! \~russian Возвращает ключ текущего элемента.
//! \~\sa \a value()
inline const Key & key() const { return m._key(pos); }
//! \~english Returns current value.
//! \~russian Возвращает значение текущего элемента.
//! \~\sa \a key()
inline const T & value() const { return m._value(pos); }
//! \~english Returns true if iterator can jump to next entry
//! \~russian Возвращает true если итератор может перейти к следующему элементу.
//! \~\sa \a next()
inline bool hasNext() const { return pos > 0; }
//! \~english Jump to next entry and return true if new position is valid.
//! \~russian Переходит к следующему элементу и возвращает true если он существует.
//! \~\sa \a hasNext(), \a reset()
inline bool next() {
--pos;
return pos >= 0;
}
//! \~english Reset iterator to initial position.
//! \~russian Переходит на начало.
//! \~\sa \a next()
inline void reset() { pos = m.size_s(); }
private:
const MapType & m;
ssize_t pos;
};
//! \addtogroup Containers
//! \{
//! \class PIMapIterator
//! \brief
//! \~english Java-style iterator for \a PIMap.
//! \~russian Итератор Java стиля для \a PIMap.
//! \~\}
//! \details
//! \~english
//! This class used to easy serial access keys and values in PIMap with write permitions.
//! Use constructor to create iterator, or use \a PIMap::makeIterator()
//! \~russian
//! Этот класс используется для удобного перебора ключей и значений всего словаря с доступом на запись.
//! Можно использовать конструктор, в который передаётся словарь, или функцию словаря \a PIMap::makeIterator().
//! \~
//! \code
//! PIMap<int, PIString> m;
//! m[1] = "one";
//! m[2] = "two";
//! m[4] = "four";
//! auto it = m.makeIterator();
//! while (it.next()) {
//! it.value().append("_!");
//! piCout << it.key() << it.value();
//! }
//! // 1 one_!
//! // 2 two_!
//! // 4 four_!
//! \endcode
template<typename Key, typename T>
class PIMapIterator { class PIMapIterator {
typedef PIMap<Key, T> MapType; typedef PIMap<Key, T> MapType;
public: public:
inline PIMapIterator(PIMap<Key, T> & map): m(map), pos(-1) {} PIMapIterator(const PIMap<Key, T> & map, bool reverse = false): m(map), pos(-1), rev(reverse) {
if (rev) pos = m.size_s();
//! \~english Returns current key.
//! \~russian Возвращает ключ текущего элемента.
//! \~\sa \a value()
inline const Key & key() const { return m._key(pos); }
//! \~english Returns current value.
//! \~russian Возвращает значение текущего элемента.
//! \~\sa \a key()
inline T & value() { return m._value(pos); }
//! \~english Returns true if iterator can jump to next entry
//! \~russian Возвращает true если итератор может перейти к следующему элементу.
//! \~\sa \a next()
inline bool hasNext() const { return pos < (m.size_s() - 1); }
//! \~english Jump to next entry and return true if new position is valid.
//! \~russian Переходит к следующему элементу и возвращает true если он существует.
//! \~\sa \a hasNext(), \a reset()
inline bool next() {
++pos;
return pos < m.size_s();
} }
const Key & key() const {return const_cast<MapType & >(m)._key(pos);}
//! \~english Reset iterator to initial position. const T & value() const {return const_cast<MapType & >(m)._value(pos);}
//! \~russian Переходит на начало. T & valueRef() const {return const_cast<MapType & >(m)._value(pos);}
//! \~\sa \a next() inline bool hasNext() const {
inline void reset() { pos = -1; } if (rev) {
return pos > 0;
private: } else {
MapType & m; return pos < (m.size_s() - 1);
ssize_t pos; }
}; return false;
}
inline bool next() {
//! \addtogroup Containers if (rev) {
//! \{ --pos;
//! \class PIMapIteratorReverse return pos >= 0;
//! \brief } else {
//! \~english Java-style reverse iterator for \a PIMap. ++pos;
//! \~russian Итератор Java стиля для \a PIMap в обратном порядке. return pos < m.size_s();
//! \~\} }
//! \details return false;
//! \~english }
//! This class used to easy serial reverse access keys and values in PIMap with write permitions. inline void reset() {
//! Use constructor to create iterator, or use \a PIMap::makeReverseIterator(). if (rev) {
//! \~russian pos = m.size_s();
//! Этот класс используется для удобного перебора ключей и значений всего словаря в обратном порядке с доступом на запись. } else {
//! Можно использовать конструктор, в который передаётся словарь, или функцию словаря \a PIMap::makeReverseIterator(). pos = -1;
//! \~ }
//! \code
//! PIMap<int, PIString> m;
//! m[1] = "one";
//! m[2] = "two";
//! m[4] = "four";
//! auto it = m.makeReverseIterator();
//! while (it.next()) {
//! it.value().append("_!");
//! piCout << it.key() << it.value();
//! }
//! // 4 four_!
//! // 2 two_!
//! // 1 one_!
//! \endcode
template<typename Key, typename T>
class PIMapIteratorReverse {
typedef PIMap<Key, T> MapType;
public:
inline PIMapIteratorReverse(PIMap<Key, T> & map): m(map), pos(m.size_s()) {}
//! \~english Returns current key.
//! \~russian Возвращает ключ текущего элемента.
//! \~\sa \a value()
inline const Key & key() const { return m._key(pos); }
//! \~english Returns current value.
//! \~russian Возвращает значение текущего элемента.
//! \~\sa \a key()
inline T & value() { return m._value(pos); }
//! \~english Returns true if iterator can jump to next entry
//! \~russian Возвращает true если итератор может перейти к следующему элементу.
//! \~\sa \a next()
inline bool hasNext() const { return pos > 0; }
//! \~english Jump to next entry and return true if new position is valid.
//! \~russian Переходит к следующему элементу и возвращает true если он существует.
//! \~\sa \a hasNext(), \a reset()
inline bool next() {
--pos;
return pos >= 0;
} }
//! \~english Reset iterator to initial position.
//! \~russian Переходит на начало.
//! \~\sa \a next()
inline void reset() { pos = m.size_s(); }
private: private:
MapType & m; const MapType & m;
ssize_t pos; ssize_t pos;
bool rev;
}; };
#ifdef PIP_STD_IOSTREAM #ifdef PIP_STD_IOSTREAM
//! \~english Output operator to [std::ostream](https://en.cppreference.com/w/cpp/io/basic_ostream).
//! \~russian Оператор вывода в [std::ostream](https://ru.cppreference.com/w/cpp/io/basic_ostream).
template<typename Key, typename Type> template<typename Key, typename Type>
inline std::ostream & operator<<(std::ostream & s, const PIMap<Key, Type> & v) { inline std::ostream & operator <<(std::ostream & s, const PIMap<Key, Type> & v) {
s << "{"; s << "{";
bool first = true; bool first = true;
for (typename PIMap<Key, Type>::const_iterator i = v.begin(); i != v.end(); ++i) { for (typename PIMap<Key, Type>::const_iterator i = v.begin(); i != v.end(); ++i) {
if (!first) s << ", "; if (!first)
s << ", ";
first = false; first = false;
s << i.key() << ": " << i.value(); s << i->first << ": " << i->second;
} }
s << "}"; s << "}";
return s; return s;
} }
#endif #endif
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
template<typename Key, typename Type> template<typename Key, typename Type>
inline PICout operator<<(PICout s, const PIMap<Key, Type> & v) { inline PICout operator <<(PICout s, const PIMap<Key, Type> & v) {
s.space(); s.space();
s.saveAndSetControls(0); s.setControl(0, true);
s << "{"; s << "{";
bool first = true; bool first = true;
for (typename PIMap<Key, Type>::const_iterator i = v.begin(); i != v.end(); ++i) { for (typename PIMap<Key, Type>::const_iterator i = v.begin(); i != v.end(); ++i) {
if (!first) s << ", "; if (!first)
s << ", ";
first = false; first = false;
s << i.key() << ": " << i.value(); s << i->first << ": " << i->second;
} }
s << "}"; s << "}";
s.restoreControls(); s.restoreControl();
return s; return s;
} }
template<typename Key, typename Type> template<typename Key, typename Type> inline void piSwap(PIMap<Key, Type> & f, PIMap<Key, Type> & s) {f.swap(s);}
inline void piSwap(PIMap<Key, Type> & f, PIMap<Key, Type> & s) {
f.swap(s);
}
#endif // PIMAP_H #endif // PIMAP_H

View File

@@ -13,22 +13,22 @@
//! Андрей Бычков work.a.b@yandex.ru; //! Андрей Бычков work.a.b@yandex.ru;
//! \~\} //! \~\}
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
pair pair
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PIPAIR_H #ifndef PIPAIR_H
@@ -44,39 +44,36 @@
//! \~english Class template that provides a way to store two heterogeneous objects as a single unit. //! \~english Class template that provides a way to store two heterogeneous objects as a single unit.
//! \~russian Класс, который позволяет хранить два разнородных объекта как единое целое. //! \~russian Класс, который позволяет хранить два разнородных объекта как единое целое.
//! \~\} //! \~\}
//! \details
//! \~english
//! \~russian
//! \~\sa \a PIMap //! \~\sa \a PIMap
template<typename Type0, typename Type1> template<typename Type0, typename Type1>
class PIPair { class PIPair {
public: public:
//! \~english Constructs an empty PIPair. //! \~english Constructs an empty PIPair.
//! \~russian Создает пустой PIPair. //! \~russian Создает пустой PIPair.
PIPair(): first(), second() {} PIPair() : first(), second() {}
//! \~english Constructs PIPair from [std::tuple](https://en.cppreference.com/w/cpp/utility/tuple). //! \~english Constructs PIPair from [std::tuple](https://en.cppreference.com/w/cpp/utility/tuple).
//! \~russian Создает PIPair из [std::tuple](https://ru.cppreference.com/w/cpp/utility/tuple). //! \~russian Создает PIPair из [std::tuple](https://ru.cppreference.com/w/cpp/utility/tuple).
explicit PIPair(std::tuple<Type0, Type1> tuple) { PIPair(std::tuple<Type0, Type1> tuple) {
first = std::get<0>(tuple); first = std::get<0>(tuple);
second = std::get<1>(tuple); second = std::get<1>(tuple);
} }
//! \~english Constructs PIPair from [std::pair](https://en.cppreference.com/w/cpp/utility/pair).
//! \~russian Создает PIPair из [std::pair](https://ru.cppreference.com/w/cpp/utility/pair).
explicit PIPair(std::pair<Type0, Type1> pair) {
first = pair.first;
second = pair.second;
}
//! \~english Constructs PIPair from values `value0` and `value1`. //! \~english Constructs PIPair from values `value0` and `value1`.
//! \~russian Создает PIPair из `value0` и `value1`. //! \~russian Создает PIPair из `value0` и `value1`.
PIPair(const Type0 & value0, const Type1 & value1) { PIPair(const Type0 & value0, const Type1 & value1) {
first = value0; first = value0;
second = value1; second = value1;
} }
//! \~english Move constructor. //! \~english Move constructor.
//! \~russian Перемещающий конструктор. //! \~russian Перемещающий конструктор.
PIPair(Type0 && value0, Type1 && value1) { PIPair(Type0 && value0, Type1 && value1) {
first = std::move(value0); first = std::move(value0);
second = std::move(value1); second = std::move(value1);
} }
@@ -92,20 +89,20 @@ public:
//! \~english Compare operator with PIPair. //! \~english Compare operator with PIPair.
//! \~russian Оператор сравнения с PIPair. //! \~russian Оператор сравнения с PIPair.
template<typename Type0, typename Type1> template<typename Type0, typename Type1>
inline bool operator==(const PIPair<Type0, Type1> & value0, const PIPair<Type0, Type1> & value1) { inline bool operator ==(const PIPair<Type0, Type1> & value0, const PIPair<Type0, Type1> & value1) {
return (value0.first == value1.first) && (value0.second == value1.second); return (value0.first == value1.first) && (value0.second == value1.second);
} }
//! \~english Compare operator with PIPair. //! \~english Compare operator with PIPair.
//! \~russian Оператор сравнения с PIPair. //! \~russian Оператор сравнения с PIPair.
template<typename Type0, typename Type1> template<typename Type0, typename Type1>
inline bool operator!=(const PIPair<Type0, Type1> & value0, const PIPair<Type0, Type1> & value1) { inline bool operator !=(const PIPair<Type0, Type1> & value0, const PIPair<Type0, Type1> & value1) {
return (value0.first != value1.first) || (value0.second != value1.second); return (value0.first != value1.first) || (value0.second != value1.second);
} }
#ifdef PIP_STD_IOSTREAM #ifdef PIP_STD_IOSTREAM
template<typename Type0, typename Type1> template<typename Type0, typename Type1>
inline std::ostream & operator<<(std::ostream & s, const PIPair<Type0, Type1> & v) { inline std::ostream & operator <<(std::ostream & s, const PIPair<Type0, Type1> & v) {
s << "(" << v.first << ", " << v.second << ")"; s << "(" << v.first << ", " << v.second << ")";
return s; return s;
} }
@@ -115,22 +112,33 @@ inline std::ostream & operator<<(std::ostream & s, const PIPair<Type0, Type1> &
//! \~english Output operator to \a PICout //! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout //! \~russian Оператор вывода в \a PICout
template<typename Type0, typename Type1> template<typename Type0, typename Type1>
inline PICout operator<<(PICout s, const PIPair<Type0, Type1> & v) { inline PICout operator <<(PICout s, const PIPair<Type0, Type1> & v) {
s.space(); s.space();
s.saveAndSetControls(0); s.setControl(0, true);
s << "(" << v.first << ", " << v.second << ")"; s << "(" << v.first << ", " << v.second << ")";
s.restoreControls(); s.restoreControl();
return s; return s;
} }
//! \~english Creates \a PIPair object, deducing the target type from the types of arguments. //! \~english Creates \a PIPair object, deducing the target type from the types of arguments.
//! \~russian Создает \a PIPair выводя типы из аргументов. //! \~russian Создает \a PIPair выводя типы из аргументов.
//! \sa \a createPIPair() //! \~\details
template<class T1, class T2> //! \~\code
PIPair<T1, T2> createPIPair(T1 f, T2 s) { //! auto p = createPIPair(1, 'a');
return PIPair<T1, T2>(std::move(f), std::move(s)); //! piCout << p; // (1, a)
//! \endcode
template< class T1, class T2 >
PIPair<T1,T2> createPIPair(const T1 & f, const T2 & s) {
return PIPair<T1,T2>(f, s);
} }
//! \~english Creates \a PIPair object, deducing the target type from the types of arguments.
//! \~russian Создает \a PIPair выводя типы из аргументов.
//! \sa \a createPIPair()
template< class T1, class T2 >
PIPair<T1,T2> createPIPair(T1 && f, T2 && s) {
return PIPair<T1,T2>(std::move(f), std::move(s));
}
#endif // PIPAIR_H #endif // PIPAIR_H

View File

@@ -1,34 +1,25 @@
//! \addtogroup Containers /*! \file piqueue.h
//! \{ * \brief Queue container
//! \file piqueue.h *
//! \brief * This file declare PIQueue
//! \~english Declares \a PIQueue */
//! \~russian Объявление \a PIQueue
//! \~\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 PIP - Platform Independent Primitives
Queue container Queue container
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PIQUEUE_H #ifndef PIQUEUE_H
@@ -37,82 +28,24 @@
#include "pideque.h" #include "pideque.h"
#include "pivector.h" #include "pivector.h"
//! \addtogroup Containers
//! \{
//! \class PIQueue
//! \brief
//! \~english A container class inherited from the \a PIDeque with queue functionality.
//! \~russian Класс контейнера наследованый от \a PIDeque с функциональностью очереди.
//! \~\}
//! \details
//! \~english The container is a array of elements organized according to the FIFO principle (first in, first out).
//! Adds \a enqueue() and \dequeue() functions to \a PIDeque.
//! \~russian Контейнер представляющий массив элементов, организованных по принципу FIFO (первым пришёл — первым вышел).
//! Добавляет к \a PIDeque функции \a enqueue() и \a dequeue().
//! \~\sa \a PIDeque
template<typename T> template<typename T>
class PIQueue: public PIDeque<T> { class PIQueue: public PIDeque<T> {
public: public:
//! \~english Constructs an empty array.
//! \~russian Создает пустой массив.
PIQueue() {} PIQueue() {}
virtual ~PIQueue() {}
//! \~english Puts an element on the queue. PIDeque<T> & enqueue(const T & v) {PIDeque<T>::push_front(v); return *this;}
//! \~russian Кладёт элемент в очередь. PIDeque<T> & enqueue(T && v) {PIDeque<T>::push_front(std::move(v)); return *this;}
PIDeque<T> & enqueue(const T & v) { T dequeue() {return PIDeque<T>::take_back();}
PIDeque<T>::push_front(v); T & head() {return PIDeque<T>::back();}
return *this; const T & head() const {return PIDeque<T>::back();}
PIVector<T> toVector() {
PIVector<T> v;
v.reserve(PIDeque<T>::size());
for (uint i = 0; i < PIDeque<T>::size(); ++i)
v.push_back(PIDeque<T>::at(i));
return v;
} }
//! \~english Move an element on the queue.
//! \~russian Перемещает элемент в очередь.
PIDeque<T> & enqueue(T && v) {
PIDeque<T>::push_front(std::move(v));
return *this;
}
//! \~english Retrieves and returns an element from the queue.
//! \~russian Забирает и возвращает элемент из очереди.
//! \~\details
//! \note
//! \~english This function assumes that the array isn't empty.
//! Otherwise will be undefined behavior.
//! \~russian Эта функция предполагает, что массив не пустой.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
T dequeue() { return PIDeque<T>::take_back(); }
//! \~english Head element of the queue.
//! \~russian Головной (верхний) элемент очереди.
//! \~\details
//! \note
//! \~english Returns a reference to the head element of the queue.
//! This function assumes that the array isn't empty.
//! Otherwise will be undefined behavior.
//! \~russian Возвращает ссылку на головной (верхний) элемент очереди.
//! Эта функция предполагает, что массив не пустой.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
T & head() { return PIDeque<T>::back(); }
const T & head() const { return PIDeque<T>::back(); }
//! \~english Tail element of the queue.
//! \~russian Хвостовой (нижний) элемент очереди.
//! \~\details
//! \~english Returns a reference to the tail element of the queue.
//! This function assumes that the array isn't empty.
//! Otherwise will be undefined behavior.
//! \~russian Возвращает ссылку на хвостовой (нижний) элемент очереди.
//! Эта функция предполагает, что массив не пустой.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
T & tail() { return PIDeque<T>::front(); }
const T & tail() const { return PIDeque<T>::front(); }
//! \~english Converts \a PIQueue to \a PIVector.
//! \~russian Преобразует \a PIQueue в \a PIVector.
PIVector<T> toVector() const { return PIVector<T>(PIDeque<T>::data(), PIDeque<T>::size()); }
//! \~english Converts \a PIQueue to \a PIDeque.
//! \~russian Преобразует \a PIQueue в \a PIDeque.
PIDeque<T> toDeque() const { return PIDeque<T>(*this); }
}; };
#endif // PIQUEUE_H #endif // PIQUEUE_H

View File

@@ -1,25 +1,25 @@
/*! \file piset.h /*! \file piset.h
* \brief Set container * \brief Set container
* *
* This file declare PISet * This file declare PISet
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Set container Set container
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PISET_H #ifndef PISET_H
@@ -34,124 +34,30 @@
* set with \a operator[] or with function \a find(). These function * set with \a operator[] or with function \a find(). These function
* has logarithmic complexity. * has logarithmic complexity.
*/ */
template<typename T> template <typename T>
class PISet: public PIMap<T, uchar> { class PISet: public PIMap<T, uchar> {
typedef PIMap<T, uchar> _CSet; typedef PIMap<T, uchar> _CSet;
public: public:
//! Contructs an empty set //! Contructs an empty set
PISet() {} PISet() {}
virtual ~PISet() {;}
//! Contructs set with one element "value" //! Contructs set with one element "value"
explicit PISet(const T & value) { _CSet::insert(value, 0); } PISet(const T & value) {_CSet::insert(value, 0);}
//! Contructs set with elements "v0" and "v1" //! Contructs set with elements "v0" and "v1"
PISet(const T & v0, const T & v1) { PISet(const T & v0, const T & v1) {_CSet::insert(v0, 0); _CSet::insert(v1, 0);}
_CSet::insert(v0, 0);
_CSet::insert(v1, 0);
}
//! Contructs set with elements "v0", "v1" and "v2" //! Contructs set with elements "v0", "v1" and "v2"
PISet(const T & v0, const T & v1, const T & v2) { PISet(const T & v0, const T & v1, const T & v2) {_CSet::insert(v0, 0); _CSet::insert(v1, 0); _CSet::insert(v2, 0);}
_CSet::insert(v0, 0);
_CSet::insert(v1, 0);
_CSet::insert(v2, 0);
}
//! Contructs set with elements "v0", "v1", "v2" and "v3" //! Contructs set with elements "v0", "v1", "v2" and "v3"
PISet(const T & v0, const T & v1, const T & v2, const T & v3) { PISet(const T & v0, const T & v1, const T & v2, const T & v3) {_CSet::insert(v0, 0); _CSet::insert(v1, 0); _CSet::insert(v2, 0); _CSet::insert(v3, 0);}
_CSet::insert(v0, 0);
_CSet::insert(v1, 0);
_CSet::insert(v2, 0);
_CSet::insert(v3, 0);
}
class const_iterator {
friend class PISet<T>;
private:
inline const_iterator(const PISet<T> * v, ssize_t p): parent(v), pos(p) {}
const PISet<T> * parent;
ssize_t pos;
public:
typedef T value_type;
typedef T * pointer;
typedef T & reference;
typedef std::ptrdiff_t difference_type;
typedef std::random_access_iterator_tag iterator_category;
inline const_iterator(): parent(0), pos(0) {}
inline const T & operator*() const { return parent->pim_index[pos].key; }
inline const T & operator->() const { return parent->pim_index[pos].key; }
inline const_iterator & operator++() {
++pos;
return *this;
}
inline const_iterator operator++(int) {
const auto tmp = *this;
++*this;
return tmp;
}
inline const_iterator & operator--() {
--pos;
return *this;
}
inline const_iterator operator--(int) {
const auto tmp = *this;
--*this;
return tmp;
}
inline const_iterator & operator+=(const const_iterator & it) {
pos += it.pos;
return *this;
}
inline const_iterator & operator+=(size_t p) {
pos += p;
return *this;
}
inline const_iterator & operator-=(const const_iterator & it) {
pos -= it.pos;
return *this;
}
inline const_iterator & operator-=(size_t p) {
pos -= p;
return *this;
}
friend inline const_iterator operator-(size_t p, const const_iterator & it) { return it - p; }
friend inline const_iterator operator-(const const_iterator & it, size_t p) {
auto tmp = it;
tmp -= p;
return tmp;
}
friend inline std::ptrdiff_t operator-(const const_iterator & it1, const const_iterator & it2) { return it1.pos - it2.pos; }
friend inline const_iterator operator+(size_t p, const const_iterator & it) { return it + p; }
friend inline const_iterator operator+(const const_iterator & it, size_t p) {
auto tmp = it;
tmp += p;
return tmp;
}
inline bool operator==(const const_iterator & it) const { return (pos == it.pos); }
inline bool operator!=(const const_iterator & it) const { return (pos != it.pos); }
friend inline bool operator<(const const_iterator & it1, const const_iterator & it2) { return it1.pos < it2.pos; }
friend inline bool operator<=(const const_iterator & it1, const const_iterator & it2) { return it1.pos <= it2.pos; }
friend inline bool operator>(const const_iterator & it1, const const_iterator & it2) { return it1.pos > it2.pos; }
friend inline bool operator>=(const const_iterator & it1, const const_iterator & it2) { return it1.pos >= it2.pos; }
};
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 //! Contructs set from vector of elements
explicit PISet(const PIVector<T> & values) { PISet(const PIVector<T> & values) {
if (values.isEmpty()) return; if (values.isEmpty()) return;
for (int i = 0; i < values.size_s(); ++i) { for (int i = 0; i < values.size_s(); ++i) {
_CSet::insert(values[i], 0); _CSet::insert(values[i], 0);
@@ -159,7 +65,7 @@ public:
} }
//! Contructs set from deque of elements //! Contructs set from deque of elements
explicit PISet(const PIDeque<T> & values) { PISet(const PIDeque<T> & values) {
if (values.isEmpty()) return; if (values.isEmpty()) return;
for (int i = 0; i < values.size_s(); ++i) { for (int i = 0; i < values.size_s(); ++i) {
_CSet::insert(values[i], 0); _CSet::insert(values[i], 0);
@@ -167,129 +73,89 @@ public:
} }
typedef T key_type; typedef T key_type;
PISet<T> & operator<<(const T & t) { PISet<T> & operator <<(const T & t) {_CSet::insert(t, 0); return *this;}
_CSet::insert(t, 0); PISet<T> & operator <<(T && t) {_CSet::insert(std::move(t), 0); return *this;}
return *this; PISet<T> & operator <<(const PISet<T> & other) {(*(_CSet*)this) << *((_CSet*)&other); return *this;}
}
PISet<T> & operator<<(T && t) {
_CSet::insert(std::move(t), 0);
return *this;
}
PISet<T> & operator<<(const PISet<T> & other) {
(*(_CSet *)this) << *((_CSet *)&other);
return *this;
}
//! \~english Tests if element `key` exists in the set.
//! \~russian Проверяет наличие элемента `key` в массиве.
inline bool contains(const T & t) const { return _CSet::contains(t); }
//! Returns if element "t" exists in this set //! Returns if element "t" exists in this set
bool operator[](const T & t) const { return _CSet::contains(t); } bool operator [](const T & t) const {return _CSet::contains(t);}
//! Returns if element "t" exists in this set //! Returns if element "t" exists in this set
PISet<T> & remove(const T & t) { PISet<T> & remove(const T & t) {_CSet::remove(t); return *this;}
_CSet::remove(t);
return *this;
}
//! Unite set with "v" //! Unite set with "v"
PISet<T> & unite(const PISet<T> & v) { PISet<T> & unite(const PISet<T> & v) {
for (const auto & i: v) for (typename PIMap<T, uchar>::const_iterator i = v.begin(); i != v.end(); ++i)
_CSet::insert(i, 0); _CSet::insert(i->first, 0);
return *this; return *this;
} }
//! Subtract set with "v" //! Subtract set with "v"
PISet<T> & subtract(const PISet<T> & v) { PISet<T> & subtract(const PISet<T> & v) {
for (const auto & i: v) for (typename PIMap<T, uchar>::const_iterator i = v.begin(); i != v.end(); ++i)
_CSet::remove(i); _CSet::remove(i->first);
return *this; return *this;
} }
//! Intersect set with "v" //! Intersect set with "v"
PISet<T> & intersect(const PISet<T> & v) { PISet<T> & intersect(const PISet<T> & v) {
_CSet::removeWhere([&v](const T & k, uchar) { return !v.contains(k); }); for (typename _CSet::iterator i = _CSet::begin(); i != _CSet::end(); ++i)
if (!v.contains(i.key())) {
_CSet::remove(i.key());
--i;
}
return *this; return *this;
} }
//! Unite set with "v" //! Unite set with "v"
PISet<T> & operator+=(const PISet<T> & v) { return unite(v); } PISet<T> & operator +=(const PISet<T> & v) {return unite(v);}
//! Unite set with "v" //! Unite set with "v"
PISet<T> & operator|=(const PISet<T> & v) { return unite(v); } PISet<T> & operator |=(const PISet<T> & v) {return unite(v);}
//! Subtract set with "v" //! Subtract set with "v"
PISet<T> & operator-=(const PISet<T> & v) { return subtract(v); } PISet<T> & operator -=(const PISet<T> & v) {return subtract(v);}
//! Intersect set with "v" //! Intersect set with "v"
PISet<T> & operator&=(const PISet<T> & v) { return intersect(v); } PISet<T> & operator &=(const PISet<T> & v) {return intersect(v);}
//! Returns content of set as PIVector //! Returns content of set as PIVector
PIVector<T> toVector() const { PIVector<T> toVector() const {PIVector<T> ret; for (typename _CSet::const_iterator i = _CSet::begin(); i != _CSet::end(); ++i) ret << (*i).first; return ret;}
PIVector<T> ret;
for (const auto & i: *this)
ret << i;
return ret;
}
//! Returns content of set as PIDeque //! Returns content of set as PIDeque
PIDeque<T> toDeque() const { PIDeque<T> toDeque() const {PIDeque<T> ret; for (typename _CSet::const_iterator i = _CSet::begin(); i != _CSet::end(); ++i) ret << (*i).first; return ret;}
PIDeque<T> ret;
for (const auto & i: *this)
ret << i;
return ret;
}
}; };
//! \relatesalso PISet \brief Returns unite of two sets //! \relatesalso PISet \brief Returns unite of two sets
template<typename T> template <typename T> PISet<T> operator +(const PISet<T> & v0, const PISet<T> & v1) {PISet<T> ret(v0); ret.unite(v1); return ret;}
PISet<T> operator+(const PISet<T> & v0, const PISet<T> & v1) {
PISet<T> ret(v0);
ret.unite(v1);
return ret;
}
//! \relatesalso PISet \brief Returns subtraction of two sets //! \relatesalso PISet \brief Returns subtraction of two sets
template<typename T> template <typename T> PISet<T> operator -(const PISet<T> & v0, const PISet<T> & v1) {PISet<T> ret(v0); ret.subtract(v1); return ret;}
PISet<T> operator-(const PISet<T> & v0, const PISet<T> & v1) {
PISet<T> ret(v0);
ret.subtract(v1);
return ret;
}
//! \relatesalso PISet \brief Returns unite of two sets //! \relatesalso PISet \brief Returns unite of two sets
template<typename T> template <typename T> PISet<T> operator |(const PISet<T> & v0, const PISet<T> & v1) {PISet<T> ret(v0); ret.unite(v1); return ret;}
PISet<T> operator|(const PISet<T> & v0, const PISet<T> & v1) {
PISet<T> ret(v0);
ret.unite(v1);
return ret;
}
//! \relatesalso PISet \brief Returns intersetion of two sets //! \relatesalso PISet \brief Returns intersetion of two sets
template<typename T> template <typename T> PISet<T> operator &(const PISet<T> & v0, const PISet<T> & v1) {PISet<T> ret(v0); ret.intersect(v1); return ret;}
PISet<T> operator&(const PISet<T> & v0, const PISet<T> & v1) {
PISet<T> ret(v0);
ret.intersect(v1);
return ret;
}
template<typename Type> template<typename Type>
inline PICout operator<<(PICout s, const PISet<Type> & v) { inline PICout operator <<(PICout s, const PISet<Type> & v) {
s.space(); s.space();
s.saveAndSetControls(0); s.setControl(0, true);
s << "{"; s << "{";
bool first = true; bool first = true;
for (const auto & i: v) { for (typename PIMap<Type, uchar>::const_iterator i = v.begin(); i != v.end(); ++i) {
if (!first) s << ", "; if (!first)
s << ", ";
first = false; first = false;
s << i; s << i->first;
} }
s << "}"; s << "}";
s.restoreControls(); s.restoreControl();
return s; return s;
} }

View File

@@ -1,106 +1,49 @@
//! \addtogroup Containers /*! \file pistack.h
//! \{ * \brief Stack container
//! \file pistack.h *
//! \brief * This file declare PIStack
//! \~english Declares \a PIStack */
//! \~russian Объявление \a PIStack
//! \~\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 PIP - Platform Independent Primitives
Stack container Stack container
Ivan Pelipenko peri4ko@yandex.ru Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PISTACK_H #ifndef PISTACK_H
#define PISTACK_H #define PISTACK_H
#include "pideque.h"
#include "pivector.h" #include "pivector.h"
//! \addtogroup Containers
//! \{
//! \class PIStack
//! \brief
//! \~english A container class inherited from the \a PIVector with stack functionality.
//! \~russian Класс контейнера наследованый от \a PIVector с функциональностью стека.
//! \~\}
//! \details
//! \~english The container is a array of elements organized according to the LIFO principle (last in, first out).
//! Adds \a push() and \pop() functions to \a PIVector.
//! \~russian Контейнер представляющий массив элементов, организованных по принципу LIFO (последним пришёл — первым вышел).
//! Добавляет к \a PIVector функции \a push() и \a pop().
//! \~\sa \a PIVector
template<typename T> template<typename T>
class PIStack: public PIVector<T> { class PIStack: public PIVector<T> {
public: public:
//! \~english Constructs an empty array. PIStack() {;}
//! \~russian Создает пустой массив. virtual ~PIStack() {;}
PIStack() {} PIVector<T> & push(const T & v) {PIVector<T>::push_back(v); return *this;}
PIVector<T> & push(T && v) {PIVector<T>::push_back(std::move(v)); return *this;}
//! \~english Puts an element on the stack. T pop() {return PIVector<T>::take_back();}
//! \~russian Кладёт элемент в стек. T & top() {return PIVector<T>::back();}
PIVector<T> & push(const T & v) { const T & top() const {return PIVector<T>::back();}
PIVector<T>::push_back(v); PIVector<T> toVector() {
return *this; PIVector<T> v;
v.reserve(PIVector<T>::size());
for (uint i = 0; i < PIVector<T>::size(); ++i)
v.push_back(PIVector<T>::at(i));
return v;
} }
//! \~english Move an element on the stack.
//! \~russian Перемещает элемент в стек.
PIVector<T> & push(T && v) {
PIVector<T>::push_back(std::move(v));
return *this;
}
//! \~english Retrieves and returns an element from the stack.
//! \~russian Забирает и возвращает элемент из стека.
//! \~\details
//! \note
//! \~english This function assumes that the array isn't empty.
//! Otherwise will be undefined behavior.
//! \~russian Эта функция предполагает, что массив не пустой.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
T pop() { return PIVector<T>::take_back(); }
//! \~english Top element of the stack
//! \~russian Верхний элемент стека.
//! \~\details
//! \note
//! \~english Returns a reference to the top element of the stack.
//! This function assumes that the array isn't empty.
//! Otherwise will be undefined behavior.
//! \~russian Возвращает ссылку на верхний элемент стека.
//! Эта функция предполагает, что массив не пустой.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
T & top() { return PIVector<T>::back(); }
const T & top() const { return PIVector<T>::back(); }
//! \~english Converts \a PIStack to \a PIVector.
//! \~russian Преобразует \a PIStack в \a PIVector.
PIVector<T> toVector() const { return PIVector<T>(*this); }
//! \~english Converts \a PIStack to \a PIDeque.
//! \~russian Преобразует \a PIStack в \a PIDeque.
PIDeque<T> toDeque() const { return PIDeque<T>(PIVector<T>::data(), PIVector<T>::size()); }
}; };
#endif // PISTACK_H #endif // PISTACK_H

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