Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d53efcedbe | |||
| d712294190 | |||
| 45f4155b6d | |||
| 99b8609c87 | |||
| 813bc0977a | |||
| 2895327752 | |||
|
|
fa431d5be7 | ||
| d1242f969c | |||
|
|
ef5eb2c891 | ||
| d076b4ae38 |
117
Doxyfile
117
Doxyfile
@ -1,4 +1,4 @@
|
||||
# Doxyfile 1.8.14
|
||||
# Doxyfile 1.8.13
|
||||
|
||||
# This file describes the settings to be used by the documentation system
|
||||
# doxygen (www.doxygen.org) for a project.
|
||||
@ -20,8 +20,8 @@
|
||||
# This tag specifies the encoding used for all characters in the config file
|
||||
# that follow. The default is UTF-8 which is also the encoding used for all text
|
||||
# before the first occurrence of this tag. Doxygen uses libiconv (or the iconv
|
||||
# built into libc) for the transcoding. See
|
||||
# https://www.gnu.org/software/libiconv/ for the list of possible encodings.
|
||||
# built into libc) for the transcoding. See http://www.gnu.org/software/libiconv
|
||||
# for the list of possible encodings.
|
||||
# The default value is: UTF-8.
|
||||
|
||||
DOXYFILE_ENCODING = UTF-8
|
||||
@ -58,7 +58,7 @@ PROJECT_LOGO =
|
||||
# entered, it will be relative to the location where doxygen was started. If
|
||||
# left blank the current directory will be used.
|
||||
|
||||
OUTPUT_DIRECTORY = W:\Work\Software\Libraries\utl\doc
|
||||
OUTPUT_DIRECTORY = doc
|
||||
|
||||
# If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub-
|
||||
# directories (in 2 levels) under the output directory of each output format and
|
||||
@ -150,7 +150,7 @@ INLINE_INHERITED_MEMB = NO
|
||||
# shortest path that makes the file name unique will be used
|
||||
# The default value is: YES.
|
||||
|
||||
FULL_PATH_NAMES = YES
|
||||
FULL_PATH_NAMES = NO
|
||||
|
||||
# The STRIP_FROM_PATH tag can be used to strip a user-defined part of the path.
|
||||
# Stripping is only done if one of the specified strings matches the left-hand
|
||||
@ -337,7 +337,7 @@ BUILTIN_STL_SUPPORT = YES
|
||||
CPP_CLI_SUPPORT = NO
|
||||
|
||||
# Set the SIP_SUPPORT tag to YES if your project consists of sip (see:
|
||||
# https://www.riverbankcomputing.com/software/sip/intro) sources only. Doxygen
|
||||
# http://www.riverbankcomputing.co.uk/software/sip/intro) sources only. Doxygen
|
||||
# will parse them like normal C++ but will assume all classes use public instead
|
||||
# of private inheritance when no explicit protection keyword is present.
|
||||
# The default value is: NO.
|
||||
@ -360,14 +360,14 @@ IDL_PROPERTY_SUPPORT = YES
|
||||
# all members of a group must be documented explicitly.
|
||||
# The default value is: NO.
|
||||
|
||||
DISTRIBUTE_GROUP_DOC = NO
|
||||
DISTRIBUTE_GROUP_DOC = YES
|
||||
|
||||
# If one adds a struct or class to a group and this option is enabled, then also
|
||||
# any nested class or struct is added to the same group. By default this option
|
||||
# is disabled and one has to add nested compounds explicitly via \ingroup.
|
||||
# The default value is: NO.
|
||||
|
||||
GROUP_NESTED_COMPOUNDS = NO
|
||||
GROUP_NESTED_COMPOUNDS = YES
|
||||
|
||||
# Set the SUBGROUPING tag to YES to allow class member groups of the same type
|
||||
# (for instance a group of public functions) to be put as a subgroup of that
|
||||
@ -408,7 +408,7 @@ INLINE_SIMPLE_STRUCTS = NO
|
||||
# types are typedef'ed and only the typedef is referenced, never the tag name.
|
||||
# The default value is: NO.
|
||||
|
||||
TYPEDEF_HIDES_STRUCT = NO
|
||||
TYPEDEF_HIDES_STRUCT = YES
|
||||
|
||||
# The size of the symbol lookup cache can be set using LOOKUP_CACHE_SIZE. This
|
||||
# cache is used to resolve symbols given their name and scope. Since this can be
|
||||
@ -435,7 +435,7 @@ LOOKUP_CACHE_SIZE = 0
|
||||
# normally produced when WARNINGS is set to YES.
|
||||
# The default value is: NO.
|
||||
|
||||
EXTRACT_ALL = YES
|
||||
EXTRACT_ALL = NO
|
||||
|
||||
# If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will
|
||||
# be included in the documentation.
|
||||
@ -447,7 +447,7 @@ EXTRACT_PRIVATE = YES
|
||||
# scope will be included in the documentation.
|
||||
# The default value is: NO.
|
||||
|
||||
EXTRACT_PACKAGE = YES
|
||||
EXTRACT_PACKAGE = NO
|
||||
|
||||
# If the EXTRACT_STATIC tag is set to YES, all static members of a file will be
|
||||
# included in the documentation.
|
||||
@ -469,7 +469,7 @@ EXTRACT_LOCAL_CLASSES = YES
|
||||
# included.
|
||||
# The default value is: NO.
|
||||
|
||||
EXTRACT_LOCAL_METHODS = YES
|
||||
EXTRACT_LOCAL_METHODS = NO
|
||||
|
||||
# If this flag is set to YES, the members of anonymous namespaces will be
|
||||
# extracted and appear in the documentation as a namespace called
|
||||
@ -486,7 +486,7 @@ EXTRACT_ANON_NSPACES = NO
|
||||
# section is generated. This option has no effect if EXTRACT_ALL is enabled.
|
||||
# The default value is: NO.
|
||||
|
||||
HIDE_UNDOC_MEMBERS = NO
|
||||
HIDE_UNDOC_MEMBERS = YES
|
||||
|
||||
# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all
|
||||
# undocumented classes that are normally visible in the class hierarchy. If set
|
||||
@ -494,7 +494,7 @@ HIDE_UNDOC_MEMBERS = NO
|
||||
# has no effect if EXTRACT_ALL is enabled.
|
||||
# The default value is: NO.
|
||||
|
||||
HIDE_UNDOC_CLASSES = NO
|
||||
HIDE_UNDOC_CLASSES = YES
|
||||
|
||||
# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, doxygen will hide all friend
|
||||
# (class|struct|union) declarations. If set to NO, these declarations will be
|
||||
@ -508,14 +508,14 @@ HIDE_FRIEND_COMPOUNDS = NO
|
||||
# blocks will be appended to the function's detailed documentation block.
|
||||
# The default value is: NO.
|
||||
|
||||
HIDE_IN_BODY_DOCS = NO
|
||||
HIDE_IN_BODY_DOCS = YES
|
||||
|
||||
# The INTERNAL_DOCS tag determines if documentation that is typed after a
|
||||
# \internal command is included. If the tag is set to NO then the documentation
|
||||
# will be excluded. Set it to YES to include the internal documentation.
|
||||
# The default value is: NO.
|
||||
|
||||
INTERNAL_DOCS = NO
|
||||
INTERNAL_DOCS = YES
|
||||
|
||||
# If the CASE_SENSE_NAMES tag is set to NO then doxygen will only generate file
|
||||
# names in lower-case letters. If set to YES, upper-case letters are also
|
||||
@ -570,7 +570,7 @@ INLINE_INFO = YES
|
||||
# name. If set to NO, the members will appear in declaration order.
|
||||
# The default value is: YES.
|
||||
|
||||
SORT_MEMBER_DOCS = YES
|
||||
SORT_MEMBER_DOCS = NO
|
||||
|
||||
# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the brief
|
||||
# descriptions of file, namespace and class members alphabetically by member
|
||||
@ -708,7 +708,7 @@ LAYOUT_FILE =
|
||||
# The CITE_BIB_FILES tag can be used to specify one or more bib files containing
|
||||
# the reference definitions. This must be a list of .bib files. The .bib
|
||||
# extension is automatically appended if omitted. This requires the bibtex tool
|
||||
# to be installed. See also https://en.wikipedia.org/wiki/BibTeX for more info.
|
||||
# to be installed. See also http://en.wikipedia.org/wiki/BibTeX for more info.
|
||||
# For LaTeX the style of the bibliography can be controlled using
|
||||
# LATEX_BIB_STYLE. To use this feature you need bibtex and perl available in the
|
||||
# search path. See also \cite for info how to create references.
|
||||
@ -790,12 +790,12 @@ WARN_LOGFILE =
|
||||
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
|
||||
# Note: If this tag is empty the current directory is searched.
|
||||
|
||||
INPUT =
|
||||
INPUT = .
|
||||
|
||||
# This tag can be used to specify the character encoding of the source files
|
||||
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
|
||||
# libiconv (or the iconv built into libc) for the transcoding. See the libiconv
|
||||
# documentation (see: https://www.gnu.org/software/libiconv/) for the list of
|
||||
# documentation (see: http://www.gnu.org/software/libiconv) for the list of
|
||||
# possible encodings.
|
||||
# The default value is: UTF-8.
|
||||
|
||||
@ -873,7 +873,11 @@ RECURSIVE = YES
|
||||
# Note that relative paths are relative to the directory from which doxygen is
|
||||
# run.
|
||||
|
||||
EXCLUDE = test
|
||||
EXCLUDE = test \
|
||||
include/utl/dev \
|
||||
include/utl/concepts \
|
||||
include/utl/com \
|
||||
include/utl/container
|
||||
|
||||
# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
|
||||
# directories that are symbolic links (a Unix file system feature) are excluded
|
||||
@ -1043,7 +1047,7 @@ SOURCE_TOOLTIPS = YES
|
||||
# If the USE_HTAGS tag is set to YES then the references to source code will
|
||||
# point to the HTML generated by the htags(1) tool instead of doxygen built-in
|
||||
# source browser. The htags tool is part of GNU's global source tagging system
|
||||
# (see https://www.gnu.org/software/global/global.html). You will need version
|
||||
# (see http://www.gnu.org/software/global/global.html). You will need version
|
||||
# 4.8.6 or higher.
|
||||
#
|
||||
# To use it do the following:
|
||||
@ -1079,7 +1083,7 @@ VERBATIM_HEADERS = YES
|
||||
# generated with the -Duse-libclang=ON option for CMake.
|
||||
# The default value is: NO.
|
||||
|
||||
CLANG_ASSISTED_PARSING = NO
|
||||
CLANG_ASSISTED_PARSING = YES
|
||||
|
||||
# If clang assisted parsing is enabled you can provide the compiler with command
|
||||
# line options that you would normally use when invoking the compiler. Note that
|
||||
@ -1087,18 +1091,7 @@ CLANG_ASSISTED_PARSING = NO
|
||||
# specified with INPUT and INCLUDE_PATH.
|
||||
# This tag requires that the tag CLANG_ASSISTED_PARSING is set to YES.
|
||||
|
||||
CLANG_OPTIONS =
|
||||
|
||||
# If clang assisted parsing is enabled you can provide the clang parser with the
|
||||
# path to the compilation database (see:
|
||||
# http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html) used when the files
|
||||
# were built. This is equivalent to specifying the "-p" option to a clang tool,
|
||||
# such as clang-check. These options will then be pased to the parser.
|
||||
# Note: The availability of this option depends on whether or not doxygen was
|
||||
# generated with the -Duse-libclang=ON option for CMake.
|
||||
# The default value is: 0.
|
||||
|
||||
CLANG_COMPILATION_DATABASE_PATH= 0
|
||||
CLANG_OPTIONS = -std=c++14
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# Configuration options related to the alphabetical class index
|
||||
@ -1218,7 +1211,7 @@ HTML_EXTRA_FILES =
|
||||
# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. Doxygen
|
||||
# will adjust the colors in the style sheet and background images according to
|
||||
# this color. Hue is specified as an angle on a colorwheel, see
|
||||
# https://en.wikipedia.org/wiki/Hue for more information. For instance the value
|
||||
# http://en.wikipedia.org/wiki/Hue for more information. For instance the value
|
||||
# 0 represents red, 60 is yellow, 120 is green, 180 is cyan, 240 is blue, 300
|
||||
# purple, and 360 is red again.
|
||||
# Minimum value: 0, maximum value: 359, default value: 220.
|
||||
@ -1254,17 +1247,6 @@ HTML_COLORSTYLE_GAMMA = 80
|
||||
|
||||
HTML_TIMESTAMP = NO
|
||||
|
||||
# If the HTML_DYNAMIC_MENUS tag is set to YES then the generated HTML
|
||||
# documentation will contain a main index with vertical navigation menus that
|
||||
# are dynamically created via Javascript. If disabled, the navigation index will
|
||||
# consists of multiple levels of tabs that are statically embedded in every HTML
|
||||
# page. Disable this option to support browsers that do not have Javascript,
|
||||
# like the Qt help browser.
|
||||
# The default value is: YES.
|
||||
# This tag requires that the tag GENERATE_HTML is set to YES.
|
||||
|
||||
HTML_DYNAMIC_MENUS = YES
|
||||
|
||||
# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML
|
||||
# documentation will contain sections that can be hidden and shown after the
|
||||
# page has loaded.
|
||||
@ -1288,12 +1270,12 @@ HTML_INDEX_NUM_ENTRIES = 100
|
||||
|
||||
# If the GENERATE_DOCSET tag is set to YES, additional index files will be
|
||||
# generated that can be used as input for Apple's Xcode 3 integrated development
|
||||
# environment (see: https://developer.apple.com/tools/xcode/), introduced with
|
||||
# environment (see: http://developer.apple.com/tools/xcode/), introduced with
|
||||
# OSX 10.5 (Leopard). To create a documentation set, doxygen will generate a
|
||||
# Makefile in the HTML output directory. Running make will produce the docset in
|
||||
# that directory and running make install will install the docset in
|
||||
# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find it at
|
||||
# startup. See https://developer.apple.com/tools/creatingdocsetswithdoxygen.html
|
||||
# startup. See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html
|
||||
# for more information.
|
||||
# The default value is: NO.
|
||||
# This tag requires that the tag GENERATE_HTML is set to YES.
|
||||
@ -1409,7 +1391,7 @@ QCH_FILE =
|
||||
|
||||
# The QHP_NAMESPACE tag specifies the namespace to use when generating Qt Help
|
||||
# Project output. For more information please see Qt Help Project / Namespace
|
||||
# (see: http://doc.qt.io/qt-4.8/qthelpproject.html#namespace).
|
||||
# (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#namespace).
|
||||
# The default value is: org.doxygen.Project.
|
||||
# This tag requires that the tag GENERATE_QHP is set to YES.
|
||||
|
||||
@ -1417,7 +1399,8 @@ QHP_NAMESPACE = org.doxygen.Project
|
||||
|
||||
# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating Qt
|
||||
# Help Project output. For more information please see Qt Help Project / Virtual
|
||||
# Folders (see: http://doc.qt.io/qt-4.8/qthelpproject.html#virtual-folders).
|
||||
# Folders (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#virtual-
|
||||
# folders).
|
||||
# The default value is: doc.
|
||||
# This tag requires that the tag GENERATE_QHP is set to YES.
|
||||
|
||||
@ -1425,21 +1408,23 @@ QHP_VIRTUAL_FOLDER = doc
|
||||
|
||||
# If the QHP_CUST_FILTER_NAME tag is set, it specifies the name of a custom
|
||||
# filter to add. For more information please see Qt Help Project / Custom
|
||||
# Filters (see: http://doc.qt.io/qt-4.8/qthelpproject.html#custom-filters).
|
||||
# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom-
|
||||
# filters).
|
||||
# This tag requires that the tag GENERATE_QHP is set to YES.
|
||||
|
||||
QHP_CUST_FILTER_NAME =
|
||||
|
||||
# The QHP_CUST_FILTER_ATTRS tag specifies the list of the attributes of the
|
||||
# custom filter to add. For more information please see Qt Help Project / Custom
|
||||
# Filters (see: http://doc.qt.io/qt-4.8/qthelpproject.html#custom-filters).
|
||||
# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom-
|
||||
# filters).
|
||||
# This tag requires that the tag GENERATE_QHP is set to YES.
|
||||
|
||||
QHP_CUST_FILTER_ATTRS =
|
||||
|
||||
# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this
|
||||
# project's filter section matches. Qt Help Project / Filter Attributes (see:
|
||||
# http://doc.qt.io/qt-4.8/qthelpproject.html#filter-attributes).
|
||||
# http://qt-project.org/doc/qt-4.8/qthelpproject.html#filter-attributes).
|
||||
# This tag requires that the tag GENERATE_QHP is set to YES.
|
||||
|
||||
QHP_SECT_FILTER_ATTRS =
|
||||
@ -1532,7 +1517,7 @@ EXT_LINKS_IN_WINDOW = NO
|
||||
|
||||
FORMULA_FONTSIZE = 10
|
||||
|
||||
# Use the FORMULA_TRANSPARENT tag to determine whether or not the images
|
||||
# Use the FORMULA_TRANPARENT tag to determine whether or not the images
|
||||
# generated for formulas are transparent PNGs. Transparent PNGs are not
|
||||
# supported properly for IE 6.0, but are supported on all modern browsers.
|
||||
#
|
||||
@ -1544,7 +1529,7 @@ FORMULA_FONTSIZE = 10
|
||||
FORMULA_TRANSPARENT = YES
|
||||
|
||||
# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax (see
|
||||
# https://www.mathjax.org) which uses client side Javascript for the rendering
|
||||
# http://www.mathjax.org) which uses client side Javascript for the rendering
|
||||
# instead of using pre-rendered bitmaps. Use this if you do not have LaTeX
|
||||
# installed or if you want to formulas look prettier in the HTML output. When
|
||||
# enabled you may also need to install MathJax separately and configure the path
|
||||
@ -1571,7 +1556,7 @@ MATHJAX_FORMAT = HTML-CSS
|
||||
# MATHJAX_RELPATH should be ../mathjax. The default value points to the MathJax
|
||||
# Content Delivery Network so you can quickly see the result without installing
|
||||
# MathJax. However, it is strongly recommended to install a local copy of
|
||||
# MathJax from https://www.mathjax.org before deployment.
|
||||
# MathJax from http://www.mathjax.org before deployment.
|
||||
# The default value is: http://cdn.mathjax.org/mathjax/latest.
|
||||
# This tag requires that the tag USE_MATHJAX is set to YES.
|
||||
|
||||
@ -1633,7 +1618,7 @@ SERVER_BASED_SEARCH = NO
|
||||
#
|
||||
# Doxygen ships with an example indexer (doxyindexer) and search engine
|
||||
# (doxysearch.cgi) which are based on the open source search engine library
|
||||
# Xapian (see: https://xapian.org/).
|
||||
# Xapian (see: http://xapian.org/).
|
||||
#
|
||||
# See the section "External Indexing and Searching" for details.
|
||||
# The default value is: NO.
|
||||
@ -1646,7 +1631,7 @@ EXTERNAL_SEARCH = NO
|
||||
#
|
||||
# Doxygen ships with an example indexer (doxyindexer) and search engine
|
||||
# (doxysearch.cgi) which are based on the open source search engine library
|
||||
# Xapian (see: https://xapian.org/). See the section "External Indexing and
|
||||
# Xapian (see: http://xapian.org/). See the section "External Indexing and
|
||||
# Searching" for details.
|
||||
# This tag requires that the tag SEARCHENGINE is set to YES.
|
||||
|
||||
@ -1833,7 +1818,7 @@ LATEX_SOURCE_CODE = NO
|
||||
|
||||
# The LATEX_BIB_STYLE tag can be used to specify the style to use for the
|
||||
# bibliography, e.g. plainnat, or ieeetr. See
|
||||
# https://en.wikipedia.org/wiki/BibTeX and \cite for more info.
|
||||
# http://en.wikipedia.org/wiki/BibTeX and \cite for more info.
|
||||
# The default value is: plain.
|
||||
# This tag requires that the tag GENERATE_LATEX is set to YES.
|
||||
|
||||
@ -2016,9 +2001,9 @@ DOCBOOK_PROGRAMLISTING = NO
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
# If the GENERATE_AUTOGEN_DEF tag is set to YES, doxygen will generate an
|
||||
# AutoGen Definitions (see http://autogen.sourceforge.net/) file that captures
|
||||
# the structure of the code including all documentation. Note that this feature
|
||||
# is still experimental and incomplete at the moment.
|
||||
# AutoGen Definitions (see http://autogen.sf.net) file that captures the
|
||||
# structure of the code including all documentation. Note that this feature is
|
||||
# still experimental and incomplete at the moment.
|
||||
# The default value is: NO.
|
||||
|
||||
GENERATE_AUTOGEN_DEF = NO
|
||||
@ -2231,7 +2216,7 @@ HIDE_UNDOC_RELATIONS = YES
|
||||
# http://www.graphviz.org/), a graph visualization toolkit from AT&T and Lucent
|
||||
# Bell Labs. The other options in this section have no effect if this option is
|
||||
# set to NO
|
||||
# The default value is: NO.
|
||||
# The default value is: YES.
|
||||
|
||||
HAVE_DOT = NO
|
||||
|
||||
@ -2387,7 +2372,9 @@ DIRECTORY_GRAPH = YES
|
||||
# Note: If you choose svg you need to set HTML_FILE_EXTENSION to xhtml in order
|
||||
# to make the SVG files visible in IE 9+ (other browsers do not have this
|
||||
# requirement).
|
||||
# Possible values are: png, jpg, gif, svg, png:gd, png:gd:gd, png:cairo,
|
||||
# Possible values are: png, png:cairo, png:cairo:cairo, png:cairo:gd, png:gd,
|
||||
# png:gd:gd, jpg, jpg:cairo, jpg:cairo:gd, jpg:gd, jpg:gd:gd, gif, gif:cairo,
|
||||
# gif:cairo:gd, gif:gd, gif:gd:gd, svg, png:gd, png:gd:gd, png:cairo,
|
||||
# png:cairo:gd, png:cairo:cairo, png:cairo:gdiplus, png:gdiplus and
|
||||
# png:gdiplus:gdiplus.
|
||||
# The default value is: png.
|
||||
|
||||
@ -19,7 +19,7 @@
|
||||
*
|
||||
*/
|
||||
#ifndef __utl_concepts_concepts_h__
|
||||
#define __utl_consepts_concepts_h__
|
||||
#define __utl_concepts_concepts_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
|
||||
|
||||
@ -1,22 +1,6 @@
|
||||
/*!
|
||||
* \file /utl/concepts/stl.h
|
||||
* \brief STL's Concepts
|
||||
*
|
||||
* Copyright (C) 2018 - 2019 Christos Choutouridis
|
||||
*
|
||||
* 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 __utl_concepts_stl_h__
|
||||
#define __utl_concepts_stl_h__
|
||||
@ -50,7 +34,7 @@ namespace utl {
|
||||
template <typename _T1, typename _T2, typename _Ret =_T1>
|
||||
using use_if_same_t = meta::eval<
|
||||
meta::enable_if<
|
||||
meta::same_<_T1, _T2>::value, _Ret
|
||||
meta::same<_T1, _T2>::value, _Ret
|
||||
>
|
||||
>;
|
||||
|
||||
@ -59,7 +43,7 @@ namespace utl {
|
||||
* Same
|
||||
*/
|
||||
template <class T, class U>
|
||||
_utlConcept Same = meta::same_<T, U>::value;
|
||||
_utlConcept Same = meta::same<T, U>::value;
|
||||
|
||||
// template<class T>
|
||||
// _utlConcept Decayed = Same<T, std::decay_t<T>>;
|
||||
|
||||
225
include/utl/container/deque.h
Normal file
225
include/utl/container/deque.h
Normal file
@ -0,0 +1,225 @@
|
||||
/*!
|
||||
* \file container/deque.h
|
||||
* \brief
|
||||
* A statically allocated deque based on a ring buffer.
|
||||
*
|
||||
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*/
|
||||
|
||||
#ifndef utl_container_deque_h__
|
||||
#define utl_container_deque_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/container/ring_iterator.h>
|
||||
#include <utl/container/range.h>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
|
||||
namespace utl {
|
||||
|
||||
/*!
|
||||
* \class deque
|
||||
* \brief
|
||||
* A statically allocated deque based on a ring buffer
|
||||
*
|
||||
* The deque uses two ring_iterators one for the front and one for the rear. The iterators
|
||||
* are pointing to the next available spot, not on the last inserted spot. This way at the
|
||||
* initialization the iterators wont "pretend" to point to a valid item .
|
||||
*
|
||||
* We use a ring buffer of size \c N+1. We start the front iterator at the last location of the buffer
|
||||
* and the rear on the first. This way when the queue is full the iterators are pointing to the same location.
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of deque
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
* \note
|
||||
* SemiAtomic means it is safe to access different ends from different threads. For example one thread can
|
||||
* push only from front and another can pop from back to implement a queue.
|
||||
*/
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false>
|
||||
class deque {
|
||||
public:
|
||||
// meta-identity type
|
||||
using type = deque<Data_t, N>;
|
||||
using buffer_t = std::array<Data_t, N+1>; // We need N+1 spaces ring buffer for N spaces deque
|
||||
using iterator_t = ring_iterator<Data_t*, N+1, SemiAtomic>;
|
||||
using range_t = range<iterator_t>;
|
||||
|
||||
// STL
|
||||
using value_type = Data_t;
|
||||
using reference = Data_t&;
|
||||
using const_reference = const Data_t&;
|
||||
using pointer = Data_t*;
|
||||
using const_pointer = const Data_t*;
|
||||
using iterator = iterator_t;
|
||||
using const_iterator = const iterator_t;
|
||||
using reverse_iterator = std::reverse_iterator<iterator>;
|
||||
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||
|
||||
//! \name Constructor / Destructor
|
||||
//! @{
|
||||
public:
|
||||
//! Default constructor
|
||||
constexpr deque () noexcept :
|
||||
data_{},
|
||||
f{data_.data(), N},
|
||||
r{data_.data()} {
|
||||
if constexpr (SemiAtomic)
|
||||
std::atomic_thread_fence(std::memory_order_release);
|
||||
}
|
||||
|
||||
//! fill contructor
|
||||
constexpr deque(const Data_t& value) noexcept {
|
||||
data_.fill(value);
|
||||
f = iterator(data_.data(), N);
|
||||
r = iterator(data_.data(), N);
|
||||
if constexpr (SemiAtomic)
|
||||
std::atomic_thread_fence(std::memory_order_release);
|
||||
}
|
||||
|
||||
//! Initializer list contructor
|
||||
template <typename ...It>
|
||||
constexpr deque(It&& ...it) noexcept :
|
||||
data_{{std::forward<It>(it)...}},
|
||||
f(data_.data(), N),
|
||||
r(data_.data(), sizeof...(It)) {
|
||||
if constexpr (SemiAtomic)
|
||||
std::atomic_thread_fence(std::memory_order_release);
|
||||
}
|
||||
|
||||
deque(const deque&) = delete; //!< No copies
|
||||
deque& operator= (const deque&) = delete; //!< No copy assignments
|
||||
~deque () = default; //!< default destructor
|
||||
//! @}
|
||||
|
||||
//! \name Iterators
|
||||
//! @{
|
||||
public:
|
||||
constexpr iterator begin() noexcept { iterator ret = f; return ++ret; }
|
||||
constexpr const_iterator begin() const noexcept { iterator ret = f; return ++ret; }
|
||||
constexpr const_iterator cbegin() const noexcept { iterator ret = f; return ++ret; }
|
||||
|
||||
constexpr iterator end() noexcept { return r; }
|
||||
constexpr const_iterator end() const noexcept { return r; }
|
||||
constexpr const_iterator cend() const noexcept { return r; }
|
||||
|
||||
constexpr reverse_iterator rbegin() noexcept { return r; }
|
||||
constexpr const_reverse_iterator rbegin() const noexcept { return r; }
|
||||
constexpr const_reverse_iterator crbegin() const noexcept { return r; }
|
||||
|
||||
constexpr reverse_iterator rend() noexcept { reverse_iterator ret = f; return ++ret; }
|
||||
constexpr const_reverse_iterator rend() const noexcept { reverse_iterator ret = f; return ++ret; }
|
||||
constexpr const_reverse_iterator crend() const noexcept { reverse_iterator ret = f; return ++ret; }
|
||||
//! @}
|
||||
|
||||
//! \name Capacity
|
||||
//! @{
|
||||
public:
|
||||
//! \return The size of the deque. The items currently in queue.
|
||||
constexpr size_t size() noexcept {
|
||||
return r - (f +1);
|
||||
}
|
||||
constexpr size_t size() const noexcept {
|
||||
return r - (f +1);
|
||||
}
|
||||
//! \return The maximum size of the deque. The items the queue can hold.
|
||||
constexpr size_t max_size() noexcept { return N; }
|
||||
//! \return The capacity of the deque. The items the queue can hold.
|
||||
constexpr size_t capacity() noexcept { return N; }
|
||||
//! \return True if the deque is empty
|
||||
constexpr bool empty() noexcept { return size() == 0 ? true : false; }
|
||||
//! \return True if the deque is full
|
||||
constexpr bool full() noexcept { return size() == N ? true : false; }
|
||||
|
||||
//! @}
|
||||
|
||||
//! \name Member access
|
||||
//! @{
|
||||
public:
|
||||
//! \brief Clears-empty the deque and return it to init state, without
|
||||
//! really deleting the contents.
|
||||
constexpr void clear() noexcept {
|
||||
f = iterator_t(data_.data(), N);
|
||||
r = iterator_t(data_.data());
|
||||
if constexpr (SemiAtomic)
|
||||
std::atomic_thread_fence(std::memory_order_release);
|
||||
}
|
||||
//! \brief Push an item in the front of the deque
|
||||
//! \param it The item to push
|
||||
constexpr void push_front (const Data_t& it) noexcept {
|
||||
if (full()) return;
|
||||
*f = it;
|
||||
--f; // keep this separate for thread safety
|
||||
}
|
||||
//! \brief Push an item in the back of the deque
|
||||
//! \param it The item to push
|
||||
constexpr void push_back (const Data_t& it) noexcept {
|
||||
if (full()) return;
|
||||
*r = it;
|
||||
++r; // keep this separate for thread safety
|
||||
}
|
||||
//! \brief Extract an item from the front of the deque and remove it from the deque
|
||||
//! \param it The item to push
|
||||
constexpr Data_t pop_front () noexcept {
|
||||
if (empty()) return Data_t{};
|
||||
return *++f;
|
||||
}
|
||||
//! \brief Extract an item from the back of the deque and remove it from the deque
|
||||
//! \param it The item to push
|
||||
constexpr Data_t pop_back () noexcept {
|
||||
if (empty()) return Data_t{};
|
||||
return *--r;
|
||||
}
|
||||
|
||||
//! \brief Get a reference to the item in the front of the deque without extracting it.
|
||||
//! \return Reference to the item
|
||||
constexpr Data_t& front() noexcept { iterator_t it = f; return *++it; }
|
||||
constexpr const Data_t& front() const noexcept { iterator_t it = f; return *++it; }
|
||||
|
||||
//! \brief Get a reference to the item in the front of the deque without extracting it.
|
||||
//! \return Reference to the item
|
||||
constexpr Data_t& back() noexcept { iterator_t it = r; return *--it; }
|
||||
constexpr const Data_t& back() const noexcept { iterator_t it = r; return *--it; }
|
||||
|
||||
//! \brief Get a pointer to the begin of the items on the deque
|
||||
//! \return
|
||||
constexpr Data_t* data() noexcept { return &front(); }
|
||||
constexpr const Data_t* data() const noexcept { return &front(); }
|
||||
|
||||
//! \brief Get a range for the data in queue
|
||||
//! \return A begin-end iterator pair struct
|
||||
constexpr range_t contents () noexcept { iterator_t b = f; return {++b, r}; }
|
||||
constexpr const range_t contents () const noexcept { iterator_t b = f; return {++b, r}; }
|
||||
|
||||
//! @}
|
||||
private:
|
||||
buffer_t data_{}; //!< The statically allocated buffer
|
||||
iterator_t f{data_.data(), N}; //!< A ring iterator for the front (points to the next available location)
|
||||
iterator_t r{data_.data()}; //!< A ring iterator for the rear (points to the next available location).
|
||||
};
|
||||
|
||||
} // namespace utl
|
||||
|
||||
#endif /* utl_container_deque_h__ */
|
||||
299
include/utl/container/edeque.h
Normal file
299
include/utl/container/edeque.h
Normal file
@ -0,0 +1,299 @@
|
||||
/*!
|
||||
* \file container/edeque.h
|
||||
* \brief
|
||||
* A deque with event based callables
|
||||
*
|
||||
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*/
|
||||
|
||||
#ifndef utl_container_edeque_h__
|
||||
#define utl_container_edeque_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/container/deque.h>
|
||||
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
|
||||
namespace utl {
|
||||
|
||||
/*!
|
||||
* \class edeque
|
||||
* \brief
|
||||
* A statically allocated deque with size and data matching event based callables.
|
||||
*
|
||||
* The edeque inherits deque and provide the callable functionality as a wrapper.
|
||||
*
|
||||
* There are two types of events.
|
||||
* - Size based events, which are cleared as soon as they served. These events are checked
|
||||
* every time the deque change its size. If the criteria match we call the callable of type
|
||||
* \c Fn
|
||||
* - Data based events, which are permanently. These events are checked every time an item is
|
||||
* pushed or popped from the deque. If the criteria match we call call the callable of type
|
||||
* \c Fn
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of edeque
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
* \tparam Fn The type of Callable
|
||||
* \note
|
||||
* SemiAtomic means it is safe to access different ends from different threads. For example one thread can
|
||||
* push only from front and another can pop from back to implement a queue.
|
||||
*/
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false, typename Fn = std::function<void()>>
|
||||
class edeque : public deque<Data_t, N, SemiAtomic> {
|
||||
public:
|
||||
// meta-identity types
|
||||
using type = edeque<Data_t, N, SemiAtomic, Fn>;
|
||||
using base_type = deque<Data_t, N, SemiAtomic>;
|
||||
using callable_t = Fn;
|
||||
using range_t = typename base_type::range_t;
|
||||
|
||||
// STL
|
||||
using value_type = typename base_type::value_type;
|
||||
using reference = typename base_type::reference;
|
||||
using const_reference = typename base_type::const_reference;
|
||||
using pointer = typename base_type::pointer;
|
||||
using const_pointer = typename base_type::const_pointer;
|
||||
using iterator = typename base_type::iterator;
|
||||
using const_iterator = typename base_type::const_iterator;
|
||||
using reverse_iterator = typename base_type::reverse_iterator;
|
||||
using const_reverse_iterator= typename base_type::const_reverse_iterator;
|
||||
|
||||
//! \name Public types
|
||||
//! @{
|
||||
public:
|
||||
//! \enum match_mode
|
||||
//! The mode of match operation
|
||||
enum class match_mode { SIZE, DATA };
|
||||
//! \enum size_match
|
||||
//! The type of matching for size based match
|
||||
enum class size_match { DISABLED =0, EQ, NE, LT, LE, GT, GE };
|
||||
//! \enum data_match
|
||||
//! The type of matching for data based match
|
||||
enum class data_match { DISABLED =0, MATCH_PUSH, MATCH_POP, MISMATCH_PUSH, MISMATCH_POP};
|
||||
|
||||
// TODO: trigger mode for one-shot or repeated functionality
|
||||
// enum class trigger_mode { ONE_SHOT, REPEATED };
|
||||
|
||||
//! \struct size_trigger
|
||||
//! Size trigger data type
|
||||
struct size_trigger {
|
||||
size_match type;
|
||||
size_t size;
|
||||
};
|
||||
//! \struct data_trigger
|
||||
//! Data trigger data type
|
||||
struct data_trigger {
|
||||
data_match type;
|
||||
Data_t value;
|
||||
};
|
||||
//! \union trigger
|
||||
//! \brief
|
||||
//! A union for the common types.
|
||||
//! There is only one mode. Either "size" with \ref size_match type and a size to match,
|
||||
//! or "data" with \ref data_match type and a value to match
|
||||
union trigger {
|
||||
size_trigger tsize;
|
||||
data_trigger tdata;
|
||||
};
|
||||
//! @}
|
||||
|
||||
//! \name Constructor / Destructor
|
||||
//! @{
|
||||
public:
|
||||
//! Default constructor
|
||||
constexpr edeque () noexcept :
|
||||
base_type() { }
|
||||
|
||||
//! Size trigger constructor
|
||||
constexpr edeque (size_match match, size_t size, callable_t&& fn) noexcept :
|
||||
base_type(),
|
||||
mode_{match_mode::SIZE},
|
||||
callback_{std::forward<callable_t>(fn)} {
|
||||
trigger_.tsize.type = match;
|
||||
trigger_.tsize.size = size;
|
||||
}
|
||||
//! Data trigger constructor
|
||||
constexpr edeque (data_match match, Data_t value, callable_t&& fn) noexcept :
|
||||
base_type(),
|
||||
mode_{match_mode::DATA},
|
||||
callback_{std::forward<callable_t>(fn)} {
|
||||
trigger_.tdata.type = match;
|
||||
trigger_.tdata.value = value;
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Public interface
|
||||
//! @{
|
||||
|
||||
//! \brief
|
||||
//! Manually checks the size trigger and calls it we have match.
|
||||
//! \return True if the callable has called.
|
||||
bool check_trigger () noexcept {
|
||||
return check_trigger_size_();
|
||||
}
|
||||
|
||||
//! \brief
|
||||
//! Manually set (or alters) the \c size trigger. This function does not fire the
|
||||
//! \ref check_trigger()
|
||||
//! \param match The match type
|
||||
//! \param size The size for with we check against
|
||||
//! \param fn The callable to call on match
|
||||
void set_trigger (size_match match, size_t size, callable_t&& fn) noexcept {
|
||||
mode_ = match_mode::SIZE;
|
||||
trigger_.tsize.type = match;
|
||||
trigger_.tsize.size = size;
|
||||
callback_ = std::forward<callable_t>(fn);
|
||||
}
|
||||
|
||||
//! \brief
|
||||
//! Manually set (or alters) the \c data trigger. This function does not fire the
|
||||
//! \ref check_trigger()
|
||||
//! \param match The match type
|
||||
//! \param value The value for with we check against
|
||||
//! \param fn The callable to call on match
|
||||
void set_trigger (data_match match, Data_t value, callable_t&& fn) noexcept {
|
||||
mode_ = match_mode::DATA;
|
||||
trigger_.tdata.type = match;
|
||||
trigger_.tdata.value= value;
|
||||
callback_ = std::forward<callable_t>(fn);
|
||||
}
|
||||
|
||||
//! \brief Manually clears the trigger
|
||||
void clear_trigger () noexcept {
|
||||
mode_ = match_mode{};
|
||||
trigger_ = trigger{};
|
||||
callback_ = callable_t{};
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Base class uses and overwrites
|
||||
//! @{
|
||||
void push_front (const Data_t& it) noexcept {
|
||||
base_type::push_front(it);
|
||||
check_trigger_push_async_(it);
|
||||
}
|
||||
void push_back (const Data_t& it) noexcept {
|
||||
base_type::push_back(it);
|
||||
check_trigger_push_async_(it);
|
||||
}
|
||||
Data_t pop_front () noexcept {
|
||||
Data_t t = base_type::pop_front();
|
||||
check_trigger_pop_async_(t);
|
||||
return t;
|
||||
}
|
||||
Data_t pop_back () noexcept {
|
||||
Data_t t = base_type::pop_back();
|
||||
check_trigger_pop_async_(t);
|
||||
return t;
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Private functionality
|
||||
//! @{
|
||||
private:
|
||||
//! \brief
|
||||
//! Manually checks the size trigger and calls it we have match.
|
||||
//! \return True if the callable has called.
|
||||
bool check_trigger_size_ () {
|
||||
bool match;
|
||||
switch (trigger_.tsize.type) {
|
||||
default:
|
||||
case size_match::DISABLED: match = false; break;
|
||||
case size_match::EQ: match = (base_type::size() == trigger_.tsize.size); break;
|
||||
case size_match::NE: match = (base_type::size() != trigger_.tsize.size); break;
|
||||
case size_match::LT: match = (base_type::size() < trigger_.tsize.size); break;
|
||||
case size_match::LE: match = (base_type::size() <= trigger_.tsize.size); break;
|
||||
case size_match::GT: match = (base_type::size() > trigger_.tsize.size); break;
|
||||
case size_match::GE: match = (base_type::size() >= trigger_.tsize.size); break;
|
||||
}
|
||||
if (match) {
|
||||
callback_();
|
||||
clear_trigger();
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
//! \brief
|
||||
//! Manually checks the data trigger on push and calls it we have match.
|
||||
//! \param it The item to check against
|
||||
//! \return True if the callable has called.
|
||||
bool check_trigger_push_value_ (const Data_t& it) {
|
||||
bool match;
|
||||
switch (trigger_.tdata.type) {
|
||||
default:
|
||||
case data_match::DISABLED: match = false; break;
|
||||
case data_match::MATCH_PUSH: match = (it == trigger_.tdata.value); break;
|
||||
case data_match::MISMATCH_PUSH: match = (it != trigger_.tdata.value); break;
|
||||
}
|
||||
if (match)
|
||||
callback_();
|
||||
return match;
|
||||
}
|
||||
|
||||
//! \brief
|
||||
//! Manually checks the data trigger on pop and calls it we have match.
|
||||
//! \param it The item to check against
|
||||
//! \return True if the callable has called.
|
||||
bool check_trigger_pop_value_ (const Data_t& it) {
|
||||
bool match;
|
||||
switch (trigger_.tdata.type) {
|
||||
default:
|
||||
case data_match::DISABLED: match = false; break;
|
||||
case data_match::MATCH_POP: match = (it == trigger_.tdata.value); break;
|
||||
case data_match::MISMATCH_POP: match = (it != trigger_.tdata.value); break;
|
||||
}
|
||||
if (match)
|
||||
callback_();
|
||||
return match;
|
||||
}
|
||||
|
||||
//! Wrapper for both triggers at push
|
||||
bool check_trigger_push_async_ (const Data_t& it) {
|
||||
switch (mode_) {
|
||||
default:
|
||||
case match_mode::SIZE: return check_trigger_size_();
|
||||
case match_mode::DATA: return check_trigger_push_value_(it);
|
||||
}
|
||||
}
|
||||
//! Wrapper for both triggers at pop
|
||||
bool check_trigger_pop_async_ (const Data_t& it) {
|
||||
switch (mode_) {
|
||||
default:
|
||||
case match_mode::SIZE: return check_trigger_size_();
|
||||
case match_mode::DATA: return check_trigger_pop_value_(it);
|
||||
}
|
||||
}
|
||||
//! @}
|
||||
private:
|
||||
match_mode mode_{};
|
||||
trigger trigger_{};
|
||||
callable_t callback_{};
|
||||
};
|
||||
} // namespace utl
|
||||
|
||||
#endif /* utl_container_edeque_h__ */
|
||||
146
include/utl/container/equeue.h
Normal file
146
include/utl/container/equeue.h
Normal file
@ -0,0 +1,146 @@
|
||||
/*!
|
||||
* \file container/equeue.h
|
||||
* \brief
|
||||
* A queue with event based callables based on edeque.
|
||||
*
|
||||
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*/
|
||||
|
||||
#ifndef utl_container_equeue_h__
|
||||
#define utl_container_equeue_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/container/edeque.h>
|
||||
|
||||
namespace utl {
|
||||
|
||||
/*!
|
||||
* \class equeue
|
||||
* \brief
|
||||
* A statically allocated queue based on edeque with size and data matching
|
||||
* event based callables.
|
||||
*
|
||||
* We use the \ref edeque::push_back() and \ref edeque::pop_front() pair from edeque's
|
||||
* functionality, so at the \c push the increment performed after the insertion.
|
||||
* Similarly at the \c pop the decrement performed before the exctraction. This way also
|
||||
* the \ref edeque::front() and \ref edeque::back() stay the same ;)
|
||||
*
|
||||
* We also provide stream operators.
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of edeque
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
* \tparam Fn The type of Callable
|
||||
* \note
|
||||
* SemiAtomic means it is safe to for one thread to push only from front and another can pop.
|
||||
*/
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false, typename Fn = std::function<void()>>
|
||||
class equeue : public edeque<Data_t, N, SemiAtomic, Fn> {
|
||||
public:
|
||||
// meta-identity types
|
||||
using equeue_t = equeue<Data_t, N, SemiAtomic, Fn>;
|
||||
using base_type = edeque<Data_t, N, SemiAtomic, Fn>;
|
||||
using range_t = typename base_type::range_t;
|
||||
|
||||
// STL
|
||||
using value_type = typename base_type::value_type;
|
||||
using reference = typename base_type::reference;
|
||||
using const_reference = typename base_type::const_reference;
|
||||
using pointer = typename base_type::pointer;
|
||||
using const_pointer = typename base_type::const_pointer;
|
||||
using iterator = typename base_type::iterator;
|
||||
using const_iterator = typename base_type::const_iterator;
|
||||
using reverse_iterator = typename base_type::reverse_iterator;
|
||||
using const_reverse_iterator= typename base_type::const_reverse_iterator;
|
||||
|
||||
//! \name Constructor / Destructor
|
||||
//! @{
|
||||
public:
|
||||
//! Default constructor
|
||||
constexpr equeue () noexcept : base_type() { }
|
||||
|
||||
//! Forward constructor
|
||||
template <typename ...It>
|
||||
constexpr equeue(It&& ...it) noexcept : base_type(std::forward<It>(it)...) { }
|
||||
|
||||
//! @}
|
||||
|
||||
//! \name Member access
|
||||
//! @{
|
||||
public:
|
||||
//! \brief Push an item in the back of the queue
|
||||
//! \param it The item to push
|
||||
void push (const Data_t& it) noexcept {
|
||||
base_type::push_back(it);
|
||||
}
|
||||
|
||||
//! \brief Extract an item from the front of the queue and remove it from the queue
|
||||
//! \param it The item to push
|
||||
Data_t pop () noexcept {
|
||||
return base_type::pop_front();
|
||||
}
|
||||
|
||||
//! \brief Push an item in the back of the queue
|
||||
//! \param it The item to push
|
||||
equeue_t& operator<< (const Data_t& it) noexcept {
|
||||
push(it);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! \brief Push an item in the back of the queue
|
||||
//! \param it The item to push
|
||||
equeue_t& operator>> (Data_t& it) noexcept {
|
||||
it = pop();
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! @}
|
||||
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Pop an item from the front of the queue.
|
||||
*
|
||||
* This definition enables the "data << equeue" syntax for pop operation
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of queue
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
* \tparam Fn The type of Callable
|
||||
*
|
||||
* \param it The item to write to
|
||||
* \param q The queue to read from
|
||||
* \return Reference to the returned item
|
||||
*/
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false, typename Fn = std::function<void()>>
|
||||
Data_t& operator<< (Data_t& it, equeue<Data_t, N, SemiAtomic, Fn>& q) noexcept {
|
||||
it = q.pop();
|
||||
return it;
|
||||
}
|
||||
|
||||
} // namespace utl
|
||||
|
||||
#endif /* utl_container_equeue_h__ */
|
||||
144
include/utl/container/queue.h
Normal file
144
include/utl/container/queue.h
Normal file
@ -0,0 +1,144 @@
|
||||
/*!
|
||||
* \file container/queue.h
|
||||
* \brief
|
||||
* A statically allocated queue based on deque.
|
||||
*
|
||||
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*/
|
||||
|
||||
#ifndef utl_container_queue_h__
|
||||
#define utl_container_queue_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/container/deque.h>
|
||||
|
||||
namespace utl {
|
||||
|
||||
/*!
|
||||
* \class queue
|
||||
* \brief
|
||||
* A statically allocated queue based on deque.
|
||||
*
|
||||
* We use the \ref deque::push_back() and \ref deque::pop_front() pair from deque's
|
||||
* functionality, so at the \c push the increment performed after the insertion.
|
||||
* Similarly at the \c pop the decrement performed before the exctraction. This way also
|
||||
* the \ref deque::front() and \ref deque::back() stay the same ;)
|
||||
*
|
||||
* We also provide stream operators.
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of queue
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
* \note
|
||||
* SemiAtomic means it is safe to for one thread to push only from front and another can pop.
|
||||
*/
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false>
|
||||
class queue : public deque<Data_t, N, SemiAtomic> {
|
||||
public:
|
||||
// meta-identity types
|
||||
using queue_t = queue<Data_t, N, SemiAtomic>;
|
||||
using base_type = deque<Data_t, N, SemiAtomic>;
|
||||
using range_t = typename base_type::range_t;
|
||||
|
||||
// STL
|
||||
using value_type = typename base_type::value_type;
|
||||
using reference = typename base_type::reference;
|
||||
using const_reference = typename base_type::const_reference;
|
||||
using pointer = typename base_type::pointer;
|
||||
using const_pointer = typename base_type::const_pointer;
|
||||
using iterator = typename base_type::iterator;
|
||||
using const_iterator = typename base_type::const_iterator;
|
||||
using reverse_iterator = typename base_type::reverse_iterator;
|
||||
using const_reverse_iterator= typename base_type::const_reverse_iterator;
|
||||
|
||||
//! \name Constructor / Destructor
|
||||
//! @{
|
||||
public:
|
||||
//! Default constructor
|
||||
constexpr queue () noexcept : base_type() { }
|
||||
|
||||
//! fill contructor
|
||||
constexpr queue(const Data_t& value) noexcept : base_type(value) { }
|
||||
|
||||
//! Initializer list contructor
|
||||
template <typename ...It>
|
||||
constexpr queue(It&& ...it) noexcept : base_type(std::forward<It>(it)...) { }
|
||||
|
||||
//! @}
|
||||
|
||||
//! \name Member access
|
||||
//! @{
|
||||
public:
|
||||
//! \brief Push an item in the back of the queue
|
||||
//! \param it The item to push
|
||||
constexpr void push (const Data_t& it) noexcept {
|
||||
base_type::push_back(it);
|
||||
}
|
||||
|
||||
//! \brief Extract an item from the front of the queue and remove it from the queue
|
||||
//! \param it The item to push
|
||||
constexpr Data_t pop () noexcept {
|
||||
return base_type::pop_front();
|
||||
}
|
||||
|
||||
//! \brief Push an item in the back of the queue
|
||||
//! \param it The item to push
|
||||
constexpr queue_t& operator<< (const Data_t& it) noexcept {
|
||||
push(it);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! \brief Pop an item from the front of the queue
|
||||
//! \param it The item to write to
|
||||
constexpr queue_t& operator>> (Data_t& it) noexcept {
|
||||
it = pop();
|
||||
return *this;
|
||||
}
|
||||
//! @}
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Pop an item from the front of the queue.
|
||||
*
|
||||
* This definition enables the "data << queue" syntax for pop operation
|
||||
*
|
||||
* \tparam Data_t The char-like queued item type. Usually \c char
|
||||
* \tparam N The size of queue
|
||||
* \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
|
||||
*
|
||||
* \param it The item to write to
|
||||
* \param q The queue to read from
|
||||
* \return Reference to the returned item
|
||||
*/
|
||||
template <typename Data_t, size_t N, bool SemiAtomic =false>
|
||||
constexpr Data_t& operator<< (Data_t& it, queue<Data_t, N, SemiAtomic>& q) noexcept {
|
||||
it = q.pop();
|
||||
return it;
|
||||
}
|
||||
|
||||
} // namespace utl
|
||||
|
||||
#endif /* utl_container_queue_h__ */
|
||||
64
include/utl/container/range.h
Normal file
64
include/utl/container/range.h
Normal file
@ -0,0 +1,64 @@
|
||||
/*!
|
||||
* \file container/range.h
|
||||
* \brief
|
||||
* A plain definition of a range struct with agregate initialization
|
||||
* and begin-end pairs.
|
||||
*
|
||||
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*/
|
||||
|
||||
#ifndef utl_container_range_h__
|
||||
#define utl_container_range_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
|
||||
namespace utl {
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* A plain definition of a range struct with begin-end pairs.
|
||||
*
|
||||
* \tparam Iter_t The iterator type of the range
|
||||
*/
|
||||
template <typename Iter_t>
|
||||
struct range {
|
||||
Iter_t b{}, e{};
|
||||
|
||||
// range () = default;
|
||||
// range (const Iter_t& first, const Iter_t& last) noexcept :
|
||||
// b(first), e(last) { }
|
||||
// range (Iter_t first, Iter_t last) noexcept :
|
||||
// b(first), e(last) { }
|
||||
|
||||
Iter_t begin() { return b; }
|
||||
const Iter_t begin() const { return b; }
|
||||
const Iter_t cbegin() const { return b; }
|
||||
Iter_t end() { return e; }
|
||||
const Iter_t end() const { return e; }
|
||||
const Iter_t cend() const { return e; }
|
||||
};
|
||||
|
||||
} // namespace utl;
|
||||
#endif /* utl_container_range_h__ */
|
||||
453
include/utl/container/ring_iterator.h
Normal file
453
include/utl/container/ring_iterator.h
Normal file
@ -0,0 +1,453 @@
|
||||
/*!
|
||||
* \file container/ring_iterator.h
|
||||
* \brief
|
||||
* A ring/circular iterator.
|
||||
*
|
||||
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*/
|
||||
|
||||
#ifndef utl_container_ring_iterator_h__
|
||||
#define utl_container_ring_iterator_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <atomic>
|
||||
|
||||
namespace utl {
|
||||
|
||||
template<typename Iter_t, size_t N, bool Atomic=false>
|
||||
class ring_iterator {
|
||||
//! \name STL iterator traits "forwarding"
|
||||
//! @{
|
||||
protected:
|
||||
using traits_type = std::iterator_traits<Iter_t>;
|
||||
|
||||
public:
|
||||
using iterator_type = Iter_t;
|
||||
using iterator_category = typename traits_type::iterator_category;
|
||||
using value_type = typename traits_type::value_type;
|
||||
using difference_type = typename traits_type::difference_type;
|
||||
using reference = typename traits_type::reference;
|
||||
using pointer = typename traits_type::pointer;
|
||||
//! @}
|
||||
|
||||
|
||||
//! \name Constructor / Destructor
|
||||
//! @{
|
||||
public:
|
||||
constexpr ring_iterator(const Iter_t base =nullptr) noexcept :
|
||||
base_(base), iter_(base) { }
|
||||
|
||||
constexpr ring_iterator(const Iter_t base, size_t elem) noexcept :
|
||||
base_(base), iter_(base + elem) { }
|
||||
|
||||
constexpr ring_iterator(const ring_iterator& it) noexcept :
|
||||
base_(it.base_), iter_(it.iter_) { }
|
||||
|
||||
constexpr ring_iterator& operator= (const ring_iterator& it) noexcept {
|
||||
base_ = it.base_;
|
||||
iter_ = it.iter_;
|
||||
return *this;
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Forward iterator requirements
|
||||
//! @{
|
||||
public:
|
||||
constexpr reference operator*() const noexcept {
|
||||
return *iter_;
|
||||
}
|
||||
|
||||
constexpr pointer operator->() const noexcept {
|
||||
return iter_;
|
||||
}
|
||||
|
||||
constexpr ring_iterator& operator++() noexcept {
|
||||
if (static_cast<size_t>(++iter_ - base_) >= N)
|
||||
iter_ = base_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr ring_iterator operator++(int) noexcept {
|
||||
ring_iterator it = *this;
|
||||
if (static_cast<size_t>(++iter_ - base_) >= N)
|
||||
iter_ = base_;
|
||||
return it;
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Bidirectional iterator requirements
|
||||
//! @{
|
||||
public:
|
||||
constexpr ring_iterator& operator--() noexcept {
|
||||
if (--iter_ < base_)
|
||||
iter_ = base_ + N -1;
|
||||
return *this;
|
||||
}
|
||||
constexpr ring_iterator operator--(int) noexcept {
|
||||
ring_iterator it = *this;
|
||||
if (--iter_ < base_)
|
||||
iter_ = base_ + N -1;
|
||||
return it;
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Random access iterator requirements
|
||||
//! @{
|
||||
constexpr reference operator[](difference_type n) const noexcept {
|
||||
difference_type k = iter_ - base_; // ptrdiff from base_
|
||||
return (static_cast<size_t>(k + n) < N) ?
|
||||
base_[k + n] : // on range
|
||||
base_[k + n - N]; // out of range, loop
|
||||
}
|
||||
constexpr ring_iterator& operator+=(difference_type n) noexcept {
|
||||
difference_type k = iter_ - base_; // ptrdiff from base_
|
||||
iter_ += (static_cast<size_t>(k + n) < N) ?
|
||||
n : // on range
|
||||
n - N; // out of range, loop
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr ring_iterator operator+(difference_type n) const noexcept {
|
||||
difference_type k = iter_ - base_; // ptrdiff from base_
|
||||
return (static_cast<size_t>(k + n) < N) ?
|
||||
ring_iterator(base_, k + n) : // on range
|
||||
ring_iterator(base_, k + n - N); // out of range, loop
|
||||
}
|
||||
constexpr ring_iterator& operator-=(difference_type n) noexcept {
|
||||
difference_type k = iter_ - base_; // ptrdiff from base_
|
||||
iter_ -= ((k - n) < 0)?
|
||||
n - N: // out of range, loop
|
||||
n; // on range
|
||||
return *this;
|
||||
}
|
||||
constexpr ring_iterator operator-(difference_type n) const noexcept {
|
||||
difference_type k = iter_ - base_; // ptrdiff from base_
|
||||
return ((k - n) < 0) ?
|
||||
ring_iterator(base_, k - n + N) : // out of range, loop
|
||||
ring_iterator(base_, k - n); // on range
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Data members and access
|
||||
//! @{
|
||||
constexpr const Iter_t& base() const noexcept {
|
||||
return base_;
|
||||
}
|
||||
constexpr const Iter_t& iter() const noexcept {
|
||||
return iter_;
|
||||
}
|
||||
constexpr size_t size() noexcept {
|
||||
return N;
|
||||
}
|
||||
|
||||
constexpr operator Iter_t() noexcept { return iter_; }
|
||||
constexpr operator const Iter_t() const noexcept { return iter_; }
|
||||
|
||||
protected:
|
||||
Iter_t base_;
|
||||
Iter_t iter_;
|
||||
//! @}
|
||||
};
|
||||
|
||||
// Forward iterator requirements
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator==(const ring_iterator<Iter_L, N>& lhs, const ring_iterator<Iter_R, N>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() == rhs.iter();
|
||||
}
|
||||
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator!=(const ring_iterator<Iter_L, N>& lhs, const ring_iterator<Iter_R, N>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() != rhs.iter();
|
||||
}
|
||||
|
||||
// Random access iterator requirements
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator<(const ring_iterator<Iter_L, N>& lhs, const ring_iterator<Iter_R, N>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() < rhs.iter();
|
||||
}
|
||||
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator<=(const ring_iterator<Iter_L, N>& lhs, const ring_iterator<Iter_R, N>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() <= rhs.iter();
|
||||
}
|
||||
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator>(const ring_iterator<Iter_L, N>& lhs, const ring_iterator<Iter_R, N>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() > rhs.iter();
|
||||
}
|
||||
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator>=(const ring_iterator<Iter_L, N>& lhs, const ring_iterator<Iter_R, N>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() >= rhs.iter();
|
||||
}
|
||||
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline auto operator-(const ring_iterator<Iter_L, N>& lhs, const ring_iterator<Iter_R, N>& rhs)
|
||||
noexcept
|
||||
-> decltype(lhs.iter() - rhs.iter()) {
|
||||
auto diff = lhs.iter() - rhs.iter();
|
||||
return diff < 0 ?
|
||||
diff + N : // loop
|
||||
diff; // no loop
|
||||
}
|
||||
|
||||
template<typename Iter, size_t N>
|
||||
inline ring_iterator<Iter, N> operator+(std::ptrdiff_t lhs, const ring_iterator<Iter, N>& rhs)
|
||||
noexcept {
|
||||
ring_iterator<Iter, N> it(rhs.iter());
|
||||
return it += lhs;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename Iter_t, size_t N>
|
||||
class ring_iterator<Iter_t, N, true> {
|
||||
//! \name STL iterator traits "forwarding"
|
||||
//! @{
|
||||
protected:
|
||||
using traits_type = std::iterator_traits<Iter_t>;
|
||||
|
||||
public:
|
||||
using iterator_type = Iter_t;
|
||||
using iterator_category = typename traits_type::iterator_category;
|
||||
using value_type = typename traits_type::value_type;
|
||||
using difference_type = typename traits_type::difference_type;
|
||||
using reference = typename traits_type::reference;
|
||||
using pointer = typename traits_type::pointer;
|
||||
//! @}
|
||||
|
||||
|
||||
//! \name Constructor / Destructor
|
||||
//! @{
|
||||
public:
|
||||
constexpr ring_iterator(const Iter_t base =nullptr) noexcept :
|
||||
base_(base), iter_(base) { }
|
||||
|
||||
constexpr ring_iterator(const Iter_t base, size_t elem) noexcept :
|
||||
base_(base), iter_(base + elem) { }
|
||||
|
||||
constexpr ring_iterator(const ring_iterator& it) noexcept :
|
||||
base_(it.base_) {
|
||||
iter_ = it.iter_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
constexpr ring_iterator& operator= (const ring_iterator& it) noexcept {
|
||||
base_ = it.base_;
|
||||
iter_ = it.iter_.load(std::memory_order_acquire);
|
||||
return *this;
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Forward iterator requirements
|
||||
//! @{
|
||||
public:
|
||||
constexpr reference operator*() const noexcept {
|
||||
return *iter_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
constexpr pointer operator->() const noexcept {
|
||||
return iter_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
constexpr ring_iterator& operator++() noexcept {
|
||||
Iter_t itnew, it = iter_.load(std::memory_order_acquire);
|
||||
do {
|
||||
itnew = it;
|
||||
if (static_cast<size_t>(++itnew - base_) >= N)
|
||||
itnew = base_;
|
||||
} while (!iter_.compare_exchange_weak(it, itnew, std::memory_order_acq_rel));
|
||||
return *this;
|
||||
}
|
||||
constexpr ring_iterator operator++(int) noexcept {
|
||||
ring_iterator ret = *this;
|
||||
Iter_t itnew, it = iter_.load(std::memory_order_acquire);
|
||||
do {
|
||||
itnew = it;
|
||||
if (static_cast<size_t>(++itnew - base_) >= N)
|
||||
itnew = base_;
|
||||
} while (!iter_.compare_exchange_weak(it, itnew, std::memory_order_acq_rel));
|
||||
return ret;
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Bidirectional iterator requirements
|
||||
//! @{
|
||||
public:
|
||||
constexpr ring_iterator& operator--() noexcept {
|
||||
Iter_t itnew, it = iter_.load(std::memory_order_acquire);
|
||||
do {
|
||||
itnew = it;
|
||||
if (--itnew < base_)
|
||||
itnew = base_ + N -1;
|
||||
} while (!iter_.compare_exchange_weak(it, itnew, std::memory_order_acq_rel));
|
||||
return *this;
|
||||
}
|
||||
constexpr ring_iterator operator--(int) noexcept {
|
||||
ring_iterator ret = *this;
|
||||
Iter_t itnew, it = iter_.load(std::memory_order_acquire);
|
||||
do {
|
||||
itnew = it;
|
||||
if (--itnew < base_)
|
||||
itnew = base_ + N -1;
|
||||
} while (!iter_.compare_exchange_weak(it, itnew, std::memory_order_acq_rel));
|
||||
return ret;
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Random access iterator requirements
|
||||
//! @{
|
||||
constexpr reference operator[](difference_type n) const noexcept {
|
||||
difference_type k = iter_.load(std::memory_order_acquire) - base_; // ptrdiff from base_
|
||||
return (static_cast<size_t>(k + n) < N) ?
|
||||
base_[k + n] : // on range
|
||||
base_[k + n - N]; // out of range, loop
|
||||
}
|
||||
constexpr ring_iterator& operator+=(difference_type n) noexcept {
|
||||
Iter_t itnew, it = iter_.load(std::memory_order_acquire);
|
||||
do {
|
||||
itnew = it;
|
||||
difference_type k = it - base_; // ptrdiff from base_
|
||||
itnew += (static_cast<size_t>(k + n) < N) ?
|
||||
n : // on range
|
||||
n - N; // out of range, loop
|
||||
} while (!iter_.compare_exchange_weak(it, itnew, std::memory_order_acquire));
|
||||
return *this;
|
||||
}
|
||||
constexpr ring_iterator operator+(difference_type n) const noexcept {
|
||||
difference_type k = iter_.load(std::memory_order_acquire) - base_; // ptrdiff from base_
|
||||
return (static_cast<size_t>(k + n) < N) ?
|
||||
ring_iterator(base_, k + n) : // on range
|
||||
ring_iterator(base_, k + n - N); // out of range, loop
|
||||
}
|
||||
constexpr ring_iterator& operator-=(difference_type n) noexcept {
|
||||
Iter_t itnew, it = iter_.load(std::memory_order_acquire);
|
||||
do {
|
||||
itnew = it;
|
||||
difference_type k = it - base_; // ptrdiff from base_
|
||||
itnew -= ((k - n) < 0)?
|
||||
n - N: // out of range, loop
|
||||
n; // on range
|
||||
} while (!iter_.compare_exchange_weak(it, itnew, std::memory_order_acquire));
|
||||
return *this;
|
||||
}
|
||||
constexpr ring_iterator operator-(difference_type n) const noexcept {
|
||||
difference_type k = iter_.load(std::memory_order_acquire) - base_; // ptrdiff from base_
|
||||
return ((k - n) < 0) ?
|
||||
ring_iterator(base_, k - n + N) : // out of range, loop
|
||||
ring_iterator(base_, k - n); // on range
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Data members and access
|
||||
//! @{
|
||||
constexpr const Iter_t& base() const noexcept {
|
||||
return base_;
|
||||
}
|
||||
constexpr const Iter_t iter() const noexcept {
|
||||
return iter_.load(std::memory_order_acquire);
|
||||
}
|
||||
constexpr size_t size() noexcept {
|
||||
return N;
|
||||
}
|
||||
|
||||
constexpr operator Iter_t() noexcept { return iter_.load(std::memory_order_acquire); }
|
||||
constexpr operator const Iter_t() const noexcept { return iter_.load(std::memory_order_acquire); }
|
||||
|
||||
protected:
|
||||
Iter_t base_;
|
||||
std::atomic<Iter_t> iter_;
|
||||
//! @}
|
||||
};
|
||||
|
||||
// Forward iterator requirements
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator==(const ring_iterator<Iter_L, N, true>& lhs, const ring_iterator<Iter_R, N, true>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() == rhs.iter();
|
||||
}
|
||||
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator!=(const ring_iterator<Iter_L, N, true>& lhs, const ring_iterator<Iter_R, N, true>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() != rhs.iter();
|
||||
}
|
||||
|
||||
// Random access iterator requirements
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator<(const ring_iterator<Iter_L, N, true>& lhs, const ring_iterator<Iter_R, N, true>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() < rhs.iter();
|
||||
}
|
||||
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator<=(const ring_iterator<Iter_L, N, true>& lhs, const ring_iterator<Iter_R, N, true>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() <= rhs.iter();
|
||||
}
|
||||
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator>(const ring_iterator<Iter_L, N, true>& lhs, const ring_iterator<Iter_R, N, true>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() > rhs.iter();
|
||||
}
|
||||
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline bool operator>=(const ring_iterator<Iter_L, N, true>& lhs, const ring_iterator<Iter_R, N, true>& rhs)
|
||||
noexcept {
|
||||
return lhs.iter() >= rhs.iter();
|
||||
}
|
||||
|
||||
template<typename Iter_L, typename Iter_R, size_t N>
|
||||
inline auto operator-(const ring_iterator<Iter_L, N, true>& lhs, const ring_iterator<Iter_R, N, true>& rhs)
|
||||
noexcept
|
||||
-> decltype(lhs.iter() - rhs.iter()) {
|
||||
auto diff = lhs.iter() - rhs.iter();
|
||||
return diff < 0 ?
|
||||
diff + N : // loop
|
||||
diff; // no loop
|
||||
}
|
||||
|
||||
template<typename Iter, size_t N>
|
||||
inline ring_iterator<Iter, N, true> operator+(std::ptrdiff_t lhs, const ring_iterator<Iter, N, true>& rhs)
|
||||
noexcept {
|
||||
ring_iterator<Iter, N, true> it(rhs.iter());
|
||||
return it += lhs;
|
||||
}
|
||||
|
||||
} //namespace utl;
|
||||
|
||||
#endif /* utl_container_ring_iterator_h__ */
|
||||
@ -1,22 +1,6 @@
|
||||
/*!
|
||||
* \file /utl/impl/crtp.h
|
||||
* \file utl/core/crtp.h
|
||||
* \brief CRTP idiom support header
|
||||
*
|
||||
* Copyright (C) 2018 Christos Choutouridis
|
||||
*
|
||||
* 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 __utl_impl_crtp_h__
|
||||
#define __utl_impl_crtp_h__
|
||||
@ -25,12 +9,14 @@
|
||||
|
||||
/*!
|
||||
* \defgroup crtp CRTP idiom support header
|
||||
* \ingroup impl
|
||||
* \ingroup core
|
||||
*
|
||||
* utl supports both CRTP idiom and dynamic polymorphism. By default
|
||||
* CRTP is the preferred way. If the user need virtuals then instead of
|
||||
* CRTP type, the \ref virtual_tag can passed to base class. The rest
|
||||
* CRTP type, the \c virtual_tag can passed to base class. The rest
|
||||
* will handled by utl automatically.
|
||||
*
|
||||
* \sa utl::virtual_tag
|
||||
*/
|
||||
//!@{
|
||||
namespace utl {
|
||||
@ -41,8 +27,8 @@ namespace utl {
|
||||
|
||||
//! \def CRTP boilerplate lines
|
||||
#define _CRTP_IMPL(T) \
|
||||
constexpr T& impl() { return *static_cast<T*>(this); } \
|
||||
constexpr const T& impl() const { return *static_cast<const T*>(this); }
|
||||
constexpr T& impl() { return *static_cast<T*>(this); } \
|
||||
constexpr const T& impl() const { return *static_cast<const T*>(this); }
|
||||
|
||||
}
|
||||
//!@}
|
||||
|
||||
@ -1,30 +1,18 @@
|
||||
/*!
|
||||
* \file /utl/core/impl.h
|
||||
* \file utl/core/impl.h
|
||||
* \brief Implementation detail main forward header
|
||||
*
|
||||
* Copyright (C) 2018 Christos Choutouridis
|
||||
*
|
||||
* 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 __utl_core_impl_h__
|
||||
#define __utl_core_impl_h__
|
||||
|
||||
//! \defgroup core Core
|
||||
//! Provide compiler feature support macros, uTL version,
|
||||
//! global types etc...
|
||||
|
||||
// FWD include implementation details once
|
||||
#include <utl/core/version.h>
|
||||
#include <utl/core/types.h>
|
||||
#include <utl/core/crtp.h>
|
||||
|
||||
|
||||
#endif /* __utl_core_impl_h__ */
|
||||
|
||||
@ -1,23 +1,8 @@
|
||||
/*!
|
||||
* \file /utl/core/types.h
|
||||
* \file utl/core/types.h
|
||||
* \brief Basic type alias support
|
||||
*
|
||||
* Copyright (C) 2018 Christos Choutouridis
|
||||
*
|
||||
* 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 __utl_core_types_h__
|
||||
#define __utl_core_types_h__
|
||||
|
||||
@ -27,13 +12,15 @@
|
||||
//#include <type_traits>
|
||||
|
||||
namespace utl {
|
||||
//! @{ \name byte and word types
|
||||
//! \name byte and word types
|
||||
//! @{
|
||||
using byte_t = uint8_t; //!< 8 bits wide
|
||||
using word_t = uint16_t; //!< 16 bits wide
|
||||
using dword_t = uint32_t; //!< 32 bits wide
|
||||
//! @}
|
||||
|
||||
//! @{ \name size and index
|
||||
//! \name size and index
|
||||
//! @{
|
||||
using size_t = std::size_t;
|
||||
using index_t = size_t; //!< index_t and size_t mend to be interchangeable
|
||||
|
||||
|
||||
@ -1,46 +1,44 @@
|
||||
/*!
|
||||
* \file /utl/core/version.h
|
||||
* \brief version and cpp version checks
|
||||
*
|
||||
* Copyright (C) 2018-2019 Christos Choutouridis
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* \file utl/core/version.h
|
||||
* \brief utl version and cpp version checks
|
||||
*/
|
||||
#ifndef __utl_core_version_h__
|
||||
#define __utl_core_version_h__
|
||||
|
||||
//!\defgroup version version
|
||||
//! Definitions of the utl version
|
||||
//!\addtogroup core
|
||||
//! @{
|
||||
|
||||
//! \defgroup version Version
|
||||
//! Definitions of the utl version and compiler features
|
||||
//!@{
|
||||
|
||||
//! utl version
|
||||
//! utl version as string
|
||||
#define UTL_VERSION "0.1.0"
|
||||
#define UTL_VERSION_MAJOR 0
|
||||
#define UTL_VERSION_MINOR 1
|
||||
#define UTL_VERSION_PATCH 0
|
||||
|
||||
//! utl version as integer
|
||||
//!
|
||||
//! This can be used in the user files to check for uTL versions.
|
||||
//! The version number is constructed using:\n
|
||||
//! \f$ ver = (Maj*10000 + Min*100 + Patch) \f$
|
||||
#define UTL_VERSION_VALUE ( (UTL_VERSION_MAJOR * 10000) \
|
||||
+ (UTL_VERSION_MINOR * 100) \
|
||||
+ UTL_VERSION_PATCH)
|
||||
|
||||
//! C++ versions
|
||||
#define CXX_VER __cplusplus
|
||||
#define CXX_VER_STD_11 201103L
|
||||
#define CXX_VER_STD_14 201402L
|
||||
#define CXX_VER_STD_17 201703L
|
||||
//!@}
|
||||
|
||||
//! Check for variable templates
|
||||
//! \name Compiler and feature checks
|
||||
//! These checks are used inside uTL, but the user is free to use the as well.
|
||||
//! @{
|
||||
#define CXX_VER __cplusplus //!< Current compiler's version
|
||||
#define CXX_VER_STD_11 201103L //!< C++11
|
||||
#define CXX_VER_STD_14 201402L //!< C++14
|
||||
#define CXX_VER_STD_17 201703L //!< C++17
|
||||
|
||||
//! \def CXX_VARIABLE_TEMPLATES
|
||||
//! Check for variable templates (Non-zero if available)
|
||||
#ifndef CXX_VARIABLE_TEMPLATES
|
||||
#ifdef __cpp_variable_templates
|
||||
#define CXX_VARIABLE_TEMPLATES __cpp_variable_templates
|
||||
@ -49,7 +47,8 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//! Check concepts
|
||||
//! \def CXX_CONCEPTS
|
||||
//! Check for concepts (Non-zero if available)
|
||||
#ifndef CXX_CONCEPTS
|
||||
#ifdef __cpp_concepts
|
||||
#define CXX_CONCEPTS __cpp_concepts
|
||||
@ -58,7 +57,8 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//! Check for inline variables
|
||||
//! \def CXX_INLINE_VARIABLES
|
||||
//! Check for inline variables (Non-zero if available)
|
||||
#ifndef CXX_INLINE_VARIABLES
|
||||
#ifdef __cpp_inline_variables
|
||||
#define CXX_INLINE_VARIABLES __cpp_inline_variables
|
||||
@ -67,6 +67,8 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//! \def CXX_FOLD_EXPRESSIONS
|
||||
//! Check for fold expressions (Non-zero if available)
|
||||
#ifndef CXX_FOLD_EXPRESSIONS
|
||||
#ifdef __cpp_fold_expressions
|
||||
#define CXX_FOLD_EXPRESSIONS __cpp_fold_expressions
|
||||
@ -75,19 +77,23 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Workaround inspections
|
||||
*/
|
||||
//! @}
|
||||
|
||||
|
||||
//! \name Workaround inspections
|
||||
//! @{
|
||||
|
||||
#if defined(__GNUC__) && (__GNUC__ < 5)
|
||||
// https://wg21.link/cwg1558
|
||||
#define UTL_WORKAROUND_CWG_1558
|
||||
#endif
|
||||
|
||||
|
||||
//! Base library requirement
|
||||
#if CXX_VER < CXX_VER_STD_14
|
||||
#error "uTL requires C++14"
|
||||
#endif
|
||||
|
||||
//!@}
|
||||
//! @}
|
||||
|
||||
//! @}
|
||||
#endif /* #ifndef __utl_core_version_h__ */
|
||||
|
||||
473
include/utl/dev/cli_device.h
Normal file
473
include/utl/dev/cli_device.h
Normal file
@ -0,0 +1,473 @@
|
||||
/*!
|
||||
* \file dev/cli_device.h
|
||||
* \brief
|
||||
* command line device driver functionality as CRTP base class
|
||||
*
|
||||
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*/
|
||||
|
||||
#ifndef utl_dev_cli_device_h__
|
||||
#define utl_dev_cli_device_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/core/crtp.h>
|
||||
#include <utl/container/equeue.h>
|
||||
#include <utl/dev/sequencer.h>
|
||||
|
||||
#include <utl/meta/meta.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
|
||||
#include <utility>
|
||||
#include <atomic>
|
||||
|
||||
namespace utl {
|
||||
|
||||
/*!
|
||||
* \class cli_device
|
||||
* \brief
|
||||
* Its a base class for command-line based devices
|
||||
*
|
||||
* Inherits the sequencer functionality and provides a command interface for sending
|
||||
* commands and parse the response.
|
||||
*
|
||||
* \example implementation example
|
||||
* \code
|
||||
* class BG95 : public cli_device<BG95, 256> {
|
||||
* using base_type = cli_device<BG95, 256>;
|
||||
* using Queue = equeue<typename base_type::value_type, 256, true>;
|
||||
* Queue RxQ{};
|
||||
* std::atomic<size_t> lines{};
|
||||
* public:
|
||||
* // cli_device driver requirements
|
||||
* BG95() noexcept :
|
||||
* RxQ(Queue::data_match::MATCH_PUSH, base_type::delimiter, [&](){
|
||||
* lines.fetch_add(1, std::memory_order_acq_rel);
|
||||
* }), lines(0) { }
|
||||
* void feed(char x) { RxQ << x; } // To be used inside ISR
|
||||
* size_t get(char* data, bool wait =false) {
|
||||
* do {
|
||||
* if (lines.load(std::memory_order_acquire)) {
|
||||
* size_t n =0;
|
||||
* do{
|
||||
* *data << RxQ;
|
||||
* ++n;
|
||||
* } while (*data++ != base_type::delimiter);
|
||||
* lines.fetch_sub(1, std::memory_order_acq_rel);
|
||||
* return n;
|
||||
* }
|
||||
* } while (wait);
|
||||
* return 0;
|
||||
* }
|
||||
* size_t contents(char* data) {
|
||||
* char* nullpos = std::copy(RxQ.begin(), RxQ.end(), data);
|
||||
* *nullpos =0;
|
||||
* return nullpos - data;
|
||||
* }
|
||||
* size_t put (const char* data, size_t n) {
|
||||
* // send data to BG95
|
||||
* return n;
|
||||
* }
|
||||
* clock_t clock() noexcept { //return CPU time }
|
||||
* };
|
||||
* \endcode
|
||||
*
|
||||
* \tparam Impl_t The type of derived class
|
||||
* \tparam N The size of the queue buffer for the receive/command interface
|
||||
* \tparam Delimiter The incoming data delimiter [default line buffered -- Delimiter = '\n']
|
||||
*/
|
||||
template<typename Impl_t, size_t N, char Delimiter ='\n'>
|
||||
class cli_device
|
||||
: public sequencer<cli_device<Impl_t, N, Delimiter>, char, N>{
|
||||
_CRTP_IMPL(Impl_t);
|
||||
|
||||
// local type dispatch
|
||||
using base_type = sequencer<cli_device, char, N>;
|
||||
|
||||
//! \name Public types
|
||||
//! @{
|
||||
public:
|
||||
using value_type = char;
|
||||
using pointer_type = char*;
|
||||
using size_type = size_t;
|
||||
using string_view = typename base_type::string_view;
|
||||
|
||||
using action_t = typename base_type::action_t;
|
||||
using control_t = typename base_type::control_t;
|
||||
using match_ft = typename base_type::match_ft;
|
||||
using handler_ft = typename base_type::handler_ft;
|
||||
template<size_t Nm>
|
||||
using script_t = typename base_type::template script_t<Nm>;
|
||||
|
||||
//! Publish delimiter
|
||||
constexpr static char delimiter = Delimiter;
|
||||
|
||||
enum flush_type { keep =0, flush };
|
||||
|
||||
//! Required types for inetd async handler operation
|
||||
//! @{
|
||||
|
||||
/*!
|
||||
* inetd handler structure for asynchronous incoming data dispatching
|
||||
*/
|
||||
struct inetd_handler_t {
|
||||
string_view token; //!< The token we match against
|
||||
match_ft match; //!< The predicate we use to match
|
||||
handler_ft handler; //!< The handler to call on match
|
||||
};
|
||||
//! Alias template for the async handler array
|
||||
template <size_t Nm>
|
||||
using inetd_handlers = std::array<inetd_handler_t, Nm>;
|
||||
//! @}
|
||||
//! @}
|
||||
|
||||
//! \name object lifetime
|
||||
//!@{
|
||||
protected:
|
||||
//!< \brief A default constructor from derived only
|
||||
cli_device() noexcept = default;
|
||||
~cli_device () = default; //!< \brief Allow destructor from derived only
|
||||
cli_device(const cli_device&) = delete; //!< No copies
|
||||
cli_device& operator= (const cli_device&) = delete; //!< No copy assignments
|
||||
//!@}
|
||||
|
||||
//! \name Sequencer interface requirements
|
||||
//! Forwarded to implementer the calls and cascade the the incoming channel
|
||||
//! @{
|
||||
friend base_type;
|
||||
|
||||
private:
|
||||
size_t get_ (char* data) {
|
||||
return impl().get (data);
|
||||
}
|
||||
size_t get (char* data) {
|
||||
return receive (data);
|
||||
}
|
||||
size_t contents (char* data) {
|
||||
return impl().contents(data);
|
||||
}
|
||||
size_t put (const char* data, size_t n) {
|
||||
return impl().put (data, n);
|
||||
}
|
||||
clock_t clock () noexcept {
|
||||
return impl().clock();
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name Private functionality
|
||||
//! @{
|
||||
private:
|
||||
|
||||
/*!
|
||||
* Convert the text pointed by \c str to a value and store it to
|
||||
* \c value. The type of conversion is deduced by the compiler
|
||||
* \tparam T The type of the value
|
||||
* \param str pointer to string with the value
|
||||
* \param value pointer to converted value
|
||||
*/
|
||||
template<typename T>
|
||||
void extract_ (const char* str, T* value) {
|
||||
static_assert (
|
||||
std::is_same_v<std::remove_cv_t<T>, int>
|
||||
|| std::is_same_v<std::remove_cv_t<T>, double>
|
||||
|| std::is_same_v<std::remove_cv_t<T>, char>,
|
||||
"Not supported conversion type.");
|
||||
if constexpr (std::is_same_v<std::remove_cv_t<T>, int>) {
|
||||
*value = std::atoi(str);
|
||||
} else if (std::is_same_v<std::remove_cv_t<T>, double>) {
|
||||
*value = std::atof(str);
|
||||
} else if (std::is_same_v<std::remove_cv_t<T>, char>) {
|
||||
std::strcpy(value, str);
|
||||
}
|
||||
}
|
||||
//! Specialization (as overload function) to handle void* types
|
||||
void extract_ (const char* str, void* value) noexcept {
|
||||
(void)*str; (void)value;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Parse a chunk of the buffer based on \c expected character
|
||||
*
|
||||
* Tries to match the \c *expected character in buffer and if so it copies the
|
||||
* character to token.
|
||||
* If the \c *expected is the \c Marker character, copy the entire chunk of the buffer
|
||||
* up to the character that matches the next expected character (expected[1]).
|
||||
* If there is no next expected character or if its not found in the buffer,
|
||||
* copy the entire buffer.
|
||||
*
|
||||
* \tparam Marker The special character to indicate chunk extraction
|
||||
*
|
||||
* \param expected The character to parse/remove from the buffer
|
||||
* \param buffer The buffer we parse
|
||||
* \param token Pointer to store the parsed tokens
|
||||
* \return A (number of characters parsed, marker found) pair
|
||||
*/
|
||||
template <char Marker = '%'>
|
||||
std::pair<size_t, bool> parse_ (const char* expected, const string_view buffer, char* token) {
|
||||
do {
|
||||
if (*expected == Marker) {
|
||||
// We have Marker. Copy the entire chunk of the buffer
|
||||
// up to the character that matches the next expected character (expected[1]).
|
||||
// If there is none next expected character or if its not found in the buffer,
|
||||
// copy the entire buffer.
|
||||
auto next = std::find(buffer.begin(), buffer.end(), expected[1]);
|
||||
char* nullpos = std::copy(buffer.begin(), next, token);
|
||||
*nullpos =0;
|
||||
return std::make_pair(next - buffer.begin(), true);
|
||||
}
|
||||
else if (*expected == buffer.front()) {
|
||||
// We have character match, copy the character to token and return 1 (the char size)
|
||||
*token++ = buffer.front();
|
||||
*token =0;
|
||||
return std::make_pair(1, false);
|
||||
}
|
||||
} while (0);
|
||||
|
||||
// Fail to parse
|
||||
*token =0;
|
||||
return std::make_pair(0, false);
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
* Analyze the response of a command based on \c expected.
|
||||
*
|
||||
* Tries to receive data with timeout and match them against expected string_view.
|
||||
* For each Marker inside the expected string the value gets extracted, converted and
|
||||
* copied to \c vargs pointer array.
|
||||
*
|
||||
* \param expected The expected string view
|
||||
* \param timeout the timeout in CPU time
|
||||
* \param vargs Pointer to variable arguments array
|
||||
* \param nargs Size of variable arguments array
|
||||
* \return
|
||||
*/
|
||||
template<char Marker = '%', typename T>
|
||||
bool response_ (const string_view expected, clock_t timeout, T* vargs, size_t nargs) {
|
||||
char buffer[N], token[N], *pbuffer = buffer;
|
||||
|
||||
size_t v =0, sz =0;
|
||||
for (auto ex = expected.begin() ; ex != expected.end() ; ) {
|
||||
clock_t mark = clock(); // mark the time
|
||||
while (sz <= 0) { // if buffer is empty get buffer with timeout
|
||||
sz = receive(buffer);
|
||||
pbuffer = buffer;
|
||||
if ((timeout != 0 )&& ((clock() - mark) >= timeout))
|
||||
return false;
|
||||
}
|
||||
// try to parse
|
||||
auto [step, marker] = parse_<Marker> (ex, {pbuffer, sz}, token);
|
||||
if (!step)
|
||||
return false; // discard buffer and fail
|
||||
|
||||
if (marker && v < nargs)
|
||||
extract_(token, vargs[v++]);
|
||||
|
||||
pbuffer += step;
|
||||
sz -= (step <= sz) ? step: sz;
|
||||
++ex;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! \name public functionality
|
||||
//! @{
|
||||
public:
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Transmit data to modem
|
||||
* \param data Pointer to data to send
|
||||
* \param n The size of data buffer
|
||||
* \return The number of transmitted chars
|
||||
*/
|
||||
size_t transmit (const char* data, size_t n) {
|
||||
if (data == nullptr)
|
||||
return 0;
|
||||
return put (data, n);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Transmit data to modem
|
||||
* \param data Pointer to data to send
|
||||
* \return The number of transmitted chars
|
||||
*/
|
||||
size_t transmit (const char* data) {
|
||||
if (data == nullptr)
|
||||
return 0;
|
||||
return put (data, std::strlen(data));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Try to receive data from modem. If there are data copy them to \c data pointer and return
|
||||
* the size. Otherwise return zero. In the case \c wait is true block until there are data to get.
|
||||
*
|
||||
* \param data Pointer to data buffer to write
|
||||
* \param wait Flag to select blocking / non-blocking functionality
|
||||
* \return The number of copied data.
|
||||
*/
|
||||
size_t receive (char* data, bool wait =false) {
|
||||
do {
|
||||
if (streams_.load(std::memory_order_acquire)) {
|
||||
size_t n =0;
|
||||
do {
|
||||
*data << rx_q;
|
||||
++n;
|
||||
} while (*data++ != delimiter);
|
||||
*data =0;
|
||||
streams_.fetch_sub(1, std::memory_order_acq_rel);
|
||||
return n;
|
||||
}
|
||||
} while (wait); // on wait flag we block until available stream
|
||||
return 0;
|
||||
}
|
||||
|
||||
//! Clears the incoming data buffer
|
||||
void clear () noexcept {
|
||||
rx_q.clear();
|
||||
}
|
||||
|
||||
//! \return Returns the size of the incoming data buffer
|
||||
size_t size() noexcept {
|
||||
return rx_q.size();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Send a command to modem and check if the response matches to \c expected.
|
||||
*
|
||||
* This function executes 3 steps.
|
||||
* - Clears the incoming buffer if requested by template parameter
|
||||
* - Sends the command to device
|
||||
* - Waits to get the response and parse it accordingly to \c expected \see response_()
|
||||
*
|
||||
* The user can mark spots inside the expected string using the \c Marker ['%'] character.
|
||||
* These spots will be extracted to tokens upon parsing. If the user passes \c values parameters,
|
||||
* then the extracted tokens will be converted to the type of the \c values (\c Ts) and copied to them
|
||||
* one by one. If the values are less than spots, the rest of the tokens get discarded.
|
||||
*
|
||||
* \param cmd The command to send (null terminated)
|
||||
* \param expected The expected response
|
||||
* \param timeout The timeout in CPU time (leave it for 0 - no timeout)
|
||||
* \param values The value pointer arguments to get the converted tokens
|
||||
*
|
||||
* \tparam Flush Flag to indicate if we flush the buffer before command or not
|
||||
* \tparam Marker The marker character
|
||||
* \tparam Ts The type of the values to read from response marked with \c Marker
|
||||
* \warning The types MUST be the same
|
||||
*
|
||||
* \return True on success
|
||||
*
|
||||
* \example examples
|
||||
* \code
|
||||
* Derived cli;
|
||||
* int status;
|
||||
* char str[32];
|
||||
*
|
||||
* // discard 3 lines and expect OK\r\n at the end with 1000[CPU time] timeout
|
||||
* cli.command("AT+CREG?\r\n", "%%%OK\r\n", 1000);
|
||||
*
|
||||
* // extract a number from response without timeout (blocking)
|
||||
* cli.command<flush>("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", 0, &status);
|
||||
*
|
||||
* // extract a number and discard the last 2 lines
|
||||
* cli.command<flush>("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n%%", 1000, &status);
|
||||
*
|
||||
* // discard first line, read the 2nd to str, discard the 3rd line.
|
||||
* // expect the last to be "OK\r\n"
|
||||
* cli.command<flush>("AT+CREG?\r\n", "", 100000);
|
||||
* cli.command<keep>("", "%", 1000);
|
||||
* cli.command<keep>("", "%%", 1000, str);
|
||||
* cli.command<keep>("", "OK\r\n", 1000);
|
||||
* \endcode
|
||||
*/
|
||||
template<flush_type Flush =flush, char Marker = '%', typename ...Ts>
|
||||
bool command (const string_view cmd, const string_view expected, clock_t timeout, Ts* ...values) {
|
||||
constexpr size_t Nr = sizeof...(Ts);
|
||||
|
||||
meta::if_c<
|
||||
(sizeof...(Ts) != 0),
|
||||
meta::front<meta::typelist<Ts...>>*,
|
||||
void*
|
||||
> vargs[Nr] = {values...}; // read all args to local buffer
|
||||
if constexpr (Flush == flush) {
|
||||
clear ();
|
||||
}
|
||||
if (transmit(cmd.data(), cmd.size()) != cmd.size()) // send command
|
||||
return false;
|
||||
// parse the response and return the status
|
||||
return response_<Marker>(expected, timeout, vargs, Nr);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* inetd daemon functionality provided as member function of the driver. This should be running
|
||||
* in the background either as consecutive calls from an periodic ISR with \c loop = false, or
|
||||
* as a thread in an RTOS environment with \c loop = true.
|
||||
*
|
||||
* \tparam Nm The number of handler array entries
|
||||
*
|
||||
* \param async_handles Reference to asynchronous handler array
|
||||
* \param loop Flag to indicate blocking mode. If true blocking.
|
||||
*/
|
||||
template <size_t Nm =0>
|
||||
void inetd (bool loop =true, const inetd_handlers<Nm>* inetd_handlers =nullptr) {
|
||||
std::array<char, N> buffer;
|
||||
size_t resp_size;
|
||||
do {
|
||||
if ((resp_size = get_(buffer.data())) != 0) {
|
||||
// on data check for async handlers
|
||||
bool match = false;
|
||||
if (inetd_handlers != nullptr) {
|
||||
for (auto& h : *inetd_handlers)
|
||||
match |= base_type::check_handle({buffer.data(), resp_size}, h.token, h.match, h.handler);
|
||||
}
|
||||
// if no match forward data to receive channel.
|
||||
if (!match) {
|
||||
char* it = buffer.data();
|
||||
do {
|
||||
rx_q << *it;
|
||||
} while (*it++ != delimiter);
|
||||
streams_.fetch_add(1, std::memory_order_acq_rel);
|
||||
}
|
||||
}
|
||||
} while (loop);
|
||||
}
|
||||
|
||||
//! @}
|
||||
|
||||
private:
|
||||
equeue<char, N, true> rx_q{};
|
||||
std::atomic<size_t> streams_{};
|
||||
};
|
||||
|
||||
} // namespace utl;
|
||||
|
||||
#endif /* #ifndef utl_dev_cli_device_h__ */
|
||||
@ -23,6 +23,7 @@
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/meta/meta.h>
|
||||
#include <iterator>
|
||||
|
||||
namespace utl {
|
||||
|
||||
@ -397,7 +398,7 @@ namespace utl {
|
||||
typename T::difference_type;
|
||||
typename T::pointer;
|
||||
typename T::reference;
|
||||
requires same_<
|
||||
requires same<
|
||||
typename T::iterator_category,
|
||||
std::output_iterator_tag
|
||||
>::value;
|
||||
@ -674,7 +675,7 @@ namespace utl {
|
||||
typename T::difference_type;
|
||||
typename T::pointer;
|
||||
typename T::reference;
|
||||
requires same_ <
|
||||
requires same <
|
||||
typename T::iterator_category,
|
||||
std::input_iterator_tag
|
||||
>::value;
|
||||
@ -959,7 +960,7 @@ namespace utl {
|
||||
typename T::difference_type;
|
||||
typename T::pointer;
|
||||
typename T::reference;
|
||||
requires same_<
|
||||
requires same<
|
||||
typename T::iterator_category,
|
||||
std::input_iterator_tag
|
||||
>::value;
|
||||
|
||||
107
include/utl/dev/gpio_dev.h
Normal file
107
include/utl/dev/gpio_dev.h
Normal file
@ -0,0 +1,107 @@
|
||||
/*!
|
||||
* \file dev/gpio_dev.h
|
||||
* \brief
|
||||
* A STM32 gpio wrapper class
|
||||
*
|
||||
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*/
|
||||
|
||||
#ifndef utl_dev_gpio_dev_h__
|
||||
#define utl_dev_gpio_dev_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
|
||||
namespace utl {
|
||||
|
||||
/*!
|
||||
* CRTP class for gpio digital input-output.
|
||||
*
|
||||
* The derived class requirements are:
|
||||
* - bool read_impl ()
|
||||
* - void write_impl(bool)
|
||||
*
|
||||
* \tparam Impl_t The type of derived class
|
||||
*/
|
||||
template <typename Impl_t>
|
||||
class DigitalInOut {
|
||||
public:
|
||||
_CRTP_IMPL(Impl_t);
|
||||
|
||||
/*!
|
||||
* \name Object lifetime
|
||||
*/
|
||||
//! @{
|
||||
protected:
|
||||
DigitalInOut() noexcept = default;
|
||||
~DigitalInOut() = default;
|
||||
DigitalInOut(const DigitalInOut&) = delete; //!< No copy constructions
|
||||
DigitalInOut operator=(const DigitalInOut&) = delete; //!< No copies
|
||||
|
||||
//! @}
|
||||
|
||||
//! \name Public interface
|
||||
//! @{
|
||||
public:
|
||||
//! Reads the state of the pin. This is true for both input and output pins.
|
||||
//! \return The state of the pin
|
||||
bool read () noexcept { return impl().read_impl (); }
|
||||
//! Write a new state to pin. If the pin is set for output, otherwise this state will remain
|
||||
//! to pin registers and reflect to the pin state if we select output mode
|
||||
void write (bool state) noexcept { impl().write_impl(state); }
|
||||
|
||||
//! Implicit conversion to bool for reading operations
|
||||
operator bool () noexcept {
|
||||
return read();
|
||||
}
|
||||
//! Stream from bool for write operations
|
||||
DigitalInOut& operator<< (bool state) noexcept {
|
||||
write(state);
|
||||
return *this;
|
||||
}
|
||||
//! Right stream to bool for read operations
|
||||
DigitalInOut& operator>> (bool& state) noexcept {
|
||||
state = read();
|
||||
return *this;
|
||||
}
|
||||
//! @}
|
||||
};
|
||||
|
||||
/*!
|
||||
* This definition enables the "data << pin" syntax for read operation
|
||||
*
|
||||
* \tparam Impl_t The derived class type of the DigitalInOut
|
||||
*
|
||||
* \param lhs Left hand site operand
|
||||
* \param rhs Right hand site operand
|
||||
* \return The read value
|
||||
*/
|
||||
template<typename Impl_t>
|
||||
bool operator<<(bool& lhs, DigitalInOut<Impl_t>& rhs) noexcept {
|
||||
lhs = rhs.read();
|
||||
return lhs;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //#ifndef utl_dev_gpio_dev_h__
|
||||
492
include/utl/dev/sequencer.h
Normal file
492
include/utl/dev/sequencer.h
Normal file
@ -0,0 +1,492 @@
|
||||
/*!
|
||||
* \file dev/sequencer.h
|
||||
* \brief
|
||||
* A script based automation tool for send/receive communications
|
||||
*
|
||||
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*/
|
||||
|
||||
#ifndef utl_dev_dequencer_h__
|
||||
#define utl_dev_dequencer_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/container/range.h>
|
||||
|
||||
#include <ctime>
|
||||
#include <array>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <tuple>
|
||||
|
||||
namespace utl {
|
||||
|
||||
/*!
|
||||
* \class sequencer
|
||||
* \brief
|
||||
* A CRTP base class to provide the sequencer functionality.
|
||||
*
|
||||
* Sequencer is a script engine with receive/transmit functionalities based on predicates. It has:
|
||||
* - A program counter like variable named \c step.
|
||||
* - \c step actions like NEXT, GOTO exit with status etc...
|
||||
* - Input data match predicates to trigger those actions.
|
||||
* - Input data handlers to trigger external functionality on predicate match
|
||||
* - Output data handlers to "edit" data before transmiting them
|
||||
* - A small predicate set provided to the user. (starts_with, ends_with, contains).
|
||||
*
|
||||
* Sequencer can automate communication with a terminal-like device such as AT-command modems, can
|
||||
* be used to implement communication protocols, or even small http servers.
|
||||
*
|
||||
* It can operate based on a script array and handle the outgoing commands and incoming responses.
|
||||
* The user can create matching rules on received data and hook handlers and actions on them.
|
||||
*
|
||||
* The derived class (implementation) has to provide:
|
||||
* 1) size_t get(Data_t* data);
|
||||
* This function return 0 or a number of Data_t items. The data points to buffer for the input data.
|
||||
*
|
||||
* 3) size_t contents_ (Data_t* data);
|
||||
* This function return 0 or a number of Data_t items without removing them from the implementer's container
|
||||
* The data points to buffer for the input data.
|
||||
*
|
||||
* 2) size_t put(const Data_t* data, size_t n);
|
||||
* This function sends to implementation the data pointed by \c data witch have size \c n.
|
||||
*
|
||||
* 4) clock_t clock();
|
||||
* This function return a number to be used as time. The units of this function may be arbitrary but they
|
||||
* match the units in \c record_t::timeout field.
|
||||
*
|
||||
* \tparam Impl_t The type of derived class
|
||||
* \tparam Data_t The char-like stream item type. Usually \c char
|
||||
* \tparam N The size of the sequence buffer to temporary store each line from get().
|
||||
*
|
||||
* \note
|
||||
* We need access to derived class container to sneaky get a range of the data beside
|
||||
* the normal data flow, in order to implement the \see control_t::DETECT operation.
|
||||
*/
|
||||
template <typename Impl_t, typename Data_t, size_t N>
|
||||
class sequencer {
|
||||
_CRTP_IMPL(Impl_t);
|
||||
|
||||
//! \name Public types
|
||||
//! @{
|
||||
public:
|
||||
using value_type = Data_t;
|
||||
using pointer_type = Data_t*;
|
||||
using size_type = size_t;
|
||||
using string_view = std::basic_string_view<Data_t>;
|
||||
|
||||
/*!
|
||||
* The sequencer engine status. A variable of this type is returned by
|
||||
* \see action_().
|
||||
*/
|
||||
enum class seq_status_t {
|
||||
CONTINUE, //!< Means we keep looping
|
||||
EXIT //!< Means, we exit with status the one indicated by \c action_t of the \c record_t
|
||||
};
|
||||
|
||||
//! \enum control_t
|
||||
//! \brief The control type of the script entry.
|
||||
enum class control_t {
|
||||
NOP, //!< No command, dont send or expect anything, used for delays
|
||||
SEND, //!< Send data to implementation through put()
|
||||
EXPECT, //!< Expects data from implementation via get()
|
||||
OR_EXPECT, //!< Expects data from implementation via get() in conjunction with previous EXPECT
|
||||
DETECT, //!< Detects data into rx buffer without receiving them via contents()
|
||||
OR_DETECT //!< Detects data into rx buffer without receiving them via contents() in conjunction with
|
||||
//!< previous DETECT
|
||||
|
||||
//! \note
|
||||
//! The \c DETECT extra incoming channel serve the purpose of sneak into receive
|
||||
//! buffer and check for data without getting them. This is useful when the receive driver
|
||||
//! is buffered with a delimiter and we seek for data that don't follow the delimiter pattern.
|
||||
//!
|
||||
//! For example:
|
||||
//! A modem sends responses with '\n' termination but for some "special" command it opens a cursor
|
||||
//! lets say ">$ " without '\n' at the end.
|
||||
};
|
||||
|
||||
//! \enum action_t
|
||||
//! \brief
|
||||
//! Possible response actions for the sequencer. This is the
|
||||
//! equivalent of changing the program counter of the sequencer
|
||||
//! and is composed by a type and a value.
|
||||
//!
|
||||
struct action_t {
|
||||
enum {
|
||||
NO =0, //!< Do not change sequencer's step
|
||||
NEXT, //!< Go to next sequencer step. In case of EXPECT/DETECT block of records
|
||||
//!< skip the entire block of EXPECT[, OR_EXPECT[, OR_EXPECT ...]] and go
|
||||
//!< to the next (non OR_*) control record.
|
||||
GOTO, //!< Manually sets the step counter to the number of the \c step member.
|
||||
EXIT, //!< Instruct for an exit returning the action.value as status
|
||||
} type;
|
||||
size_t value; //!< Used by \c GOTO to indicate the next sequencer's step.
|
||||
};
|
||||
|
||||
//! A no_action action_t
|
||||
static constexpr action_t no_action = {action_t::NO, 0};
|
||||
//! A next action_t
|
||||
static constexpr action_t next = {action_t::NEXT, 0};
|
||||
//! A goto action_t template
|
||||
template <size_t GOTO>
|
||||
static constexpr action_t go_to = {action_t::GOTO, static_cast<size_t>(GOTO)};
|
||||
|
||||
//! An exit ok action_t
|
||||
static constexpr action_t exit_ok = {action_t::EXIT, 0};
|
||||
//! An exit error action_t
|
||||
static constexpr action_t exit_error = {action_t::EXIT, static_cast<size_t>(-1)};
|
||||
//! A generic exit action_t template
|
||||
template <size_t Status>
|
||||
static constexpr action_t exit = {action_t::EXIT, static_cast<size_t>(Status)};
|
||||
|
||||
/*!
|
||||
* Match binary predicate function pointer type.
|
||||
* Expects two string views and return a boolean.
|
||||
* It is used by EXPECT/DETECT blocks to trigger their {handler, action} pair.
|
||||
*/
|
||||
using match_ft = bool (*) (const string_view haystack, const string_view needle);
|
||||
|
||||
/*!
|
||||
* Send/Receive handler function pointer type.
|
||||
* Expects a pointer to buffer and a size and returns status.
|
||||
* It is used on predicate match on EXPECT/DETECT blocks, or as data wrapper on SEND blocks.
|
||||
*/
|
||||
using handler_ft = void (*) (const Data_t*, size_t);
|
||||
|
||||
/*!
|
||||
* \struct record_t
|
||||
* \brief
|
||||
* Describes the sequencer's script record entry (line).
|
||||
*/
|
||||
struct record_t {
|
||||
control_t control; //!< The control type of the entry
|
||||
string_view token; //!< String view to token data. [MUST BE null terminated].
|
||||
//!< This is passed as 2nd argument to match predicate on EXPECT/DETECT, or as
|
||||
//! {data, size} pair to SEND handler and put_().
|
||||
//!< If unused set it to ""
|
||||
match_ft match; //!< Match predicate to used in EXPECT/DETECT blocks
|
||||
//!< If unused set it to nullptr
|
||||
handler_ft handler; //!< The handler to called if the match is successful, or before put_()
|
||||
//!< If unused set it to nullptr
|
||||
action_t action; //!< Indicates the step manipulation if the match is successful or after NOP and put_()
|
||||
clock_t timeout; //!< Timeout in CPU time
|
||||
};
|
||||
|
||||
/*!
|
||||
* \struct script_t
|
||||
* \brief
|
||||
* Describes the sequencer's script.
|
||||
*
|
||||
* The user can create arrays as the example bellow to act as a script.
|
||||
* \code
|
||||
* Seq s;
|
||||
* const Seq::script_t<4> script = {{
|
||||
* {Seq::control_t::NOP, "", Seq::nil, Seq::nil, {Seq::action_t::GOTO, 1}, 1000},
|
||||
*
|
||||
* {Seq::control_t::SEND, "ATE0\r\n", Seq::nil, Seq::nil, {Seq::action_t::NEXT, 0}, 0},
|
||||
* {Seq::control_t::EXPECT, "OK\r\n", Seq::ends_with, Seq::nil, {Seq::action_t::EXIT_OK, 0}, 1000},
|
||||
* {Seq::control_t::OR_EXPECT, "ERROR", Seq::contains, Seq::nil, {Seq::action_t::EXIT_ERROR, 0}, 0}
|
||||
* }};
|
||||
* s.run(script);
|
||||
* \endcode
|
||||
*/
|
||||
template <size_t Nrecords>
|
||||
using script_t = std::array<record_t, Nrecords>;
|
||||
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Check if the \c stream1 is equal to \c stream2
|
||||
* \param stream1 The stream in witch we search [The input buffer]
|
||||
* \param stream2 What we search [The record's token]
|
||||
* \return True on success, false otherwise
|
||||
*/
|
||||
static constexpr auto equals = [](const string_view stream1, const string_view stream2) noexcept -> bool {
|
||||
return (stream1 == stream2);
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Check if the \c stream starts with the \c prefix
|
||||
* \param stream The stream in witch we search [The input buffer]
|
||||
* \param prefix What we search [The record's token]
|
||||
* \return True on success, false otherwise
|
||||
*/
|
||||
static constexpr auto starts_with = [](const string_view stream, const string_view prefix) noexcept -> bool {
|
||||
return (stream.rfind(prefix, 0) != string_view::npos);
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Check if the \c stream ends with the \c postfix
|
||||
* \param stream The stream in witch we search [The input buffer]
|
||||
* \param postfix What we search [The record's token]
|
||||
* \return True on success, false otherwise
|
||||
*/
|
||||
static constexpr auto ends_with = [](const string_view stream, const string_view postfix) -> bool {
|
||||
if (stream.size() < postfix.size())
|
||||
return false;
|
||||
return (
|
||||
stream.compare(
|
||||
stream.size() - postfix.size(),
|
||||
postfix.size(),
|
||||
postfix) == 0
|
||||
);
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Check if the \c haystack contains the \c needle
|
||||
* \param haystack The stream in witch we search [The input buffer]
|
||||
* \param needle What we search [The record's token]
|
||||
* \return True on success, false otherwise
|
||||
*/
|
||||
static constexpr auto contains = [](const string_view haystack, const string_view needle) noexcept -> bool {
|
||||
return (haystack.find(needle) != string_view::npos);
|
||||
};
|
||||
|
||||
//! Always false predicate
|
||||
static constexpr auto always_true = [](const string_view s1, const string_view s2) noexcept -> bool {
|
||||
(void)s1; (void)s2;
|
||||
return true;
|
||||
};
|
||||
|
||||
//! Always false predicate
|
||||
static constexpr auto always_false = [](const string_view s1, const string_view s2) noexcept -> bool {
|
||||
(void)s1; (void)s2;
|
||||
return false;
|
||||
};
|
||||
|
||||
//! Empty predicate or handler
|
||||
static constexpr auto nil = nullptr;
|
||||
//! @}
|
||||
|
||||
|
||||
|
||||
//! \name Object lifetime
|
||||
//!@{
|
||||
protected:
|
||||
~sequencer () = default; //!< \brief Allow destructor from derived only
|
||||
constexpr sequencer () noexcept = default; //!< \brief A default constructor from derived only
|
||||
sequencer(const sequencer&) = delete; //!< No copies
|
||||
sequencer& operator= (const sequencer&) = delete; //!< No copy assignments
|
||||
//!@}
|
||||
|
||||
//! \name Sequencer interface requirements for implementer
|
||||
//! @{
|
||||
private:
|
||||
size_t get_ (Data_t* data) { return impl().get (data); }
|
||||
size_t contents_ (Data_t* data) { return impl().contents(data); }
|
||||
size_t put_ (const Data_t* data, size_t n) { return impl().put (data, n); }
|
||||
clock_t clock_ () noexcept { return impl().clock(); }
|
||||
//! @}
|
||||
|
||||
//! \name Private functionality
|
||||
//! @{
|
||||
private:
|
||||
|
||||
/*!
|
||||
* Check if there is a handler and call it
|
||||
* \param handler The handler to check
|
||||
* \param buffer String view to buffer to pass to handler
|
||||
* \return True if handler is called
|
||||
*/
|
||||
constexpr bool handle_ (handler_ft handler, const string_view buffer = string_view{}) {
|
||||
if (handler != nullptr) {
|
||||
handler (buffer.begin(), buffer.size());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Return the new sequencer's step value and the sequencer's loop status as pair.
|
||||
*
|
||||
* \param script Reference to entire script.
|
||||
* \param step The current step
|
||||
* \return new step - status pair
|
||||
*/
|
||||
template <size_t Steps>
|
||||
constexpr std::pair<size_t, seq_status_t> action_ (const script_t<Steps>& script, size_t step) {
|
||||
control_t skip_while{};
|
||||
size_t s;
|
||||
|
||||
switch (script[step].action.type) {
|
||||
default:
|
||||
case action_t::NO: return std::make_pair(step, seq_status_t::CONTINUE);
|
||||
case action_t::NEXT:
|
||||
switch (script[step].control) {
|
||||
case control_t::NOP: return std::make_pair(++step, seq_status_t::CONTINUE);
|
||||
case control_t::SEND: return std::make_pair(++step, seq_status_t::CONTINUE);
|
||||
case control_t::EXPECT:
|
||||
case control_t::OR_EXPECT: skip_while = control_t::OR_EXPECT; break;
|
||||
case control_t::DETECT:
|
||||
case control_t::OR_DETECT: skip_while = control_t::OR_DETECT; break;
|
||||
}
|
||||
s = step;
|
||||
while (script[++s].control == skip_while)
|
||||
;
|
||||
return std::make_pair(s, seq_status_t::CONTINUE);
|
||||
case action_t::GOTO: return std::make_pair(script[step].action.value, seq_status_t::CONTINUE);
|
||||
case action_t::EXIT: return std::make_pair(script[step].action.value, seq_status_t::EXIT);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//! @}
|
||||
|
||||
|
||||
public:
|
||||
|
||||
//! \return The buffer size of the sequencer
|
||||
constexpr size_t size() const noexcept { return N; }
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* A static functionality to provide access to sequencer's inner matching mechanism.
|
||||
* Checks the \c buffer against \c handle and calls its action if needed.
|
||||
*
|
||||
* \param buffer The buffer to check (1st parameter to match)
|
||||
* \param token String view to check against buffer (2nd parameter to match)
|
||||
* \param handler Function pointer to match predicate to use
|
||||
* \param handle Reference to handle to call on match
|
||||
*
|
||||
* \return True on match, false otherwise
|
||||
*/
|
||||
constexpr bool check_handle (const string_view buffer, const string_view token, match_ft match, handler_ft handle) {
|
||||
if (match != nullptr && match(buffer, token))
|
||||
return handle_ (handle, buffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Run the script array
|
||||
*
|
||||
* The main sequencer functionality. It starts with the first entry of the array.
|
||||
*
|
||||
* - If the record is \c NOP it executes the action after the timeout.
|
||||
* \c NOP uses {\c action_t, \c timeout}.
|
||||
* - If the record is \c SEND passes the token to handler (if any), then to put_() and executes the action after that.
|
||||
* \c SEND uses {\c token, \c handler, \c action_t}
|
||||
* - If the record is \c EXCEPT it continuously try to receive data using \see get_()
|
||||
* * If no data until timeout, exit with failure
|
||||
* * On data reception for this record AND for each OR_EXPECT that follows, calls the match predicate
|
||||
* by passing the received data and token.
|
||||
* On predicate match
|
||||
* - Calls the handler if there is one
|
||||
* - Executes the action. No farther EXPECT, OR_EXPECT, ... checks are made.
|
||||
* - If the record is \c DETECT it continuously try to receive data using \see contents_()
|
||||
* * If no data until timeout, exit with failure
|
||||
* * On data reception for this record AND for each OR_DETECT that follows, calls the match predicate
|
||||
* by passing the received data and token.
|
||||
* On predicate match
|
||||
* - Calls the handler if there is one
|
||||
* - Executes the action. No farther DETECT, OR_DETECT, ... checks are made.
|
||||
*
|
||||
* \tparam Steps The number of records of the script
|
||||
*
|
||||
* \param script Reference to script to run
|
||||
* \return The status of entire operation as described above
|
||||
* \arg 0 Success
|
||||
* \arg (size_t)-1 Failure
|
||||
* \arg other Arbitrary return status
|
||||
*/
|
||||
template <size_t Steps>
|
||||
size_t run (const script_t<Steps>& script) {
|
||||
Data_t buffer[N];
|
||||
size_t resp_size;
|
||||
|
||||
size_t step =0, p_step =0;
|
||||
clock_t mark = clock_();
|
||||
|
||||
seq_status_t status{seq_status_t::CONTINUE}; do {
|
||||
if (step >= Steps)
|
||||
return exit_error.value;
|
||||
const record_t& record = script[step]; // get reference ot current line
|
||||
|
||||
if (step != p_step) { // renew time marker in each step
|
||||
p_step = step;
|
||||
mark = clock_();
|
||||
}
|
||||
switch (record.control) {
|
||||
default:
|
||||
case control_t::NOP:
|
||||
if ((clock_() - mark) >= record.timeout)
|
||||
std::tie(step, status) = action_ (script, step);
|
||||
break;
|
||||
|
||||
case control_t::SEND:
|
||||
if (record.handler != nullptr)
|
||||
record.handler(record.token.data(), record.token.size());
|
||||
if (put_(record.token.data(), record.token.size()) != record.token.size())
|
||||
return exit_error.value;
|
||||
std::tie(step, status) = action_ (script, step);
|
||||
break;
|
||||
|
||||
case control_t::EXPECT:
|
||||
case control_t::OR_EXPECT:
|
||||
resp_size = get_(buffer);
|
||||
if (resp_size) {
|
||||
size_t s = step ; do{
|
||||
if (script[s].match != nullptr && script[s].match({buffer, resp_size}, script[s].token)) {
|
||||
handle_ (script[s].handler, {buffer, resp_size});
|
||||
std::tie(step, status) = action_ (script, s);
|
||||
break;
|
||||
}
|
||||
} while ((++s < Steps) && (script[s].control == control_t::OR_EXPECT));
|
||||
}
|
||||
if (record.timeout && (clock_() - mark) >= record.timeout)
|
||||
return exit_error.value;
|
||||
break;
|
||||
|
||||
case control_t::DETECT:
|
||||
case control_t::OR_DETECT:
|
||||
resp_size = contents_(buffer);
|
||||
if (resp_size) {
|
||||
size_t s = step ; do {
|
||||
if (script[s].match != nullptr && script[s].match({buffer, resp_size}, script[s].token)) {
|
||||
handle_ (script[s].handler, {buffer, resp_size});
|
||||
std::tie(step, status) = action_ (script, s);
|
||||
break;
|
||||
}
|
||||
} while ((++s < Steps) && (script[s].control == control_t::OR_DETECT));
|
||||
}
|
||||
if (record.timeout && (clock_() - mark) >= record.timeout)
|
||||
return exit_error.value;
|
||||
break;
|
||||
|
||||
} // switch (record.control)
|
||||
|
||||
} while ( status == seq_status_t::CONTINUE);
|
||||
|
||||
return step; // step here is set by action_ as the return status
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} //namespace utl;
|
||||
#endif /* utl_dev_dequencer_h__ */
|
||||
415
include/utl/meta/basic.h
Normal file
415
include/utl/meta/basic.h
Normal file
@ -0,0 +1,415 @@
|
||||
/*!
|
||||
* \file utl/meta/basic.h
|
||||
* \brief Template meta-programming basic definitions
|
||||
*/
|
||||
#ifndef __utl_meta_basic_h__
|
||||
#define __utl_meta_basic_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
/*!
|
||||
* \ingroup meta
|
||||
* \defgroup basic Basic
|
||||
* Basic definitions
|
||||
*/
|
||||
//! @{
|
||||
|
||||
/*!
|
||||
* \ingroup basic
|
||||
* \defgroup meta_core Core
|
||||
* Core definitions
|
||||
*/
|
||||
//! @{
|
||||
|
||||
namespace utl {
|
||||
namespace meta {
|
||||
|
||||
/*!
|
||||
* \brief meta's empty type
|
||||
*
|
||||
* utl::meta's nil type is not pure nil. It's a recursive "de-referencable nil.
|
||||
* Each time someone applies \c \::type to it, he gets back nil_. This way we can prevent
|
||||
* a lot of compilation errors in a wrong meta:: handling.
|
||||
*/
|
||||
struct nil_ {
|
||||
using type = nil_;
|
||||
};
|
||||
|
||||
//! Type alias for \c Tp::type.
|
||||
//! Is used to evaluate/extract return type of metafunctions
|
||||
//! \tparam Tp The metafunction to evaluate
|
||||
//! \return The inner \::type
|
||||
template <typename Tp>
|
||||
using eval = typename Tp::type;
|
||||
|
||||
//! Type alias for \c Tp::type::value.
|
||||
//! Is used to evaluate/extract return value of metafunctions
|
||||
//! \tparam Tp The metafunction to evaluate
|
||||
//! \return The inner \::type::value
|
||||
template <typename Tp>
|
||||
using eval_v = typename eval<Tp>::value;
|
||||
|
||||
|
||||
|
||||
//!
|
||||
//! integral_ is a holder class for a compile-time value of an integral type.
|
||||
//!
|
||||
//! Every Integral Constant is also a null-ary Metafunction, returning itself.\n
|
||||
//! An integral constant object is implicitly convertible to the corresponding
|
||||
//! run-time value of the wrapped integral type
|
||||
template <typename Tp, Tp v>
|
||||
using integral_ = std::integral_constant<Tp, v>;
|
||||
|
||||
|
||||
//! \name Wrappers for basic types
|
||||
//! @{
|
||||
|
||||
//! bool_ type: integral constant wrapper for bool
|
||||
template<bool v>
|
||||
using bool_ = integral_<bool, v>;
|
||||
|
||||
using true_ = bool_<true>; //!< The type used as a compile-time boolean with true value.
|
||||
using false_ = bool_<false>; //!< The type used as a compile-time boolean with false value.
|
||||
|
||||
//! int8_ type: integral constant wrapper for \c int8_t
|
||||
template<int8_t v>
|
||||
using int8_ = integral_<int8_t, v>;
|
||||
//! uint8_ type: integral constant wrapper for \c uint8_t
|
||||
template<uint8_t v>
|
||||
using uint8_ = integral_<uint8_t, v>;
|
||||
|
||||
//! int16_ type: integral constant wrapper for \c int16_t
|
||||
template<int16_t v>
|
||||
using int16_ = integral_<int16_t, v>;
|
||||
//! uint16_ type: integral constant wrapper for \c uint16_t
|
||||
template<uint16_t v>
|
||||
using uint16_ = integral_<uint16_t, v>;
|
||||
|
||||
//! int32_ type: integral constant wrapper for \c int32_t
|
||||
template<int32_t v>
|
||||
using int32_ = integral_<int32_t, v>;
|
||||
//! uint32_ type: integral constant wrapper for \c uint32_t
|
||||
template<uint32_t v>
|
||||
using uint32_ = integral_<uint32_t, v>;
|
||||
|
||||
//! char_ type: integral constant wrapper for \c char
|
||||
template<char v>
|
||||
using char_ = integral_<char, v>;
|
||||
|
||||
//! int_ type: integral constant wrapper for \c int
|
||||
template<int v>
|
||||
using int_ = integral_<int, v>;
|
||||
|
||||
//! long_ type: integral constant wrapper for \c long
|
||||
template<long v>
|
||||
using long_ = integral_<long, v>;
|
||||
|
||||
//! index_ type: integral constant wrapper for \c index_t a.k.a std::size_t
|
||||
template<index_t v>
|
||||
using index_ = integral_<index_t, v>;
|
||||
|
||||
//! size_ type: integral constant wrapper for \c size_t a.k.a std::size_t
|
||||
template<size_t v>
|
||||
using size_ = integral_<size_t, v>;
|
||||
|
||||
//! The last position we can express for indexing
|
||||
using Npos = size_<index_t(-1)>;
|
||||
//! @}
|
||||
|
||||
//! \name unevaluated expressions
|
||||
//! @{
|
||||
|
||||
//! Computes the size of the type \p Tp.
|
||||
//! Complexity \f$ O(1) \f$.
|
||||
template <typename Tp>
|
||||
using sizeof_ = size_<sizeof(Tp)>;
|
||||
|
||||
//! Computes the alignment required for any instance of the type \p Tp.
|
||||
//! Complexity \f$ O(1) \f$.
|
||||
template <typename Tp>
|
||||
using alignof_ = size_<alignof(Tp)>;
|
||||
//! @}
|
||||
|
||||
//! \name integer sequence
|
||||
//! @{
|
||||
template< class Tp, Tp... Ints >
|
||||
using integer_sequence = std::integer_sequence<Tp, Ints...>;
|
||||
|
||||
template<typename Tp, Tp Num>
|
||||
using make_integer_sequence = std::make_integer_sequence<Tp, Num>;
|
||||
|
||||
//! Alias template index_sequence
|
||||
template<index_t... Idx>
|
||||
using index_sequence = integer_sequence<index_t, Idx...>;
|
||||
|
||||
//! Alias template make_index_sequence
|
||||
template<index_t Num>
|
||||
using make_index_sequence = make_integer_sequence <index_t, Num>;
|
||||
|
||||
//! Alias template index_sequence_for
|
||||
template<typename... Types>
|
||||
using index_sequence_for = make_index_sequence<sizeof...(Types)>;
|
||||
//! @}
|
||||
}}
|
||||
|
||||
//!@}
|
||||
|
||||
/*!
|
||||
* \ingroup basic
|
||||
* \defgroup selection Selection
|
||||
* Type selection support header
|
||||
*/
|
||||
//! @{
|
||||
namespace utl {
|
||||
namespace meta{
|
||||
|
||||
//! \name if implementation
|
||||
//! @{
|
||||
namespace details {
|
||||
template <bool If, typename...>
|
||||
struct if_c_ {
|
||||
using type = nil_; //< avoid ill formed result
|
||||
};
|
||||
template<typename Then>
|
||||
struct if_c_<true, Then> {
|
||||
using type = Then;
|
||||
};
|
||||
template<typename Then, typename Else>
|
||||
struct if_c_<true, Then, Else> {
|
||||
using type = Then;
|
||||
};
|
||||
template<typename Then, typename Else>
|
||||
struct if_c_<false, Then, Else> {
|
||||
using type = Else;
|
||||
};
|
||||
}
|
||||
//! Select one type or another depending on a compile-time Boolean.
|
||||
template <bool B, typename... Args>
|
||||
using if_c = eval<details::if_c_<B, Args...>>;
|
||||
|
||||
//! Select one type or another depending on a compile-time Boolean type
|
||||
template <typename If, typename... Args>
|
||||
using if_ = if_c<If::type::value, Args...>;
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* \name Named type selectors
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! Select the first type of a type sequence
|
||||
template <typename T1, typename ...> using first_of = T1;
|
||||
|
||||
//! Select the second type of a type sequence
|
||||
template <typename T1, typename T2, typename ...> using second_of = T2;
|
||||
//! @}
|
||||
}}
|
||||
|
||||
//! @}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
* \ingroup basic
|
||||
* \defgroup logic_operations Logic Operations
|
||||
* logic operators and type relations support
|
||||
*/
|
||||
//! @{
|
||||
namespace utl {
|
||||
namespace meta{
|
||||
|
||||
/*!
|
||||
* \name Logical relation for types
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! Negate the *bool* constant parameter and return bool_
|
||||
template <bool B>
|
||||
using not_c = bool_<!B>;
|
||||
|
||||
//! negate the bool_ parameter and return bool_
|
||||
template<typename Tp>
|
||||
using not_ = not_c<Tp::type::value>;
|
||||
|
||||
//! \name OR implementation
|
||||
//! @{
|
||||
namespace details {
|
||||
template<typename...> struct _or_;
|
||||
|
||||
template<>
|
||||
struct _or_<> : false_ { };
|
||||
|
||||
template<typename T1>
|
||||
struct _or_<T1> : T1 { };
|
||||
|
||||
template<typename T1, typename T2>
|
||||
struct _or_ <T1, T2>
|
||||
: if_<T1, T1, T2> { };
|
||||
|
||||
template<typename T1, typename T2, typename T3, typename... Tn>
|
||||
struct _or_<T1, T2, T3, Tn...>
|
||||
: if_<T1, T1, _or_<T2, T3, Tn...>> { };
|
||||
}
|
||||
|
||||
//! Operator or for bool_ types
|
||||
//! \tparam Ts Variadic args of type bool_
|
||||
//! \return Logical or as bool_
|
||||
template <typename... Ts>
|
||||
using or_ = eval<details::_or_<Ts...>>;
|
||||
//! @}
|
||||
|
||||
//! \name AND implementation
|
||||
//! @{
|
||||
namespace details {
|
||||
template<typename...> struct _and_;
|
||||
|
||||
template<>
|
||||
struct _and_<>
|
||||
: true_ { };
|
||||
|
||||
template<typename T1>
|
||||
struct _and_ <T1>
|
||||
: T1 { };
|
||||
|
||||
template<typename T1, typename T2>
|
||||
struct _and_<T1, T2>
|
||||
: if_<T1, T2, T1> { };
|
||||
|
||||
template<typename T1, typename T2, typename T3, typename... Tn>
|
||||
struct _and_<T1, T2, T3, Tn...>
|
||||
: if_<T1, _and_<T2, T3, Tn...>, T1> { };
|
||||
}
|
||||
|
||||
//! Operator and for bool_ types
|
||||
//! \tparam Ts Variadic args of type bool_
|
||||
//! \return Logical and as bool_
|
||||
template <typename... Ts>
|
||||
using and_ = eval<details::_and_<Ts...>>;
|
||||
//! @}
|
||||
|
||||
//! \name same
|
||||
//! @{
|
||||
template<typename T1, typename T2>
|
||||
struct same : false_ { };
|
||||
|
||||
template<typename Tp>
|
||||
struct same <Tp, Tp> : true_ { };
|
||||
|
||||
template<typename T1, typename T2>
|
||||
using not_same = not_<eval<same<T1, T2>>>;
|
||||
//! @}
|
||||
|
||||
//! @}
|
||||
}}
|
||||
|
||||
//! @}
|
||||
|
||||
|
||||
/*!
|
||||
* \ingroup basic
|
||||
* \defgroup integral_operators integral operators
|
||||
* Type arithmetic and operations
|
||||
*/
|
||||
//! @{
|
||||
|
||||
namespace utl {
|
||||
namespace meta {
|
||||
|
||||
/*!
|
||||
* \name Math operations
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! Negation
|
||||
template <typename Tp>
|
||||
using negate = integral_<decltype(-Tp()), -Tp()>;
|
||||
//! Addition
|
||||
template <typename Tp1, typename Tp2>
|
||||
using add = integral_<
|
||||
decltype(Tp1() + Tp2()),
|
||||
Tp1() + Tp2()
|
||||
>;
|
||||
//! Multiplication
|
||||
template <typename Tp1, typename Tp2>
|
||||
using mult = integral_<
|
||||
decltype(Tp2() * Tp2()),
|
||||
Tp1() * Tp2()
|
||||
>;
|
||||
//! Division
|
||||
template <typename Tp1, typename Tp2>
|
||||
using divide = integral_<
|
||||
decltype(Tp2() / Tp2()),
|
||||
Tp1() / Tp2()
|
||||
>;
|
||||
//! Modulo
|
||||
template <typename Tp1, typename Tp2>
|
||||
using modulo = integral_<
|
||||
decltype(Tp1() % Tp2()),
|
||||
Tp1() % Tp2()
|
||||
>;
|
||||
//! Substruction
|
||||
template <typename Tp1, typename Tp2>
|
||||
using sub = add<Tp1, negate<Tp2>>;
|
||||
|
||||
//! Increase
|
||||
template <typename Tp>
|
||||
using inc = add<Tp, int_<1>>;
|
||||
|
||||
//! decrease
|
||||
template <typename Tp>
|
||||
using dec = add<Tp, int_<-1>>;
|
||||
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* \name Comparison operations
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! \return a true-valued Integral Constant if Tp1 and Tp2 are equal.
|
||||
template <typename Tp1, typename Tp2> using comp_eq = bool_<Tp1() == Tp2()>;
|
||||
//! \return a true-valued Integral Constant if Tp1 is less than Tp2.
|
||||
template <typename Tp1, typename Tp2> using comp_lt = bool_<(Tp1() < Tp2())>;
|
||||
|
||||
//! Not equal
|
||||
template <typename Tp1, typename Tp2> using comp_ne = not_<comp_eq<Tp1, Tp2>>;
|
||||
//! Greater than
|
||||
template <typename Tp1, typename Tp2> using comp_gt = comp_lt <Tp2, Tp1>;
|
||||
//! Less or equal
|
||||
template <typename Tp1, typename Tp2> using comp_le = not_<comp_lt<Tp2, Tp1>>;
|
||||
//! Greater or equal
|
||||
template <typename Tp1, typename Tp2> using comp_ge = not_<comp_lt<Tp1, Tp2>>;
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* \name Bitwise operations
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! \return bitwise not (~) operation of its argument.
|
||||
template <typename T> using bitnot_ = integral_<typename T::value_type, (typename T::value_type)(~T())>;
|
||||
//! \return bitwise and (&) operation of its arguments
|
||||
template <typename Tp1, typename Tp2>
|
||||
using bitand_ = integral_<decltype(Tp1() & Tp2()), Tp1() & Tp2()>;
|
||||
//! \return bitwise or (|) operation of its arguments.
|
||||
template <typename Tp1, typename Tp2>
|
||||
using bitor_ = integral_<decltype(Tp1() | Tp2()), Tp1() | Tp2()>;
|
||||
|
||||
//! \return bitwise xor (^) operation of its arguments.
|
||||
template <typename Tp1, typename Tp2>
|
||||
using bitxor_ = integral_<decltype(Tp1() ^ Tp2()), Tp1() ^ Tp2()>;
|
||||
//! \return the result of bitwise shift left (<<) operation on Tp.
|
||||
template <typename Tp, typename shift>
|
||||
using shift_left = integral_<typename Tp::value_type, (typename Tp::value_type)(Tp() << shift())>;
|
||||
//! \return the result of bitwise shift right (>>) operation on Tp.
|
||||
template <typename Tp, typename shift>
|
||||
using shift_right = integral_<typename Tp::value_type, (typename Tp::value_type)(Tp() >> shift())>;
|
||||
//! @}
|
||||
}}
|
||||
//! @}
|
||||
|
||||
//! @}
|
||||
|
||||
#endif /* __utl_meta_basic_h__ */
|
||||
@ -1,34 +1,17 @@
|
||||
/*!
|
||||
* \file detection.h
|
||||
* \brief Detection idiom based on WG21's N4502 [\ref 1] from Walter E. Brown
|
||||
*
|
||||
* Copyright (C) 2018-2019 Christos Choutouridis
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* \anchor [1]: www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf
|
||||
* \brief Detection idiom based on WG21's N4502 from Walter E. Brown
|
||||
*/
|
||||
#ifndef __utl_meta_detection_h__
|
||||
#define __utl_meta_detection_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/meta/operations.h>
|
||||
#include <utl/meta/basic.h>
|
||||
#include <type_traits>
|
||||
|
||||
/*!
|
||||
* \ingroup meta
|
||||
* \defgroup detection
|
||||
* \defgroup detection Detection
|
||||
* Detection idiom support header.
|
||||
*/
|
||||
//! @{
|
||||
@ -37,7 +20,7 @@ namespace utl {
|
||||
namespace meta {
|
||||
|
||||
/*!
|
||||
* void_t meta-function that maps a sequence of any types to the type void
|
||||
* \name void_t implementation
|
||||
*/
|
||||
//! @{
|
||||
#if defined(UTL_WORKAROUND_CWG_1558)
|
||||
@ -49,16 +32,18 @@ namespace meta {
|
||||
template<typename... _Ts>
|
||||
using void_t = eval<void_<_Ts...>>;
|
||||
#else
|
||||
//! void_ type alias
|
||||
//! void_ meta-function that maps a sequence of any types to the type void
|
||||
template <typename...> using void_ = void;
|
||||
//! void_t type alias
|
||||
//! void_t meta-function that maps a sequence of any types to the type void
|
||||
template <typename...> using void_t = void;
|
||||
#endif
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* Not a type to use in detected idiom. This type can
|
||||
* not be constructed, destructed or copied
|
||||
* \brief
|
||||
* Not a type to use in detected idiom.
|
||||
*
|
||||
* This type can not be constructed, destructed or copied.
|
||||
*/
|
||||
struct nat_ {
|
||||
nat_() = delete;
|
||||
@ -67,9 +52,9 @@ namespace meta {
|
||||
void operator = (nat_ const&) = delete;
|
||||
};
|
||||
|
||||
//! Detector for detection idiom
|
||||
//! \name Detector for detection idiom
|
||||
//! @{
|
||||
namespace detail {
|
||||
namespace details {
|
||||
template <typename Default,
|
||||
typename AlwaysVoid,
|
||||
template<typename...> class Op, typename... Args>
|
||||
@ -89,24 +74,23 @@ namespace meta {
|
||||
template <typename Default,
|
||||
template<typename...> class Op, typename... Args>
|
||||
using detected_or = detector<Default, void, Op, Args...>;
|
||||
} // namespace detail
|
||||
} // namespace details
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* detection interface
|
||||
* \name detection interface
|
||||
*/
|
||||
//! @{
|
||||
|
||||
/*!
|
||||
* Checks if Op<Args...> is a valid expression without evaluating it.
|
||||
*
|
||||
* \tparam Op a meta-callback function to pass Args...
|
||||
* \tparam Args... types to pass to Op for checking
|
||||
* \tparam Op a meta-callback function to pass Args...
|
||||
* \tparam Args... types to pass to Op for checking
|
||||
* \return status of the operation [bool_]
|
||||
* \arg true_ if Op<Args...> is valid expression
|
||||
* \arg false_ if Op<Args...> is not valid
|
||||
*
|
||||
* \example
|
||||
* \code
|
||||
* // archetypal alias for a copy assignment operation
|
||||
* template< class T > using copy_assign_t = decltype( declval<T&>() = declval<T const &>() );
|
||||
@ -115,7 +99,7 @@ namespace meta {
|
||||
* \endcode
|
||||
*/
|
||||
template <template<typename...> class Op, typename... Args>
|
||||
using is_detected = typename detail::detector<nat_, void, Op, Args...>::detected;
|
||||
using is_detected = typename details::detector<nat_, void, Op, Args...>::detected;
|
||||
|
||||
//! Detection predicate
|
||||
template< template<typename...> class Op, typename... Args>
|
||||
@ -124,12 +108,12 @@ namespace meta {
|
||||
/*!
|
||||
* Detection tool that evaluates to Op<Args...> if it's valid and to nat_ if not
|
||||
*
|
||||
* \param Op metafunction detector
|
||||
* \param Args... The arguments to pass to \p Op and check if is well formed
|
||||
* \tparam Op metafunction detector
|
||||
* \tparam Args... The arguments to pass to \p Op and check if is well formed
|
||||
* \return The result type
|
||||
* \arg Op<Args...> if is well formed
|
||||
* \arg nat_ if Op<Args...> is ill formed
|
||||
* \example
|
||||
*
|
||||
* \code
|
||||
* template <typename T> using try_type = typename T::type; // detector
|
||||
* template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
|
||||
@ -142,19 +126,19 @@ namespace meta {
|
||||
*/
|
||||
template <template<typename...> class Op, typename... Args>
|
||||
using detected_t = eval <
|
||||
detail::detector<nat_, void, Op, Args...>
|
||||
details::detector<nat_, void, Op, Args...>
|
||||
>;
|
||||
|
||||
/*!
|
||||
* Detection tool that evaluates to Op<Args...> if it's valid and to \p Default if not
|
||||
*
|
||||
* \param Default The resulting type if detection fail
|
||||
* \param Op metafunction detector
|
||||
* \param Args... The arguments to pass to \p Op and check if is well formed
|
||||
* \tparam Default The resulting type if detection fail
|
||||
* \tparam Op metafunction detector
|
||||
* \tparam Args... The arguments to pass to \p Op and check if is well formed
|
||||
* \return The result type
|
||||
* \arg Op<Args...> if is well formed
|
||||
* \arg Default if Op<Args...> is ill formed
|
||||
* \example
|
||||
*
|
||||
* \code
|
||||
* template <typename T> using try_type = typename T::type; // detector
|
||||
* template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
|
||||
@ -168,20 +152,20 @@ namespace meta {
|
||||
template <typename Default,
|
||||
template<typename...> class Op, typename... Args>
|
||||
using detected_or_t = eval <
|
||||
detail::detected_or<Default, Op, Args...>
|
||||
details::detected_or<Default, Op, Args...>
|
||||
>;
|
||||
|
||||
/*!
|
||||
* Detection tool that evaluates to true_ if evaluation of Op<Args...>
|
||||
* is \p Expected and to false_ if not
|
||||
*
|
||||
* \param Expected The expected resulting type if detection succeed
|
||||
* \param Op metafunction detector
|
||||
* \param Args... The arguments to pass to \p Op and check if is well formed
|
||||
* \tparam Expected The expected resulting type if detection succeed
|
||||
* \tparam Op metafunction detector
|
||||
* \tparam Args... The arguments to pass to \p Op and check if is well formed
|
||||
* \return The result type
|
||||
* \arg true_ if Op<Args...> is well formed and evaluate to Expected
|
||||
* \arg false_ Any other case
|
||||
* \example
|
||||
*
|
||||
* \code
|
||||
* template <typename T> using try_type = typename T::type; // detector
|
||||
* template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
|
||||
@ -195,7 +179,7 @@ namespace meta {
|
||||
template <typename Expected,
|
||||
template<typename...> class Op, typename... Args >
|
||||
using is_detected_exact = eval <
|
||||
same_<Expected, detected_t<Op, Args...>>
|
||||
same<Expected, detected_t<Op, Args...>>
|
||||
>;
|
||||
|
||||
//! evaluates to true if evaluation of Op<Args...> is \p Expected and to false if not
|
||||
@ -207,13 +191,13 @@ namespace meta {
|
||||
* Detection tool that evaluates to true_ if evaluation of Op<Args...> is convertible
|
||||
* to \p To and to false_ if not
|
||||
*
|
||||
* \param To The to convert to if detection succeed
|
||||
* \param Op metafunction detector
|
||||
* \param Args... The arguments to pass to \p Op and check if is well formed
|
||||
* \tparam To The to convert to if detection succeed
|
||||
* \tparam Op metafunction detector
|
||||
* \tparam Args... The arguments to pass to \p Op and check if is well formed
|
||||
* \return The result type
|
||||
* \arg true_ if Op<Args...> is well formed and convertible to To
|
||||
* \arg false_ Any other case
|
||||
* \example
|
||||
*
|
||||
* \code
|
||||
* template <typename T> using try_type = typename T::type; // detector
|
||||
* template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
|
||||
|
||||
@ -1,151 +0,0 @@
|
||||
/*!
|
||||
* \file integralconstant.h
|
||||
* \brief Template meta-programming integral constant
|
||||
*
|
||||
* Copyright (C) 2018-2019 Christos Choutouridis
|
||||
*
|
||||
* 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 __utl_meta_integralconstant_h__
|
||||
#define __utl_meta_integralconstant_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
/*!
|
||||
* \ingroup meta
|
||||
* \defgroup integral
|
||||
* integral constant support header
|
||||
*/
|
||||
//! @{
|
||||
|
||||
namespace utl {
|
||||
namespace meta {
|
||||
|
||||
/*!
|
||||
* Empty type
|
||||
* utl::meta's nil type is not pure nil. It's a recursive "de-referencable nil.
|
||||
* Each time someone applies ::type to it, he gets back nil_. This way we can prevent
|
||||
* a lot of compilation errors in a wrong meta:: handling.
|
||||
*/
|
||||
struct nil_ {
|
||||
using type = nil_;
|
||||
};
|
||||
|
||||
//! Type alias for \p Tp::type. Used to evaluate/extract return type of metafunctions
|
||||
template <typename Tp>
|
||||
using eval = typename Tp::type;
|
||||
|
||||
//! integral_
|
||||
//! Integral Constant is a holder class for a compile-time value of an integral type.
|
||||
//! Every Integral Constant is also a null-ary Metafunction, returning itself.
|
||||
//! An integral constant object is implicitly convertible to the corresponding
|
||||
//! run-time value of the wrapped integral type
|
||||
//! @{
|
||||
template <typename Tp, Tp v>
|
||||
using integral_ = std::integral_constant<Tp, v>;
|
||||
//! @}
|
||||
|
||||
//! Wrappers for basic types
|
||||
//! @{
|
||||
|
||||
//! bool_ type: integral constant wrapper for bool
|
||||
template<bool v>
|
||||
using bool_ = integral_<bool, v>;
|
||||
|
||||
using true_ = bool_<true>; //!< The type used as a compile-time boolean with true value.
|
||||
using false_ = bool_<false>; //!< The type used as a compile-time boolean with false value.
|
||||
|
||||
//! int8_ type: integral constant wrapper for \c int8_t
|
||||
template<int8_t v>
|
||||
using int8_ = integral_<int8_t, v>;
|
||||
//! uint8_ type: integral constant wrapper for \c uint8_t
|
||||
template<uint8_t v>
|
||||
using uint8_ = integral_<uint8_t, v>;
|
||||
|
||||
//! int16_ type: integral constant wrapper for \c int16_t
|
||||
template<int16_t v>
|
||||
using int16_ = integral_<int16_t, v>;
|
||||
//! uint16_ type: integral constant wrapper for \c uint16_t
|
||||
template<uint16_t v>
|
||||
using uint16_ = integral_<uint16_t, v>;
|
||||
|
||||
//! int32_ type: integral constant wrapper for \c int32_t
|
||||
template<int32_t v>
|
||||
using int32_ = integral_<int32_t, v>;
|
||||
//! uint32_ type: integral constant wrapper for \c uint32_t
|
||||
template<uint32_t v>
|
||||
using uint32_ = integral_<uint32_t, v>;
|
||||
|
||||
//! char_ type: integral constant wrapper for \c char
|
||||
template<char v>
|
||||
using char_ = integral_<char, v>;
|
||||
|
||||
//! int_ type: integral constant wrapper for \c int
|
||||
template<int v>
|
||||
using int_ = integral_<int, v>;
|
||||
|
||||
//! long_ type: integral constant wrapper for \c long
|
||||
template<long v>
|
||||
using long_ = integral_<long, v>;
|
||||
|
||||
//! index_ type: integral constant wrapper for \c index_t a.k.a std::size_t
|
||||
template<index_t v>
|
||||
using index_ = integral_<index_t, v>;
|
||||
|
||||
//! size_ type: integral constant wrapper for \c size_t a.k.a std::size_t
|
||||
template<size_t v>
|
||||
using size_ = integral_<size_t, v>;
|
||||
|
||||
//! Computes the size of the type \p Tp.
|
||||
//! Complexity \f$ O(1) \f$.
|
||||
template <typename Tp>
|
||||
using sizeof_ = size_<sizeof(Tp)>;
|
||||
|
||||
//! Computes the alignment required for any instance of the type \p Tp.
|
||||
//! Complexity \f$ O(1) \f$.
|
||||
template <typename Tp>
|
||||
using alignof_ = size_<alignof(Tp)>;
|
||||
//! @}
|
||||
|
||||
//! The last position we can express for indexing
|
||||
using Npos = size_<index_t(-1)>;
|
||||
|
||||
//! integer sequence
|
||||
//! @{
|
||||
template< class Tp, Tp... Ints >
|
||||
using integer_sequence = std::integer_sequence<Tp, Ints...>;
|
||||
|
||||
template<typename Tp, Tp Num>
|
||||
using make_integer_sequence = std::make_integer_sequence<Tp, Num>;
|
||||
|
||||
//! Alias template index_sequence
|
||||
template<index_t... Idx>
|
||||
using index_sequence = integer_sequence<index_t, Idx...>;
|
||||
|
||||
//! Alias template make_index_sequence
|
||||
template<index_t Num>
|
||||
using make_index_sequence = make_integer_sequence <index_t, Num>;
|
||||
|
||||
//! Alias template index_sequence_for
|
||||
template<typename... Types>
|
||||
using index_sequence_for = make_index_sequence<sizeof...(Types)>;
|
||||
|
||||
//! @}
|
||||
}}
|
||||
|
||||
//!@}
|
||||
|
||||
#endif /* __utl_meta_integralconstant_h__ */
|
||||
@ -1,75 +1,64 @@
|
||||
/*!
|
||||
* \file invoke.h
|
||||
* \file utl/meta/invoke.h
|
||||
* \brief Template meta-programming utilities for callables
|
||||
*
|
||||
* Copyright (C) 2018 Christos Choutouridis
|
||||
*
|
||||
* 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 detail.
|
||||
*
|
||||
* 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 __utl_meta_invoke_h__
|
||||
#define __utl_meta_invoke_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/meta/integral.h>
|
||||
#include <utl/meta/basic.h>
|
||||
#include <utl/meta/detection.h>
|
||||
#include <utl/meta/operations.h>
|
||||
|
||||
/*!
|
||||
* \ingroup meta
|
||||
* \defgroup invoke
|
||||
* \defgroup meta_invoke Invoke
|
||||
* A meta-programming invoke() analogous.
|
||||
*
|
||||
* This module provides <b>higher order</b> tools to meta. utl::meta's metafunctions inputs are types.
|
||||
* The metafunctions though are templates in the form of `template <typename...> class`.
|
||||
* So we can not pass metafunctions to other metafunctions. The utl::meta provides tools to wrap metafunctions
|
||||
* in a type. This way we create the concepts of:
|
||||
*
|
||||
* - <b>\c invocable</b> which is a type containing a metafunction inside.
|
||||
* - <b>\c evaluation</b> which is the way of unwrapping the metafunction inside the invocable type.
|
||||
*
|
||||
* In order to accomplish that, by convention, a \c meta::invocable shall contain a nested
|
||||
* template type named \c apply which is bind to actual invocable meta-function. Then we can:
|
||||
*
|
||||
* - Use \c wrap<> or even better \c quote<> in order to wrap a metafunction to a type (metafunction class)
|
||||
* - Pass these wrapped types to other metafunctions
|
||||
* - \c invoke<> the inner \c apply from a wrapped metafunction class.
|
||||
*/
|
||||
//! @{
|
||||
namespace utl {
|
||||
namespace meta{
|
||||
|
||||
/*!
|
||||
* \name meta::invoke
|
||||
*
|
||||
* A meta-programming invoke() analogous. A \c meta::invocable shall contain a nested
|
||||
* template type named \b apply which is bind to actual invocable meta-function.
|
||||
*
|
||||
* - We can use \c wrap<> or even better \c quote<> in order to wrap a metafunction to a type (metafunction class)
|
||||
* - We can pass these wrapped types to other metafunctions
|
||||
* - We can \c invoke<> the inner \c apply from a wrapped metafunction class.
|
||||
* \name identity implementation
|
||||
*/
|
||||
//! @{
|
||||
|
||||
/*!
|
||||
* identity, identity_t.
|
||||
*/
|
||||
//! @{
|
||||
template <typename _Tp>
|
||||
//! Identity is a metafunction always return the input type
|
||||
template <typename Tp>
|
||||
struct identity {
|
||||
#if defined (UTL_WORKAROUND_CWG_1558)
|
||||
// redirect unused Ts... via void_t
|
||||
template <typename... Ts>
|
||||
using apply = first_of<_Tp, void_t<Ts...>>; //!< identity is invokable, must also have apply
|
||||
using apply = first_of<Tp, void_t<Ts...>>; //!< identity is invokable, must also have apply
|
||||
#else
|
||||
template <typename...>
|
||||
using apply = _Tp; //!< identity is invokable, must also have apply
|
||||
using apply = Tp; //!< identity is invokable, must also have apply
|
||||
#endif
|
||||
using type = _Tp; //!< identity
|
||||
using type = Tp; //!< identity
|
||||
};
|
||||
|
||||
//! identity type alias
|
||||
template <typename _Tp>
|
||||
using identity_t = eval<identity<_Tp>>;
|
||||
template <typename Tp>
|
||||
using identity_t = eval<identity<Tp>>;
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* invoke, invoke_t
|
||||
* \name invoke, invoke_t
|
||||
*/
|
||||
//! @{
|
||||
/*!
|
||||
@ -88,13 +77,19 @@ namespace meta{
|
||||
using invoke_t = eval< invoke <Fn, Args...>>;
|
||||
//! @}
|
||||
|
||||
//! wrap
|
||||
//! \name wrap
|
||||
//! @{
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Wraps an n-ary Metafunction \c F to a Metafunction Class
|
||||
*
|
||||
* wrap is a higher-order primitive that wraps an n-ary Metafunction
|
||||
* to create a corresponding Metafunction Class (Invocable). This way
|
||||
* we can pass Metafunctions as types to other metafunctions and let
|
||||
* them \c invoke the inner templated apply
|
||||
* them \c invoke the inner templated apply.
|
||||
*
|
||||
* \tparam F The metafunction to wrap
|
||||
*/
|
||||
template <template <typename...> class F>
|
||||
struct wrap {
|
||||
@ -102,7 +97,19 @@ namespace meta{
|
||||
using apply = F<Args...>;
|
||||
};
|
||||
|
||||
//! Wrap a template \p F taking literal constants of type \p T into an Invokable
|
||||
/*!
|
||||
* \brief
|
||||
* Wraps an n-ary Metafunction \c F taking literal constants of type \c T
|
||||
* to a Metafunction Class
|
||||
*
|
||||
* wrap_i is a higher-order primitive that wraps an n-ary Metafunction
|
||||
* to create a corresponding Metafunction Class (Invocable).
|
||||
* This way we can pass Metafunctions as types to other metafunctions and let
|
||||
* them \c invoke the inner templated apply.
|
||||
*
|
||||
* \tparam T Type of integral constants
|
||||
* \tparam F The metafunction to wrap
|
||||
*/
|
||||
template <typename T, template <T...> class F>
|
||||
struct wrap_i {
|
||||
// requires meta::Integral
|
||||
@ -111,9 +118,9 @@ namespace meta{
|
||||
};
|
||||
//! @}
|
||||
|
||||
//! Is applicable trait
|
||||
//! \name Is applicable trait
|
||||
//! @{
|
||||
namespace detail {
|
||||
namespace details {
|
||||
|
||||
template<template<typename...> class F, typename... T>
|
||||
struct is_applicable_ {
|
||||
@ -133,7 +140,7 @@ namespace meta{
|
||||
static nil_ check (...); //< all other combinations
|
||||
|
||||
using type = if_ <
|
||||
not_same_<
|
||||
not_same<
|
||||
nil_,
|
||||
decltype(check<F>(0))
|
||||
>, true_, false_
|
||||
@ -156,29 +163,51 @@ namespace meta{
|
||||
};
|
||||
}
|
||||
|
||||
//! check if we can instantiate \p F with parameters \p T
|
||||
template<template<typename...> class F, typename... T>
|
||||
/*!
|
||||
* \brief
|
||||
* Check if we can instantiate metafunction \c F with parameters \p Ts
|
||||
*
|
||||
* \tparam F The metafunction
|
||||
* \tparam Ts The parameters to \c F
|
||||
*/
|
||||
template<template<typename...> class F, typename... Ts>
|
||||
using is_applicable_t = eval<
|
||||
detail::is_applicable_<F, T...>
|
||||
>;
|
||||
//! check if we can invoke \p Q with parameters \p T
|
||||
template<typename Q, typename... T>
|
||||
using is_applicable_qt = eval <
|
||||
detail::is_applicable_q_ <Q, T...>
|
||||
details::is_applicable_<F, Ts...>
|
||||
>;
|
||||
|
||||
//! check if we can instantiate \p F with parameters \p Is of type \p T
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Check if we can invoke the quoted metafunction \c Q with parameters \p Ts
|
||||
*
|
||||
* \tparam Q The quoted metafunction
|
||||
* \tparam Ts The parameters to \c Q
|
||||
*
|
||||
* \sa utl::meta::quote
|
||||
*/
|
||||
template<typename Q, typename... Ts>
|
||||
using is_applicable_qt = eval <
|
||||
details::is_applicable_q_ <Q, Ts...>
|
||||
>;
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Check if we can instantiate metafunction \c F with parameters \p Is of type \c T
|
||||
*
|
||||
* \tparam T The type of \c Is
|
||||
* \tparam F The metafunction
|
||||
* \tparam Is The parameters to \c F
|
||||
*/
|
||||
template <typename T, template<T...> class F, T... Is>
|
||||
using is_applicable_it = eval<
|
||||
detail::is_applicable_i_<T, F, Is...>
|
||||
details::is_applicable_i_<T, F, Is...>
|
||||
>;
|
||||
|
||||
//! @}
|
||||
|
||||
//! defer
|
||||
//! \name defer
|
||||
//! @{
|
||||
namespace detail {
|
||||
//! @{
|
||||
namespace details {
|
||||
template<template<typename...> class F, typename... Ts>
|
||||
struct defer_ {
|
||||
using type = F<Ts...>;
|
||||
@ -194,41 +223,64 @@ namespace meta{
|
||||
//! template<template<typename...> class F, typename... Ts>
|
||||
//! using defer_ = F<Ts...>;
|
||||
//!
|
||||
//! The use of struct here is due to Core issue 1430 [\ref link1 1] and is used
|
||||
//! as suggested by Roy Crihfield in [\ref link2 2].
|
||||
//! The use of struct here is due to Core issue 1430 [1] and is used
|
||||
//! as suggested by Roy Crihfield in [2].
|
||||
//! In short, this is due to language's inability to expand Ts... into
|
||||
//! a fixed parameter list of an alias template.
|
||||
//!
|
||||
//! \anchor link1 [1]: https://wg21.link/cwg1430
|
||||
//! \anchor link2 [2]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59498
|
||||
//! @}
|
||||
//! [1] https://wg21.link/cwg1430\n
|
||||
//! [2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59498
|
||||
}
|
||||
|
||||
//! defer alias template for F<Ts...>
|
||||
/*!
|
||||
* \brief
|
||||
* Postpone the instantiation of a metafunction \c F with parameters \c Ts
|
||||
*
|
||||
* This metafunction first checks if the given arguments \c Ts are applicable to
|
||||
* the metafunction \c F and if so nest inside (a layer deeper) the <em>metafunction call</em>.
|
||||
*
|
||||
* \tparam F The metafunction
|
||||
* \tparam Ts The parameters (arguments) to \c F
|
||||
*/
|
||||
template<template<class...> class F, class... Ts>
|
||||
using defer = if_<
|
||||
detail::is_applicable_<F, Ts...>,
|
||||
detail::defer_<F, Ts...>,
|
||||
details::is_applicable_<F, Ts...>,
|
||||
details::defer_<F, Ts...>,
|
||||
nil_ //!< Safe, nil_ is dereferencable
|
||||
>;
|
||||
|
||||
//! defer_i alias template for F<T, Is...>
|
||||
/*!
|
||||
* \brief
|
||||
* Postpone the instantiation of a metafunction \c F with parameters \c Is of type \c T.
|
||||
*
|
||||
* This metafunction first checks if the given arguments \c Is of type \c T are applicable to
|
||||
* the metafunction \c F and if so nest inside (a layer deeper) the <em>metafunction call</em>.
|
||||
*
|
||||
* \tparam T The type of parameters
|
||||
* \tparam F The metafunction
|
||||
* \tparam Is The parameters (arguments) to \c F
|
||||
*/
|
||||
template <typename T, template<T...> class F, T... Is>
|
||||
using defer_i = if_ <
|
||||
detail::is_applicable_i_<T, F, Is...>,
|
||||
detail::defer_i_<T, F, Is...>,
|
||||
details::is_applicable_i_<T, F, Is...>,
|
||||
details::defer_i_<T, F, Is...>,
|
||||
nil_ //!< Safe, nil_ is dereferencable
|
||||
>;
|
||||
//! @}
|
||||
|
||||
//! quote
|
||||
//! \name quote
|
||||
//! @{
|
||||
/*!
|
||||
* quote deferred is a higher-order primitive that wraps an n-ary Metafunction
|
||||
* \brief
|
||||
* Wraps an n-ary Metafunction \c F to a Metafunction Class using meta::defer<>
|
||||
*
|
||||
* quote is a higher-order primitive that wraps an n-ary Metafunction
|
||||
* to create a corresponding Metafunction Class (Invocable) using defer<> to
|
||||
* postpone the evaluation of Metafunction. This is a safe version of \c wrap<>.
|
||||
* postpone the evaluation of Metafunction. This is a safe version of \c wrap<>.\n
|
||||
* Again this way we can pass Metafunctions as types to other metafunctions and let
|
||||
* them \c invoke the inner templated apply
|
||||
*
|
||||
* \tparam F The metafunction to wrap
|
||||
*/
|
||||
template <template <typename...> class F>
|
||||
struct quote {
|
||||
@ -238,7 +290,21 @@ namespace meta{
|
||||
>;
|
||||
};
|
||||
|
||||
//! Wrap a template \p F taking literal constants of type \p T into an Invokable
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* Wraps an n-ary Metafunction \c F taking literal constants of type \c T
|
||||
* to a Metafunction Class using meta::defer<>
|
||||
*
|
||||
* quote is a higher-order primitive that wraps an n-ary Metafunction
|
||||
* to create a corresponding Metafunction Class (Invocable) using defer<> to
|
||||
* postpone the evaluation of Metafunction. This is a safe version of \c wrap<>.\n
|
||||
* Again this way we can pass Metafunctions as types to other metafunctions and let
|
||||
* them \c invoke the inner templated apply
|
||||
*
|
||||
* \tparam T Type of integral constants
|
||||
* \tparam F The metafunction to wrap
|
||||
*/
|
||||
template <typename T, template <T...> class F>
|
||||
struct quote_i {
|
||||
// requires meta::Integral
|
||||
@ -249,9 +315,9 @@ namespace meta{
|
||||
};
|
||||
//! @}
|
||||
|
||||
//! compose
|
||||
//! \name compose
|
||||
//! @{
|
||||
namespace detail {
|
||||
namespace details {
|
||||
template <template <typename...> class... Fns> struct compose_f_ {};
|
||||
|
||||
// recursive call to all invokes
|
||||
@ -292,12 +358,13 @@ namespace meta{
|
||||
}
|
||||
|
||||
/*!
|
||||
* Create an invocable from other invocables(quoted metafunctions) by composition.
|
||||
* \brief
|
||||
* Create an invocable from other invocables(quoted metafunctions) by composition.
|
||||
* \note
|
||||
* This implies from N invocables in \p Fns the first N-1 has to be unary.
|
||||
* Thats because of the "return" type of metafunction. They can only return one
|
||||
* type. So for n-ary invocables in the N-1 places the typelist<> is the solution.
|
||||
* \example
|
||||
*
|
||||
* \code
|
||||
* static_assert( std::is_same<
|
||||
* invoke<compose<quote<F1>, quote<F2>, quote<F3>>, int>, F1<F2<F3<int>>>
|
||||
@ -305,15 +372,16 @@ namespace meta{
|
||||
* \endcode
|
||||
*/
|
||||
template <typename... Fns>
|
||||
using compose = detail::compose_<Fns...>;
|
||||
using compose = details::compose_<Fns...>;
|
||||
|
||||
/*!
|
||||
* Create an invocable from other metafunctions by composition.
|
||||
* \brief
|
||||
* Create an invocable from other metafunctions by composition.
|
||||
* \note
|
||||
* This implies from N invocables in \p Fns the first N-1 has to be unary.
|
||||
* Thats because of the "return" type of metafunction. They can only return one
|
||||
* type. So for n-ary invocables in the N-1 places the typelist<> is the solution.
|
||||
* \example
|
||||
*
|
||||
* \code
|
||||
* static_assert( std::is_same<
|
||||
* invoke<compose_f<F1, F2, F3>, int>, F1 <F2 <F3 <int>>>
|
||||
@ -321,12 +389,12 @@ namespace meta{
|
||||
* \endcode
|
||||
*/
|
||||
template <template <typename...> class... Fns>
|
||||
using compose_f = detail::compose_f_<Fns...>;
|
||||
using compose_f = details::compose_f_<Fns...>;
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* Applies the invocable \p Fn by binding the arguments \p Ts
|
||||
* to the front of \p Fn.
|
||||
* \brief
|
||||
* Applies the invocable \p Fn by binding the arguments \p Ts to the front of \p Fn.
|
||||
*/
|
||||
template<typename Fn, typename... Ts>
|
||||
struct bind_front {
|
||||
@ -335,8 +403,8 @@ namespace meta{
|
||||
};
|
||||
|
||||
/*!
|
||||
* Applies the Invocable \p Fn by binding the arguments \p Ts
|
||||
* to the back of \p Fn.
|
||||
* \brief
|
||||
* Applies the Invocable \p Fn by binding the arguments \p Ts to the back of \p Fn.
|
||||
*/
|
||||
template<typename Fn, typename... Ts>
|
||||
struct bind_back {
|
||||
@ -351,18 +419,17 @@ namespace meta{
|
||||
template <typename T1>
|
||||
struct same_as {
|
||||
template <typename T2>
|
||||
struct apply : same_<T1, T2> { };
|
||||
struct apply : same<T1, T2> { };
|
||||
};
|
||||
|
||||
template <typename T1>
|
||||
struct not_same_as {
|
||||
template <typename T2>
|
||||
struct apply : not_same_<T1, T2> { };
|
||||
struct apply : not_same<T1, T2> { };
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
//! @}
|
||||
//! @}
|
||||
|
||||
#endif /* __utl_meta_invoke_h__ */
|
||||
|
||||
@ -1,30 +1,44 @@
|
||||
/*!
|
||||
* \file /utl/meta/meta.h
|
||||
* \brief Include all meta library
|
||||
*
|
||||
* Copyright (C) 2018-2019 Christos Choutouridis
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* \file utl/meta/meta.h
|
||||
* \brief Meta library forward header
|
||||
*/
|
||||
#ifndef __utl_meta_meta_h__
|
||||
#define __utl_meta_meta_h__
|
||||
|
||||
#include <utl/meta/integral.h>
|
||||
#include <utl/meta/selection.h>
|
||||
#include <utl/meta/operations.h>
|
||||
#include <utl/meta/useif.h>
|
||||
/*! \defgroup meta Meta
|
||||
* An embedded metaprogramming library for uTL.
|
||||
*
|
||||
* uTL::meta is a simple metaprogramming library used widely inside uTL.
|
||||
* The lib is provided to the end user via namespace \c utl::meta
|
||||
* The library assembles:
|
||||
*
|
||||
* - integral constant based, operation and arithmetic functionality\n
|
||||
* meta defines wrappers for all of integral types such as \c int, \c long, \c char, \c uint32_t etc...
|
||||
* and also facilities to emulate:
|
||||
* 1. <b>conditional (if)</b>
|
||||
* 2. <b>logical operations</b> like \c or, \c and, \c not, \c bitor, \c bitand etc...
|
||||
* 3. <b>math operations</b> like \c add, \c sub, \c modulo
|
||||
* 4. <b>comparison operations</b> like \c equal, \c not_equal etc...
|
||||
*
|
||||
* All of these operate on integral types.
|
||||
*
|
||||
* - <b>SFINAE</b> wrappers as syntactic sugar to the rest of the uTL.
|
||||
*
|
||||
* - Walter's Brown <b>detection idiom</b> to help uTL concept implementation.
|
||||
*
|
||||
* - <b>Higher order</b> metafunction tools for composition.\n
|
||||
* This module provides tools such as \c wrap, \c compose, \c fold, \c bind etc... The user can
|
||||
* wrap metafunctions as types and pass them around to other metafunctions. The evaluation
|
||||
* of these functions can be done both in aggressive or lazy maner using tools such as \c eval or \c invoke
|
||||
*
|
||||
* - <b>typelist "container"</b> implementation.\n
|
||||
* Typelist is a container like template type holding the parameter list as items in the container.
|
||||
* This facility has also all the expected eco-system of functions like \c push_front,
|
||||
* \c push_back, \c at etc...
|
||||
*/
|
||||
|
||||
#include <utl/meta/basic.h>
|
||||
#include <utl/meta/sfinae.h>
|
||||
#include <utl/meta/typelist.h>
|
||||
#include <utl/meta/detection.h>
|
||||
#include <utl/meta/invoke.h>
|
||||
|
||||
@ -1,227 +0,0 @@
|
||||
/*!
|
||||
* \file operations.h
|
||||
* \brief Integral constant operations and logical operations
|
||||
*
|
||||
* Copyright (C) 2018-2019 Christos Choutouridis
|
||||
*
|
||||
* 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 __utl_meta_operations_h__
|
||||
#define __utl_meta_operations_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/meta/selection.h>
|
||||
|
||||
|
||||
/*!
|
||||
* \ingroup meta
|
||||
* \defgroup logic
|
||||
* logic operators and type relations support
|
||||
*/
|
||||
//! @{
|
||||
namespace utl {
|
||||
namespace meta{
|
||||
|
||||
/*!
|
||||
* Logical relation for types
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! Negate the *bool* constant parameter
|
||||
template <bool B>
|
||||
using not_c = bool_<!B>;
|
||||
|
||||
//! not
|
||||
template<typename _Tp>
|
||||
using not_ = not_c<_Tp::type::value>;
|
||||
|
||||
//! OR implementation
|
||||
//! @{
|
||||
namespace detail {
|
||||
template<typename...> struct _or_;
|
||||
|
||||
template<>
|
||||
struct _or_<> : false_ { };
|
||||
|
||||
template<typename _T1>
|
||||
struct _or_<_T1> : _T1 { };
|
||||
|
||||
template<typename _T1, typename _T2>
|
||||
struct _or_ <_T1, _T2>
|
||||
: if_<_T1, _T1, _T2> { };
|
||||
|
||||
template<typename _T1, typename _T2, typename _T3, typename... _Tn>
|
||||
struct _or_<_T1, _T2, _T3, _Tn...>
|
||||
: if_<_T1, _T1, _or_<_T2, _T3, _Tn...>> { };
|
||||
}
|
||||
|
||||
template <typename... _Ts>
|
||||
using or_ = eval<detail::_or_<_Ts...>>;
|
||||
//! @}
|
||||
|
||||
//! AND implementation
|
||||
//! @{
|
||||
namespace detail {
|
||||
template<typename...> struct _and_;
|
||||
|
||||
template<>
|
||||
struct _and_<>
|
||||
: true_ { };
|
||||
|
||||
template<typename _T1>
|
||||
struct _and_ <_T1>
|
||||
: _T1 { };
|
||||
|
||||
template<typename _T1, typename _T2>
|
||||
struct _and_<_T1, _T2>
|
||||
: if_<_T1, _T2, _T1> { };
|
||||
|
||||
template<typename _T1, typename _T2, typename _T3, typename... _Tn>
|
||||
struct _and_<_T1, _T2, _T3, _Tn...>
|
||||
: if_<_T1, _and_<_T2, _T3, _Tn...>, _T1> { };
|
||||
}
|
||||
|
||||
template <typename... _Ts>
|
||||
using and_ = eval<detail::_and_<_Ts...>>;
|
||||
//! @}
|
||||
|
||||
//! same
|
||||
//! @{
|
||||
template<typename _T1, typename _T2>
|
||||
struct same_ : false_ { };
|
||||
|
||||
template<typename _Tp>
|
||||
struct same_ <_Tp, _Tp> : true_ { };
|
||||
//! @}
|
||||
|
||||
//! not same
|
||||
//! @{
|
||||
template<typename _T1, typename _T2>
|
||||
using not_same_ = not_<eval<same_<_T1, _T2>>>;
|
||||
//! @}
|
||||
|
||||
//! @}
|
||||
}}
|
||||
|
||||
//! @}
|
||||
|
||||
|
||||
/*!
|
||||
* \ingroup meta
|
||||
* \defgroup integral operators
|
||||
* Type arithmetic and operators
|
||||
*/
|
||||
//! @{
|
||||
|
||||
namespace utl {
|
||||
namespace meta {
|
||||
|
||||
/*!
|
||||
* Math operations
|
||||
* requires IntegralConstant(_Tp)
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! Negation
|
||||
template <typename _Tp>
|
||||
using negate = integral_<decltype(-_Tp()), -_Tp()>;
|
||||
//! Addition
|
||||
template <typename _Tp1, typename _Tp2>
|
||||
using add = integral_<
|
||||
decltype(_Tp1() + _Tp2()),
|
||||
_Tp1() + _Tp2()
|
||||
>;
|
||||
//! Multiplication
|
||||
template <typename _Tp1, typename _Tp2>
|
||||
using mult = integral_<
|
||||
decltype(_Tp2() * _Tp2()),
|
||||
_Tp1() * _Tp2()
|
||||
>;
|
||||
//! Division
|
||||
template <typename _Tp1, typename _Tp2>
|
||||
using divide = integral_<
|
||||
decltype(_Tp2() / _Tp2()),
|
||||
_Tp1() / _Tp2()
|
||||
>;
|
||||
//! Modulo
|
||||
template <typename _Tp1, typename _Tp2>
|
||||
using modulo = integral_<
|
||||
decltype(_Tp1() % _Tp2()),
|
||||
_Tp1() % _Tp2()
|
||||
>;
|
||||
//! Substruction
|
||||
template <typename _Tp1, typename _Tp2>
|
||||
using sub = add<_Tp1, negate<_Tp2>>;
|
||||
|
||||
//! Increase
|
||||
template <typename _Tp>
|
||||
using inc = add<_Tp, int_<1>>;
|
||||
|
||||
//! decrease
|
||||
template <typename _Tp>
|
||||
using dec = add<_Tp, int_<-1>>;
|
||||
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* Comparison operations
|
||||
* requires IntegralConstant(_Tp)
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! \return a true-valued Integral Constant if _Tp1 and _Tp2 are equal.
|
||||
template <typename _Tp1, typename _Tp2> using comp_eq = bool_<_Tp1() == _Tp2()>;
|
||||
//! \return a true-valued Integral Constant if _Tp1 is less than _Tp2.
|
||||
template <typename _Tp1, typename _Tp2> using comp_lt = bool_<(_Tp1() < _Tp2())>;
|
||||
|
||||
//! Not equal
|
||||
template <typename _Tp1, typename _Tp2> using comp_ne = not_<comp_eq<_Tp1, _Tp2>>;
|
||||
//! Greater than
|
||||
template <typename _Tp1, typename _Tp2> using comp_gt = comp_lt <_Tp2, _Tp1>;
|
||||
//! Less or equal
|
||||
template <typename _Tp1, typename _Tp2> using comp_le = not_<comp_lt<_Tp2, _Tp1>>;
|
||||
//! Greater or equal
|
||||
template <typename _Tp1, typename _Tp2> using comp_ge = not_<comp_lt<_Tp1, _Tp2>>;
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* Bitwise operations
|
||||
* requires IntegralConstant(_Tp)
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! \return bitwise not (~) operation of its argument.
|
||||
template <typename _T> using bitnot_ = integral_<typename _T::value_type, (typename _T::value_type)(~_T())>;
|
||||
//! \return bitwise and (&) operation of its arguments
|
||||
template <typename _Tp1, typename _Tp2>
|
||||
using bitand_ = integral_<decltype(_Tp1() & _Tp2()), _Tp1() & _Tp2()>;
|
||||
//! \return bitwise or (|) operation of its arguments.
|
||||
template <typename _Tp1, typename _Tp2>
|
||||
using bitor_ = integral_<decltype(_Tp1() | _Tp2()), _Tp1() | _Tp2()>;
|
||||
|
||||
//! \return bitwise xor (^) operation of its arguments.
|
||||
template <typename _Tp1, typename _Tp2>
|
||||
using bitxor_ = integral_<decltype(_Tp1() ^ _Tp2()), _Tp1() ^ _Tp2()>;
|
||||
//! \return the result of bitwise shift left (<<) operation on _Tp.
|
||||
template <typename _Tp, typename shift>
|
||||
using shift_left = integral_<typename _Tp::value_type, (typename _Tp::value_type)(_Tp() << shift())>;
|
||||
//! \return the result of bitwise shift right (>>) operation on _Tp.
|
||||
template <typename _Tp, typename shift>
|
||||
using shift_right = integral_<typename _Tp::value_type, (typename _Tp::value_type)(_Tp() >> shift())>;
|
||||
//! @}
|
||||
}}
|
||||
//!@}
|
||||
|
||||
|
||||
#endif /* __utl_meta_operations_h__ */
|
||||
@ -1,86 +0,0 @@
|
||||
/*!
|
||||
* \file selection.h
|
||||
* \brief Template meta-programming type selections.
|
||||
*
|
||||
* Copyright (C) 2018-2019 Christos Choutouridis
|
||||
*
|
||||
* 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 __utl_meta_selection_h__
|
||||
#define __utl_meta_selection_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/meta/integral.h>
|
||||
|
||||
/*!
|
||||
* \ingroup meta
|
||||
* \defgroup type selection
|
||||
* Type selection support header
|
||||
*/
|
||||
//! @{
|
||||
namespace utl {
|
||||
namespace meta{
|
||||
|
||||
/*!
|
||||
* Type selection
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! if_, if_c
|
||||
//! @{
|
||||
namespace detail {
|
||||
template <bool If, typename...>
|
||||
struct if_c_ {
|
||||
using type = nil_; //!< avoid ill formed result
|
||||
};
|
||||
template<typename Then>
|
||||
struct if_c_<true, Then> {
|
||||
using type = Then;
|
||||
};
|
||||
template<typename Then, typename Else>
|
||||
struct if_c_<true, Then, Else> {
|
||||
using type = Then;
|
||||
};
|
||||
template<typename Then, typename Else>
|
||||
struct if_c_<false, Then, Else> {
|
||||
using type = Else;
|
||||
};
|
||||
}
|
||||
//! Select one type or another depending on a compile-time Boolean.
|
||||
template <bool B, typename... Args>
|
||||
using if_c = eval<detail::if_c_<B, Args...>>;
|
||||
|
||||
//! Select one type or another depending on a compile-time Boolean type
|
||||
template <typename If, typename... Args>
|
||||
using if_ = if_c<If::type::value, Args...>;
|
||||
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* Named type selectors
|
||||
*/
|
||||
//! @{
|
||||
|
||||
//! Select the first type of a type sequence
|
||||
template <typename T1, typename ...> using first_of = T1;
|
||||
|
||||
//! Select the second type of a type sequence
|
||||
template <typename T1, typename T2, typename ...> using second_of = T2;
|
||||
|
||||
//! @}
|
||||
}}
|
||||
|
||||
//! @}
|
||||
|
||||
#endif /* __utl_meta_selection_h__ */
|
||||
@ -1,59 +1,24 @@
|
||||
/*!
|
||||
* \file sfinae.h
|
||||
* \brief Template meta-programming SFINAE helpers
|
||||
*
|
||||
* Copyright (C) 2018-2019 Christos Choutouridis
|
||||
*
|
||||
* 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 __utl_meta_sfinae_h__
|
||||
#define __utl_meta_sfinae_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/meta/basic.h>
|
||||
#include <type_traits>
|
||||
|
||||
/*!
|
||||
* \ingroup meta
|
||||
* \defgroup sfinae
|
||||
* \defgroup sfinae SFINAE
|
||||
* conditional use support header.
|
||||
*/
|
||||
//! @{
|
||||
namespace utl {
|
||||
namespace meta {
|
||||
|
||||
//! Tool to enable a partial specialization only if a boolean condition is true.
|
||||
//! @{
|
||||
namespace detail {
|
||||
// template <typename... T>
|
||||
// struct dev_null { using type = dev_null; }; //< Same as typelist
|
||||
|
||||
template <bool If> struct when_ { };
|
||||
template <> struct when_<true> { using type = void; };
|
||||
}
|
||||
//! Well formed only if \p If is true
|
||||
template <bool If>
|
||||
using when = eval< detail::when_<If> >;
|
||||
|
||||
// //! Well formed only if all of \p Ifs are \c true
|
||||
// template <bool ...Ifs>
|
||||
// using when_all = detail::dev_null<
|
||||
// when<Ifs>...
|
||||
// >;
|
||||
|
||||
//! @}
|
||||
|
||||
//! enable_if
|
||||
//! \name enable_if from STL
|
||||
//! @{
|
||||
|
||||
//! enable_if, imported from stl
|
||||
@ -64,6 +29,52 @@ namespace meta {
|
||||
|
||||
//! @}
|
||||
|
||||
//! \name when implementation
|
||||
//! @{
|
||||
namespace details {
|
||||
// template <typename... T>
|
||||
// struct dev_null { using type = dev_null; }; //< Same as typelist
|
||||
|
||||
template <bool If> struct when_ { };
|
||||
template <> struct when_<true> { using type = void; };
|
||||
}
|
||||
//! Tool to enable a partial specialization only if a boolean condition is true.
|
||||
//! Well formed only if \p If is true
|
||||
template <bool If>
|
||||
using when = eval< details::when_<If> >;
|
||||
|
||||
// //! Well formed only if all of \p Ifs are \c true
|
||||
// template <bool ...Ifs>
|
||||
// using when_all = details::dev_null<
|
||||
// when<Ifs>...
|
||||
// >;
|
||||
|
||||
//! @}
|
||||
|
||||
//! If same type resolves to _Ret, else SFINAE
|
||||
template <typename _T1, typename _T2, typename _Ret =_T1>
|
||||
using use_if_same_t = enable_if_t<
|
||||
same<_T1, _T2>::value, _Ret
|
||||
>;
|
||||
//! If not same type resolves to _Ret, else SFINAE
|
||||
template <typename _T1, typename _T2, typename _Ret =_T1>
|
||||
using use_if_not_same_t = enable_if_t<
|
||||
!same<_T1, _T2>::value, _Ret
|
||||
>;
|
||||
//! If any type (_T1 or _T2) type resolves to _Ret, else to SFINAE
|
||||
template <typename T1, typename... Ts>
|
||||
using use_if_any_t = enable_if_t<
|
||||
or_<T1, Ts...>::value, T1
|
||||
>;
|
||||
|
||||
//! If both type (_T1 and _T2) type resolves to _Ret, else to SFINAE
|
||||
template <typename T1, typename... Ts>
|
||||
using use_if_all_t = enable_if_t<
|
||||
and_<T1, Ts...>::value, T1
|
||||
>;
|
||||
|
||||
|
||||
|
||||
}}
|
||||
|
||||
//! @}
|
||||
|
||||
@ -1,33 +1,42 @@
|
||||
/*!
|
||||
* \file typelist.h
|
||||
* \brief A template parameter "container"
|
||||
*
|
||||
* Copyright (C) 2018-2019 Christos Choutouridis
|
||||
*
|
||||
* 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 __utl_meta_typelist_h__
|
||||
#define __utl_meta_typelist_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/meta/integral.h>
|
||||
#include <utl/meta/basic.h>
|
||||
#include <utl/meta/detection.h>
|
||||
#include <utl/meta/invoke.h>
|
||||
#include <utl/meta/sfinae.h>
|
||||
/*!
|
||||
* \ingroup meta
|
||||
* \defgroup typelist
|
||||
* \defgroup typelist typelist
|
||||
*
|
||||
* The idea came from MPL's sequence concept [1] and from N4115 [2].
|
||||
* In addition to N4115's name "packer" we just prefer a name which is an object, not a subject.
|
||||
* This way the name gives the feeling of a container and smells like Python.\n
|
||||
*
|
||||
* In addition to tuple we lack members, so typelist could serve as an empty base class,
|
||||
* and an object of the ultimate type could always be instantiated
|
||||
* (even if the parameter typelist contains void or some type that lacks
|
||||
* a default constructor).\n
|
||||
*
|
||||
* \code{.cpp}
|
||||
* using l1 = typelist<int, void*, double, void>;
|
||||
* l1 a {};
|
||||
* \endcode
|
||||
*
|
||||
* boost::hana [3] suggests a more powerful scheme were type invariant structures can be used
|
||||
* for metaprograming also. This lib does not need (yet) this kind of power (we afraid the
|
||||
* responsibility that comes along).\n
|
||||
* So a simple python-like list with some extra vector-like
|
||||
* element access functionalities and no iterators is good enough(for now).
|
||||
*
|
||||
* [1]: https://www.boost.org/doc/ \n
|
||||
* [2]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4115.html \n
|
||||
* [3]: https://github.com/boostorg/hana \n
|
||||
*/
|
||||
//! @{
|
||||
|
||||
@ -36,30 +45,8 @@ namespace meta {
|
||||
|
||||
/*!
|
||||
* \brief
|
||||
* A class template that just holds a parameter pack.
|
||||
*
|
||||
* The idea came from MPL's sequence concept [\ref link1 1] and from N4115 [\ref link2 2].
|
||||
* In addition to N4115's name "packer" we just prefer a name which is object, not a subject.
|
||||
* This way the name gives the feeling of a container and smells like Python.
|
||||
*
|
||||
* In addition to tuple we lack members, so typelist could serve as an empty base class,
|
||||
* and an object of the ultimate type could always be instantiated
|
||||
* (even if the parameter typelist contains void or some type that lacks
|
||||
* a default constructor).
|
||||
* \example
|
||||
* \code
|
||||
* using l1 = typelist<int, void*, double, void>;
|
||||
* l1 a {};
|
||||
* \endcode
|
||||
*
|
||||
* boost::hana [\ref link3 3] suggests a more powerful scheme were type invariant structures can be used
|
||||
* for metaprograming also. This lib does not need (yet) this kind of power (we afraid the
|
||||
* responsibility that comes along). So a simple python-like list with some extra vector-like
|
||||
* element access functionalities and no iterators is good enough(for now).
|
||||
*
|
||||
* \anchor link1 [1]: https://www.boost.org/doc/
|
||||
* \anchor link2 [2]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4115.html
|
||||
* \anchor link3 [3]: https://github.com/boostorg/hana
|
||||
* The \c typelist 's implementation container
|
||||
* \tparam Ts The \a contained types
|
||||
*/
|
||||
template <typename... Ts>
|
||||
struct typelist {
|
||||
@ -102,8 +89,8 @@ namespace meta {
|
||||
public:
|
||||
/*!
|
||||
* Generate typelist<Ts..., Ts..., ...> of size \c N arguments.
|
||||
* \example
|
||||
* \code
|
||||
*
|
||||
* \code{.cpp}
|
||||
* static_assert (
|
||||
* std::is_same<typelist<int, char>::times<2>,
|
||||
* typelist<int, char, int, char>
|
||||
@ -122,8 +109,8 @@ namespace meta {
|
||||
*
|
||||
* Complexity \f$ O(1) \f$.
|
||||
*
|
||||
* \param List A typelist
|
||||
* \return The size of the typelist
|
||||
* \tparam List A typelist
|
||||
* \return The size of the typelist
|
||||
*/
|
||||
template <typename List>
|
||||
using size = size_<List::size()>;
|
||||
@ -133,8 +120,8 @@ namespace meta {
|
||||
*
|
||||
* Complexity \f$ O(1) \f$.
|
||||
*
|
||||
* \param List A typelist
|
||||
* \return Empty or not
|
||||
* \tparam List A typelist
|
||||
* \return Empty or not
|
||||
*/
|
||||
template <typename List>
|
||||
using empty = bool_<List::empty()>;
|
||||
@ -145,24 +132,24 @@ namespace meta {
|
||||
using pair = typelist<T1, T2>;
|
||||
|
||||
|
||||
//! repeat
|
||||
//! \name repeat
|
||||
//! @{
|
||||
|
||||
/*!
|
||||
* A wrapper to typelist<>::times<> utility for integer argument \p N
|
||||
* A wrapper to typelist<>::times<> utility for integer argument \c N
|
||||
*/
|
||||
template <size_t N, typename ...Ts>
|
||||
using repeat_c = typename typelist<Ts...>::template times<N>;
|
||||
|
||||
/*!
|
||||
* A wrapper to typelist<>::times<> utility for integral_c argument \p N
|
||||
* A wrapper to typelist<>::times<> utility for integral_c argument \c N
|
||||
*/
|
||||
template <typename N, typename ...Ts>
|
||||
using repeat = repeat_c<N::type::value, Ts...>;
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* Apply
|
||||
* \name Apply
|
||||
* An analogous to apply() implementation for tuples. We just use
|
||||
* Our typelist<> and integer_sequence<> types.
|
||||
*/
|
||||
@ -171,12 +158,12 @@ namespace meta {
|
||||
template <typename Fn, typename Seq>
|
||||
struct apply_ { };
|
||||
|
||||
//! \p Sequence == typelist<>
|
||||
// Sequence == typelist<>
|
||||
template<typename Fn, typename ...List>
|
||||
struct apply_<Fn, typelist<List...>> {
|
||||
using type = invoke<Fn, List...>;
|
||||
};
|
||||
//! Sequence == integer_sequence<>
|
||||
// Sequence == integer_sequence<>
|
||||
template <typename Fn, typename T, T... Is>
|
||||
struct apply_<Fn, integer_sequence<T, Is...>> {
|
||||
using type = invoke<Fn, integral_<T, Is>...>;
|
||||
@ -184,14 +171,13 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* Apply the Invocable \p Fn using the types in the type \p Seq as arguments.
|
||||
* \brief
|
||||
* Apply the Invocable \c Fn using the types in the type \c Seq as arguments.
|
||||
* \note
|
||||
* This is the opposed operation of typelist<Ts...>
|
||||
* This is the opposed operation of typelist<Ts...>
|
||||
*
|
||||
* If \p Seq == typelist<> then
|
||||
* Unpack typelist and apply to \c Fn
|
||||
* It \p Seq == integer_sequence<> then
|
||||
* Unpack and use the integral_c<> of each integer
|
||||
* If \c Seq == typelist<> then unpack typelist and apply to \c Fn \n
|
||||
* It \c Seq == integer_sequence<> then unpack and use the integral_c<> of each integer
|
||||
*/
|
||||
template <typename Fn, typename Seq>
|
||||
using apply = apply_impl::apply_<Fn, Seq>;
|
||||
@ -204,7 +190,7 @@ namespace meta {
|
||||
/*
|
||||
* ========= element access ========
|
||||
*/
|
||||
//! at: random element access
|
||||
//! \name random element access
|
||||
//! @{
|
||||
namespace at_impl {
|
||||
|
||||
@ -235,7 +221,7 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* Return the \p N th element in the \c meta::typelist \p List.
|
||||
* Return the \c N th element in the \c meta::typelist \c List.
|
||||
*
|
||||
* Complexity \f$ O(logN) \f$.
|
||||
*/
|
||||
@ -245,16 +231,16 @@ namespace meta {
|
||||
>;
|
||||
|
||||
/*!
|
||||
* Return the \p N th element in the \c meta::typelist \p List.
|
||||
* Return the \c N th element in the \c meta::typelist \c List.
|
||||
*
|
||||
* Complexity \f$ O(N) \f$.
|
||||
* Complexity \f$ O(logN) \f$.
|
||||
*/
|
||||
template <typename List, typename N>
|
||||
using at = at_c<List, N::type::value>;
|
||||
//!@}
|
||||
|
||||
|
||||
//! front
|
||||
//! \name front
|
||||
//! @{
|
||||
namespace front_impl {
|
||||
template <typename L>
|
||||
@ -266,7 +252,8 @@ namespace meta {
|
||||
};
|
||||
}
|
||||
|
||||
//! Return the first element in \c meta::typelist \p List.
|
||||
//! Return the first element in \c meta::typelist \c List.
|
||||
//!
|
||||
//! Complexity \f$ O(1) \f$.
|
||||
template <typename List>
|
||||
using front = eval<
|
||||
@ -274,11 +261,11 @@ namespace meta {
|
||||
>;
|
||||
//! @}
|
||||
|
||||
//! back
|
||||
//! \name back
|
||||
//! @{
|
||||
namespace back_impl {
|
||||
template <typename List>
|
||||
struct back_ { };
|
||||
struct back_ { using type = nil_; };
|
||||
|
||||
template <typename Head, typename... Tail>
|
||||
struct back_<typelist<Head, Tail...>> {
|
||||
@ -288,7 +275,8 @@ namespace meta {
|
||||
};
|
||||
}
|
||||
|
||||
//! Return the last element in \c meta::typelist \p List.
|
||||
//! Return the last element in \c meta::typelist \c List.
|
||||
//!
|
||||
//! Complexity \f$ O(N) \f$.
|
||||
template <typename List>
|
||||
using back = eval<
|
||||
@ -299,7 +287,7 @@ namespace meta {
|
||||
* ========= typelist operations =========
|
||||
*/
|
||||
|
||||
//! Concatenation
|
||||
//! \name Concatenation
|
||||
//! @{
|
||||
namespace cat_impl {
|
||||
template <typename... Lists>
|
||||
@ -329,8 +317,9 @@ namespace meta {
|
||||
/*!
|
||||
* Transformation that concatenates several lists into a single typelist.
|
||||
* The parameters must all be instantiations of \c meta::typelist.
|
||||
*
|
||||
* Complexity: \f$ O(N) \f$
|
||||
* where \f$ N \f$ is the number of lists passed to the algorithm.
|
||||
* where \f$ N \f$ is the number of lists passed to the algorithm.
|
||||
*/
|
||||
template <typename... Lists>
|
||||
using cat = eval<
|
||||
@ -339,7 +328,7 @@ namespace meta {
|
||||
//! @}
|
||||
|
||||
|
||||
//! fold<List, V, Fn>, rev_fold<List, V, Fn>
|
||||
//! \name Fold
|
||||
//! @{
|
||||
namespace fold_impl {
|
||||
// fold<<T1, T2, T3>, V, F> == F<F<F<V, T1>, T2>, T3>
|
||||
@ -368,16 +357,17 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* transform the \p List to a new one by doing a left fold using binary Invocable \p Fn
|
||||
* and initial value \p V
|
||||
* transform the \c List to a new one by doing a left fold using binary Invocable \c Fn
|
||||
* and initial value \c V
|
||||
*
|
||||
* Complexity \f$ O(N) \f$
|
||||
* \example
|
||||
* \code{.cpp}
|
||||
* fold<typelist<T1, T2, T3>, V, F> == F<F<F<V, T1>, T2>, T3>
|
||||
* \example
|
||||
* fold<typelist<>, V, F> == V
|
||||
* \param List The list to fold
|
||||
* \param V The initial item feeded to Fn
|
||||
* \param Fn The binary Invocable
|
||||
* \endcode
|
||||
* \tparam List The list to fold
|
||||
* \tparam V The initial item feeded to Fn
|
||||
* \tparam Fn The binary Invocable
|
||||
*/
|
||||
template <typename List, typename V, typename Fn>
|
||||
using fold = eval<fold_impl::fold_<List, V, Fn>>;
|
||||
@ -421,16 +411,17 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* transform the \p List to a new one by doing a right fold using binary Invocable \p Fn
|
||||
* and initial value \p V
|
||||
* transform the \c List to a new one by doing a right fold using binary Invocable \c Fn
|
||||
* and initial value \c V
|
||||
*
|
||||
* Complexity \f$ O(N) \f$
|
||||
* \example
|
||||
* \code{.cpp}
|
||||
* rev_fold<typelist<T1, T2, T3>, V, F> == F<T1, F<T2, F<T3, V>>>
|
||||
* \example
|
||||
* rev_fold<typelist<>, V, F> == V
|
||||
* \param List The list to fold
|
||||
* \param V The initial item fed to Fn
|
||||
* \param Fn The binary Invocable
|
||||
* \endcode
|
||||
* \tparam List The list to fold
|
||||
* \tparam V The initial item fed to Fn
|
||||
* \tparam Fn The binary Invocable
|
||||
*/
|
||||
template <typename List, typename V, typename Fn>
|
||||
using rev_fold = eval<
|
||||
@ -439,7 +430,8 @@ namespace meta {
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* Return a new \c typelist by adding the elements \p Ts to the front of \p List.
|
||||
* Return a new \c typelist by adding the elements \c Ts to the front of \c List.
|
||||
*
|
||||
* Complexity \f$ O(1) \f$
|
||||
*/
|
||||
template <typename List, typename... Ts>
|
||||
@ -450,7 +442,8 @@ namespace meta {
|
||||
>;
|
||||
|
||||
/*!
|
||||
* Return a new \c typelist by adding the elements \p Ts to the back of \p List.
|
||||
* Return a new \c typelist by adding the elements \c Ts to the back of \c List.
|
||||
*
|
||||
* Complexity \f$ O(1) \f$
|
||||
*/
|
||||
template <typename List, typename... Ts>
|
||||
@ -460,7 +453,7 @@ namespace meta {
|
||||
>
|
||||
>;
|
||||
|
||||
//! reverse
|
||||
//! \name reverse
|
||||
//! @{
|
||||
namespace reverse_impl {
|
||||
template <typename List, typename V = typelist<>>
|
||||
@ -470,7 +463,8 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* Return a new \c typelist by reversing the elements in the list \p List.
|
||||
* Return a new \c typelist by reversing the elements in the list \c List.
|
||||
*
|
||||
* Complexity \f$ O(N) \f$
|
||||
*/
|
||||
template <typename List>
|
||||
@ -479,7 +473,7 @@ namespace meta {
|
||||
>;
|
||||
//! @}
|
||||
|
||||
//! pop_front
|
||||
//! \name pop_front
|
||||
//! @{
|
||||
namespace pop_front_impl {
|
||||
template <typename List>
|
||||
@ -493,7 +487,8 @@ namespace meta {
|
||||
|
||||
/*!
|
||||
* Return a new \c typelist by removing the first element from the
|
||||
* front of \p List.
|
||||
* front of \c List.
|
||||
*
|
||||
* Complexity \f$ O(1) \f$
|
||||
*/
|
||||
template <typename List>
|
||||
@ -502,7 +497,7 @@ namespace meta {
|
||||
>;
|
||||
//! @}
|
||||
|
||||
//! pop_back
|
||||
//! \name pop_back
|
||||
//! @{
|
||||
namespace pop_back_impl {
|
||||
template <typename List>
|
||||
@ -514,7 +509,8 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* Return a new \c typelist by removing the last element from the \p List.
|
||||
* Return a new \c typelist by removing the last element from the \c List.
|
||||
*
|
||||
* Complexity \f$ O(N) \f$.
|
||||
* \note
|
||||
* This operation, in addition from other push/pop operations, is
|
||||
@ -526,7 +522,7 @@ namespace meta {
|
||||
>;
|
||||
//! @}
|
||||
|
||||
//! Transform
|
||||
//! \name Transform
|
||||
//! @{
|
||||
namespace transform_impl {
|
||||
template <typename, typename = void>
|
||||
@ -554,8 +550,7 @@ namespace meta {
|
||||
* and return the resulting typelist
|
||||
*
|
||||
* Complexity \f$ O(N) \f$.
|
||||
* \example
|
||||
* \code
|
||||
* \code{.cpp}
|
||||
* using l1 = typelist<char, int, ...>;
|
||||
* using l2 = typelist<void, double, ...>;
|
||||
* using r1 = transform<l1, F1>; // F1, unary invocable
|
||||
@ -568,7 +563,7 @@ namespace meta {
|
||||
>;
|
||||
//! @}
|
||||
|
||||
//! Transform lazy
|
||||
//! \name Transform lazy
|
||||
//! @{
|
||||
namespace transform_lazy_impl {
|
||||
template <typename, typename = void>
|
||||
@ -600,8 +595,7 @@ namespace meta {
|
||||
*
|
||||
* Complexity \f$ O(N) \f$
|
||||
*
|
||||
* \example
|
||||
* \code
|
||||
* \code{.cpp}
|
||||
* using l1 = typelist<char, int, ...>;
|
||||
* using l2 = typelist<void, void, ...>;
|
||||
* using r1 = transform<l1, F1>; // F1, unary invocable
|
||||
@ -615,7 +609,7 @@ namespace meta {
|
||||
//! @}
|
||||
|
||||
|
||||
//! find_if, find
|
||||
//! \name find_if, find
|
||||
//! @{
|
||||
namespace find_if_impl {
|
||||
template <typename, typename, index_t>
|
||||
@ -641,13 +635,13 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* Search for the first \c Item on the \p List for which the predicate \p Pred
|
||||
* Search for the first \c Item on the \c List for which the predicate \c Pred
|
||||
* returns true_ when `eval<invoke<Pred, Item>>`
|
||||
*
|
||||
* Complexity \f$ O(N) \f$
|
||||
*
|
||||
* \param List A typelist
|
||||
* \param Pred A Unary invocable predicate
|
||||
* \tparam List A typelist
|
||||
* \tparam Pred A Unary invocable predicate
|
||||
* \return An integral constant of index_t with the location of the first match,
|
||||
* or Npos otherwise.
|
||||
*/
|
||||
@ -657,13 +651,13 @@ namespace meta {
|
||||
>;
|
||||
|
||||
/*!
|
||||
* Search for the first occurrence of type \p T on a \p List
|
||||
* Search for the first occurrence of type \c T on a \c List
|
||||
*/
|
||||
template <typename List, typename T>
|
||||
using find = find_if<List, same_as<T>>;
|
||||
//! @}
|
||||
|
||||
//! seek_if
|
||||
//! \name seek_if
|
||||
//! @{
|
||||
namespace seek_if_impl {
|
||||
template <typename, typename, index_t>
|
||||
@ -689,14 +683,14 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* Search for the first \c Item on the \p List for which the predicate \p Pred
|
||||
* returns true_ when `eval<invoke<Pred, Item>>` and return the rest of the \p List
|
||||
* Search for the first \c Item on the \c List for which the predicate \c Pred
|
||||
* returns true_ when `eval<invoke<Pred, Item>>` and return the rest of the \c List
|
||||
* starting from that position as new typelist
|
||||
*
|
||||
* Complexity \f$ O(N) \f$
|
||||
*
|
||||
* \param List A typelist
|
||||
* \param Pred A Unary invocable predicate
|
||||
* \tparam List A typelist
|
||||
* \tparam Pred A Unary invocable predicate
|
||||
* \return An integral constant with the location of the first match, on Npos otherwise
|
||||
*/
|
||||
template <typename List, typename Pred>
|
||||
@ -704,14 +698,17 @@ namespace meta {
|
||||
seek_if_impl::seek_if_<List, Pred, 0>
|
||||
>;
|
||||
/*!
|
||||
* Search for the first \c Item on the \p List of type \p T and return the rest
|
||||
* of the \p List starting from that position as new typelist
|
||||
* Search for the first \c Item on the \c List of type \c T and return the rest
|
||||
* of the \c List starting from that position as new typelist
|
||||
*/
|
||||
template <typename List, typename T>
|
||||
using seek = seek_if <List, same_as<T>>;
|
||||
//! @}
|
||||
|
||||
//! count_if
|
||||
template <typename List, typename Pred>
|
||||
using first_if = front<seek_if<List, Pred>>;
|
||||
|
||||
//! \name count_if
|
||||
//! @{
|
||||
namespace count_if_impl {
|
||||
template <typename, typename, size_t>
|
||||
@ -740,13 +737,13 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* Count all \c Items on the \p List for which the predicate \p Pred
|
||||
* Count all \c Items on the \c List for which the predicate \c Pred
|
||||
* returns true_ when `eval<invoke<Pred, Item>>`
|
||||
*
|
||||
* Complexity \f$ O(N) \f$
|
||||
*
|
||||
* \param List A typelist
|
||||
* \param Pred A Unary invocable predicate
|
||||
* \tparam List A typelist
|
||||
* \tparam Pred A Unary invocable predicate
|
||||
* \return The total count of occurrences as an integral constant of size_t
|
||||
*/
|
||||
template <typename List, typename Pred>
|
||||
@ -755,13 +752,13 @@ namespace meta {
|
||||
>;
|
||||
|
||||
/*!
|
||||
* Count all occurrences of type \p T int \p List
|
||||
* Count all occurrences of type \c T int \c List
|
||||
*/
|
||||
template <typename List, typename T>
|
||||
using count = count_if<List, same_as<T>>;
|
||||
//! @}
|
||||
|
||||
//! filter
|
||||
//! \name filter
|
||||
//! @{
|
||||
namespace filter_impl {
|
||||
template <typename, typename, typename>
|
||||
@ -786,13 +783,13 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* Return a new typelist with elements, the elements of \p List that satisfy the
|
||||
* invocable \p Pred such that `eval<invoke<Pred, Item>>` is \c true_
|
||||
* Return a new typelist with elements, the elements of \c List that satisfy the
|
||||
* invocable \c Pred such that `eval<invoke<Pred, Item>>` is \c true_
|
||||
*
|
||||
* Complexity \f$ O(N) \f$
|
||||
*
|
||||
* \param List The input typelist
|
||||
* \param Pred A unary invocable predicate
|
||||
* \tparam List The input typelist
|
||||
* \tparam Pred A unary invocable predicate
|
||||
*/
|
||||
template <typename List, typename Pred>
|
||||
using filter = eval<
|
||||
@ -800,7 +797,7 @@ namespace meta {
|
||||
>;
|
||||
//! @}
|
||||
|
||||
//! replace
|
||||
//! \name replace
|
||||
//! @{
|
||||
namespace replace_if_impl {
|
||||
template <typename, typename, typename, typename>
|
||||
@ -825,14 +822,14 @@ namespace meta {
|
||||
}
|
||||
|
||||
/*!
|
||||
* Return a new typelist where all the instances for which the invocation of\p Pred
|
||||
* returns \c true_, are replaced with \p T
|
||||
* Return a new typelist where all the instances for which the invocation of\c Pred
|
||||
* returns \c true_, are replaced with \c T
|
||||
*
|
||||
* Complexity \f$ O(N) \f$
|
||||
*
|
||||
* \param List The input typelist
|
||||
* \param Pred A unary invocable predicate
|
||||
* \param T The new type to replace the item of the \p List, when eval<invoke<Pred, Item>>
|
||||
* \tparam List The input typelist
|
||||
* \tparam Pred A unary invocable predicate
|
||||
* \tparam T The new type to replace the item of the \c List, when eval<invoke<Pred, Item>>
|
||||
* returns \c true_
|
||||
*/
|
||||
template<typename List, typename Pred, typename T>
|
||||
@ -840,8 +837,8 @@ namespace meta {
|
||||
replace_if_impl::replace_if_<List, Pred, T, typelist<>>
|
||||
>;
|
||||
|
||||
//! Alias wrapper that returns a new \c typelist where all instances of type \p T have
|
||||
//! been replaced with \p U.
|
||||
//! Alias wrapper that returns a new \c typelist where all instances of type \c T have
|
||||
//! been replaced with \c U.
|
||||
template <typename List, typename T, typename U>
|
||||
using replace = eval <
|
||||
replace_if <List, same_as<T>, U>
|
||||
@ -849,8 +846,8 @@ namespace meta {
|
||||
//! @}
|
||||
|
||||
|
||||
//! Returns \c true_ if \p Pred returns \c true_ for all the elements in the \p List or if the
|
||||
//! \p List is empty and \c false_ otherwise.
|
||||
//! Returns \c true_ if \c Pred returns \c true_ for all the elements in the \c List or if the
|
||||
//! \c List is empty and \c false_ otherwise.
|
||||
template <typename List, typename Pred>
|
||||
using all_of = if_ <
|
||||
empty <List>,
|
||||
@ -860,15 +857,15 @@ namespace meta {
|
||||
>
|
||||
>;
|
||||
|
||||
//! Returns \c true_ if \p Pred returns \c true_ for any of the elements in the \p List
|
||||
//! Returns \c true_ if \c Pred returns \c true_ for any of the elements in the \c List
|
||||
//! and \c false_ otherwise.
|
||||
template <typename List, typename Pred>
|
||||
using any_of = not_<
|
||||
empty<filter <List, Pred>>
|
||||
>;
|
||||
|
||||
//! Returns \c true_ if \p Pred returns \c false_ for all the elements in the \p List
|
||||
//! or if the \p List is empty and \c false otherwise.
|
||||
//! Returns \c true_ if \c Pred returns \c false_ for all the elements in the \c List
|
||||
//! or if the \c List is empty and \c false otherwise.
|
||||
template <typename List, typename Pred>
|
||||
using none_of = empty<
|
||||
filter <List, Pred>
|
||||
|
||||
@ -1,62 +0,0 @@
|
||||
/*!
|
||||
* \file useif.h
|
||||
* \brief Template meta-programming SFINAE helpers
|
||||
*
|
||||
* Copyright (C) 2018 Christos Choutouridis
|
||||
*
|
||||
* 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 __utl_meta_useif_h__
|
||||
#define __utl_meta_useif_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
#include <utl/meta/operations.h>
|
||||
#include <utl/meta/sfinae.h>
|
||||
|
||||
/*!
|
||||
* \ingroup meta
|
||||
* \defgroup sfinae
|
||||
* conditional use support header. This is a SFINAE wrapper
|
||||
*/
|
||||
//! @{
|
||||
namespace utl {
|
||||
namespace meta {
|
||||
|
||||
//! If same type resolves to _Ret, else SFINAE
|
||||
template <typename _T1, typename _T2, typename _Ret =_T1>
|
||||
using use_if_same_t = enable_if_t<
|
||||
same_<_T1, _T2>::value, _Ret
|
||||
>;
|
||||
//! If not same type resolves to _Ret, else SFINAE
|
||||
template <typename _T1, typename _T2, typename _Ret =_T1>
|
||||
using use_if_not_same_t = enable_if_t<
|
||||
!same_<_T1, _T2>::value, _Ret
|
||||
>;
|
||||
//! If any type (_T1 or _T2) type resolves to _Ret, else to SFINAE
|
||||
template <typename T1, typename... Ts>
|
||||
using use_if_any_t = enable_if_t<
|
||||
or_<T1, Ts...>::value, T1
|
||||
>;
|
||||
|
||||
//! If both type (_T1 and _T2) type resolves to _Ret, else to SFINAE
|
||||
template <typename T1, typename... Ts>
|
||||
using use_if_all_t = enable_if_t<
|
||||
and_<T1, Ts...>::value, T1
|
||||
>;
|
||||
|
||||
}}
|
||||
|
||||
//! @}
|
||||
|
||||
#endif /* __utl_meta_useif_h__ */
|
||||
@ -1,21 +1,6 @@
|
||||
/*!
|
||||
* \file utl/utility/invoke.h
|
||||
* \brief invoke() and invoke traits implementation
|
||||
*
|
||||
* Copyright (C) 2018-2019 Christos Choutouridis
|
||||
*
|
||||
* 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 detail.
|
||||
*
|
||||
* 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 __utl_utility_invoke_h__
|
||||
#define __utl_utility_invoke_h__
|
||||
@ -27,13 +12,16 @@
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
|
||||
//! \defgroup utility Utility
|
||||
|
||||
/*!
|
||||
* \ingroup utility
|
||||
* \defgroup invoke
|
||||
* \defgroup util_invoke Invoke
|
||||
*/
|
||||
//! @{
|
||||
namespace utl {
|
||||
|
||||
//#if !defined __cpp_lib_is_invocable
|
||||
namespace detail {
|
||||
|
||||
template <class T>
|
||||
@ -116,16 +104,41 @@ namespace utl {
|
||||
|
||||
} // namespace detail
|
||||
|
||||
//! Invoke a callable object (for C++14)
|
||||
template<typename _Callable, typename... _Args>
|
||||
inline decltype(auto) invoke(_Callable&& fn, _Args&&... args) {
|
||||
//! Invoke the Callable object \c fn with the parameters args.
|
||||
//! As by INVOKE(std::forward<F>(f), std::forward<Args>(args)...).
|
||||
//!
|
||||
//! \note
|
||||
//! This implementation fills the lack of an invoke() utility for builds
|
||||
//! pre-c++17.
|
||||
//!
|
||||
//! \param fn Callable object to be invoked
|
||||
//! \param args Arguments to pass to \c fn
|
||||
//! \return The return of the Callable underling functionality
|
||||
//!
|
||||
template<typename Callable, typename... Args>
|
||||
inline decltype(auto) invoke(Callable&& fn, Args&&... args) {
|
||||
return detail::invoke_impl_(
|
||||
std::forward<_Callable>(fn), std::forward<_Args>(args)...
|
||||
std::forward<Callable>(fn), std::forward<Args>(args)...
|
||||
);
|
||||
}
|
||||
//! @}
|
||||
|
||||
//! std::is_invocable trait for C++11
|
||||
//!
|
||||
//! \brief
|
||||
//! Determines whether \c F can be invoked with the arguments \c Args....
|
||||
//!
|
||||
//! Formally, determines whether invoke(declval<Fn>(), declval<ArgTypes>()...)
|
||||
//! is well formed when treated as an unevaluated operand,
|
||||
//! where invoke is \c Callable.
|
||||
//!
|
||||
//! \note
|
||||
//! This implementation fills the lack of an invoke() utility for builds
|
||||
//! pre-c++17.
|
||||
//!
|
||||
//! \tparam F The candidate type to check if its invocable
|
||||
//! \tparam Args The arguments for the call
|
||||
//! \return If \c F is invocable
|
||||
//! \arg true Is invocable
|
||||
//! \arg false Is not invocable
|
||||
template <typename F, typename... Args>
|
||||
struct is_invocable :
|
||||
std::is_constructible<
|
||||
@ -133,7 +146,19 @@ namespace utl {
|
||||
std::reference_wrapper<typename std::remove_reference<F>::type>
|
||||
> { };
|
||||
|
||||
//! std::is_invocable_r trait for C++11
|
||||
//! \brief
|
||||
//! Determines whether \c F can be invoked with the arguments \c Args...
|
||||
//! to yield a result that is convertible to \c R.
|
||||
//!
|
||||
//! Formally, determines whether invoke(declval<Fn>(), declval<ArgTypes>()...)
|
||||
//! is well formed when treated as an unevaluated operand, where invoke is \c Callable.
|
||||
//!
|
||||
//! \tparam R The return type of invocable functionality
|
||||
//! \tparam F The candidate type to check if its invocable
|
||||
//! \tparam Args The arguments to pass to \c F
|
||||
//! \return If \c F is invocable
|
||||
//! \arg true Is invocable
|
||||
//! \arg false Is not invocable
|
||||
template <typename R, typename F, typename... Args>
|
||||
struct is_invocable_r :
|
||||
std::is_constructible<
|
||||
@ -146,38 +171,55 @@ namespace utl {
|
||||
*/
|
||||
//! @{
|
||||
namespace detail {
|
||||
template<typename _Callable, typename... _Args>
|
||||
template<typename Callable, typename... Args>
|
||||
struct try_invoke {
|
||||
using type = decltype (
|
||||
detail::invoke_impl_(std::declval<_Callable&&>(), std::declval<_Args&&>()...)
|
||||
detail::invoke_impl_(std::declval<Callable&&>(), std::declval<Args&&>()...)
|
||||
);
|
||||
};
|
||||
|
||||
template<bool B, typename _Callable, typename... _Args>
|
||||
template<bool B, typename Callable, typename... Args>
|
||||
struct invoke_result_ {
|
||||
using type = meta::nil_;
|
||||
};
|
||||
|
||||
template <typename _Callable, typename... _Args>
|
||||
struct invoke_result_ <true, _Callable, _Args...> {
|
||||
template <typename Callable, typename... Args>
|
||||
struct invoke_result_ <true, Callable, Args...> {
|
||||
using type = meta::invoke_t<
|
||||
meta::quote<try_invoke>, _Callable, _Args...
|
||||
meta::quote<try_invoke>, Callable, Args...
|
||||
>;
|
||||
};
|
||||
}
|
||||
//! invoke_result (for C++14)
|
||||
template <typename _Callable, typename... _Args>
|
||||
//! trait that deduces the return type of an INVOKE expression at compile time.
|
||||
//!
|
||||
//! \tparam Callable The candidate type to check if its invocable
|
||||
//! \tparam Args The arguments to pass to \c F
|
||||
//!
|
||||
//! \b member \n
|
||||
//! \::type The return type of the \c Callable type if invoked with the arguments Args....
|
||||
template <typename Callable, typename... Args>
|
||||
using invoke_result = detail::invoke_result_<
|
||||
is_invocable<_Callable, _Args...>::value,
|
||||
_Callable,
|
||||
_Args...
|
||||
is_invocable<Callable, Args...>::value,
|
||||
Callable,
|
||||
Args...
|
||||
>;
|
||||
|
||||
//! invoke_result_t (for C++14)
|
||||
template<typename _Callable, typename... _Args>
|
||||
//! trait that deduces the return type of an INVOKE expression at compile time.
|
||||
//!
|
||||
//! \tparam Callable The candidate type to check if its invocable
|
||||
//! \tparam Args The arguments to pass to \c F
|
||||
//!
|
||||
//! \return The type of the \c Callable type if invoked with the arguments Args....
|
||||
template<typename Callable, typename... Args>
|
||||
using invoke_result_t = meta::eval <
|
||||
invoke_result<_Callable, _Args...>
|
||||
invoke_result<Callable, Args...>
|
||||
>;
|
||||
//! @}
|
||||
|
||||
//#else
|
||||
//using is_invocable = std::is_invocable;
|
||||
//
|
||||
//#endif
|
||||
}
|
||||
|
||||
//! @}
|
||||
|
||||
106
include/utl/utility/shared.h
Normal file
106
include/utl/utility/shared.h
Normal file
@ -0,0 +1,106 @@
|
||||
/*!
|
||||
* \file utility/shared.h
|
||||
* \brief
|
||||
* A CRTP base class to provide acquire/release functionality for shared resources
|
||||
* without handle pointers.
|
||||
*
|
||||
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*/
|
||||
|
||||
#ifndef utl_utility_shared_h__
|
||||
#define utl_utility_shared_h__
|
||||
|
||||
#include <utl/core/impl.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace utl {
|
||||
|
||||
/*!
|
||||
* A CRTP base class to provide acquire/release functionality for shared resources
|
||||
* without handle pointers.
|
||||
*
|
||||
* \example
|
||||
* \code
|
||||
* class GPIOClock : public shared<GPIOClock> {
|
||||
* friend shared<GPIOClock>;
|
||||
* void acquire_impl() { // HAL enable gpio clock }
|
||||
* void release_impl() { // HAL disable gpio clock }
|
||||
* };
|
||||
* GPIOClock clk;
|
||||
* class Pin {
|
||||
* Pin() {
|
||||
* clk.acquire();
|
||||
* // init pin
|
||||
* }
|
||||
* ~Pin() {
|
||||
* // de-init pin
|
||||
* clk.release();
|
||||
* }
|
||||
* };
|
||||
* \endcode
|
||||
*
|
||||
* \tparam Impl_t The derived class type
|
||||
*/
|
||||
template <typename Impl_t>
|
||||
class shared {
|
||||
_CRTP_IMPL(Impl_t);
|
||||
int count {}; //!< acquisition counter
|
||||
|
||||
protected:
|
||||
shared() noexcept = default;
|
||||
shared(const shared&) = delete; //!< No copies
|
||||
shared operator=(const shared&) = delete; //!< No copies
|
||||
|
||||
public:
|
||||
/*!
|
||||
* Acquires the recourse. If it is the first call to acquire the resource we actually acquire it.
|
||||
* Otherwise we just keep track of how many acquisition have made.
|
||||
*
|
||||
* \tparam Ts The types of possible arguments
|
||||
* \param args Possible arguments to pass to acquire_impl() of derived class
|
||||
*/
|
||||
template <typename ...Ts>
|
||||
void acquire (Ts&&... args) {
|
||||
if (!count++) impl().acquire_impl(std::forward<Ts>(args)...);
|
||||
}
|
||||
/*!
|
||||
* Release the recourse. On every call we decrease the count of acquisitions. If we reach zero
|
||||
* we actually release the resource.
|
||||
*
|
||||
* \tparam Ts The types of possible arguments
|
||||
* \param args Possible arguments to pass to release_impl() of derived class
|
||||
*/
|
||||
template <typename ...Ts>
|
||||
void release (Ts&&... args) noexcept {
|
||||
if (--count <= 0) {
|
||||
impl().release_impl(std::forward<Ts>(args)...);
|
||||
count =0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace utl;
|
||||
#endif /* utl_utility_shared_h__ */
|
||||
383
test/Makefile
383
test/Makefile
@ -1,165 +1,218 @@
|
||||
#
|
||||
# Makefile for utl Unit test
|
||||
#
|
||||
# Copyright (C) 2019-2020 Christos Choutouridis <christos@choutouridis.net>
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
|
||||
# ========== Project settings ==========
|
||||
# Excecutable's name
|
||||
TARGET := utlTest
|
||||
# Source directories list(space seperated). Full or relative path
|
||||
SRC_DIR_LIST := tests gtest
|
||||
# Include directories list(space seperated). Full or relative path
|
||||
INC_DIR_LIST := ../include gtest
|
||||
# Exclude files list(space seperated). Filenames only.
|
||||
# EXC_FILE_LIST := bad.cpp old.cpp
|
||||
|
||||
# Build directories
|
||||
BUILD_DIR := bin
|
||||
OBJ_DIR := $(BUILD_DIR)/obj
|
||||
DEP_DIR := $(BUILD_DIR)/.dep
|
||||
|
||||
# ========== Compiler settings ==========
|
||||
CLANGXX := clang++
|
||||
GCCXX := g++
|
||||
CSIZE := size
|
||||
# Compiler flags for debug and release
|
||||
DEB_CFLAGS := -DDEBUG -g3 -Wall -Wextra
|
||||
REL_CFLAGS := -Wall -Wextra -O2
|
||||
# Pre-defines
|
||||
# PRE_DEFS := MYCAB=1729 SUPER_MODE
|
||||
|
||||
# ========== Linker settings ==========
|
||||
# Linker flags
|
||||
LDFLAGS := -pthread
|
||||
# Map output file
|
||||
MAP_FILE := output.map
|
||||
MAP_FLAG := -Xlinker -Map=$(BUILD_DIR)/$(MAP_FILE)
|
||||
|
||||
# ========== Default settings ==========
|
||||
# compiler and compiler flagfs
|
||||
CFLAGS := $(DEB_CFLAGS)
|
||||
CXX := $(GCCXX)
|
||||
|
||||
#
|
||||
# =========== Main body and Patterns ===========
|
||||
#
|
||||
INC := $(foreach dir,$(INC_DIR_LIST),-I$(dir))
|
||||
DEF := $(foreach def,$(PRE_DEFS),-D$(def))
|
||||
EXC := $(foreach fil,$(EXC_FILE_LIST), \
|
||||
$(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/$(fil))) \
|
||||
)
|
||||
# source files, object and dependencies list
|
||||
# recursive search into current and source directories
|
||||
SRC := $(wildcard *.cpp)
|
||||
SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/*.cpp))
|
||||
SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/**/*.cpp))
|
||||
SRC := $(abspath $(filter-out $(EXC),$(SRC)))
|
||||
|
||||
OBJ := $(foreach file,$(SRC:%.cpp=%.o),$(OBJ_DIR)$(file))
|
||||
DEP := $(foreach file,$(SRC:%.cpp=%.d),$(DEP_DIR)$(file))
|
||||
|
||||
|
||||
# Make Dependencies pattern.
|
||||
# This little trick enables recompilation only when dependencies change
|
||||
# and it does so for changes both in source AND header files ;)
|
||||
#
|
||||
# It is based on Tom Tromey's method.
|
||||
#
|
||||
# Invoke cpp to create makefile rules with dependencies for each source file
|
||||
$(DEP_DIR)%.d: %.cpp
|
||||
@mkdir -p $(@D)
|
||||
@$(CXX) -E $(CFLAGS) $(INC) $(DEF) -MM -MT $(OBJ_DIR)$(<:.cpp=.o) -MF $@ $<
|
||||
|
||||
# objects depent on .cpp AND dependency files, which have an empty recipe
|
||||
$(OBJ_DIR)%.o: %.cpp $(DEP_DIR)%.d
|
||||
@mkdir -p $(@D)
|
||||
$(CXX) -c $(CFLAGS) $(INC) $(DEF) -o $@ $<
|
||||
|
||||
# empty recipe for dependency files. This prevents make errors
|
||||
$(DEP):
|
||||
|
||||
# now include all dependencies
|
||||
# After all they are makefile dependency rules ;)
|
||||
include $(wildcard $(DEP))
|
||||
|
||||
# main target rule
|
||||
$(BUILD_DIR)/$(TARGET): $(OBJ)
|
||||
@mkdir -p $(@D)
|
||||
@echo Linking to target: $(TARGET)
|
||||
@echo $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) '$$(OBJ)'
|
||||
@$(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) $(OBJ)
|
||||
@echo
|
||||
@echo Print size information
|
||||
@$(CSIZE) $(@D)/$(TARGET)
|
||||
@echo Done
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@echo Cleaning build directories
|
||||
@rm -rf $(OBJ_DIR)
|
||||
@rm -rf $(DEP_DIR)
|
||||
@rm -rf $(BUILD_DIR)
|
||||
|
||||
|
||||
#
|
||||
# ============ User Rules =============
|
||||
#
|
||||
|
||||
.PHONY: gcc14
|
||||
gcc14: CFLAGS += -std=c++14
|
||||
gcc14: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: gcc14_conc
|
||||
gcc14_conc: CFLAGS += -std=c++14 -fconcepts
|
||||
gcc14_conc: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: gcc17
|
||||
gcc17: CFLAGS += -std=c++17
|
||||
gcc17: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: gcc17_conc
|
||||
gcc17_conc: CFLAGS += -std=c++17 -fconcepts
|
||||
gcc17_conc: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: gcc2a
|
||||
gcc2a: CFLAGS += -std=c++2a
|
||||
gcc2a: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: clang14
|
||||
clang14: CXX := $(CLANGXX)
|
||||
clang14: CFLAGS += -std=c++14
|
||||
clang14: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: clang17
|
||||
clang17: CXX := $(CLANGXX)
|
||||
clang17: CFLAGS += -std=c++17
|
||||
clang17: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: clang2a
|
||||
clang2a: CXX := $(CLANGXX)
|
||||
clang2a: CFLAGS += -std=c++2a
|
||||
clang2a: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: debug
|
||||
debug: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: release
|
||||
release: CFLAGS := $(REL_FLAGS)
|
||||
release: clean $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: all
|
||||
all: clean release
|
||||
#
|
||||
# Makefile for utl Unit test
|
||||
#
|
||||
# Copyright (C) 2019-2020 Christos Choutouridis <christos@choutouridis.net>
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
|
||||
# ult notes:
|
||||
# ==============
|
||||
# This makefile provides support for unix-like local builds and
|
||||
# docker based builds for gcc and clang.
|
||||
# ** msvc is not currently supported.
|
||||
#
|
||||
# Use cases:
|
||||
# 1) build localy using **local rules** in terminal.
|
||||
# example:
|
||||
# make -j4 MK_ARG="-std=c++14 -fconcepts" build-gcc
|
||||
#
|
||||
# 2) build localy using **docker based rules** via terminal or script
|
||||
# in order to compile for all compilers/versions/dialects.
|
||||
# example in bash:
|
||||
# #!/bin/bash
|
||||
# for im in gcc:8 gcc:9 gcc:10; do
|
||||
# for dial in -std=c++14 -std=c++17; do
|
||||
# make IMAGE="$im" MK_ARG="$dial" dock-gcc
|
||||
# done
|
||||
# done
|
||||
#
|
||||
# 3) build inside a docker instance using **local rules** in order to
|
||||
# build and test using a CD/CI system.
|
||||
# example in a yml file:
|
||||
# image: gcc:8
|
||||
# build:
|
||||
# stage: build
|
||||
# - make MK_ARG="-std=c++14" build-gcc
|
||||
# - make MK_ARG="-std=c++14 -fconcepts" build-gcc
|
||||
# - make MK_ARG="-std=c++17" build-gcc
|
||||
# - make MK_ARG="-std=c++17 -fconcepts" build-gcc
|
||||
# - make MK_ARG="-std=c++2a" build-gcc
|
||||
# ... etc
|
||||
# test:
|
||||
# stage: test
|
||||
# - bin/utlTest
|
||||
# ... etc
|
||||
|
||||
|
||||
# ============== Project settings ==============
|
||||
# Project's name
|
||||
PROJECT := utl
|
||||
# Excecutable's name
|
||||
TARGET := utlTest
|
||||
# Source directories list(space seperated).
|
||||
# Relative path, under current directory only
|
||||
SRC_DIR_LIST := tests gtest
|
||||
# Include directories list(space seperated).
|
||||
# Relative path
|
||||
INC_DIR_LIST := ../include gtest
|
||||
# Exclude files list(space seperated). Filenames only.
|
||||
# EXC_FILE_LIST := bad.cpp old.cpp
|
||||
|
||||
# Build directories
|
||||
BUILD_DIR := bin
|
||||
OBJ_DIR := $(BUILD_DIR)/obj
|
||||
DEP_DIR := $(BUILD_DIR)/.dep
|
||||
|
||||
# ============== Compiler settings ==============
|
||||
CLANGXX := clang++
|
||||
GCCXX := g++
|
||||
CSIZE := size
|
||||
# Compiler flags for debug and release
|
||||
DEB_CFLAGS := -DDEBUG -g3 -Wall -Wextra
|
||||
REL_CFLAGS := -Wall -Wextra -O2
|
||||
# Pre-defines
|
||||
# PRE_DEFS := MYCAB=1729 SUPER_MODE
|
||||
|
||||
# ============== Linker settings ==============
|
||||
# Linker flags
|
||||
LDFLAGS := -pthread
|
||||
# Map output file
|
||||
MAP_FILE := output.map
|
||||
MAP_FLAG := -Xlinker -Map=$(BUILD_DIR)/$(MAP_FILE)
|
||||
|
||||
# ============== Docker settings ==============
|
||||
# We need:
|
||||
# 1) Bind the entire project directory(the dir that icludes all the code) as volume.
|
||||
# 2) In docker instance change to working directory(where the makefile is).
|
||||
# For utl we use the directories `${PWD%/*}` and `test`.
|
||||
# We double-$$ for double evaluating.
|
||||
DOCKER_VOL_DIR := "$${PWD%/*}"
|
||||
DOCKER_WRK_DIR := test
|
||||
DOCKER_RUN := docker run --rm -v $(DOCKER_VOL_DIR):/usr/src/$(PROJECT) -w /usr/src/$(PROJECT)/$(DOCKER_WRK_DIR)
|
||||
|
||||
# ============== Default settings ==============
|
||||
# compiler and compiler flags. By default docker is not used.
|
||||
CFLAGS := $(DEB_CFLAGS)
|
||||
CXX := $(GCCXX)
|
||||
DOCKER :=
|
||||
|
||||
#
|
||||
# =========== Main body and Patterns ===========
|
||||
#
|
||||
ifeq ($(OS), Windows_NT)
|
||||
TARGET := $(TARGET).exe
|
||||
endif
|
||||
INC := $(foreach dir,$(INC_DIR_LIST),-I$(dir))
|
||||
DEF := $(foreach def,$(PRE_DEFS),-D$(def))
|
||||
EXC := $(foreach fil,$(EXC_FILE_LIST), \
|
||||
$(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/$(fil))) \
|
||||
)
|
||||
# source files, object and dependencies list
|
||||
# recursive search into current and source directories
|
||||
SRC := $(wildcard *.cpp)
|
||||
SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/*.cpp))
|
||||
SRC += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/**/*.cpp))
|
||||
#SRC := $(abspath $(filter-out $(EXC),$(SRC)))
|
||||
SRC := $(filter-out $(EXC),$(SRC))
|
||||
|
||||
OBJ := $(foreach file,$(SRC:%.cpp=%.o),$(OBJ_DIR)/$(file))
|
||||
DEP := $(foreach file,$(SRC:%.cpp=%.d),$(DEP_DIR)/$(file))
|
||||
|
||||
|
||||
# Make Dependencies pattern.
|
||||
# This little trick enables recompilation only when dependencies change
|
||||
# and it does so for changes both in source AND header files ;)
|
||||
#
|
||||
# It is based on Tom Tromey's method.
|
||||
#
|
||||
# Invoke cpp to create makefile rules with dependencies for each source file
|
||||
$(DEP_DIR)/%.d: %.cpp
|
||||
@mkdir -p $(@D)
|
||||
@$(DOCKER) $(CXX) -E $(CFLAGS) $(INC) $(DEF) -MM -MT $(OBJ_DIR)/$(<:.cpp=.o) -MF $@ $<
|
||||
|
||||
# objects depent on .cpp AND dependency files, which have an empty recipe
|
||||
$(OBJ_DIR)/%.o: %.cpp $(DEP_DIR)/%.d
|
||||
@mkdir -p $(@D)
|
||||
$(DOCKER) $(CXX) -c $(CFLAGS) $(INC) $(DEF) -o $@ $<
|
||||
|
||||
# empty recipe for dependency files. This prevents make errors
|
||||
$(DEP):
|
||||
|
||||
# now include all dependencies
|
||||
# After all they are makefile dependency rules ;)
|
||||
include $(wildcard $(DEP))
|
||||
|
||||
# main target rule
|
||||
$(BUILD_DIR)/$(TARGET): $(OBJ)
|
||||
@mkdir -p $(@D)
|
||||
@echo Linking to target: $(TARGET)
|
||||
@echo $(DOCKER) $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) '$$(OBJ)'
|
||||
@$(DOCKER) $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) $(OBJ)
|
||||
@echo
|
||||
@echo Print size information
|
||||
@$(CSIZE) $(@D)/$(TARGET)
|
||||
@echo Done
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@echo Cleaning build directories
|
||||
@rm -rf $(OBJ_DIR)
|
||||
@rm -rf $(DEP_DIR)
|
||||
@rm -rf $(BUILD_DIR)
|
||||
|
||||
|
||||
#
|
||||
# ================ Local build rules =================
|
||||
# examples:
|
||||
# make MK_ARG="-std=c++14 -fconcepts" build-gcc
|
||||
# make MK_ARG="-std=c++17" build-clang
|
||||
#
|
||||
.PHONY: build-gcc
|
||||
build-gcc: CFLAGS += $(MK_ARG)
|
||||
build-gcc: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: build-clang
|
||||
build-clang: CXX := $(CLANGXX)
|
||||
build-clang: CFLAGS += $(MK_ARG)
|
||||
build-clang: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: debug
|
||||
debug: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: release
|
||||
release: CFLAGS := $(REL_FLAGS)
|
||||
release: clean $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: all
|
||||
all: clean release
|
||||
|
||||
#
|
||||
# ================ Docker based rules ================
|
||||
# examples:
|
||||
# make IMAGE="gcc:8.3" MK_ARG="-std=c++14 -fconcepts" dock-gcc
|
||||
# make IMAGE="a-clang-image" MK_ARG="-std=c++17" dock-clang
|
||||
#
|
||||
.PHONY: dock-gcc
|
||||
dock-gcc: DOCKER := $(DOCKER_RUN) $(IMAGE)
|
||||
dock-gcc: CFLAGS += $(MK_ARG)
|
||||
dock-gcc: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
.PHONY: dock-clang
|
||||
dock-clang: CXX := $(CLANGXX)
|
||||
dock-clang: DOCKER := $(DOCKER_RUN) $(IMAGE)
|
||||
dock-clang: CFLAGS += $(MK_ARG)
|
||||
dock-clang: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
|
||||
|
||||
@ -19,11 +19,14 @@
|
||||
*
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include <exception>
|
||||
|
||||
GTEST_API_ int main(int argc, char **argv) {
|
||||
GTEST_API_ int main(int argc, char **argv) try {
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
||||
catch (std::exception& e) {
|
||||
std::cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -140,9 +140,9 @@ namespace TmetaBasic {
|
||||
EXPECT_EQ (true, (std::is_same<true_, and_<true_, true_, true_>>()));
|
||||
EXPECT_EQ (true, (std::is_same<false_, and_<true_, true_, false_>>()));
|
||||
|
||||
EXPECT_EQ (true, (same_<Foo, Foo>()));
|
||||
EXPECT_EQ (false, (same_<Foo, Bar>()));
|
||||
EXPECT_EQ (true, (not_same_<Foo, Bar>()));
|
||||
EXPECT_EQ (true, (same<Foo, Foo>()));
|
||||
EXPECT_EQ (false, (same<Foo, Bar>()));
|
||||
EXPECT_EQ (true, (not_same<Foo, Bar>()));
|
||||
|
||||
}
|
||||
|
||||
@ -218,11 +218,11 @@ namespace TmetaBasic {
|
||||
/*
|
||||
* SFINAE
|
||||
*/
|
||||
template <typename T, typename =when<same_<T, int>::type::value>>
|
||||
template <typename T, typename =when<same<T, int>::type::value>>
|
||||
int check1 (T x) { return x; }
|
||||
int check1 (...) { return 0; }
|
||||
|
||||
template <typename T, typename =enable_if_t<same_<T, int>::type::value, void>>
|
||||
template <typename T, typename =enable_if_t<same<T, int>::type::value, void>>
|
||||
int check2 (T x) { return x; }
|
||||
int check2 (...) { return 0; }
|
||||
|
||||
|
||||
422
test/tests/cli_device.cpp
Normal file
422
test/tests/cli_device.cpp
Normal file
@ -0,0 +1,422 @@
|
||||
/*!
|
||||
* \file cli_device.cpp
|
||||
*
|
||||
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*
|
||||
*/
|
||||
#include <utl/dev/cli_device.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <utl/container/equeue.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#ifndef WIN_TRHEADS
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#else
|
||||
#include <mingw.thread.h>
|
||||
#include <mingw.mutex.h>
|
||||
#endif
|
||||
|
||||
namespace test_cli_device {
|
||||
using namespace utl;
|
||||
|
||||
// test settings
|
||||
constexpr size_t Size = 128;
|
||||
using data_type = char;
|
||||
|
||||
// cli_device implementer mock. We simulate a BG95 ATmodem for that purpose
|
||||
template<size_t N>
|
||||
class BG95 : public cli_device<BG95<N>, N> {
|
||||
using base_type = cli_device<BG95<N>, N>;
|
||||
using Queue = equeue<typename base_type::value_type, N, true>;
|
||||
|
||||
public:
|
||||
enum class event {
|
||||
MQTT_DISCONNECT, MQTT_RXDATA
|
||||
};
|
||||
// simulated modem operation
|
||||
private:
|
||||
struct cmd_pair {
|
||||
const char *cmd;
|
||||
const char *resp;
|
||||
};
|
||||
struct event_pair {
|
||||
event e;
|
||||
const char* resp;
|
||||
};
|
||||
|
||||
std::array<cmd_pair, 20> cmd_map = {{
|
||||
{"", ""},
|
||||
{"ERROR", "\r\nERROR\r\n"},
|
||||
{"ATE0\r\n", "\r\nATE0\r\nOK\r\n"},
|
||||
{"AT\r\n", "\r\nOK\r\n"},
|
||||
{"AT+QCFG=\"nwscanseq\"\r\n", "\r\n+QCFG: \"nwscanseq\",020301\r\n"},
|
||||
{"AT+QCFG=\"nwscanseq\",010302\r\n", "\r\nOK\r\n"},
|
||||
{"AT+CREG?\r\n", "\r\n+CREG: 0,5\r\n\r\nOK\r\n"},
|
||||
{"AT+CSQ\r\n", "\r\n+CSQ: 19,99\r\n\r\nOK\r\n"},
|
||||
{"AT+QNWINFO\r\n", "\r\n+QNWINFO: \"EDGE\",\"20201\",\"GSM 1800\",865\r\n\r\nOK\r\n"},
|
||||
// Files
|
||||
{"AT+QFLST\r\n", "\r\n+QFLST: \"cacert.pem\",1220\r\n+QFLST: \"security/\",2\r\nOK\r\n"},
|
||||
// MQTT config
|
||||
{"AT+QSSLCFG=\"ignorelocaltime\",2,1\r\n", "\r\nOK\r\n"},
|
||||
{"AT+QSSLCFG=\"seclevel\",2,1\r\n", "\r\nOK\r\n"},
|
||||
{"AT+QSSLCFG=\"sslversion\",2,4\r\n", "\r\nOK\r\n"},
|
||||
{"AT+QSSLCFG=\"ciphersuite\",2\r\n", "\r\n+QSSLCFG: \"ciphersuite\",2,0XFFFF\r\n\r\nOK\r\n"},
|
||||
{"AT+QMTCFG=\"ssl\",0,1,2\r\n", "\r\nOK\r\n"},
|
||||
{"AT+QMTCFG=\"keepalive\",0,3600\r\n", "\r\nOK\r\n"},
|
||||
// MQTT
|
||||
{"AT+QMTOPEN=0,\"server.com\",8883\r\n", "\r\nOK\r\n\r\n+QMTOPEN: 0,0\r\n"},
|
||||
{"AT+QMTCONN=0,\"myID\",\"user\",\"pass\"\r\n", "\r\nOK\r\n\r\n+QMTCONN: 0,0,0\r\n"},
|
||||
{"AT+QMTSUB=0,1,\"/path/topic1\",2\r\n", "\r\nOK\r\n\r\n+QMTSUB: 0,1,0,2\r\n"},
|
||||
{"AT+QMTPUB=0,0,0,0,\"/path/topic2\",9\r\n", "\r\n> \r\nOK\r\n\r\n+QMTPUB: 0,0,0\r\n"},
|
||||
}};
|
||||
std::array<event_pair, 2> event_map {{
|
||||
{event::MQTT_DISCONNECT, "\r\n+QMTSTAT: 0,1\r\n"},
|
||||
{event::MQTT_RXDATA, "\r\n+QMTRECV: 0,1,\"/path/topic1\",\"BR: hello to all of my subscribers\""}
|
||||
}};
|
||||
const char* cmd_responce (const char* cmd) {
|
||||
if (cmd != nullptr) {
|
||||
for (auto& it : cmd_map) {
|
||||
if (!std::strcmp(it.cmd, cmd))
|
||||
return it.resp;
|
||||
}
|
||||
}
|
||||
return cmd_map[1].resp;
|
||||
}
|
||||
const char* event_responce (const event e) {
|
||||
for (auto& it : event_map) {
|
||||
if (e == it.e)
|
||||
return it.resp;
|
||||
}
|
||||
return nullptr; // non reachable
|
||||
}
|
||||
|
||||
// data
|
||||
Queue RxQ{};
|
||||
std::atomic<size_t> lines{};
|
||||
clock_t t=0;
|
||||
|
||||
public:
|
||||
// cli_device driver requirements
|
||||
BG95() noexcept :
|
||||
RxQ(Queue::data_match::MATCH_PUSH, base_type::delimiter, [&](){
|
||||
lines.fetch_add(1, std::memory_order_acq_rel);
|
||||
}), lines(0) { }
|
||||
|
||||
size_t get(char* data, bool wait =false) {
|
||||
do {
|
||||
if (lines.load(std::memory_order_acquire)) {
|
||||
size_t n =0;
|
||||
do{
|
||||
*data << RxQ;
|
||||
++n;
|
||||
} while (*data++ != base_type::delimiter);
|
||||
lines.fetch_sub(1, std::memory_order_acq_rel);
|
||||
return n;
|
||||
}
|
||||
} while (wait);
|
||||
return 0;
|
||||
}
|
||||
size_t contents(char* data) {
|
||||
char* nullpos = std::copy(RxQ.begin(), RxQ.end(), data);
|
||||
*nullpos =0;
|
||||
return nullpos - data;
|
||||
}
|
||||
size_t put (const char* data, size_t n) {
|
||||
const char* reply = cmd_responce (data);
|
||||
while (*reply)
|
||||
RxQ << *reply++;
|
||||
return n;
|
||||
}
|
||||
|
||||
clock_t clock() noexcept { return ++t; }
|
||||
void clear_clock() noexcept { t=0; }
|
||||
|
||||
// extra helper for testing purposes
|
||||
void async (event e) {
|
||||
const char* reply =event_responce (e);
|
||||
while (*reply)
|
||||
RxQ << *reply++;
|
||||
}
|
||||
};
|
||||
|
||||
// Behavior flag
|
||||
bool handler_flag = false;
|
||||
void handler (const char* data, size_t n) {
|
||||
(void)*data;
|
||||
(void)n;
|
||||
handler_flag = true;
|
||||
}
|
||||
void clear_flag () {
|
||||
handler_flag = false;
|
||||
}
|
||||
|
||||
TEST(Tcli_device, traits) {
|
||||
EXPECT_EQ ( std::is_default_constructible<BG95<Size>>::value, true);
|
||||
EXPECT_EQ ( std::is_nothrow_default_constructible<BG95<Size>>::value, true);
|
||||
EXPECT_EQ (!std::is_copy_constructible<BG95<Size>>::value, true);
|
||||
EXPECT_EQ (!std::is_copy_assignable<BG95<Size>>::value, true);
|
||||
|
||||
EXPECT_EQ ((std::is_same_v<BG95<Size>::value_type, data_type>), true);
|
||||
EXPECT_EQ ((std::is_same_v<BG95<Size>::pointer_type, data_type*>), true);
|
||||
EXPECT_EQ ((std::is_same_v<BG95<Size>::size_type, size_t>), true);
|
||||
EXPECT_EQ ((std::is_same_v<BG95<Size>::string_view, std::basic_string_view<data_type>>), true);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Test inetd in non blocking mode
|
||||
*/
|
||||
TEST(Tcli_device, txrx_inetd) {
|
||||
BG95<Size> modem;
|
||||
char buffer[Size];
|
||||
size_t s =0;
|
||||
|
||||
const BG95<Size>::inetd_handlers<2> async = {{
|
||||
{"+QMTSTAT:", BG95<Size>::starts_with, handler},
|
||||
{"+QMTRECV", BG95<Size>::contains, handler},
|
||||
}};
|
||||
|
||||
s = modem.transmit("", std::strlen(""));
|
||||
EXPECT_EQ (s, 0UL);
|
||||
s = modem.transmit("");
|
||||
EXPECT_EQ (s, 0UL);
|
||||
s = modem.transmit(nullptr);
|
||||
EXPECT_EQ (s, 0UL);
|
||||
|
||||
clear_flag();
|
||||
modem.inetd(false, &async);
|
||||
EXPECT_EQ (handler_flag, false);
|
||||
modem.async(BG95<Size>::event::MQTT_DISCONNECT);
|
||||
modem.inetd(false, &async); // parse "\r\n"
|
||||
EXPECT_EQ (handler_flag, false);
|
||||
modem.inetd(false, &async); // parse "+QMT*\r\n" and dispatch to handler()
|
||||
EXPECT_EQ (handler_flag, true);
|
||||
clear_flag(); // nothing to parse
|
||||
modem.inetd(false, &async);
|
||||
modem.inetd(false, &async);
|
||||
modem.inetd(false, &async);
|
||||
EXPECT_EQ (handler_flag, false);
|
||||
EXPECT_NE (modem.receive(buffer), 0UL); // "\r\n" in buffer
|
||||
EXPECT_EQ (std::strcmp(buffer, "\r\n"), 0);
|
||||
|
||||
clear_flag();
|
||||
modem.inetd(false, &async);
|
||||
EXPECT_EQ (handler_flag, false);
|
||||
modem.transmit("AT+CSQ\r\n", 8);
|
||||
EXPECT_EQ (modem.receive(buffer), 0UL);
|
||||
modem.inetd(false, &async); // parse "\r\n"
|
||||
EXPECT_NE (modem.receive(buffer), 0UL);
|
||||
EXPECT_EQ (std::strcmp(buffer, "\r\n"), 0);
|
||||
modem.inetd(false, &async); // parse "+CSQ: 19,99\r\n"
|
||||
EXPECT_NE (modem.receive(buffer), 0UL);
|
||||
EXPECT_EQ (std::strcmp(buffer, "+CSQ: 19,99\r\n"), 0);
|
||||
modem.inetd(false, &async); // parse "\r\n"
|
||||
EXPECT_NE (modem.receive(buffer), 0UL);
|
||||
EXPECT_EQ (std::strcmp(buffer, "\r\n"), 0);
|
||||
modem.inetd(false, &async); // parse "OK\r\n"
|
||||
EXPECT_NE (modem.receive(buffer), 0UL);
|
||||
EXPECT_EQ (std::strcmp(buffer, "OK\r\n"), 0);
|
||||
modem.inetd(false, &async); // nothing to parse
|
||||
modem.inetd(false, &async);
|
||||
modem.inetd(false, &async);
|
||||
EXPECT_EQ (modem.receive(buffer), 0UL);
|
||||
}
|
||||
|
||||
TEST(Tcli_device, run) {
|
||||
BG95<Size> modem;
|
||||
|
||||
using Control = BG95<Size>::control_t;
|
||||
|
||||
const BG95<Size>::script_t<4> script = {{
|
||||
{Control::NOP, "", BG95<Size>::nil, BG95<Size>::nil, BG95<Size>::go_to<1>, 100000},
|
||||
{Control::SEND, "ATE0\r\n", BG95<Size>::nil, BG95<Size>::nil, BG95<Size>::next, 0},
|
||||
{Control::EXPECT, "OK\r\n", BG95<Size>::ends_with, BG95<Size>::nil, BG95<Size>::exit_ok, 100000},
|
||||
{Control::OR_EXPECT,"ERROR", BG95<Size>::contains, BG95<Size>::nil, BG95<Size>::exit_error, 0}
|
||||
}};
|
||||
|
||||
std::mutex m;
|
||||
m.lock();
|
||||
std::thread th1 ([&](){
|
||||
do
|
||||
modem.inetd(false);
|
||||
while (!m.try_lock());
|
||||
m.unlock();
|
||||
});
|
||||
EXPECT_EQ (modem.run(script), BG95<Size>::exit_ok.value);
|
||||
m.unlock(); // stop and join inetd
|
||||
th1.join();
|
||||
}
|
||||
|
||||
TEST(Tcli_device, clear_size) {
|
||||
BG95<Size> modem;
|
||||
char buffer[Size];
|
||||
|
||||
modem.clear();
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
EXPECT_EQ (modem.receive(buffer), 0UL);
|
||||
|
||||
modem.transmit("abcd", 4);
|
||||
modem.inetd(false);
|
||||
modem.inetd(false);
|
||||
EXPECT_NE (modem.size(), 0UL);
|
||||
modem.clear();
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
|
||||
}
|
||||
|
||||
TEST(Tcli_device, command_non_extraction) {
|
||||
BG95<Size> modem;
|
||||
char buffer[Size];
|
||||
|
||||
std::mutex m;
|
||||
m.lock();
|
||||
std::thread th1 ([&](){
|
||||
do
|
||||
modem.inetd(false);
|
||||
while (!m.try_lock());
|
||||
m.unlock();
|
||||
});
|
||||
|
||||
auto run_receive = [&](size_t times) -> size_t {
|
||||
size_t s =0;
|
||||
for (size_t i=0 ; i<times ; ++i)
|
||||
s += modem.receive(buffer);
|
||||
return s;
|
||||
};
|
||||
|
||||
EXPECT_EQ (modem.command("", "", 0), true); // returns: ""
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
EXPECT_EQ (run_receive(100000), 0UL);
|
||||
|
||||
EXPECT_EQ (modem.command("", "abcd", 100000), false);// returns: ""
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
EXPECT_EQ (run_receive(100000), 0UL);
|
||||
|
||||
EXPECT_EQ (modem.command("abcd", "", 0), true); // returns: "\r\nERROR\r\n"
|
||||
EXPECT_GT (run_receive(100000), 0UL);
|
||||
|
||||
EXPECT_EQ (modem.command("AT\r\n", "Something else", 100000), false);// returns: "\r\nOK\r\n"
|
||||
EXPECT_GT (run_receive(100000), 0UL);
|
||||
|
||||
EXPECT_EQ (modem.command("AT\r\n", "\r\nOK\r\n", 100000), true); // returns: "\r\nOK\r\n"
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
EXPECT_EQ (run_receive(100000), 0UL);
|
||||
|
||||
EXPECT_EQ (modem.command("AT\r\n", "%OK\r\n", 100000), true); // returns: "\r\nOK\r\n"
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
EXPECT_EQ (run_receive(100000), 0UL);
|
||||
|
||||
// returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
|
||||
EXPECT_EQ (modem.command<BG95<Size>::flush>("AT+CREG?\r\n", "%OK\r\n", 0), false);
|
||||
EXPECT_GT (run_receive(100000), 0UL);
|
||||
|
||||
// returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
|
||||
EXPECT_EQ (modem.command<BG95<Size>::flush>("AT+CREG?\r\n", "%%%OK\r\n", 0), true);
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
EXPECT_EQ (run_receive(100000), 0UL);
|
||||
|
||||
// returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
|
||||
EXPECT_EQ (modem.command<BG95<Size>::flush>("AT+CREG?\r\n", "%", 0), true);
|
||||
EXPECT_GT (run_receive(100000), 0UL);
|
||||
|
||||
EXPECT_EQ (modem.command<BG95<Size>::flush>("AT\r\n", "%%", 0), true); // returns: "\r\nOK\r\n"
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
EXPECT_EQ (run_receive(100000), 0UL);
|
||||
|
||||
EXPECT_EQ (modem.command<BG95<Size>::flush>("AT\r\n", "%%%", 10000), false); // returns: "\r\nOK\r\n"
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
EXPECT_EQ (run_receive(100000), 0UL);
|
||||
|
||||
// returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
|
||||
EXPECT_EQ (modem.command<modem.flush>("AT+CREG?\r\n", "", 0), true);
|
||||
EXPECT_EQ (modem.command<modem.keep>("", "%", 0), true);
|
||||
EXPECT_EQ (modem.command<modem.keep>("", "%%", 0), true);
|
||||
EXPECT_EQ (modem.command<modem.keep>("", "%", 0), true);
|
||||
EXPECT_EQ (modem.command<modem.keep>("", "%", 10000), false);
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
EXPECT_EQ (run_receive(100000), 0UL);
|
||||
|
||||
m.unlock(); // stop and join inetd
|
||||
th1.join();
|
||||
}
|
||||
|
||||
TEST(Tcli_device, command_extraction) {
|
||||
BG95<Size> modem;
|
||||
char buffer[Size];
|
||||
|
||||
std::mutex m;
|
||||
m.lock();
|
||||
std::thread th1 ([&](){
|
||||
do
|
||||
modem.inetd(false);
|
||||
while (!m.try_lock());
|
||||
m.unlock();
|
||||
});
|
||||
|
||||
auto run_receive = [&](size_t times) -> size_t {
|
||||
size_t s =0;
|
||||
for (size_t i=0 ; i<times ; ++i)
|
||||
s += modem.receive(buffer);
|
||||
return s;
|
||||
};
|
||||
|
||||
int status1, status2;
|
||||
EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", 100000, &status1), true);
|
||||
EXPECT_EQ (status1, 5);
|
||||
EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: %,%\r\n\r\nOK\r\n", 100000, &status1, &status2), true);
|
||||
EXPECT_EQ (status1, 0);
|
||||
EXPECT_EQ (status2, 5);
|
||||
|
||||
char substr1[32], substr2[32];
|
||||
EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n%\r\n\r\n%\r\n", 100000, substr1, substr2), true);
|
||||
EXPECT_EQ (std::strcmp("+CREG: 0,5", substr1), 0);
|
||||
EXPECT_EQ (std::strcmp("OK", substr2), 0);
|
||||
|
||||
// returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
|
||||
EXPECT_EQ (modem.command<modem.flush>("AT+CREG?\r\n", "", 100000), true);
|
||||
EXPECT_EQ (modem.command<modem.keep>("", "%", 100000, substr1), true);
|
||||
EXPECT_EQ (std::strcmp("\r\n", substr1), 0);
|
||||
|
||||
EXPECT_EQ (modem.command<modem.keep>("", "%%", 100000, substr1, substr2), true);
|
||||
EXPECT_EQ (std::strcmp("+CREG: 0,5\r\n", substr1), 0);
|
||||
EXPECT_EQ (std::strcmp("\r\n", substr2), 0);
|
||||
|
||||
EXPECT_EQ (modem.command<modem.keep>("", "%", 100000, substr1), true);
|
||||
EXPECT_EQ (std::strcmp("OK\r\n", substr1), 0);
|
||||
|
||||
EXPECT_EQ (modem.command<modem.keep>("", "%", 10000), false);
|
||||
|
||||
EXPECT_EQ (modem.size(), 0UL);
|
||||
EXPECT_EQ (run_receive(100000), 0UL);
|
||||
|
||||
m.unlock(); // stop and join inetd
|
||||
th1.join();
|
||||
|
||||
}
|
||||
}
|
||||
469
test/tests/deque.cpp
Normal file
469
test/tests/deque.cpp
Normal file
@ -0,0 +1,469 @@
|
||||
/*!
|
||||
* \file deque.cpp
|
||||
* \brief
|
||||
* Unit tests for deque
|
||||
*
|
||||
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*
|
||||
*/
|
||||
#include <utl/container/deque.h>
|
||||
//#include <utl/container/span.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <array>
|
||||
#include <type_traits>
|
||||
#include <cstring>
|
||||
#ifndef WIN_TRHEADS
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#else
|
||||
#include <mingw.thread.h>
|
||||
#include <mingw.mutex.h>
|
||||
#endif
|
||||
|
||||
namespace Tdeque {
|
||||
using namespace utl;
|
||||
|
||||
// template <typename>
|
||||
// struct is_span : std::false_type {};
|
||||
//
|
||||
// template <typename T, std::size_t S>
|
||||
// struct is_span<tbx::span<T, S>> : std::true_type {};
|
||||
|
||||
template <typename>
|
||||
struct is_std_array : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct is_std_array<std::array<T, N>> : std::true_type {};
|
||||
|
||||
template <typename, typename = void>
|
||||
struct has_size_and_data : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct has_size_and_data<T, std::void_t<decltype(std::declval<T>().size()),
|
||||
decltype(std::declval<T>().data())>>
|
||||
: std::true_type {};
|
||||
|
||||
// Concept
|
||||
TEST(Tdeque, concept) {
|
||||
using deque_t = deque<int, 8>;
|
||||
|
||||
EXPECT_EQ ( std::is_default_constructible<deque_t>::value, true);
|
||||
EXPECT_EQ ( std::is_nothrow_default_constructible<deque_t>::value, true);
|
||||
EXPECT_EQ (!std::is_copy_constructible<deque_t>::value, true);
|
||||
EXPECT_EQ (!std::is_copy_assignable<deque_t>::value, true);
|
||||
|
||||
// EXPECT_EQ (true, !is_span<deque_t>::value);
|
||||
EXPECT_EQ (true, !is_std_array<deque_t>::value);
|
||||
EXPECT_EQ (true, !std::is_array<deque_t>::value);
|
||||
EXPECT_EQ (true, has_size_and_data<deque_t>::value);
|
||||
}
|
||||
// Test construction
|
||||
TEST(Tdeque, contruct) {
|
||||
deque<int, 8> q1;
|
||||
deque<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
deque<int, 8> q3{1, 2, 3, 4, 5};
|
||||
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (8UL, q2.capacity());
|
||||
EXPECT_EQ (8UL, q2.size());
|
||||
EXPECT_EQ (8UL, q3.capacity());
|
||||
EXPECT_EQ (5UL, q3.size());
|
||||
}
|
||||
|
||||
// simple push-pop functionality
|
||||
TEST(Tdeque, push_pop) {
|
||||
deque<int, 8> q1;
|
||||
deque<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
|
||||
q1.push_front(1);
|
||||
q1.push_front(2);
|
||||
EXPECT_EQ (1, q1.pop_back());
|
||||
EXPECT_EQ (2, q1.pop_back());
|
||||
|
||||
q1.push_back(1);
|
||||
q1.push_back(2);
|
||||
EXPECT_EQ (1, q1.pop_front());
|
||||
EXPECT_EQ (2, q1.pop_front());
|
||||
|
||||
q1.push_front(2);
|
||||
q1.push_back(3);
|
||||
q1.push_front(1);
|
||||
q1.push_back(4);
|
||||
|
||||
for (int i=1 ; i<= 4 ; ++i)
|
||||
EXPECT_EQ ((int)i, q1.pop_front());
|
||||
}
|
||||
|
||||
// front-back
|
||||
TEST(Tdeque, front_back) {
|
||||
deque<int, 8> q1;
|
||||
deque<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
|
||||
q1.push_front(2);
|
||||
q1.push_front(1);
|
||||
q1.push_back(3);
|
||||
q1.push_back(4);
|
||||
|
||||
EXPECT_EQ (1, q1.front());
|
||||
EXPECT_EQ (4, q1.back());
|
||||
EXPECT_EQ (1, q2.front());
|
||||
EXPECT_EQ (8, q2.back());
|
||||
}
|
||||
|
||||
// capacity
|
||||
TEST(Tdeque, capacity) {
|
||||
deque<int, 8> q1;
|
||||
deque<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
|
||||
q1.push_back(1);
|
||||
q1.clear();
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (true, q2.full());
|
||||
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q2.capacity());
|
||||
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (8UL, q2.size());
|
||||
|
||||
q1.push_back(2);
|
||||
EXPECT_EQ (1UL, q1.size());
|
||||
q1.push_front(1);
|
||||
EXPECT_EQ (2UL, q1.size());
|
||||
|
||||
q1.pop_back();
|
||||
EXPECT_EQ (1UL, q1.size());
|
||||
q1.pop_front();
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
}
|
||||
|
||||
// push-pop limits
|
||||
TEST (Tdeque, push_pop_limits) {
|
||||
deque<int, 8> q1;
|
||||
deque<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
|
||||
EXPECT_EQ (int{}, q1.pop_back());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
EXPECT_EQ (int{}, q1.pop_front());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
q2.push_front(0);
|
||||
EXPECT_EQ (1, q2.front());
|
||||
EXPECT_EQ (8, q2.back());
|
||||
EXPECT_EQ (8UL, q2.size());
|
||||
EXPECT_EQ (false, q2.empty());
|
||||
EXPECT_EQ (true, q2.full());
|
||||
|
||||
q2.push_back(9);
|
||||
EXPECT_EQ (1, q2.front());
|
||||
EXPECT_EQ (8, q2.back());
|
||||
EXPECT_EQ (8UL, q2.size());
|
||||
EXPECT_EQ (false, q2.empty());
|
||||
EXPECT_EQ (true, q2.full());
|
||||
}
|
||||
|
||||
// iterators
|
||||
TEST (Tdeque, iterators) {
|
||||
deque<int, 8> q1{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
int check_it=1;
|
||||
|
||||
EXPECT_EQ (q1.begin().base(), q1.end().base());
|
||||
EXPECT_NE (q1.begin().iter(), q1.end().iter());
|
||||
EXPECT_EQ (1, *q1.begin());
|
||||
EXPECT_EQ (true, (q1.begin() == ++q1.end())); // loop edge iterators
|
||||
|
||||
for (auto it = q1.begin() ; it != q1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(9, check_it); // run through all
|
||||
|
||||
EXPECT_EQ (1, q1.front()); // queue stays intact
|
||||
EXPECT_EQ (8, q1.back());
|
||||
EXPECT_EQ (8UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (true, q1.full());
|
||||
|
||||
q1.pop_front();
|
||||
q1.pop_back();
|
||||
|
||||
check_it=2;
|
||||
for (auto& it : q1)
|
||||
EXPECT_EQ(it, check_it++);
|
||||
EXPECT_EQ(8, check_it); // run through all
|
||||
|
||||
EXPECT_EQ (2, q1.front()); // queue stays intact
|
||||
EXPECT_EQ (7, q1.back());
|
||||
EXPECT_EQ (6UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
deque<int, 8> q2;
|
||||
q2.push_front(2);
|
||||
q2.push_front(1);
|
||||
q2.push_back(3);
|
||||
q2.push_back(4);
|
||||
q2.push_back(5);
|
||||
check_it =1;
|
||||
for (auto& it : q2)
|
||||
EXPECT_EQ(it, check_it++);
|
||||
EXPECT_EQ(6, check_it); // run through all
|
||||
|
||||
}
|
||||
TEST (Tdeque, range) {
|
||||
deque<int, 8> q1{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
int check_it=1;
|
||||
|
||||
for (auto& it : q1.contents())
|
||||
EXPECT_EQ(it, check_it++);
|
||||
|
||||
EXPECT_EQ(9, check_it); // run through all
|
||||
}
|
||||
|
||||
|
||||
// Concept
|
||||
TEST(Tdeque, concept_atomic) {
|
||||
using deque_t = deque<int, 8, true>;
|
||||
|
||||
// EXPECT_EQ (true, !is_span<deque_t>::value);
|
||||
EXPECT_EQ (true, !is_std_array<deque_t>::value);
|
||||
EXPECT_EQ (true, !std::is_array<deque_t>::value);
|
||||
EXPECT_EQ (true, has_size_and_data<deque_t>::value);
|
||||
}
|
||||
// Test construction
|
||||
TEST(Tdeque, contruct_atomic) {
|
||||
deque<int, 8, true> q1;
|
||||
deque<int, 8, true> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
deque<int, 8, true> q3{1, 2, 3, 4, 5};
|
||||
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (8UL, q2.capacity());
|
||||
EXPECT_EQ (8UL, q2.size());
|
||||
EXPECT_EQ (8UL, q3.capacity());
|
||||
EXPECT_EQ (5UL, q3.size());
|
||||
}
|
||||
|
||||
// simple push-pop functionality
|
||||
TEST(Tdeque, push_pop_atomic) {
|
||||
deque<int, 8, true> q1;
|
||||
deque<int, 8, true> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
|
||||
q1.push_front(1);
|
||||
q1.push_front(2);
|
||||
EXPECT_EQ (1, q1.pop_back());
|
||||
EXPECT_EQ (2, q1.pop_back());
|
||||
|
||||
q1.push_back(1);
|
||||
q1.push_back(2);
|
||||
EXPECT_EQ (1, q1.pop_front());
|
||||
EXPECT_EQ (2, q1.pop_front());
|
||||
|
||||
q1.push_front(2);
|
||||
q1.push_back(3);
|
||||
q1.push_front(1);
|
||||
q1.push_back(4);
|
||||
|
||||
for (int i=1 ; i<= 4 ; ++i)
|
||||
EXPECT_EQ ((int)i, q1.pop_front());
|
||||
}
|
||||
|
||||
// front-back
|
||||
TEST(Tdeque, front_back_atomic) {
|
||||
deque<int, 8, true> q1;
|
||||
deque<int, 8, true> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
|
||||
q1.push_front(2);
|
||||
q1.push_front(1);
|
||||
q1.push_back(3);
|
||||
q1.push_back(4);
|
||||
|
||||
EXPECT_EQ (1, q1.front());
|
||||
EXPECT_EQ (4, q1.back());
|
||||
EXPECT_EQ (1, q2.front());
|
||||
EXPECT_EQ (8, q2.back());
|
||||
}
|
||||
|
||||
// capacity
|
||||
TEST(Tdeque, capacity_atomic) {
|
||||
deque<int, 8, true> q1;
|
||||
deque<int, 8, true> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
|
||||
q1.push_back(1);
|
||||
q1.clear();
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (true, q2.full());
|
||||
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q2.capacity());
|
||||
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (8UL, q2.size());
|
||||
|
||||
q1.push_back(2);
|
||||
EXPECT_EQ (1UL, q1.size());
|
||||
q1.push_front(1);
|
||||
EXPECT_EQ (2UL, q1.size());
|
||||
|
||||
q1.pop_back();
|
||||
EXPECT_EQ (1UL, q1.size());
|
||||
q1.pop_front();
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
}
|
||||
|
||||
// push-pop limits
|
||||
TEST (Tdeque, push_pop_limits_atomic) {
|
||||
deque<int, 8, true> q1;
|
||||
deque<int, 8, true> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
|
||||
EXPECT_EQ (int{}, q1.pop_back());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
EXPECT_EQ (int{}, q1.pop_front());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
q2.push_front(0);
|
||||
EXPECT_EQ (1, q2.front());
|
||||
EXPECT_EQ (8, q2.back());
|
||||
EXPECT_EQ (8UL, q2.size());
|
||||
EXPECT_EQ (false, q2.empty());
|
||||
EXPECT_EQ (true, q2.full());
|
||||
|
||||
q2.push_back(9);
|
||||
EXPECT_EQ (1, q2.front());
|
||||
EXPECT_EQ (8, q2.back());
|
||||
EXPECT_EQ (8UL, q2.size());
|
||||
EXPECT_EQ (false, q2.empty());
|
||||
EXPECT_EQ (true, q2.full());
|
||||
}
|
||||
|
||||
// iterators
|
||||
TEST (Tdeque, iterators_atomic) {
|
||||
deque<int, 8, true> q1{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
int check_it=1;
|
||||
|
||||
EXPECT_EQ (q1.begin().base(), q1.end().base());
|
||||
EXPECT_NE (q1.begin().iter(), q1.end().iter());
|
||||
EXPECT_EQ (1, *q1.begin());
|
||||
EXPECT_EQ (true, (q1.begin() == ++q1.end())); // loop edge iterators
|
||||
|
||||
for (auto it = q1.begin() ; it != q1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(9, check_it); // run through all
|
||||
|
||||
EXPECT_EQ (1, q1.front()); // queue stays intact
|
||||
EXPECT_EQ (8, q1.back());
|
||||
EXPECT_EQ (8UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (true, q1.full());
|
||||
|
||||
q1.pop_front();
|
||||
q1.pop_back();
|
||||
|
||||
check_it=2;
|
||||
for (auto& it : q1)
|
||||
EXPECT_EQ(it, check_it++);
|
||||
EXPECT_EQ(8, check_it); // run through all
|
||||
|
||||
EXPECT_EQ (2, q1.front()); // queue stays intact
|
||||
EXPECT_EQ (7, q1.back());
|
||||
EXPECT_EQ (6UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
deque<int, 8, true> q2;
|
||||
q2.push_front(2);
|
||||
q2.push_front(1);
|
||||
q2.push_back(3);
|
||||
q2.push_back(4);
|
||||
q2.push_back(5);
|
||||
check_it =1;
|
||||
for (auto& it : q2)
|
||||
EXPECT_EQ(it, check_it++);
|
||||
EXPECT_EQ(6, check_it); // run through all
|
||||
|
||||
}
|
||||
TEST (Tdeque, range_atomic) {
|
||||
deque<int, 8, true> q1{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
int check_it=1;
|
||||
|
||||
for (auto& it : q1.contents())
|
||||
EXPECT_EQ(it, check_it++);
|
||||
|
||||
EXPECT_EQ(9, check_it); // run through all
|
||||
}
|
||||
|
||||
TEST(Tdeque, race) {
|
||||
constexpr size_t N = 1000000;
|
||||
deque<int, N, true> q;
|
||||
int result[N];
|
||||
|
||||
auto push_front = [&](){
|
||||
for (size_t i=1 ; i<=N ; ++i) q.push_front(i);
|
||||
};
|
||||
auto push_back = [&](){
|
||||
for (size_t i=1 ; i<=N ; ++i) q.push_back(i);
|
||||
};
|
||||
auto pop_front = [&](){
|
||||
for (size_t i=0 ; i<N ; ) {
|
||||
result[i] = q.pop_front();
|
||||
if (result[i] != int{})
|
||||
++i;
|
||||
}
|
||||
};
|
||||
auto pop_back = [&](){
|
||||
for (size_t i=0 ; i<N ; ) {
|
||||
result[i] = q.pop_back();
|
||||
if (result[i] != int{})
|
||||
++i;
|
||||
}
|
||||
};
|
||||
|
||||
std::memset(result, 0, sizeof result);
|
||||
std::thread th1 (push_front);
|
||||
std::thread th2 (pop_back);
|
||||
th1.join();
|
||||
th2.join();
|
||||
for (size_t i=0 ; i<N ; ++i)
|
||||
EXPECT_EQ (result[i], (int)i+1);
|
||||
|
||||
std::memset(result, 0, sizeof result);
|
||||
std::thread th3 (push_back);
|
||||
std::thread th4 (pop_front);
|
||||
th3.join();
|
||||
th4.join();
|
||||
for (size_t i=0 ; i<N ; ++i)
|
||||
EXPECT_EQ (result[i], (int)i+1);
|
||||
}
|
||||
}
|
||||
555
test/tests/edeque.cpp
Normal file
555
test/tests/edeque.cpp
Normal file
@ -0,0 +1,555 @@
|
||||
/*!
|
||||
* \file deque.cpp
|
||||
* \brief
|
||||
* Unit tests for edeque
|
||||
*
|
||||
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*
|
||||
*/
|
||||
#include <utl/container/edeque.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace Tedeque {
|
||||
using namespace utl;
|
||||
|
||||
int global_flag =0;
|
||||
|
||||
// Callable mocks
|
||||
void vfun(void) { ++global_flag; }
|
||||
|
||||
struct vfoo {
|
||||
void operator() (void) { ++global_flag; }
|
||||
};
|
||||
|
||||
|
||||
TEST (Tedeque, construct) {
|
||||
using Edeque = edeque<int, 8>;
|
||||
struct T { int a,b; };
|
||||
int local{};
|
||||
|
||||
Edeque e1(Edeque::size_match::GE, 3, [](){
|
||||
++global_flag;
|
||||
});
|
||||
Edeque e2(Edeque::size_match::GE, 3, [&](){
|
||||
++local;
|
||||
});
|
||||
Edeque e3(Edeque::size_match::EQ, 7, vfun);
|
||||
edeque<T, 8> e4(edeque<T, 8>::size_match::EQ, 2, vfoo{});
|
||||
|
||||
edeque<int, 8> q1;
|
||||
edeque<int, 8> q2(edeque<int, 8>::size_match::DISABLED, 0, nullptr);
|
||||
|
||||
EXPECT_EQ (8UL, e1.capacity());
|
||||
EXPECT_EQ (8UL, e2.capacity());
|
||||
EXPECT_EQ (8UL, e3.capacity());
|
||||
EXPECT_EQ (8UL, e4.capacity());
|
||||
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q2.capacity());
|
||||
}
|
||||
|
||||
TEST (Tedeque, base_class) {
|
||||
using Edeque = edeque<int, 8>;
|
||||
|
||||
Edeque e1(Edeque::size_match::GE, 3, [](){
|
||||
++global_flag;
|
||||
});
|
||||
|
||||
// Access of base class functionality
|
||||
EXPECT_EQ (8UL, e1.capacity());
|
||||
EXPECT_EQ (0UL, e1.size());
|
||||
EXPECT_EQ (true, e1.empty());
|
||||
EXPECT_EQ (false, e1.full());
|
||||
|
||||
e1.push_back(7);
|
||||
EXPECT_EQ (7, e1.front());
|
||||
EXPECT_EQ (7, e1.back());
|
||||
EXPECT_EQ (7, e1.pop_front());
|
||||
|
||||
e1.push_front(42);
|
||||
EXPECT_EQ (42, e1.front());
|
||||
EXPECT_EQ (42, e1.back());
|
||||
EXPECT_EQ (42, e1.pop_back());
|
||||
|
||||
e1.push_back(1);
|
||||
e1.push_back(2);
|
||||
e1.push_back(3);
|
||||
|
||||
int check_it=1;
|
||||
for (auto it = e1.begin() ; it != e1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(4, check_it); // run through all
|
||||
}
|
||||
|
||||
TEST (Tedeque, set_clear_check_trigger) {
|
||||
using Edeque = edeque<int, 8>;
|
||||
bool flag{};
|
||||
|
||||
Edeque e1(Edeque::size_match::GE, 1, [&](){ flag = true; });
|
||||
|
||||
flag = false;
|
||||
e1.clear_trigger();
|
||||
EXPECT_EQ (false, flag);
|
||||
e1.push_back(1); // 1, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
flag = false;
|
||||
e1.clear();
|
||||
e1.clear_trigger();
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e1.push_back(1); // 1
|
||||
e1.push_back(2); // 2
|
||||
e1.set_trigger(Edeque::size_match::GE, 1, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e1.check_trigger(); // manual trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
e1.check_trigger(); // manual trigger attempt
|
||||
EXPECT_EQ (false, flag); // [SIZE triggers are auto clear]
|
||||
|
||||
Edeque e2(Edeque::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
|
||||
flag = false;
|
||||
e2.clear_trigger();
|
||||
EXPECT_EQ (false, flag);
|
||||
e2.push_back(42); // push 42, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
e2.set_trigger(Edeque::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e2.push_back(42); // push 42, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
e2.push_back(42); // push 42, re-trigger [DATA re-triggers]
|
||||
EXPECT_EQ (true, flag);
|
||||
}
|
||||
|
||||
TEST (Tedeque, size_triggers) {
|
||||
using Edeque = edeque<int, 8>;
|
||||
bool flag{};
|
||||
|
||||
// size_match::GE (size()>= 2)
|
||||
Edeque ee(Edeque::size_match::GE, 2, [&](){ flag = true; });
|
||||
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.push_back(1); // 1
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(2); // 2, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.push_back(3); // 3, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
// size_match::GT (size()> 1)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.set_trigger(Edeque::size_match::GT, 1, [&](){ flag = true; });
|
||||
ee.push_back(1); // 1
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(2); // 2, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.push_back(3); // 3, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
// size_match::LE (size()<= 1)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.push_back(1); // 1
|
||||
ee.push_back(2); // 2
|
||||
ee.push_back(3); // 3
|
||||
ee.set_trigger(Edeque::size_match::LE, 1, [&](){ flag = true; });
|
||||
ee.pop_front(); // 2
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.pop_front(); // 1, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.pop_front(); // 0, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
// size_match::LT (size()< 2)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.push_back(1); // 1
|
||||
ee.push_back(2); // 2
|
||||
ee.push_back(3); // 3
|
||||
ee.set_trigger(Edeque::size_match::LT, 2, [&](){ flag = true; });
|
||||
ee.pop_front(); // 2
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.pop_front(); // 1, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.pop_front(); // 0, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
// size_match::EQ (size()== 2)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.set_trigger(Edeque::size_match::EQ, 2, [&](){ flag = true; });
|
||||
ee.push_back(1); // 1
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(2); // 2, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.push_back(3); // 3
|
||||
ee.pop_front(); // 2, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
// size_match::NE (size()!= 0)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.set_trigger(Edeque::size_match::NE, 0, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(1); // 1, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.push_back(2); // 2, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
}
|
||||
|
||||
TEST (Tedeque, data_triggers) {
|
||||
using Edeque = edeque<int, 8>;
|
||||
bool flag{};
|
||||
|
||||
// data_match::MATCH_PUSH (item == 42)
|
||||
Edeque ee(Edeque::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
|
||||
|
||||
flag = false;
|
||||
ee.push_back(7); // 7
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(42); // push:42, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
ee.pop_back(); // pop:42, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(42); // push:42, re-trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
// data_match::MATCH_POP (item == 42)
|
||||
flag = false;
|
||||
ee.clear_trigger();
|
||||
ee.set_trigger(Edeque::data_match::MATCH_POP, 42, [&](){ flag = true; });
|
||||
ee.push_back(7); // 7
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(42); // push:42, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.pop_back(); // pop:42, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
// data_match::MISMATCH_PUSH (item != 42)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.clear_trigger();
|
||||
ee.push_back(7); // 7
|
||||
ee.set_trigger(Edeque::data_match::MISMATCH_PUSH, 42, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
ee.push_back(42); // 42, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(0); // 0, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
ee.push_back(1); // 1, re-trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
// data_match::MISMATCH_POP (item != 42)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.clear_trigger();
|
||||
ee.push_back(7); // ->7
|
||||
ee.pop_back(); // <-7
|
||||
ee.set_trigger(Edeque::data_match::MISMATCH_POP, 42, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
ee.push_back(42); // ->42, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(0); // ->0, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.pop_back(); // pop:0, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
ee.push_back(0);
|
||||
ee.pop_back(); // pop:0, re-trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
}
|
||||
|
||||
// atomic
|
||||
TEST (Tedeque, construct_atomic) {
|
||||
using Edeque = edeque<int, 8, true>;
|
||||
struct T { int a,b; };
|
||||
int local{};
|
||||
|
||||
Edeque e1(Edeque::size_match::GE, 3, [](){
|
||||
++global_flag;
|
||||
});
|
||||
Edeque e2(Edeque::size_match::GE, 3, [&](){
|
||||
++local;
|
||||
});
|
||||
Edeque e3(Edeque::size_match::EQ, 7, vfun);
|
||||
edeque<T, 8> e4(edeque<T, 8>::size_match::EQ, 2, vfoo{});
|
||||
|
||||
edeque<int, 8, true> q1;
|
||||
edeque<int, 8, true> q2(edeque<int, 8, true>::size_match::DISABLED, 0, nullptr);
|
||||
|
||||
EXPECT_EQ (8UL, e1.capacity());
|
||||
EXPECT_EQ (8UL, e2.capacity());
|
||||
EXPECT_EQ (8UL, e3.capacity());
|
||||
EXPECT_EQ (8UL, e4.capacity());
|
||||
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q2.capacity());
|
||||
}
|
||||
|
||||
TEST (Tedeque, base_class_atomic) {
|
||||
using Edeque = edeque<int, 8, true>;
|
||||
|
||||
Edeque e1(Edeque::size_match::GE, 3, [](){
|
||||
++global_flag;
|
||||
});
|
||||
|
||||
// Access of base class functionality
|
||||
EXPECT_EQ (8UL, e1.capacity());
|
||||
EXPECT_EQ (0UL, e1.size());
|
||||
EXPECT_EQ (true, e1.empty());
|
||||
EXPECT_EQ (false, e1.full());
|
||||
|
||||
e1.push_back(7);
|
||||
EXPECT_EQ (7, e1.front());
|
||||
EXPECT_EQ (7, e1.back());
|
||||
EXPECT_EQ (7, e1.pop_front());
|
||||
|
||||
e1.push_front(42);
|
||||
EXPECT_EQ (42, e1.front());
|
||||
EXPECT_EQ (42, e1.back());
|
||||
EXPECT_EQ (42, e1.pop_back());
|
||||
|
||||
e1.push_back(1);
|
||||
e1.push_back(2);
|
||||
e1.push_back(3);
|
||||
|
||||
int check_it=1;
|
||||
for (auto it = e1.begin() ; it != e1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(4, check_it); // run through all
|
||||
}
|
||||
|
||||
TEST (Tedeque, set_clear_check_trigger_atomic) {
|
||||
using Edeque = edeque<int, 8, true>;
|
||||
bool flag{};
|
||||
|
||||
Edeque e1(Edeque::size_match::GE, 1, [&](){ flag = true; });
|
||||
|
||||
flag = false;
|
||||
e1.clear_trigger();
|
||||
EXPECT_EQ (false, flag);
|
||||
e1.push_back(1); // 1, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
flag = false;
|
||||
e1.clear();
|
||||
e1.clear_trigger();
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e1.push_back(1); // 1
|
||||
e1.push_back(2); // 2
|
||||
e1.set_trigger(Edeque::size_match::GE, 1, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e1.check_trigger(); // manual trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
e1.check_trigger(); // manual trigger attempt
|
||||
EXPECT_EQ (false, flag); // [SIZE triggers are auto clear]
|
||||
|
||||
Edeque e2(Edeque::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
|
||||
flag = false;
|
||||
e2.clear_trigger();
|
||||
EXPECT_EQ (false, flag);
|
||||
e2.push_back(42); // push 42, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
e2.set_trigger(Edeque::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e2.push_back(42); // push 42, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
e2.push_back(42); // push 42, re-trigger [DATA re-triggers]
|
||||
EXPECT_EQ (true, flag);
|
||||
}
|
||||
|
||||
TEST (Tedeque, size_triggers_atomic) {
|
||||
using Edeque = edeque<int, 8, true>;
|
||||
bool flag{};
|
||||
|
||||
// size_match::GE (size()>= 2)
|
||||
Edeque ee(Edeque::size_match::GE, 2, [&](){ flag = true; });
|
||||
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.push_back(1); // 1
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(2); // 2, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.push_back(3); // 3, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
// size_match::GT (size()> 1)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.set_trigger(Edeque::size_match::GT, 1, [&](){ flag = true; });
|
||||
ee.push_back(1); // 1
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(2); // 2, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.push_back(3); // 3, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
// size_match::LE (size()<= 1)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.push_back(1); // 1
|
||||
ee.push_back(2); // 2
|
||||
ee.push_back(3); // 3
|
||||
ee.set_trigger(Edeque::size_match::LE, 1, [&](){ flag = true; });
|
||||
ee.pop_front(); // 2
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.pop_front(); // 1, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.pop_front(); // 0, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
// size_match::LT (size()< 2)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.push_back(1); // 1
|
||||
ee.push_back(2); // 2
|
||||
ee.push_back(3); // 3
|
||||
ee.set_trigger(Edeque::size_match::LT, 2, [&](){ flag = true; });
|
||||
ee.pop_front(); // 2
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.pop_front(); // 1, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.pop_front(); // 0, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
// size_match::EQ (size()== 2)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.set_trigger(Edeque::size_match::EQ, 2, [&](){ flag = true; });
|
||||
ee.push_back(1); // 1
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(2); // 2, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.push_back(3); // 3
|
||||
ee.pop_front(); // 2, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
// size_match::NE (size()!= 0)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.set_trigger(Edeque::size_match::NE, 0, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(1); // 1, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
flag = false;
|
||||
ee.push_back(2); // 2, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
}
|
||||
|
||||
TEST (Tedeque, data_triggers_atomic) {
|
||||
using Edeque = edeque<int, 8, true>;
|
||||
bool flag{};
|
||||
|
||||
// data_match::MATCH_PUSH (item == 42)
|
||||
Edeque ee(Edeque::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
|
||||
|
||||
flag = false;
|
||||
ee.push_back(7); // 7
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(42); // push:42, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
ee.pop_back(); // pop:42, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(42); // push:42, re-trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
// data_match::MATCH_POP (item == 42)
|
||||
flag = false;
|
||||
ee.clear_trigger();
|
||||
ee.set_trigger(Edeque::data_match::MATCH_POP, 42, [&](){ flag = true; });
|
||||
ee.push_back(7); // 7
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(42); // push:42, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.pop_back(); // pop:42, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
// data_match::MISMATCH_PUSH (item != 42)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.clear_trigger();
|
||||
ee.push_back(7); // 7
|
||||
ee.set_trigger(Edeque::data_match::MISMATCH_PUSH, 42, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
ee.push_back(42); // 42, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(0); // 0, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
ee.push_back(1); // 1, re-trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
// data_match::MISMATCH_POP (item != 42)
|
||||
flag = false;
|
||||
ee.clear();
|
||||
ee.clear_trigger();
|
||||
ee.push_back(7); // ->7
|
||||
ee.pop_back(); // <-7
|
||||
ee.set_trigger(Edeque::data_match::MISMATCH_POP, 42, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
ee.push_back(42); // ->42, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.push_back(0); // ->0, no-trigger
|
||||
EXPECT_EQ (false, flag);
|
||||
ee.pop_back(); // pop:0, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
ee.push_back(0);
|
||||
ee.pop_back(); // pop:0, re-trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
}
|
||||
}
|
||||
317
test/tests/equeue.cpp
Normal file
317
test/tests/equeue.cpp
Normal file
@ -0,0 +1,317 @@
|
||||
/*!
|
||||
* \file equeue.cpp
|
||||
* \brief
|
||||
* Unit tests for equeue
|
||||
*
|
||||
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*
|
||||
*/
|
||||
#include <utl/container/equeue.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace Tequeue {
|
||||
using namespace utl;
|
||||
|
||||
int global_flag =0;
|
||||
|
||||
// Callable mocks
|
||||
void vfun(void) { ++global_flag; }
|
||||
|
||||
struct vfoo {
|
||||
void operator() (void) { ++global_flag; }
|
||||
};
|
||||
|
||||
// Test construction
|
||||
TEST(Tequeue, contruct) {
|
||||
using Equeue = equeue<int, 8>;
|
||||
struct T { int a,b; };
|
||||
int local{};
|
||||
|
||||
Equeue e1(Equeue::size_match::GE, 3, [](){
|
||||
++global_flag;
|
||||
});
|
||||
Equeue e2(Equeue::size_match::GE, 3, [&](){
|
||||
++local;
|
||||
});
|
||||
Equeue e3(Equeue::size_match::EQ, 7, vfun);
|
||||
equeue<T, 8> e4(equeue<T, 8>::size_match::EQ, 2, vfoo{});
|
||||
|
||||
equeue<int, 8> q1;
|
||||
equeue<int, 8> q2(equeue<int, 8>::size_match::DISABLED, 0, nullptr);
|
||||
|
||||
EXPECT_EQ (8UL, e1.capacity());
|
||||
EXPECT_EQ (8UL, e2.capacity());
|
||||
EXPECT_EQ (8UL, e3.capacity());
|
||||
EXPECT_EQ (8UL, e4.capacity());
|
||||
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q2.capacity());
|
||||
}
|
||||
|
||||
// simple push-pop functionality
|
||||
TEST(Tequeue, base_class) {
|
||||
using Equeue = equeue<int, 8>;
|
||||
|
||||
Equeue e1(Equeue::size_match::GE, 3, [](){
|
||||
++global_flag;
|
||||
});
|
||||
|
||||
// Access of base class functionality
|
||||
EXPECT_EQ (8UL, e1.capacity());
|
||||
EXPECT_EQ (0UL, e1.size());
|
||||
EXPECT_EQ (true, e1.empty());
|
||||
EXPECT_EQ (false, e1.full());
|
||||
|
||||
e1.push(42);
|
||||
EXPECT_EQ (42, e1.front());
|
||||
EXPECT_EQ (42, e1.back());
|
||||
EXPECT_EQ (42, e1.pop());
|
||||
|
||||
e1.push(1);
|
||||
e1.push(2);
|
||||
e1.push(3);
|
||||
|
||||
int check_it=1;
|
||||
for (auto it = e1.begin() ; it != e1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(4, check_it); // run through all
|
||||
|
||||
}
|
||||
|
||||
// trigger functionality
|
||||
TEST (Tequeue, set_clear_check_trigger) {
|
||||
using Equeue = equeue<int, 8>;
|
||||
bool flag{};
|
||||
|
||||
Equeue e1(Equeue::size_match::GE, 1, [&](){ flag = true; });
|
||||
|
||||
flag = false;
|
||||
e1.clear_trigger();
|
||||
EXPECT_EQ (false, flag);
|
||||
e1.push_back(1); // 1, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
flag = false;
|
||||
e1.clear();
|
||||
e1.clear_trigger();
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e1.push_back(1); // 1
|
||||
e1.push_back(2); // 2
|
||||
e1.set_trigger(Equeue::size_match::GE, 1, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e1.check_trigger(); // manual trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
e1.check_trigger(); // manual trigger attempt
|
||||
EXPECT_EQ (false, flag); // [SIZE triggers are auto clear]
|
||||
|
||||
Equeue e2(Equeue::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
|
||||
flag = false;
|
||||
e2.clear_trigger();
|
||||
EXPECT_EQ (false, flag);
|
||||
e2.push_back(42); // push 42, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
e2.set_trigger(Equeue::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e2.push_back(42); // push 42, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
e2.push_back(42); // push 42, re-trigger [DATA re-triggers]
|
||||
EXPECT_EQ (true, flag);
|
||||
}
|
||||
|
||||
// stream push-pop
|
||||
TEST(Tequeue, stream_push_pop) {
|
||||
equeue<int, 8> q1;
|
||||
|
||||
q1 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8;
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (true, q1.full());
|
||||
|
||||
q1 << 9; // try to insert in full queue
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (true, q1.full());
|
||||
|
||||
int check_it=1;
|
||||
for (auto it = q1.begin() ; it != q1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(9, check_it); // run through all
|
||||
|
||||
for (int i =1 ; i <= 8 ; ++i) {
|
||||
check_it << q1;
|
||||
EXPECT_EQ(i, check_it);
|
||||
}
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
q1 >> check_it;
|
||||
EXPECT_EQ (int{}, check_it);
|
||||
}
|
||||
|
||||
// atomic
|
||||
// Test construction
|
||||
TEST(Tequeue, contruct_atomic) {
|
||||
using Equeue = equeue<int, 8, true>;
|
||||
struct T { int a,b; };
|
||||
int local{};
|
||||
|
||||
Equeue e1(Equeue::size_match::GE, 3, [](){
|
||||
++global_flag;
|
||||
});
|
||||
Equeue e2(Equeue::size_match::GE, 3, [&](){
|
||||
++local;
|
||||
});
|
||||
Equeue e3(Equeue::size_match::EQ, 7, vfun);
|
||||
equeue<T, 8> e4(equeue<T, 8>::size_match::EQ, 2, vfoo{});
|
||||
|
||||
equeue<int, 8, true> q1;
|
||||
equeue<int, 8, true> q2(equeue<int, 8, true>::size_match::DISABLED, 0, nullptr);
|
||||
|
||||
EXPECT_EQ (8UL, e1.capacity());
|
||||
EXPECT_EQ (8UL, e2.capacity());
|
||||
EXPECT_EQ (8UL, e3.capacity());
|
||||
EXPECT_EQ (8UL, e4.capacity());
|
||||
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q2.capacity());
|
||||
}
|
||||
|
||||
// simple push-pop functionality
|
||||
TEST(Tequeue, base_class_atomic) {
|
||||
using Equeue = equeue<int, 8, true>;
|
||||
|
||||
Equeue e1(Equeue::size_match::GE, 3, [](){
|
||||
++global_flag;
|
||||
});
|
||||
|
||||
// Access of base class functionality
|
||||
EXPECT_EQ (8UL, e1.capacity());
|
||||
EXPECT_EQ (0UL, e1.size());
|
||||
EXPECT_EQ (true, e1.empty());
|
||||
EXPECT_EQ (false, e1.full());
|
||||
|
||||
e1.push(42);
|
||||
EXPECT_EQ (42, e1.front());
|
||||
EXPECT_EQ (42, e1.back());
|
||||
EXPECT_EQ (42, e1.pop());
|
||||
|
||||
e1.push(1);
|
||||
e1.push(2);
|
||||
e1.push(3);
|
||||
|
||||
int check_it=1;
|
||||
for (auto it = e1.begin() ; it != e1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(4, check_it); // run through all
|
||||
|
||||
}
|
||||
|
||||
// trigger functionality
|
||||
TEST (Tequeue, set_clear_check_trigger_atomic) {
|
||||
using Equeue = equeue<int, 8, true>;
|
||||
bool flag{};
|
||||
|
||||
Equeue e1(Equeue::size_match::GE, 1, [&](){ flag = true; });
|
||||
|
||||
flag = false;
|
||||
e1.clear_trigger();
|
||||
EXPECT_EQ (false, flag);
|
||||
e1.push_back(1); // 1, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
|
||||
flag = false;
|
||||
e1.clear();
|
||||
e1.clear_trigger();
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e1.push_back(1); // 1
|
||||
e1.push_back(2); // 2
|
||||
e1.set_trigger(Equeue::size_match::GE, 1, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e1.check_trigger(); // manual trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
e1.check_trigger(); // manual trigger attempt
|
||||
EXPECT_EQ (false, flag); // [SIZE triggers are auto clear]
|
||||
|
||||
Equeue e2(Equeue::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
|
||||
flag = false;
|
||||
e2.clear_trigger();
|
||||
EXPECT_EQ (false, flag);
|
||||
e2.push_back(42); // push 42, no-trigger cleared
|
||||
EXPECT_EQ (false, flag);
|
||||
e2.set_trigger(Equeue::data_match::MATCH_PUSH, 42, [&](){ flag = true; });
|
||||
EXPECT_EQ (false, flag); // no spurious triggers
|
||||
e2.push_back(42); // push 42, trigger
|
||||
EXPECT_EQ (true, flag);
|
||||
|
||||
flag = false;
|
||||
e2.push_back(42); // push 42, re-trigger [DATA re-triggers]
|
||||
EXPECT_EQ (true, flag);
|
||||
}
|
||||
|
||||
// stream push-pop
|
||||
TEST(Tequeue, stream_push_pop_atomic) {
|
||||
equeue<int, 8, true> q1;
|
||||
|
||||
q1 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8;
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (true, q1.full());
|
||||
|
||||
q1 << 9; // try to insert in full queue
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (true, q1.full());
|
||||
|
||||
int check_it=1;
|
||||
for (auto it = q1.begin() ; it != q1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(9, check_it); // run through all
|
||||
|
||||
for (int i =1 ; i <= 8 ; ++i) {
|
||||
check_it << q1;
|
||||
EXPECT_EQ(i, check_it);
|
||||
}
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
q1 >> check_it;
|
||||
EXPECT_EQ (int{}, check_it);
|
||||
}
|
||||
|
||||
}
|
||||
188
test/tests/queue.cpp
Normal file
188
test/tests/queue.cpp
Normal file
@ -0,0 +1,188 @@
|
||||
/*!
|
||||
* \file queue.cpp
|
||||
* \brief
|
||||
* Unit tests for queue
|
||||
*
|
||||
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*
|
||||
*/
|
||||
#include <utl/container/queue.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace Tqueue {
|
||||
using namespace utl;
|
||||
|
||||
|
||||
// Test construction
|
||||
TEST(Tqueue, contruct) {
|
||||
queue<int, 8> q1;
|
||||
queue<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
queue<int, 8> q3{1, 2, 3, 4, 5};
|
||||
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (8UL, q2.capacity());
|
||||
EXPECT_EQ (8UL, q2.size());
|
||||
EXPECT_EQ (8UL, q3.capacity());
|
||||
EXPECT_EQ (5UL, q3.size());
|
||||
}
|
||||
|
||||
// base class functionality check
|
||||
TEST(Tqueue, base_class) {
|
||||
|
||||
queue<int, 8> q1;
|
||||
|
||||
// Access of base class functionality
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
q1.push(42);
|
||||
EXPECT_EQ (42, q1.front());
|
||||
EXPECT_EQ (42, q1.back());
|
||||
EXPECT_EQ (42, q1.pop());
|
||||
|
||||
q1.push(1);
|
||||
q1.push(2);
|
||||
q1.push(3);
|
||||
|
||||
int check_it=1;
|
||||
for (auto it = q1.begin() ; it != q1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(4, check_it); // run through all
|
||||
}
|
||||
|
||||
// stream push-pop
|
||||
TEST(Tqueue, stream_push_pop) {
|
||||
queue<int, 8> q1;
|
||||
|
||||
q1 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8;
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (true, q1.full());
|
||||
|
||||
q1 << 9; // try to insert in full queue
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (true, q1.full());
|
||||
|
||||
int check_it=1;
|
||||
for (auto it = q1.begin() ; it != q1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(9, check_it); // run through all
|
||||
|
||||
for (int i =1 ; i <= 8 ; ++i) {
|
||||
check_it << q1;
|
||||
EXPECT_EQ(i, check_it);
|
||||
}
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
q1 >> check_it;
|
||||
EXPECT_EQ (int{}, check_it);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Test construction
|
||||
TEST(Tqueue, contruct_atomic) {
|
||||
queue<int, 8, true> q1;
|
||||
queue<int, 8, true> q2{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
queue<int, 8, true> q3{1, 2, 3, 4, 5};
|
||||
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (8UL, q2.capacity());
|
||||
EXPECT_EQ (8UL, q2.size());
|
||||
EXPECT_EQ (8UL, q3.capacity());
|
||||
EXPECT_EQ (5UL, q3.size());
|
||||
}
|
||||
|
||||
// base class functionality check
|
||||
TEST(Tqueue, base_class_atomic) {
|
||||
|
||||
queue<int, 8, true> q1;
|
||||
|
||||
// Access of base class functionality
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
q1.push(42);
|
||||
EXPECT_EQ (42, q1.front());
|
||||
EXPECT_EQ (42, q1.back());
|
||||
EXPECT_EQ (42, q1.pop());
|
||||
|
||||
q1.push(1);
|
||||
q1.push(2);
|
||||
q1.push(3);
|
||||
|
||||
int check_it=1;
|
||||
for (auto it = q1.begin() ; it != q1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(4, check_it); // run through all
|
||||
}
|
||||
|
||||
// stream push-pop
|
||||
TEST(Tqueue, stream_push_pop_atomic) {
|
||||
queue<int, 8, true> q1;
|
||||
|
||||
q1 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8;
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (true, q1.full());
|
||||
|
||||
q1 << 9; // try to insert in full queue
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (8UL, q1.size());
|
||||
EXPECT_EQ (false, q1.empty());
|
||||
EXPECT_EQ (true, q1.full());
|
||||
|
||||
int check_it=1;
|
||||
for (auto it = q1.begin() ; it != q1.end() ; ++it)
|
||||
EXPECT_EQ(*it, check_it++);
|
||||
EXPECT_EQ(9, check_it); // run through all
|
||||
|
||||
for (int i =1 ; i <= 8 ; ++i) {
|
||||
check_it << q1;
|
||||
EXPECT_EQ(i, check_it);
|
||||
}
|
||||
EXPECT_EQ (8UL, q1.capacity());
|
||||
EXPECT_EQ (0UL, q1.size());
|
||||
EXPECT_EQ (true, q1.empty());
|
||||
EXPECT_EQ (false, q1.full());
|
||||
|
||||
q1 >> check_it;
|
||||
EXPECT_EQ (int{}, check_it);
|
||||
}
|
||||
|
||||
}
|
||||
424
test/tests/ring_iterator.cpp
Normal file
424
test/tests/ring_iterator.cpp
Normal file
@ -0,0 +1,424 @@
|
||||
/*!
|
||||
* \file ring_iterator.cpp
|
||||
* \brief
|
||||
* Unit tests for ring_iterator
|
||||
*
|
||||
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*
|
||||
*/
|
||||
#include <utl/container/ring_iterator.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <array>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Tring_iterator {
|
||||
using namespace utl;
|
||||
|
||||
// Test construction
|
||||
TEST(Tring_iterator, construct) {
|
||||
int A[10];
|
||||
|
||||
//default constructor
|
||||
ring_iterator<int*, 10> i1;
|
||||
EXPECT_EQ(nullptr, i1.base());
|
||||
EXPECT_EQ(nullptr, i1.iter());
|
||||
EXPECT_EQ(10UL, i1.size());
|
||||
|
||||
// implementation specific (you can remove it freely)
|
||||
EXPECT_EQ(2*sizeof(int*), sizeof(i1));
|
||||
|
||||
// basic
|
||||
ring_iterator<int*, 10> i2(A);
|
||||
EXPECT_EQ(A, i2.base());
|
||||
EXPECT_EQ(A, i2.iter());
|
||||
EXPECT_EQ(10UL, i2.size());
|
||||
|
||||
// basic from assignment
|
||||
ring_iterator<int*, 10> i3 = A;
|
||||
EXPECT_EQ(A, i3.base());
|
||||
EXPECT_EQ(A, i3.iter());
|
||||
EXPECT_EQ(10UL, i3.size());
|
||||
|
||||
// basic with offset
|
||||
ring_iterator<int*, 10> i4(A, 5);
|
||||
EXPECT_EQ(A, i4.base());
|
||||
EXPECT_EQ(&A[5], i4.iter());
|
||||
EXPECT_EQ(10UL, i4.size());
|
||||
|
||||
// copy (Legacy iterator)
|
||||
auto i5 = i2;
|
||||
EXPECT_EQ(A, i5.base());
|
||||
EXPECT_EQ(A, i5.iter());
|
||||
EXPECT_EQ(10UL, i5.size());
|
||||
|
||||
// arbitrary type
|
||||
struct TT { int a,b,c; };
|
||||
std::array<TT, 10> t;
|
||||
ring_iterator<TT*, 10> it(t.data(), 2);
|
||||
EXPECT_EQ(t.begin(), it.base());
|
||||
EXPECT_EQ(&t[2], it.iter());
|
||||
EXPECT_EQ(10UL, it.size());
|
||||
}
|
||||
|
||||
// Legacy iterator
|
||||
TEST(Tring_iterator, LegacyIterator) {
|
||||
|
||||
EXPECT_EQ(true, (std::is_same<int, typename ring_iterator<int*, 10>::value_type>::value));
|
||||
EXPECT_EQ(true, (std::is_same<std::ptrdiff_t, typename ring_iterator<int*, 10>::difference_type>::value));
|
||||
EXPECT_EQ(true, (std::is_same<int&, typename ring_iterator<int*, 10>::reference>::value));
|
||||
EXPECT_EQ(true, (std::is_same<int*, typename ring_iterator<int*, 10>::pointer>::value));
|
||||
EXPECT_EQ(true, (std::is_same<std::random_access_iterator_tag, typename ring_iterator<int*, 10>::iterator_category>::value));
|
||||
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10> i1(A);
|
||||
|
||||
// copy constructible/assignable
|
||||
auto i2 = i1;
|
||||
EXPECT_EQ(A, i2.base());
|
||||
EXPECT_EQ(A, i2.iter());
|
||||
EXPECT_EQ(10UL, i2.size());
|
||||
|
||||
// dereferenceable - incrementable
|
||||
ring_iterator<int*, 10> i3(A);
|
||||
EXPECT_EQ(true, (std::is_reference<decltype(*i3)>::value));
|
||||
EXPECT_EQ(true, (std::is_same<ring_iterator<int*, 10>&, decltype(++i3)>::value));
|
||||
EXPECT_EQ(true, (std::is_reference<decltype((*i3++))>::value));
|
||||
|
||||
// more practical
|
||||
ring_iterator<int*, 10> i4(A);
|
||||
ring_iterator<int*, 10> i5(A, 9);
|
||||
EXPECT_EQ(A[0], *i4);
|
||||
EXPECT_EQ(&A[1], (++i4).iter());
|
||||
// check loop
|
||||
EXPECT_EQ(A[9], *i5);
|
||||
EXPECT_EQ(&A[0], (++i5).iter());
|
||||
}
|
||||
|
||||
// Legacy input iterator
|
||||
TEST(Tring_iterator, LegacyInputIterator) {
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10> i1(A), i2(A), i3(A, 1);
|
||||
|
||||
struct T { int m; };
|
||||
T B[5] { {0}, {1}, {2}, {3}, {4}};
|
||||
ring_iterator<T*, 5> it(B);
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(i1 == i2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(i1 != i2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<int&, decltype(*i1)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<int, decltype(it->m)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10>&, decltype(++i1)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<int&, decltype(*i1++)>::value));
|
||||
|
||||
// more practical
|
||||
EXPECT_EQ (true, i1 == i2);
|
||||
EXPECT_EQ (true, i1 != i3);
|
||||
EXPECT_EQ (0, *i1);
|
||||
EXPECT_EQ (0, it->m);
|
||||
EXPECT_EQ (true, (++i1 == i3));
|
||||
EXPECT_EQ (1, *i1++);
|
||||
EXPECT_EQ (2, *i1);
|
||||
}
|
||||
|
||||
// Legacy input iterator
|
||||
TEST(Tring_iterator, LegacyOutputIterator) {
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10> it(A);
|
||||
|
||||
EXPECT_EQ (true, (std::is_assignable<decltype(*it), int>::value));
|
||||
EXPECT_EQ (true, (std::is_assignable<decltype(*it++), int>::value));
|
||||
|
||||
// more practical
|
||||
*it = 42;
|
||||
EXPECT_EQ (42, A[0]);
|
||||
*it++ = 7;
|
||||
EXPECT_EQ (7, A[0]);
|
||||
EXPECT_EQ (&A[1], it.iter());
|
||||
}
|
||||
|
||||
// Legacy forward iterator
|
||||
TEST(Tring_iterator, LegacyForwardIterator)
|
||||
{
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10> it(A);
|
||||
|
||||
EXPECT_EQ (0, *it++);
|
||||
EXPECT_EQ (1, *it);
|
||||
}
|
||||
|
||||
// Legacy bidirectional iterator
|
||||
TEST(Tring_iterator, LegacyBidirectionalIterator) {
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10> it(A);
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10>&, decltype(--it)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10>, decltype(it--)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<int&, decltype(*it--)>::value));
|
||||
|
||||
// more practical
|
||||
ring_iterator<int*, 10> i1(A), i2(A, 9);
|
||||
EXPECT_EQ (9, *i2--); // check loop also
|
||||
EXPECT_EQ (8, *i2);
|
||||
EXPECT_EQ (0, *i1--); // check loop also
|
||||
EXPECT_EQ (9, *i1);
|
||||
}
|
||||
|
||||
// Legacy random access iterator
|
||||
TEST(Tring_iterator, LegacyRandomAccessIterator) {
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10> it1(A), it2(A, 7);
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10>&, decltype(it1 += 7)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10>, decltype(it1 + 7)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10>, decltype(7 + it1)>::value));
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10>&, decltype(it1 -= 7)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10>, decltype(it1 - 7)>::value));
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<std::ptrdiff_t, decltype(it1 - it2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<int&, decltype(it1[7])>::value));
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(it1 < it2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(it1 > it2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(it1 <= it2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(it1 >= it2)>::value));
|
||||
|
||||
// more practical
|
||||
ring_iterator<int*, 10> i1(A), i2(A);
|
||||
i1 += 7;
|
||||
EXPECT_EQ (7, *i1);
|
||||
i1 -= 7;
|
||||
EXPECT_EQ (0, *i1);
|
||||
i1 += 11;
|
||||
EXPECT_EQ (1, *i1);
|
||||
i1 -= 2;
|
||||
EXPECT_EQ (9, *i1);
|
||||
|
||||
EXPECT_EQ (7, *(i2+7));
|
||||
EXPECT_EQ (7, *(7+i2));
|
||||
EXPECT_EQ (1, *(i2+11));
|
||||
EXPECT_EQ (1, *(11+i2));
|
||||
EXPECT_EQ (7, *(i1-2));
|
||||
EXPECT_EQ (8, *(i2-2));
|
||||
|
||||
EXPECT_EQ (9, (i1 - i2));
|
||||
EXPECT_EQ (1, (i2 - i1)); // loop
|
||||
|
||||
}
|
||||
|
||||
// Test construction atomic
|
||||
TEST(Tring_iterator, construct_atomic) {
|
||||
int A[10];
|
||||
|
||||
//default constructor
|
||||
ring_iterator<int*, 10, true> i1;
|
||||
EXPECT_EQ(nullptr, i1.base());
|
||||
EXPECT_EQ(nullptr, i1.iter());
|
||||
EXPECT_EQ(10UL, i1.size());
|
||||
|
||||
// implementation specific (you can remove it freely)
|
||||
EXPECT_EQ(2*sizeof(int*), sizeof(i1));
|
||||
|
||||
// basic
|
||||
ring_iterator<int*, 10, true> i2(A);
|
||||
EXPECT_EQ(A, i2.base());
|
||||
EXPECT_EQ(A, i2.iter());
|
||||
EXPECT_EQ(10UL, i2.size());
|
||||
|
||||
// basic from assignment
|
||||
ring_iterator<int*, 10, true> i3 = A;
|
||||
EXPECT_EQ(A, i3.base());
|
||||
EXPECT_EQ(A, i3.iter());
|
||||
EXPECT_EQ(10UL, i3.size());
|
||||
|
||||
// basic with offset
|
||||
ring_iterator<int*, 10, true> i4(A, 5);
|
||||
EXPECT_EQ(A, i4.base());
|
||||
EXPECT_EQ(&A[5], i4.iter());
|
||||
EXPECT_EQ(10UL, i4.size());
|
||||
|
||||
// copy (Legacy iterator)
|
||||
auto i5 = i2;
|
||||
EXPECT_EQ(A, i5.base());
|
||||
EXPECT_EQ(A, i5.iter());
|
||||
EXPECT_EQ(10UL, i5.size());
|
||||
|
||||
// arbitrary type
|
||||
struct TT { int a,b,c; };
|
||||
std::array<TT, 10> t;
|
||||
ring_iterator<TT*, 10, true> it(t.data(), 2);
|
||||
EXPECT_EQ(t.begin(), it.base());
|
||||
EXPECT_EQ(&t[2], it.iter());
|
||||
EXPECT_EQ(10UL, it.size());
|
||||
}
|
||||
|
||||
// Legacy iterator atomic
|
||||
TEST(Tring_iterator, LegacyIterator_atomic) {
|
||||
|
||||
EXPECT_EQ(true, (std::is_same<int, typename ring_iterator<int*, 10, true>::value_type>::value));
|
||||
EXPECT_EQ(true, (std::is_same<std::ptrdiff_t, typename ring_iterator<int*, 10, true>::difference_type>::value));
|
||||
EXPECT_EQ(true, (std::is_same<int&, typename ring_iterator<int*, 10, true>::reference>::value));
|
||||
EXPECT_EQ(true, (std::is_same<int*, typename ring_iterator<int*, 10, true>::pointer>::value));
|
||||
EXPECT_EQ(true, (std::is_same<std::random_access_iterator_tag, typename ring_iterator<int*, 10, true>::iterator_category>::value));
|
||||
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10, true> i1(A);
|
||||
|
||||
// copy constructible/assignable
|
||||
auto i2 = i1;
|
||||
EXPECT_EQ(A, i2.base());
|
||||
EXPECT_EQ(A, i2.iter());
|
||||
EXPECT_EQ(10UL, i2.size());
|
||||
|
||||
// dereferenceable - incrementable
|
||||
ring_iterator<int*, 10, true> i3(A);
|
||||
EXPECT_EQ(true, (std::is_reference<decltype(*i3)>::value));
|
||||
EXPECT_EQ(true, (std::is_same<ring_iterator<int*, 10, true>&, decltype(++i3)>::value));
|
||||
EXPECT_EQ(true, (std::is_reference<decltype((*i3++))>::value));
|
||||
|
||||
// more practical
|
||||
ring_iterator<int*, 10, true> i4(A);
|
||||
ring_iterator<int*, 10, true> i5(A, 9);
|
||||
EXPECT_EQ(A[0], *i4);
|
||||
EXPECT_EQ(&A[1], (++i4).iter());
|
||||
// check loop
|
||||
EXPECT_EQ(A[9], *i5);
|
||||
EXPECT_EQ(&A[0], (++i5).iter());
|
||||
}
|
||||
|
||||
// Legacy input iterator atomic
|
||||
TEST(Tring_iterator, LegacyInputIterator_atomic) {
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10, true> i1(A), i2(A), i3(A, 1);
|
||||
|
||||
struct T { int m; };
|
||||
T B[5] { {0}, {1}, {2}, {3}, {4}};
|
||||
ring_iterator<T*, 5, true> it(B);
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(i1 == i2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(i1 != i2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<int&, decltype(*i1)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<int, decltype(it->m)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10, true>&, decltype(++i1)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<int&, decltype(*i1++)>::value));
|
||||
|
||||
// more practical
|
||||
EXPECT_EQ (true, i1 == i2);
|
||||
EXPECT_EQ (true, i1 != i3);
|
||||
EXPECT_EQ (0, *i1);
|
||||
EXPECT_EQ (0, it->m);
|
||||
EXPECT_EQ (true, (++i1 == i3));
|
||||
EXPECT_EQ (1, *i1++);
|
||||
EXPECT_EQ (2, *i1);
|
||||
}
|
||||
|
||||
// Legacy input iterator atomic
|
||||
TEST(Tring_iterator, LegacyOutputIterator_atomic) {
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10, true> it(A);
|
||||
|
||||
EXPECT_EQ (true, (std::is_assignable<decltype(*it), int>::value));
|
||||
EXPECT_EQ (true, (std::is_assignable<decltype(*it++), int>::value));
|
||||
|
||||
// more practical
|
||||
*it = 42;
|
||||
EXPECT_EQ (42, A[0]);
|
||||
*it++ = 7;
|
||||
EXPECT_EQ (7, A[0]);
|
||||
EXPECT_EQ (&A[1], it.iter());
|
||||
}
|
||||
|
||||
// Legacy forward iterator atomic
|
||||
TEST(Tring_iterator, LegacyForwardIterator_atomic)
|
||||
{
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10, true> it(A);
|
||||
|
||||
EXPECT_EQ (0, *it++);
|
||||
EXPECT_EQ (1, *it);
|
||||
}
|
||||
|
||||
// Legacy bidirectional iterator atomic
|
||||
TEST(Tring_iterator, LegacyBidirectionalIterator_atomic) {
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10, true> it(A);
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10, true>&, decltype(--it)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10, true>, decltype(it--)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<int&, decltype(*it--)>::value));
|
||||
|
||||
// more practical
|
||||
ring_iterator<int*, 10, true> i1(A), i2(A, 9);
|
||||
EXPECT_EQ (9, *i2--); // check loop also
|
||||
EXPECT_EQ (8, *i2);
|
||||
EXPECT_EQ (0, *i1--); // check loop also
|
||||
EXPECT_EQ (9, *i1);
|
||||
}
|
||||
|
||||
// Legacy random access iterator atomic
|
||||
TEST(Tring_iterator, LegacyRandomAccessIterator_atomic) {
|
||||
int A[10] {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
|
||||
ring_iterator<int*, 10, true> it1(A), it2(A, 7);
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10, true>&, decltype(it1 += 7)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10, true>, decltype(it1 + 7)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10, true>, decltype(7 + it1)>::value));
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10, true>&, decltype(it1 -= 7)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<ring_iterator<int*, 10, true>, decltype(it1 - 7)>::value));
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<std::ptrdiff_t, decltype(it1 - it2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<int&, decltype(it1[7])>::value));
|
||||
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(it1 < it2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(it1 > it2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(it1 <= it2)>::value));
|
||||
EXPECT_EQ (true, (std::is_same<bool, decltype(it1 >= it2)>::value));
|
||||
|
||||
// more practical
|
||||
ring_iterator<int*, 10, true> i1(A), i2(A);
|
||||
i1 += 7;
|
||||
EXPECT_EQ (7, *i1);
|
||||
i1 -= 7;
|
||||
EXPECT_EQ (0, *i1);
|
||||
i1 += 11;
|
||||
EXPECT_EQ (1, *i1);
|
||||
i1 -= 2;
|
||||
EXPECT_EQ (9, *i1);
|
||||
|
||||
EXPECT_EQ (7, *(i2+7));
|
||||
EXPECT_EQ (7, *(7+i2));
|
||||
EXPECT_EQ (1, *(i2+11));
|
||||
EXPECT_EQ (1, *(11+i2));
|
||||
EXPECT_EQ (7, *(i1-2));
|
||||
EXPECT_EQ (8, *(i2-2));
|
||||
|
||||
EXPECT_EQ (9, (i1 - i2));
|
||||
EXPECT_EQ (1, (i2 - i1)); // loop
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
451
test/tests/sequencer.cpp
Normal file
451
test/tests/sequencer.cpp
Normal file
@ -0,0 +1,451 @@
|
||||
/*!
|
||||
* \file sequencer.cpp
|
||||
*
|
||||
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
|
||||
*
|
||||
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* </dd></dl>
|
||||
*
|
||||
*/
|
||||
#include <utl/dev/sequencer.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <type_traits>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
|
||||
namespace test_sequencer {
|
||||
using namespace utl;
|
||||
|
||||
// test settings
|
||||
using data_type = char;
|
||||
constexpr size_t size = 64;
|
||||
|
||||
// Sequencer implementer mock
|
||||
class Seq : public sequencer<Seq, data_type, size> {
|
||||
static constexpr int NrCommands =5;
|
||||
static constexpr int NoCommand =-1;
|
||||
|
||||
std::array<const char*, NrCommands> command = {
|
||||
"cmd1",
|
||||
"cmd2\n",
|
||||
"cmd3\r\n",
|
||||
"cmd4\n\r",
|
||||
"cmd5\n",
|
||||
};
|
||||
std::array<const char*, NrCommands> reply {
|
||||
"reply1",
|
||||
"reply2\n",
|
||||
"reply3\n text \r text text\n",
|
||||
"reply4\n text\n text \r text\r\n",
|
||||
"reply5\n",
|
||||
};
|
||||
int cmd =NoCommand;
|
||||
clock_t t =0;
|
||||
|
||||
public:
|
||||
size_t get(char* data) {
|
||||
static int ans = 0;
|
||||
if ((++ans % 3) == 0)
|
||||
return 0;
|
||||
|
||||
if (cmd == NoCommand) {
|
||||
std::strcpy(data, "ERROR\n");
|
||||
return 6;
|
||||
} else {
|
||||
std::strcpy(data, reply[cmd]);
|
||||
size_t s = std::strlen(reply[cmd]);
|
||||
cmd =NoCommand;
|
||||
return s;
|
||||
}
|
||||
}
|
||||
size_t contents (char* data) {
|
||||
if (cmd == NoCommand) {
|
||||
std::strcpy(data, "");
|
||||
return 0;
|
||||
} else {
|
||||
std::strcpy(data, reply[cmd]);
|
||||
return std::strlen(reply[cmd]);
|
||||
}
|
||||
}
|
||||
size_t put (const char* data, size_t n) {
|
||||
for (size_t i =0 ; i<NrCommands ; ++i) {
|
||||
if (!std::strcmp(data, command[i])) {
|
||||
cmd =i;
|
||||
return n;
|
||||
}
|
||||
}
|
||||
cmd =NoCommand;
|
||||
return n;
|
||||
}
|
||||
clock_t clock() { return ++t; }
|
||||
void clear_clock() { t =0; }
|
||||
};
|
||||
|
||||
/*
|
||||
* Test sequencer object
|
||||
*/
|
||||
TEST (Tsequencer, traits) {
|
||||
EXPECT_EQ ( std::is_default_constructible<Seq>::value, true);
|
||||
EXPECT_EQ ( std::is_nothrow_default_constructible<Seq>::value, true);
|
||||
EXPECT_EQ (!std::is_copy_constructible<Seq>::value, true);
|
||||
EXPECT_EQ (!std::is_copy_assignable<Seq>::value, true);
|
||||
|
||||
EXPECT_EQ ((std::is_same_v<Seq::value_type, data_type>), true);
|
||||
EXPECT_EQ ((std::is_same_v<Seq::pointer_type, data_type*>), true);
|
||||
EXPECT_EQ ((std::is_same_v<Seq::size_type, size_t>), true);
|
||||
EXPECT_EQ ((std::is_same_v<Seq::string_view, std::basic_string_view<data_type>>), true);
|
||||
|
||||
Seq s;
|
||||
EXPECT_EQ (s.size(), size);
|
||||
}
|
||||
|
||||
TEST (Tsequencer, predicates) {
|
||||
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::equals), Seq::string_view, Seq::string_view>::value), true);
|
||||
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::starts_with), Seq::string_view, Seq::string_view>::value), true);
|
||||
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::ends_with), Seq::string_view, Seq::string_view>::value), true);
|
||||
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::contains), Seq::string_view, Seq::string_view>::value), true);
|
||||
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::always_true), Seq::string_view, Seq::string_view>::value), true);
|
||||
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::always_false), Seq::string_view, Seq::string_view>::value), true);
|
||||
|
||||
EXPECT_EQ (Seq::nil, nullptr);
|
||||
}
|
||||
|
||||
TEST (Tsequencer, actions) {
|
||||
EXPECT_EQ ( std::is_default_constructible<Seq::action_t>::value, true);
|
||||
EXPECT_EQ ( std::is_nothrow_default_constructible<Seq::action_t>::value, true);
|
||||
EXPECT_EQ ( std::is_copy_constructible<Seq::action_t>::value, true);
|
||||
EXPECT_EQ ( std::is_copy_assignable<Seq::action_t>::value, true);
|
||||
|
||||
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::no_action)>), true);
|
||||
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::next)>), true);
|
||||
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::exit_ok)>), true);
|
||||
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::exit_error)>), true);
|
||||
|
||||
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::go_to<0>)>), true);
|
||||
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::exit<0>)>), true);
|
||||
}
|
||||
|
||||
bool handler_flag = false;
|
||||
const char* text = "abc";
|
||||
|
||||
//static bool check_handle (const str_view_t buffer, const str_view_t token, match_ft match, handler_ft handle)
|
||||
TEST(Tsequencer, check_handle) {
|
||||
Seq s;
|
||||
// foo (5);
|
||||
// bar (5);
|
||||
using str_t = Seq::string_view;
|
||||
using val_t = Seq::value_type;
|
||||
|
||||
auto match = [](const str_t x, const str_t y) ->bool { (void)x; (void)y; return true; };
|
||||
auto no_match = [](const str_t x, const str_t y) ->bool { (void)x; (void)y; return false; };
|
||||
auto check_match = [] (const str_t x, const str_t y) ->bool {
|
||||
return x == y;
|
||||
};
|
||||
auto handler = [](const val_t* v, size_t s){ (void)*v; (void)s; handler_flag = true; };
|
||||
auto set_if_abc = [](const val_t* v, size_t s){
|
||||
(void)*v; (void)s;
|
||||
handler_flag = (str_t(v, s) == "abc");
|
||||
};
|
||||
|
||||
EXPECT_EQ (s.check_handle("", "", nullptr, nullptr), false);
|
||||
EXPECT_EQ (s.check_handle("", "", no_match, nullptr), false);
|
||||
EXPECT_EQ (s.check_handle("", "", match, nullptr), false);
|
||||
|
||||
handler_flag = false;
|
||||
EXPECT_EQ (s.check_handle("", "", no_match, handler), false);
|
||||
EXPECT_EQ (handler_flag, false);
|
||||
|
||||
handler_flag = false;
|
||||
EXPECT_EQ (s.check_handle("", "", match, handler), true);
|
||||
EXPECT_EQ (handler_flag, true);
|
||||
|
||||
handler_flag = false;
|
||||
EXPECT_EQ (s.check_handle("abcd", "abc", check_match, set_if_abc), false);
|
||||
EXPECT_EQ (handler_flag, false);
|
||||
|
||||
handler_flag = false;
|
||||
EXPECT_EQ (s.check_handle("abc", "abc", check_match, set_if_abc), true);
|
||||
EXPECT_EQ (handler_flag, true);
|
||||
|
||||
handler_flag = false;
|
||||
EXPECT_EQ (s.check_handle("abc", "abcd", check_match, set_if_abc), false);
|
||||
EXPECT_EQ (handler_flag, false);
|
||||
|
||||
}
|
||||
|
||||
TEST(Tsequencer, run_nop_and_exits) {
|
||||
Seq s;
|
||||
const Seq::script_t<1> script1 = {{
|
||||
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_ok, 1000}
|
||||
}};
|
||||
s.clear_clock();
|
||||
EXPECT_EQ (s.run(script1), Seq::exit_ok.value);
|
||||
EXPECT_GE (s.clock(), (clock_t)1000);
|
||||
|
||||
const Seq::script_t<1> script2 = {{
|
||||
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_error, 1000}
|
||||
}};
|
||||
s.clear_clock();
|
||||
EXPECT_EQ (s.run(script2), Seq::exit_error.value);
|
||||
EXPECT_GE (s.clock(), (clock_t)1000);
|
||||
|
||||
const Seq::script_t<3> script3 = {{
|
||||
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::next, 1000},
|
||||
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::next, 1000},
|
||||
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_ok, 1000}
|
||||
}};
|
||||
s.clear_clock();
|
||||
EXPECT_EQ (s.run(script3), Seq::exit_ok.value);
|
||||
EXPECT_GE (s.clock(), (clock_t)3000);
|
||||
}
|
||||
|
||||
TEST(Tsequencer, run_send) {
|
||||
Seq s;
|
||||
|
||||
auto send_wrapper = [](const data_type* d, size_t s){
|
||||
(void)*d; (void)s; handler_flag = true;
|
||||
};
|
||||
auto send_chk_text = [](const data_type* d, size_t s){
|
||||
handler_flag = (Seq::string_view(d,s) == Seq::string_view(text));
|
||||
};
|
||||
|
||||
const Seq::script_t<2> script1 = {{
|
||||
{Seq::control_t::SEND, "", Seq::nil, Seq::nil, Seq::next, 0},
|
||||
{Seq::control_t::SEND, "", Seq::nil, send_wrapper, Seq::exit_ok, 0}
|
||||
}};
|
||||
handler_flag =false;
|
||||
EXPECT_EQ (s.run(script1), Seq::exit_ok.value);
|
||||
EXPECT_EQ (handler_flag, true);
|
||||
|
||||
|
||||
const Seq::script_t<1> script2 = {{
|
||||
{Seq::control_t::SEND, "abcd", Seq::nil, send_chk_text, Seq::exit_ok, 0}
|
||||
}};
|
||||
handler_flag =false;
|
||||
EXPECT_EQ (s.run(script2), Seq::exit_ok.value);
|
||||
EXPECT_EQ (handler_flag, false);
|
||||
|
||||
const Seq::script_t<2> script3 = {{
|
||||
{Seq::control_t::SEND, text, Seq::nil, send_chk_text, Seq::exit_ok, 0}
|
||||
}};
|
||||
handler_flag =false;
|
||||
EXPECT_EQ (s.run(script3), Seq::exit_ok.value);
|
||||
EXPECT_EQ (handler_flag, true);
|
||||
}
|
||||
|
||||
TEST(Tsequencer, run_expect) {
|
||||
Seq s;
|
||||
|
||||
const Seq::script_t<7> script = {{
|
||||
{Seq::control_t::EXPECT, "reply1", Seq::equals, Seq::nil, Seq::exit<1UL>, 1000},
|
||||
{Seq::control_t::OR_EXPECT, "reply2", Seq::starts_with, Seq::nil, Seq::exit<2UL>, 0},
|
||||
{Seq::control_t::OR_EXPECT, "reply3", Seq::starts_with, Seq::nil, Seq::exit<3UL>, 0},
|
||||
{Seq::control_t::OR_EXPECT, "reply4\n", Seq::starts_with, Seq::nil, Seq::exit<4UL>, 0},
|
||||
{Seq::control_t::OR_EXPECT, "reply5", Seq::starts_with, Seq::nil, Seq::exit<5UL>, 0},
|
||||
{Seq::control_t::OR_EXPECT, "ERROR", Seq::contains, Seq::nil, Seq::exit<6UL>, 0},
|
||||
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_error, 1000}
|
||||
}};
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd1", std::strlen("cmd1"));
|
||||
EXPECT_EQ (s.run(script), 1UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd2\n", std::strlen("cmd2\n"));
|
||||
EXPECT_EQ (s.run(script), 2UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd3\r\n", std::strlen("cmd3\r\n"));
|
||||
EXPECT_EQ (s.run(script), 3UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd4\n\r", std::strlen("cmd4\n\r"));
|
||||
EXPECT_EQ (s.run(script), 4UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd5\n", std::strlen("cmd5\n"));
|
||||
EXPECT_EQ (s.run(script), 5UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd", std::strlen("cmd"));
|
||||
EXPECT_EQ (s.run(script), 6UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
}
|
||||
|
||||
TEST(Tsequencer, run_detect) {
|
||||
Seq s;
|
||||
|
||||
const Seq::script_t<7> script = {{
|
||||
{Seq::control_t::DETECT, "reply1", Seq::equals, Seq::nil, Seq::exit<1UL>, 1000},
|
||||
{Seq::control_t::OR_DETECT, "reply2", Seq::starts_with, Seq::nil, Seq::exit<2UL>, 0},
|
||||
{Seq::control_t::OR_DETECT, "reply3", Seq::starts_with, Seq::nil, Seq::exit<3UL>, 0},
|
||||
{Seq::control_t::OR_DETECT, "reply4\n", Seq::starts_with, Seq::nil, Seq::exit<4UL>, 0},
|
||||
{Seq::control_t::OR_DETECT, "reply5", Seq::starts_with, Seq::nil, Seq::exit<5UL>, 0},
|
||||
{Seq::control_t::OR_DETECT, "ERROR", Seq::contains, Seq::nil, Seq::exit<6UL>, 0},
|
||||
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_ok, 1000}
|
||||
}};
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd1", std::strlen("cmd1"));
|
||||
EXPECT_EQ (s.run(script), 1UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd2\n", std::strlen("cmd2\n"));
|
||||
EXPECT_EQ (s.run(script), 2UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd3\r\n", std::strlen("cmd3\r\n"));
|
||||
EXPECT_EQ (s.run(script), 3UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd4\n\r", std::strlen("cmd4\n\r"));
|
||||
EXPECT_EQ (s.run(script), 4UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd5\n", std::strlen("cmd5\n"));
|
||||
EXPECT_EQ (s.run(script), 5UL);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
|
||||
s.clear_clock();
|
||||
s.put("cmd", std::strlen("cmd"));
|
||||
EXPECT_EQ (s.run(script), Seq::exit_error.value);
|
||||
EXPECT_GT (s.clock(), (clock_t)1000);
|
||||
}
|
||||
|
||||
TEST(Tsequencer, run_script_blocks_n_gotos) {
|
||||
Seq s;
|
||||
const Seq::script_t<15> script = {{
|
||||
/* 0 */{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::go_to<1>, 1000},
|
||||
|
||||
/* 1 */{Seq::control_t::SEND, "cmd1", Seq::nil, Seq::nil, Seq::next, 0},
|
||||
/* 2 */{Seq::control_t::EXPECT, "reply1", Seq::starts_with, Seq::nil, Seq::next, 1000},
|
||||
/* 3 */{Seq::control_t::OR_EXPECT, "ERROR", Seq::contains, Seq::nil, Seq::exit_error, 0},
|
||||
|
||||
/* 4 */{Seq::control_t::SEND, "cmd2\n", Seq::nil, Seq::nil, Seq::next, 0},
|
||||
/* 5 */{Seq::control_t::DETECT, "ERROR", Seq::contains, Seq::nil, Seq::exit_error, 1000},
|
||||
/* 6 */{Seq::control_t::OR_DETECT, "reply2", Seq::contains, Seq::nil, Seq::go_to<11>, 0},
|
||||
|
||||
/* 7 */{Seq::control_t::SEND, "cmd3\r\n", Seq::nil, Seq::nil, Seq::next, 0},
|
||||
/* 8 */{Seq::control_t::EXPECT, "ERROR", Seq::contains, Seq::nil, Seq::exit_error, 1000},
|
||||
/* 9 */{Seq::control_t::OR_EXPECT, "lalala", Seq::starts_with, Seq::nil, Seq::exit_error, 0},
|
||||
/*10 */{Seq::control_t::OR_EXPECT, "text\n", Seq::ends_with, Seq::nil, Seq::go_to<14>, 0},
|
||||
|
||||
/*11 */{Seq::control_t::SEND, "cmd4\n\r", Seq::nil, Seq::nil, Seq::next, 0},
|
||||
/*12 */{Seq::control_t::EXPECT, "reply4\n", Seq::starts_with, Seq::nil, Seq::go_to<7>, 1000},
|
||||
/*13 */{Seq::control_t::OR_EXPECT, "ERROR", Seq::contains, Seq::nil, Seq::exit_error, 0},
|
||||
|
||||
/*14 */{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_ok, 1000}
|
||||
}};
|
||||
s.clear_clock();
|
||||
EXPECT_EQ (s.run(script), Seq::exit_ok.value);
|
||||
EXPECT_GT (s.clock(), (clock_t)2000);
|
||||
}
|
||||
|
||||
TEST(Tsequencer, run_match_n_handler) {
|
||||
Seq s;
|
||||
|
||||
using str_t = Seq::string_view;
|
||||
using val_t = Seq::value_type;
|
||||
|
||||
auto match = [](const str_t x, const str_t y) ->bool { (void)x; (void)y; return true; };
|
||||
auto check_match = [] (const str_t x, const str_t y) ->bool {
|
||||
return x == y;
|
||||
};
|
||||
auto handler = [](const val_t* v, size_t s){ (void)*v; (void)s; handler_flag = true; };
|
||||
auto set_if_rpl2 = [](const val_t* v, size_t s){
|
||||
(void)*v; (void)s;
|
||||
handler_flag = (str_t(v, s) == "reply2\n");
|
||||
};
|
||||
|
||||
const Seq::script_t<4> script1 = {{
|
||||
{Seq::control_t::SEND, "cmd1", Seq::nil, Seq::nil, Seq::next, 0},
|
||||
{Seq::control_t::EXPECT, "", match, Seq::nil, Seq::next, 1000},
|
||||
{Seq::control_t::OR_EXPECT, "ERROR", Seq::contains, Seq::nil, Seq::exit_error, 0},
|
||||
|
||||
{Seq::control_t::SEND, "cmd1", Seq::nil, handler, Seq::exit_ok, 0}
|
||||
}};
|
||||
handler_flag = false;
|
||||
s.clear_clock();
|
||||
EXPECT_EQ (s.run(script1), Seq::exit_ok.value);
|
||||
EXPECT_LT (s.clock(), (clock_t)1000);
|
||||
EXPECT_EQ (handler_flag, true);
|
||||
|
||||
const Seq::script_t<2> script2 = {{
|
||||
{Seq::control_t::SEND, "cmd1", Seq::nil, Seq::nil, Seq::next, 0},
|
||||
{Seq::control_t::EXPECT, "reply1", check_match, set_if_rpl2, Seq::exit_ok, 1000},
|
||||
}};
|
||||
handler_flag = false;
|
||||
EXPECT_EQ (s.run(script2), Seq::exit_ok.value);
|
||||
EXPECT_EQ (handler_flag, false);
|
||||
|
||||
const Seq::script_t<2> script3 = {{
|
||||
{Seq::control_t::SEND, "cmd2\n", Seq::nil, Seq::nil, Seq::next, 0},
|
||||
{Seq::control_t::EXPECT, "reply2\n", check_match, set_if_rpl2, Seq::exit_ok, 1000},
|
||||
}};
|
||||
handler_flag = false;
|
||||
EXPECT_EQ (s.run(script3), Seq::exit_ok.value);
|
||||
EXPECT_EQ (handler_flag, true);
|
||||
|
||||
const Seq::script_t<1> script4 = {{
|
||||
{Seq::control_t::SEND, "cmd1", Seq::nil, handler, Seq::exit_ok, 0}
|
||||
}};
|
||||
handler_flag = false;
|
||||
EXPECT_EQ (s.run(script4), Seq::exit_ok.value);
|
||||
EXPECT_EQ (handler_flag, true);
|
||||
}
|
||||
|
||||
TEST(Tsequencer, run_boundaries) {
|
||||
Seq s;
|
||||
|
||||
const Seq::script_t<1> script1 = {{
|
||||
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::next, 0},
|
||||
}};
|
||||
EXPECT_EQ (s.run(script1), Seq::exit_error.value);
|
||||
|
||||
const Seq::script_t<1> script2 = {{
|
||||
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::go_to<1>, 0},
|
||||
}};
|
||||
EXPECT_EQ (s.run(script2), Seq::exit_error.value);
|
||||
|
||||
const Seq::script_t<1> script3 = {{
|
||||
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::go_to<(size_t)-1>, 0},
|
||||
}};
|
||||
EXPECT_EQ (s.run(script3), Seq::exit_error.value);
|
||||
|
||||
const Seq::script_t<1> script4 = {{
|
||||
{Seq::control_t::EXPECT, "abc", Seq::nil, Seq::nil, Seq::next, 1000},
|
||||
}};
|
||||
s.clear_clock();
|
||||
EXPECT_EQ (s.run(script4), Seq::exit_error.value);
|
||||
EXPECT_GT (s.clock(), (clock_t)1000);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user