Unverified Commit 1e5342c7 authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #1052

 Convert manual to reStructuredText
parents e500adec 23efb520
...@@ -6,5 +6,5 @@ blog_release_note.md ...@@ -6,5 +6,5 @@ blog_release_note.md
build/* build/*
github_release_note.md github_release_note.md
html/* html/*
manual manual/html/*
release.txt release.txt
...@@ -164,54 +164,35 @@ pipeline { ...@@ -164,54 +164,35 @@ pipeline {
buildParallel(config, PrettyJobBaseName) buildParallel(config, PrettyJobBaseName)
} }
} }
stage('Documentation') { // TODO: generate PDF from reStructuredText
agent { label 'pandoc' } // stage('Documentation') {
steps { // agent { label 'pandoc' }
deleteDir() // steps {
unstash('sources') // deleteDir()
dir('sources') { // unstash('sources')
// Configure and build. // dir('sources') {
cmakeBuild([ // // Configure and build.
buildDir: 'build', // cmakeBuild([
installation: 'cmake in search path', // buildDir: 'build',
sourceDir: '.', // installation: 'cmake in search path',
cmakeArgs: '-DCAF_BUILD_TEX_MANUAL=yes', // sourceDir: '.',
steps: [[ // cmakeArgs: '-DCAF_BUILD_TEX_MANUAL=yes',
args: '--target doc', // steps: [[
withCmake: true, // args: '--target doc',
]], // withCmake: true,
]) // ]],
sshagent(['84d71a75-cbb6-489a-8f4c-d0e2793201e9']) { // ])
sh """ // sshagent(['84d71a75-cbb6-489a-8f4c-d0e2793201e9']) {
if [ "${env.GIT_BRANCH}" = "master" ]; then // sh """
rsync -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" -r -z --delete build/doc/html/ www.inet.haw-hamburg.de:/users/www/www.actor-framework.org/html/doc // if [ "${env.GIT_BRANCH}" = "master" ]; then
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null build/doc/manual.pdf www.inet.haw-hamburg.de:/users/www/www.actor-framework.org/html/pdf/manual.pdf // rsync -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" -r -z --delete build/doc/html/ www.inet.haw-hamburg.de:/users/www/www.actor-framework.org/html/doc
fi // scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null build/doc/manual.pdf www.inet.haw-hamburg.de:/users/www/www.actor-framework.org/html/pdf/manual.pdf
""" // fi
} // """
} // }
dir('read-the-docs') { // }
git([ // }
credentialsId: '9b054212-9bb4-41fd-ad8e-b7d47495303f', // }
url: 'git@github.com:actor-framework/read-the-docs.git',
])
sh """
if [ "${env.GIT_BRANCH}" = "master" ]; then
cp ../sources/build/doc/rst/* .
if [ -n "\$(git status --porcelain)" ]; then
git add .
git commit -m "Update Manual"
git push --set-upstream origin master
if [ -z "\$(grep 'exp.sha' ../sources/release.txt)" ] ; then
git tag \$(cat ../sources/release.txt)
git push origin \$(cat ../sources/release.txt)
fi
fi
fi
"""
}
}
}
stage('Notify') { stage('Notify') {
steps { steps {
collectResults(config, PrettyJobName) collectResults(config, PrettyJobName)
......
...@@ -137,6 +137,14 @@ A SNocs workspace is provided by GitHub user ...@@ -137,6 +137,14 @@ A SNocs workspace is provided by GitHub user
* Doxygen (for the `doxygen` target) * Doxygen (for the `doxygen` target)
* LaTeX (for the `manual` target) * LaTeX (for the `manual` target)
## Build the Manual
CAF uses [Sphinx](https://www.sphinx-doc.org):
```sh
sphinx-build . manual/html -c manual
```
## Scientific Use ## Scientific Use
If you use CAF in a scientific context, please use one of the following citations: If you use CAF in a scientific context, please use one of the following citations:
......
...@@ -47,7 +47,6 @@ Usage: $0 [OPTION]... [VAR=VALUE]... ...@@ -47,7 +47,6 @@ Usage: $0 [OPTION]... [VAR=VALUE]...
Optional Targets: Optional Targets:
--with-qt-examples build Qt example(s) --with-qt-examples build Qt example(s)
--with-protobuf-examples build Google Protobuf example(s) --with-protobuf-examples build Google Protobuf example(s)
--with-tex-manual build the LaTeX manual
Installation Directories: Installation Directories:
--prefix=PREFIX installation directory [/usr/local] --prefix=PREFIX installation directory [/usr/local]
...@@ -315,9 +314,6 @@ while [ $# -ne 0 ]; do ...@@ -315,9 +314,6 @@ while [ $# -ne 0 ]; do
--with-protobuf-examples) --with-protobuf-examples)
append_cache_entry CAF_BUILD_PROTOBUF_EXAMPLES BOOL yes append_cache_entry CAF_BUILD_PROTOBUF_EXAMPLES BOOL yes
;; ;;
--with-tex-manual)
append_cache_entry CAF_BUILD_TEX_MANUAL BOOL yes
;;
--no-curl-examples) --no-curl-examples)
append_cache_entry CAF_NO_CURL_EXAMPLES BOOL yes append_cache_entry CAF_NO_CURL_EXAMPLES BOOL yes
;; ;;
......
add_custom_target(doc) add_custom_target(doc)
# -- list all .tex source files ------------------------------------------------
set(sources
tex/Actors.tex
tex/Brokers.tex
tex/CommonPitfalls.tex
tex/ConfiguringActorApplications.tex
tex/Error.tex
tex/FAQ.tex
tex/FirstSteps.tex
tex/GroupCommunication.tex
tex/Introduction.tex
tex/ManagingGroupsOfWorkers.tex
tex/MessageHandlers.tex
tex/MessagePassing.tex
tex/Messages.tex
tex/MigrationGuides.tex
tex/NetworkTransparency.tex
tex/ReferenceCounting.tex
tex/Registry.tex
tex/RemoteSpawn.tex
tex/Scheduler.tex
tex/Streaming.tex
tex/TypeInspection.tex
tex/UsingAout.tex
tex/Utility.tex
tex/Testing.tex
)
# -- create target folders -----------------------------------------------------
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tex")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/rst")
# -- process .in files --------------------------------------------------------- # -- process .in files ---------------------------------------------------------
configure_file("cmake/Doxyfile.in" configure_file("cmake/Doxyfile.in"
"${CMAKE_CURRENT_BINARY_DIR}/Doxyfile" "${CMAKE_CURRENT_BINARY_DIR}/Doxyfile"
@ONLY) @ONLY)
configure_file("cmake/variables.tex.in"
"${CMAKE_CURRENT_BINARY_DIR}/tex/variables.tex"
@ONLY)
configure_file("cmake/conf.py.in"
"${CMAKE_CURRENT_BINARY_DIR}/rst/conf.py"
@ONLY)
configure_file("cmake/index_footer.rst.in"
"${CMAKE_CURRENT_BINARY_DIR}/rst/index_footer.rst"
@ONLY)
configure_file("cmake/index_header.rst.in"
"${CMAKE_CURRENT_BINARY_DIR}/rst/index_header.rst"
@ONLY)
# -- generate .rst files -------------------------------------------------------
add_executable(caf-generate-rst cmake/caf-generate-rst.cpp)
target_link_libraries(caf-generate-rst
${CAF_EXTRA_LDFLAGS}
${CAF_LIBRARIES}
${PTHREAD_LIBRARIES})
add_custom_target(rst)
add_dependencies(doc rst)
function(convert_to_rst tex_file)
get_filename_component(file_name "${tex_file}" NAME_WE)
set(input "${CMAKE_CURRENT_SOURCE_DIR}/tex/${tex_file}")
set(rst_file "${file_name}.rst")
set(output "${CMAKE_CURRENT_BINARY_DIR}/rst/${rst_file}")
add_custom_command(OUTPUT "${output}"
COMMAND
caf-generate-rst
-o "${output}"
-i "${input}"
-r "${PROJECT_SOURCE_DIR}"
DEPENDS caf-generate-rst "${input}")
add_custom_target("${rst_file}" DEPENDS "${output}")
add_dependencies(rst "${rst_file}")
endfunction()
foreach(filename ${sources})
get_filename_component(filename_no_dir "${filename}" NAME)
convert_to_rst("${filename_no_dir}")
endforeach()
# generate index.rst file from manual.tex
add_custom_target("index.rst"
DEPENDS "tex/manual.tex"
COMMAND "python"
"${CMAKE_SOURCE_DIR}/scripts/make_index_rst.py"
"${CMAKE_CURRENT_BINARY_DIR}/rst/index.rst"
"${CMAKE_SOURCE_DIR}/doc/tex/manual.tex"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/rst")
add_dependencies(rst "index.rst")
# -- Doxygen setup ------------------------------------------------------------- # -- Doxygen setup -------------------------------------------------------------
find_package(Doxygen) find_package(Doxygen)
...@@ -116,21 +21,3 @@ else() ...@@ -116,21 +21,3 @@ else()
VERBATIM) VERBATIM)
add_dependencies(doc doxygen) add_dependencies(doc doxygen)
endif() endif()
# -- LaTeX setup ---------------------------------------------------------------
if (CAF_BUILD_TEX_MANUAL)
find_package(LATEX)
message(STATUS "Add optional target: manual.")
include("cmake/UseLATEX.cmake")
# enable synctex for convenient editing
set(LATEX_USE_SYNCTEX yes)
# add manual.pdf as target
add_latex_document(tex/manual.tex
INPUTS ${sources} "tex/variables.tex"
IMAGE_DIRS "pdf"
FORCE_PDF
TARGET_NAME manual)
add_dependencies(doc manual)
endif()
# File: UseLATEX.cmake
# CMAKE commands to actually use the LaTeX compiler
# Version: 2.4.8
# Author: Kenneth Moreland <kmorel@sandia.gov>
#
# Copyright 2004, 2015 Sandia Corporation.
# Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
# license for use of this work by or on behalf of the U.S. Government.
#
# This software is released under the BSD 3-Clause License.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# The following function is defined:
#
# add_latex_document(<tex_file>
# [BIBFILES <bib_files>]
# [INPUTS <input_tex_files>]
# [IMAGE_DIRS] <image_directories>
# [IMAGES] <image_files>
# [CONFIGURE] <tex_files>
# [DEPENDS] <tex_files>
# [MULTIBIB_NEWCITES] <suffix_list>
# [USE_BIBLATEX]
# [USE_INDEX]
# [INDEX_NAMES <index_names>]
# [USE_GLOSSARY] [USE_NOMENCL]
# [FORCE_PDF] [FORCE_DVI] [FORCE_HTML]
# [TARGET_NAME] <name>
# [EXCLUDE_FROM_ALL]
# [EXCLUDE_FROM_DEFAULTS])
# Adds targets that compile <tex_file>. The latex output is placed
# in LATEX_OUTPUT_PATH or CMAKE_CURRENT_BINARY_DIR if the former is
# not set. The latex program is picky about where files are located,
# so all input files are copied from the source directory to the
# output directory. This includes the target tex file, any tex file
# listed with the INPUTS option, the bibliography files listed with
# the BIBFILES option, and any .cls, .bst, .clo, .sty, .ist, and .fd
# files found in the current source directory. Images found in the
# IMAGE_DIRS directories or listed by IMAGES are also copied to the
# output directory and converted to an appropriate format if necessary.
# Any tex files also listed with the CONFIGURE option are also processed
# with the CMake CONFIGURE_FILE command (with the @ONLY flag). Any file
# listed in CONFIGURE but not the target tex file or listed with INPUTS
# has no effect. DEPENDS can be used to specify generated files that are
# needed to compile the latex target.
#
# The following targets are made. The name prefix is based off of the
# base name of the tex file unless TARGET_NAME is specified. If
# TARGET_NAME is specified, then that name is used for the targets.
#
# name_dvi: Makes <name>.dvi
# name_pdf: Makes <name>.pdf using pdflatex.
# name_safepdf: Makes <name>.pdf using ps2pdf. If using the
# default program arguments, this will ensure all fonts
# are embedded and no lossy compression has been
# performed on images.
# name_ps: Makes <name>.ps
# name_html: Makes <name>.html
# name_auxclean: Deletes <name>.aux and other auxiliary files.
# This is sometimes necessary if a LaTeX error occurs
# and writes a bad aux file. Unlike the regular clean
# target, it does not delete other input files, such as
# converted images, to save time on the rebuild.
#
# Unless the EXCLUDE_FROM_ALL option is given, one of these targets
# is added to the ALL target and built by default. Which target is
# determined by the LATEX_DEFAULT_BUILD CMake variable. See the
# documentation of that variable for more details.
#
# Unless the EXCLUDE_FROM_DEFAULTS option is given, all these targets
# are added as dependencies to targets named dvi, pdf, safepdf, ps,
# html, and auxclean, respectively.
#
# USE_BIBLATEX enables the use of biblatex/biber as an alternative to
# bibtex. Bibtex remains the default if USE_BIBLATEX is not
# specified.
#
# If the argument USE_INDEX is given, then commands to build an index
# are made. If the argument INDEX_NAMES is given, an index file is
# generated for each name in this list. See the LaTeX package multind
# for more information about how to generate multiple indices.
#
# If the argument USE_GLOSSARY is given, then commands to
# build a glossary are made. If the argument MULTIBIB_NEWCITES is
# given, then additional bibtex calls are added to the build to
# support the extra auxiliary files created with the \newcite command
# in the multibib package.
#
# History:
#
# 2.4.8 Fix synctex issue with absolute paths not being converted.
#
# 2.4.7 Fix some issues with spaces in the path of the working directory where
# LaTeX is executed.
#
# 2.4.6 Fix parse issue with older versions of CMake.
#
# 2.4.5 Fix issues with files and paths containing spaces.
#
# 2.4.4 Improve error reporting message when LaTeX fails.
#
# When LaTeX fails, delete the output file, which is invalid.
#
# Add warnings for "missing characters." These usually mean that a
# non-ASCII character is in the document and will not be printed
# correctly.
#
# 2.4.3 Check for warnings from the natbib package. When using natbib,
# warnings for missing bibliography references look different. So
# far, natbib seems to be quiet unless something is important, so
# look for all natbib warnings. (We can change this later if
# necessary.)
#
# 2.4.2 Fix an issue where new versions of ImageMagick expect the order of
# options in command line execution of magick/convert. (See, for
# example, http://www.imagemagick.org/Usage/basics/#why.)
#
# 2.4.1 Add ability to dump LaTeX log file when using batch mode. Batch
# mode suppresses most output, often including error messages. To
# make sure critical error messages get displayed, show the full log
# on failures.
#
# 2.4.0 Remove "-r 600" from the default PDFTOPS_CONVERTER_FLAGS. The -r flag
# is available from the Poppler version of pdftops, but not the Xpdf
# version.
#
# Fix an issue with the flags for the different programs not being
# properly separated.
#
# Fix an issue on windows where the = character is not allowed for
# ps2pdf arguments.
#
# Change default arguments for latex and pdflatex commands. Makes the
# output more quiet and prints out the file/line where errors occur.
# (Thanks to Nikos Koukis.)
#
# After a LaTeX build, check the log file for warnings that are
# indicative of problems with the build.
#
# Remove support for latex2html. Instead, use the htlatex program.
# This is now part of TeX Live and most other distributions. It also
# behaves much more like the other LaTeX programs. Also fixed some
# nasty issues with the htlatex arguments.
#
# 2.3.2 Declare LaTeX input files as sources for targets so that they show
# up in IDEs like QtCreator.
#
# Fix issue where main tex files in subdirectories were creating
# invalid targets for building HTML. Just disable the HTML targets in
# this case.
#
# 2.3.1 Support use of magick command instead of convert command for
# ImageMagick 7.
#
# 2.3.0 Add USE_BIBLATEX option to support the biblatex package, which
# requires using the program biber as a replacement for bibtex
# (thanks to David Tracey).
#
# 2.2.1 Add STRINGS property to LATEX_DEFAULT_BUILD to make it easier to
# select the default build in the CMake GUI.
#
# 2.2.0 Add TARGET_NAME option.
#
# 2.1.1 Support for finding bmp, ppm, and other image files.
#
# 2.1.0 Fix an error where the pdf target and others were defined multiple
# times if UseLATEX.cmake was included multiple times.
#
# Added INDEX_NAMES option to support multiple indexes in a single
# document from the multind package (thanks to Dan Lipsa).
#
# 2.0.0 First major revision of UseLATEX.cmake updates to more recent features
# of CMake and some non-backward compatible changes.
#
# Changed all function and macro names to lower case. CMake's identifiers
# are case insensitive, but the convention moved from all upper case to
# all lower case somewhere around the release of CMake 2. (The original
# version of UseLATEX.cmake predates that.)
#
# Remove condition matching in if statements. They are no longer necessary
# and are even discouraged (because else clauses get confusing).
#
# Use "new" features available in CMake such as list and argument parsing.
#
# Remove some code that has been deprecated for a while.
#
# Mark variables for compiler and converter executables as advanced to
# match the more conventional CMake behavior.
#
# Changed how default builds are specified and add the ability to force
# a particular build.
#
# Made the base targets (pdf, dvi, etc.) global. add_latex_document
# always mangles its target names and these base targets depend on
# the targets with mangled names.
#
# 1.10.5 Fix for Window's convert check (thanks to Martin Baute).
#
# 1.10.4 Copy font files to binary directory for packages that come with
# their own fonts.
#
# 1.10.3 Check for Windows version of convert being used instead of
# ImageMagick's version (thanks to Martin Baute).
#
# 1.10.2 Use htlatex as a fallback when latex2html is not available (thanks
# to Tomasz Grzegurzko).
#
# 1.10.1 Make convert program mandatory only if actually used (thanks to
# Julien Schueller).
#
# 1.10.0 Added NO_DEFAULT and DEFAULT_PS options.
# Fixed issue with cleaning files for LaTeX documents originating in
# a subdirectory.
#
# 1.9.6 Fixed problem with LATEX_SMALL_IMAGES.
# Strengthened check to make sure the output directory does not contain
# the source files.
#
# 1.9.5 Add support for image types not directly supported by either latex
# or pdflatex. (Thanks to Jorge Gerardo Pena Pastor for SVG support.)
#
# 1.9.4 Fix issues with filenames containing multiple periods.
#
# 1.9.3 Hide some variables that are now cached but should not show up in
# the ccmake list of variables.
#
# 1.9.2 Changed MACRO declarations to FUNCTION declarations. The better
# FUNCTION scoping will hopefully avoid some common but subtle bugs.
# This implicitly increases the minimum CMake version to 4.6 (although
# I honestly only test it with the latest 4.8 version).
#
# Since we are updating the minimum CMake version, I'm going to start
# using the builtin LIST commands that are now available.
#
# Favor using pdftops from the Poppler package to convert from pdf to
# eps. It does a much better job than ImageMagick or ghostscript.
#
# 1.9.1 Fixed typo that caused the LATEX_SMALL_IMAGES option to fail to
# activate.
#
# 1.9.0 Add support for the multibib package (thanks to Antonio LaTorre).
#
# 1.8.2 Fix corner case when an argument name was also a variable containing
# the text of an argument. In this case, the CMake IF was matching
# the argument text with the contents of the variable with the same
# argument name.
#
# 1.8.1 Fix problem where ps2pdf was not getting the appropriate arguments.
#
# 1.8.0 Add support for synctex.
#
# 1.7.7 Support calling xindy when making glossaries.
#
# Improved make clean support.
#
# 1.7.6 Add support for the nomencl package (thanks to Myles English).
#
# 1.7.5 Fix issue with bibfiles being copied two different ways, which causes
# Problems with dependencies (thanks to Edwin van Leeuwen).
#
# 1.7.4 Added the DEFAULT_SAFEPDF option (thanks to Raymond Wan).
#
# Added warnings when image directories are not found (and were
# probably not given relative to the source directory).
#
# 1.7.3 Fix some issues with interactions between makeglossaries and bibtex
# (thanks to Mark de Wever).
#
# 1.7.2 Use ps2pdf to convert eps to pdf to get around the problem with
# ImageMagick dropping the bounding box (thanks to Lukasz Lis).
#
# 1.7.1 Fixed some dependency issues.
#
# 1.7.0 Added DEPENDS options (thanks to Theodore Papadopoulo).
#
# 1.6.1 Ported the makeglossaries command to CMake and embedded the port
# into UseLATEX.cmake.
#
# 1.6.0 Allow the use of the makeglossaries command. Thanks to Oystein
# S. Haaland for the patch.
#
# 1.5.0 Allow any type of file in the INPUTS lists, not just tex file
# (suggested by Eric Noulard). As a consequence, the ability to
# specify tex files without the .tex extension is removed. The removed
# function is of dubious value anyway.
#
# When copying input files, skip over any file that exists in the
# binary directory but does not exist in the source directory with the
# assumption that these files were added by some other mechanism. I
# find this useful when creating large documents with multiple
# chapters that I want to build separately (for speed) as I work on
# them. I use the same boilerplate as the starting point for all
# and just copy it with different configurations. This was what the
# separate ADD_LATEX_DOCUMENT method was supposed to originally be for.
# Since its external use is pretty much deprecated, I removed that
# documentation.
#
# 1.4.1 Copy .sty files along with the other class and package files.
#
# 1.4.0 Added a MANGLE_TARGET_NAMES option that will mangle the target names.
#
# Fixed problem with copying bib files that became apparent with
# CMake 2.4.
#
# 1.3.0 Added a LATEX_OUTPUT_PATH variable that allows you or the user to
# specify where the built latex documents to go. This is especially
# handy if you want to do in-source builds.
#
# Removed the ADD_LATEX_IMAGES macro and absorbed the functionality
# into ADD_LATEX_DOCUMENT. The old interface was always kind of
# clunky anyway since you had to specify the image directory in both
# places. It also made supporting LATEX_OUTPUT_PATH problematic.
#
# Added support for jpeg files.
#
# 1.2.0 Changed the configuration options yet again. Removed the NO_CONFIGURE
# Replaced it with a CONFIGURE option that lists input files for which
# configure should be run.
#
# The pdf target no longer depends on the dvi target. This allows you
# to build latex documents that require pdflatex. Also added an option
# to make the pdf target the default one.
#
# 1.1.1 Added the NO_CONFIGURE option. The @ character can be used when
# specifying table column separators. If two or more are used, then
# will incorrectly substitute them.
#
# 1.1.0 Added ability include multiple bib files. Added ability to do copy
# sub-tex files for multipart tex files.
#
# 1.0.0 If both ps and pdf type images exist, just copy the one that
# matches the current render mode. Replaced a bunch of STRING
# commands with GET_FILENAME_COMPONENT commands that were made to do
# the desired function.
#
# 0.4.0 First version posted to CMake Wiki.
#
if(__USE_LATEX_INCLUDED)
return()
endif()
set(__USE_LATEX_INCLUDED TRUE)
#############################################################################
# Find the location of myself while originally executing. If you do this
# inside of a macro, it will recode where the macro was invoked.
#############################################################################
set(LATEX_USE_LATEX_LOCATION ${CMAKE_CURRENT_LIST_FILE}
CACHE INTERNAL "Location of UseLATEX.cmake file." FORCE
)
#############################################################################
# Generic helper functions
#############################################################################
include(CMakeParseArguments)
function(latex_list_contains var value)
set(input_list ${ARGN})
list(FIND input_list "${value}" index)
if(index GREATER -1)
set(${var} TRUE PARENT_SCOPE)
else()
set(${var} PARENT_SCOPE)
endif()
endfunction(latex_list_contains)
# Match the contents of a file to a regular expression.
function(latex_file_match variable filename regexp default)
# The FILE STRINGS command would be a bit better, but I'm not totally sure
# the match will always be to a whole line, and I don't want to break things.
file(READ ${filename} file_contents)
string(REGEX MATCHALL "${regexp}"
match_result ${file_contents}
)
if(match_result)
set(${variable} "${match_result}" PARENT_SCOPE)
else()
set(${variable} "${default}" PARENT_SCOPE)
endif()
endfunction(latex_file_match)
# A version of GET_FILENAME_COMPONENT that treats extensions after the last
# period rather than the first. To the best of my knowledge, all filenames
# typically used by LaTeX, including image files, have small extensions
# after the last dot.
function(latex_get_filename_component varname filename type)
set(result)
if("${type}" STREQUAL "NAME_WE")
get_filename_component(name ${filename} NAME)
string(REGEX REPLACE "\\.[^.]*\$" "" result "${name}")
elseif("${type}" STREQUAL "EXT")
get_filename_component(name ${filename} NAME)
string(REGEX MATCH "\\.[^.]*\$" result "${name}")
else()
get_filename_component(result ${filename} ${type})
endif()
set(${varname} "${result}" PARENT_SCOPE)
endfunction(latex_get_filename_component)
#############################################################################
# Functions that perform processing during a LaTeX build.
#############################################################################
function(latex_execute_latex)
if(NOT LATEX_TARGET)
message(SEND_ERROR "Need to define LATEX_TARGET")
endif()
if(NOT LATEX_WORKING_DIRECTORY)
message(SEND_ERROR "Need to define LATEX_WORKING_DIRECTORY")
endif()
if(NOT LATEX_FULL_COMMAND)
message(SEND_ERROR "Need to define LATEX_FULL_COMMAND")
endif()
if(NOT LATEX_OUTPUT_FILE)
message(SEND_ERROR "Need to define LATEX_OUTPUT_FILE")
endif()
set(full_command_original "${LATEX_FULL_COMMAND}")
# Chose the native method for parsing command arguments. Newer versions of
# CMake allow you to just use NATIVE_COMMAND.
if (CMAKE_VERSION VERSION_GREATER 3.8)
set(separate_arguments_mode NATIVE_COMMAND)
else()
if (WIN32)
set(separate_arguments_mode WINDOWS_COMMAND)
else()
set(separate_arguments_mode UNIX_COMMAND)
endif()
endif()
# Preps variables for use in execute_process.
# Even though we expect LATEX_WORKING_DIRECTORY to have a single "argument,"
# we also want to make sure that we strip out any escape characters that can
# foul up the WORKING_DIRECTORY argument.
separate_arguments(LATEX_FULL_COMMAND UNIX_COMMAND "${LATEX_FULL_COMMAND}")
separate_arguments(LATEX_WORKING_DIRECTORY_SEP UNIX_COMMAND "${LATEX_WORKING_DIRECTORY}")
execute_process(
COMMAND ${LATEX_FULL_COMMAND}
WORKING_DIRECTORY "${LATEX_WORKING_DIRECTORY_SEP}"
RESULT_VARIABLE execute_result
)
if(NOT ${execute_result} EQUAL 0)
# LaTeX tends to write a file when a failure happens. Delete that file so
# that LaTeX will run again.
file(REMOVE "${LATEX_WORKING_DIRECTORY}/${LATEX_OUTPUT_FILE}")
message("\n\nLaTeX command failed")
message("${full_command_original}")
message("Log output:")
file(READ "${LATEX_WORKING_DIRECTORY}/${LATEX_TARGET}.log" log_output)
message("${log_output}")
message(FATAL_ERROR
"Successfully executed LaTeX, but LaTeX returned an error.")
endif()
endfunction(latex_execute_latex)
function(latex_makeglossaries)
# This is really a bare bones port of the makeglossaries perl script into
# CMake scripting.
message("**************************** In makeglossaries")
if(NOT LATEX_TARGET)
message(SEND_ERROR "Need to define LATEX_TARGET")
endif()
set(aux_file ${LATEX_TARGET}.aux)
if(NOT EXISTS ${aux_file})
message(SEND_ERROR "${aux_file} does not exist. Run latex on your target file.")
endif()
latex_file_match(newglossary_lines ${aux_file}
"@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}"
"@newglossary{main}{glg}{gls}{glo}"
)
latex_file_match(istfile_line ${aux_file}
"@istfilename[ \t]*{([^}]*)}"
"@istfilename{${LATEX_TARGET}.ist}"
)
string(REGEX REPLACE "@istfilename[ \t]*{([^}]*)}" "\\1"
istfile ${istfile_line}
)
string(REGEX MATCH ".*\\.xdy" use_xindy "${istfile}")
if(use_xindy)
message("*************** Using xindy")
if(NOT XINDY_COMPILER)
message(SEND_ERROR "Need to define XINDY_COMPILER")
endif()
else()
message("*************** Using makeindex")
if(NOT MAKEINDEX_COMPILER)
message(SEND_ERROR "Need to define MAKEINDEX_COMPILER")
endif()
endif()
foreach(newglossary ${newglossary_lines})
string(REGEX REPLACE
"@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}"
"\\1" glossary_name ${newglossary}
)
string(REGEX REPLACE
"@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}"
"${LATEX_TARGET}.\\2" glossary_log ${newglossary}
)
string(REGEX REPLACE
"@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}"
"${LATEX_TARGET}.\\3" glossary_out ${newglossary}
)
string(REGEX REPLACE
"@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}"
"${LATEX_TARGET}.\\4" glossary_in ${newglossary}
)
if(use_xindy)
latex_file_match(xdylanguage_line ${aux_file}
"@xdylanguage[ \t]*{${glossary_name}}{([^}]*)}"
"@xdylanguage{${glossary_name}}{english}"
)
string(REGEX REPLACE
"@xdylanguage[ \t]*{${glossary_name}}{([^}]*)}"
"\\1"
language
${xdylanguage_line}
)
# What crazy person makes a LaTeX index generator that uses different
# identifiers for language than babel (or at least does not support
# the old ones)?
if(${language} STREQUAL "frenchb")
set(language "french")
elseif(${language} MATCHES "^n?germanb?$")
set(language "german")
elseif(${language} STREQUAL "magyar")
set(language "hungarian")
elseif(${language} STREQUAL "lsorbian")
set(language "lower-sorbian")
elseif(${language} STREQUAL "norsk")
set(language "norwegian")
elseif(${language} STREQUAL "portuges")
set(language "portuguese")
elseif(${language} STREQUAL "russianb")
set(language "russian")
elseif(${language} STREQUAL "slovene")
set(language "slovenian")
elseif(${language} STREQUAL "ukraineb")
set(language "ukrainian")
elseif(${language} STREQUAL "usorbian")
set(language "upper-sorbian")
endif()
if(language)
set(language_flags "-L ${language}")
else()
set(language_flags "")
endif()
latex_file_match(codepage_line ${aux_file}
"@gls@codepage[ \t]*{${glossary_name}}{([^}]*)}"
"@gls@codepage{${glossary_name}}{utf}"
)
string(REGEX REPLACE
"@gls@codepage[ \t]*{${glossary_name}}{([^}]*)}"
"\\1"
codepage
${codepage_line}
)
if(codepage)
set(codepage_flags "-C ${codepage}")
else()
# Ideally, we would check that the language is compatible with the
# default codepage, but I'm hoping that distributions will be smart
# enough to specify their own codepage. I know, it's asking a lot.
set(codepage_flags "")
endif()
message("${XINDY_COMPILER} ${MAKEGLOSSARIES_COMPILER_ARGS} ${language_flags} ${codepage_flags} -I xindy -M ${glossary_name} -t ${glossary_log} -o ${glossary_out} ${glossary_in}"
)
exec_program(${XINDY_COMPILER}
ARGS ${MAKEGLOSSARIES_COMPILER_ARGS}
${language_flags}
${codepage_flags}
-I xindy
-M ${glossary_name}
-t ${glossary_log}
-o ${glossary_out}
${glossary_in}
OUTPUT_VARIABLE xindy_output
)
message("${xindy_output}")
# So, it is possible (perhaps common?) for aux files to specify a
# language and codepage that are incompatible with each other. Check
# for that condition, and if it happens run again with the default
# codepage.
if("${xindy_output}" MATCHES "^Cannot locate xindy module for language (.+) in codepage (.+)\\.$")
message("*************** Retrying xindy with default codepage.")
exec_program(${XINDY_COMPILER}
ARGS ${MAKEGLOSSARIES_COMPILER_ARGS}
${language_flags}
-I xindy
-M ${glossary_name}
-t ${glossary_log}
-o ${glossary_out}
${glossary_in}
)
endif()
else()
message("${MAKEINDEX_COMPILER} ${MAKEGLOSSARIES_COMPILER_ARGS} -s ${istfile} -t ${glossary_log} -o ${glossary_out} ${glossary_in}")
exec_program(${MAKEINDEX_COMPILER} ARGS ${MAKEGLOSSARIES_COMPILER_ARGS}
-s ${istfile} -t ${glossary_log} -o ${glossary_out} ${glossary_in}
)
endif()
endforeach(newglossary)
endfunction(latex_makeglossaries)
function(latex_makenomenclature)
message("**************************** In makenomenclature")
if(NOT LATEX_TARGET)
message(SEND_ERROR "Need to define LATEX_TARGET")
endif()
if(NOT MAKEINDEX_COMPILER)
message(SEND_ERROR "Need to define MAKEINDEX_COMPILER")
endif()
set(nomencl_out ${LATEX_TARGET}.nls)
set(nomencl_in ${LATEX_TARGET}.nlo)
exec_program(${MAKEINDEX_COMPILER} ARGS ${MAKENOMENCLATURE_COMPILER_ARGS}
${nomencl_in} -s "nomencl.ist" -o ${nomencl_out}
)
endfunction(latex_makenomenclature)
function(latex_correct_synctex)
message("**************************** In correct SyncTeX")
if(NOT LATEX_TARGET)
message(SEND_ERROR "Need to define LATEX_TARGET")
endif()
if(NOT GZIP)
message(SEND_ERROR "Need to define GZIP")
endif()
if(NOT LATEX_SOURCE_DIRECTORY)
message(SEND_ERROR "Need to define LATEX_SOURCE_DIRECTORY")
endif()
if(NOT LATEX_BINARY_DIRECTORY)
message(SEND_ERROR "Need to define LATEX_BINARY_DIRECTORY")
endif()
message("${LATEX_BINARY_DIRECTORY}")
message("${LATEX_SOURCE_DIRECTORY}")
set(synctex_file ${LATEX_BINARY_DIRECTORY}/${LATEX_TARGET}.synctex)
set(synctex_file_gz ${synctex_file}.gz)
if(EXISTS ${synctex_file_gz})
message("Making backup of synctex file.")
configure_file(${synctex_file_gz} ${synctex_file}.bak.gz COPYONLY)
message("Uncompressing synctex file.")
exec_program(${GZIP}
ARGS --decompress ${synctex_file_gz}
)
message("Reading synctex file.")
file(READ ${synctex_file} synctex_data)
message("Replacing output paths with input paths.")
foreach(extension tex cls bst clo sty ist fd)
# Relative paths
string(REGEX REPLACE
"(Input:[0-9]+:)([^/\n][^\n]\\.${extension}*)"
"\\1${LATEX_SOURCE_DIRECTORY}/\\2"
synctex_data
"${synctex_data}"
)
# Absolute paths
string(REGEX REPLACE
"(Input:[0-9]+:)${LATEX_BINARY_DIRECTORY}([^\n]*\\.${extension})"
"\\1${LATEX_SOURCE_DIRECTORY}\\2"
synctex_data
"${synctex_data}"
)
endforeach(extension)
message("Writing synctex file.")
file(WRITE ${synctex_file} "${synctex_data}")
message("Compressing synctex file.")
exec_program(${GZIP}
ARGS ${synctex_file}
)
else()
message(SEND_ERROR "File ${synctex_file_gz} not found. Perhaps synctex is not supported by your LaTeX compiler.")
endif()
endfunction(latex_correct_synctex)
function(latex_check_important_warnings)
set(log_file ${LATEX_TARGET}.log)
message("\nChecking ${log_file} for important warnings.")
if(NOT LATEX_TARGET)
message(SEND_ERROR "Need to define LATEX_TARGET")
endif()
if(NOT EXISTS ${log_file})
message("Could not find log file: ${log_file}")
return()
endif()
set(found_error)
file(READ ${log_file} log)
# Check for undefined references
string(REGEX MATCHALL
"\n[^\n]*Reference[^\n]*undefined[^\n]*"
reference_warnings
"${log}")
if(reference_warnings)
set(found_error TRUE)
message("\nFound missing reference warnings.")
foreach(warning ${reference_warnings})
string(STRIP "${warning}" warning_no_newline)
message("${warning_no_newline}")
endforeach(warning)
endif()
# Check for natbib warnings
string(REGEX MATCHALL
"\nPackage natbib Warning:[^\n]*"
natbib_warnings
"${log}")
if(natbib_warnings)
set(found_error TRUE)
message("\nFound natbib package warnings.")
foreach(warning ${natbib_warnings})
string(STRIP "${warning}" warning_no_newline)
message("${warning_no_newline}")
endforeach(warning)
endif()
# Check for overfull
string(REGEX MATCHALL
"\nOverfull[^\n]*"
overfull_warnings
"${log}")
if(overfull_warnings)
set(found_error TRUE)
message("\nFound overfull warnings. These are indicative of layout errors.")
foreach(warning ${overfull_warnings})
string(STRIP "${warning}" warning_no_newline)
message("${warning_no_newline}")
endforeach(warning)
endif()
# Check for invalid characters
string(REGEX MATCHALL
"\nMissing character:[^\n]*"
invalid_character_warnings
"${log}")
if(invalid_character_warnings)
set(found_error TRUE)
message("\nFound invalid character warnings. These characters are likely not printed correctly.")
foreach(warning ${invalid_character_warnings})
string(STRIP "${warning}" warning_no_newline)
message("${warning_no_newline}")
endforeach(warning)
endif()
if(found_error)
latex_get_filename_component(log_file_path ${log_file} ABSOLUTE)
message("\nConsult ${log_file_path} for more information on LaTeX build.")
else()
message("No known important warnings found.")
endif(found_error)
endfunction(latex_check_important_warnings)
#############################################################################
# Helper functions for establishing LaTeX build.
#############################################################################
function(latex_needit VAR NAME)
if(NOT ${VAR})
message(SEND_ERROR "I need the ${NAME} command.")
endif()
endfunction(latex_needit)
function(latex_wantit VAR NAME)
if(NOT ${VAR})
message(STATUS "I could not find the ${NAME} command.")
endif()
endfunction(latex_wantit)
function(latex_setup_variables)
set(LATEX_OUTPUT_PATH "${LATEX_OUTPUT_PATH}"
CACHE PATH "If non empty, specifies the location to place LaTeX output."
)
find_package(LATEX)
find_program(XINDY_COMPILER
NAME xindy
PATHS ${MIKTEX_BINARY_PATH} /usr/bin
)
find_package(UnixCommands)
find_program(PDFTOPS_CONVERTER
NAMES pdftops
DOC "The pdf to ps converter program from the Poppler package."
)
find_program(HTLATEX_COMPILER
NAMES htlatex
PATHS ${MIKTEX_BINARY_PATH}
/usr/bin
)
mark_as_advanced(
LATEX_COMPILER
PDFLATEX_COMPILER
BIBTEX_COMPILER
BIBER_COMPILER
MAKEINDEX_COMPILER
XINDY_COMPILER
DVIPS_CONVERTER
PS2PDF_CONVERTER
PDFTOPS_CONVERTER
LATEX2HTML_CONVERTER
HTLATEX_COMPILER
)
latex_needit(LATEX_COMPILER latex)
latex_wantit(PDFLATEX_COMPILER pdflatex)
latex_wantit(HTLATEX_COMPILER htlatex)
latex_needit(BIBTEX_COMPILER bibtex)
latex_wantit(BIBER_COMPILER biber)
latex_needit(MAKEINDEX_COMPILER makeindex)
latex_wantit(DVIPS_CONVERTER dvips)
latex_wantit(PS2PDF_CONVERTER ps2pdf)
latex_wantit(PDFTOPS_CONVERTER pdftops)
set(LATEX_COMPILER_FLAGS "-interaction=batchmode -file-line-error"
CACHE STRING "Flags passed to latex.")
set(PDFLATEX_COMPILER_FLAGS ${LATEX_COMPILER_FLAGS}
CACHE STRING "Flags passed to pdflatex.")
set(HTLATEX_COMPILER_TEX4HT_FLAGS "html"
CACHE STRING "Options for the tex4ht.sty and *.4ht style files.")
set(HTLATEX_COMPILER_TEX4HT_POSTPROCESSOR_FLAGS ""
CACHE STRING "Options for the text4ht postprocessor.")
set(HTLATEX_COMPILER_T4HT_POSTPROCESSOR_FLAGS ""
CACHE STRING "Options for the t4ht postprocessor.")
set(HTLATEX_COMPILER_LATEX_FLAGS ${LATEX_COMPILER_FLAGS}
CACHE STRING "Flags passed from htlatex to the LaTeX compiler.")
set(LATEX_SYNCTEX_FLAGS "-synctex=1"
CACHE STRING "latex/pdflatex flags used to create synctex file.")
set(BIBTEX_COMPILER_FLAGS ""
CACHE STRING "Flags passed to bibtex.")
set(BIBER_COMPILER_FLAGS ""
CACHE STRING "Flags passed to biber.")
set(MAKEINDEX_COMPILER_FLAGS ""
CACHE STRING "Flags passed to makeindex.")
set(MAKEGLOSSARIES_COMPILER_FLAGS ""
CACHE STRING "Flags passed to makeglossaries.")
set(MAKENOMENCLATURE_COMPILER_FLAGS ""
CACHE STRING "Flags passed to makenomenclature.")
set(DVIPS_CONVERTER_FLAGS "-Ppdf -G0 -t letter"
CACHE STRING "Flags passed to dvips.")
if(NOT WIN32)
set(PS2PDF_CONVERTER_FLAGS "-dMaxSubsetPct=100 -dCompatibilityLevel=1.3 -dSubsetFonts=true -dEmbedAllFonts=true -dAutoFilterColorImages=false -dAutoFilterGrayImages=false -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -dMonoImageFilter=/FlateEncode"
CACHE STRING "Flags passed to ps2pdf.")
else()
# Most windows ports of ghostscript utilities use .bat files for ps2pdf
# commands. bat scripts interpret "=" as a special character and separate
# those arguments. To get around this, the ghostscript utilities also
# support using "#" in place of "=".
set(PS2PDF_CONVERTER_FLAGS "-dMaxSubsetPct#100 -dCompatibilityLevel#1.3 -dSubsetFonts#true -dEmbedAllFonts#true -dAutoFilterColorImages#false -dAutoFilterGrayImages#false -dColorImageFilter#/FlateEncode -dGrayImageFilter#/FlateEncode -dMonoImageFilter#/FlateEncode"
CACHE STRING "Flags passed to ps2pdf.")
endif()
set(PDFTOPS_CONVERTER_FLAGS ""
CACHE STRING "Flags passed to pdftops.")
mark_as_advanced(
LATEX_COMPILER_FLAGS
PDFLATEX_COMPILER_FLAGS
HTLATEX_COMPILER_TEX4HT_FLAGS
HTLATEX_COMPILER_TEX4HT_POSTPROCESSOR_FLAGS
HTLATEX_COMPILER_T4HT_POSTPROCESSOR_FLAGS
HTLATEX_COMPILER_LATEX_FLAGS
LATEX_SYNCTEX_FLAGS
BIBTEX_COMPILER_FLAGS
BIBER_COMPILER_FLAGS
MAKEINDEX_COMPILER_FLAGS
MAKEGLOSSARIES_COMPILER_FLAGS
MAKENOMENCLATURE_COMPILER_FLAGS
DVIPS_CONVERTER_FLAGS
PS2PDF_CONVERTER_FLAGS
PDFTOPS_CONVERTER_FLAGS
)
# Because it is easier to type, the flags variables are entered as
# space-separated strings much like you would in a shell. However, when
# using a CMake command to execute a program, it works better to hold the
# arguments in semicolon-separated lists (otherwise the whole string will
# be interpreted as a single argument). Use the separate_arguments to
# convert the space-separated strings to semicolon-separated lists.
separate_arguments(LATEX_COMPILER_FLAGS)
separate_arguments(PDFLATEX_COMPILER_FLAGS)
separate_arguments(HTLATEX_COMPILER_LATEX_FLAGS)
separate_arguments(LATEX_SYNCTEX_FLAGS)
separate_arguments(BIBTEX_COMPILER_FLAGS)
separate_arguments(BIBER_COMPILER_FLAGS)
separate_arguments(MAKEINDEX_COMPILER_FLAGS)
separate_arguments(MAKEGLOSSARIES_COMPILER_FLAGS)
separate_arguments(MAKENOMENCLATURE_COMPILER_FLAGS)
separate_arguments(DVIPS_CONVERTER_FLAGS)
separate_arguments(PS2PDF_CONVERTER_FLAGS)
separate_arguments(PDFTOPS_CONVERTER_FLAGS)
# Not quite done. When you call separate_arguments on a cache variable,
# the result is written to a local variable. That local variable goes
# away when this function returns (which is before any of them are used).
# So, copy these variables with local scope to cache variables with
# global scope.
set(LATEX_COMPILER_ARGS "${LATEX_COMPILER_FLAGS}" CACHE INTERNAL "")
set(PDFLATEX_COMPILER_ARGS "${PDFLATEX_COMPILER_FLAGS}" CACHE INTERNAL "")
set(HTLATEX_COMPILER_ARGS "${HTLATEX_COMPILER_LATEX_FLAGS}" CACHE INTERNAL "")
set(LATEX_SYNCTEX_ARGS "${LATEX_SYNCTEX_FLAGS}" CACHE INTERNAL "")
set(BIBTEX_COMPILER_ARGS "${BIBTEX_COMPILER_FLAGS}" CACHE INTERNAL "")
set(BIBER_COMPILER_ARGS "${BIBER_COMPILER_FLAGS}" CACHE INTERNAL "")
set(MAKEINDEX_COMPILER_ARGS "${MAKEINDEX_COMPILER_FLAGS}" CACHE INTERNAL "")
set(MAKEGLOSSARIES_COMPILER_ARGS "${MAKEGLOSSARIES_COMPILER_FLAGS}" CACHE INTERNAL "")
set(MAKENOMENCLATURE_COMPILER_ARGS "${MAKENOMENCLATURE_COMPILER_FLAGS}" CACHE INTERNAL "")
set(DVIPS_CONVERTER_ARGS "${DVIPS_CONVERTER_FLAGS}" CACHE INTERNAL "")
set(PS2PDF_CONVERTER_ARGS "${PS2PDF_CONVERTER_FLAGS}" CACHE INTERNAL "")
set(PDFTOPS_CONVERTER_ARGS "${PDFTOPS_CONVERTER_FLAGS}" CACHE INTERNAL "")
find_program(IMAGEMAGICK_CONVERT
NAMES magick convert
DOC "The convert program that comes with ImageMagick (available at http://www.imagemagick.org)."
)
mark_as_advanced(IMAGEMAGICK_CONVERT)
if(DEFINED ENV{LATEX_DEFAULT_BUILD})
set(default_build $ENV{LATEX_DEFAULT_BUILD})
else()
set(default_build pdf)
endif()
set(LATEX_DEFAULT_BUILD "${default_build}" CACHE STRING
"Choose the default type of LaTeX build. Valid options are pdf, dvi, ps, safepdf, html"
)
set_property(CACHE LATEX_DEFAULT_BUILD
PROPERTY STRINGS pdf dvi ps safepdf html
)
option(LATEX_USE_SYNCTEX
"If on, have LaTeX generate a synctex file, which WYSIWYG editors can use to correlate output files like dvi and pdf with the lines of LaTeX source that generates them. In addition to adding the LATEX_SYNCTEX_FLAGS to the command line, this option also adds build commands that \"corrects\" the resulting synctex file to point to the original LaTeX files rather than those generated by UseLATEX.cmake."
OFF
)
option(LATEX_SMALL_IMAGES
"If on, the raster images will be converted to 1/6 the original size. This is because papers usually require 600 dpi images whereas most monitors only require at most 96 dpi. Thus, smaller images make smaller files for web distribution and can make it faster to read dvi files."
OFF)
if(LATEX_SMALL_IMAGES)
set(LATEX_RASTER_SCALE 16 PARENT_SCOPE)
set(LATEX_OPPOSITE_RASTER_SCALE 100 PARENT_SCOPE)
else()
set(LATEX_RASTER_SCALE 100 PARENT_SCOPE)
set(LATEX_OPPOSITE_RASTER_SCALE 16 PARENT_SCOPE)
endif()
# Just holds extensions for known image types. They should all be lower case.
# For historical reasons, these are all declared in the global scope.
set(LATEX_DVI_VECTOR_IMAGE_EXTENSIONS .eps CACHE INTERNAL "")
set(LATEX_DVI_RASTER_IMAGE_EXTENSIONS CACHE INTERNAL "")
set(LATEX_DVI_IMAGE_EXTENSIONS
${LATEX_DVI_VECTOR_IMAGE_EXTENSIONS}
${LATEX_DVI_RASTER_IMAGE_EXTENSIONS}
CACHE INTERNAL ""
)
set(LATEX_PDF_VECTOR_IMAGE_EXTENSIONS .pdf CACHE INTERNAL "")
set(LATEX_PDF_RASTER_IMAGE_EXTENSIONS .jpeg .jpg .png CACHE INTERNAL "")
set(LATEX_PDF_IMAGE_EXTENSIONS
${LATEX_PDF_VECTOR_IMAGE_EXTENSIONS}
${LATEX_PDF_RASTER_IMAGE_EXTENSIONS}
CACHE INTERNAL ""
)
set(LATEX_OTHER_VECTOR_IMAGE_EXTENSIONS .ai .dot .svg CACHE INTERNAL "")
set(LATEX_OTHER_RASTER_IMAGE_EXTENSIONS
.bmp .bmp2 .bmp3 .dcm .dcx .ico .gif .pict .ppm .tif .tiff
CACHE INTERNAL "")
set(LATEX_OTHER_IMAGE_EXTENSIONS
${LATEX_OTHER_VECTOR_IMAGE_EXTENSIONS}
${LATEX_OTHER_RASTER_IMAGE_EXTENSIONS}
CACHE INTERNAL ""
)
set(LATEX_VECTOR_IMAGE_EXTENSIONS
${LATEX_DVI_VECTOR_IMAGE_EXTENSIONS}
${LATEX_PDF_VECTOR_IMAGE_EXTENSIONS}
${LATEX_OTHER_VECTOR_IMAGE_EXTENSIONS}
CACHE INTERNAL ""
)
set(LATEX_RASTER_IMAGE_EXTENSIONS
${LATEX_DVI_RASTER_IMAGE_EXTENSIONS}
${LATEX_PDF_RASTER_IMAGE_EXTENSIONS}
${LATEX_OTHER_RASTER_IMAGE_EXTENSIONS}
CACHE INTERNAL ""
)
set(LATEX_IMAGE_EXTENSIONS
${LATEX_DVI_IMAGE_EXTENSIONS}
${LATEX_PDF_IMAGE_EXTENSIONS}
${LATEX_OTHER_IMAGE_EXTENSIONS}
CACHE INTERNAL ""
)
endfunction(latex_setup_variables)
function(latex_setup_targets)
if(NOT TARGET pdf)
add_custom_target(pdf)
endif()
if(NOT TARGET dvi)
add_custom_target(dvi)
endif()
if(NOT TARGET ps)
add_custom_target(ps)
endif()
if(NOT TARGET safepdf)
add_custom_target(safepdf)
endif()
if(NOT TARGET html)
add_custom_target(html)
endif()
if(NOT TARGET auxclean)
add_custom_target(auxclean)
endif()
endfunction(latex_setup_targets)
function(latex_get_output_path var)
set(latex_output_path)
if(LATEX_OUTPUT_PATH)
get_filename_component(
LATEX_OUTPUT_PATH_FULL "${LATEX_OUTPUT_PATH}" ABSOLUTE
)
if("${LATEX_OUTPUT_PATH_FULL}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
message(SEND_ERROR "You cannot set LATEX_OUTPUT_PATH to the same directory that contains LaTeX input files.")
else()
set(latex_output_path "${LATEX_OUTPUT_PATH_FULL}")
endif()
else()
if("${CMAKE_CURRENT_BINARY_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
message(SEND_ERROR "LaTeX files must be built out of source or you must set LATEX_OUTPUT_PATH.")
else()
set(latex_output_path "${CMAKE_CURRENT_BINARY_DIR}")
endif()
endif()
set(${var} ${latex_output_path} PARENT_SCOPE)
endfunction(latex_get_output_path)
function(latex_add_convert_command
output_path
input_path
output_extension
input_extension
flags
)
set(require_imagemagick_convert TRUE)
set(convert_flags "")
if(${input_extension} STREQUAL ".eps" AND ${output_extension} STREQUAL ".pdf")
# ImageMagick has broken eps to pdf conversion
# use ps2pdf instead
if(PS2PDF_CONVERTER)
set(require_imagemagick_convert FALSE)
set(converter ${PS2PDF_CONVERTER})
set(convert_flags -dEPSCrop ${PS2PDF_CONVERTER_ARGS})
else()
message(SEND_ERROR "Using postscript files with pdflatex requires ps2pdf for conversion.")
endif()
elseif(${input_extension} STREQUAL ".pdf" AND ${output_extension} STREQUAL ".eps")
# ImageMagick can also be sketchy on pdf to eps conversion. Not good with
# color spaces and tends to unnecessarily rasterize.
# use pdftops instead
if(PDFTOPS_CONVERTER)
set(require_imagemagick_convert FALSE)
set(converter ${PDFTOPS_CONVERTER})
set(convert_flags -eps ${PDFTOPS_CONVERTER_ARGS})
else()
message(STATUS "Consider getting pdftops from Poppler to convert PDF images to EPS images.")
set(convert_flags ${flags})
endif()
else()
set(convert_flags ${flags})
endif()
if(require_imagemagick_convert)
if(IMAGEMAGICK_CONVERT)
string(TOLOWER ${IMAGEMAGICK_CONVERT} IMAGEMAGICK_CONVERT_LOWERCASE)
if(${IMAGEMAGICK_CONVERT_LOWERCASE} MATCHES "system32[/\\\\]convert\\.exe")
message(SEND_ERROR "IMAGEMAGICK_CONVERT set to Window's convert.exe for changing file systems rather than ImageMagick's convert for changing image formats. Please make sure ImageMagick is installed (available at http://www.imagemagick.org). If you have a recent version of ImageMagick (7.0 or higher), use the magick program instead of convert for IMAGEMAGICK_CONVERT.")
else()
set(converter ${IMAGEMAGICK_CONVERT})
# ImageMagick requires a special order of arguments where resize and
# arguments of that nature must be placed after the input image path.
add_custom_command(OUTPUT ${output_path}
COMMAND ${converter}
ARGS ${input_path} ${convert_flags} ${output_path}
DEPENDS ${input_path}
)
endif()
else()
message(SEND_ERROR "Could not find convert program. Please download ImageMagick from http://www.imagemagick.org and install.")
endif()
else() # Not ImageMagick convert
add_custom_command(OUTPUT ${output_path}
COMMAND ${converter}
ARGS ${convert_flags} ${input_path} ${output_path}
DEPENDS ${input_path}
)
endif()
endfunction(latex_add_convert_command)
# Makes custom commands to convert a file to a particular type.
function(latex_convert_image
output_files_var
input_file
output_extension
convert_flags
output_extensions
other_files
)
set(output_file_list)
set(input_dir ${CMAKE_CURRENT_SOURCE_DIR})
latex_get_output_path(output_dir)
latex_get_filename_component(extension "${input_file}" EXT)
# Check input filename for potential problems with LaTeX.
latex_get_filename_component(name "${input_file}" NAME_WE)
set(suggested_name "${name}")
if(suggested_name MATCHES ".*\\..*")
string(REPLACE "." "-" suggested_name "${suggested_name}")
endif()
if(suggested_name MATCHES ".* .*")
string(REPLACE " " "-" suggested_name "${suggested_name}")
endif()
if(NOT suggested_name STREQUAL name)
message(WARNING "Some LaTeX distributions have problems with image file names with multiple extensions or spaces. Consider changing ${name}${extension} to something like ${suggested_name}${extension}.")
endif()
string(REGEX REPLACE "\\.[^.]*\$" ${output_extension} output_file
"${input_file}")
latex_list_contains(is_type ${extension} ${output_extensions})
if(is_type)
if(convert_flags)
latex_add_convert_command(${output_dir}/${output_file}
${input_dir}/${input_file} ${output_extension} ${extension}
"${convert_flags}")
set(output_file_list ${output_dir}/${output_file})
else()
# As a shortcut, we can just copy the file.
add_custom_command(OUTPUT ${output_dir}/${input_file}
COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${input_dir}/${input_file} ${output_dir}/${input_file}
DEPENDS ${input_dir}/${input_file}
)
set(output_file_list ${output_dir}/${input_file})
endif()
else()
set(do_convert TRUE)
# Check to see if there is another input file of the appropriate type.
foreach(valid_extension ${output_extensions})
string(REGEX REPLACE "\\.[^.]*\$" ${output_extension} try_file
"${input_file}")
latex_list_contains(has_native_file "${try_file}" ${other_files})
if(has_native_file)
set(do_convert FALSE)
endif()
endforeach(valid_extension)
# If we still need to convert, do it.
if(do_convert)
latex_add_convert_command(${output_dir}/${output_file}
${input_dir}/${input_file} ${output_extension} ${extension}
"${convert_flags}")
set(output_file_list ${output_dir}/${output_file})
endif()
endif()
set(${output_files_var} ${output_file_list} PARENT_SCOPE)
endfunction(latex_convert_image)
# Adds custom commands to process the given files for dvi and pdf builds.
# Adds the output files to the given variables (does not replace).
function(latex_process_images dvi_outputs_var pdf_outputs_var)
latex_get_output_path(output_dir)
set(dvi_outputs)
set(pdf_outputs)
foreach(file ${ARGN})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
latex_get_filename_component(extension "${file}" EXT)
set(convert_flags)
# Check to see if we need to downsample the image.
latex_list_contains(is_raster "${extension}"
${LATEX_RASTER_IMAGE_EXTENSIONS})
if(LATEX_SMALL_IMAGES)
if(is_raster)
set(convert_flags -resize ${LATEX_RASTER_SCALE}%)
endif()
endif()
# Make sure the output directory exists.
latex_get_filename_component(path "${output_dir}/${file}" PATH)
make_directory("${path}")
latex_convert_image(output_files "${file}" .pdf "${convert_flags}"
"${LATEX_PDF_IMAGE_EXTENSIONS}" "${ARGN}")
list(APPEND pdf_outputs ${output_files})
else()
message(WARNING "Could not find file ${CMAKE_CURRENT_SOURCE_DIR}/${file}. Are you sure you gave relative paths to IMAGES?")
endif()
endforeach(file)
set(${dvi_outputs_var} ${dvi_outputs} PARENT_SCOPE)
set(${pdf_outputs_var} ${pdf_outputs} PARENT_SCOPE)
endfunction(latex_process_images)
function(latex_copy_globbed_files pattern dest)
file(GLOB file_list ${pattern})
foreach(in_file ${file_list})
latex_get_filename_component(out_file ${in_file} NAME)
configure_file(${in_file} ${dest}/${out_file} COPYONLY)
endforeach(in_file)
endfunction(latex_copy_globbed_files)
function(latex_copy_input_file file)
latex_get_output_path(output_dir)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file})
latex_get_filename_component(path ${file} PATH)
file(MAKE_DIRECTORY ${output_dir}/${path})
latex_list_contains(use_config ${file} ${LATEX_CONFIGURE})
if(use_config)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${file}
${output_dir}/${file}
@ONLY
)
add_custom_command(OUTPUT ${output_dir}/${file}
COMMAND ${CMAKE_COMMAND}
ARGS ${CMAKE_BINARY_DIR}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file}
)
else()
add_custom_command(OUTPUT ${output_dir}/${file}
COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${file} ${output_dir}/${file}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file}
)
endif()
else()
if(EXISTS ${output_dir}/${file})
# Special case: output exists but input does not. Assume that it was
# created elsewhere and skip the input file copy.
else()
message("Could not find input file ${CMAKE_CURRENT_SOURCE_DIR}/${file}")
endif()
endif()
endfunction(latex_copy_input_file)
#############################################################################
# Commands provided by the UseLATEX.cmake "package"
#############################################################################
function(latex_usage command message)
message(SEND_ERROR
"${message}\n Usage: ${command}(<tex_file>\n [BIBFILES <bib_file> <bib_file> ...]\n [INPUTS <tex_file> <tex_file> ...]\n [IMAGE_DIRS <directory1> <directory2> ...]\n [IMAGES <image_file1> <image_file2>\n [CONFIGURE <tex_file> <tex_file> ...]\n [DEPENDS <tex_file> <tex_file> ...]\n [MULTIBIB_NEWCITES] <suffix_list>\n [USE_BIBLATEX] [USE_INDEX] [USE_GLOSSARY] [USE_NOMENCL]\n [FORCE_PDF] [FORCE_DVI] [FORCE_HTML]\n [TARGET_NAME] <name>\n [EXCLUDE_FROM_ALL]\n [EXCLUDE_FROM_DEFAULTS])"
)
endfunction(latex_usage command message)
# Parses arguments to add_latex_document and ADD_LATEX_TARGETS and sets the
# variables LATEX_TARGET, LATEX_IMAGE_DIR, LATEX_BIBFILES, LATEX_DEPENDS, and
# LATEX_INPUTS.
function(parse_add_latex_arguments command latex_main_input)
set(options
USE_BIBLATEX
USE_INDEX
USE_GLOSSARY
USE_NOMENCL
FORCE_PDF
FORCE_DVI
FORCE_HTML
EXCLUDE_FROM_ALL
EXCLUDE_FROM_DEFAULTS
# Deprecated options
USE_GLOSSARIES
DEFAULT_PDF
DEFAULT_SAFEPDF
DEFAULT_PS
NO_DEFAULT
MANGLE_TARGET_NAMES
)
set(oneValueArgs
TARGET_NAME
)
set(multiValueArgs
BIBFILES
MULTIBIB_NEWCITES
INPUTS
IMAGE_DIRS
IMAGES
CONFIGURE
DEPENDS
INDEX_NAMES
)
cmake_parse_arguments(
LATEX "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
# Handle invalid and deprecated arguments
if(LATEX_UNPARSED_ARGUMENTS)
latex_usage(${command} "Invalid or deprecated arguments: ${LATEX_UNPARSED_ARGUMENTS}")
endif()
if(LATEX_USE_GLOSSARIES)
latex_usage(${command} "USE_GLOSSARIES option removed in version 1.6.1. Use USE_GLOSSARY instead.")
endif()
if(LATEX_DEFAULT_PDF)
latex_usage(${command} "DEFAULT_PDF option removed in version 2.0. Use FORCE_PDF option or LATEX_DEFAULT_BUILD CMake variable instead.")
endif()
if(LATEX_DEFAULT_SAFEPDF)
latex_usage(${command} "DEFAULT_SAFEPDF option removed in version 2.0. Use LATEX_DEFAULT_BUILD CMake variable instead.")
endif()
if(LATEX_DEFAULT_DVI)
latex_usage(${command} "DEFAULT_DVI option removed in version 2.0. Use FORCE_DVI option or LATEX_DEFAULT_BUILD CMake variable instead.")
endif()
if(LATEX_NO_DEFAULT)
latex_usage(${command} "NO_DEFAULT option removed in version 2.0. Use EXCLUDE_FROM_ALL instead.")
endif()
if(LATEX_MANGLE_TARGET_NAMES)
latex_usage(${command} "MANGLE_TARGET_NAMES option removed in version 2.0. All LaTeX targets use mangled names now.")
endif()
# Capture the first argument, which is the main LaTeX input.
latex_get_filename_component(latex_target ${latex_main_input} NAME_WE)
set(LATEX_MAIN_INPUT ${latex_main_input} PARENT_SCOPE)
set(LATEX_TARGET ${latex_target} PARENT_SCOPE)
# Propagate the result variables to the caller
foreach(arg_name ${options} ${oneValueArgs} ${multiValueArgs})
set(var_name LATEX_${arg_name})
set(${var_name} ${${var_name}} PARENT_SCOPE)
endforeach(arg_name)
endfunction(parse_add_latex_arguments)
function(add_latex_targets_internal)
latex_get_output_path(output_dir)
if(LATEX_USE_SYNCTEX)
set(synctex_flags ${LATEX_SYNCTEX_ARGS})
else()
set(synctex_flags)
endif()
# The commands to run LaTeX. They are repeated multiple times.
set(latex_build_command
${LATEX_COMPILER} ${LATEX_COMPILER_ARGS} ${synctex_flags} ${LATEX_MAIN_INPUT}
)
if(LATEX_COMPILER_ARGS MATCHES ".*batchmode.*")
# Wrap command in script that dumps the log file on error. This makes sure
# errors can be seen.
set(latex_build_command
${CMAKE_COMMAND}
-D LATEX_BUILD_COMMAND=execute_latex
-D LATEX_TARGET=${LATEX_TARGET}
-D LATEX_WORKING_DIRECTORY="${output_dir}"
-D LATEX_FULL_COMMAND="${latex_build_command}"
-D LATEX_OUTPUT_FILE="${LATEX_TARGET}.dvi"
-P "${LATEX_USE_LATEX_LOCATION}"
)
endif()
set(pdflatex_build_command
${PDFLATEX_COMPILER} ${PDFLATEX_COMPILER_ARGS} ${synctex_flags} ${LATEX_MAIN_INPUT}
)
if(PDFLATEX_COMPILER_ARGS MATCHES ".*batchmode.*")
# Wrap command in script that dumps the log file on error. This makes sure
# errors can be seen.
set(pdflatex_build_command
${CMAKE_COMMAND}
-D LATEX_BUILD_COMMAND=execute_latex
-D LATEX_TARGET=${LATEX_TARGET}
-D LATEX_WORKING_DIRECTORY="${output_dir}"
-D LATEX_FULL_COMMAND="${pdflatex_build_command}"
-D LATEX_OUTPUT_FILE="${LATEX_TARGET}.pdf"
-P "${LATEX_USE_LATEX_LOCATION}"
)
endif()
if(NOT LATEX_TARGET_NAME)
# Use the main filename (minus the .tex) as the target name. Remove any
# spaces since CMake cannot have spaces in its target names.
string(REPLACE " " "_" LATEX_TARGET_NAME ${LATEX_TARGET})
endif()
# Some LaTeX commands may need to be modified (or may not work) if the main
# tex file is in a subdirectory. Make a flag for that.
get_filename_component(LATEX_MAIN_INPUT_SUBDIR ${LATEX_MAIN_INPUT} DIRECTORY)
# Set up target names.
set(dvi_target ${LATEX_TARGET_NAME}_dvi)
set(pdf_target ${LATEX_TARGET_NAME}_pdf)
set(ps_target ${LATEX_TARGET_NAME}_ps)
set(safepdf_target ${LATEX_TARGET_NAME}_safepdf)
set(html_target ${LATEX_TARGET_NAME}_html)
set(auxclean_target ${LATEX_TARGET_NAME}_auxclean)
# Probably not all of these will be generated, but they could be.
# Note that the aux file is added later.
set(auxiliary_clean_files
${output_dir}/${LATEX_TARGET}.aux
${output_dir}/${LATEX_TARGET}.bbl
${output_dir}/${LATEX_TARGET}.blg
${output_dir}/${LATEX_TARGET}-blx.bib
${output_dir}/${LATEX_TARGET}.glg
${output_dir}/${LATEX_TARGET}.glo
${output_dir}/${LATEX_TARGET}.gls
${output_dir}/${LATEX_TARGET}.idx
${output_dir}/${LATEX_TARGET}.ilg
${output_dir}/${LATEX_TARGET}.ind
${output_dir}/${LATEX_TARGET}.ist
${output_dir}/${LATEX_TARGET}.log
${output_dir}/${LATEX_TARGET}.out
${output_dir}/${LATEX_TARGET}.toc
${output_dir}/${LATEX_TARGET}.lof
${output_dir}/${LATEX_TARGET}.xdy
${output_dir}/${LATEX_TARGET}.synctex.gz
${output_dir}/${LATEX_TARGET}.synctex.bak.gz
${output_dir}/${LATEX_TARGET}.dvi
${output_dir}/${LATEX_TARGET}.ps
${output_dir}/${LATEX_TARGET}.pdf
)
set(image_list ${LATEX_IMAGES})
# For each directory in LATEX_IMAGE_DIRS, glob all the image files and
# place them in LATEX_IMAGES.
foreach(dir ${LATEX_IMAGE_DIRS})
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${dir})
message(WARNING "Image directory ${CMAKE_CURRENT_SOURCE_DIR}/${dir} does not exist. Are you sure you gave relative directories to IMAGE_DIRS?")
endif()
foreach(extension ${LATEX_IMAGE_EXTENSIONS})
file(GLOB files ${CMAKE_CURRENT_SOURCE_DIR}/${dir}/*${extension})
foreach(file ${files})
latex_get_filename_component(filename ${file} NAME)
list(APPEND image_list ${dir}/${filename})
endforeach(file)
endforeach(extension)
endforeach(dir)
latex_process_images(dvi_images pdf_images ${image_list})
set(make_dvi_command
${CMAKE_COMMAND} -E chdir ${output_dir}
${latex_build_command})
set(make_pdf_command
${CMAKE_COMMAND} -E chdir ${output_dir}
${pdflatex_build_command}
)
set(make_dvi_depends ${LATEX_DEPENDS} ${dvi_images})
set(make_pdf_depends ${LATEX_DEPENDS} ${pdf_images})
foreach(input ${LATEX_MAIN_INPUT} ${LATEX_INPUTS})
list(APPEND make_dvi_depends ${output_dir}/${input})
list(APPEND make_pdf_depends ${output_dir}/${input})
if(${input} MATCHES "\\.tex$")
# Dependent .tex files might have their own .aux files created. Make
# sure these get cleaned as well. This might replicate the cleaning
# of the main .aux file, which is OK.
string(REGEX REPLACE "\\.tex$" "" input_we ${input})
list(APPEND auxiliary_clean_files
${output_dir}/${input_we}.aux
${output_dir}/${input}.aux
)
endif()
endforeach(input)
set(all_latex_sources ${LATEX_MAIN_INPUT} ${LATEX_INPUTS} ${image_list})
if(LATEX_USE_GLOSSARY)
foreach(dummy 0 1) # Repeat these commands twice.
set(make_dvi_command ${make_dvi_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${CMAKE_COMMAND}
-D LATEX_BUILD_COMMAND=makeglossaries
-D LATEX_TARGET=${LATEX_TARGET}
-D MAKEINDEX_COMPILER=${MAKEINDEX_COMPILER}
-D XINDY_COMPILER=${XINDY_COMPILER}
-D MAKEGLOSSARIES_COMPILER_ARGS=${MAKEGLOSSARIES_COMPILER_ARGS}
-P ${LATEX_USE_LATEX_LOCATION}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${latex_build_command}
)
set(make_pdf_command ${make_pdf_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${CMAKE_COMMAND}
-D LATEX_BUILD_COMMAND=makeglossaries
-D LATEX_TARGET=${LATEX_TARGET}
-D MAKEINDEX_COMPILER=${MAKEINDEX_COMPILER}
-D XINDY_COMPILER=${XINDY_COMPILER}
-D MAKEGLOSSARIES_COMPILER_ARGS=${MAKEGLOSSARIES_COMPILER_ARGS}
-P ${LATEX_USE_LATEX_LOCATION}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${pdflatex_build_command}
)
endforeach(dummy)
endif()
if(LATEX_USE_NOMENCL)
foreach(dummy 0 1) # Repeat these commands twice.
set(make_dvi_command ${make_dvi_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${CMAKE_COMMAND}
-D LATEX_BUILD_COMMAND=makenomenclature
-D LATEX_TARGET=${LATEX_TARGET}
-D MAKEINDEX_COMPILER=${MAKEINDEX_COMPILER}
-D MAKENOMENCLATURE_COMPILER_ARGS=${MAKENOMENCLATURE_COMPILER_ARGS}
-P ${LATEX_USE_LATEX_LOCATION}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${latex_build_command}
)
set(make_pdf_command ${make_pdf_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${CMAKE_COMMAND}
-D LATEX_BUILD_COMMAND=makenomenclature
-D LATEX_TARGET=${LATEX_TARGET}
-D MAKEINDEX_COMPILER=${MAKEINDEX_COMPILER}
-D MAKENOMENCLATURE_COMPILER_ARGS=${MAKENOMENCLATURE_COMPILER_ARGS}
-P ${LATEX_USE_LATEX_LOCATION}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${pdflatex_build_command}
)
endforeach(dummy)
endif()
if(LATEX_BIBFILES)
if(LATEX_USE_BIBLATEX)
if(NOT BIBER_COMPILER)
message(SEND_ERROR "I need the biber command.")
endif()
set(bib_compiler ${BIBER_COMPILER})
set(bib_compiler_flags ${BIBER_COMPILER_ARGS})
else()
set(bib_compiler ${BIBTEX_COMPILER})
set(bib_compiler_flags ${BIBTEX_COMPILER_ARGS})
endif()
if(LATEX_MULTIBIB_NEWCITES)
foreach (multibib_auxfile ${LATEX_MULTIBIB_NEWCITES})
latex_get_filename_component(multibib_target ${multibib_auxfile} NAME_WE)
set(make_dvi_command ${make_dvi_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${bib_compiler} ${bib_compiler_flags} ${multibib_target})
set(make_pdf_command ${make_pdf_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${bib_compiler} ${bib_compiler_flags} ${multibib_target})
set(auxiliary_clean_files ${auxiliary_clean_files}
${output_dir}/${multibib_target}.aux)
endforeach (multibib_auxfile ${LATEX_MULTIBIB_NEWCITES})
else()
set(make_dvi_command ${make_dvi_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${bib_compiler} ${bib_compiler_flags} ${LATEX_TARGET})
set(make_pdf_command ${make_pdf_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${bib_compiler} ${bib_compiler_flags} ${LATEX_TARGET})
endif()
foreach (bibfile ${LATEX_BIBFILES})
list(APPEND make_dvi_depends ${output_dir}/${bibfile})
list(APPEND make_pdf_depends ${output_dir}/${bibfile})
endforeach (bibfile ${LATEX_BIBFILES})
else()
if(LATEX_MULTIBIB_NEWCITES)
message(WARNING "MULTIBIB_NEWCITES has no effect without BIBFILES option.")
endif()
endif()
if(LATEX_USE_INDEX)
if(LATEX_INDEX_NAMES)
set(INDEX_NAMES ${LATEX_INDEX_NAMES})
else()
set(INDEX_NAMES ${LATEX_TARGET})
endif()
foreach(idx_name ${INDEX_NAMES})
set(make_dvi_command ${make_dvi_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${latex_build_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${MAKEINDEX_COMPILER} ${MAKEINDEX_COMPILER_ARGS} ${idx_name}.idx)
set(make_pdf_command ${make_pdf_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${pdflatex_build_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${MAKEINDEX_COMPILER} ${MAKEINDEX_COMPILER_ARGS} ${idx_name}.idx)
set(auxiliary_clean_files ${auxiliary_clean_files}
${output_dir}/${idx_name}.idx
${output_dir}/${idx_name}.ilg
${output_dir}/${idx_name}.ind)
endforeach()
else()
if(LATEX_INDEX_NAMES)
message(WARNING "INDEX_NAMES has no effect without USE_INDEX option.")
endif()
endif()
set(make_dvi_command ${make_dvi_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${latex_build_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${latex_build_command})
set(make_pdf_command ${make_pdf_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${pdflatex_build_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${pdflatex_build_command})
# Need to run one more time to remove biblatex' warning
# about page breaks that have changed.
if(LATEX_USE_BIBLATEX)
set(make_dvi_command ${make_dvi_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${latex_build_command})
set(make_pdf_command ${make_pdf_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${pdflatex_build_command})
endif()
if(LATEX_USE_SYNCTEX)
if(NOT GZIP)
message(SEND_ERROR "UseLATEX.cmake: USE_SYNTEX option requires gzip program. Set GZIP variable.")
endif()
set(make_dvi_command ${make_dvi_command}
COMMAND ${CMAKE_COMMAND}
-D LATEX_BUILD_COMMAND=correct_synctex
-D LATEX_TARGET=${LATEX_TARGET}
-D GZIP=${GZIP}
-D "LATEX_SOURCE_DIRECTORY=${CMAKE_CURRENT_SOURCE_DIR}"
-D "LATEX_BINARY_DIRECTORY=${output_dir}"
-P ${LATEX_USE_LATEX_LOCATION}
)
set(make_pdf_command ${make_pdf_command}
COMMAND ${CMAKE_COMMAND}
-D LATEX_BUILD_COMMAND=correct_synctex
-D LATEX_TARGET=${LATEX_TARGET}
-D GZIP=${GZIP}
-D "LATEX_SOURCE_DIRECTORY=${CMAKE_CURRENT_SOURCE_DIR}"
-D "LATEX_BINARY_DIRECTORY=${output_dir}"
-P ${LATEX_USE_LATEX_LOCATION}
)
endif()
# Check LaTeX output for important warnings at end of build
set(make_dvi_command ${make_dvi_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${CMAKE_COMMAND}
-D LATEX_BUILD_COMMAND=check_important_warnings
-D LATEX_TARGET=${LATEX_TARGET}
-P ${LATEX_USE_LATEX_LOCATION}
)
set(make_pdf_command ${make_pdf_command}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${CMAKE_COMMAND}
-D LATEX_BUILD_COMMAND=check_important_warnings
-D LATEX_TARGET=${LATEX_TARGET}
-P ${LATEX_USE_LATEX_LOCATION}
)
# Capture the default build.
string(TOLOWER "${LATEX_DEFAULT_BUILD}" default_build)
if((NOT LATEX_FORCE_PDF) AND (NOT LATEX_FORCE_DVI) AND (NOT LATEX_FORCE_HTML))
set(no_force TRUE)
endif()
# Add commands and targets for building pdf outputs (with pdflatex).
if(LATEX_FORCE_PDF OR no_force)
if(LATEX_FORCE_PDF)
set(default_build pdf)
endif()
if(PDFLATEX_COMPILER)
add_custom_command(OUTPUT ${output_dir}/${LATEX_TARGET}.pdf
COMMAND ${make_pdf_command}
DEPENDS ${make_pdf_depends}
)
add_custom_target(${pdf_target}
DEPENDS ${output_dir}/${LATEX_TARGET}.pdf
SOURCES ${all_latex_sources}
)
if(NOT LATEX_EXCLUDE_FROM_DEFAULTS)
add_dependencies(pdf ${pdf_target})
endif()
endif()
endif()
# Add commands and targets for building dvi outputs.
if(LATEX_FORCE_DVI OR LATEX_FORCE_HTML OR no_force)
if(LATEX_FORCE_DVI)
if((NOT default_build STREQUAL dvi) AND
(NOT default_build STREQUAL ps) AND
(NOT default_build STREQUAL safepdf))
set(default_build dvi)
endif()
endif()
add_custom_command(OUTPUT ${output_dir}/${LATEX_TARGET}.dvi
COMMAND ${make_dvi_command}
DEPENDS ${make_dvi_depends}
)
add_custom_target(${dvi_target}
DEPENDS ${output_dir}/${LATEX_TARGET}.dvi
SOURCES ${all_latex_sources}
)
if(NOT LATEX_EXCLUDE_FROM_DEFAULTS)
add_dependencies(dvi ${dvi_target})
endif()
if(DVIPS_CONVERTER)
add_custom_command(OUTPUT ${output_dir}/${LATEX_TARGET}.ps
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${DVIPS_CONVERTER} ${DVIPS_CONVERTER_ARGS} -o ${LATEX_TARGET}.ps ${LATEX_TARGET}.dvi
DEPENDS ${output_dir}/${LATEX_TARGET}.dvi)
add_custom_target(${ps_target}
DEPENDS ${output_dir}/${LATEX_TARGET}.ps
SOURCES ${all_latex_sources}
)
if(NOT LATEX_EXCLUDE_FROM_DEFAULTS)
add_dependencies(ps ${ps_target})
endif()
if(PS2PDF_CONVERTER)
# Since both the pdf and safepdf targets have the same output, we
# cannot properly do the dependencies for both. When selecting safepdf,
# simply force a recompile every time.
add_custom_target(${safepdf_target}
${CMAKE_COMMAND} -E chdir ${output_dir}
${PS2PDF_CONVERTER} ${PS2PDF_CONVERTER_ARGS} ${LATEX_TARGET}.ps ${LATEX_TARGET}.pdf
DEPENDS ${ps_target}
)
if(NOT LATEX_EXCLUDE_FROM_DEFAULTS)
add_dependencies(safepdf ${safepdf_target})
endif()
endif()
endif()
endif()
if(LATEX_FORCE_HTML OR no_force)
if (LATEX_FORCE_HTML)
set(default_build html)
endif()
if(HTLATEX_COMPILER AND LATEX_MAIN_INPUT_SUBDIR)
message(STATUS
"Disabling HTML build for ${LATEX_TARGET_NAME}.tex because the main file is in subdirectory ${LATEX_MAIN_INPUT_SUBDIR}"
)
# The code below to run HTML assumes that LATEX_TARGET.tex is in the
# current directory. I have tried to specify that LATEX_TARGET.tex is
# in a subdirectory. That makes the build targets correct, but the
# HTML build still fails (at least for htlatex) because files are not
# generated where expected. I am getting around the problem by simply
# disabling HTML in this case. If someone really cares, they can fix
# this, but make sure it runs on many platforms and build programs.
elseif(HTLATEX_COMPILER)
# htlatex places the output in a different location
set(HTML_OUTPUT "${output_dir}/${LATEX_TARGET}.html")
add_custom_command(OUTPUT ${HTML_OUTPUT}
COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
${HTLATEX_COMPILER} ${LATEX_MAIN_INPUT}
"${HTLATEX_COMPILER_TEX4HT_FLAGS}"
"${HTLATEX_COMPILER_TEX4HT_POSTPROCESSOR_FLAGS}"
"${HTLATEX_COMPILER_T4HT_POSTPROCESSOR_FLAGS}"
${HTLATEX_COMPILER_ARGS}
DEPENDS
${output_dir}/${LATEX_TARGET}.tex
${output_dir}/${LATEX_TARGET}.dvi
VERBATIM
)
add_custom_target(${html_target}
DEPENDS ${HTML_OUTPUT} ${dvi_target}
SOURCES ${all_latex_sources}
)
if(NOT LATEX_EXCLUDE_FROM_DEFAULTS)
add_dependencies(html ${html_target})
endif()
endif()
endif()
# Set default targets.
if("${default_build}" STREQUAL "pdf")
add_custom_target(${LATEX_TARGET_NAME} DEPENDS ${pdf_target})
elseif("${default_build}" STREQUAL "dvi")
add_custom_target(${LATEX_TARGET_NAME} DEPENDS ${dvi_target})
elseif("${default_build}" STREQUAL "ps")
add_custom_target(${LATEX_TARGET_NAME} DEPENDS ${ps_target})
elseif("${default_build}" STREQUAL "safepdf")
add_custom_target(${LATEX_TARGET_NAME} DEPENDS ${safepdf_target})
elseif("${default_build}" STREQUAL "html")
add_custom_target(${LATEX_TARGET_NAME} DEPENDS ${html_target})
else()
message(SEND_ERROR "LATEX_DEFAULT_BUILD set to an invalid value. See the documentation for that variable.")
endif()
if(NOT LATEX_EXCLUDE_FROM_ALL)
add_custom_target(_${LATEX_TARGET_NAME} ALL DEPENDS ${LATEX_TARGET_NAME})
endif()
set_directory_properties(.
ADDITIONAL_MAKE_CLEAN_FILES "${auxiliary_clean_files}"
)
add_custom_target(${auxclean_target}
COMMENT "Cleaning auxiliary LaTeX files."
COMMAND ${CMAKE_COMMAND} -E remove ${auxiliary_clean_files}
)
add_dependencies(auxclean ${auxclean_target})
endfunction(add_latex_targets_internal)
function(add_latex_targets latex_main_input)
latex_get_output_path(output_dir)
parse_add_latex_arguments(ADD_LATEX_TARGETS ${latex_main_input} ${ARGN})
add_latex_targets_internal()
endfunction(add_latex_targets)
function(add_latex_document latex_main_input)
latex_get_output_path(output_dir)
if(output_dir)
parse_add_latex_arguments(add_latex_document ${latex_main_input} ${ARGN})
latex_copy_input_file(${LATEX_MAIN_INPUT})
foreach (bib_file ${LATEX_BIBFILES})
latex_copy_input_file(${bib_file})
endforeach (bib_file)
foreach (input ${LATEX_INPUTS})
latex_copy_input_file(${input})
endforeach(input)
latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.cls ${output_dir})
latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.bst ${output_dir})
latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.clo ${output_dir})
latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.sty ${output_dir})
latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.ist ${output_dir})
latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.fd ${output_dir})
add_latex_targets_internal()
endif()
endfunction(add_latex_document)
#############################################################################
# Actually do stuff
#############################################################################
if(LATEX_BUILD_COMMAND)
set(command_handled)
if("${LATEX_BUILD_COMMAND}" STREQUAL execute_latex)
latex_execute_latex()
set(command_handled TRUE)
endif()
if("${LATEX_BUILD_COMMAND}" STREQUAL makeglossaries)
latex_makeglossaries()
set(command_handled TRUE)
endif()
if("${LATEX_BUILD_COMMAND}" STREQUAL makenomenclature)
latex_makenomenclature()
set(command_handled TRUE)
endif()
if("${LATEX_BUILD_COMMAND}" STREQUAL correct_synctex)
latex_correct_synctex()
set(command_handled TRUE)
endif()
if("${LATEX_BUILD_COMMAND}" STREQUAL check_important_warnings)
latex_check_important_warnings()
set(command_handled TRUE)
endif()
if(NOT command_handled)
message(SEND_ERROR "Unknown command: ${LATEX_BUILD_COMMAND}")
endif()
else()
# Must be part of the actual configure (included from CMakeLists.txt).
latex_setup_variables()
latex_setup_targets()
endif()
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include <ctype.h>
#include <stack>
#include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp"
#include "caf/config.hpp"
#include "caf/detail/parser/chars.hpp"
#include "caf/detail/parser/read_bool.hpp"
#include "caf/detail/parser/read_number_or_timespan.hpp"
#include "caf/detail/parser/read_string.hpp"
#include "caf/detail/parser/read_uri.hpp"
#include "caf/detail/scope_guard.hpp"
#include "caf/exec_main.hpp"
#include "caf/pec.hpp"
#include "caf/uri_builder.hpp"
namespace {
void trim(std::string& s) {
auto not_space = [](char c) { return isspace(c) == 0; };
s.erase(s.begin(), find_if(s.begin(), s.end(), not_space));
s.erase(find_if(s.rbegin(), s.rend(), not_space).base(), s.end());
}
template <class>
struct type_name;
template <class>
struct is_inline;
#define DECLARE_NAMED_STRUCT(type, inline_flag) \
struct type; \
template <> \
struct type_name<type> { \
static constexpr const char* value = #type; \
}; \
template <> \
struct is_inline<type> { \
static constexpr bool value = inline_flag; \
}
DECLARE_NAMED_STRUCT(section, false);
DECLARE_NAMED_STRUCT(subsection, false);
DECLARE_NAMED_STRUCT(subsubsection, false);
DECLARE_NAMED_STRUCT(paragraph, false);
DECLARE_NAMED_STRUCT(label, false);
DECLARE_NAMED_STRUCT(see, true);
DECLARE_NAMED_STRUCT(sref, true);
DECLARE_NAMED_STRUCT(ref, true);
DECLARE_NAMED_STRUCT(verbatim, false);
DECLARE_NAMED_STRUCT(lstlisting, false);
DECLARE_NAMED_STRUCT(lstinline, true);
DECLARE_NAMED_STRUCT(text, true);
DECLARE_NAMED_STRUCT(texttt, true);
DECLARE_NAMED_STRUCT(textbf, true);
DECLARE_NAMED_STRUCT(textit, true);
DECLARE_NAMED_STRUCT(href, true);
DECLARE_NAMED_STRUCT(item, false);
DECLARE_NAMED_STRUCT(itemize, false);
DECLARE_NAMED_STRUCT(enumerate, false);
DECLARE_NAMED_STRUCT(tabular, false);
DECLARE_NAMED_STRUCT(cppexample, false);
DECLARE_NAMED_STRUCT(iniexample, false);
DECLARE_NAMED_STRUCT(sourcefile, false);
DECLARE_NAMED_STRUCT(singlefig, false);
DECLARE_NAMED_STRUCT(experimental, true);
using node = caf::variant<section, subsection, subsubsection, paragraph, label,
see, sref, ref, verbatim, lstlisting, lstinline, text,
texttt, textbf, textit, href, item, itemize,
enumerate, tabular, cppexample, iniexample,
sourcefile, singlefig, experimental>;
struct section {
std::string name;
};
struct subsection {
std::string name;
};
struct subsubsection {
std::string name;
};
struct paragraph {
std::string name;
};
struct label {
std::string name;
};
struct see {
std::string link;
};
struct sref {
std::string link;
};
struct ref {
std::string link;
};
struct verbatim {
std::string block;
};
struct lstlisting {
std::string block;
};
struct lstinline {
std::string str;
};
struct text {
std::string str;
};
struct texttt {
std::string str;
};
struct textbf {
std::string str;
};
struct textit {
std::string str;
};
struct href {
std::string url;
std::string str;
};
struct item {
std::vector<node> nodes;
};
struct itemize {
std::vector<item> items;
};
struct enumerate {
std::vector<item> items;
};
struct tabular {
using column_type = item;
using row_type = std::vector<column_type>;
std::vector<row_type> rows;
};
struct cppexample {
std::string lines;
std::string file;
};
struct iniexample {
std::string lines;
std::string file;
};
struct sourcefile {
std::string lines;
std::string file;
};
struct singlefig {
std::string file;
std::string caption;
std::string label;
};
struct experimental {
// nop
};
#define MAKE_NODE_0(type) \
if (name == #type) { \
if (args.size() != 0) \
throw std::runtime_error("expected exactly 0 argument for " #type \
", got: " \
+ caf::deep_to_string(args)); \
return type{}; \
}
#define MAKE_NODE_1(type) \
if (name == #type) { \
if (args.size() != 1) \
throw std::runtime_error("expected exactly 1 argument for " #type \
", got: " \
+ caf::deep_to_string(args)); \
return type{std::move(args[0])}; \
}
#define MAKE_NODE_2(type) \
if (name == #type) { \
if (args.size() != 2) \
throw std::runtime_error("expected exactly 2 argument for " #type \
", got: " \
+ caf::deep_to_string(args)); \
return type{std::move(args[0]), std::move(args[1])}; \
}
#define MAKE_NODE_3(type) \
if (name == #type) { \
if (args.size() != 3) \
throw std::runtime_error("expected exactly 3 argument for " #type \
", got: " \
+ caf::deep_to_string(args)); \
return type{std::move(args[0]), std::move(args[1]), std::move(args[2])}; \
}
#define MAKE_LINES_AND_FILE_NODE(type) \
if (name == #type) { \
if (args.size() == 1) { \
return type{std::string{}, std::move(args[0])}; \
} else if (args.size() == 2) { \
return type{std::move(args[0]), std::move(args[1])}; \
} \
throw std::runtime_error("expected 1 or 2 arguments for " #type); \
}
node make_node(const std::string& name, std::vector<std::string> args) {
MAKE_NODE_1(section)
MAKE_NODE_1(subsection)
MAKE_NODE_1(subsubsection)
MAKE_NODE_1(paragraph)
MAKE_NODE_1(label)
MAKE_NODE_1(see)
MAKE_NODE_1(sref)
MAKE_NODE_1(ref)
MAKE_NODE_1(verbatim)
MAKE_NODE_1(lstlisting)
MAKE_NODE_1(lstinline)
MAKE_NODE_1(texttt)
MAKE_NODE_1(textbf)
MAKE_NODE_1(textit)
MAKE_NODE_2(href)
MAKE_LINES_AND_FILE_NODE(cppexample)
MAKE_LINES_AND_FILE_NODE(iniexample)
MAKE_LINES_AND_FILE_NODE(sourcefile)
MAKE_NODE_3(singlefig)
MAKE_NODE_0(experimental)
if (name == "emph")
return make_node("textit", std::move(args));
throw std::runtime_error("unrecognized command: " + name
+ caf::deep_to_string(args));
}
bool is_ignored_node(const std::string& name,
const std::vector<std::string>& args) {
if (args.size() == 0)
return name == "clearpage" || name == "textwidth";
if (args.size() == 1)
return (name == "begin" || name == "end")
&& (args[0] == "center" || args[0] == "footnotesize");
return false;
}
struct abstract_consumer {
public:
virtual ~abstract_consumer() {
// nop
}
virtual void consume(node x) = 0;
};
template <class Result>
struct list_builder : abstract_consumer {
using result_type = Result;
abstract_consumer* consumer;
Result result;
bool finalized = false;
explicit list_builder(abstract_consumer* ptr) : consumer(ptr) {
// nop
}
void finalize() {
consumer->consume(std::move(result));
finalized = true;
}
void consume(node x) override {
// The command parser might pass whitespaces to this builder after seeing
// the end command.
if (finalized) {
consumer->consume(std::move(x));
return;
}
if (result.items.empty())
throw std::runtime_error("expected \\item as first token for list block");
result.items.back().nodes.emplace_back(std::move(x));
}
void cmd(const std::string& name, std::vector<std::string> args) {
if (name == "end" && args.size() == 1
&& args[0] == type_name<Result>::value) {
finalize();
} else if (name == "item") {
result.items.emplace_back();
} else {
consume(make_node(name, std::move(args)));
}
}
};
struct tabular_builder : abstract_consumer {
abstract_consumer* consumer;
tabular result;
bool finalized = false;
explicit tabular_builder(abstract_consumer* consumer) : consumer(consumer) {
next_row();
}
void consume(node x) override {
// The command parser might pass whitespaces to this builder after seeing
// the end command.
if (finalized)
consumer->consume(std::move(x));
else
result.rows.back().back().nodes.emplace_back(std::move(x));
}
void next_column() {
result.rows.back().emplace_back();
}
void next_row() {
result.rows.emplace_back();
next_column();
}
void cmd(const std::string& name, std::vector<std::string> args) {
if (name == "hline") {
// drop
} else if (name == "end" && args.size() == 1 && args[0] == "tabular") {
if (result.rows.back().empty()) {
result.rows.pop_back();
if (result.rows.empty())
throw std::runtime_error("empty table");
}
consumer->consume(std::move(result));
finalized = true;
} else {
consume(make_node(name, std::move(args)));
}
}
};
} // namespace
CAF_PUSH_UNUSED_LABEL_WARNING
#include "caf/detail/parser/fsm.hpp"
namespace {
using std::string;
using namespace caf;
using namespace caf::detail;
using namespace caf::detail::parser;
template <class State, class Consumer>
void read_tex_comment(State& ps, Consumer&&) {
start();
term_state(init){transition(done, '\n') transition(init)} term_state(done) {
// nop
}
fin();
}
template <class State, class Consumer>
void read_tex_verbatim(State& ps, Consumer&& consumer, const string& cmd_name) {
string verbatim;
string cmd;
string end_of_command = "end{" + cmd_name;
auto flush_cmd = [&] {
verbatim += '\\';
verbatim += cmd;
cmd.clear();
};
auto guard = make_scope_guard([&] {
if (ps.code <= pec::trailing_character) {
std::vector<string> args;
args.emplace_back(std::move(verbatim));
consumer.cmd(cmd_name, std::move(args));
}
});
// clang-format off
start();
state(init) {
transition(read_end_verbatim, "\\");
transition(init, any_char, verbatim += ch)
}
state(read_end_verbatim) {
transition_if(cmd == end_of_command, done, "}")
transition(init, "}", flush_cmd());
transition(read_end_verbatim, "\\", flush_cmd());
transition(read_end_verbatim, any_char, cmd += ch)
}
term_state(done) {
// nop
}
fin();
// clang-format on
}
template <class State, class Consumer>
void read_tex_list(State& ps, Consumer&& consumer, const string& cmd_name);
template <class State, class Consumer>
void read_tex_tabular(State& ps, Consumer&& consumer);
struct sub_command_consumer : abstract_consumer {
std::vector<string>& args;
explicit sub_command_consumer(std::vector<string>& args) : args(args) {
// nop
}
template <class T>
void cmd(const T& x, std::vector<std::string> args);
void consume(node x);
};
template <class State, class Consumer>
void read_tex_command(State& ps, Consumer&& consumer) {
string cmd;
string spaces;
std::vector<string> args;
const char* stop_chars;
auto add_arg = [&](const char* closing_char) {
spaces.clear();
stop_chars = closing_char;
args.emplace_back();
};
auto is_verbatim_cmd = [&] {
if (args.size() != 1)
return false;
return cmd == "begin" && (args[0] == "verbatim" || args[0] == "lstlisting");
};
auto is_list_cmd = [&] {
if (args.size() != 1)
return false;
return cmd == "begin" && (args[0] == "itemize" || args[0] == "enumerate");
};
auto is_tabular_cmd = [&] {
if (args.size() != 2)
return false;
return cmd == "begin" && args[0] == "tabular";
};
auto guard = make_scope_guard([&] {
if (is_ignored_node(cmd, args))
return;
if (cmd.empty()) {
ps.code = pec::unexpected_eof;
return;
}
if (ps.code <= pec::trailing_character) {
consumer.cmd(cmd, std::move(args));
if (!spaces.empty())
consumer.consume(text{std::move(spaces)});
}
});
sub_command_consumer scc{args};
// clang-format off
start();
state(init) {
epsilon(read_command)
}
term_state(read_command) {
fsm_epsilon(read_tex_comment(ps, consumer), read_command, '%')
fsm_epsilon_if(is_verbatim_cmd(), read_tex_verbatim(ps, consumer, args[0]), done, any_char)
fsm_epsilon_if(is_list_cmd(), read_tex_list(ps, consumer, args[0]), done, any_char)
fsm_epsilon_if(is_tabular_cmd(), read_tex_tabular(ps, consumer), done, any_char)
transition_if(args.empty() && spaces.empty(), read_command, alphanumeric_chars, cmd += ch)
transition(read_command_arg, "[", add_arg("]"))
transition(read_command_arg, "{", add_arg("}"))
transition(read_command_arg, "`", add_arg("`"))
transition(read_command_arg, "^", add_arg("^"))
transition(read_command, " \t\n", spaces += ch)
}
state(read_command_arg) {
transition(start_escaping_inside_command_arg, '\\')
fsm_epsilon(read_tex_comment(ps, consumer), read_command_arg, '%')
transition(read_command, stop_chars)
transition(read_command_arg, any_char, args.back() += ch)
}
state(start_escaping_inside_command_arg) {
transition(read_command_arg, '\\', args.back() += '\\')
transition(read_command_arg, '%', args.back() += '%')
transition(read_command_arg, '_', args.back() += '_')
fsm_epsilon(read_tex_command(ps, scc), read_command_arg, any_char)
}
term_state(done) {
guard.disable();
}
fin();
// clang-format on
}
/// Reads an .tex formatted input file for the CAF manual.
template <class State, class Consumer>
void read_tex(State& ps, Consumer&& consumer) {
string str;
auto consume_str = [&] {
if (!str.empty()) {
consumer.consume(text{std::move(str)});
str.clear();
}
};
auto guard = make_scope_guard([&] {
if (ps.code <= pec::trailing_character)
consume_str();
});
// clang-format off
start();
term_state(init) {
fsm_transition(read_tex_comment(ps, consumer), init, '%')
transition(start_escaping, '\\')
transition(init, '~', str += ' ')
transition(init, any_char, str += ch)
}
state(start_escaping) {
transition(init, '\\', str += '\\')
transition(init, '%', str += '%')
transition(init, '_', str += '_')
fsm_epsilon(read_tex_command(ps, consumer), init, any_char, consume_str())
}
fin();
// clang-format on
}
template <class State, class Consumer>
void read_tex_list(State& ps, Consumer& consumer) {
string str;
auto consume_str = [&] {
if (!str.empty()) {
consumer.consume(text{std::move(str)});
str.clear();
}
};
auto before_first_item = [&] { return consumer.result.items.empty(); };
// clang-format off
start();
state(init) {
fsm_transition(read_tex_comment(ps, consumer), init, '%')
transition(start_escaping, "\\")
transition_if(!before_first_item(), init, "~", str += ' ')
transition_if(!before_first_item(), init, any_char, str += ch)
transition_if(before_first_item(), init, " \t\n")
}
state(start_escaping) {
transition_if(!before_first_item(), init, '\\', str += '\\')
transition_if(!before_first_item(), init, '%', str += '%')
transition_if(!before_first_item(), init, '_', str += '_')
fsm_epsilon(read_tex_command(ps, consumer), after_cmd, any_char, consume_str())
}
unstable_state(after_cmd) {
epsilon_if(consumer.finalized, done, any_char)
epsilon(init, any_char)
}
term_state(done) {
// nop
}
fin();
// clang-format on
}
template <class State, class Consumer>
void read_tex_list(State& ps, Consumer&& consumer, const string& cmd_name) {
if (cmd_name == "itemize") {
list_builder<itemize> builder{&consumer};
read_tex_list(ps, builder);
} else if (cmd_name == "enumerate") {
list_builder<enumerate> builder{&consumer};
read_tex_list(ps, builder);
} else {
throw std::logic_error("expected itemize or enumerate");
}
}
template <class State, class Consumer>
void read_tex_tabular(State& ps, Consumer&& consumer) {
tabular_builder builder{&consumer};
string str;
auto consume_str = [&] {
if (!str.empty()) {
builder.consume(text{str});
str.clear();
}
};
auto next_column = [&] {
consume_str();
builder.next_column();
};
auto next_row = [&] {
consume_str();
builder.next_row();
};
// clang-format off
start();
state(init) {
fsm_transition(read_tex_comment(ps, builder), init, '%')
transition(start_escaping, '\\')
transition(init, "&", next_column())
transition(init, "~", str += ' ')
transition(init, any_char, str += ch)
}
state(start_escaping) {
transition(init, '\\', next_row())
transition(init, '%', str += '%')
transition(init, '_', str += '_')
fsm_epsilon(read_tex_command(ps, builder), after_cmd, any_char, consume_str())
}
unstable_state(after_cmd) {
epsilon_if(builder.finalized, done, any_char)
epsilon(init, any_char)
}
term_state(done) {
// nop
}
fin();
// clang-format on
}
} // namespace
#include "caf/detail/parser/fsm_undef.hpp"
CAF_POP_WARNINGS
namespace {
struct file_iter {
std::istream* f;
char ch;
explicit file_iter(std::istream* f) : f(f) {
f->get(ch);
}
file_iter() : f(nullptr), ch('\0') {
// nop
}
file_iter(const file_iter&) = default;
file_iter& operator=(const file_iter&) = default;
char operator*() const {
return ch;
}
file_iter& operator++() {
f->get(ch);
return *this;
}
};
struct file_sentinel {};
bool operator!=(file_iter iter, file_sentinel) {
return !iter.f->fail();
}
class rst_writer;
class rst_writer_state : public abstract_consumer {
public:
rst_writer_state(rst_writer* parent) : parent_(parent) {
// nop
}
virtual ~rst_writer_state() {
// nop
}
virtual const char* name() const noexcept = 0;
virtual void exit();
rst_writer* parent() const noexcept {
return parent_;
};
std::ostream& out();
protected:
rst_writer* parent_;
};
struct parse_failure : std::runtime_error {
using super = std::runtime_error;
const char* state_name;
parse_failure(const char* state_name, const std::string& what)
: super(what), state_name(state_name) {
// nop
}
template <class... Ts>
[[noreturn]] static void raise(const char* state_name, const Ts&... xs) {
throw parse_failure(state_name,
deep_to_string(std::forward_as_tuple(xs...)));
}
};
template <class Subtype>
class rst_writer_state_base : public rst_writer_state {
public:
using rst_writer_state::rst_writer_state;
void consume(node x) override {
current_node_ = &x;
visit(dref(), x);
current_node_ = nullptr;
}
protected:
template <class T, class... Ts>
void epsilon(const std::unique_ptr<T>& target, Ts&&... xs) {
auto& st = target->parent()->state;
if (st)
st->exit();
st = target.get();
target->entry(std::forward<Ts>(xs)...);
target->consume(std::move(*current_node_));
}
template <class T>
void unexpected(T&) {
auto cstr = type_name<T>::value;
parse_failure::raise(dref().name(), "unexpected command: ", cstr);
}
private:
Subtype& dref() {
return static_cast<Subtype&>(*this);
}
node* current_node_ = nullptr;
};
template <class T, class... Ts>
void transition(const std::unique_ptr<T>& target, Ts&&... xs) {
auto& st = target->parent()->state;
if (st)
st->exit();
st = target.get();
target->entry(std::forward<Ts>(xs)...);
}
#define DECLARE_STATE(name) \
struct name##_state; \
std::unique_ptr<name##_state> name; \
void make_##name()
class rst_writer : public abstract_consumer {
public:
using state_ptr = std::unique_ptr<rst_writer_state>;
rst_writer() : state(nullptr) {
make_await_section();
make_await_section_label();
make_read_body();
transition(await_section);
}
~rst_writer() {
state->exit();
}
void consume(node x) override {
state->consume(std::move(x));
}
void cmd(const std::string& name, std::vector<std::string> args) {
consume(make_node(name, std::move(args)));
}
std::string project_root;
rst_writer_state* state;
std::ofstream out;
DECLARE_STATE(await_section);
DECLARE_STATE(await_section_label);
DECLARE_STATE(read_body);
};
std::ostream& rst_writer_state::out() {
return parent_->out;
}
void rst_writer_state::exit() {
// customization point
}
#define BEGIN_STATE(type) \
struct rst_writer::type##_state \
: rst_writer_state_base<rst_writer::type##_state> { \
using super = rst_writer_state_base<rst_writer::type##_state>; \
type##_state(rst_writer* parent) : super(parent) { \
} \
const char* name() const noexcept override { \
return #type; \
}
#define END_STATE(type) \
} \
; \
void rst_writer::make_##type() { \
type.reset(new type##_state(this)); \
}
BEGIN_STATE(await_section)
void entry() {
// nop
}
void operator()(section& x) {
transition(parent_->await_section_label, x.name, '=');
}
template <class T>
void operator()(T& x) {
unexpected(x);
}
END_STATE(await_section)
struct string_stream {
std::string& result;
};
string_stream& operator<<(string_stream& out, string_view str) {
out.result.insert(out.result.end(), str.begin(), str.end());
return out;
}
string_stream& operator<<(string_stream& out, char c) {
out.result += c;
return out;
}
namespace rst_ops {
template <class Out>
Out& operator<<(Out& out, text& x) {
// Trim all whitespaces on the left and right but one.
if (x.str.empty())
return out;
auto no_space = [](char c) { return c != ' '; };
auto i = std::find_if(x.str.begin(), x.str.end(), no_space);
auto e = std::find_if(x.str.rbegin(), x.str.rend(), no_space).base();
if (i > e)
return out << ' ';
if (i != x.str.begin())
out << ' ';
for (; i != e; ++i)
out << *i;
if (e != x.str.end())
out << ' ';
return out;
}
template <class Out>
Out& operator<<(Out& out, see& x) {
return out << x.link << '_';
}
template <class Out>
Out& operator<<(Out& out, sref& x) {
return out << x.link << '_';
}
template <class Out>
Out& operator<<(Out& out, ref& x) {
return out << x.link << '_';
}
template <class Out>
Out& operator<<(Out& out, lstinline& x) {
return out << "``" << x.str << "``";
}
template <class Out>
Out& operator<<(Out& out, texttt& x) {
return out << "``" << x.str << "``";
}
template <class Out>
Out& operator<<(Out& out, textbf& x) {
return out << "**" << x.str << "**";
}
template <class Out>
Out& operator<<(Out& out, textit& x) {
return out << "*" << x.str << "*";
}
template <class Out>
Out& operator<<(Out& out, href& x) {
return out << "`" << x.str << " <" << x.url << ">`_";
}
template <class Out>
Out& operator<<(Out& out, experimental&) {
return out << " :sup:`experimental`";
}
} // namespace rst_ops
template <class Out>
struct rst_ops_visitor : abstract_consumer {
Out& out;
rst_ops_visitor(Out& out) : out(out) {
// nop
}
template <class T>
detail::enable_if_t<is_inline<T>::value> operator()(T& x) {
using namespace rst_ops;
out << x;
}
template <class T>
detail::enable_if_t<!is_inline<T>::value> operator()(T&) {
throw std::runtime_error("expected an inline command");
}
void consume(node x) override {
caf::visit(*this, x);
}
template <class T>
void consume(T x) {
(*this)(x);
}
void cmd(const std::string& name, std::vector<std::string> args) {
consume(make_node(name, std::move(args)));
}
};
template <class T>
void sub_command_consumer::cmd(const T& x, std::vector<std::string> xs) {
consume(make_node(x, std::move(xs)));
}
void sub_command_consumer::consume(node x) {
string_stream out{args.back()};
rst_ops_visitor<string_stream> visitor{out};
visitor.consume(std::move(x));
}
BEGIN_STATE(await_section_label)
void entry(const std::string& section_name, char highlighting) {
spaces.clear();
this->section_name.clear();
this->highlighting = highlighting;
// TODO: The tokenzier should parse TeX inside section names, too. Remove
// this hack once the sections contains nodes instead of just a
// string.
string_stream str_out{this->section_name};
rst_ops_visitor<string_stream> v{str_out};
using iterator_type = std::string::const_iterator;
parser_state<iterator_type> res{section_name.begin(), section_name.end()};
read_tex(res, v);
}
void operator()(label& x) {
out() << ".. _" << x.name << ":\n\n"
<< section_name << '\n'
<< std::string(section_name.size(), highlighting) << "\n\n";
transition(parent_->read_body);
}
void operator()(text& x) {
// Ignore whitespaces between \section and \label commands.
if (x.str.empty())
return;
if (!spaces.empty()) {
x.str.insert(x.str.begin(), spaces.begin(), spaces.end());
delegate();
return;
}
if (std::all_of(x.str.begin(), x.str.end(), ::isspace))
spaces = std::move(x.str);
else
delegate();
}
template <class T>
void operator()(T&) {
delegate();
}
void delegate() {
out() << section_name << '\n'
<< std::string(section_name.size(), highlighting) << "\n\n";
epsilon(parent_->read_body);
}
std::string section_name;
std::string spaces;
char highlighting;
END_STATE(await_section_label)
BEGIN_STATE(read_body)
void entry() {
// nop
}
template <class T>
detail::enable_if_t<is_inline<T>::value> operator()(T& x) {
using namespace rst_ops;
out() << x;
}
template <class T>
detail::enable_if_t<!is_inline<T>::value> operator()(T& x) {
unexpected(x);
}
void operator()(subsection& x) {
transition(parent_->await_section_label, x.name, '-');
}
void operator()(subsubsection& x) {
transition(parent_->await_section_label, x.name, '~');
}
void operator()(paragraph& x) {
transition(parent_->await_section_label, x.name, '+');
}
void operator()(lstlisting& x) {
print_block(".. code-block:: C++", x.block);
}
void operator()(verbatim& x) {
print_block(".. ::", x.block);
}
template <class T, class NextLine>
void print_list(T & x, NextLine next_line) {
out() << "\n\n";
for (auto& i : x.items) {
// lists must be aligned
std::string block;
string_stream block_stream{block};
rst_ops_visitor<string_stream> sub{block_stream};
for (auto& n : i.nodes)
visit(sub, n);
std::vector<std::string> lines;
split(lines, block, is_any_of("\n"), token_compress_on);
for (auto i = lines.begin(); i != lines.end();) {
trim(*i);
if (i->empty())
i = lines.erase(i);
else
++i;
}
if (!lines.empty()) {
next_line(true);
out() << lines.front();
for (size_t i = 1; i < lines.size(); ++i) {
next_line(false);
out() << lines[i];
}
}
out() << '\n';
}
out() << '\n';
}
void operator()(itemize& x) {
auto next_line = [&](bool new_item) {
if (new_item)
out() << "* ";
else
out() << " ";
};
print_list(x, next_line);
}
void operator()(enumerate& x) {
size_t num = 1;
auto next_line = [&](bool new_item) {
if (new_item)
out() << num++ << ". ";
else
out() << " "; // TODO: support more than 10 items
};
print_list(x, next_line);
}
void operator()(tabular& x) {
if (x.rows.empty() || x.rows[0].empty())
throw std::runtime_error("empty tabular");
// Convert the tabular into a string matrix.
std::vector<std::vector<std::string>> content;
content.reserve(x.rows.size());
auto num_columns = x.rows[0].size();
std::vector<size_t> column_sizes(num_columns);
for (auto& row : x.rows) {
// This hack makes sure we can handle \hline on the last line of a
// tabular. It silently drops anything with different column side, but a
// proper fix is not trivial.
if (row.size() != num_columns)
continue;
content.emplace_back();
auto& content_row = content.back();
content_row.resize(num_columns);
for (size_t col_index = 0; col_index < num_columns; ++col_index) {
auto& cell = content_row[col_index];
string_stream cell_out{cell};
rst_ops_visitor<string_stream> v{cell_out};
for (auto& node : row[col_index].nodes)
visit(v, node);
trim(cell);
column_sizes[col_index] = std::max(column_sizes[col_index],
cell.size());
}
}
// Output the matrix.
auto hline = [&] {
for (auto cs : column_sizes) {
out() << "+-";
for (size_t i = 0; i < cs; ++i)
out() << '-';
}
out() << "-+\n";
};
out() << "\n\n";
hline();
for (auto& row : content) {
for (size_t col_index = 0; col_index < row.size(); ++col_index) {
auto& column = row[col_index];
column.resize(column_sizes[col_index], ' ');
out() << "| " << column;
}
out() << " |\n";
hline();
}
out() << '\n';
}
void operator()(cppexample& x) {
auto path = parent_->project_root;
path += "/examples/";
path += x.file;
path += ".cpp";
std::ifstream in{path};
print_file(".. code-block:: c++", in, x.lines);
}
void operator()(iniexample& x) {
auto path = parent_->project_root;
path += "/examples/";
path += x.file;
path += ".ini";
std::ifstream in{path};
print_file(".. code-block:: ini", in, x.lines);
}
void operator()(sourcefile& x) {
auto path = parent_->project_root;
path += '/';
path += x.file;
std::ifstream in{path};
print_file(".. code-block:: c++", in, x.lines);
}
void operator()(singlefig& x) {
out() << ".. _" << x.label << ":\n\n"
<< ".. image:: " << x.file << ".png" << '\n'
<< " :alt: " << x.caption << "\n\n";
}
void print_block(const char* hdr, std::string& block) {
// Trim leading and trailing newlines.
while (block.size() > 0 && block.front() == '\n')
block.erase(block.begin());
while (block.size() > 0 && block.back() == '\n')
block.pop_back();
out() << "\n" << hdr << "\n\n";
out() << " ";
for (auto ch : block) {
if (ch == '\n')
out() << "\n ";
else
out() << ch;
}
out() << "\n\n";
}
void print_file(const char* hdr, std::ifstream& in, int first_line,
int last_line, int& line_num) {
std::string line;
while (line_num < first_line) {
if (!std::getline(in, line))
throw std::runtime_error("unexpected end of file");
++line_num;
}
out() << "\n" << hdr << "\n\n";
while (line_num < last_line) {
if (!std::getline(in, line))
break;
out() << " " << line << '\n';
++line_num;
}
out() << "\n\n";
}
void print_file(const char* hdr, std::ifstream& in,
const std::string& lines) {
int line_num = 1;
if (lines.empty()) {
print_file(hdr, in, 1, std::numeric_limits<int>::max(), line_num);
return;
}
std::vector<std::string> line_ranges;
split(line_ranges, lines, ",");
for (const auto& lr : line_ranges) {
std::vector<std::string> line_nums;
split(line_nums, lr, "-");
if (line_nums.size() != 2)
throw std::runtime_error("illegal line range");
print_file(hdr, in, std::stoi(line_nums[0]), std::stoi(line_nums[1]),
line_num);
}
}
END_STATE(read_body)
struct config : actor_system_config {
config() {
opt_group{custom_options_, "global"}
.add(input, "input,i", "input .tex file")
.add(output, "output,o", "output .rst file")
.add(project_root, "project-root,r", "project root for C++ examples");
}
std::string input;
std::string output;
std::string project_root;
};
} // namespace
int main(int argc, char** argv) {
// Read CAF configuration.
config cfg;
if (auto err = cfg.parse(argc, argv)) {
std::cerr << "unable to parse CAF config: " << cfg.render(err) << '\n';
return EXIT_FAILURE;
}
if (cfg.cli_helptext_printed)
return EXIT_SUCCESS;
if (cfg.input.empty() || cfg.output.empty()) {
std::cerr << "input or output path missing\n";
return EXIT_FAILURE;
}
rst_writer consumer;
consumer.out.open(cfg.output);
if (!consumer.out) {
std::cerr << "unable to open output file: " << cfg.output << '\n';
return EXIT_FAILURE;
}
consumer.project_root = cfg.project_root;
std::ifstream input{cfg.input};
parser_state<file_iter, file_sentinel> res{file_iter{&input}};
try {
read_tex(res, consumer);
if (res.i != res.e) {
std::cerr << "error in line " << res.line << " on column " << res.column
<< ": " << to_string(res.code) << '\n';
return EXIT_FAILURE;
}
} catch (parse_failure& x) {
std::cerr << "error in line " << res.line << " on column " << res.column
<< " while in state " << x.state_name << ": " << x.what() << '\n';
return EXIT_FAILURE;
} catch (std::exception& x) {
std::cerr << "error in line " << res.line << " on column " << res.column
<< ": " << x.what() << '\n';
return EXIT_FAILURE;
} catch (...) {
std::cerr << "unknown error in line " << res.line << " on column "
<< res.column << '\n';
return EXIT_FAILURE;
}
}
.. include:: index_header.rst
.. toctree::
:maxdepth: 2
:caption: Core Library
Introduction
FirstSteps
TypeInspection
MessageHandlers
Actors
MessagePassing
Scheduler
Registry
ReferenceCounting
Error
ConfiguringActorApplications
Messages
GroupCommunication
ManagingGroupsOfWorkers
Streaming
Testing
.. toctree::
:maxdepth: 2
:caption: I/O Library
NetworkTransparency
Brokers
RemoteSpawn
.. toctree::
:maxdepth: 2
:caption: Appendix
FAQ
Utility
CommonPitfalls
UsingAout
MigrationGuides
.. include:: index_footer.rst
Version Information
===================
This version of the Manual was automatically generated from CAF commit
`@CAF_SHA@ <https://github.com/actor-framework/actor-framework/commit/@CAF_SHA@>`_.
CAF User Manual
===============
**C++ Actor Framework** version @CAF_RELEASE@.
Contents
========
\section{Actors}
\label{actor}
Actors in CAF are a lightweight abstraction for units of computations. They
are active objects in the sense that they own their state and do not allow
others to access it. The only way to modify the state of an actor is sending
messages to it.
CAF provides several actor implementations, each covering a particular use
case. The available implementations differ in three characteristics: (1)
dynamically or statically typed, (2) class-based or function-based, and (3)
using asynchronous event handlers or blocking receives. These three
characteristics can be combined freely, with one exception: statically typed
actors are always event-based. For example, an actor can have dynamically typed
messaging, implement a class, and use blocking receives. The common base class
for all user-defined actors is called \lstinline^local_actor^.
Dynamically typed actors are more familiar to developers coming from Erlang or
Akka. They (usually) enable faster prototyping but require extensive unit
testing. Statically typed actors require more source code but enable the
compiler to verify communication between actors. Since CAF supports both,
developers can freely mix both kinds of actors to get the best of both worlds.
A good rule of thumb is to make use of static type checking for actors that are
visible across multiple translation units.
Actors that utilize the blocking receive API always require an exclusive thread
of execution. Event-based actors, on the other hand, are usually scheduled
cooperatively and are very lightweight with a memory footprint of only few
hundred bytes. Developers can exclude---detach---event-based actors that
potentially starve others from the cooperative scheduling while spawning it. A
detached actor lives in its own thread of execution.
\subsection{Environment / Actor Systems}
\label{actor-system}
All actors live in an \lstinline^actor_system^ representing an actor
environment including scheduler~\see{scheduler}, registry~\see{registry}, and
optional components such as a middleman~\see{middleman}. A single process can
have multiple \lstinline^actor_system^ instances, but this is usually not
recommended (a use case for multiple systems is to strictly separate two or
more sets of actors by running them in different schedulers). For configuration
and fine-tuning options of actor systems see \sref{system-config}. A
distributed CAF application consists of two or more connected actor systems. We
also refer to interconnected \lstinline^actor_system^ instances as a
\emph{distributed actor system}.
\clearpage
\subsection{Common Actor Base Types}
The following pseudo-UML depicts the class diagram for actors in CAF.
Irrelevant member functions and classes as well as mixins are omitted for
brevity. Selected individual classes are presented in more detail in the
following sections.
\singlefig{actor_types}%
{Actor Types in CAF}%
{actor-types}
\clearpage
\subsubsection{Class \lstinline^local_actor^}
The class \lstinline^local_actor^ is the root type for all user-defined actors
in CAF. It defines all common operations. However, users of the library
usually do not inherit from this class directly. Proper base classes for
user-defined actors are \lstinline^event_based_actor^ or
\lstinline^blocking_actor^. The following table also includes member function
inherited from \lstinline^monitorable_actor^ and \lstinline^abstract_actor^.
\begin{center}
\begin{tabular}{ll}
\textbf{Types} & ~ \\
\hline
\lstinline^mailbox_type^ & A concurrent, many-writers-single-reader queue type. \\
\hline
~ & ~ \\ \textbf{Constructors} & ~ \\
\hline
\lstinline^(actor_config&)^ & Constructs the actor using a config. \\
\hline
~ & ~ \\ \textbf{Observers} & ~ \\
\hline
\lstinline^actor_addr address()^ & Returns the address of this actor. \\
\hline
\lstinline^actor_system& system()^ & Returns \lstinline^context()->system()^. \\
\hline
\lstinline^actor_system& home_system()^ & Returns the system that spawned this actor. \\
\hline
\lstinline^execution_unit* context()^ & Returns underlying thread or current scheduler worker. \\
\hline
~ & ~ \\ \textbf{Customization Points} & ~ \\
\hline
\lstinline^on_exit()^ & Can be overridden to perform cleanup code. \\
\hline
\lstinline^const char* name()^ & Returns a debug name for this actor type. \\
\hline
~ & ~ \\ \textbf{Actor Management} & ~ \\
\hline
\lstinline^link_to(other)^ & Link to an actor \see{link}. \\
\hline
\lstinline^unlink_from(other)^ & Remove link to an actor \see{link}. \\
\hline
\lstinline^monitor(other)^ & Unidirectionally monitors an actor \see{monitor}. \\
\hline
\lstinline^demonitor(other)^ & Removes a monitor from \lstinline^whom^. \\
\hline
\lstinline^spawn(F fun, xs...)^ & Spawns a new actor from \lstinline^fun^. \\
\hline
\lstinline^spawn<T>(xs...)^ & Spawns a new actor of type \lstinline^T^. \\
\hline
~ & ~ \\ \textbf{Message Processing} & ~ \\
\hline
\lstinline^T make_response_promise<Ts...>()^ & Allows an actor to delay its response message. \\
\hline
\lstinline^T response(xs...)^ & Convenience function for creating fulfilled promises. \\
\hline
\end{tabular}
\end{center}
\clearpage
\subsubsection{Class \lstinline^scheduled_actor^}
All scheduled actors inherit from \lstinline^scheduled_actor^. This includes
statically and dynamically typed event-based actors as well as brokers
\see{broker}.
\begin{center}
\begin{tabular}{ll}
\textbf{Types} & ~ \\
\hline
\lstinline^pointer^ & \lstinline^scheduled_actor*^ \\
\hline
\lstinline^exception_handler^ & \lstinline^function<error (pointer, std::exception_ptr&)>^ \\
\hline
\lstinline^default_handler^ & \lstinline^function<result<message> (pointer, message_view&)>^ \\
\hline
\lstinline^error_handler^ & \lstinline^function<void (pointer, error&)>^ \\
\hline
\lstinline^down_handler^ & \lstinline^function<void (pointer, down_msg&)>^ \\
\hline
\lstinline^exit_handler^ & \lstinline^function<void (pointer, exit_msg&)>^ \\
\hline
~ & ~ \\ \textbf{Constructors} & ~ \\
\hline
\lstinline^(actor_config&)^ & Constructs the actor using a config. \\
\hline
~ & ~ \\ \textbf{Termination} & ~ \\
\hline
\lstinline^quit()^ & Stops this actor with normal exit reason. \\
\hline
\lstinline^quit(error x)^ & Stops this actor with error \lstinline^x^. \\
\hline
~ & ~ \\ \textbf{Special-purpose Handlers} & ~ \\
\hline
\lstinline^set_exception_handler(F f)^ & Installs \lstinline^f^ for converting exceptions to errors \see{error}. \\
\hline
\lstinline^set_down_handler(F f)^ & Installs \lstinline^f^ to handle down messages \see{down-message}. \\
\hline
\lstinline^set_exit_handler(F f)^ & Installs \lstinline^f^ to handle exit messages \see{exit-message}. \\
\hline
\lstinline^set_error_handler(F f)^ & Installs \lstinline^f^ to handle error messages (see \sref{error-message} and \sref{error}). \\
\hline
\lstinline^set_default_handler(F f)^ & Installs \lstinline^f^ as fallback message handler \see{default-handler}. \\
\hline
\end{tabular}
\end{center}
\clearpage
\subsubsection{Class \lstinline^blocking_actor^}
A blocking actor always lives in its own thread of execution. They are not as
lightweight as event-based actors and thus do not scale up to large numbers.
The primary use case for blocking actors is to use a \lstinline^scoped_actor^
for ad-hoc communication to selected actors. Unlike scheduled actors, CAF does
\textbf{not} dispatch system messages to special-purpose handlers. A blocking
actors receives \emph{all} messages regularly through its mailbox. A blocking
actor is considered \emph{done} only after it returned from \lstinline^act^ (or
from the implementation in function-based actors). A \lstinline^scoped_actor^
sends its exit messages as part of its destruction.
\begin{center}
\begin{tabular}{ll}
\textbf{Constructors} & ~ \\
\hline
\lstinline^(actor_config&)^ & Constructs the actor using a config. \\
\hline
~ & ~ \\ \textbf{Customization Points} & ~ \\
\hline
\lstinline^void act()^ & Implements the behavior of the actor. \\
\hline
~ & ~ \\ \textbf{Termination} & ~ \\
\hline
\lstinline^const error& fail_state()^ & Returns the current exit reason. \\
\hline
\lstinline^fail_state(error x)^ & Sets the current exit reason. \\
\hline
~ & ~ \\ \textbf{Actor Management} & ~ \\
\hline
\lstinline^wait_for(Ts... xs)^ & Blocks until all actors \lstinline^xs...^ are done. \\
\hline
\lstinline^await_all_other_actors_done()^ & Blocks until all other actors are done. \\
\hline
~ & ~ \\ \textbf{Message Handling} & ~ \\
\hline
\lstinline^receive(Ts... xs)^ & Receives a message using the callbacks \lstinline^xs...^. \\
\hline
\lstinline^receive_for(T& begin, T end)^ & See \sref{receive-loop}. \\
\hline
\lstinline^receive_while(F stmt)^ & See \sref{receive-loop}. \\
\hline
\lstinline^do_receive(Ts... xs)^ & See \sref{receive-loop}. \\
\hline
\end{tabular}
\end{center}
\clearpage
\subsection{Messaging Interfaces}
\label{interface}
Statically typed actors require abstract messaging interfaces to allow the
compiler to type-check actor communication. Interfaces in CAF are defined using
the variadic template \lstinline^typed_actor<...>^, which defines the proper
actor handle at the same time. Each template parameter defines one
\lstinline^input/output^ pair via
\lstinline^replies_to<X1,...,Xn>::with<Y1,...,Yn>^. For inputs that do not
generate outputs, \lstinline^reacts_to<X1,...,Xn>^ can be used as shortcut for
\lstinline^replies_to<X1,...,Xn>::with<void>^. In the same way functions cannot
be overloaded only by their return type, interfaces cannot accept one input
twice (possibly mapping it to different outputs). The example below defines a
messaging interface for a simple calculator.
\cppexample[17-18]{message_passing/calculator}
It is not required to create a type alias such as \lstinline^calculator_actor^,
but it makes dealing with statically typed actors much easier. Also, a central
alias definition eases refactoring later on.
Interfaces have set semantics. This means the following two type aliases
\lstinline^i1^ and \lstinline^i2^ are equal:
\begin{lstlisting}
using i1 = typed_actor<replies_to<A>::with<B>, replies_to<C>::with<D>>;
using i2 = typed_actor<replies_to<C>::with<D>, replies_to<A>::with<B>>;
\end{lstlisting}
Further, actor handles of type \lstinline^A^ are assignable to handles of type
\lstinline^B^ as long as \lstinline^B^ is a subset of \lstinline^A^.
For convenience, the class \lstinline^typed_actor<...>^ defines the member
types shown below to grant access to derived types.
\begin{center}
\begin{tabular}{ll}
\textbf{Types} & ~ \\
\hline
\lstinline^behavior_type^ & A statically typed set of message handlers. \\
\hline
\lstinline^base^ & Base type for actors, i.e., \lstinline^typed_event_based_actor<...>^. \\
\hline
\lstinline^pointer^ & A pointer of type \lstinline^base*^. \\
\hline
\lstinline^stateful_base<T>^ & See \sref{stateful-actor}. \\
\hline
\lstinline^stateful_pointer<T>^ & A pointer of type \lstinline^stateful_base<T>*^. \\
\hline
\lstinline^extend<Ts...>^ & Extend this typed actor with \lstinline^Ts...^. \\
\hline
\lstinline^extend_with<Other>^ & Extend this typed actor with all cases from \lstinline^Other^. \\
\hline
\end{tabular}
\end{center}
\clearpage
\subsection{Spawning Actors}
\label{spawn}
Both statically and dynamically typed actors are spawned from an
\lstinline^actor_system^ using the member function \lstinline^spawn^. The
function either takes a function as first argument or a class as first template
parameter. For example, the following functions and classes represent actors.
\cppexample[21-26]{message_passing/calculator}
Spawning an actor for each implementation is illustrated below.
\cppexample[123-128]{message_passing/calculator}
Additional arguments to \lstinline^spawn^ are passed to the constructor of a
class or used as additional function arguments, respectively. In the example
above, none of the three functions takes any argument other than the implicit
but optional \lstinline^self^ pointer.
\subsection{Function-based Actors}
\label{function-based}
When using a function or function object to implement an actor, the first
argument \emph{can} be used to capture a pointer to the actor itself. The type
of this pointer is usually \lstinline^event_based_actor*^ or
\lstinline^blocking_actor*^. The proper pointer type for any
\lstinline^typed_actor^ handle \lstinline^T^ can be obtained via
\lstinline^T::pointer^ \see{interface}.
Blocking actors simply implement their behavior in the function body. The actor
is done once it returns from that function.
Event-based actors can either return a \lstinline^behavior^
\see{message-handler} that is used to initialize the actor or explicitly set the
initial behavior by calling \lstinline^self->become(...)^. Due to the
asynchronous, event-based nature of this kind of actor, the function usually
returns immediately after setting a behavior (message handler) for the
\emph{next} incoming message. Hence, variables on the stack will be out of scope
once a message arrives. Managing state in function-based actors can be done
either via rebinding state with \lstinline^become^, using heap-located data
referenced via \lstinline^std::shared_ptr^ or by using the \textit{stateful
actor} abstraction~\see{stateful-actor}.
The following three functions implement the prototypes shown in~\sref{spawn}
and illustrate one blocking actor and two event-based actors (statically and
dynamically typed).
\clearpage
\cppexample[28-56]{message_passing/calculator}
\clearpage
\subsection{Class-based Actors}
\label{class-based}
Implementing an actor using a class requires the following:
\begin{itemize}
\item Provide a constructor taking a reference of type
\lstinline^actor_config&^ as first argument, which is forwarded to the base
class. The config is passed implicitly to the constructor when calling
\lstinline^spawn^, which also forwards any number of additional arguments
to the constructor.
\item Override \lstinline^make_behavior^ for event-based actors and
\lstinline^act^ for blocking actors.
\end{itemize}
Implementing actors with classes works for all kinds of actors and allows
simple management of state via member variables. However, composing states via
inheritance can get quite tedious. For dynamically typed actors, composing
states is particularly hard, because the compiler cannot provide much help. For
statically typed actors, CAF also provides an API for composable
behaviors~\see{composable-behavior} that works well with inheritance. The
following three examples implement the forward declarations shown in
\sref{spawn}.
\cppexample[58-92]{message_passing/calculator}
\clearpage
\subsection{Stateful Actors}
\label{stateful-actor}
The stateful actor API makes it easy to maintain state in function-based
actors. It is also safer than putting state in member variables, because the
state ceases to exist after an actor is done and is not delayed until the
destructor runs. For example, if two actors hold a reference to each other via
member variables, they produce a cycle and neither will get destroyed. Using
stateful actors instead breaks the cycle, because references are destroyed when
an actor calls \lstinline^self->quit()^ (or is killed externally). The
following example illustrates how to implement stateful actors with static
typing as well as with dynamic typing.
\cppexample[18-44]{message_passing/cell}
Stateful actors are spawned in the same way as any other function-based actor
\see{function-based}.
\cppexample[49-50]{message_passing/cell}
\clearpage
\subsection{Actors from Composable Behaviors \experimental}
\label{composable-behavior}
When building larger systems, it is often useful to implement the behavior of
an actor in terms of other, existing behaviors. The composable behaviors in
CAF allow developers to generate a behavior class from a messaging
interface~\see{interface}.
The base type for composable behaviors is \lstinline^composable_behavior<T>^,
where \lstinline^T^ is a \lstinline^typed_actor<...>^. CAF maps each
\lstinline^replies_to<A,B,C>::with<D,E,F>^ in \lstinline^T^ to a pure virtual
member function with signature:
\begin{lstlisting}
result<D, E, F> operator()(param<A>, param<B>, param<C>);.
\end{lstlisting}
Note that \lstinline^operator()^ will take integral types as well as atom
constants simply by value. A \lstinline^result<T>^ accepts either a value of
type \lstinline^T^, a \lstinline^skip_t^ \see{default-handler}, an
\lstinline^error^ \see{error}, a \lstinline^delegated<T>^ \see{delegate}, or a
\lstinline^response_promise<T>^ \see{promise}. A \lstinline^result<void>^ is
constructed by returning \lstinline^unit^.
A behavior that combines the behaviors \lstinline^X^, \lstinline^Y^, and
\lstinline^Z^ must inherit from \lstinline^composed_behavior<X,Y,Z>^ instead of
inheriting from the three classes directly. The class
\lstinline^composed_behavior^ ensures that the behaviors are concatenated
correctly. In case one message handler is defined in multiple base types, the
\emph{first} type in declaration order wins. For example, if \lstinline^X^ and
\lstinline^Y^ both implement the interface
\lstinline^replies_to<int,int>::with<int>^, only the handler implemented in
\lstinline^X^ is active.
Any composable (or composed) behavior with no pure virtual member functions can
be spawned directly through an actor system by calling
\lstinline^system.spawn<...>()^, as shown below.
\cppexample[20-45]{composition/calculator_behavior}
\clearpage
The second example illustrates how to use non-primitive values that are wrapped
in a \lstinline^param<T>^ when working with composable behaviors. The purpose
of \lstinline^param<T>^ is to provide a single interface for both constant and
non-constant access. Constant access is modeled with the implicit conversion
operator to a const reference, the member function \lstinline^get()^, and
\lstinline^operator->^.
When acquiring mutable access to the represented value, CAF copies the value
before allowing mutable access to it if more than one reference to the value
exists. This copy-on-write optimization avoids race conditions by design, while
minimizing copy operations \see{copy-on-write}. A mutable reference is returned
from the member functions \lstinline^get_mutable()^ and \lstinline^move()^. The
latter is a convenience function for \lstinline^std::move(x.get_mutable())^.
The following example illustrates how to use \lstinline^param<std::string>^
when implementing a simple dictionary.
\cppexample[22-44]{composition/dictionary_behavior}
\subsection{Attaching Cleanup Code to Actors}
\label{attach}
Users can attach cleanup code to actors. This code is executed immediately if
the actor has already exited. Otherwise, the actor will execute it as part of
its termination. The following example attaches a function object to actors for
printing a custom string on exit.
\cppexample[42-47]{broker/simple_broker}
It is possible to attach code to remote actors. However, the cleanup code will
run on the local machine.
\subsection{Blocking Actors}
\label{blocking-actor}
Blocking actors always run in a separate thread and are not scheduled by CAF.
Unlike event-based actors, blocking actors have explicit, blocking
\emph{receive} functions. Further, blocking actors do not handle system
messages automatically via special-purpose callbacks \see{special-handler}.
This gives users full control over the behavior of blocking actors. However,
blocking actors still should follow conventions of the actor system. For
example, actors should unconditionally terminate after receiving an
\lstinline^exit_msg^ with reason \lstinline^exit_reason::kill^.
\subsubsection{Receiving Messages}
The function \lstinline^receive^ sequentially iterates over all elements in the
mailbox beginning with the first. It takes a message handler that is applied to
the elements in the mailbox until an element was matched by the handler. An
actor calling \lstinline^receive^ is blocked until it successfully dequeued a
message from its mailbox or an optional timeout occurs. Messages that are not
matched by the behavior are automatically skipped and remain in the mailbox.
\begin{lstlisting}
self->receive (
[](int x) { /* ... */ }
);
\end{lstlisting}
\subsubsection{Catch-all Receive Statements}
\label{catch-all}
Blocking actors can use inline catch-all callbacks instead of setting a default
handler \see{default-handler}. A catch-all case must be the last callback
before the optional timeout, as shown in the example below.
\begin{lstlisting}
self->receive(
[&](float x) {
// ...
},
[&](const down_msg& x) {
// ...
},
[&](const exit_msg& x) {
// ...
},
others >> [](message_view& x) -> result<message> {
// report unexpected message back to client
return sec::unexpected_message;
}
);
\end{lstlisting}
\clearpage
\subsubsection{Receive Loops}
\label{receive-loop}
Message handler passed to \lstinline^receive^ are temporary object at runtime.
Hence, calling \lstinline^receive^ inside a loop creates an unnecessary amount
of short-lived objects. CAF provides predefined receive loops to allow for
more efficient code.
\begin{lstlisting}
// BAD
std::vector<int> results;
for (size_t i = 0; i < 10; ++i)
receive (
[&](int value) {
results.push_back(value);
}
);
// GOOD
std::vector<int> results;
size_t i = 0;
receive_for(i, 10) (
[&](int value) {
results.push_back(value);
}
);
\end{lstlisting}
\begin{lstlisting}
// BAD
size_t received = 0;
while (received < 10) {
receive (
[&](int) {
++received;
}
);
} ;
// GOOD
size_t received = 0;
receive_while([&] { return received < 10; }) (
[&](int) {
++received;
}
);
\end{lstlisting}
\clearpage
\begin{lstlisting}
// BAD
size_t received = 0;
do {
receive (
[&](int) {
++received;
}
);
} while (received < 10);
// GOOD
size_t received = 0;
do_receive (
[&](int) {
++received;
}
).until([&] { return received >= 10; });
\end{lstlisting}
The examples above illustrate the correct usage of the three loops
\lstinline^receive_for^, \lstinline^receive_while^ and
\lstinline^do_receive(...).until^. It is possible to nest receives and receive
loops.
\begin{lstlisting}
bool running = true;
self->receive_while([&] { return running; }) (
[&](int value1) {
self->receive (
[&](float value2) {
aout(self) << value1 << " => " << value2 << endl;
}
);
},
// ...
);
\end{lstlisting}
\subsubsection{Scoped Actors}
\label{scoped-actors}
The class \lstinline^scoped_actor^ offers a simple way of communicating with
CAF actors from non-actor contexts. It overloads \lstinline^operator->^ to
return a \lstinline^blocking_actor*^. Hence, it behaves like the implicit
\lstinline^self^ pointer in functor-based actors, only that it ceases to exist
at scope end.
\begin{lstlisting}
void test(actor_system& system) {
scoped_actor self{system};
// spawn some actor
auto aut = self->spawn(my_actor_impl);
self->send(aut, "hi there");
// self will be destroyed automatically here; any
// actor monitoring it will receive down messages etc.
}
\end{lstlisting}
\section{Network I/O with Brokers}
\label{broker}
When communicating to other services in the network, sometimes low-level socket
I/O is inevitable. For this reason, CAF provides \emph{brokers}. A broker is
an event-based actor running in the middleman that multiplexes socket I/O. It
can maintain any number of acceptors and connections. Since the broker runs in
the middleman, implementations should be careful to consume as little time as
possible in message handlers. Brokers should outsource any considerable amount
of work by spawning new actors or maintaining worker actors.
\textit{Note that all UDP-related functionality is still \experimental.}
\subsection{Spawning Brokers}
Brokers are implemented as functions and are spawned by calling on of the three
following member functions of the middleman.
\begin{lstlisting}
template <spawn_options Os = no_spawn_options,
class F = std::function<void(broker*)>, class... Ts>
typename infer_handle_from_fun<F>::type
spawn_broker(F fun, Ts&&... xs);
template <spawn_options Os = no_spawn_options,
class F = std::function<void(broker*)>, class... Ts>
expected<typename infer_handle_from_fun<F>::type>
spawn_client(F fun, const std::string& host, uint16_t port, Ts&&... xs);
template <spawn_options Os = no_spawn_options,
class F = std::function<void(broker*)>, class... Ts>
expected<typename infer_handle_from_fun<F>::type>
spawn_server(F fun, uint16_t port, Ts&&... xs);
\end{lstlisting}
The function \lstinline^spawn_broker^ simply spawns a broker. The convenience
function \lstinline^spawn_client^ tries to connect to given host and port over
TCP and returns a broker managing this connection on success. Finally,
\lstinline^spawn_server^ opens a local TCP port and spawns a broker managing it
on success. There are no convenience functions spawn a UDP-based client or
server.
\subsection{Class \lstinline^broker^}
\label{broker-class}
\begin{lstlisting}
void configure_read(connection_handle hdl, receive_policy::config config);
\end{lstlisting}
Modifies the receive policy for the connection identified by \lstinline^hdl^.
This will cause the middleman to enqueue the next \lstinline^new_data_msg^
according to the given \lstinline^config^ created by
\lstinline^receive_policy::exactly(x)^, \lstinline^receive_policy::at_most(x)^,
or \lstinline^receive_policy::at_least(x)^ (with \lstinline^x^ denoting the
number of bytes).
\begin{lstlisting}
void write(connection_handle hdl, size_t num_bytes, const void* buf)
void write(datagram_handle hdl, size_t num_bytes, const void* buf)
\end{lstlisting}
Writes data to the output buffer.
\begin{lstlisting}
void enqueue_datagram(datagram_handle hdl, std::vector<char> buf);
\end{lstlisting}
Enqueues a buffer to be sent as a datagram. Use of this function is encouraged
over write as it allows reuse of the buffer which can be returned to the broker
in a \lstinline^datagram_sent_msg^.
\begin{lstlisting}
void flush(connection_handle hdl);
void flush(datagram_handle hdl);
\end{lstlisting}
Sends the data from the output buffer.
\begin{lstlisting}
template <class F, class... Ts>
actor fork(F fun, connection_handle hdl, Ts&&... xs);
\end{lstlisting}
Spawns a new broker that takes ownership of a given connection.
\begin{lstlisting}
size_t num_connections();
\end{lstlisting}
Returns the number of open connections.
\begin{lstlisting}
void close(connection_handle hdl);
void close(accept_handle hdl);
void close(datagram_handle hdl);
\end{lstlisting}
Closes the endpoint related to the handle.
\begin{lstlisting}
expected<std::pair<accept_handle, uint16_t>>
add_tcp_doorman(uint16_t port = 0, const char* in = nullptr,
bool reuse_addr = false);
\end{lstlisting}
Creates new doorman that accepts incoming connections on a given port and
returns the handle to the doorman and the port in use or an error.
\begin{lstlisting}
expected<connection_handle>
add_tcp_scribe(const std::string& host, uint16_t port);
\end{lstlisting}
Creates a new scribe to connect to host:port and returns handle to it or an
error.
\begin{lstlisting}
expected<std::pair<datagram_handle, uint16_t>>
add_udp_datagram_servant(uint16_t port = 0, const char* in = nullptr,
bool reuse_addr = false);
\end{lstlisting}
Creates a datagram servant to handle incoming datagrams on a given port.
Returns the handle to the servant and the port in use or an error.
\begin{lstlisting}
expected<datagram_handle>
add_udp_datagram_servant(const std::string& host, uint16_t port);
\end{lstlisting}
Creates a datagram servant to send datagrams to host:port and returns a handle
to it or an error.
\subsection{Broker-related Message Types}
Brokers receive system messages directly from the middleman for connection and
acceptor events.
\textbf{Note:} brokers are \emph{required} to handle these messages immediately
regardless of their current state. Not handling the system messages from the
broker results in loss of data, because system messages are \emph{not}
delivered through the mailbox and thus cannot be skipped.
\begin{lstlisting}
struct new_connection_msg {
accept_handle source;
connection_handle handle;
};
\end{lstlisting}
Indicates that \lstinline^source^ accepted a new TCP connection identified by
\lstinline^handle^.
\begin{lstlisting}
struct new_data_msg {
connection_handle handle;
std::vector<char> buf;
};
\end{lstlisting}
Contains raw bytes received from \lstinline^handle^. The amount of data
received per event is controlled with \lstinline^configure_read^ (see
\ref{broker-class}). It is worth mentioning that the buffer is re-used whenever
possible.
\begin{lstlisting}
struct data_transferred_msg {
connection_handle handle;
uint64_t written;
uint64_t remaining;
};
\end{lstlisting}
This message informs the broker that the \lstinline^handle^ sent
\lstinline^written^ bytes with \lstinline^remaining^ bytes in the buffer. Note,
that these messages are not sent per default but must be explicitly enabled via
the member function \lstinline^ack_writes^.
\begin{lstlisting}
struct connection_closed_msg {
connection_handle handle;
};
struct acceptor_closed_msg {
accept_handle handle;
};
\end{lstlisting}
A \lstinline^connection_closed_msg^ or \lstinline^acceptor_closed_msg^ informs
the broker that one of its handles is no longer valid.
\begin{lstlisting}
struct connection_passivated_msg {
connection_handle handle;
};
struct acceptor_passivated_msg {
accept_handle handle;
};
\end{lstlisting}
A \lstinline^connection_passivated_msg^ or \lstinline^acceptor_passivated_msg^
informs the broker that one of its handles entered passive mode and no longer
accepts new data or connections \see{trigger}.
The following messages are related to UDP communication
(see~\sref{transport-protocols}. Since UDP is not connection oriented, there is
no equivalent to the \lstinline^new_connection_msg^ of TCP.
\begin{lstlisting}
struct new_datagram_msg {
datagram_handle handle;
network::receive_buffer buf;
};
\end{lstlisting}
Contains the raw bytes from \lstinline^handle^. The buffer always has a maximum
size of 65k to receive all regular UDP messages. The amount of bytes can be
queried via the \lstinline^.size()^ member function. Similar to TCP, the buffer
is reused when possible---please do not resize it.
\begin{lstlisting}
struct datagram_sent_msg {
datagram_handle handle;
uint64_t written;
std::vector<char> buf;
};
\end{lstlisting}
This message informs the broker that the \lstinline^handle^ sent a datagram of
\lstinline^written^ bytes. It includes the buffer that held the sent message to
allow its reuse. Note, that these messages are not sent per default but must be
explicitly enabled via the member function \lstinline^ack_writes^.
\begin{lstlisting}
struct datagram_servant_closed_msg {
std::vector<datagram_handle> handles;
};
\end{lstlisting}
A \lstinline^datagram_servant_closed_msg^ informs the broker that one of its
handles is no longer valid.
\begin{lstlisting}
struct datagram_servant_passivated_msg {
datagram_handle handle;
};
\end{lstlisting}
A \lstinline^datagram_servant_closed_msg^ informs the broker that one of its
handles entered passive mode and no longer accepts new data \see{trigger}.
\subsection{Manually Triggering Events \experimental}
\label{trigger}
Brokers receive new events as \lstinline^new_connection_msg^ and
\lstinline^new_data_msg^ as soon and as often as they occur, per default. This
means a fast peer can overwhelm a broker by sending it data faster than the
broker can process it. In particular if the broker outsources work items to
other actors, because work items can accumulate in the mailboxes of the
workers.
Calling \lstinline^self->trigger(x,y)^, where \lstinline^x^ is a connection or
acceptor handle and \lstinline^y^ is a positive integer, allows brokers to halt
activities after \lstinline^y^ additional events. Once a connection or acceptor
stops accepting new data or connections, the broker receives a
\lstinline^connection_passivated_msg^ or \lstinline^acceptor_passivated_msg^.
Brokers can stop activities unconditionally with \lstinline^self->halt(x)^ and
resume activities unconditionally with \lstinline^self->trigger(x)^.
\section{Configuring Actor Applications}
\label{system-config}
CAF configures applications at startup using an
\lstinline^actor_system_config^ or a user-defined subclass of that type. The
config objects allow users to add custom types, to load modules, and to
fine-tune the behavior of loaded modules with command line options or
configuration files~\see{system-config-options}.
The following code example is a minimal CAF application with a
middleman~\see{middleman} but without any custom configuration options.
\begin{lstlisting}
void caf_main(actor_system& system) {
// ...
}
CAF_MAIN(io::middleman)
\end{lstlisting}
The compiler expands this example code to the following.
\begin{lstlisting}
void caf_main(actor_system& system) {
// ...
}
int main(int argc, char** argv) {
return exec_main<io::middleman>(caf_main, argc, argv);
}
\end{lstlisting}
The function \lstinline^exec_main^ creates a config object, loads all modules
requested in \lstinline^CAF_MAIN^ and then calls \lstinline^caf_main^. A
minimal implementation for \lstinline^main^ performing all these steps manually
is shown in the next example for the sake of completeness.
\begin{lstlisting}
int main(int argc, char** argv) {
actor_system_config cfg;
// read CLI options
cfg.parse(argc, argv);
// return immediately if a help text was printed
if (cfg.cli_helptext_printed)
return 0;
// load modules
cfg.load<io::middleman>();
// create actor system and call caf_main
actor_system system{cfg};
caf_main(system);
}
\end{lstlisting}
However, setting up config objects by hand is usually not necessary. CAF
automatically selects user-defined subclasses of
\lstinline^actor_system_config^ if \lstinline^caf_main^ takes a second
parameter by reference, as shown in the minimal example below.
\begin{lstlisting}
class my_config : public actor_system_config {
public:
my_config() {
// ...
}
};
void caf_main(actor_system& system, const my_config& cfg) {
// ...
}
CAF_MAIN()
\end{lstlisting}
Users can perform additional initialization, add custom program options, etc.
simply by implementing a default constructor.
\subsection{Loading Modules}
\label{system-config-module}
The simplest way to load modules is to use the macro \lstinline^CAF_MAIN^ and
to pass a list of all requested modules, as shown below.
\begin{lstlisting}
void caf_main(actor_system& system) {
// ...
}
CAF_MAIN(mod1, mod2, ...)
\end{lstlisting}
Alternatively, users can load modules in user-defined config classes.
\begin{lstlisting}
class my_config : public actor_system_config {
public:
my_config() {
load<mod1>();
load<mod2>();
// ...
}
};
\end{lstlisting}
The third option is to simply call \lstinline^x.load<mod1>()^ on a config
object \emph{before} initializing an actor system with it.
\subsection{Command Line Options and INI Configuration Files}
\label{system-config-options}
CAF organizes program options in categories and parses CLI arguments as well as
INI files. CLI arguments override values in the INI file which override
hard-coded defaults. Users can add any number of custom program options by
implementing a subtype of \lstinline^actor_system_config^. The example below
adds three options to the \lstinline^global^ category.
\cppexample[206-218]{remoting/distributed_calculator}
We create a new \lstinline^global^ category in \lstinline^custom_options_}^.
Each following call to \lstinline^add^ then appends individual options to the
category. The first argument to \lstinline^add^ is the associated variable. The
second argument is the name for the parameter, optionally suffixed with a
comma-separated single-character short name. The short name is only considered
for CLI parsing and allows users to abbreviate commonly used option names. The
third and final argument to \lstinline^add^ is a help text.
The custom \lstinline^config^ class allows end users to set the port for the
application to 42 with either \lstinline^-p 42^ (short name) or
\lstinline^--port=42^ (long name). The long option name is prefixed by the
category when using a different category than ``global''. For example, adding
the port option to the category \lstinline^foo^ means end users have to type
\lstinline^--foo.port=42^ when using the long name. Short names are unaffected
by the category, but have to be unique.
Boolean options do not require arguments. The member variable
\lstinline^server_mode^ is set to \lstinline^true^ if the command line contains
either \lstinline^--server-mode^ or \lstinline^-s^.
The example uses member variables for capturing user-provided settings for
simplicity. However, this is not required. For example,
\lstinline^add<bool>(...)^ allows omitting the first argument entirely. All
values of the configuration are accessible with \lstinline^get_or^. Note that
all global options can omit the \lstinline^"global."^ prefix.
CAF adds the program options \lstinline^help^ (with short names \lstinline^-h^
and \lstinline^-?^) as well as \lstinline^long-help^ to the \lstinline^global^
category.
The default name for the INI file is \lstinline^caf-application.ini^. Users can
change the file name and path by passing \lstinline^--config-file=<path>^ on the
command line.
INI files are organized in categories. No value is allowed outside of a category
(no implicit \lstinline^global^ category). The parses uses the following syntax:
\begin{tabular}{ll}
\lstinline^key=true^ & is a boolean \\
\lstinline^key=1^ & is an integer \\
\lstinline^key=1.0^ & is an floating point number \\
\lstinline^key=1ms^ & is an timespan \\
\lstinline^key='foo'^ & is an atom \\
\lstinline^key="foo"^ & is a string \\
\lstinline^key=[0, 1, ...]^ & is as a list \\
\lstinline^key={a=1, b=2, ...}^ & is a dictionary (map) \\
\end{tabular}
The following example INI file lists all standard options in CAF and their
default value. Note that some options such as \lstinline^scheduler.max-threads^
are usually detected at runtime and thus have no hard-coded default.
\clearpage
\iniexample{caf-application}
\clearpage
\subsection{Adding Custom Message Types}
\label{add-custom-message-type}
CAF requires serialization support for all of its message types
\see{type-inspection}. However, CAF also needs a mapping of unique type names
to user-defined types at runtime. This is required to deserialize arbitrary
messages from the network.
As an introductory example, we (again) use the following POD type
\lstinline^foo^.
\cppexample[24-27]{custom_type/custom_types_1}
To make \lstinline^foo^ serializable, we make it inspectable
\see{type-inspection}:
\cppexample[30-34]{custom_type/custom_types_1}
Finally, we give \lstinline^foo^ a platform-neutral name and add it to the list
of serializable types by using a custom config class.
\cppexample[75-78,81-84]{custom_type/custom_types_1}
\subsection{Adding Custom Error Types}
Adding a custom error type to the system is a convenience feature to allow
improve the string representation. Error types can be added by implementing a
render function and passing it to \lstinline^add_error_category^, as shown
in~\sref{custom-error}.
\clearpage
\subsection{Adding Custom Actor Types \experimental}
\label{add-custom-actor-type}
Adding actor types to the configuration allows users to spawn actors by their
name. In particular, this enables spawning of actors on a different node
\see{remote-spawn}. For our example configuration, we consider the following
simple \lstinline^calculator^ actor.
\cppexample[33-34]{remoting/remote_spawn}
Adding the calculator actor type to our config is achieved by calling
\lstinline^add_actor_type<T>^. Note that adding an actor type in this way
implicitly calls \lstinline^add_message_type<T>^ for typed actors
\see{add-custom-message-type}. This makes our \lstinline^calculator^ actor type
serializable and also enables remote nodes to spawn calculators anywhere in the
distributed actor system (assuming all nodes use the same config).
\cppexample[98-109]{remoting/remote_spawn}
Our final example illustrates how to spawn a \lstinline^calculator^ locally by
using its type name. Because the dynamic type name lookup can fail and the
construction arguments passed as message can mismatch, this version of
\lstinline^spawn^ returns \lstinline^expected<T>^.
\begin{lstlisting}
auto x = system.spawn<calculator>("calculator", make_message());
if (! x) {
std::cerr << "*** unable to spawn calculator: "
<< system.render(x.error()) << std::endl;
return;
}
calculator c = std::move(*x);
\end{lstlisting}
Adding dynamically typed actors to the config is achieved in the same way. When
spawning a dynamically typed actor in this way, the template parameter is
simply \lstinline^actor^. For example, spawning an actor "foo" which requires
one string is created with:
\begin{lstlisting}
auto worker = system.spawn<actor>("foo", make_message("bar"));
\end{lstlisting}
Because constructor (or function) arguments for spawning the actor are stored
in a \lstinline^message^, only actors with appropriate input types are allowed.
For example, pointer types are illegal. Hence users need to replace C-strings
with \lstinline^std::string^.
\clearpage
\subsection{Log Output}
\label{log-output}
Logging is disabled in CAF per default. It can be enabled by setting the
\lstinline^--with-log-level=^ option of the \lstinline^configure^ script to one
of \lstinline^error^, \lstinline^warning^, \lstinline^info^, \lstinline^debug^,
or \lstinline^trace^ (from least output to most). Alternatively, setting the
CMake variable \lstinline^CAF_LOG_LEVEL^ to one of these values has the same
effect.
All logger-related configuration options listed here and in
\sref{system-config-options} are silently ignored if logging is disabled.
\subsubsection{File Name}
\label{log-output-file-name}
The output file is generated from the template configured by
\lstinline^logger-file-name^. This template supports the following variables.
\begin{tabular}{ll}
\hline
\textbf{Variable} & \textbf{Output} \\
\hline
\texttt{[PID]} & The OS-specific process ID. \\
\hline
\texttt{[TIMESTAMP]} & The UNIX timestamp on startup. \\
\hline
\texttt{[NODE]} & The node ID of the CAF system. \\
\hline
\end{tabular}
\subsubsection{Console}
\label{log-output-console}
Console output is disabled per default. Setting \lstinline^logger-console^ to
either \lstinline^"uncolored"^ or \lstinline^"colored"^ prints log events to
\lstinline^std::clog^. Using the \lstinline^"colored"^ option will print the
log events in different colors depending on the severity level.
\subsubsection{Format Strings}
\label{log-output-format-strings}
CAF uses log4j-like format strings for configuring printing of individual
events via \lstinline^logger-file-format^ and
\lstinline^logger-console-format^. Note that format modifiers are not supported
at the moment. The recognized field identifiers are:
\begin{tabular}{ll}
\hline
\textbf{Character} & \textbf{Output} \\
\hline
\texttt{c} & The category/component. \\
\hline
\texttt{C} & The full qualifier of the current function. For example, the qualifier of \lstinline^void ns::foo::bar()^ is printed as \lstinline^ns.foo^. \\
\hline
\texttt{d} & The date in ISO 8601 format, i.e., \lstinline^"YYYY-MM-DDThh:mm:ss"^. \\
\hline
\texttt{F} & The file name. \\
\hline
\texttt{L} & The line number. \\
\hline
\texttt{m} & The user-defined log message. \\
\hline
\texttt{M} & The name of the current function. For example, the name of \lstinline^void ns::foo::bar()^ is printed as \lstinline^bar^. \\
\hline
\texttt{n} & A newline. \\
\hline
\texttt{p} & The priority (severity level). \\
\hline
\texttt{r} & Elapsed time since starting the application in milliseconds. \\
\hline
\texttt{t} & ID of the current thread. \\
\hline
\texttt{a} & ID of the current actor (or \lstinline^actor0^ when not logging inside an actor). \\
\hline
\texttt{\%} & A single percent sign. \\
\hline
\end{tabular}
\subsubsection{Filtering}
\label{log-output-filtering}
The two configuration options \lstinline^logger-component-filter^ and
\lstinline^logger-verbosity^ reduce the amount of generated log events. The
former is a list of excluded component names and the latter can increase the
reported severity level (but not decrease it beyond the level defined at
compile time).
\section{Errors}
\label{error}
Errors in CAF have a code and a category, similar to
\lstinline^std::error_code^ and \lstinline^std::error_condition^. Unlike its
counterparts from the C++ standard library, \lstinline^error^ is
plattform-neutral and serializable. Instead of using category singletons, CAF
stores categories as atoms~\see{atom}. Errors can also include a message to
provide additional context information.
\subsection{Class Interface}
\begin{center}
\begin{tabular}{ll}
\textbf{Constructors} & ~ \\
\hline
\lstinline^(Enum x)^ & Construct error by calling \lstinline^make_error(x)^ \\
\hline
\lstinline^(uint8_t x, atom_value y)^ & Construct error with code \lstinline^x^ and category \lstinline^y^ \\
\hline
\lstinline^(uint8_t x, atom_value y, message z)^ & Construct error with code \lstinline^x^, category \lstinline^y^, and context \lstinline^z^ \\
\hline
~ & ~ \\ \textbf{Observers} & ~ \\
\hline
\lstinline^uint8_t code()^ & Returns the error code \\
\hline
\lstinline^atom_value category()^ & Returns the error category \\
\hline
\lstinline^message context()^ & Returns additional context information \\
\hline
\lstinline^explicit operator bool()^ & Returns \lstinline^code() != 0^ \\
\hline
\end{tabular}
\end{center}
\subsection{Add Custom Error Categories}
\label{custom-error}
Adding custom error categories requires three steps: (1)~declare an enum class
of type \lstinline^uint8_t^ with the first value starting at 1, (2)~specialize
\lstinline^error_category^ to give your type a custom ID (value 0-99 are
reserved by CAF), and (3)~add your error category to the actor system config.
The following example adds custom error codes for math errors.
\cppexample[17-47]{message_passing/divider}
\clearpage
\subsection{System Error Codes}
\label{sec}
System Error Codes (SECs) use the error category \lstinline^"system"^. They
represent errors in the actor system or one of its modules and are defined as
follows.
\sourcefile[32-117]{libcaf_core/caf/sec.hpp}
%\clearpage
\subsection{Default Exit Reasons}
\label{exit-reason}
CAF uses the error category \lstinline^"exit"^ for default exit reasons. These
errors are usually fail states set by the actor system itself. The two
exceptions are \lstinline^exit_reason::user_shutdown^ and
\lstinline^exit_reason::kill^. The former is used in CAF to signalize orderly,
user-requested shutdown and can be used by programmers in the same way. The
latter terminates an actor unconditionally when used in \lstinline^send_exit^,
even if the default handler for exit messages~\see{exit-message} is overridden.
\sourcefile[29-49]{libcaf_core/caf/exit_reason.hpp}
\section{Frequently Asked Questions}
\label{faq}
This Section is a compilation of the most common questions via GitHub, chat,
and mailing list.
\subsection{Can I Encrypt CAF Communication?}
Yes, by using the OpenSSL module~\see{free-remoting-functions}.
\subsection{Can I Create Messages Dynamically?}
Yes.
Usually, messages are created implicitly when sending messages but can also be
created explicitly using \lstinline^make_message^. In both cases, types and
number of elements are known at compile time. To allow for fully dynamic
message generation, CAF also offers \lstinline^message_builder^:
\begin{lstlisting}
message_builder mb;
// prefix message with some atom
mb.append(strings_atom::value);
// fill message with some strings
std::vector<std::string> strings{/*...*/};
for (auto& str : strings)
mb.append(str);
// create the message
message msg = mb.to_message();
\end{lstlisting}
\subsection{What Debugging Tools Exist?}
The \lstinline^scripts/^ and \lstinline^tools/^ directories contain some useful
tools to aid in development and debugging.
\lstinline^scripts/atom.py^ converts integer atom values back into strings.
\lstinline^scripts/demystify.py^ replaces cryptic \lstinline^typed_mpi<...>^
templates with \lstinline^replies_to<...>::with<...>^ and
\lstinline^atom_constant<...>^ with a human-readable representation of the
actual atom.
\lstinline^scripts/caf-prof^ is an R script that generates plots from CAF
profiler output.
\lstinline^caf-vec^ is a (highly) experimental tool that annotates CAF logs
with vector timestamps. It gives you happens-before relations and a nice
visualization via \href{https://bestchai.bitbucket.io/shiviz/}{ShiViz}.
\section{Overview}
Compiling CAF requires CMake and a C++11-compatible compiler. To get and
compile the sources on UNIX-like systems, type the following in a terminal:
\begin{verbatim}
git clone https://github.com/actor-framework/actor-framework
cd actor-framework
./configure
make
make install [as root, optional]
\end{verbatim}
We recommended to run the unit tests as well:
\begin{verbatim}
make test
\end{verbatim}
If the output indicates an error, please submit a bug report that includes (a)
your compiler version, (b) your OS, and (c) the content of the file
\texttt{build/Testing/Temporary/LastTest.log}.
\subsection{Features}
\begin{itemize}
\item Lightweight, fast and efficient actor implementations
\item Network transparent messaging
\item Error handling based on Erlang's failure model
\item Pattern matching for messages as internal DSL to ease development
\item Thread-mapped actors for soft migration of existing applications
\item Publish/subscribe group communication
\end{itemize}
\subsection{Minimal Compiler Versions}
\begin{itemize}
\item GCC 4.8
\item Clang 3.4
\item Visual Studio 2015, Update 3
\end{itemize}
\subsection{Supported Operating Systems}
\begin{itemize}
\item Linux
\item Mac OS X
\item Windows (static library only)
\end{itemize}
\clearpage
\subsection{Hello World Example}
\cppexample{hello_world}
\section{Type-Erased Tuples, Messages and Message Views}
\label{message}
Messages in CAF are stored in type-erased tuples. The actual message type
itself is usually hidden, as actors use pattern matching to decompose messages
automatically. However, the classes \lstinline^message^ and
\lstinline^message_builder^ allow more advanced use cases than only sending
data from one actor to another.
The interface \lstinline^type_erased_tuple^ encapsulates access to arbitrary
data. This data can be stored on the heap or on the stack. A
\lstinline^message^ is a type-erased tuple that is always heap-allocated and
uses copy-on-write semantics. When dealing with "plain" type-erased tuples,
users are required to check if a tuple is referenced by others via
\lstinline^type_erased_tuple::shared^ before modifying its content.
The convenience class \lstinline^message_view^ holds a reference to either a
stack-located \lstinline^type_erased_tuple^ or a \lstinline^message^. The
content of the data can be access via \lstinline^message_view::content^ in both
cases, which returns a \lstinline^type_erased_tuple&^. The content of the view
can be forced into a message object by calling
\lstinline^message_view::move_content_to_message^. This member function either
returns the stored message object or moves the content of a stack-allocated
tuple into a new message.
\subsection{RTTI and Type Numbers}
All builtin types in CAF have a non-zero 6-bit \emph{type number}. All
user-defined types are mapped to 0. When querying the run-time type information
(RTTI) for individual message or tuple elements, CAF returns a pair consisting
of an integer and a pointer to \lstinline^std::type_info^. The first value is
the 6-bit type number. If the type number is non-zero, the second value is a
pointer to the C++ type info, otherwise the second value is null. Additionally,
CAF generates 32 bit \emph{type tokens}. These tokens are \emph{type hints}
that summarizes all types in a type-erased tuple. Two type-erased tuples are of
different type if they have different type tokens (the reverse is not true).
\clearpage
\subsection{Class \lstinline^type_erased_tuple^}
\textbf{Note}: Calling modifiers on a shared type-erased tuple is undefined
behavior.
\begin{center}
\begin{tabular}{ll}
\textbf{Observers} & ~ \\
\hline
\lstinline^bool empty()^ & Returns whether this message is empty. \\
\hline
\lstinline^size_t size()^ & Returns the size of this message. \\
\hline
\lstinline^rtti_pair type(size_t pos)^ & Returns run-time type information for the nth element. \\
\hline
\lstinline^error save(serializer& x)^ & Writes the tuple to \lstinline^x^. \\
\hline
\lstinline^error save(size_t n, serializer& x)^ & Writes the nth element to \lstinline^x^. \\
\hline
\lstinline^const void* get(size_t n)^ & Returns a const pointer to the nth element. \\
\hline
\lstinline^std::string stringify()^ & Returns a string representation of the tuple. \\
\hline
\lstinline^std::string stringify(size_t n)^ & Returns a string representation of the nth element. \\
\hline
\lstinline^bool matches(size_t n, rtti_pair)^ & Checks whether the nth element has given type. \\
\hline
\lstinline^bool shared()^ & Checks whether more than one reference to the data exists. \\
\hline
\lstinline^bool match_element<T>(size_t n)^ & Checks whether element \lstinline^n^ has type \lstinline^T^. \\
\hline
\lstinline^bool match_elements<Ts...>()^ & Checks whether this message has the types \lstinline^Ts...^. \\
\hline
\lstinline^const T& get_as<T>(size_t n)^ & Returns a const reference to the nth element. \\
\hline
~ & ~ \\ \textbf{Modifiers} & ~ \\
\hline
\lstinline^void* get_mutable(size_t n)^ & Returns a mutable pointer to the nth element. \\
\hline
\lstinline^T& get_mutable_as<T>(size_t n)^ & Returns a mutable reference to the nth element. \\
\hline
\lstinline^void load(deserializer& x)^ & Reads the tuple from \lstinline^x^. \\
\hline
\end{tabular}
\end{center}
\subsection{Class \lstinline^message^}
The class \lstinline^message^ includes all member functions of
\lstinline^type_erased_tuple^. However, calling modifiers is always guaranteed
to be safe. A \lstinline^message^ automatically detaches its content by copying
it from the shared data on mutable access. The class further adds the following
member functions over \lstinline^type_erased_tuple^. Note that
\lstinline^apply^ only detaches the content if a callback takes mutable
references as arguments.
\begin{center}
\begin{tabular}{ll}
\textbf{Observers} & ~ \\
\hline
\lstinline^message drop(size_t n)^ & Creates a new message with all but the first \lstinline^n^ values. \\
\hline
\lstinline^message drop_right(size_t n)^ & Creates a new message with all but the last \lstinline^n^ values. \\
\hline
\lstinline^message take(size_t n)^ & Creates a new message from the first \lstinline^n^ values. \\
\hline
\lstinline^message take_right(size_t n)^ & Creates a new message from the last \lstinline^n^ values. \\
\hline
\lstinline^message slice(size_t p, size_t n)^ & Creates a new message from \lstinline^[p, p + n)^. \\
\hline
\lstinline^message extract(message_handler)^ & See \sref{extract}. \\
\hline
\lstinline^message extract_opts(...)^ & See \sref{extract-opts}. \\
\hline
~ & ~ \\ \textbf{Modifiers} & ~ \\
\hline
\lstinline^optional<message> apply(message_handler f)^ & Returns \lstinline^f(*this)^. \\
\hline
~ & ~ \\ \textbf{Operators} & ~ \\
\hline
\lstinline^message operator+(message x, message y)^ & Concatenates \lstinline^x^ and \lstinline^y^. \\
\hline
\lstinline^message& operator+=(message& x, message y)^ & Concatenates \lstinline^x^ and \lstinline^y^. \\
\hline
\end{tabular}
\end{center}
\clearpage
\subsection{Class \texttt{message\_builder}}
\begin{center}
\begin{tabular}{ll}
\textbf{Constructors} & ~ \\
\hline
\lstinline^(void)^ & Creates an empty message builder. \\
\hline
\lstinline^(Iter first, Iter last)^ & Adds all elements from range \lstinline^[first, last)^. \\
\hline
~ & ~ \\ \textbf{Observers} & ~ \\
\hline
\lstinline^bool empty()^ & Returns whether this message is empty. \\
\hline
\lstinline^size_t size()^ & Returns the size of this message. \\
\hline
\lstinline^message to_message( )^ & Converts the buffer to an actual message object. \\
\hline
\lstinline^append(T val)^ & Adds \lstinline^val^ to the buffer. \\
\hline
\lstinline^append(Iter first, Iter last)^ & Adds all elements from range \lstinline^[first, last)^. \\
\hline
\lstinline^message extract(message_handler)^ & See \sref{extract}. \\
\hline
\lstinline^message extract_opts(...)^ & See \sref{extract-opts}. \\
\hline
~ & ~ \\ \textbf{Modifiers} & ~ \\
\hline
\lstinline^optional<message>^ \lstinline^apply(message_handler f)^ & Returns \lstinline^f(*this)^. \\
\hline
\lstinline^message move_to_message()^ & Transfers ownership of its data to the new message. \\
\hline
\end{tabular}
\end{center}
\clearpage
\subsection{Extracting}
\label{extract}
The member function \lstinline^message::extract^ removes matched elements from
a message. x Messages are filtered by repeatedly applying a message handler to
the greatest remaining slice, whereas slices are generated in the sequence $[0,
size)$, $[0, size-1)$, $...$, $[1, size-1)$, $...$, $[size-1, size)$. Whenever
a slice is matched, it is removed from the message and the next slice starts at
the same index on the reduced message.
For example:
\begin{lstlisting}
auto msg = make_message(1, 2.f, 3.f, 4);
// remove float and integer pairs
auto msg2 = msg.extract({
[](float, float) { },
[](int, int) { }
});
assert(msg2 == make_message(1, 4));
\end{lstlisting}
Step-by-step explanation:
\begin{itemize}
\item Slice 1: \lstinline^(1, 2.f, 3.f, 4)^, no match
\item Slice 2: \lstinline^(1, 2.f, 3.f)^, no match
\item Slice 3: \lstinline^(1, 2.f)^, no match
\item Slice 4: \lstinline^(1)^, no match
\item Slice 5: \lstinline^(2.f, 3.f, 4)^, no match
\item Slice 6: \lstinline^(2.f, 3.f)^, \emph{match}; new message is \lstinline^(1, 4)^
\item Slice 7: \lstinline^(4)^, no match
\end{itemize}
Slice 7 is \lstinline^(4)^, i.e., does not contain the first element, because
the match on slice 6 occurred at index position 1. The function
\lstinline^extract^ iterates a message only once, from left to right. The
returned message contains the remaining, i.e., unmatched, elements.
\clearpage
\subsection{Extracting Command Line Options}
\label{extract-opts}
The class \lstinline^message^ also contains a convenience interface to
\lstinline^extract^ for parsing command line options: the member function
\lstinline^extract_opts^.
\begin{lstlisting}
int main(int argc, char** argv) {
uint16_t port;
string host = "localhost";
auto res = message_builder(argv + 1, argv + argc).extract_opts({
{"port,p", "set port", port},
{"host,H", "set host (default: localhost)", host},
{"verbose,v", "enable verbose mode"}
});
if (! res.error.empty()) {
// read invalid CLI arguments
cerr << res.error << endl;
return 1;
}
if (res.opts.count("help") > 0) {
// CLI arguments contained "-h", "--help", or "-?" (builtin);
cout << res.helptext << endl;
return 0;
}
if (! res.remainder.empty()) {
// res.remainder stors all extra arguments that weren't consumed
}
if (res.opts.count("verbose") > 0) {
// enable verbose mode
}
// ...
}
/*
Output of ./program_name -h:
Allowed options:
-p [--port] arg : set port
-H [--host] arg : set host (default: localhost)
-v [--verbose] : enable verbose mode
*/
\end{lstlisting}
\section{Middleman}
\label{middleman}
The middleman is the main component of the I/O module and enables distribution.
It transparently manages proxy actor instances representing remote actors,
maintains connections to other nodes, and takes care of serialization of
messages. Applications install a middleman by loading
\lstinline^caf::io::middleman^ as module~\see{system-config}. Users can include
\lstinline^"caf/io/all.hpp"^ to get access to all public classes of the I/O
module.
\subsection{Class \texttt{middleman}}
\begin{center}
\begin{tabular}{ll}
\textbf{Remoting} & ~ \\
\hline
\lstinline^expected<uint16> open(uint16, const char*, bool)^ & See~\sref{remoting}. \\
\hline
\lstinline^expected<uint16> publish(T, uint16, const char*, bool)^ & See~\sref{remoting}. \\
\hline
\lstinline^expected<void> unpublish(T x, uint16)^ & See~\sref{remoting}. \\
\hline
\lstinline^expected<node_id> connect(std::string host, uint16_t port)^ & See~\sref{remoting}. \\
\hline
\lstinline^expected<T> remote_actor<T = actor>(string, uint16)^ & See~\sref{remoting}. \\
\hline
\lstinline^expected<T> spawn_broker(F fun, ...)^ & See~\sref{broker}. \\
\hline
\lstinline^expected<T> spawn_client(F, string, uint16, ...)^ & See~\sref{broker}. \\
\hline
\lstinline^expected<T> spawn_server(F, uint16, ...)^ & See~\sref{broker}. \\
\hline
\end{tabular}
\end{center}
\subsection{Publishing and Connecting}
\label{remoting}
The member function \lstinline^publish^ binds an actor to a given port, thereby
allowing other nodes to access it over the network.
\begin{lstlisting}
template <class T>
expected<uint16_t> middleman::publish(T x, uint16_t port,
const char* in = nullptr,
bool reuse_addr = false);
\end{lstlisting}
The first argument is a handle of type \lstinline^actor^ or
\lstinline^typed_actor<...>^. The second argument denotes the TCP port. The OS
will pick a random high-level port when passing 0. The third parameter
configures the listening address. Passing null will accept all incoming
connections (\lstinline^INADDR_ANY^). Finally, the flag \lstinline^reuse_addr^
controls the behavior when binding an IP address to a port, with the same
semantics as the BSD socket flag \lstinline^SO_REUSEADDR^. For example, with
\lstinline^reuse_addr = false^, binding two sockets to 0.0.0.0:42 and
10.0.0.1:42 will fail with \texttt{EADDRINUSE} since 0.0.0.0 includes 10.0.0.1.
With \lstinline^reuse_addr = true^ binding would succeed because 10.0.0.1 and
0.0.0.0 are not literally equal addresses.
The member function returns the bound port on success. Otherwise, an
\lstinline^error^ \see{error} is returned.
\begin{lstlisting}
template <class T>
expected<uint16_t> middleman::unpublish(T x, uint16_t port = 0);
\end{lstlisting}
The member function \lstinline^unpublish^ allows actors to close a port
manually. This is performed automatically if the published actor terminates.
Passing 0 as second argument closes all ports an actor is published to,
otherwise only one specific port is closed.
The function returns an \lstinline^error^ \see{error} if the actor was not
bound to given port.
\clearpage
\begin{lstlisting}
template<class T = actor>
expected<T> middleman::remote_actor(std::string host, uint16_t port);
\end{lstlisting}
After a server has published an actor with \lstinline^publish^, clients can
connect to the published actor by calling \lstinline^remote_actor^:
\begin{lstlisting}
// node A
auto ping = spawn(ping);
system.middleman().publish(ping, 4242);
// node B
auto ping = system.middleman().remote_actor("node A", 4242);
if (! ping) {
cerr << "unable to connect to node A: "
<< system.render(ping.error()) << std::endl;
} else {
self->send(*ping, ping_atom::value);
}
\end{lstlisting}
There is no difference between server and client after the connection phase.
Remote actors use the same handle types as local actors and are thus fully
transparent.
The function pair \lstinline^open^ and \lstinline^connect^ allows users to
connect CAF instances without remote actor setup. The function
\lstinline^connect^ returns a \lstinline^node_id^ that can be used for remote
spawning (see~\sref{remote-spawn}).
\subsection{Free Functions}
\label{free-remoting-functions}
The following free functions in the namespace \lstinline^caf::io^ avoid calling
the middleman directly. This enables users to easily switch between
communication backends as long as the interfaces have the same signatures. For
example, the (experimental) OpenSSL binding of CAF implements the same
functions in the namespace \lstinline^caf::openssl^ to easily switch between
encrypted and unencrypted communication.
\begin{center}
\begin{tabular}{ll}
\hline
\lstinline^expected<uint16> open(actor_system&, uint16, const char*, bool)^ & See~\sref{remoting}. \\
\hline
\lstinline^expected<uint16> publish(T, uint16, const char*, bool)^ & See~\sref{remoting}. \\
\hline
\lstinline^expected<void> unpublish(T x, uint16)^ & See~\sref{remoting}. \\
\hline
\lstinline^expected<node_id> connect(actor_system&, std::string host, uint16_t port)^ & See~\sref{remoting}. \\
\hline
\lstinline^expected<T> remote_actor<T = actor>(actor_system&, string, uint16)^ & See~\sref{remoting}. \\
\hline
\end{tabular}
\end{center}
\subsection{Transport Protocols \experimental}
\label{transport-protocols}
CAF communication uses TCP per default and thus the functions shown in the
middleman API above are related to TCP. There are two alternatives to plain
TCP: TLS via the OpenSSL module shortly discussed in
\sref{free-remoting-functions} and UDP.
UDP is integrated in the default multiplexer and BASP broker. Set the flag
\lstinline^middleman_enable_udp^ to true to enable it
(see~\sref{system-config}). This does not require you to disable TCP. Use
\lstinline^publish_udp^ and \lstinline^remote_actor_udp^ to establish
communication.
Communication via UDP is inherently unreliable and unordered. CAF reestablishes
order and drops messages that arrive late. Messages that are sent via datagrams
are limited to a maximum of 65.535 bytes which is used as a receive buffer size
by CAF. Note that messages that exceed the MTU are fragmented by IP and are
considered lost if a single fragment is lost. Optional reliability based on
retransmissions and messages slicing on the application layer are planned for
the future.
\section{Registry}
\label{registry}
The actor registry in CAF keeps track of the number of running actors and
allows to map actors to their ID or a custom atom~\see{atom} representing a
name. The registry does \emph{not} contain all actors. Actors have to be stored
in the registry explicitly. Users can access the registry through an actor
system by calling \lstinline^system.registry()^. The registry stores actors
using \lstinline^strong_actor_ptr^ \see{actor-pointer}.
Users can use the registry to make actors system-wide available by name. The
middleman~\see{middleman} uses the registry to keep track of all actors known
to remote nodes in order to serialize and deserialize them. Actors are removed
automatically when they terminate.
It is worth mentioning that the registry is not synchronized between connected
actor system. Each actor system has its own, local registry in a distributed
setting.
\begin{center}
\begin{tabular}{ll}
\textbf{Types} & ~ \\
\hline
\lstinline^name_map^ & \lstinline^unordered_map<atom_value, strong_actor_ptr>^ \\
\hline
~ & ~ \\ \textbf{Observers} & ~ \\
\hline
\lstinline^strong_actor_ptr get(actor_id)^ & Returns the actor associated to given ID. \\
\hline
\lstinline^strong_actor_ptr get(atom_value)^ & Returns the actor associated to given name. \\
\hline
\lstinline^name_map named_actors()^ & Returns all name mappings. \\
\hline
\lstinline^size_t running()^ & Returns the number of currently running actors. \\
\hline
~ & ~ \\ \textbf{Modifiers} & ~ \\
\hline
\lstinline^void put(actor_id, strong_actor_ptr)^ & Maps an actor to its ID. \\
\hline
\lstinline^void erase(actor_id)^ & Removes an ID mapping from the registry. \\
\hline
\lstinline^void put(atom_value, strong_actor_ptr)^ & Maps an actor to a name. \\
\hline
\lstinline^void erase(atom_value)^ & Removes a name mapping from the registry. \\
\hline
\end{tabular}
\end{center}
\section{Remote Spawning of Actors \experimental}
\label{remote-spawn}
Remote spawning is an extension of the dynamic spawn using run-time type names
\see{add-custom-actor-type}. The following example assumes a typed actor handle
named \lstinline^calculator^ with an actor implementing this messaging
interface named "calculator".
\cppexample[123-137]{remoting/remote_spawn}
We first connect to a CAF node with \lstinline^middleman().connect(...)^. On
success, \lstinline^connect^ returns the node ID we need for
\lstinline^remote_spawn^. This requires the server to open a port with
\lstinline^middleman().open(...)^ or \lstinline^middleman().publish(...)^.
Alternatively, we can obtain the node ID from an already existing remote actor
handle---returned from \lstinline^remote_actor^ for example---via
\lstinline^hdl->node()^. After connecting to the server, we can use
\lstinline^middleman().remote_spawn<...>(...)^ to create actors remotely.
\section{Testing}
\label{testing}
CAF comes with built-in support for writing unit tests in a domain-specific
language (DSL). The API looks similar to well-known testing frameworks such as
Boost.Test and Catch but adds CAF-specific macros for testing messaging between
actors.
Our design leverages four main concepts:
\begin{itemize}
\item \textbf{Checks} represent single boolean expressions.
\item \textbf{Tests} contain one or more checks.
\item \textbf{Fixtures} equip tests with a fixed data environment.
\item \textbf{Suites} group tests together.
\end{itemize}
The following code illustrates a very basic test case that captures the four
main concepts described above.
\begin{lstlisting}
// Adds all tests in this compilation unit to the suite "math".
#define CAF_SUITE math
// Pulls in all the necessary macros.
#include "caf/test/dsl.hpp"
namespace {
struct fixture {};
} // namespace
// Makes all members of `fixture` available to tests in the scope.
CAF_TEST_FIXTURE_SCOPE(math_tests, fixture)
// Implements our first test.
CAF_TEST(divide) {
CAF_CHECK(1 / 1 == 0); // fails
CAF_CHECK(2 / 2 == 1); // passes
CAF_REQUIRE(3 + 3 == 5); // fails and aborts test execution
CAF_CHECK(4 - 4 == 0); // unreachable due to previous requirement error
}
CAF_TEST_FIXTURE_SCOPE_END()
\end{lstlisting}
The code above highlights the two basic macros \lstinline^CAF_CHECK^ and
\lstinline^CAF_REQUIRE^. The former reports failed checks, but allows the test
to continue on error. The latter stops test execution if the boolean expression
evaluates to false.
The third macro worth mentioning is \lstinline^CAF_FAIL^. It unconditionally
stops test execution with an error message. This is particularly useful for
stopping program execution after receiving unexpected messages, as we will see
later.
\subsection{Testing Actors}
The following example illustrates how to add an actor system as well as a
scoped actor to fixtures. This allows spawning of and interacting with actors
in a similar way regular programs would. Except that we are using macros such
as \lstinline^CAF_CHECK^ and provide tests rather than implementing a
\lstinline^caf_main^.
\begin{lstlisting}
namespace {
struct fixture {
caf::actor_system_config cfg;
caf::actor_system sys;
caf::scoped_actor self;
fixture() : sys(cfg), self(sys) {
// nop
}
};
caf::behavior adder() {
return {
[=](int x, int y) {
return x + y;
}
};
}
} // namespace
CAF_TEST_FIXTURE_SCOPE(actor_tests, fixture)
CAF_TEST(simple actor test) {
// Our Actor-Under-Test.
auto aut = self->spawn(adder);
self->request(aut, caf::infinite, 3, 4).receive(
[=](int r) {
CAF_CHECK(r == 7);
},
[&](caf::error& err) {
// Must not happen, stop test.
CAF_FAIL(sys.render(err));
});
}
CAF_TEST_FIXTURE_SCOPE_END()
\end{lstlisting}
The example above works, but suffers from several issues:
\begin{itemize}
\item
Significant amount of boilerplate code.
\item
Using a scoped actor as illustrated above can only test one actor at a
time. However, messages between other actors are invisible to us.
\item
CAF runs actors in a thread pool by default. The resulting nondeterminism
makes triggering reliable ordering of messages near impossible. Further,
forcing timeouts to test error handling code is even harder.
\end{itemize}
\subsection{Deterministic Testing}
CAF provides a scheduler implementation specifically tailored for writing unit
tests called \lstinline^test_coordinator^. It does not start any threads and
instead gives unit tests full control over message dispatching and timeout
management.
To reduce boilerplate code, CAF also provides a fixture template called
\lstinline^test_coordinator_fixture^ that comes with ready-to-use actor system
(\lstinline^sys^) and testing scheduler (\lstinline^sched^). The optional
template parameter allows unit tests to plugin custom actor system
configuration classes.
Using this fixture unlocks three additional macros:
\begin{itemize}
\item
\lstinline^expect^ checks for a single message. The macro verifies the
content types of the message and invokes the necessary member functions on
the test coordinator. Optionally, the macro checks the receiver of the
message and its content. If the expected message does not exist, the test
aborts.
\item
\lstinline^allow^ is similar to \lstinline^expect^, but it does not abort
the test if the expected message is missing. This macro returns
\lstinline^true^ if the allowed message was delivered, \lstinline^false^
otherwise.
\item
\lstinline^disallow^ aborts the test if a particular message was delivered
to an actor.
\end{itemize}
The following example implements two actors, \lstinline^ping^ and
\lstinline^pong^, that exchange a configurable amount of messages. The test
\emph{three pings} then checks the contents of each message with
\lstinline^expect^ and verifies that no additional messages exist using
\lstinline^disallow^.
\cppexample[12-60]{testing/ping_pong}
\section{Type Inspection (Serialization and String Conversion)}
\label{type-inspection}
CAF is designed with distributed systems in mind. Hence, all message types
must be serializable and need a platform-neutral, unique name that is
configured at startup \see{add-custom-message-type}. Using a message type that
is not serializable causes a compiler error \see{unsafe-message-type}. CAF
serializes individual elements of a message by using the inspection API. This
API allows users to provide code for serialization as well as string conversion
with a single free function. The signature for a class \lstinline^my_class^ is
always as follows:
\begin{lstlisting}
template <class Inspector>
typename Inspector::result_type inspect(Inspector& f, my_class& x) {
return f(...);
}
\end{lstlisting}
The function \lstinline^inspect^ passes meta information and data fields to the
variadic call operator of the inspector. The following example illustrates an
implementation for \lstinline^inspect^ for a simple POD struct.
\cppexample[23-33]{custom_type/custom_types_1}
The inspector recursively inspects all data fields and has builtin support for
(1) \lstinline^std::tuple^, (2) \lstinline^std::pair^, (3) C arrays, (4) any
container type with \lstinline^x.size()^, \lstinline^x.empty()^,
\lstinline^x.begin()^ and \lstinline^x.end()^.
We consciously made the inspect API as generic as possible to allow for
extensibility. This allows users to use CAF's types in other contexts, to
implement parsers, etc.
\subsection{Inspector Concept}
The following concept class shows the requirements for inspectors. The
placeholder \lstinline^T^ represents any user-defined type. For example,
\lstinline^error^ when performing I/O operations or some integer type when
implementing a hash function.
\begin{lstlisting}
Inspector {
using result_type = T;
if (inspector only requires read access to the state of T)
static constexpr bool reads_state = true;
else
static constexpr bool writes_state = true;
template <class... Ts>
result_type operator()(Ts&&...);
}
\end{lstlisting}
A saving \lstinline^Inspector^ is required to handle constant lvalue and rvalue
references. A loading \lstinline^Inspector^ must only accept mutable lvalue
references to data fields, but still allow for constant lvalue references and
rvalue references to annotations.
\subsection{Annotations}
Annotations allow users to fine-tune the behavior of inspectors by providing
addition meta information about a type. All annotations live in the namespace
\lstinline^caf::meta^ and derive from \lstinline^caf::meta::annotation^. An
inspector can query whether a type \lstinline^T^ is an annotation with
\lstinline^caf::meta::is_annotation<T>::value^. Annotations are passed to the
call operator of the inspector along with data fields. The following list shows
all annotations supported by CAF:
\begin{itemize}
\item \lstinline^type_name(n)^: Display type name as \lstinline^n^ in
human-friendly output (position before data fields).
\item \lstinline^hex_formatted()^: Format the following data field in hex
format.
\item \lstinline^omittable()^: Omit the following data field in human-friendly
output.
\item \lstinline^omittable_if_empty()^: Omit the following data field if it is
empty in human-friendly output.
\item \lstinline^omittable_if_none()^: Omit the following data field if it
equals \lstinline^none^ in human-friendly output.
\item \lstinline^save_callback(f)^: Call \lstinline^f^ when serializing
(position after data fields).
\item \lstinline^load_callback(f)^: Call \lstinline^f^ after deserializing all
data fields (position after data fields).
\end{itemize}
\subsection{Backwards and Third-party Compatibility}
CAF evaluates common free function other than \lstinline^inspect^ in order to
simplify users to integrate CAF into existing code bases.
Serializers and deserializers call user-defined \lstinline^serialize^
functions. Both types support \lstinline^operator&^ as well as
\lstinline^operator()^ for individual data fields. A \lstinline^serialize^
function has priority over \lstinline^inspect^.
When converting a user-defined type to a string, CAF calls user-defined
\lstinline^to_string^ functions and prefers those over \lstinline^inspect^.
\subsection{Whitelisting Unsafe Message Types}
\label{unsafe-message-type}
Message types that are not serializable cause compile time errors when used in
actor communication. When using CAF for concurrency only, this errors can be
suppressed by whitelisting types with
\lstinline^CAF_ALLOW_UNSAFE_MESSAGE_TYPE^. The macro is defined as follows.
% TODO: expand example here\cppexample[linerange={50-54}]{../../libcaf_core/caf/allowed_unsafe_message_type.hpp}
\clearpage
\subsection{Splitting Save and Load Operations}
If loading and storing cannot be implemented in a single function, users can
query whether the inspector is loading or storing. For example, consider the
following class \lstinline^foo^ with getter and setter functions and no public
access to its members.
\cppexample[20-49]{custom_type/custom_types_3}
\clearpage
Since there is no access to the data fields \lstinline^a_^ and \lstinline^b_^
(and assuming no changes to \lstinline^foo^ are possible), we need to split our
implementation of \lstinline^inspect^ as shown below.
\cppexample[76-103]{custom_type/custom_types_3}
The purpose of the scope guard in the example above is to write the content of
the temporaries back to \lstinline^foo^ at scope exit automatically. Storing
the result of \lstinline^f(...)^ in a temporary first and then writing the
changes to \lstinline^foo^ is not possible, because \lstinline^f(...)^ can
return \lstinline^void^.
\section{Using \texttt{aout} -- A Concurrency-safe Wrapper for \texttt{cout}}
When using \lstinline^cout^ from multiple actors, output often appears
interleaved. Moreover, using \lstinline^cout^ from multiple actors -- and thus
from multiple threads -- in parallel should be avoided regardless, since the
standard does not guarantee a thread-safe implementation.
By replacing \texttt{std::cout} with \texttt{caf::aout}, actors can achieve a
concurrency-safe text output. The header \lstinline^caf/all.hpp^ also defines
overloads for \texttt{std::endl} and \texttt{std::flush} for \lstinline^aout^,
but does not support the full range of ostream operations (yet). Each write
operation to \texttt{aout} sends a message to a `hidden' actor. This actor only
prints lines, unless output is forced using \lstinline^flush^. The example
below illustrates printing of lines of text from multiple actors (in random
order).
\cppexample{aout}
\section{Utility}
\label{utility}
CAF includes a few utility classes that are likely to be part of C++
eventually (or already are in newer versions of the standard). However, until
these classes are part of the standard library on all supported compilers, we
unfortunately have to maintain our own implementations.
\subsection{Class \lstinline^optional^}
\label{optional}
Represents a value that may or may not exist.
\begin{center}
\begin{tabular}{ll}
\textbf{Constructors} & ~ \\
\hline
\lstinline^(T value)^ & Constructs an object with a value. \\
\hline
\lstinline^(none_t = none)^ & Constructs an object without a value. \\
\hline
~ & ~ \\ \textbf{Observers} & ~ \\
\hline
\lstinline^explicit operator bool()^ & Checks whether the object contains a value. \\
\hline
\lstinline^T* operator->()^ & Accesses the contained value. \\
\hline
\lstinline^T& operator*()^ & Accesses the contained value. \\
\hline
\end{tabular}
\end{center}
\subsection{Class \lstinline^expected^}
Represents the result of a computation that \emph{should} return a value. If no
value could be produced, the \lstinline^expected<T>^ contains an
\lstinline^error^ \see{error}.
\begin{center}
\begin{tabular}{ll}
\textbf{Constructors} & ~ \\
\hline
\lstinline^(T value)^ & Constructs an object with a value. \\
\hline
\lstinline^(error err)^ & Constructs an object with an error. \\
\hline
~ & ~ \\ \textbf{Observers} & ~ \\
\hline
\lstinline^explicit operator bool()^ & Checks whether the object contains a value. \\
\hline
\lstinline^T* operator->()^ & Accesses the contained value. \\
\hline
\lstinline^T& operator*()^ & Accesses the contained value. \\
\hline
\lstinline^error& error()^ & Accesses the contained error. \\
\hline
\end{tabular}
\end{center}
\subsection{Constant \lstinline^unit^}
The constant \lstinline^unit^ of type \lstinline^unit_t^ is the equivalent of
\lstinline^void^ and can be used to initialize \lstinline^optional<void>^ and
\lstinline^expected<void>^.
\subsection{Constant \lstinline^none^}
The constant \lstinline^none^ of type \lstinline^none_t^ can be used to
initialize an \lstinline^optional<T>^ to represent ``nothing''.
\documentclass[%
a4paper,% % DIN A4
oneside,% % einseitiger Druck
12pt,% % 12pt Schriftgröße
]{article}
\usepackage[utf8]{inputenc}
% include required packages
\usepackage{array}
\usepackage{capt-of}
\usepackage{color}
\usepackage{float}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{multicol}
\usepackage{tabularx}
\usepackage{url}
\usepackage{xifthen}
\usepackage{xspace}
% font setup
\usepackage[scaled=.90]{helvet}
\usepackage{cmbright}
\usepackage{courier}
\usepackage{txfonts}
% paragraph settings
\parindent 0pt
\parskip 8pt
\pretolerance=150
\tolerance=500
\emergencystretch=\maxdimen
\hbadness=10000
\pagenumbering{arabic}
% custom colors
\definecolor{lightgrey}{rgb}{0.9,0.9,0.9}
\definecolor{lightblue}{rgb}{0,0,1}
\definecolor{grey}{rgb}{0.5,0.5,0.5}
\definecolor{blue}{rgb}{0,0,1}
\definecolor{violet}{rgb}{0.5,0,0.5}
\definecolor{darkred}{rgb}{0.5,0,0}
\definecolor{darkblue}{rgb}{0,0,0.5}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\input{tex/variables}
\title{%
\texttt{\huge{\textbf{CAF}}}\\
~\\
The C++ Actor Framework\\
~\\
~\\
~\\
User Manual\\
\normalsize{Version \cafrelease}\\
~\\
~\\
\tiny SHA \cafsha
\vfill}
\author{Dominik Charousset}
\date{\today}
% page setup
\setlength{\voffset}{-1in}
\setlength{\hoffset}{-0.75in}
\addtolength{\textwidth}{1.5in}
\addtolength{\textheight}{2in}
\setlength{\headheight}{15pt}
% include paragraphs in TOC
\setcounter{tocdepth}{3}
% more compact itemize
\newenvironment{itemize*}%
{\begin{itemize}%
\setlength{\itemsep}{0pt}%
\setlength{\parskip}{0pt}}%
{\end{itemize}}
\begin{document}
\maketitle\thispagestyle{empty}
\pagestyle{empty}
\clearpage
\tableofcontents
\clearpage
\setcounter{page}{1}
\pagestyle{plain}
% directory layout
\graphicspath{{pdf/}}
% custom commands
\newcommand{\sref}[1]{\S\,\ref{#1}}
\newcommand{\see}[1]{(see~\sref{#1})}
\newcommand{\singlefig}[3]{
\begin{figure}[H]
\centering
\includegraphics[width=.6\columnwidth]{#1}
\caption{#2}
\label{#3}
\end{figure}
}
\newcommand{\experimental}{
{\color{darkred}\textsuperscript{experimental}}
}
\newcommand{\cppexample}[2][]{%
\ifthenelse{\isempty{#1}}%
{\lstinputlisting{\cafroot/examples/#2.cpp}}%
{\lstinputlisting[language=C++,linerange={#1}]{\cafroot/examples/#2.cpp}}%
}
\newcommand{\iniexample}[2][]{%
\ifthenelse{\isempty{#1}}%
{\lstinputlisting[language=ini]{\cafroot/examples/#2.ini}}%
{\lstinputlisting[language=ini,linerange={#1}]{\cafroot/examples/#2.ini}}%
}
\newcommand{\sourcefile}[2][]{%
\ifthenelse{\isempty{#1}}%
{\lstinputlisting[language=C++]{\cafroot/#2}}%
{\lstinputlisting[language=C++,linerange={#1}]{\cafroot/#2}}%
}
% highlight for INI file syntax
\lstdefinelanguage{ini}{%
basicstyle=\ttfamily\footnotesize,%
% columns=fullflexible,%
morecomment=[s][\color{blue}]{[}{]},%
morecomment=[l]{;},%
morecomment=[s]{<}{>},%
morestring=[b]",%
morestring=[b]',%
commentstyle=\color{violet},%
morekeywords={},%
otherkeywords={=,false,true},%
keywordstyle=\color{blue},%
stringstyle=\color{darkgreen},%
}
% code listings setup
\lstset{%
language=C++,%
morekeywords={constexpr,nullptr,size_t,uint32_t,assert,override,final},%
basicstyle=\ttfamily\footnotesize,%
sensitive=true,%
keywordstyle=\color{blue},%
stringstyle=\color{darkgreen},%
commentstyle=\color{violet},%
showstringspaces=false,%
tabsize=2,%
frame=leftline,
rulecolor=\color{lightblue},
xleftmargin=20pt,
}
\lstset{
numberstyle=\tiny,
numbers=left,
numbersep=10pt,
xleftmargin=20pt,
%framesep=4.5mm,
%framexleftmargin=2.5mm,
framexleftmargin=5pt,
framesep=15pt,
fillcolor=\color{lightgrey},
}
% content
\part{Core Library}
\include{tex/Introduction}
\include{tex/FirstSteps}
\include{tex/TypeInspection}
\include{tex/MessageHandlers}
\include{tex/Actors}
\include{tex/MessagePassing}
\include{tex/Scheduler}
\include{tex/Registry}
\include{tex/ReferenceCounting}
\include{tex/Error}
\include{tex/ConfiguringActorApplications}
\include{tex/Messages}
\include{tex/GroupCommunication}
\include{tex/ManagingGroupsOfWorkers}
\include{tex/Streaming}
\include{tex/Testing}
\part{I/O Library}
\include{tex/NetworkTransparency}
\include{tex/Brokers}
\include{tex/RemoteSpawn}
\part{Appendix}
\include{tex/FAQ}
\include{tex/Utility}
\include{tex/CommonPitfalls}
\include{tex/UsingAout}
\include{tex/MigrationGuides}
\end{document}
CAF User Manual
===============
**C++ Actor Framework** version |release|.
Contents
========
.. toctree::
:maxdepth: 2
:caption: Core Library
manual/Introduction
manual/Overview
manual/TypeInspection
manual/MessageHandlers
manual/Actors
manual/MessagePassing
manual/Scheduler
manual/Registry
manual/ReferenceCounting
manual/Error
manual/ConfiguringActorApplications
manual/Messages
manual/GroupCommunication
manual/ManagingGroupsOfWorkers
manual/Streaming
manual/Testing
.. toctree::
:maxdepth: 2
:caption: I/O Library
manual/NetworkTransparency
manual/Brokers
manual/RemoteSpawn
.. toctree::
:maxdepth: 2
:caption: Appendix
manual/FAQ
manual/Utility
manual/CommonPitfalls
manual/UsingAout
manual/MigrationGuides
.. _actor:
Actors
======
Actors in CAF are a lightweight abstraction for units of computations. They
are active objects in the sense that they own their state and do not allow
others to access it. The only way to modify the state of an actor is sending
messages to it.
CAF provides several actor implementations, each covering a particular use
case. The available implementations differ in three characteristics: (1)
dynamically or statically typed, (2) class-based or function-based, and (3)
using asynchronous event handlers or blocking receives. These three
characteristics can be combined freely, with one exception: statically typed
actors are always event-based. For example, an actor can have dynamically typed
messaging, implement a class, and use blocking receives. The common base class
for all user-defined actors is called ``local_actor``.
Dynamically typed actors are more familiar to developers coming from Erlang or
Akka. They (usually) enable faster prototyping but require extensive unit
testing. Statically typed actors require more source code but enable the
compiler to verify communication between actors. Since CAF supports both,
developers can freely mix both kinds of actors to get the best of both worlds.
A good rule of thumb is to make use of static type checking for actors that are
visible across multiple translation units.
Actors that utilize the blocking receive API always require an exclusive thread
of execution. Event-based actors, on the other hand, are usually scheduled
cooperatively and are very lightweight with a memory footprint of only few
hundred bytes. Developers can exclude---detach---event-based actors that
potentially starve others from the cooperative scheduling while spawning it. A
detached actor lives in its own thread of execution.
.. _actor-system:
Environment / Actor Systems
---------------------------
All actors live in an ``actor_system`` representing an actor environment
including :ref:`scheduler`, :ref:`registry`, and optional components such as a
:ref:`middleman`. A single process can have multiple ``actor_system`` instances,
but this is usually not recommended (a use case for multiple systems is to
strictly separate two or more sets of actors by running them in different
schedulers). For configuration and fine-tuning options of actor systems see
:ref:`system-config`. A distributed CAF application consists of two or more
connected actor systems. We also refer to interconnected ``actor_system``
instances as a *distributed actor system*.
Common Actor Base Types
-----------------------
The following pseudo-UML depicts the class diagram for actors in CAF.
Irrelevant member functions and classes as well as mixins are omitted for
brevity. Selected individual classes are presented in more detail in the
following sections.
.. _actor-types:
.. image:: actor_types.png
:alt: Actor Types in CAF
Class ``local_actor``
~~~~~~~~~~~~~~~~~~~~~
The class ``local_actor`` is the root type for all user-defined actors
in CAF. It defines all common operations. However, users of the library
usually do not inherit from this class directly. Proper base classes for
user-defined actors are ``event_based_actor`` or
``blocking_actor``. The following table also includes member function
inherited from ``monitorable_actor`` and ``abstract_actor``.
+-------------------------------------+--------------------------------------------------------+
| **Types** | |
+-------------------------------------+--------------------------------------------------------+
| ``mailbox_type`` | A concurrent, many-writers-single-reader queue type. |
+-------------------------------------+--------------------------------------------------------+
| | |
+-------------------------------------+--------------------------------------------------------+
| **Constructors** | |
+-------------------------------------+--------------------------------------------------------+
| ``(actor_config&)`` | Constructs the actor using a config. |
+-------------------------------------+--------------------------------------------------------+
| | |
+-------------------------------------+--------------------------------------------------------+
| **Observers** | |
+-------------------------------------+--------------------------------------------------------+
| ``actor_addr address()`` | Returns the address of this actor. |
+-------------------------------------+--------------------------------------------------------+
| ``actor_system& system()`` | Returns ``context()->system()``. |
+-------------------------------------+--------------------------------------------------------+
| ``actor_system& home_system()`` | Returns the system that spawned this actor. |
+-------------------------------------+--------------------------------------------------------+
| ``execution_unit* context()`` | Returns underlying thread or current scheduler worker. |
+-------------------------------------+--------------------------------------------------------+
| | |
+-------------------------------------+--------------------------------------------------------+
| **Customization Points** | |
+-------------------------------------+--------------------------------------------------------+
| ``on_exit()`` | Can be overridden to perform cleanup code. |
+-------------------------------------+--------------------------------------------------------+
| ``const char* name()`` | Returns a debug name for this actor type. |
+-------------------------------------+--------------------------------------------------------+
| | |
+-------------------------------------+--------------------------------------------------------+
| **Actor Management** | |
+-------------------------------------+--------------------------------------------------------+
| ``link_to(other)`` | Links to ``other`` (see :ref:`link`). |
+-------------------------------------+--------------------------------------------------------+
| ``unlink_from(other)`` | Remove the link to ``other``. |
+-------------------------------------+--------------------------------------------------------+
| ``monitor(other)`` | Adds a monitor to ``other`` (see :ref:`monitor`). |
+-------------------------------------+--------------------------------------------------------+
| ``demonitor(other)`` | Removes a monitor from ``whom``. |
+-------------------------------------+--------------------------------------------------------+
| ``spawn(F fun, xs...)`` | Spawns a new actor from ``fun``. |
+-------------------------------------+--------------------------------------------------------+
| ``spawn<T>(xs...)`` | Spawns a new actor of type ``T``. |
+-------------------------------------+--------------------------------------------------------+
| | |
+-------------------------------------+--------------------------------------------------------+
| **Message Processing** | |
+-------------------------------------+--------------------------------------------------------+
| ``T make_response_promise<Ts...>()``| Allows an actor to delay its response message. |
+-------------------------------------+--------------------------------------------------------+
| ``T response(xs...)`` | Convenience function for creating fulfilled promises. |
+-------------------------------------+--------------------------------------------------------+
Class ``scheduled_actor``
~~~~~~~~~~~~~~~~~~~~~~~~~
All scheduled actors inherit from ``scheduled_actor``. This includes
statically and dynamically typed event-based actors as well as brokers
:ref:`broker`.
+-------------------------------+--------------------------------------------------------------------------+
| **Types** | |
+-------------------------------+--------------------------------------------------------------------------+
| ``pointer`` | ``scheduled_actor*`` |
+-------------------------------+--------------------------------------------------------------------------+
| ``exception_handler`` | ``function<error (pointer, std::exception_ptr&)>`` |
+-------------------------------+--------------------------------------------------------------------------+
| ``default_handler`` | ``function<result<message> (pointer, message_view&)>`` |
+-------------------------------+--------------------------------------------------------------------------+
| ``error_handler`` | ``function<void (pointer, error&)>`` |
+-------------------------------+--------------------------------------------------------------------------+
| ``down_handler`` | ``function<void (pointer, down_msg&)>`` |
+-------------------------------+--------------------------------------------------------------------------+
| ``exit_handler`` | ``function<void (pointer, exit_msg&)>`` |
+-------------------------------+--------------------------------------------------------------------------+
| | |
+-------------------------------+--------------------------------------------------------------------------+
| **Constructors** | |
+-------------------------------+--------------------------------------------------------------------------+
| ``(actor_config&)`` | Constructs the actor using a config. |
+-------------------------------+--------------------------------------------------------------------------+
| | |
+-------------------------------+--------------------------------------------------------------------------+
| **Termination** | |
+-------------------------------+--------------------------------------------------------------------------+
| ``quit()`` | Stops this actor with normal exit reason. |
+-------------------------------+--------------------------------------------------------------------------+
| ``quit(error x)`` | Stops this actor with error ``x``. |
+-------------------------------+--------------------------------------------------------------------------+
| | |
+-------------------------------+--------------------------------------------------------------------------+
| **Special-purpose Handlers** | |
+-------------------------------+--------------------------------------------------------------------------+
| ``set_exception_handler(F f)``| Installs ``f`` for converting exceptions to errors (see :ref:`error`). |
+-------------------------------+--------------------------------------------------------------------------+
| ``set_down_handler(F f)`` | Installs ``f`` to handle down messages (see :ref:`down-message`). |
+-------------------------------+--------------------------------------------------------------------------+
| ``set_exit_handler(F f)`` | Installs ``f`` to handle exit messages (see :ref:`exit-message`). |
+-------------------------------+--------------------------------------------------------------------------+
| ``set_error_handler(F f)`` | Installs ``f`` to handle error messages (see :ref:`error-message`). |
+-------------------------------+--------------------------------------------------------------------------+
| ``set_default_handler(F f)`` | Installs ``f`` as fallback message handler (see :ref:`default-handler`). |
+-------------------------------+--------------------------------------------------------------------------+
Class ``blocking_actor``
~~~~~~~~~~~~~~~~~~~~~~~~
A blocking actor always lives in its own thread of execution. They are not as
lightweight as event-based actors and thus do not scale up to large numbers.
The primary use case for blocking actors is to use a ``scoped_actor``
for ad-hoc communication to selected actors. Unlike scheduled actors, CAF does
**not** dispatch system messages to special-purpose handlers. A blocking
actors receives *all* messages regularly through its mailbox. A blocking
actor is considered *done* only after it returned from ``act`` (or
from the implementation in function-based actors). A ``scoped_actor``
sends its exit messages as part of its destruction.
+----------------------------------+---------------------------------------------------+
| **Constructors** | |
+----------------------------------+---------------------------------------------------+
| ``(actor_config&)`` | Constructs the actor using a config. |
+----------------------------------+---------------------------------------------------+
| | |
+----------------------------------+---------------------------------------------------+
| **Customization Points** | |
+----------------------------------+---------------------------------------------------+
| ``void act()`` | Implements the behavior of the actor. |
+----------------------------------+---------------------------------------------------+
| | |
+----------------------------------+---------------------------------------------------+
| **Termination** | |
+----------------------------------+---------------------------------------------------+
| ``const error& fail_state()`` | Returns the current exit reason. |
+----------------------------------+---------------------------------------------------+
| ``fail_state(error x)`` | Sets the current exit reason. |
+----------------------------------+---------------------------------------------------+
| | |
+----------------------------------+---------------------------------------------------+
| **Actor Management** | |
+----------------------------------+---------------------------------------------------+
| ``wait_for(Ts... xs)`` | Blocks until all actors ``xs...`` are done. |
+----------------------------------+---------------------------------------------------+
| ``await_all_other_actors_done()``| Blocks until all other actors are done. |
+----------------------------------+---------------------------------------------------+
| | |
+----------------------------------+---------------------------------------------------+
| **Message Handling** | |
+----------------------------------+---------------------------------------------------+
| ``receive(Ts... xs)`` | Receives a message using the callbacks ``xs...``. |
+----------------------------------+---------------------------------------------------+
| ``receive_for(T& begin, T end)`` | See receive-loop_. |
+----------------------------------+---------------------------------------------------+
| ``receive_while(F stmt)`` | See receive-loop_. |
+----------------------------------+---------------------------------------------------+
| ``do_receive(Ts... xs)`` | See receive-loop_. |
+----------------------------------+---------------------------------------------------+
.. _interface:
Messaging Interfaces
--------------------
Statically typed actors require abstract messaging interfaces to allow the
compiler to type-check actor communication. Interfaces in CAF are defined using
the variadic template ``typed_actor<...>``, which defines the proper
actor handle at the same time. Each template parameter defines one
``input/output`` pair via
``replies_to<X1,...,Xn>::with<Y1,...,Yn>``. For inputs that do not
generate outputs, ``reacts_to<X1,...,Xn>`` can be used as shortcut for
``replies_to<X1,...,Xn>::with<void>``. In the same way functions cannot
be overloaded only by their return type, interfaces cannot accept one input
twice (possibly mapping it to different outputs). The example below defines a
messaging interface for a simple calculator.
.. literalinclude:: /examples/message_passing/calculator.cpp
:language: C++
:lines: 17-18
It is not required to create a type alias such as ``calculator_actor``,
but it makes dealing with statically typed actors much easier. Also, a central
alias definition eases refactoring later on.
Interfaces have set semantics. This means the following two type aliases
``i1`` and ``i2`` are equal:
.. code-block:: C++
using i1 = typed_actor<replies_to<A>::with<B>, replies_to<C>::with<D>>;
using i2 = typed_actor<replies_to<C>::with<D>, replies_to<A>::with<B>>;
Further, actor handles of type ``A`` are assignable to handles of type
``B`` as long as ``B`` is a subset of ``A``.
For convenience, the class ``typed_actor<...>`` defines the member
types shown below to grant access to derived types.
+------------------------+---------------------------------------------------------------+
| **Types** | |
+------------------------+---------------------------------------------------------------+
| ``behavior_type`` | A statically typed set of message handlers. |
+------------------------+---------------------------------------------------------------+
| ``base`` | Base type for actors, i.e., ``typed_event_based_actor<...>``. |
+------------------------+---------------------------------------------------------------+
| ``pointer`` | A pointer of type ``base*``. |
+------------------------+---------------------------------------------------------------+
| ``stateful_base<T>`` | See stateful-actor_. |
+------------------------+---------------------------------------------------------------+
| ``stateful_pointer<T>``| A pointer of type ``stateful_base<T>*``. |
+------------------------+---------------------------------------------------------------+
| ``extend<Ts...>`` | Extend this typed actor with ``Ts...``. |
+------------------------+---------------------------------------------------------------+
| ``extend_with<Other>`` | Extend this typed actor with all cases from ``Other``. |
+------------------------+---------------------------------------------------------------+
.. _spawn:
Spawning Actors
---------------
Both statically and dynamically typed actors are spawned from an
``actor_system`` using the member function ``spawn``. The
function either takes a function as first argument or a class as first template
parameter. For example, the following functions and classes represent actors.
.. literalinclude:: /examples/message_passing/calculator.cpp
:language: C++
:lines: 21-26
Spawning an actor for each implementation is illustrated below.
.. literalinclude:: /examples/message_passing/calculator.cpp
:language: C++
:lines: 123-128
Additional arguments to ``spawn`` are passed to the constructor of a
class or used as additional function arguments, respectively. In the example
above, none of the three functions takes any argument other than the implicit
but optional ``self`` pointer.
.. _function-based:
Function-based Actors
---------------------
When using a function or function object to implement an actor, the first
argument *can* be used to capture a pointer to the actor itself. The type
of this pointer is usually ``event_based_actor*`` or
``blocking_actor*``. The proper pointer type for any
``typed_actor`` handle ``T`` can be obtained via
``T::pointer`` interface_.
Blocking actors simply implement their behavior in the function body. The actor
is done once it returns from that function.
Event-based actors can either return a ``behavior`` (see :ref:`message-handler`)
that is used to initialize the actor or explicitly set the initial behavior by
calling ``self->become(...)``. Due to the asynchronous, event-based nature of
this kind of actor, the function usually returns immediately after setting a
behavior (message handler) for the *next* incoming message. Hence, variables on
the stack will be out of scope once a message arrives. Managing state in
function-based actors can be done either via rebinding state with ``become``,
using heap-located data referenced via ``std::shared_ptr`` or by using the
*stateful actor* abstraction (see :ref:`stateful-actor`).
The following three functions implement the prototypes shown in spawn_ and
illustrate one blocking actor and two event-based actors (statically and
dynamically typed).
.. literalinclude:: /examples/message_passing/calculator.cpp
:language: C++
:lines: 28-56
.. _class-based:
Class-based Actors
------------------
Implementing an actor using a class requires the following:
* Provide a constructor taking a reference of type ``actor_config&`` as first argument, which is forwarded to the base class. The config is passed implicitly to the constructor when calling ``spawn``, which also forwards any number of additional arguments to the constructor.
* Override ``make_behavior`` for event-based actors and ``act`` for blocking actors.
Implementing actors with classes works for all kinds of actors and allows
simple management of state via member variables. However, composing states via
inheritance can get quite tedious. For dynamically typed actors, composing
states is particularly hard, because the compiler cannot provide much help. For
statically typed actors, CAF also provides an API for composable
behaviors composable-behavior_ that works well with inheritance. The
following three examples implement the forward declarations shown in
spawn_.
.. literalinclude:: /examples/message_passing/calculator.cpp
:language: C++
:lines: 58-92
.. _stateful-actor:
Stateful Actors
---------------
The stateful actor API makes it easy to maintain state in function-based
actors. It is also safer than putting state in member variables, because the
state ceases to exist after an actor is done and is not delayed until the
destructor runs. For example, if two actors hold a reference to each other via
member variables, they produce a cycle and neither will get destroyed. Using
stateful actors instead breaks the cycle, because references are destroyed when
an actor calls ``self->quit()`` (or is killed externally). The
following example illustrates how to implement stateful actors with static
typing as well as with dynamic typing.
.. literalinclude:: /examples/message_passing/cell.cpp
:language: C++
:lines: 18-44
Stateful actors are spawned in the same way as any other function-based actor
function-based_.
.. literalinclude:: /examples/message_passing/cell.cpp
:language: C++
:lines: 49-50
.. _composable-behavior:
Actors from Composable Behaviors :sup:`experimental`
-----------------------------------------------------
When building larger systems, it is often useful to implement the behavior of
an actor in terms of other, existing behaviors. The composable behaviors in
CAF allow developers to generate a behavior class from a messaging
interface interface_.
The base type for composable behaviors is ``composable_behavior<T>``,
where ``T`` is a ``typed_actor<...>``. CAF maps each
``replies_to<A,B,C>::with<D,E,F>`` in ``T`` to a pure virtual
member function with signature:
.. code-block:: C++
result<D, E, F> operator()(param<A>, param<B>, param<C>);.
Note that ``operator()`` will take integral types as well as atom constants
simply by value. A ``result<T>`` accepts either a value of type ``T``, a
``skip_t`` (see :ref:`default-handler`), an ``error`` (see :ref:`error`), a
``delegated<T>`` (see :ref:`delegate`), or a ``response_promise<T>`` (see
:ref:`promise`). A ``result<void>`` is constructed by returning ``unit``.
A behavior that combines the behaviors ``X``, ``Y``, and
``Z`` must inherit from ``composed_behavior<X,Y,Z>`` instead of
inheriting from the three classes directly. The class
``composed_behavior`` ensures that the behaviors are concatenated
correctly. In case one message handler is defined in multiple base types, the
*first* type in declaration order wins. For example, if ``X`` and
``Y`` both implement the interface
``replies_to<int,int>::with<int>``, only the handler implemented in
``X`` is active.
Any composable (or composed) behavior with no pure virtual member functions can
be spawned directly through an actor system by calling
``system.spawn<...>()``, as shown below.
.. literalinclude:: /examples/composition/calculator_behavior.cpp
:language: C++
:lines: 20-45
The second example illustrates how to use non-primitive values that are wrapped
in a ``param<T>`` when working with composable behaviors. The purpose
of ``param<T>`` is to provide a single interface for both constant and
non-constant access. Constant access is modeled with the implicit conversion
operator to a const reference, the member function ``get()``, and
``operator->``.
When acquiring mutable access to the represented value, CAF copies the value
before allowing mutable access to it if more than one reference to the value
exists. This copy-on-write optimization avoids race conditions by design, while
minimizing copy operations (see :ref:`copy-on-write`). A mutable reference is
returned from the member functions ``get_mutable()`` and ``move()``. The latter
is a convenience function for ``std::move(x.get_mutable())``. The following
example illustrates how to use ``param<std::string>`` when implementing a simple
dictionary.
.. literalinclude:: /examples/composition/dictionary_behavior.cpp
:language: C++
:lines: 22-44
.. _attach:
Attaching Cleanup Code to Actors
--------------------------------
Users can attach cleanup code to actors. This code is executed immediately if
the actor has already exited. Otherwise, the actor will execute it as part of
its termination. The following example attaches a function object to actors for
printing a custom string on exit.
.. literalinclude:: /examples/broker/simple_broker.cpp
:language: C++
:lines: 42-47
It is possible to attach code to remote actors. However, the cleanup code will
run on the local machine.
.. _blocking-actor:
Blocking Actors
---------------
Blocking actors always run in a separate thread and are not scheduled by CAF.
Unlike event-based actors, blocking actors have explicit, blocking *receive*
functions. Further, blocking actors do not handle system messages automatically
via special-purpose callbacks (see :ref:`special-handler`). This gives users
full control over the behavior of blocking actors. However, blocking actors
still should follow conventions of the actor system. For example, actors should
unconditionally terminate after receiving an ``exit_msg`` with reason
``exit_reason::kill``.
Receiving Messages
~~~~~~~~~~~~~~~~~~
The function ``receive`` sequentially iterates over all elements in the
mailbox beginning with the first. It takes a message handler that is applied to
the elements in the mailbox until an element was matched by the handler. An
actor calling ``receive`` is blocked until it successfully dequeued a
message from its mailbox or an optional timeout occurs. Messages that are not
matched by the behavior are automatically skipped and remain in the mailbox.
.. code-block:: C++
self->receive (
[](int x) { /* ... */ }
);
.. _catch-all:
Catch-all Receive Statements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Blocking actors can use inline catch-all callbacks instead of setting a default
handler (see :ref:`default-handler`). A catch-all case must be the last callback
before the optional timeout, as shown in the example below.
.. code-block:: C++
self->receive(
[&](float x) {
// ...
},
[&](const down_msg& x) {
// ...
},
[&](const exit_msg& x) {
// ...
},
others >> [](message_view& x) -> result<message> {
// report unexpected message back to client
return sec::unexpected_message;
}
);
.. _receive-loop:
Receive Loops
~~~~~~~~~~~~~
Message handler passed to ``receive`` are temporary object at runtime.
Hence, calling ``receive`` inside a loop creates an unnecessary amount
of short-lived objects. CAF provides predefined receive loops to allow for
more efficient code.
.. code-block:: C++
// BAD
std::vector<int> results;
for (size_t i = 0; i < 10; ++i)
receive (
[&](int value) {
results.push_back(value);
}
);
// GOOD
std::vector<int> results;
size_t i = 0;
receive_for(i, 10) (
[&](int value) {
results.push_back(value);
}
);
.. code-block:: C++
// BAD
size_t received = 0;
while (received < 10) {
receive (
[&](int) {
++received;
}
);
} ;
// GOOD
size_t received = 0;
receive_while([&] { return received < 10; }) (
[&](int) {
++received;
}
);
.. code-block:: C++
// BAD
size_t received = 0;
do {
receive (
[&](int) {
++received;
}
);
} while (received < 10);
// GOOD
size_t received = 0;
do_receive (
[&](int) {
++received;
}
).until([&] { return received >= 10; });
The examples above illustrate the correct usage of the three loops
``receive_for``, ``receive_while`` and
``do_receive(...).until``. It is possible to nest receives and receive
loops.
.. code-block:: C++
bool running = true;
self->receive_while([&] { return running; }) (
[&](int value1) {
self->receive (
[&](float value2) {
aout(self) << value1 << " => " << value2 << endl;
}
);
},
// ...
);
.. _scoped-actors:
Scoped Actors
~~~~~~~~~~~~~
The class ``scoped_actor`` offers a simple way of communicating with
CAF actors from non-actor contexts. It overloads ``operator->`` to
return a ``blocking_actor*``. Hence, it behaves like the implicit
``self`` pointer in functor-based actors, only that it ceases to exist
at scope end.
.. code-block:: C++
void test(actor_system& system) {
scoped_actor self{system};
// spawn some actor
auto aut = self->spawn(my_actor_impl);
self->send(aut, "hi there");
// self will be destroyed automatically here; any
// actor monitoring it will receive down messages etc.
}
.. _broker:
Network I/O with Brokers
========================
When communicating to other services in the network, sometimes low-level socket
I/O is inevitable. For this reason, CAF provides *brokers*. A broker is
an event-based actor running in the middleman that multiplexes socket I/O. It
can maintain any number of acceptors and connections. Since the broker runs in
the middleman, implementations should be careful to consume as little time as
possible in message handlers. Brokers should outsource any considerable amount
of work by spawning new actors or maintaining worker actors.
*Note that all UDP-related functionality is still experimental.*
Spawning Brokers
----------------
Brokers are implemented as functions and are spawned by calling on of the three
following member functions of the middleman.
.. code-block:: C++
template <spawn_options Os = no_spawn_options,
class F = std::function<void(broker*)>, class... Ts>
typename infer_handle_from_fun<F>::type
spawn_broker(F fun, Ts&&... xs);
template <spawn_options Os = no_spawn_options,
class F = std::function<void(broker*)>, class... Ts>
expected<typename infer_handle_from_fun<F>::type>
spawn_client(F fun, const std::string& host, uint16_t port, Ts&&... xs);
template <spawn_options Os = no_spawn_options,
class F = std::function<void(broker*)>, class... Ts>
expected<typename infer_handle_from_fun<F>::type>
spawn_server(F fun, uint16_t port, Ts&&... xs);
The function ``spawn_broker`` simply spawns a broker. The convenience
function ``spawn_client`` tries to connect to given host and port over
TCP and returns a broker managing this connection on success. Finally,
``spawn_server`` opens a local TCP port and spawns a broker managing it
on success. There are no convenience functions spawn a UDP-based client or
server.
.. _broker-class:
Class ``broker``
----------------
.. code-block:: C++
void configure_read(connection_handle hdl, receive_policy::config config);
Modifies the receive policy for the connection identified by ``hdl``.
This will cause the middleman to enqueue the next ``new_data_msg``
according to the given ``config`` created by
``receive_policy::exactly(x)``, ``receive_policy::at_most(x)``,
or ``receive_policy::at_least(x)`` (with ``x`` denoting the
number of bytes).
.. code-block:: C++
void write(connection_handle hdl, size_t num_bytes, const void* buf)
void write(datagram_handle hdl, size_t num_bytes, const void* buf)
Writes data to the output buffer.
.. code-block:: C++
void enqueue_datagram(datagram_handle hdl, std::vector<char> buf);
Enqueues a buffer to be sent as a datagram. Use of this function is encouraged
over write as it allows reuse of the buffer which can be returned to the broker
in a ``datagram_sent_msg``.
.. code-block:: C++
void flush(connection_handle hdl);
void flush(datagram_handle hdl);
Sends the data from the output buffer.
.. code-block:: C++
template <class F, class... Ts>
actor fork(F fun, connection_handle hdl, Ts&&... xs);
Spawns a new broker that takes ownership of a given connection.
.. code-block:: C++
size_t num_connections();
Returns the number of open connections.
.. code-block:: C++
void close(connection_handle hdl);
void close(accept_handle hdl);
void close(datagram_handle hdl);
Closes the endpoint related to the handle.
.. code-block:: C++
expected<std::pair<accept_handle, uint16_t>>
add_tcp_doorman(uint16_t port = 0, const char* in = nullptr,
bool reuse_addr = false);
Creates new doorman that accepts incoming connections on a given port and
returns the handle to the doorman and the port in use or an error.
.. code-block:: C++
expected<connection_handle>
add_tcp_scribe(const std::string& host, uint16_t port);
Creates a new scribe to connect to host:port and returns handle to it or an
error.
.. code-block:: C++
expected<std::pair<datagram_handle, uint16_t>>
add_udp_datagram_servant(uint16_t port = 0, const char* in = nullptr,
bool reuse_addr = false);
Creates a datagram servant to handle incoming datagrams on a given port.
Returns the handle to the servant and the port in use or an error.
.. code-block:: C++
expected<datagram_handle>
add_udp_datagram_servant(const std::string& host, uint16_t port);
Creates a datagram servant to send datagrams to host:port and returns a handle
to it or an error.
Broker-related Message Types
----------------------------
Brokers receive system messages directly from the middleman for connection and
acceptor events.
**Note:** brokers are *required* to handle these messages immediately
regardless of their current state. Not handling the system messages from the
broker results in loss of data, because system messages are *not*
delivered through the mailbox and thus cannot be skipped.
.. code-block:: C++
struct new_connection_msg {
accept_handle source;
connection_handle handle;
};
Indicates that ``source`` accepted a new TCP connection identified by
``handle``.
.. code-block:: C++
struct new_data_msg {
connection_handle handle;
std::vector<char> buf;
};
Contains raw bytes received from ``handle``. The amount of data
received per event is controlled with ``configure_read`` (see
broker-class_). It is worth mentioning that the buffer is re-used whenever
possible.
.. code-block:: C++
struct data_transferred_msg {
connection_handle handle;
uint64_t written;
uint64_t remaining;
};
This message informs the broker that the ``handle`` sent
``written`` bytes with ``remaining`` bytes in the buffer. Note,
that these messages are not sent per default but must be explicitly enabled via
the member function ``ack_writes``.
.. code-block:: C++
struct connection_closed_msg {
connection_handle handle;
};
struct acceptor_closed_msg {
accept_handle handle;
};
A ``connection_closed_msg`` or ``acceptor_closed_msg`` informs
the broker that one of its handles is no longer valid.
.. code-block:: C++
struct connection_passivated_msg {
connection_handle handle;
};
struct acceptor_passivated_msg {
accept_handle handle;
};
A ``connection_passivated_msg`` or ``acceptor_passivated_msg``
informs the broker that one of its handles entered passive mode and no longer
accepts new data or connections trigger_.
The following messages are related to UDP communication (see
:ref:`transport-protocols`). Since UDP is not connection oriented, there is no
equivalent to the ``new_connection_msg`` of TCP.
.. code-block:: C++
struct new_datagram_msg {
datagram_handle handle;
network::receive_buffer buf;
};
Contains the raw bytes from ``handle``. The buffer always has a maximum
size of 65k to receive all regular UDP messages. The amount of bytes can be
queried via the ``.size()`` member function. Similar to TCP, the buffer
is reused when possible---please do not resize it.
.. code-block:: C++
struct datagram_sent_msg {
datagram_handle handle;
uint64_t written;
std::vector<char> buf;
};
This message informs the broker that the ``handle`` sent a datagram of
``written`` bytes. It includes the buffer that held the sent message to
allow its reuse. Note, that these messages are not sent per default but must be
explicitly enabled via the member function ``ack_writes``.
.. code-block:: C++
struct datagram_servant_closed_msg {
std::vector<datagram_handle> handles;
};
A ``datagram_servant_closed_msg`` informs the broker that one of its
handles is no longer valid.
.. code-block:: C++
struct datagram_servant_passivated_msg {
datagram_handle handle;
};
A ``datagram_servant_closed_msg`` informs the broker that one of its
handles entered passive mode and no longer accepts new data trigger_.
.. _trigger:
Manually Triggering Events :sup:`experimental`
-----------------------------------------------
Brokers receive new events as ``new_connection_msg`` and
``new_data_msg`` as soon and as often as they occur, per default. This
means a fast peer can overwhelm a broker by sending it data faster than the
broker can process it. In particular if the broker outsources work items to
other actors, because work items can accumulate in the mailboxes of the
workers.
Calling ``self->trigger(x,y)``, where ``x`` is a connection or
acceptor handle and ``y`` is a positive integer, allows brokers to halt
activities after ``y`` additional events. Once a connection or acceptor
stops accepting new data or connections, the broker receives a
``connection_passivated_msg`` or ``acceptor_passivated_msg``.
Brokers can stop activities unconditionally with ``self->halt(x)`` and
resume activities unconditionally with ``self->trigger(x)``.
\section{Common Pitfalls} .. _pitfalls:
\label{pitfalls}
Common Pitfalls
===============
This Section highlights common mistakes or C++ subtleties that can show up when This Section highlights common mistakes or C++ subtleties that can show up when
programming in CAF. programming in CAF.
\subsection{Defining Message Handlers} Defining Message Handlers
-------------------------
C++ evaluates comma-separated expressions from left-to-right, using only the C++ evaluates comma-separated expressions from left-to-right, using only the
last element as return type of the whole expression. This means that message last element as return type of the whole expression. This means that message
handlers and behaviors must \emph{not} be initialized like this: handlers and behaviors must *not* be initialized like this:
.. code-block:: C++
\begin{lstlisting} message_handler wrong = (
message_handler wrong = (
[](int i) { /*...*/ }, [](int i) { /*...*/ },
[](float f) { /*...*/ } [](float f) { /*...*/ }
); );
\end{lstlisting}
The correct way to initialize message handlers and behaviors is to either The correct way to initialize message handlers and behaviors is to either
use the constructor or the member function \lstinline^assign^: use the constructor or the member function ``assign``:
.. code-block:: C++
\begin{lstlisting} message_handler ok1{
message_handler ok1{
[](int i) { /*...*/ }, [](int i) { /*...*/ },
[](float f) { /*...*/ } [](float f) { /*...*/ }
}; };
message_handler ok2; message_handler ok2;
// some place later // some place later
ok2.assign( ok2.assign(
[](int i) { /*...*/ }, [](int i) { /*...*/ },
[](float f) { /*...*/ } [](float f) { /*...*/ }
); );
\end{lstlisting}
\subsection{Event-Based API} Event-Based API
---------------
The member function \lstinline^become^ does not block, i.e., always returns The member function ``become`` does not block, i.e., always returns
immediately. Thus, lambda expressions should \textit{always} capture by value. immediately. Thus, lambda expressions should *always* capture by value.
Otherwise, all references on the stack will cause undefined behavior if the Otherwise, all references on the stack will cause undefined behavior if the
lambda expression is executed. lambda expression is executed.
\subsection{Requests} Requests
--------
A handle returned by \lstinline^request^ represents \emph{exactly one} response A handle returned by ``request`` represents *exactly one* response
message. It is not possible to receive more than one response message. message. It is not possible to receive more than one response message.
The handle returned by \lstinline^request^ is bound to the calling actor. It is The handle returned by ``request`` is bound to the calling actor. It is
not possible to transfer a handle to a response to another actor. not possible to transfer a handle to a response to another actor.
\clearpage Sharing
\subsection{Sharing} -------
It is strongly recommended to \textbf{not} share states between actors. In It is strongly recommended to **not** share states between actors. In
particular, no actor shall ever access member variables or member functions of particular, no actor shall ever access member variables or member functions of
another actor. Accessing shared memory segments concurrently can cause undefined another actor. Accessing shared memory segments concurrently can cause undefined
behavior that is incredibly hard to find and debug. However, sharing behavior that is incredibly hard to find and debug. However, sharing
\textit{data} between actors is fine, as long as the data is \textit{immutable} *data* between actors is fine, as long as the data is *immutable*
and its lifetime is guaranteed to outlive all actors. The simplest way to meet and its lifetime is guaranteed to outlive all actors. The simplest way to meet
the lifetime guarantee is by storing the data in smart pointers such as the lifetime guarantee is by storing the data in smart pointers such as
\lstinline^std::shared_ptr^. Nevertheless, the recommended way of sharing ``std::shared_ptr``. Nevertheless, the recommended way of sharing
information is message passing. Sending the same message to multiple actors information is message passing. Sending the same message to multiple actors
does not result in copying the data several times. does not result in copying the data several times.
.. _system-config:
Configuring Actor Applications
==============================
CAF configures applications at startup using an
``actor_system_config`` or a user-defined subclass of that type. The
config objects allow users to add custom types, to load modules, and to
fine-tune the behavior of loaded modules with command line options or
configuration files system-config-options_.
The following code example is a minimal CAF application with a :ref:`middleman`
but without any custom configuration options.
.. code-block:: C++
void caf_main(actor_system& system) {
// ...
}
CAF_MAIN(io::middleman)
The compiler expands this example code to the following.
.. code-block:: C++
void caf_main(actor_system& system) {
// ...
}
int main(int argc, char** argv) {
return exec_main<io::middleman>(caf_main, argc, argv);
}
The function ``exec_main`` creates a config object, loads all modules
requested in ``CAF_MAIN`` and then calls ``caf_main``. A
minimal implementation for ``main`` performing all these steps manually
is shown in the next example for the sake of completeness.
.. code-block:: C++
int main(int argc, char** argv) {
actor_system_config cfg;
// read CLI options
cfg.parse(argc, argv);
// return immediately if a help text was printed
if (cfg.cli_helptext_printed)
return 0;
// load modules
cfg.load<io::middleman>();
// create actor system and call caf_main
actor_system system{cfg};
caf_main(system);
}
However, setting up config objects by hand is usually not necessary. CAF
automatically selects user-defined subclasses of
``actor_system_config`` if ``caf_main`` takes a second
parameter by reference, as shown in the minimal example below.
.. code-block:: C++
class my_config : public actor_system_config {
public:
my_config() {
// ...
}
};
void caf_main(actor_system& system, const my_config& cfg) {
// ...
}
CAF_MAIN()
Users can perform additional initialization, add custom program options, etc.
simply by implementing a default constructor.
.. _system-config-module:
Loading Modules
---------------
The simplest way to load modules is to use the macro ``CAF_MAIN`` and
to pass a list of all requested modules, as shown below.
.. code-block:: C++
void caf_main(actor_system& system) {
// ...
}
CAF_MAIN(mod1, mod2, ...)
Alternatively, users can load modules in user-defined config classes.
.. code-block:: C++
class my_config : public actor_system_config {
public:
my_config() {
load<mod1>();
load<mod2>();
// ...
}
};
The third option is to simply call ``x.load<mod1>()`` on a config
object *before* initializing an actor system with it.
.. _system-config-options:
Command Line Options and INI Configuration Files
------------------------------------------------
CAF organizes program options in categories and parses CLI arguments as well as
INI files. CLI arguments override values in the INI file which override
hard-coded defaults. Users can add any number of custom program options by
implementing a subtype of ``actor_system_config``. The example below
adds three options to the ``global`` category.
.. literalinclude:: /examples/remoting/distributed_calculator.cpp
:language: C++
:lines: 206-218
We create a new ``global`` category in ``custom_options_``.
Each following call to ``add`` then appends individual options to the
category. The first argument to ``add`` is the associated variable. The
second argument is the name for the parameter, optionally suffixed with a
comma-separated single-character short name. The short name is only considered
for CLI parsing and allows users to abbreviate commonly used option names. The
third and final argument to ``add`` is a help text.
The custom ``config`` class allows end users to set the port for the application
to 42 with either ``-p 42`` (short name) or ``--port=42`` (long name). The long
option name is prefixed by the category when using a different category than
``global``. For example, adding the port option to the category ``foo`` means
end users have to type ``--foo.port=42`` when using the long name. Short names
are unaffected by the category, but have to be unique.
Boolean options do not require arguments. The member variable
``server_mode`` is set to ``true`` if the command line contains
either ``--server-mode`` or ``-s``.
The example uses member variables for capturing user-provided settings for
simplicity. However, this is not required. For example,
``add<bool>(...)`` allows omitting the first argument entirely. All
values of the configuration are accessible with ``get_or``. Note that
all global options can omit the ``"global."`` prefix.
CAF adds the program options ``help`` (with short names ``-h``
and ``-?``) as well as ``long-help`` to the ``global``
category.
The default name for the INI file is ``caf-application.ini``. Users can
change the file name and path by passing ``--config-file=<path>`` on the
command line.
INI files are organized in categories. No value is allowed outside of a category
(no implicit ``global`` category). The parses uses the following syntax:
+------------------------+-----------------------------+
| ``key=true`` | is a boolean |
+------------------------+-----------------------------+
| ``key=1`` | is an integer |
+------------------------+-----------------------------+
| ``key=1.0`` | is an floating point number |
+------------------------+-----------------------------+
| ``key=1ms`` | is an timespan |
+------------------------+-----------------------------+
| ``key='foo'`` | is an atom |
+------------------------+-----------------------------+
| ``key="foo"`` | is a string |
+------------------------+-----------------------------+
| ``key=[0, 1, ...]`` | is as a list |
+------------------------+-----------------------------+
| ``key={a=1, b=2, ...}``| is a dictionary (map) |
+------------------------+-----------------------------+
The following example INI file lists all standard options in CAF and their
default value. Note that some options such as ``scheduler.max-threads``
are usually detected at runtime and thus have no hard-coded default.
.. literalinclude:: /examples/caf-application.ini
:language: INI
.. _add-custom-message-type:
Adding Custom Message Types
---------------------------
CAF requires serialization support for all of its message types (see
:ref:`type-inspection`). However, CAF also needs a mapping of unique type names
to user-defined types at runtime. This is required to deserialize arbitrary
messages from the network.
As an introductory example, we (again) use the following POD type
``foo``.
.. literalinclude:: /examples/custom_type/custom_types_1.cpp
:language: C++
:lines: 24-27
To make ``foo`` serializable, we make it inspectable:
.. literalinclude:: /examples/custom_type/custom_types_1.cpp
:language: C++
:lines: 30-34
Finally, we give ``foo`` a platform-neutral name and add it to the list
of serializable types by using a custom config class.
.. literalinclude:: /examples/custom_type/custom_types_1.cpp
:language: C++
:lines: 75-78,81-84
Adding Custom Error Types
-------------------------
Adding a custom error type to the system is a convenience feature to allow
improve the string representation. Error types can be added by implementing a
render function and passing it to ``add_error_category``, as shown in
:ref:`custom-error`.
.. _add-custom-actor-type:
Adding Custom Actor Types :sup:`experimental`
----------------------------------------------
Adding actor types to the configuration allows users to spawn actors by their
name. In particular, this enables spawning of actors on a different node (see
:ref:`remote-spawn`). For our example configuration, we consider the following
simple ``calculator`` actor.
.. literalinclude:: /examples/remoting/remote_spawn.cpp
:language: C++
:lines: 33-34
Adding the calculator actor type to our config is achieved by calling
``add_actor_type<T>``. Note that adding an actor type in this way
implicitly calls ``add_message_type<T>`` for typed actors
add-custom-message-type_. This makes our ``calculator`` actor type
serializable and also enables remote nodes to spawn calculators anywhere in the
distributed actor system (assuming all nodes use the same config).
.. literalinclude:: /examples/remoting/remote_spawn.cpp
:language: C++
:lines: 98-109
Our final example illustrates how to spawn a ``calculator`` locally by
using its type name. Because the dynamic type name lookup can fail and the
construction arguments passed as message can mismatch, this version of
``spawn`` returns ``expected<T>``.
.. code-block:: C++
auto x = system.spawn<calculator>("calculator", make_message());
if (! x) {
std::cerr << "*** unable to spawn calculator: "
<< system.render(x.error()) << std::endl;
return;
}
calculator c = std::move(*x);
Adding dynamically typed actors to the config is achieved in the same way. When
spawning a dynamically typed actor in this way, the template parameter is
simply ``actor``. For example, spawning an actor "foo" which requires
one string is created with:
.. code-block:: C++
auto worker = system.spawn<actor>("foo", make_message("bar"));
Because constructor (or function) arguments for spawning the actor are stored
in a ``message``, only actors with appropriate input types are allowed.
For example, pointer types are illegal. Hence users need to replace C-strings
with ``std::string``.
.. _log-output:
Log Output
----------
Logging is disabled in CAF per default. It can be enabled by setting the
``--with-log-level=`` option of the ``configure`` script to one
of ``error``, ``warning``, ``info``, ``debug``,
or ``trace`` (from least output to most). Alternatively, setting the
CMake variable ``CAF_LOG_LEVEL`` to one of these values has the same
effect.
All logger-related configuration options listed here and in
system-config-options_ are silently ignored if logging is disabled.
.. _log-output-file-name:
File Name
~~~~~~~~~
The output file is generated from the template configured by
``logger-file-name``. This template supports the following variables.
+----------------+--------------------------------+
| **Variable** | **Output** |
+----------------+--------------------------------+
| ``[PID]`` | The OS-specific process ID. |
+----------------+--------------------------------+
| ``[TIMESTAMP]``| The UNIX timestamp on startup. |
+----------------+--------------------------------+
| ``[NODE]`` | The node ID of the CAF system. |
+----------------+--------------------------------+
.. _log-output-console:
Console
~~~~~~~
Console output is disabled per default. Setting ``logger-console`` to
either ``"uncolored"`` or ``"colored"`` prints log events to
``std::clog``. Using the ``"colored"`` option will print the
log events in different colors depending on the severity level.
.. _log-output-format-strings:
Format Strings
~~~~~~~~~~~~~~
CAF uses log4j-like format strings for configuring printing of individual
events via ``logger-file-format`` and
``logger-console-format``. Note that format modifiers are not supported
at the moment. The recognized field identifiers are:
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| **Character**| **Output** |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``c`` | The category/component. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``C`` | The full qualifier of the current function. For example, the qualifier of ``void ns::foo::bar()`` is printed as ``ns.foo``. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``d`` | The date in ISO 8601 format, i.e., ``"YYYY-MM-DDThh:mm:ss"``. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``F`` | The file name. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``L`` | The line number. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``m`` | The user-defined log message. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``M`` | The name of the current function. For example, the name of ``void ns::foo::bar()`` is printed as ``bar``. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``n`` | A newline. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``p`` | The priority (severity level). |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``r`` | Elapsed time since starting the application in milliseconds. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``t`` | ID of the current thread. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``a`` | ID of the current actor (or ``actor0`` when not logging inside an actor). |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| ``%`` | A single percent sign. |
+--------------+-----------------------------------------------------------------------------------------------------------------------------+
.. _log-output-filtering:
Filtering
~~~~~~~~~
The two configuration options ``logger-component-filter`` and
``logger-verbosity`` reduce the amount of generated log events. The
former is a list of excluded component names and the latter can increase the
reported severity level (but not decrease it beyond the level defined at
compile time).
.. _error:
Errors
======
Errors in CAF have a code and a category, similar to ``std::error_code`` and
``std::error_condition``. Unlike its counterparts from the C++ standard library,
``error`` is plattform-neutral and serializable. Instead of using category
singletons, CAF stores categories as atoms (see :ref:`atom`). Errors can also
include a message to provide additional context information.
Class Interface
---------------
+-----------------------------------------+--------------------------------------------------------------------+
| **Constructors** | |
+-----------------------------------------+--------------------------------------------------------------------+
| ``(Enum x)`` | Construct error by calling ``make_error(x)`` |
+-----------------------------------------+--------------------------------------------------------------------+
| ``(uint8_t x, atom_value y)`` | Construct error with code ``x`` and category ``y`` |
+-----------------------------------------+--------------------------------------------------------------------+
| ``(uint8_t x, atom_value y, message z)``| Construct error with code ``x``, category ``y``, and context ``z`` |
+-----------------------------------------+--------------------------------------------------------------------+
| | |
+-----------------------------------------+--------------------------------------------------------------------+
| **Observers** | |
+-----------------------------------------+--------------------------------------------------------------------+
| ``uint8_t code()`` | Returns the error code |
+-----------------------------------------+--------------------------------------------------------------------+
| ``atom_value category()`` | Returns the error category |
+-----------------------------------------+--------------------------------------------------------------------+
| ``message context()`` | Returns additional context information |
+-----------------------------------------+--------------------------------------------------------------------+
| ``explicit operator bool()`` | Returns ``code() != 0`` |
+-----------------------------------------+--------------------------------------------------------------------+
.. _custom-error:
Add Custom Error Categories
---------------------------
Adding custom error categories requires three steps: (1) declare an enum class
of type ``uint8_t`` with the first value starting at 1, (2) specialize
``error_category`` to give your type a custom ID (value 0-99 are
reserved by CAF), and (3) add your error category to the actor system config.
The following example adds custom error codes for math errors.
.. literalinclude:: /examples/message_passing/divider.cpp
:language: C++
:lines: 17-47
.. _sec:
System Error Codes
------------------
System Error Codes (SECs) use the error category ``"system"``. They
represent errors in the actor system or one of its modules and are defined as
follows.
.. literalinclude:: /libcaf_core/caf/sec.hpp
:language: C++
:lines: 32-117
.. _exit-reason:
Default Exit Reasons
--------------------
CAF uses the error category ``"exit"`` for default exit reasons. These errors
are usually fail states set by the actor system itself. The two exceptions are
``exit_reason::user_shutdown`` and ``exit_reason::kill``. The former is used in
CAF to signalize orderly, user-requested shutdown and can be used by programmers
in the same way. The latter terminates an actor unconditionally when used in
``send_exit``, even if the default handler for exit messages (see
:ref:`exit-message`) is overridden.
.. literalinclude:: /libcaf_core/caf/exit_reason.hpp
:language: C++
:lines: 29-49
.. _faq:
Frequently Asked Questions
==========================
This Section is a compilation of the most common questions via GitHub, chat,
and mailing list.
Can I Encrypt CAF Communication?
--------------------------------
Yes, by using the OpenSSL module (see :ref:`free-remoting-functions`).
Can I Create Messages Dynamically?
----------------------------------
Yes.
Usually, messages are created implicitly when sending messages but can also be
created explicitly using ``make_message``. In both cases, types and number of
elements are known at compile time. To allow for fully dynamic message
generation, CAF also offers ``message_builder``:
.. code-block:: C++
message_builder mb;
// prefix message with some atom
mb.append(strings_atom::value);
// fill message with some strings
std::vector<std::string> strings{/*...*/};
for (auto& str : strings)
mb.append(str);
// create the message
message msg = mb.to_message();
What Debugging Tools Exist?
---------------------------
The ``scripts/`` and ``tools/`` directories contain some useful tools to aid in
development and debugging.
``scripts/atom.py`` converts integer atom values back into strings.
``scripts/demystify.py`` replaces cryptic ``typed_mpi<...>``
templates with ``replies_to<...>::with<...>`` and
``atom_constant<...>`` with a human-readable representation of the
actual atom.
``scripts/caf-prof`` is an R script that generates plots from CAF
profiler output.
``caf-vec`` is a (highly) experimental tool that annotates CAF logs
with vector timestamps. It gives you happens-before relations and a nice
visualization via `ShiViz <https://bestchai.bitbucket.io/shiviz/>`_.
\section{Group Communication} .. _groups:
\label{groups}
Group Communication
===================
CAF supports publish/subscribe-based group communication. Dynamically typed CAF supports publish/subscribe-based group communication. Dynamically typed
actors can join and leave groups and send messages to groups. The following actors can join and leave groups and send messages to groups. The following
example showcases the basic API for retrieving a group from a module by its example showcases the basic API for retrieving a group from a module by its
name, joining, and leaving. name, joining, and leaving.
\begin{lstlisting} .. code-block:: C++
std::string module = "local";
std::string id = "foo"; std::string module = "local";
auto expected_grp = system.groups().get(module, id); std::string id = "foo";
if (! expected_grp) { auto expected_grp = system.groups().get(module, id);
if (! expected_grp) {
std::cerr << "*** cannot load group: " std::cerr << "*** cannot load group: "
<< system.render(expected_grp.error()) << std::endl; << system.render(expected_grp.error()) << std::endl;
return; return;
} }
auto grp = std::move(*expected_grp); auto grp = std::move(*expected_grp);
scoped_actor self{system}; scoped_actor self{system};
self->join(grp); self->join(grp);
self->send(grp, "test"); self->send(grp, "test");
self->receive( self->receive(
[](const std::string& str) { [](const std::string& str) {
assert(str == "test"); assert(str == "test");
} }
); );
self->leave(grp); self->leave(grp);
\end{lstlisting}
It is worth mentioning that the module \lstinline`"local"` is guaranteed to It is worth mentioning that the module ``"local"`` is guaranteed to
never return an error. The example above uses the general API for retrieving never return an error. The example above uses the general API for retrieving
the group. However, local modules can be easier accessed by calling the group. However, local modules can be easier accessed by calling
\lstinline`system.groups().get_local(id)`, which returns \lstinline`group` ``system.groups().get_local(id)``, which returns ``group``
instead of \lstinline`expected<group>`. instead of ``expected<group>``.
\subsection{Anonymous Groups} .. _anonymous-group:
\label{anonymous-group}
Groups created on-the-fly with \lstinline^system.groups().anonymous()^ can be Anonymous Groups
----------------
Groups created on-the-fly with ``system.groups().anonymous()`` can be
used to coordinate a set of workers. Each call to this function returns a new, used to coordinate a set of workers. Each call to this function returns a new,
unique group instance. unique group instance.
\subsection{Local Groups} .. _local-group:
\label{local-group}
Local Groups
------------
The \lstinline^"local"^ group module creates groups for in-process The ``"local"`` group module creates groups for in-process
communication. For example, a group for GUI related events could be identified communication. For example, a group for GUI related events could be identified
by \lstinline^system.groups().get_local("GUI events")^. The group ID by ``system.groups().get_local("GUI events")``. The group ID
\lstinline^"GUI events"^ uniquely identifies a singleton group instance of the ``"GUI events"`` uniquely identifies a singleton group instance of the
module \lstinline^"local"^. module ``"local"``.
.. _remote-group:
\subsection{Remote Groups} Remote Groups
\label{remote-group} -------------
Calling\lstinline^system.middleman().publish_local_groups(port, addr)^ makes Calling``system.middleman().publish_local_groups(port, addr)`` makes
all local groups available to other nodes in the network. The first argument all local groups available to other nodes in the network. The first argument
denotes the port, while the second (optional) parameter can be used to denotes the port, while the second (optional) parameter can be used to
whitelist IP addresses. whitelist IP addresses.
After publishing the group at one node (the server), other nodes (the clients) After publishing the group at one node (the server), other nodes (the clients)
can get a handle for that group by using the ``remote'' module: can get a handle for that group by using the ``remote`` module:
\lstinline^system.groups().get("remote", "<group>@<host>:<port>")^. This ``system.groups().get("remote", "<group>@<host>:<port>")``. This implementation
implementation uses N-times unicast underneath and the group is only available uses N-times unicast underneath and the group is only available as long as the
as long as the hosting server is alive. hosting server is alive.
\section{Introduction} Introduction
============
Before diving into the API of CAF, we discuss the concepts behind it and Before diving into the API of CAF, we discuss the concepts behind it and
explain the terminology used in this manual. explain the terminology used in this manual.
\subsection{Actor Model} Actor Model
-----------
The actor model describes concurrent entities---actors---that do not share The actor model describes concurrent entities---actors---that do not share
state and communicate only via asynchronous message passing. Decoupling state and communicate only via asynchronous message passing. Decoupling
...@@ -27,10 +29,11 @@ and from one node to many nodes. However, the actor model has not yet been ...@@ -27,10 +29,11 @@ and from one node to many nodes. However, the actor model has not yet been
widely adopted in the native programming domain. With CAF, we contribute a widely adopted in the native programming domain. With CAF, we contribute a
library for actor programming in C++ as open-source software to ease native library for actor programming in C++ as open-source software to ease native
development of concurrent as well as distributed systems. In this regard, CAF development of concurrent as well as distributed systems. In this regard, CAF
follows the C++ philosophy \textit{building the highest abstraction possible follows the C++ philosophy *building the highest abstraction possible
without sacrificing performance}. without sacrificing performance*.
\subsection{Terminology} Terminology
-----------
CAF is inspired by other implementations based on the actor model such as CAF is inspired by other implementations based on the actor model such as
Erlang or Akka. It aims to provide a modern C++ API allowing for type-safe as Erlang or Akka. It aims to provide a modern C++ API allowing for type-safe as
...@@ -38,7 +41,8 @@ well as dynamically typed messaging. While there are similarities to other ...@@ -38,7 +41,8 @@ well as dynamically typed messaging. While there are similarities to other
implementations, we made many different design decisions that lead to slight implementations, we made many different design decisions that lead to slight
differences when comparing CAF to other actor frameworks. differences when comparing CAF to other actor frameworks.
\subsubsection{Dynamically Typed Actor} Dynamically Typed Actor
~~~~~~~~~~~~~~~~~~~~~~~
A dynamically typed actor accepts any kind of message and dispatches on its A dynamically typed actor accepts any kind of message and dispatches on its
content dynamically at the receiver. This is the traditional messaging style content dynamically at the receiver. This is the traditional messaging style
...@@ -46,7 +50,8 @@ found in implementations like Erlang or Akka. The upside of this approach is ...@@ -46,7 +50,8 @@ found in implementations like Erlang or Akka. The upside of this approach is
(usually) faster prototyping and less code. This comes at the cost of requiring (usually) faster prototyping and less code. This comes at the cost of requiring
excessive testing. excessive testing.
\subsubsection{Statically Typed Actor} Statically Typed Actor
~~~~~~~~~~~~~~~~~~~~~~
CAF achieves static type-checking for actors by defining abstract messaging CAF achieves static type-checking for actors by defining abstract messaging
interfaces. Since interfaces define both input and output types, CAF is able to interfaces. Since interfaces define both input and output types, CAF is able to
...@@ -55,75 +60,87 @@ higher robustness to code changes and fewer possible runtime errors. This comes ...@@ -55,75 +60,87 @@ higher robustness to code changes and fewer possible runtime errors. This comes
at an increase in required source code, as developers have to define and use at an increase in required source code, as developers have to define and use
messaging interfaces. messaging interfaces.
\subsubsection{Actor References} .. _actor-reference:
\label{actor-reference}
Actor References
~~~~~~~~~~~~~~~~
CAF uses reference counting for actors. The three ways to store a reference to CAF uses reference counting for actors. The three ways to store a reference to
an actor are addresses, handles, and pointers. Note that \emph{address} does an actor are addresses, handles, and pointers. Note that *address* does
not refer to a \emph{memory region} in this context. not refer to a *memory region* in this context.
.. _actor-address:
\paragraph{Address} Address
\label{actor-address} +++++++
Each actor has a (network-wide) unique logical address. This identifier is Each actor has a (network-wide) unique logical address. This identifier is
represented by \lstinline^actor_addr^, which allows to identify and monitor an represented by ``actor_addr``, which allows to identify and monitor an actor.
actor. Unlike other actor frameworks, CAF does \emph{not} allow users to send Unlike other actor frameworks, CAF does *not* allow users to send messages to
messages to addresses. This limitation is due to the fact that the address does addresses. This limitation is due to the fact that the address does not contain
not contain any type information. Hence, it would not be safe to send it a any type information. Hence, it would not be safe to send it a message, because
message, because the receiving actor might use a statically typed interface the receiving actor might use a statically typed interface that does not accept
that does not accept the given message. Because an \lstinline^actor_addr^ fills the given message. Because an ``actor_addr`` fills the role of an identifier, it
the role of an identifier, it has \emph{weak reference semantics} has *weak reference semantics* (see :ref:`reference-counting`).
\see{reference-counting}.
.. _actor-handle:
\paragraph{Handle}
\label{actor-handle} Handle
++++++
An actor handle contains the address of an actor along with its type
information and is required for sending messages to actors. The distinction An actor handle contains the address of an actor along with its type information
between handles and addresses---which is unique to CAF when comparing it to and is required for sending messages to actors. The distinction between handles
other actor systems---is a consequence of the design decision to enforce static and addresses---which is unique to CAF when comparing it to other actor
type checking for all messages. Dynamically typed actors use \lstinline^actor^ systems---is a consequence of the design decision to enforce static type
handles, while statically typed actors use \lstinline^typed_actor<...>^ checking for all messages. Dynamically typed actors use ``actor`` handles, while
handles. Both types have \emph{strong reference semantics} statically typed actors use ``typed_actor<...>`` handles. Both types have
\see{reference-counting}. *strong reference semantics* (see :ref:`reference-counting`).
\paragraph{Pointer} .. _actor-pointer:
\label{actor-pointer}
Pointer
In a few instances, CAF uses \lstinline^strong_actor_ptr^ to refer to an actor +++++++
using \emph{strong reference semantics} \see{reference-counting} without
knowing the proper handle type. Pointers must be converted to a handle via In a few instances, CAF uses ``strong_actor_ptr`` to refer to an actor using
\lstinline^actor_cast^ \see{actor-cast} prior to sending messages. A *strong reference semantics* (see :ref:`reference-counting`) without knowing the
\lstinline^strong_actor_ptr^ can be \emph{null}. proper handle type. Pointers must be converted to a handle via ``actor_cast``
(see :ref:`actor-cast`) prior to sending messages. A ``strong_actor_ptr`` can be
\subsubsection{Spawning} *null*.
\emph{Spawning} an actor means to create and run a new actor. Spawning
~~~~~~~~
\subsubsection{Monitor}
\label{monitor} *Spawning* an actor means to create and run a new actor.
A monitored actor sends a down message~\see{down-message} to all actors .. _monitor:
Monitor
~~~~~~~
A monitored actor sends a down message (see :ref:`down-message`) to all actors
monitoring it as part of its termination. This allows actors to supervise other monitoring it as part of its termination. This allows actors to supervise other
actors and to take actions when one of the supervised actors fails, i.e., actors and to take actions when one of the supervised actors fails, i.e.,
terminates with a non-normal exit reason. terminates with a non-normal exit reason.
\subsubsection{Link} .. _link:
\label{link}
Link
~~~~
A link is a bidirectional connection between two actors. Each actor sends an A link is a bidirectional connection between two actors. Each actor sends an
exit message~\see{exit-message} to all of its links as part of its termination. exit message (see :ref:`exit-message`) to all of its links as part of its
Unlike down messages, exit messages cause the receiving actor to terminate as termination. Unlike down messages, exit messages cause the receiving actor to
well when receiving a non-normal exit reason per default. This allows terminate as well when receiving a non-normal exit reason per default. This
developers to create a set of actors with the guarantee that either all or no allows developers to create a set of actors with the guarantee that either all
actors are alive. Actors can override the default handler to implement error or no actors are alive. Actors can override the default handler to implement
recovery strategies. error recovery strategies.
\subsection{Experimental Features} Experimental Features
---------------------
Sections that discuss experimental features are highlighted with \experimental.
The API of such features is not stable. This means even minor updates to CAF Sections that discuss experimental features are highlighted with
can come with breaking changes to the API or even remove a feature completely. :sup:`experimental`. The API of such features is not stable. This means even
However, we encourage developers to extensively test such features and to start minor updates to CAF can come with breaking changes to the API or even remove a
discussions to uncover flaws, report bugs, or tweaking the API in order to feature completely. However, we encourage developers to extensively test such
improve a feature or streamline it to cover certain use cases. features and to start discussions to uncover flaws, report bugs, or tweaking the
API in order to improve a feature or streamline it to cover certain use cases.
\section{Managing Groups of Workers \experimental} .. _worker-groups:
\label{worker-groups}
Managing Groups of Workers :sup:`experimental`
===============================================
When managing a set of workers, a central actor often dispatches requests to a When managing a set of workers, a central actor often dispatches requests to a
set of workers. For this purpose, the class \lstinline^actor_pool^ implements a set of workers. For this purpose, the class ``actor_pool`` implements a
lightweight abstraction for managing a set of workers using a dispatching lightweight abstraction for managing a set of workers using a dispatching
policy. Unlike groups, pools usually own their workers. policy. Unlike groups, pools usually own their workers.
Pools are created using the static member function \lstinline^make^, which Pools are created using the static member function ``make``, which
takes either one argument (the policy) or three (number of workers, factory takes either one argument (the policy) or three (number of workers, factory
function for workers, and dispatching policy). After construction, one can add function for workers, and dispatching policy). After construction, one can add
new workers via messages of the form \texttt{('SYS', 'PUT', worker)}, remove new workers via messages of the form ``('SYS', 'PUT', worker)``, remove
workers with \texttt{('SYS', 'DELETE', worker)}, and retrieve the set of workers with ``('SYS', 'DELETE', worker)``, and retrieve the set of
workers as \lstinline^vector<actor>^ via \texttt{('SYS', 'GET')}. workers as ``vector<actor>`` via ``('SYS', 'GET')``.
An actor pool takes ownership of its workers. When forced to quit, it sends an An actor pool takes ownership of its workers. When forced to quit, it sends an
exit messages to all of its workers, forcing them to quit as well. The pool exit messages to all of its workers, forcing them to quit as well. The pool
...@@ -22,61 +24,62 @@ Consequently, a terminating worker loses all unprocessed messages. For more ...@@ -22,61 +24,62 @@ Consequently, a terminating worker loses all unprocessed messages. For more
advanced caching strategies, such as reliable message delivery, users can advanced caching strategies, such as reliable message delivery, users can
implement their own dispatching policies. implement their own dispatching policies.
\subsection{Dispatching Policies} Dispatching Policies
--------------------
A dispatching policy is a functor with the following signature: A dispatching policy is a functor with the following signature:
\begin{lstlisting} .. code-block:: C++
using uplock = upgrade_lock<detail::shared_spinlock>;
using policy = std::function<void (actor_system& sys, using uplock = upgrade_lock<detail::shared_spinlock>;
using policy = std::function<void (actor_system& sys,
uplock& guard, uplock& guard,
const actor_vec& workers, const actor_vec& workers,
mailbox_element_ptr& ptr, mailbox_element_ptr& ptr,
execution_unit* host)>; execution_unit* host)>;
\end{lstlisting}
The argument \lstinline^guard^ is a shared lock that can be upgraded for unique The argument ``guard`` is a shared lock that can be upgraded for unique
access if the policy includes a critical section. The second argument is a access if the policy includes a critical section. The second argument is a
vector containing all workers managed by the pool. The argument \lstinline^ptr^ vector containing all workers managed by the pool. The argument ``ptr``
contains the full message as received by the pool. Finally, \lstinline^host^ is contains the full message as received by the pool. Finally, ``host`` is
the current scheduler context that can be used to enqueue workers into the the current scheduler context that can be used to enqueue workers into the
corresponding job queue. corresponding job queue.
The actor pool class comes with a set predefined policies, accessible via The actor pool class comes with a set predefined policies, accessible via
factory functions, for convenience. factory functions, for convenience.
\begin{lstlisting} .. code-block:: C++
actor_pool::policy actor_pool::round_robin();
\end{lstlisting} actor_pool::policy actor_pool::round_robin();
This policy forwards incoming requests in a round-robin manner to workers. This policy forwards incoming requests in a round-robin manner to workers.
There is no guarantee that messages are consumed, i.e., work items are lost if There is no guarantee that messages are consumed, i.e., work items are lost if
the worker exits before processing all of its messages. the worker exits before processing all of its messages.
\begin{lstlisting} .. code-block:: C++
actor_pool::policy actor_pool::broadcast();
\end{lstlisting}
This policy forwards \emph{each} message to \emph{all} workers. Synchronous actor_pool::policy actor_pool::broadcast();
This policy forwards *each* message to *all* workers. Synchronous
messages to the pool will be received by all workers, but the client will only messages to the pool will be received by all workers, but the client will only
recognize the first arriving response message---or error---and discard recognize the first arriving response message---or error---and discard
subsequent messages. Note that this is not caused by the policy itself, but a subsequent messages. Note that this is not caused by the policy itself, but a
consequence of forwarding synchronous messages to more than one actor. consequence of forwarding synchronous messages to more than one actor.
\begin{lstlisting} .. code-block:: C++
actor_pool::policy actor_pool::random();
\end{lstlisting} actor_pool::policy actor_pool::random();
This policy forwards incoming requests to one worker from the pool chosen This policy forwards incoming requests to one worker from the pool chosen
uniformly at random. Analogous to \lstinline^round_robin^, this policy does not uniformly at random. Analogous to ``round_robin``, this policy does not
cache or redispatch messages. cache or redispatch messages.
\begin{lstlisting} .. code-block:: C++
using join = function<void (T&, message&)>;
using split = function<void (vector<pair<actor, message>>&, message&)>; using join = function<void (T&, message&)>;
template <class T> using split = function<void (vector<pair<actor, message>>&, message&)>;
static policy split_join(join jf, split sf = ..., T init = T()); template <class T>
\end{lstlisting} static policy split_join(join jf, split sf = ..., T init = T());
This policy models split/join or scatter/gather work flows, where a work item This policy models split/join or scatter/gather work flows, where a work item
is split into as many tasks as workers are available and then the individuals is split into as many tasks as workers are available and then the individuals
...@@ -84,7 +87,7 @@ results are joined together before sending the full result back to the client. ...@@ -84,7 +87,7 @@ results are joined together before sending the full result back to the client.
The join function is responsible for ``glueing'' all result messages together The join function is responsible for ``glueing'' all result messages together
to create a single result. The function is called with the result object to create a single result. The function is called with the result object
(initialed using \lstinline^init^) and the current result messages from a (initialed using ``init``) and the current result messages from a
worker. worker.
The first argument of a split function is a mapping from actors (workers) to The first argument of a split function is a mapping from actors (workers) to
......
\section{Message Handlers} .. _message-handler:
\label{message-handler}
Message Handlers
================
Actors can store a set of callbacks---usually implemented as lambda Actors can store a set of callbacks---usually implemented as lambda
expressions---using either \lstinline^behavior^ or \lstinline^message_handler^. expressions---using either ``behavior`` or ``message_handler``.
The former stores an optional timeout, while the latter is composable. The former stores an optional timeout, while the latter is composable.
\subsection{Definition and Composition} Definition and Composition
--------------------------
As the name implies, a \lstinline^behavior^ defines the response of an actor to As the name implies, a ``behavior`` defines the response of an actor to
messages it receives. The optional timeout allows an actor to dynamically messages it receives. The optional timeout allows an actor to dynamically
change its behavior when not receiving message after a certain amount of time. change its behavior when not receiving message after a certain amount of time.
\begin{lstlisting} .. code-block:: C++
message_handler x1{
message_handler x1{
[](int i) { /*...*/ }, [](int i) { /*...*/ },
[](double db) { /*...*/ }, [](double db) { /*...*/ },
[](int a, int b, int c) { /*...*/ } [](int a, int b, int c) { /*...*/ }
}; };
\end{lstlisting}
In our first example, \lstinline^x1^ models a behavior accepting messages that In our first example, ``x1`` models a behavior accepting messages that consist
consist of either exactly one \lstinline^int^, or one \lstinline^double^, or of either exactly one ``int``, or one ``double``, or three ``int`` values. Any
three \lstinline^int^ values. Any other message is not matched and gets other message is not matched and gets forwarded to the default handler (see
forwarded to the default handler \see{default-handler}. :ref:`default-handler`).
\begin{lstlisting} .. code-block:: C++
message_handler x2{
message_handler x2{
[](double db) { /*...*/ }, [](double db) { /*...*/ },
[](double db) { /* - unreachable - */ } [](double db) { /* - unreachable - */ }
}; };
\end{lstlisting}
Our second example illustrates an important characteristic of the matching Our second example illustrates an important characteristic of the matching
mechanism. Each message is matched against the callbacks in the order they are mechanism. Each message is matched against the callbacks in the order they are
defined. The algorithm stops at the first match. Hence, the second callback in defined. The algorithm stops at the first match. Hence, the second callback in
\lstinline^x2^ is unreachable. ``x2`` is unreachable.
.. code-block:: C++
\begin{lstlisting} message_handler x3 = x1.or_else(x2);
message_handler x3 = x1.or_else(x2); message_handler x4 = x2.or_else(x1);
message_handler x4 = x2.or_else(x1);
\end{lstlisting}
Message handlers can be combined using \lstinline^or_else^. This composition is Message handlers can be combined using ``or_else``. This composition is
not commutative, as our third examples illustrates. The resulting message not commutative, as our third examples illustrates. The resulting message
handler will first try to handle a message using the left-hand operand and will handler will first try to handle a message using the left-hand operand and will
fall back to the right-hand operand if the former did not match. Thus, fall back to the right-hand operand if the former did not match. Thus,
\lstinline^x3^ behaves exactly like \lstinline^x1^. This is because the second ``x3`` behaves exactly like ``x1``. This is because the second
callback in \lstinline^x1^ will consume any message with a single callback in ``x1`` will consume any message with a single
\lstinline^double^ and both callbacks in \lstinline^x2^ are thus unreachable. ``double`` and both callbacks in ``x2`` are thus unreachable.
The handler \lstinline^x4^ will consume messages with a single The handler ``x4`` will consume messages with a single
\lstinline^double^ using the first callback in \lstinline^x2^, essentially ``double`` using the first callback in ``x2``, essentially
overriding the second callback in \lstinline^x1^. overriding the second callback in ``x1``.
\clearpage .. _atom:
\subsection{Atoms}
\label{atom} Atoms
-----
Defining message handlers in terms of callbacks is convenient, but requires a Defining message handlers in terms of callbacks is convenient, but requires a
simple way to annotate messages with meta data. Imagine an actor that provides simple way to annotate messages with meta data. Imagine an actor that provides
...@@ -63,51 +67,51 @@ user-defined operation and returns the result. Without additional context, the ...@@ -63,51 +67,51 @@ user-defined operation and returns the result. Without additional context, the
actor cannot decide whether it should multiply or add the integers. Thus, the actor cannot decide whether it should multiply or add the integers. Thus, the
operation must be encoded into the message. The Erlang programming language operation must be encoded into the message. The Erlang programming language
introduced an approach to use non-numerical constants, so-called introduced an approach to use non-numerical constants, so-called
\textit{atoms}, which have an unambiguous, special-purpose type and do not have *atoms*, which have an unambiguous, special-purpose type and do not have
the runtime overhead of string constants. the runtime overhead of string constants.
Atoms in CAF are mapped to integer values at compile time. This mapping is Atoms in CAF are mapped to integer values at compile time. This mapping is
guaranteed to be collision-free and invertible, but limits atom literals to ten guaranteed to be collision-free and invertible, but limits atom literals to ten
characters and prohibits special characters. Legal characters are characters and prohibits special characters. Legal characters are
\lstinline^_0-9A-Za-z^ and the whitespace character. Atoms are created using ``_0-9A-Za-z`` and the whitespace character. Atoms are created using
the \lstinline^constexpr^ function \lstinline^atom^, as the following example the ``constexpr`` function ``atom``, as the following example
illustrates. illustrates.
\begin{lstlisting} .. code-block:: C++
atom_value a1 = atom("add");
atom_value a2 = atom("multiply"); atom_value a1 = atom("add");
\end{lstlisting} atom_value a2 = atom("multiply");
\textbf{Warning}: The compiler cannot enforce the restrictions at compile time, **Warning**: The compiler cannot enforce the restrictions at compile time,
except for a length check. The assertion \lstinline^atom("!?") != atom("?!")^ except for a length check. The assertion ``atom("!?") != atom("?!")``
is not true, because each invalid character translates to a whitespace is not true, because each invalid character translates to a whitespace
character. character.
While the \lstinline^atom_value^ is computed at compile time, it is not While the ``atom_value`` is computed at compile time, it is not
uniquely typed and thus cannot be used in the signature of a callback. To uniquely typed and thus cannot be used in the signature of a callback. To
accomplish this, CAF offers compile-time \emph{atom constants}. accomplish this, CAF offers compile-time *atom constants*.
\begin{lstlisting} .. code-block:: C++
using add_atom = atom_constant<atom("add")>;
using multiply_atom = atom_constant<atom("multiply")>; using add_atom = atom_constant<atom("add")>;
\end{lstlisting} using multiply_atom = atom_constant<atom("multiply")>;
Using these constants, we can now define message passing interfaces in a Using these constants, we can now define message passing interfaces in a
convenient way: convenient way:
\begin{lstlisting} .. code-block:: C++
behavior do_math{
behavior do_math{
[](add_atom, int a, int b) { [](add_atom, int a, int b) {
return a + b; return a + b;
}, },
[](multiply_atom, int a, int b) { [](multiply_atom, int a, int b) {
return a * b; return a * b;
} }
}; };
// caller side: send(math_actor, add_atom::value, 1, 2) // caller side: send(math_actor, add_atom::value, 1, 2)
\end{lstlisting}
Atom constants define a static member \lstinline^value^. Please note that this Atom constants define a static member ``value``. Please note that this
static \lstinline^value^ member does \emph{not} have the type static ``value`` member does *not* have the type
\lstinline^atom_value^, unlike \lstinline^std::integral_constant^ for example. ``atom_value``, unlike ``std::integral_constant`` for example.
\section{Message Passing} .. _message-passing:
\label{message-passing}
Message Passing
===============
Message passing in CAF is always asynchronous. Further, CAF neither guarantees Message passing in CAF is always asynchronous. Further, CAF neither guarantees
message delivery nor message ordering in a distributed setting. CAF uses TCP message delivery nor message ordering in a distributed setting. CAF uses TCP
...@@ -9,29 +11,34 @@ intermediate nodes and can get lost if one of the forwarding nodes fails. ...@@ -9,29 +11,34 @@ intermediate nodes and can get lost if one of the forwarding nodes fails.
Likewise, forwarding paths can change dynamically and thus cause messages to Likewise, forwarding paths can change dynamically and thus cause messages to
arrive out of order. arrive out of order.
The messaging layer of CAF has three primitives for sending messages: The messaging layer of CAF has three primitives for sending messages: ``send``,
\lstinline^send^, \lstinline^request^, and \lstinline^delegate^. The former ``request``, and ``delegate``. The former simply enqueues a message to the
simply enqueues a message to the mailbox the receiver. The latter two are mailbox the receiver. The latter two are discussed in more detail in
discussed in more detail in \sref{request} and \sref{delegate}. :ref:`request` and :ref:`delegate`.
.. _mailbox-element:
\subsection{Structure of Mailbox Elements} Structure of Mailbox Elements
\label{mailbox-element} -----------------------------
When enqueuing a message to the mailbox of an actor, CAF wraps the content of When enqueuing a message to the mailbox of an actor, CAF wraps the content of
the message into a \lstinline^mailbox_element^ (shown below) to add meta data the message into a ``mailbox_element`` (shown below) to add meta data
and processing paths. and processing paths.
\singlefig{mailbox_element}{UML class diagram for \lstinline^mailbox_element^}{mailbox_element} .. _mailbox_element:
.. image:: mailbox_element.png
:alt: UML class diagram for ``mailbox_element``
The sender is stored as a \lstinline^strong_actor_ptr^ \see{actor-pointer} and The sender is stored as a ``strong_actor_ptr`` (see :ref:`actor-pointer`) and
denotes the origin of the message. The message ID is either 0---invalid---or a denotes the origin of the message. The message ID is either 0---invalid---or a
positive integer value that allows the sender to match a response to its positive integer value that allows the sender to match a response to its
request. The \lstinline^stages^ vector stores the path of the message. Response request. The ``stages`` vector stores the path of the message. Response
messages, i.e., the returned values of a message handler, are sent to messages, i.e., the returned values of a message handler, are sent to
\lstinline^stages.back()^ after calling \lstinline^stages.pop_back()^. This ``stages.back()`` after calling ``stages.pop_back()``. This allows CAF to build
allows CAF to build pipelines of arbitrary size. If no more stage is left, the pipelines of arbitrary size. If no more stage is left, the response reaches the
response reaches the sender. Finally, \lstinline^content()^ grants access to sender. Finally, ``content()`` grants access to the type-erased tuple storing
the type-erased tuple storing the message itself. the message itself.
Mailbox elements are created by CAF automatically and are usually invisible to Mailbox elements are created by CAF automatically and are usually invisible to
the programmer. However, understanding how messages are processed internally the programmer. However, understanding how messages are processed internally
...@@ -41,214 +48,278 @@ It is worth mentioning that CAF usually wraps the mailbox element and its ...@@ -41,214 +48,278 @@ It is worth mentioning that CAF usually wraps the mailbox element and its
content into a single object in order to reduce the number of memory content into a single object in order to reduce the number of memory
allocations. allocations.
\subsection{Copy on Write} .. _copy-on-write:
\label{copy-on-write}
Copy on Write
-------------
CAF allows multiple actors to implicitly share message contents, as long as no CAF allows multiple actors to implicitly share message contents, as long as no
actor performs writes. This allows groups~\see{groups} to send the same content actor performs writes. This allows groups (see :ref:`groups`) to send the same
to all subscribed actors without any copying overhead. content to all subscribed actors without any copying overhead.
Actors copy message contents whenever other actors hold references to it and if Actors copy message contents whenever other actors hold references to it and if
one or more arguments of a message handler take a mutable reference. one or more arguments of a message handler take a mutable reference.
\subsection{Requirements for Message Types} Requirements for Message Types
------------------------------
Message types in CAF must meet the following requirements: Message types in CAF must meet the following requirements:
\begin{enumerate} 1. Serializable or inspectable (see :ref:`type-inspection`)
\item Serializable or inspectable \see{type-inspection} 2. Default constructible
\item Default constructible 3. Copy constructible
\item Copy constructible
\end{enumerate} A type is serializable if it provides free function
``serialize(Serializer&, T&)`` or
``serialize(Serializer&, T&, const unsigned int)``. Accordingly, a type is
inspectable if it provides a free function ``inspect(Inspector&, T&)``.
A type is serializable if it provides free function \lstinline^serialize(Serializer&, T&)^ or \lstinline^serialize(Serializer&, T&, const unsigned int)^. Accordingly, a type is inspectable if it provides a free function \lstinline^inspect(Inspector&, T&)^. Requirement 2 is a consequence of requirement 1, because CAF needs to be able to
create an object of a type before it can call ``serialize`` or ``inspect`` on
it. Requirement 3 allows CAF to implement Copy on Write (see
:ref:`copy-on-write`).
Requirement 2 is a consequence of requirement 1, because CAF needs to be able .. _special-handler:
to create an object of a type before it can call \lstinline^serialize^ or
\lstinline^inspect^ on it. Requirement 3 allows CAF to implement Copy on
Write~\see{copy-on-write}.
\subsection{Default and System Message Handlers} Default and System Message Handlers
\label{special-handler} -----------------------------------
CAF has three system-level message types (\lstinline^down_msg^, CAF has three system-level message types (``down_msg``, ``exit_msg``, and
\lstinline^exit_msg^, and \lstinline^error^) that all actor should handle ``error``) that all actor should handle regardless of there current state.
regardless of there current state. Consequently, event-based actors handle such Consequently, event-based actors handle such messages in special-purpose message
messages in special-purpose message handlers. Additionally, event-based actors handlers. Additionally, event-based actors have a fallback handler for unmatched
have a fallback handler for unmatched messages. Note that blocking actors have messages. Note that blocking actors have neither of those special-purpose
neither of those special-purpose handlers \see{blocking-actor}. handlers (see :ref:`blocking-actor`).
\subsubsection{Down Handler} .. _down-message:
\label{down-message}
Actors can monitor the lifetime of other actors by calling \lstinline^self->monitor(other)^. This will cause the runtime system of CAF to send a \lstinline^down_msg^ for \lstinline^other^ if it dies. Actors drop down messages unless they provide a custom handler via \lstinline^set_down_handler(f)^, where \lstinline^f^ is a function object with signature \lstinline^void (down_msg&)^ or \lstinline^void (scheduled_actor*, down_msg&)^. The latter signature allows users to implement down message handlers as free function. Down Handler
~~~~~~~~~~~~~
\subsubsection{Exit Handler} Actors can monitor the lifetime of other actors by calling
\label{exit-message} ``self->monitor(other)``. This will cause the runtime system of CAF to send a
``down_msg`` for ``other`` if it dies. Actors drop down messages unless they
provide a custom handler via ``set_down_handler(f)``, where ``f`` is a function
object with signature ``void (down_msg&)`` or
``void (scheduled_actor*, down_msg&)``. The latter signature allows users to
implement down message handlers as free function.
Bidirectional monitoring with a strong lifetime coupling is established by calling \lstinline^self->link_to(other)^. This will cause the runtime to send an \lstinline^exit_msg^ if either \lstinline^this^ or \lstinline^other^ dies. Per default, actors terminate after receiving an \lstinline^exit_msg^ unless the exit reason is \lstinline^exit_reason::normal^. This mechanism propagates failure states in an actor system. Linked actors form a sub system in which an error causes all actors to fail collectively. Actors can override the default handler via \lstinline^set_exit_handler(f)^, where \lstinline^f^ is a function object with signature \lstinline^void (exit_message&)^ or \lstinline^void (scheduled_actor*, exit_message&)^. .. _exit-message:
\subsubsection{Error Handler} Exit Handler
\label{error-message} ~~~~~~~~~~~~
Actors send error messages to others by returning an \lstinline^error^ \see{error} from a message handler. Similar to exit messages, error messages usually cause the receiving actor to terminate, unless a custom handler was installed via \lstinline^set_error_handler(f)^, where \lstinline^f^ is a function object with signature \lstinline^void (error&)^ or \lstinline^void (scheduled_actor*, error&)^. Additionally, \lstinline^request^ accepts an error handler as second argument to handle errors for a particular request~\see{error-response}. The default handler is used as fallback if \lstinline^request^ is used without error handler. Bidirectional monitoring with a strong lifetime coupling is established by
calling ``self->link_to(other)``. This will cause the runtime to send an
``exit_msg`` if either ``this`` or ``other`` dies. Per default, actors terminate
after receiving an ``exit_msg`` unless the exit reason is
``exit_reason::normal``. This mechanism propagates failure states in an actor
system. Linked actors form a sub system in which an error causes all actors to
fail collectively. Actors can override the default handler via
``set_exit_handler(f)``, where ``f`` is a function object with signature
``void (exit_message&)`` or ``void (scheduled_actor*, exit_message&)``.
\subsubsection{Default Handler} .. _error-message:
\label{default-handler}
Error Handler
~~~~~~~~~~~~~
Actors send error messages to others by returning an ``error`` (see
:ref:`error`) from a message handler. Similar to exit messages, error messages
usually cause the receiving actor to terminate, unless a custom handler was
installed via ``set_error_handler(f)``, where ``f`` is a function object with
signature ``void (error&)`` or ``void (scheduled_actor*, error&)``.
Additionally, ``request`` accepts an error handler as second argument to handle
errors for a particular request (see :ref:`error-response`). The default handler
is used as fallback if ``request`` is used without error handler.
.. _default-handler:
Default Handler
~~~~~~~~~~~~~~~
The default handler is called whenever the behavior of an actor did not match The default handler is called whenever the behavior of an actor did not match
the input. Actors can change the default handler by calling the input. Actors can change the default handler by calling
\lstinline^set_default_handler^. The expected signature of the function object ``set_default_handler``. The expected signature of the function object
is \lstinline^result<message> (scheduled_actor*, message_view&)^, whereas the is ``result<message> (scheduled_actor*, message_view&)``, whereas the
\lstinline^self^ pointer can again be omitted. The default handler can return a ``self`` pointer can again be omitted. The default handler can return a
response message or cause the runtime to \emph{skip} the input message to allow response message or cause the runtime to *skip* the input message to allow
an actor to handle it in a later state. CAF provides the following built-in an actor to handle it in a later state. CAF provides the following built-in
implementations: \lstinline^reflect^, \lstinline^reflect_and_quit^, implementations: ``reflect``, ``reflect_and_quit``,
\lstinline^print_and_drop^, \lstinline^drop^, and \lstinline^skip^. The former ``print_and_drop``, ``drop``, and ``skip``. The former
two are meant for debugging and testing purposes and allow an actor to simply two are meant for debugging and testing purposes and allow an actor to simply
return an input. The next two functions drop unexpected messages with or return an input. The next two functions drop unexpected messages with or
without printing a warning beforehand. Finally, \lstinline^skip^ leaves the without printing a warning beforehand. Finally, ``skip`` leaves the
input message in the mailbox. The default is \lstinline^print_and_drop^. input message in the mailbox. The default is ``print_and_drop``.
.. _request:
\subsection{Requests} Requests
\label{request} --------
A main feature of CAF is its ability to couple input and output types via the A main feature of CAF is its ability to couple input and output types via the
type system. For example, a \lstinline^typed_actor<replies_to<int>::with<int>>^ type system. For example, a ``typed_actor<replies_to<int>::with<int>>``
essentially behaves like a function. It receives a single \lstinline^int^ as essentially behaves like a function. It receives a single ``int`` as
input and responds with another \lstinline^int^. CAF embraces this functional input and responds with another ``int``. CAF embraces this functional
take on actors by simply creating response messages from the result of message take on actors by simply creating response messages from the result of message
handlers. This allows CAF to match \emph{request} to \emph{response} messages handlers. This allows CAF to match *request* to *response* messages
and to provide a convenient API for this style of communication. and to provide a convenient API for this style of communication.
\subsubsection{Sending Requests and Handling Responses} .. _handling-response:
\label{handling-response}
Actors send request messages by calling \lstinline^request(receiver, timeout, content...)^. This function returns an intermediate object that allows an actor to set a one-shot handler for the response message. Event-based actors can use either \lstinline^request(...).then^ or \lstinline^request(...).await^. The former multiplexes the one-shot handler with the regular actor behavior and handles requests as they arrive. The latter suspends the regular actor behavior until all awaited responses arrive and handles requests in LIFO order. Blocking actors always use \lstinline^request(...).receive^, which blocks until the one-shot handler was called. Actors receive a \lstinline^sec::request_timeout^ \see{sec} error message~\see{error-message} if a timeout occurs. Users can set the timeout to \lstinline^infinite^ for unbound operations. This is only recommended if the receiver is running locally. Sending Requests and Handling Responses
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Actors send request messages by calling ``request(receiver, timeout,
content...)``. This function returns an intermediate object that allows an actor
to set a one-shot handler for the response message. Event-based actors can use
either ``request(...).then`` or ``request(...).await``. The former multiplexes
the one-shot handler with the regular actor behavior and handles requests as
they arrive. The latter suspends the regular actor behavior until all awaited
responses arrive and handles requests in LIFO order. Blocking actors always use
``request(...).receive``, which blocks until the one-shot handler was called.
Actors receive a ``sec::request_timeout`` (see :ref:`sec`) error message (see
:ref:`error-message`) if a timeout occurs. Users can set the timeout to
``infinite`` for unbound operations. This is only recommended if the receiver is
running locally.
In our following example, we use the simple cell actors shown below as In our following example, we use the simple cell actors shown below as
communication endpoints. communication endpoints.
\cppexample[20-37]{message_passing/request} .. literalinclude:: /examples/message_passing/request.cpp
:language: C++
:lines: 20-37
The first part of the example illustrates how event-based actors can use either The first part of the example illustrates how event-based actors can use either
\lstinline^then^ or \lstinline^await^. ``then`` or ``await``.
\cppexample[39-51]{message_passing/request}
\clearpage .. literalinclude:: /examples/message_passing/request.cpp
:language: C++
:lines: 39-51
The second half of the example shows a blocking actor making use of The second half of the example shows a blocking actor making use of
\lstinline^receive^. Note that blocking actors have no special-purpose handler ``receive``. Note that blocking actors have no special-purpose handler
for error messages and therefore are required to pass a callback for error for error messages and therefore are required to pass a callback for error
messages when handling response messages. messages when handling response messages.
\cppexample[53-64]{message_passing/request} .. literalinclude:: /examples/message_passing/request.cpp
:language: C++
:lines: 53-64
We spawn five cells and assign the values 0, 1, 4, 9, and 16. We spawn five cells and assign the values 0, 1, 4, 9, and 16.
\cppexample[67-69]{message_passing/request} .. literalinclude:: /examples/message_passing/request.cpp
:language: C++
:lines: 67-69
When passing the \lstinline^cells^ vector to our three different When passing the ``cells`` vector to our three different
implementations, we observe three outputs. Our \lstinline^waiting_testee^ actor implementations, we observe three outputs. Our ``waiting_testee`` actor
will always print: will always print:
\begin{footnotesize} .. ::
\begin{verbatim}
cell #9 -> 16 cell #9 -> 16
cell #8 -> 9 cell #8 -> 9
cell #7 -> 4 cell #7 -> 4
cell #6 -> 1 cell #6 -> 1
cell #5 -> 0 cell #5 -> 0
\end{verbatim}
\end{footnotesize} This is because ``await`` puts the one-shots handlers onto a stack and
This is because \lstinline^await^ puts the one-shots handlers onto a stack and
enforces LIFO order by re-ordering incoming response messages. enforces LIFO order by re-ordering incoming response messages.
The \lstinline^multiplexed_testee^ implementation does not print its results in The ``multiplexed_testee`` implementation does not print its results in
a predicable order. Response messages arrive in arbitrary order and are handled a predicable order. Response messages arrive in arbitrary order and are handled
immediately. immediately.
Finally, the \lstinline^blocking_testee^ implementation will always print: Finally, the ``blocking_testee`` implementation will always print:
.. ::
\begin{footnotesize} cell #5 -> 0
\begin{verbatim} cell #6 -> 1
cell #5 -> 0 cell #7 -> 4
cell #6 -> 1 cell #8 -> 9
cell #7 -> 4 cell #9 -> 16
cell #8 -> 9
cell #9 -> 16
\end{verbatim}
\end{footnotesize}
Both event-based approaches send all requests, install a series of one-shot Both event-based approaches send all requests, install a series of one-shot
handlers, and then return from the implementing function. In contrast, the handlers, and then return from the implementing function. In contrast, the
blocking function waits for a response before sending another request. blocking function waits for a response before sending another request.
\subsubsection{Sending Multiple Requests} Sending Multiple Requests
~~~~~~~~~~~~~~~~~~~~~~~~~
Sending the same message to a group of workers is a common work flow in actor Sending the same message to a group of workers is a common work flow in actor
applications. Usually, a manager maintains a set of workers. On request, the applications. Usually, a manager maintains a set of workers. On request, the
manager fans-out the request to all of its workers and then collects the manager fans-out the request to all of its workers and then collects the
results. The function \lstinline`fan_out_request` combined with the merge policy results. The function ``fan_out_request`` combined with the merge policy
\lstinline`select_all` streamlines this exact use case. ``select_all`` streamlines this exact use case.
In the following snippet, we have a matrix actor (\lstinline`self`) that stores In the following snippet, we have a matrix actor (``self``) that stores
worker actors for each cell (each simply storing an integer). For computing the worker actors for each cell (each simply storing an integer). For computing the
average over a row, we use \lstinline`fan_out_request`. The result handler average over a row, we use ``fan_out_request``. The result handler
passed to \lstinline`then` now gets called only once with a \lstinline`vector` passed to ``then`` now gets called only once with a ``vector``
holding all collected results. Using a response promise \see{promise} further holding all collected results. Using a response promise promise_ further
allows us to delay responding to the client until we have collected all worker allows us to delay responding to the client until we have collected all worker
results. results.
\cppexample[86-98]{message_passing/fan_out_request} .. literalinclude:: /examples/message_passing/fan_out_request.cpp
:language: C++
:lines: 86-98
The policy \lstinline`select_any` models a second common use case: sending a The policy ``select_any`` models a second common use case: sending a
request to multiple receivers but only caring for the first arriving response. request to multiple receivers but only caring for the first arriving response.
\clearpage .. _error-response:
\subsubsection{Error Handling in Requests}
\label{error-response} Error Handling in Requests
~~~~~~~~~~~~~~~~~~~~~~~~~~
Requests allow CAF to unambiguously correlate request and response messages. Requests allow CAF to unambiguously correlate request and response messages.
This is also true if the response is an error message. Hence, CAF allows to This is also true if the response is an error message. Hence, CAF allows to add
add an error handler as optional second parameter to \lstinline^then^ and an error handler as optional second parameter to ``then`` and ``await`` (this
\lstinline^await^ (this parameter is mandatory for \lstinline^receive^). If no parameter is mandatory for ``receive``). If no such handler is defined, the
such handler is defined, the default error handler \see{error-message} is used default error handler (see :ref:`error-message`) is used as a fallback in
as a fallback in scheduled actors. scheduled actors.
As an example, we consider a simple divider that returns an error on a division As an example, we consider a simple divider that returns an error on a division
by zero. This examples uses a custom error category~\see{error}. by zero. This examples uses a custom error category (see :ref:`error`).
\cppexample[17-19,49-59]{message_passing/divider} .. literalinclude:: /examples/message_passing/divider.cpp
:language: C++
:lines: 17-19,49-59
When sending requests to the divider, we use a custom error handlers to report When sending requests to the divider, we use a custom error handlers to report
errors to the user. errors to the user.
\cppexample[70-76]{message_passing/divider} .. literalinclude:: /examples/message_passing/divider.cpp
:language: C++
:lines: 70-76
.. _delay-message:
\clearpage Delaying Messages
\subsection{Delaying Messages} -----------------
\label{delay-message}
Messages can be delayed by using the function \lstinline^delayed_send^, as Messages can be delayed by using the function ``delayed_send``, as
illustrated in the following time-based loop example. illustrated in the following time-based loop example.
\cppexample[58-75]{message_passing/dancing_kirby} .. literalinclude:: /examples/message_passing/dancing_kirby.cpp
:language: C++
:lines: 58-75
.. _delegate:
\clearpage Delegating Messages
\subsection{Delegating Messages} -------------------
\label{delegate}
Actors can transfer responsibility for a request by using \lstinline^delegate^. Actors can transfer responsibility for a request by using ``delegate``.
This enables the receiver of the delegated message to reply as usual---simply This enables the receiver of the delegated message to reply as usual---simply
by returning a value from its message handler---and the original sender of the by returning a value from its message handler---and the original sender of the
message will receive the response. The following diagram illustrates request message will receive the response. The following diagram illustrates request
delegation from actor B to actor C. delegation from actor B to actor C.
\begin{footnotesize} .. ::
\begin{verbatim}
A B C A B C
| | | | | |
| ---(request)---> | | | ---(request)---> | |
...@@ -265,34 +336,38 @@ delegation from actor B to actor C. ...@@ -265,34 +336,38 @@ delegation from actor B to actor C.
|<--/ |<--/
| |
X X
\end{verbatim}
\end{footnotesize}
Returning the result of \lstinline^delegate(...)^ from a message handler, as Returning the result of ``delegate(...)`` from a message handler, as
shown in the example below, suppresses the implicit response message and allows shown in the example below, suppresses the implicit response message and allows
the compiler to check the result type when using statically typed actors. the compiler to check the result type when using statically typed actors.
\cppexample[15-36]{message_passing/delegating} .. literalinclude:: /examples/message_passing/delegating.cpp
:language: C++
:lines: 15-36
.. _promise:
\subsection{Response Promises} Response Promises
\label{promise} -----------------
Response promises allow an actor to send and receive other messages prior to Response promises allow an actor to send and receive other messages prior to
replying to a particular request. Actors create a response promise using replying to a particular request. Actors create a response promise using
\lstinline^self->make_response_promise<Ts...>()^, where \lstinline^Ts^ is a ``self->make_response_promise<Ts...>()``, where ``Ts`` is a
template parameter pack describing the promised return type. Dynamically typed template parameter pack describing the promised return type. Dynamically typed
actors simply call \lstinline^self->make_response_promise()^. After retrieving actors simply call ``self->make_response_promise()``. After retrieving
a promise, an actor can fulfill it by calling the member function a promise, an actor can fulfill it by calling the member function
\lstinline^deliver(...)^, as shown in the following example. ``deliver(...)``, as shown in the following example.
\cppexample[18-43]{message_passing/promises} .. literalinclude:: /examples/message_passing/promises.cpp
:language: C++
:lines: 18-43
\clearpage Message Priorities
\subsection{Message Priorities} ------------------
By default, all messages have the default priority, i.e., By default, all messages have the default priority, i.e.,
\lstinline^message_priority::normal^. Actors can send urgent messages by ``message_priority::normal``. Actors can send urgent messages by
setting the priority explicitly: setting the priority explicitly:
\lstinline^send<message_priority::high>(dst,...)^. Urgent messages are put into ``send<message_priority::high>(dst,...)``. Urgent messages are put into
a different queue of the receiver's mailbox. Hence, long wait delays can be a different queue of the receiver's mailbox. Hence, long wait delays can be
avoided for urgent communication. avoided for urgent communication.
.. _message:
Type-Erased Tuples, Messages and Message Views
==============================================
Messages in CAF are stored in type-erased tuples. The actual message type
itself is usually hidden, as actors use pattern matching to decompose messages
automatically. However, the classes ``message`` and
``message_builder`` allow more advanced use cases than only sending
data from one actor to another.
The interface ``type_erased_tuple`` encapsulates access to arbitrary
data. This data can be stored on the heap or on the stack. A
``message`` is a type-erased tuple that is always heap-allocated and
uses copy-on-write semantics. When dealing with "plain" type-erased tuples,
users are required to check if a tuple is referenced by others via
``type_erased_tuple::shared`` before modifying its content.
The convenience class ``message_view`` holds a reference to either a
stack-located ``type_erased_tuple`` or a ``message``. The
content of the data can be access via ``message_view::content`` in both
cases, which returns a ``type_erased_tuple&``. The content of the view
can be forced into a message object by calling
``message_view::move_content_to_message``. This member function either
returns the stored message object or moves the content of a stack-allocated
tuple into a new message.
RTTI and Type Numbers
---------------------
All builtin types in CAF have a non-zero 6-bit *type number*. All
user-defined types are mapped to 0. When querying the run-time type information
(RTTI) for individual message or tuple elements, CAF returns a pair consisting
of an integer and a pointer to ``std::type_info``. The first value is
the 6-bit type number. If the type number is non-zero, the second value is a
pointer to the C++ type info, otherwise the second value is null. Additionally,
CAF generates 32 bit *type tokens*. These tokens are *type hints*
that summarizes all types in a type-erased tuple. Two type-erased tuples are of
different type if they have different type tokens (the reverse is not true).
Class ``type_erased_tuple``
---------------------------
**Note**: Calling modifiers on a shared type-erased tuple is undefined
behavior.
+----------------------------------------+------------------------------------------------------------+
| **Observers** | |
+----------------------------------------+------------------------------------------------------------+
| ``bool empty()`` | Returns whether this message is empty. |
+----------------------------------------+------------------------------------------------------------+
| ``size_t size()`` | Returns the size of this message. |
+----------------------------------------+------------------------------------------------------------+
| ``rtti_pair type(size_t pos)`` | Returns run-time type information for the nth element. |
+----------------------------------------+------------------------------------------------------------+
| ``error save(serializer& x)`` | Writes the tuple to ``x``. |
+----------------------------------------+------------------------------------------------------------+
| ``error save(size_t n, serializer& x)``| Writes the nth element to ``x``. |
+----------------------------------------+------------------------------------------------------------+
| ``const void* get(size_t n)`` | Returns a const pointer to the nth element. |
+----------------------------------------+------------------------------------------------------------+
| ``std::string stringify()`` | Returns a string representation of the tuple. |
+----------------------------------------+------------------------------------------------------------+
| ``std::string stringify(size_t n)`` | Returns a string representation of the nth element. |
+----------------------------------------+------------------------------------------------------------+
| ``bool matches(size_t n, rtti_pair)`` | Checks whether the nth element has given type. |
+----------------------------------------+------------------------------------------------------------+
| ``bool shared()`` | Checks whether more than one reference to the data exists. |
+----------------------------------------+------------------------------------------------------------+
| ``bool match_element<T>(size_t n)`` | Checks whether element ``n`` has type ``T``. |
+----------------------------------------+------------------------------------------------------------+
| ``bool match_elements<Ts...>()`` | Checks whether this message has the types ``Ts...``. |
+----------------------------------------+------------------------------------------------------------+
| ``const T& get_as<T>(size_t n)`` | Returns a const reference to the nth element. |
+----------------------------------------+------------------------------------------------------------+
| | |
+----------------------------------------+------------------------------------------------------------+
| **Modifiers** | |
+----------------------------------------+------------------------------------------------------------+
| ``void* get_mutable(size_t n)`` | Returns a mutable pointer to the nth element. |
+----------------------------------------+------------------------------------------------------------+
| ``T& get_mutable_as<T>(size_t n)`` | Returns a mutable reference to the nth element. |
+----------------------------------------+------------------------------------------------------------+
| ``void load(deserializer& x)`` | Reads the tuple from ``x``. |
+----------------------------------------+------------------------------------------------------------+
Class ``message``
-----------------
The class ``message`` includes all member functions of
``type_erased_tuple``. However, calling modifiers is always guaranteed
to be safe. A ``message`` automatically detaches its content by copying
it from the shared data on mutable access. The class further adds the following
member functions over ``type_erased_tuple``. Note that
``apply`` only detaches the content if a callback takes mutable
references as arguments.
+-----------------------------------------------+------------------------------------------------------------+
| **Observers** | |
+-----------------------------------------------+------------------------------------------------------------+
| ``message drop(size_t n)`` | Creates a new message with all but the first ``n`` values. |
+-----------------------------------------------+------------------------------------------------------------+
| ``message drop_right(size_t n)`` | Creates a new message with all but the last ``n`` values. |
+-----------------------------------------------+------------------------------------------------------------+
| ``message take(size_t n)`` | Creates a new message from the first ``n`` values. |
+-----------------------------------------------+------------------------------------------------------------+
| ``message take_right(size_t n)`` | Creates a new message from the last ``n`` values. |
+-----------------------------------------------+------------------------------------------------------------+
| ``message slice(size_t p, size_t n)`` | Creates a new message from ``[p, p + n)``. |
+-----------------------------------------------+------------------------------------------------------------+
| ``message extract(message_handler)`` | See extract_. |
+-----------------------------------------------+------------------------------------------------------------+
| ``message extract_opts(...)`` | See extract-opts_. |
+-----------------------------------------------+------------------------------------------------------------+
| | |
+-----------------------------------------------+------------------------------------------------------------+
| **Modifiers** | |
+-----------------------------------------------+------------------------------------------------------------+
| ``optional<message> apply(message_handler f)``| Returns ``f(*this)``. |
+-----------------------------------------------+------------------------------------------------------------+
| | |
+-----------------------------------------------+------------------------------------------------------------+
| **Operators** | |
+-----------------------------------------------+------------------------------------------------------------+
| ``message operator+(message x, message y)`` | Concatenates ``x`` and ``y``. |
+-----------------------------------------------+------------------------------------------------------------+
| ``message& operator+=(message& x, message y)``| Concatenates ``x`` and ``y``. |
+-----------------------------------------------+------------------------------------------------------------+
Class ``message_builder``
-------------------------
+---------------------------------------------------+-----------------------------------------------------+
| **Constructors** | |
+---------------------------------------------------+-----------------------------------------------------+
| ``(void)`` | Creates an empty message builder. |
+---------------------------------------------------+-----------------------------------------------------+
| ``(Iter first, Iter last)`` | Adds all elements from range ``[first, last)``. |
+---------------------------------------------------+-----------------------------------------------------+
| | |
+---------------------------------------------------+-----------------------------------------------------+
| **Observers** | |
+---------------------------------------------------+-----------------------------------------------------+
| ``bool empty()`` | Returns whether this message is empty. |
+---------------------------------------------------+-----------------------------------------------------+
| ``size_t size()`` | Returns the size of this message. |
+---------------------------------------------------+-----------------------------------------------------+
| ``message to_message( )`` | Converts the buffer to an actual message object. |
+---------------------------------------------------+-----------------------------------------------------+
| ``append(T val)`` | Adds ``val`` to the buffer. |
+---------------------------------------------------+-----------------------------------------------------+
| ``append(Iter first, Iter last)`` | Adds all elements from range ``[first, last)``. |
+---------------------------------------------------+-----------------------------------------------------+
| ``message extract(message_handler)`` | See extract_. |
+---------------------------------------------------+-----------------------------------------------------+
| ``message extract_opts(...)`` | See extract-opts_. |
+---------------------------------------------------+-----------------------------------------------------+
| | |
+---------------------------------------------------+-----------------------------------------------------+
| **Modifiers** | |
+---------------------------------------------------+-----------------------------------------------------+
| ``optional<message>`` ``apply(message_handler f)``| Returns ``f(*this)``. |
+---------------------------------------------------+-----------------------------------------------------+
| ``message move_to_message()`` | Transfers ownership of its data to the new message. |
+---------------------------------------------------+-----------------------------------------------------+
.. _extract:
Extracting
----------
The member function ``message::extract`` removes matched elements from a
message. x Messages are filtered by repeatedly applying a message handler to the
greatest remaining slice, whereas slices are generated in the sequence
``[0, size)``, ``[0, size-1)``, ``...``, ``[1, size-1)``, ``...``,
``[size-1, size)``. Whenever a slice is matched, it is removed from the message
and the next slice starts at the same index on the reduced message.
For example:
.. code-block:: C++
auto msg = make_message(1, 2.f, 3.f, 4);
// remove float and integer pairs
auto msg2 = msg.extract({
[](float, float) { },
[](int, int) { }
});
assert(msg2 == make_message(1, 4));
Step-by-step explanation:
* Slice 1: ``(1, 2.f, 3.f, 4)``, no match
* Slice 2: ``(1, 2.f, 3.f)``, no match
* Slice 3: ``(1, 2.f)``, no match
* Slice 4: ``(1)``, no match
* Slice 5: ``(2.f, 3.f, 4)``, no match
* Slice 6: ``(2.f, 3.f)``, *match*; new message is ``(1, 4)``
* Slice 7: ``(4)``, no match
Slice 7 is ``(4)``, i.e., does not contain the first element, because
the match on slice 6 occurred at index position 1. The function
``extract`` iterates a message only once, from left to right. The
returned message contains the remaining, i.e., unmatched, elements.
.. _extract-opts:
Extracting Command Line Options
-------------------------------
The class ``message`` also contains a convenience interface to
``extract`` for parsing command line options: the member function
``extract_opts``.
.. code-block:: C++
int main(int argc, char** argv) {
uint16_t port;
string host = "localhost";
auto res = message_builder(argv + 1, argv + argc).extract_opts({
{"port,p", "set port", port},
{"host,H", "set host (default: localhost)", host},
{"verbose,v", "enable verbose mode"}
});
if (! res.error.empty()) {
// read invalid CLI arguments
cerr << res.error << endl;
return 1;
}
if (res.opts.count("help") > 0) {
// CLI arguments contained "-h", "--help", or "-?" (builtin);
cout << res.helptext << endl;
return 0;
}
if (! res.remainder.empty()) {
// res.remainder stors all extra arguments that weren't consumed
}
if (res.opts.count("verbose") > 0) {
// enable verbose mode
}
// ...
}
/*
Output of ./program_name -h:
Allowed options:
-p [--port] arg : set port
-H [--host] arg : set host (default: localhost)
-v [--verbose] : enable verbose mode
*/
\section{Migration Guides} Migration Guides
================
The guides in this section document all possibly breaking changes in the The guides in this section document all possibly breaking changes in the
library for that last versions of CAF. library for that last versions of CAF.
\subsection{0.8 to 0.9} 0.8 to 0.9
----------
Version 0.9 included a lot of changes and improvements in its implementation, Version 0.9 included a lot of changes and improvements in its implementation,
but it also made breaking changes to the API. but it also made breaking changes to the API.
\paragraph{\lstinline^self^ has been removed} ``self`` has been removed
+++++++++++++++++++++++++
~
This is the biggest library change since the initial release. The major problem This is the biggest library change since the initial release. The major problem
with this keyword-like identifier is that it must have a single type as it's with this keyword-like identifier is that it must have a single type as it's
implemented as a thread-local variable. Since there are so many different kinds implemented as a thread-local variable. Since there are so many different kinds
of actors (event-based or blocking, untyped or typed), \lstinline^self^ needs of actors (event-based or blocking, untyped or typed), ``self`` needs
to perform type erasure at some point, rendering it ultimately useless. Instead to perform type erasure at some point, rendering it ultimately useless. Instead
of a thread-local pointer, you can now use the first argument in functor-based of a thread-local pointer, you can now use the first argument in functor-based
actors to "catch" the self pointer with proper type information. actors to "catch" the self pointer with proper type information.
\paragraph{\lstinline^actor_ptr^ has been replaced} ``actor_ptr`` has been replaced
+++++++++++++++++++++++++++++++
~
CAF now distinguishes between handles to actors, i.e., CAF now distinguishes between handles to actors, i.e.,
\lstinline^typed_actor<...>^ or simply \lstinline^actor^, and \emph{addresses} ``typed_actor<...>`` or simply ``actor``, and *addresses*
of actors, i.e., \lstinline^actor_addr^. The reason for this change is that of actors, i.e., ``actor_addr``. The reason for this change is that
each actor has a logical, (network-wide) unique address, which is used by the each actor has a logical, (network-wide) unique address, which is used by the
networking layer of CAF. Furthermore, for monitoring or linking, the address networking layer of CAF. Furthermore, for monitoring or linking, the address
is all you need. However, the address is not sufficient for sending messages, is all you need. However, the address is not sufficient for sending messages,
because it doesn't have any type information. The function because it doesn't have any type information. The function
\lstinline^current_sender()^ now returns the \emph{address} of the sender. This ``current_sender()`` now returns the *address* of the sender. This
means that previously valid code such as \lstinline^send(current_sender(),...)^ means that previously valid code such as ``send(current_sender(),...)``
will cause a compiler error. However, the recommended way of replying to will cause a compiler error. However, the recommended way of replying to
messages is to return the result from the message handler. messages is to return the result from the message handler.
\paragraph{The API for typed actors is now similar to the API for untyped actors} The API for typed actors is now similar to the API for untyped actors
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
~
The APIs of typed and untyped actors have been harmonized. Typed actors can now The APIs of typed and untyped actors have been harmonized. Typed actors can now
be published in the network and also use all operations untyped actors can. be published in the network and also use all operations untyped actors can.
\clearpage 0.9 to 0.10 (``libcppa`` to CAF)
\subsection{0.9 to 0.10 (\texttt{libcppa} to CAF)} --------------------------------
The first release under the new name CAF is an overhaul of the entire library. The first release under the new name CAF is an overhaul of the entire library.
Some classes have been renamed or relocated, others have been removed. The Some classes have been renamed or relocated, others have been removed. The
purpose of this refactoring was to make the library easier to grasp and to make purpose of this refactoring was to make the library easier to grasp and to make
its API more consistent. All classes now live in the namespace \texttt{caf} and its API more consistent. All classes now live in the namespace ``caf`` and
all headers have the top level folder \texttt{caf} instead of \texttt{cppa}. all headers have the top level folder ``caf`` instead of ``cppa``.
For example, \texttt{cppa/actor.hpp} becomes \texttt{caf/actor.hpp}. Further, For example, ``cppa/actor.hpp`` becomes ``caf/actor.hpp``. Further,
the convenience header to get all parts of the user API is now the convenience header to get all parts of the user API is now
\texttt{"caf/all.hpp"}. The networking has been separated from the core ``"caf/all.hpp"``. The networking has been separated from the core
library. To get the networking components, simply include library. To get the networking components, simply include
\texttt{caf/io/all.hpp} and use the namespace \lstinline^caf::io^. ``caf/io/all.hpp`` and use the namespace ``caf::io``.
Version 0.10 still includes the header \texttt{cppa/cppa.hpp} to make the Version 0.10 still includes the header ``cppa/cppa.hpp`` to make the
transition process for users easier and to not break existing code right away. transition process for users easier and to not break existing code right away.
The header defines the namespace \texttt{cppa} as an alias for \texttt{caf}. The header defines the namespace ``cppa`` as an alias for ``caf``.
Furthermore, it provides implementations or type aliases for renamed or removed Furthermore, it provides implementations or type aliases for renamed or removed
classes such as \lstinline^cow_tuple^. You won't get any warning about deprecated classes such as ``cow_tuple``. You won't get any warning about deprecated
headers with 0.10. However, we will add this warnings in the next library headers with 0.10. However, we will add this warnings in the next library
version and remove deprecated code eventually. version and remove deprecated code eventually.
...@@ -73,105 +72,102 @@ the library but a whole lot of code that is hard to maintain in the long run ...@@ -73,105 +72,102 @@ the library but a whole lot of code that is hard to maintain in the long run
due to its complexity. Using projections to not only perform type conversions due to its complexity. Using projections to not only perform type conversions
but also to restrict values is the more natural choice. but also to restrict values is the more natural choice.
\lstinline^any_tuple => message^ ``any_tuple => message``
This type is only being used to pass a message from one actor to another. This type is only being used to pass a message from one actor to another.
Hence, \lstinline^message^ is the logical name. Hence, ``message`` is the logical name.
\lstinline^partial_function => message_handler^ ``partial_function => message_handler``
Technically, it still is a partial function. However, we wanted to put Technically, it still is a partial function. However, we wanted to put
emphasize on its use case. emphasize on its use case.
\lstinline^cow_tuple => X^ ``cow_tuple => X``
We want to provide a streamlined, simple API. Shipping a full tuple abstraction We want to provide a streamlined, simple API. Shipping a full tuple abstraction
with the library does not fit into this philosophy. The removal of with the library does not fit into this philosophy. The removal of
\lstinline^cow_tuple^ implies the removal of related functions such as ``cow_tuple`` implies the removal of related functions such as
\lstinline^tuple_cast^. ``tuple_cast``.
\lstinline^cow_ptr => X^ ``cow_ptr => X``
This pointer class is an implementation detail of \lstinline^message^ and This pointer class is an implementation detail of ``message`` and
should not live in the global namespace in the first place. It also had the should not live in the global namespace in the first place. It also had the
wrong name, because it is intrusive. wrong name, because it is intrusive.
\lstinline^X => message_builder^ ``X => message_builder``
This new class can be used to create messages dynamically. For example, the This new class can be used to create messages dynamically. For example, the
content of a vector can be used to create a message using a series of content of a vector can be used to create a message using a series of
\lstinline^append^ calls. ``append`` calls.
.. code-block:: C++
\begin{lstlisting} accept_handle, connection_handle, publish, remote_actor,
accept_handle, connection_handle, publish, remote_actor, max_msg_size, typed_publish, typed_remote_actor, publish_local_groups,
max_msg_size, typed_publish, typed_remote_actor, publish_local_groups, new_connection_msg, new_data_msg, connection_closed_msg, acceptor_closed_msg
new_connection_msg, new_data_msg, connection_closed_msg, acceptor_closed_msg
\end{lstlisting}
These classes concern I/O functionality and have thus been moved to These classes concern I/O functionality and have thus been moved to
\lstinline^caf::io^ ``caf::io``
\subsection{0.10 to 0.11} 0.10 to 0.11
------------
Version 0.11 introduced new, optional components. The core library itself, Version 0.11 introduced new, optional components. The core library itself,
however, mainly received optimizations and bugfixes with one exception: the however, mainly received optimizations and bugfixes with one exception: the
member function \lstinline^on_exit^ is no longer virtual. You can still provide member function ``on_exit`` is no longer virtual. You can still provide
it to define a custom exit handler, but you must not use \lstinline^override^. it to define a custom exit handler, but you must not use ``override``.
\subsection{0.11 to 0.12} 0.11 to 0.12
------------
Version 0.12 removed two features: Version 0.12 removed two features:
\begin{itemize} * Type names are no longer demangled automatically. Hence, users must explicitly pass the type name as first argument when using ``announce``, i.e., ``announce<my_class>(...)`` becomes ``announce<my_class>("my_class", ...)``.
\item Type names are no longer demangled automatically. Hence, users must * Synchronous send blocks no longer support ``continue_with``. This feature has been removed without substitution.
explicitly pass the type name as first argument when using
\lstinline^announce^, i.e., \lstinline^announce<my_class>(...)^ becomes
\lstinline^announce<my_class>("my_class", ...)^.
\item Synchronous send blocks no longer support \lstinline^continue_with^. This
feature has been removed without substitution.
\end{itemize}
\subsection{0.12 to 0.13} 0.12 to 0.13
------------
This release removes the (since 0.9 deprecated) \lstinline^cppa^ headers and This release removes the (since 0.9 deprecated) ``cppa`` headers and
deprecates all \lstinline^*_send_tuple^ versions (simply use the function deprecates all ``*_send_tuple`` versions (simply use the function
without \lstinline^_tuple^ suffix). \lstinline^local_actor::on_exit^ once again without ``_tuple`` suffix). ``local_actor::on_exit`` once again
became virtual. became virtual.
In case you were using the old \lstinline^cppa::options_description^ API, you In case you were using the old ``cppa::options_description`` API, you can
can migrate to the new API based on \lstinline^extract^ \see{extract-opts}. migrate to the new API based on ``extract`` (see :ref:`extract-opts`).
Most importantly, version 0.13 slightly changes \lstinline^last_dequeued^ and Most importantly, version 0.13 slightly changes ``last_dequeued`` and
\lstinline^last_sender^. Both functions will now cause undefined behavior ``last_sender``. Both functions will now cause undefined behavior (dereferencing
(dereferencing a \lstinline^nullptr^) instead of returning dummy values when a ``nullptr``) instead of returning dummy values when accessed from outside a
accessed from outside a callback or after forwarding the current message. callback or after forwarding the current message. Besides, these function names
Besides, these function names were not a good choice in the first place, since were not a good choice in the first place, since "last" implies accessing data
``last'' implies accessing data received in the past. As a result, both received in the past. As a result, both functions are now deprecated. Their
functions are now deprecated. Their replacements are named replacements are named ``current_message`` and ``current_sender`` (see
\lstinline^current_message^ and \lstinline^current_sender^ \see{interface}. :ref:`interface`).
\subsection{0.13 to 0.14} 0.13 to 0.14
------------
The function \lstinline^timed_sync_send^ has been removed. It offered an The function ``timed_sync_send`` has been removed. It offered an
alternative way of defining message handlers, which is inconsistent with the alternative way of defining message handlers, which is inconsistent with the
rest of the API. rest of the API.
The policy classes \lstinline^broadcast^, \lstinline^random^, and The policy classes ``broadcast``, ``random``, and
\lstinline^round_robin^ in \lstinline^actor_pool^ were removed and replaced by ``round_robin`` in ``actor_pool`` were removed and replaced by
factory functions using the same name. factory functions using the same name.
\clearpage 0.14 to 0.15
\subsection{0.14 to 0.15} ------------
Version 0.15 replaces the singleton-based architecture with Version 0.15 replaces the singleton-based architecture with ``actor_system``.
\lstinline^actor_system^. Most of the free functions in namespace Most of the free functions in namespace ``caf`` are now member functions of
\lstinline^caf^ are now member functions of \lstinline^actor_system^ ``actor_system`` (see :ref:`actor-system`). Likewise, most functions in
\see{actor-system}. Likewise, most functions in namespace \lstinline^caf::io^ namespace ``caf::io`` are now member functions of ``middleman`` (see
are now member functions of \lstinline^middleman^ \see{middleman}. The :ref:`middleman`). The structure of CAF applications has changed fundamentally
structure of CAF applications has changed fundamentally with a focus on with a focus on configurability. Setting and fine-tuning the scheduler, changing
configurability. Setting and fine-tuning the scheduler, changing parameters of parameters of the middleman, etc. is now bundled in the class
the middleman, etc. is now bundled in the class ``actor_system_config``. The new configuration mechanism is also easily
\lstinline^actor_system_config^. The new configuration mechanism is also easily
extensible. extensible.
Patterns are now limited to the simple notation, because the advanced features Patterns are now limited to the simple notation, because the advanced features
...@@ -179,5 +175,5 @@ Patterns are now limited to the simple notation, because the advanced features ...@@ -179,5 +175,5 @@ Patterns are now limited to the simple notation, because the advanced features
Windows/MSVC, and (3) drastically impact compile times. Dropping this Windows/MSVC, and (3) drastically impact compile times. Dropping this
functionality also simplifies the implementation and improves performance. functionality also simplifies the implementation and improves performance.
The \lstinline^blocking_api^ flag has been removed. All variants of The ``blocking_api`` flag has been removed. All variants of
\emph{spawn} now auto-detect blocking actors. *spawn* now auto-detect blocking actors.
.. _middleman:
Middleman
=========
The middleman is the main component of the I/O module and enables distribution.
It transparently manages proxy actor instances representing remote actors,
maintains connections to other nodes, and takes care of serialization of
messages. Applications install a middleman by loading ``caf::io::middleman`` as
module (see :ref:`system-config`). Users can include ``"caf/io/all.hpp"`` to get
access to all public classes of the I/O module.
Class ``middleman``
-------------------
+---------------------------------------------------------------+----------------------+
| **Remoting** | |
+---------------------------------------------------------------+----------------------+
| ``expected<uint16> open(uint16, const char*, bool)`` | See :ref:`remoting`. |
+---------------------------------------------------------------+----------------------+
| ``expected<uint16> publish(T, uint16, const char*, bool)`` | See :ref:`remoting`. |
+---------------------------------------------------------------+----------------------+
| ``expected<void> unpublish(T x, uint16)`` | See :ref:`remoting`. |
+---------------------------------------------------------------+----------------------+
| ``expected<node_id> connect(std::string host, uint16_t port)``| See :ref:`remoting`. |
+---------------------------------------------------------------+----------------------+
| ``expected<T> remote_actor<T = actor>(string, uint16)`` | See :ref:`remoting`. |
+---------------------------------------------------------------+----------------------+
| ``expected<T> spawn_broker(F fun, ...)`` | See :ref:`broker`. |
+---------------------------------------------------------------+----------------------+
| ``expected<T> spawn_client(F, string, uint16, ...)`` | See :ref:`broker`. |
+---------------------------------------------------------------+----------------------+
| ``expected<T> spawn_server(F, uint16, ...)`` | See :ref:`broker`. |
+---------------------------------------------------------------+----------------------+
.. _remoting:
Publishing and Connecting
-------------------------
The member function ``publish`` binds an actor to a given port, thereby
allowing other nodes to access it over the network.
.. code-block:: C++
template <class T>
expected<uint16_t> middleman::publish(T x, uint16_t port,
const char* in = nullptr,
bool reuse_addr = false);
The first argument is a handle of type ``actor`` or
``typed_actor<...>``. The second argument denotes the TCP port. The OS
will pick a random high-level port when passing 0. The third parameter
configures the listening address. Passing null will accept all incoming
connections (``INADDR_ANY``). Finally, the flag ``reuse_addr``
controls the behavior when binding an IP address to a port, with the same
semantics as the BSD socket flag ``SO_REUSEADDR``. For example, with
``reuse_addr = false``, binding two sockets to 0.0.0.0:42 and
10.0.0.1:42 will fail with ``EADDRINUSE`` since 0.0.0.0 includes 10.0.0.1.
With ``reuse_addr = true`` binding would succeed because 10.0.0.1 and
0.0.0.0 are not literally equal addresses.
The member function returns the bound port on success. Otherwise, an ``error``
(see :ref:`error`) is returned.
.. code-block:: C++
template <class T>
expected<uint16_t> middleman::unpublish(T x, uint16_t port = 0);
The member function ``unpublish`` allows actors to close a port
manually. This is performed automatically if the published actor terminates.
Passing 0 as second argument closes all ports an actor is published to,
otherwise only one specific port is closed.
The function returns an ``error`` (see :ref:`error`) if the actor was not bound
to given port.
.. code-block:: C++
template<class T = actor>
expected<T> middleman::remote_actor(std::string host, uint16_t port);
After a server has published an actor with ``publish``, clients can
connect to the published actor by calling ``remote_actor``:
.. code-block:: C++
// node A
auto ping = spawn(ping);
system.middleman().publish(ping, 4242);
// node B
auto ping = system.middleman().remote_actor("node A", 4242);
if (! ping) {
cerr << "unable to connect to node A: "
<< system.render(ping.error()) << std::endl;
} else {
self->send(*ping, ping_atom::value);
}
There is no difference between server and client after the connection phase.
Remote actors use the same handle types as local actors and are thus fully
transparent.
The function pair ``open`` and ``connect`` allows users to connect CAF instances
without remote actor setup. The function ``connect`` returns a ``node_id`` that
can be used for remote spawning (see (see :ref:`remote-spawn`)).
.. _free-remoting-functions:
Free Functions
--------------
The following free functions in the namespace ``caf::io`` avoid calling
the middleman directly. This enables users to easily switch between
communication backends as long as the interfaces have the same signatures. For
example, the (experimental) OpenSSL binding of CAF implements the same
functions in the namespace ``caf::openssl`` to easily switch between
encrypted and unencrypted communication.
+------------------------------------------------------------------------------+----------------------+
| ``expected<uint16> open(actor_system&, uint16, const char*, bool)`` | See :ref:`remoting`. |
+------------------------------------------------------------------------------+----------------------+
| ``expected<uint16> publish(T, uint16, const char*, bool)`` | See :ref:`remoting`. |
+------------------------------------------------------------------------------+----------------------+
| ``expected<void> unpublish(T x, uint16)`` | See :ref:`remoting`. |
+------------------------------------------------------------------------------+----------------------+
| ``expected<node_id> connect(actor_system&, std::string host, uint16_t port)``| See :ref:`remoting`. |
+------------------------------------------------------------------------------+----------------------+
| ``expected<T> remote_actor<T = actor>(actor_system&, string, uint16)`` | See :ref:`remoting`. |
+------------------------------------------------------------------------------+----------------------+
.. _transport-protocols:
Transport Protocols :sup:`experimental`
----------------------------------------
CAF communication uses TCP per default and thus the functions shown in the
middleman API above are related to TCP. There are two alternatives to plain TCP:
TLS via the OpenSSL module shortly discussed in (see
:ref:`free-remoting-functions`) and UDP.
UDP is integrated in the default multiplexer and BASP broker. Set the flag
``middleman_enable_udp`` to true to enable it (see :ref:`system-config`). This
does not require you to disable TCP. Use ``publish_udp`` and
``remote_actor_udp`` to establish communication.
Communication via UDP is inherently unreliable and unordered. CAF reestablishes
order and drops messages that arrive late. Messages that are sent via datagrams
are limited to a maximum of 65.535 bytes which is used as a receive buffer size
by CAF. Note that messages that exceed the MTU are fragmented by IP and are
considered lost if a single fragment is lost. Optional reliability based on
retransmissions and messages slicing on the application layer are planned for
the future.
Overview
========
Compiling CAF requires CMake and a C++11-compatible compiler. To get and
compile the sources on UNIX-like systems, type the following in a terminal:
.. ::
git clone https://github.com/actor-framework/actor-framework
cd actor-framework
./configure
make
make install [as root, optional]
We recommended to run the unit tests as well:
.. ::
make test
If the output indicates an error, please submit a bug report that includes (a)
your compiler version, (b) your OS, and (c) the content of the file
``build/Testing/Temporary/LastTest.log``.
Features
--------
* Lightweight, fast and efficient actor implementations
* Network transparent messaging
* Error handling based on Erlang's failure model
* Pattern matching for messages as internal DSL to ease development
* Thread-mapped actors for soft migration of existing applications
* Publish/subscribe group communication
Minimal Compiler Versions
-------------------------
* GCC 4.8
* Clang 3.4
* Visual Studio 2015, Update 3
Supported Operating Systems
---------------------------
* Linux
* Mac OS X
* Windows (static library only)
Hello World Example
-------------------
.. literalinclude:: /examples/hello_world.cpp
:language: C++
\section{Reference Counting} .. _reference-counting:
\label{reference-counting}
Reference Counting
==================
Actors systems can span complex communication graphs that make it hard to Actors systems can span complex communication graphs that make it hard to
decide when actors are no longer needed. As a result, manually managing decide when actors are no longer needed. As a result, manually managing
...@@ -7,13 +9,14 @@ lifetime of actors is merely impossible. For this reason, CAF implements a ...@@ -7,13 +9,14 @@ lifetime of actors is merely impossible. For this reason, CAF implements a
garbage collection strategy for actors based on weak and strong reference garbage collection strategy for actors based on weak and strong reference
counts. counts.
\subsection{Shared Ownership in C++} Shared Ownership in C++
-----------------------
The C++ standard library already offers \lstinline^shared_ptr^ and The C++ standard library already offers ``shared_ptr`` and
\lstinline^weak_ptr^ to manage objects with complex shared ownership. The ``weak_ptr`` to manage objects with complex shared ownership. The
standard implementation is a solid general purpose design that covers most use standard implementation is a solid general purpose design that covers most use
cases. Weak and strong references to an object are stored in a \emph{control cases. Weak and strong references to an object are stored in a *control
block}. However, CAF uses a slightly different design. The reason for this is block*. However, CAF uses a slightly different design. The reason for this is
twofold. First, we need the control block to store the identity of an actor. twofold. First, we need the control block to store the identity of an actor.
Second, we wanted a design that requires less indirections, because actor Second, we wanted a design that requires less indirections, because actor
handles are used extensively copied for messaging, and this overhead adds up. handles are used extensively copied for messaging, and this overhead adds up.
...@@ -21,126 +24,129 @@ handles are used extensively copied for messaging, and this overhead adds up. ...@@ -21,126 +24,129 @@ handles are used extensively copied for messaging, and this overhead adds up.
Before discussing the approach to shared ownership in CAF, we look at the Before discussing the approach to shared ownership in CAF, we look at the
design of shared pointers in the C++ standard library. design of shared pointers in the C++ standard library.
\singlefig{shared_ptr}{Shared pointer design in the C++ standard library}{shared-ptr} .. _shared-ptr:
.. image:: shared_ptr.png
:alt: Shared pointer design in the C++ standard library
The figure above depicts the default memory layout when using shared pointers. The figure above depicts the default memory layout when using shared pointers.
The control block is allocated separately from the data and thus stores a The control block is allocated separately from the data and thus stores a
pointer to the data. This is when using manually-allocated objects, for example pointer to the data. This is when using manually-allocated objects, for example
\lstinline^shared_ptr<int> iptr{new int}^. The benefit of this design is that ``shared_ptr<int> iptr{new int}``. The benefit of this design is that
one can destroy \lstinline^T^ independently from its control block. While one can destroy ``T`` independently from its control block. While
irrelevant for small objects, it can become an issue for large objects. irrelevant for small objects, it can become an issue for large objects.
Notably, the shared pointer stores two pointers internally. Otherwise, Notably, the shared pointer stores two pointers internally. Otherwise,
dereferencing it would require to get the data location from the control block dereferencing it would require to get the data location from the control block
first. first.
\singlefig{make_shared}{Memory layout when using \lstinline^std::make_shared^}{make-shared} .. _make-shared:
.. image:: make_shared.png
:alt: Memory layout when using ``std::make_shared``
When using \lstinline^make_shared^ or \lstinline^allocate_shared^, the standard When using ``make_shared`` or ``allocate_shared``, the standard
library can store reference count and data in a single memory block as shown library can store reference count and data in a single memory block as shown
above. However, \lstinline^shared_ptr^ still has to store two pointers, because above. However, ``shared_ptr`` still has to store two pointers, because
it is unaware where the data is allocated. it is unaware where the data is allocated.
\singlefig{enable_shared_from_this}{Memory layout with \lstinline^std::enable_shared_from_this^}{enable-shared-from-this} .. _enable-shared-from-this:
.. image:: enable_shared_from_this.png
:alt: Memory layout with ``std::enable_shared_from_this``
Finally, the design of the standard library becomes convoluted when an object Finally, the design of the standard library becomes convoluted when an object
should be able to hand out a \lstinline^shared_ptr^ to itself. Classes must should be able to hand out a ``shared_ptr`` to itself. Classes must
inherit from \lstinline^std::enable_shared_from_this^ to navigate from an inherit from ``std::enable_shared_from_this`` to navigate from an
object to its control block. This additional navigation path is required, object to its control block. This additional navigation path is required,
because \lstinline^std::shared_ptr^ needs two pointers. One to the data and one because ``std::shared_ptr`` needs two pointers. One to the data and one
to the control block. Programmers can still use \lstinline^make_shared^ for to the control block. Programmers can still use ``make_shared`` for
such objects, in which case the object is again stored along with the control such objects, in which case the object is again stored along with the control
block. block.
\subsection{Smart Pointers to Actors} Smart Pointers to Actors
------------------------
In CAF, we use a different approach than the standard library because (1) we In CAF, we use a different approach than the standard library because (1) we
always allocate actors along with their control block, (2) we need additional always allocate actors along with their control block, (2) we need additional
information in the control block, and (3) we can store only a single raw information in the control block, and (3) we can store only a single raw
pointer internally instead of the two raw pointers \lstinline^std::shared_ptr^ pointer internally instead of the two raw pointers ``std::shared_ptr``
needs. The following figure summarizes the design of smart pointers to actors. needs. The following figure summarizes the design of smart pointers to actors.
\singlefig{refcounting}{Shared pointer design in CAF}{actor-pointer} .. image:: refcounting.png
:alt: Shared pointer design in CAF
CAF uses \lstinline^strong_actor_ptr^ instead of CAF uses ``strong_actor_ptr`` instead of
\lstinline^std::shared_ptr<...>^ and \lstinline^weak_actor_ptr^ instead of ``std::shared_ptr<...>`` and ``weak_actor_ptr`` instead of
\lstinline^std::weak_ptr<...>^. Unlike the counterparts from the standard ``std::weak_ptr<...>``. Unlike the counterparts from the standard
library, both smart pointer types only store a single pointer. library, both smart pointer types only store a single pointer.
Also, the control block in CAF is not a template and stores the identity of an Also, the control block in CAF is not a template and stores the identity of an
actor (\lstinline^actor_id^ plus \lstinline^node_id^). This allows CAF to actor (``actor_id`` plus ``node_id``). This allows CAF to
access this information even after an actor died. The control block fits access this information even after an actor died. The control block fits
exactly into a single cache line (64 Bytes). This makes sure no \emph{false exactly into a single cache line (64 Bytes). This makes sure no *false
sharing} occurs between an actor and other actors that have references to it. sharing* occurs between an actor and other actors that have references to it.
Since the size of the control block is fixed and CAF \emph{guarantees} the Since the size of the control block is fixed and CAF *guarantees* the
memory layout enforced by \lstinline^actor_storage^, CAF can compute the memory layout enforced by ``actor_storage``, CAF can compute the
address of an actor from the pointer to its control block by offsetting it by address of an actor from the pointer to its control block by offsetting it by
64 Bytes. Likewise, an actor can compute the address of its control block. 64 Bytes. Likewise, an actor can compute the address of its control block.
The smart pointer design in CAF relies on a few assumptions about actor types. The smart pointer design in CAF relies on a few assumptions about actor types.
Most notably, the actor object is placed 64 Bytes after the control block. This Most notably, the actor object is placed 64 Bytes after the control block. This
starting address is cast to \lstinline^abstract_actor*^. Hence, \lstinline^T*^ starting address is cast to ``abstract_actor*``. Hence, ``T*``
must be convertible to \lstinline^abstract_actor*^ via must be convertible to ``abstract_actor*`` via
\lstinline^reinterpret_cast^. In practice, this means actor subclasses must not ``reinterpret_cast``. In practice, this means actor subclasses must not
use virtual inheritance, which is enforced in CAF with a use virtual inheritance, which is enforced in CAF with a
\lstinline^static_assert^. ``static_assert``.
\subsection{Strong and Weak References} Strong and Weak References
--------------------------
A \emph{strong} reference manipulates the \lstinline^strong refs^ counter as
shown above. An actor is destroyed if there are \emph{zero} strong references A *strong* reference manipulates the ``strong refs`` counter as shown above. An
to it. If two actors keep strong references to each other via member variable, actor is destroyed if there are *zero* strong references to it. If two actors
neither actor can ever be destroyed because they produce a cycle keep strong references to each other via member variable, neither actor can ever
\see{breaking-cycles}. Strong references are formed by be destroyed because they produce a cycle (see :ref:`breaking-cycles`). Strong
\lstinline^strong_actor_ptr^, \lstinline^actor^, and references are formed by ``strong_actor_ptr``, ``actor``, and
\lstinline^typed_actor<...>^ \see{actor-reference}. ``typed_actor<...>`` (see :ref:`actor-reference`).
A \emph{weak} reference manipulates the \lstinline^weak refs^ counter. This A *weak* reference manipulates the ``weak refs`` counter. This counter keeps
counter keeps track of how many references to the control block exist. The track of how many references to the control block exist. The control block is
control block is destroyed if there are \emph{zero} weak references to an actor destroyed if there are *zero* weak references to an actor (which cannot occur
(which cannot occur before \lstinline^strong refs^ reached \emph{zero} as before ``strong refs`` reached *zero* as well). No cycle occurs if two actors
well). No cycle occurs if two actors keep weak references to each other, keep weak references to each other, because the actor objects themselves can get
because the actor objects themselves can get destroyed independently from their destroyed independently from their control block. A weak reference is only
control block. A weak reference is only formed by \lstinline^actor_addr^ formed by ``actor_addr`` (see :ref:`actor-address`).
\see{actor-address}.
.. _actor-cast:
\subsection{Converting Actor References with \lstinline^actor_cast^}
\label{actor-cast} Converting Actor References with ``actor_cast``
-----------------------------------------------
The function \lstinline^actor_cast^ converts between actor pointers and
handles. The first common use case is to convert a \lstinline^strong_actor_ptr^ The function ``actor_cast`` converts between actor pointers and
to either \lstinline^actor^ or \lstinline^typed_actor<...>^ before being able handles. The first common use case is to convert a ``strong_actor_ptr``
to either ``actor`` or ``typed_actor<...>`` before being able
to send messages to an actor. The second common use case is to convert to send messages to an actor. The second common use case is to convert
\lstinline^actor_addr^ to \lstinline^strong_actor_ptr^ to upgrade a weak ``actor_addr`` to ``strong_actor_ptr`` to upgrade a weak
reference to a strong reference. Note that casting \lstinline^actor_addr^ to a reference to a strong reference. Note that casting ``actor_addr`` to a
strong actor pointer or handle can result in invalid handles. The syntax for strong actor pointer or handle can result in invalid handles. The syntax for
\lstinline^actor_cast^ resembles builtin C++ casts. For example, ``actor_cast`` resembles builtin C++ casts. For example,
\lstinline^actor_cast<actor>(x)^ converts \lstinline^x^ to an handle of type ``actor_cast<actor>(x)`` converts ``x`` to an handle of type
\lstinline^actor^. ``actor``.
.. _breaking-cycles:
\subsection{Breaking Cycles Manually} Breaking Cycles Manually
\label{breaking-cycles} ------------------------
Cycles can occur only when using class-based actors when storing references to Cycles can occur only when using class-based actors when storing references to
other actors via member variable. Stateful actors \see{stateful-actor} break other actors via member variable. Stateful actors (see :ref:`stateful-actor`)
cycles by destroying the state when an actor terminates, \emph{before} the break cycles by destroying the state when an actor terminates, *before* the
destructor of the actor itself runs. This means an actor releases all destructor of the actor itself runs. This means an actor releases all references
references to others automatically after calling \lstinline^quit^. However, to others automatically after calling ``quit``. However, class-based actors have
class-based actors have to break cycles manually, because references to others to break cycles manually, because references to others are not released until
are not released until the destructor of an actor runs. Two actors storing the destructor of an actor runs. Two actors storing references to each other via
references to each other via member variable produce a cycle and neither member variable produce a cycle and neither destructor can ever be called.
destructor can ever be called.
Class-based actors can break cycles manually by overriding ``on_exit()`` and
Class-based actors can break cycles manually by overriding calling ``destroy(x)`` on each handle (see :ref:`actor-handle`). Using a handle
\lstinline^on_exit()^ and calling \lstinline^destroy(x)^ on each after destroying it is undefined behavior, but it is safe to assign a new value
handle~\see{actor-handle}. Using a handle after destroying it is undefined to the handle.
behavior, but it is safe to assign a new value to the handle.
%TODO: Add use case for the following casting scenario. There is one
%requirement of this design: `static_cast<abstract_actor*>(self)` must return
%the same pointer as `reinterpret_cast<abstract_actor*>(self)` for any actor
%`self`. Otherwise, our assumption that the actor object starts exactly 64
%Bytes after its control block would break. Luckily, this boils down to a
%single limitation in practice: User-defined actors must not use virtual
%inheritance. When trying to spawn actors that do make use of virtual
%inheritance, CAF generates a compile-time error: `"actor subtype has illegal
%memory alignment (probably due to virtual inheritance)"`.
.. _registry:
Registry
========
The actor registry in CAF keeps track of the number of running actors and allows
to map actors to their ID or a custom atom (see :ref:`atom`) representing a
name. The registry does *not* contain all actors. Actors have to be stored in
the registry explicitly. Users can access the registry through an actor system
by calling ``system.registry()``. The registry stores actors using
``strong_actor_ptr`` (see :ref:`actor-pointer`).
Users can use the registry to make actors system-wide available by name. The
:ref:`middleman` uses the registry to keep track of all actors known to remote
nodes in order to serialize and deserialize them. Actors are removed
automatically when they terminate.
It is worth mentioning that the registry is not synchronized between connected
actor system. Each actor system has its own, local registry in a distributed
setting.
+-------------------------------------------+-------------------------------------------------+
| **Types** | |
+-------------------------------------------+-------------------------------------------------+
| ``name_map`` | ``unordered_map<atom_value, strong_actor_ptr>`` |
+-------------------------------------------+-------------------------------------------------+
| | |
+-------------------------------------------+-------------------------------------------------+
| **Observers** | |
+-------------------------------------------+-------------------------------------------------+
| ``strong_actor_ptr get(actor_id)`` | Returns the actor associated to given ID. |
+-------------------------------------------+-------------------------------------------------+
| ``strong_actor_ptr get(atom_value)`` | Returns the actor associated to given name. |
+-------------------------------------------+-------------------------------------------------+
| ``name_map named_actors()`` | Returns all name mappings. |
+-------------------------------------------+-------------------------------------------------+
| ``size_t running()`` | Returns the number of currently running actors. |
+-------------------------------------------+-------------------------------------------------+
| | |
+-------------------------------------------+-------------------------------------------------+
| **Modifiers** | |
+-------------------------------------------+-------------------------------------------------+
| ``void put(actor_id, strong_actor_ptr)`` | Maps an actor to its ID. |
+-------------------------------------------+-------------------------------------------------+
| ``void erase(actor_id)`` | Removes an ID mapping from the registry. |
+-------------------------------------------+-------------------------------------------------+
| ``void put(atom_value, strong_actor_ptr)``| Maps an actor to a name. |
+-------------------------------------------+-------------------------------------------------+
| ``void erase(atom_value)`` | Removes a name mapping from the registry. |
+-------------------------------------------+-------------------------------------------------+
.. _remote-spawn:
Remote Spawning of Actors :sup:`experimental`
==============================================
Remote spawning is an extension of the dynamic spawn using run-time type names
(see :ref:`add-custom-actor-type`). The following example assumes a typed actor
handle named ``calculator`` with an actor implementing this messaging interface
named "calculator".
.. literalinclude:: /examples/remoting/remote_spawn.cpp
:language: C++
:lines: 123-137
We first connect to a CAF node with ``middleman().connect(...)``. On success,
``connect`` returns the node ID we need for ``remote_spawn``. This requires the
server to open a port with ``middleman().open(...)`` or
``middleman().publish(...)``. Alternatively, we can obtain the node ID from an
already existing remote actor handle---returned from ``remote_actor`` for
example---via ``hdl->node()``. After connecting to the server, we can use
``middleman().remote_spawn<...>(...)`` to create actors remotely.
\section{Scheduler} .. _scheduler:
\label{scheduler}
Scheduler
=========
The CAF runtime maps N actors to M threads on the local machine. Applications The CAF runtime maps N actors to M threads on the local machine. Applications
build with CAF scale by decomposing tasks into many independent steps that are build with CAF scale by decomposing tasks into many independent steps that are
...@@ -11,14 +13,13 @@ Amdahl's law. ...@@ -11,14 +13,13 @@ Amdahl's law.
Decomposing tasks implies that actors are often short-lived. Assigning a Decomposing tasks implies that actors are often short-lived. Assigning a
dedicated thread to each actor would not scale well. Instead, CAF includes a dedicated thread to each actor would not scale well. Instead, CAF includes a
scheduler that dynamically assigns actors to a pre-dimensioned set of worker scheduler that dynamically assigns actors to a pre-dimensioned set of worker
threads. Actors are modeled as lightweight state machines. Whenever a threads. Actors are modeled as lightweight state machines. Whenever a *waiting*
\emph{waiting} actor receives a message, it changes its state to \emph{ready} actor receives a message, it changes its state to *ready* and is scheduled for
and is scheduled for execution. CAF cannot interrupt running actors because it execution. CAF cannot interrupt running actors because it is implemented in user
is implemented in user space. Consequently, actors that use blocking system space. Consequently, actors that use blocking system calls such as I/O functions
calls such as I/O functions can suspend threads and create an imbalance or lead can suspend threads and create an imbalance or lead to starvation. Such
to starvation. Such ``uncooperative'' actors can be explicitly detached by the "uncooperative" actors can be explicitly detached by the programmer by using the
programmer by using the \lstinline^detached^ spawn option, e.g., ``detached`` spawn option, e.g., ``system.spawn<detached>(my_actor_fun)``.
\lstinline^system.spawn<detached>(my_actor_fun)^.
The performance of actor-based applications depends on the scheduling algorithm The performance of actor-based applications depends on the scheduling algorithm
in use and its configuration. Different application scenarios require different in use and its configuration. Different application scenarios require different
...@@ -33,22 +34,25 @@ or an actor receives a message from a thread that is not under the control of ...@@ -33,22 +34,25 @@ or an actor receives a message from a thread that is not under the control of
the scheduler. Internal events are send and spawn operations from scheduled the scheduler. Internal events are send and spawn operations from scheduled
actors. actors.
\subsection{Policies} .. _scheduler-policy:
\label{scheduler-policy}
Policies
--------
The scheduler consists of a single coordinator and a set of workers. The The scheduler consists of a single coordinator and a set of workers. The
coordinator is needed by the public API to bridge actor and non-actor contexts, coordinator is needed by the public API to bridge actor and non-actor contexts,
but is not necessarily an active software entity. but is not necessarily an active software entity.
The scheduler of CAF is fully customizable by using a policy-based design. The The scheduler of CAF is fully customizable by using a policy-based design. The
following class shows a \emph{concept} class that lists all required member following class shows a *concept* class that lists all required member
types and member functions. A policy provides the two data structures types and member functions. A policy provides the two data structures
\lstinline^coordinator_data^ and \lstinline^worker_data^ that add additional ``coordinator_data`` and ``worker_data`` that add additional
data members to the coordinator and its workers respectively, e.g., work data members to the coordinator and its workers respectively, e.g., work
queues. This grants developers full control over the state of the scheduler. queues. This grants developers full control over the state of the scheduler.
\begin{lstlisting} .. code-block:: C++
struct scheduler_policy {
struct scheduler_policy {
struct coordinator_data; struct coordinator_data;
struct worker_data; struct worker_data;
void central_enqueue(Coordinator* self, resumable* job); void central_enqueue(Coordinator* self, resumable* job);
...@@ -59,47 +63,51 @@ struct scheduler_policy { ...@@ -59,47 +63,51 @@ struct scheduler_policy {
void before_resume(Worker* self, resumable* job); void before_resume(Worker* self, resumable* job);
void after_resume(Worker* self, resumable* job); void after_resume(Worker* self, resumable* job);
void after_completion(Worker* self, resumable* job); void after_completion(Worker* self, resumable* job);
}; };
\end{lstlisting}
Whenever a new work item is scheduled---usually by sending a message to an idle Whenever a new work item is scheduled---usually by sending a message to an idle
actor---, one of the functions \lstinline^central_enqueue^, actor---, one of the functions ``central_enqueue``,
\lstinline^external_enqueue^, and \lstinline^internal_enqueue^ is called. The ``external_enqueue``, and ``internal_enqueue`` is called. The
first function is called whenever non-actor code interacts with the actor first function is called whenever non-actor code interacts with the actor
system. For example when spawning an actor from \lstinline^main^. Its first system. For example when spawning an actor from ``main``. Its first
argument is a pointer to the coordinator singleton and the second argument is argument is a pointer to the coordinator singleton and the second argument is
the new work item---usually an actor that became ready. The function the new work item---usually an actor that became ready. The function
\lstinline^external_enqueue^ is never called directly by CAF. It models the ``external_enqueue`` is never called directly by CAF. It models the
transfer of a task to a worker by the coordinator or another worker. Its first transfer of a task to a worker by the coordinator or another worker. Its first
argument is the worker receiving the new task referenced in the second argument is the worker receiving the new task referenced in the second
argument. The third function, \lstinline^internal_enqueue^, is called whenever argument. The third function, ``internal_enqueue``, is called whenever
an actor interacts with other actors in the system. Its first argument is the an actor interacts with other actors in the system. Its first argument is the
current worker and the second argument is the new work item. current worker and the second argument is the new work item.
Actors reaching the maximum number of messages per run are re-scheduled with Actors reaching the maximum number of messages per run are re-scheduled with
\lstinline^resume_job_later^ and workers acquire new work by calling ``resume_job_later`` and workers acquire new work by calling
\lstinline^dequeue^. The two functions \lstinline^before_resume^ and ``dequeue``. The two functions ``before_resume`` and
\lstinline^after_resume^ allow programmers to measure individual actor runtime, ``after_resume`` allow programmers to measure individual actor runtime,
while \lstinline^after_completion^ allows to execute custom code whenever a while ``after_completion`` allows to execute custom code whenever a
work item has finished execution by changing its state to \emph{done}, but work item has finished execution by changing its state to *done*, but
before it is destroyed. In this way, the last three functions enable developers before it is destroyed. In this way, the last three functions enable developers
to gain fine-grained insight into the scheduling order and individual execution to gain fine-grained insight into the scheduling order and individual execution
times. times.
\subsection{Work Stealing} .. _work-stealing:
\label{work-stealing}
Work Stealing
-------------
The default policy in CAF is work stealing. The key idea of this algorithm is The default policy in CAF is work stealing. The key idea of this algorithm is
to remove the bottleneck of a single, global work queue. The original to remove the bottleneck of a single, global work queue. The original
algorithm was developed for fully strict computations by Blumofe et al in 1994. algorithm was developed for fully strict computations by Blumofe et al in 1994.
It schedules any number of tasks to \lstinline^P^ workers, where \lstinline^P^ It schedules any number of tasks to ``P`` workers, where ``P``
is the number of processors available. is the number of processors available.
\singlefig{stealing}{Stealing of work items}{fig-stealing} .. _fig-stealing:
.. image:: stealing.png
:alt: Stealing of work items
Each worker dequeues work items from an individual queue until it is drained. Each worker dequeues work items from an individual queue until it is drained.
Once this happens, the worker becomes a \emph{thief}. It picks one of the other Once this happens, the worker becomes a *thief*. It picks one of the other
workers---usually at random---as a \emph{victim} and tries to \emph{steal} a workers---usually at random---as a *victim* and tries to *steal* a
work item. As a consequence, tasks (actors) are bound to workers by default and work item. As a consequence, tasks (actors) are bound to workers by default and
only migrate between threads as a result of stealing. This strategy minimizes only migrate between threads as a result of stealing. This strategy minimizes
communication between threads and maximizes cache locality. Work stealing has communication between threads and maximizes cache locality. Work stealing has
...@@ -114,24 +122,24 @@ or all? Since each worker has only local knowledge, it cannot decide when it ...@@ -114,24 +122,24 @@ or all? Since each worker has only local knowledge, it cannot decide when it
could safely suspend itself. Likewise, workers cannot resume if new job items could safely suspend itself. Likewise, workers cannot resume if new job items
arrived at one or more workers. For this reason, CAF uses three polling arrived at one or more workers. For this reason, CAF uses three polling
intervals. Once a worker runs out of work items, it tries to steal items from intervals. Once a worker runs out of work items, it tries to steal items from
others. First, it uses the \emph{aggressive} polling interval. It falls back to others. First, it uses the *aggressive* polling interval. It falls back to
a \emph{moderate} interval after a predefined number of trials. After another a *moderate* interval after a predefined number of trials. After another
predefined number of trials, it will finally use a \emph{relaxed} interval. predefined number of trials, it will finally use a *relaxed* interval.
Per default, the *aggressive* strategy performs 100 steal attempts with no sleep
interval in between. The *moderate* strategy tries to steal 500 times with 50
microseconds sleep between two steal attempts. Finally, the *relaxed* strategy
runs indefinitely but sleeps for 10 milliseconds between two attempts. These
defaults can be overridden via system config at startup (see
:ref:`system-config`).
Per default, the \emph{aggressive} strategy performs 100 steal attempts with no .. _work-sharing:
sleep interval in between. The \emph{moderate} strategy tries to steal 500
times with 50 microseconds sleep between two steal attempts. Finally, the
\emph{relaxed} strategy runs indefinitely but sleeps for 10 milliseconds
between two attempts. These defaults can be overridden via system config at
startup~\see{system-config}.
\subsection{Work Sharing} Work Sharing
\label{work-sharing} ------------
Work sharing is an alternative scheduler policy in CAF that uses a single, Work sharing is an alternative scheduler policy in CAF that uses a single,
global work queue. This policy uses a mutex and a condition variable on the global work queue. This policy uses a mutex and a condition variable on the
central queue. Thus, the policy supports only limited concurrency but does not central queue. Thus, the policy supports only limited concurrency but does not
need to poll. Using this policy can be a good fit for low-end devices where need to poll. Using this policy can be a good fit for low-end devices where
power consumption is an important metric. power consumption is an important metric.
% TODO: profiling section
\section{Streaming\experimental} .. _streaming:
\label{streaming}
Streaming :sup:`experimental`
=============================
Streams in CAF describe data flow between actors. We are not aiming to provide Streams in CAF describe data flow between actors. We are not aiming to provide
functionality similar to Apache projects like Spark, Flink or Storm. Likewise, functionality similar to Apache projects like Spark, Flink or Storm. Likewise,
...@@ -13,17 +15,23 @@ A stream establishes a logical channel between two or more actors for ...@@ -13,17 +15,23 @@ A stream establishes a logical channel between two or more actors for
exchanging a potentially unbound sequence of values. This channel uses demand exchanging a potentially unbound sequence of values. This channel uses demand
signaling to guarantee that senders cannot overload receivers. signaling to guarantee that senders cannot overload receivers.
\singlefig{stream}{Streaming Concept}{stream} .. _stream:
.. image:: stream.png
:alt: Streaming Concept
Streams are directed and data flows only \emph{downstream}, i.e., from sender Streams are directed and data flows only *downstream*, i.e., from sender
(source) to receiver (sink). Establishing a stream requires a handshake in (source) to receiver (sink). Establishing a stream requires a handshake in
order to initialize required state and signal initial demand. order to initialize required state and signal initial demand.
\singlefig{stream-roles}{Streaming Roles}{stream-roles} .. _stream-roles:
CAF distinguishes between three roles in a stream: (1) a \emph{source} creates .. image:: stream-roles.png
streams and generates data, (2) a \emph{stage} transforms or filters data, and :alt: Streaming Roles
(3) a \emph{sink} terminates streams by consuming data.
CAF distinguishes between three roles in a stream: (1) a *source* creates
streams and generates data, (2) a *stage* transforms or filters data, and
(3) a *sink* terminates streams by consuming data.
We usually draw streams as pipelines for simplicity. However, sources can have We usually draw streams as pipelines for simplicity. However, sources can have
any number of outputs (downstream actors). Likewise, sinks can have any number any number of outputs (downstream actors). Likewise, sinks can have any number
...@@ -31,19 +39,23 @@ of inputs (upstream actors) and stages can multiplex N inputs to M outputs. ...@@ -31,19 +39,23 @@ of inputs (upstream actors) and stages can multiplex N inputs to M outputs.
Hence, streaming topologies in CAF support arbitrary complexity with forks and Hence, streaming topologies in CAF support arbitrary complexity with forks and
joins. joins.
\subsection{Stream Managers} Stream Managers
---------------
Streaming-related messages are handled separately. Under the hood, actors Streaming-related messages are handled separately. Under the hood, actors
delegate to \emph{stream managers} that in turn allow customization of their delegate to *stream managers* that in turn allow customization of their
behavior with \emph{drivers} and \emph{downstream managers}. behavior with *drivers* and *downstream managers*.
.. _fig-stream-manager:
\singlefig{stream-manager}{Internals of Stream Managers}{fig-stream-manager} .. image:: stream-manager.png
:alt: Internals of Stream Managers
Users usually can skip implementing driver classes and instead use the Users usually can skip implementing driver classes and instead use the
lambda-based interface showcased in the following sections. Drivers implement lambda-based interface showcased in the following sections. Drivers implement
the streaming logic by taking inputs from upstream actors and pushing data to the streaming logic by taking inputs from upstream actors and pushing data to
the downstream manager. A source has no input buffer. Hence, drivers only the downstream manager. A source has no input buffer. Hence, drivers only
provide a \emph{generator} function that downstream managers call according to provide a *generator* function that downstream managers call according to
demand. demand.
A downstream manager is responsible for dispatching data to downstream actors. A downstream manager is responsible for dispatching data to downstream actors.
...@@ -52,49 +64,53 @@ the same data. The downstream manager can also perform any sort multi- or ...@@ -52,49 +64,53 @@ the same data. The downstream manager can also perform any sort multi- or
anycast. For example, a load-balancer would use an anycast policy to dispatch anycast. For example, a load-balancer would use an anycast policy to dispatch
data to the next available worker. data to the next available worker.
\clearpage Defining Sources
----------------
\subsection{Defining Sources} .. literalinclude:: /examples/streaming/integer_stream.cpp
:language: C++
\cppexample[17-48]{streaming/integer_stream} :lines: 17-48
The simplest way to defining a source is to use the The simplest way to defining a source is to use the
\lstinline^attach_stream_source^ function and pass it four arguments: a pointer ``attach_stream_source`` function and pass it four arguments: a pointer
to \emph{self}, \emph{initializer} for the state, \emph{generator} for to *self*, *initializer* for the state, *generator* for
producing values, and \emph{predicate} for signaling the end of the stream. producing values, and *predicate* for signaling the end of the stream.
\clearpage
\subsection{Defining Stages} Defining Stages
---------------
\cppexample[50-83]{streaming/integer_stream} .. literalinclude:: /examples/streaming/integer_stream.cpp
:language: C++
:lines: 50-83
The function \lstinline^make_stage^ also takes three lambdas but additionally The function ``make_stage`` also takes three lambdas but additionally
the received input stream handshake as first argument. Instead of a predicate, the received input stream handshake as first argument. Instead of a predicate,
\lstinline^make_stage^ only takes a finalizer, since the stage does not produce ``make_stage`` only takes a finalizer, since the stage does not produce
data on its own and a stream terminates if no more sources exist. data on its own and a stream terminates if no more sources exist.
\clearpage Defining Sinks
--------------
\subsection{Defining Sinks} .. literalinclude:: /examples/streaming/integer_stream.cpp
:language: C++
:lines: 85-114
\cppexample[85-114]{streaming/integer_stream} The function ``make_sink`` is similar to ``make_stage``, except
The function \lstinline^make_sink^ is similar to \lstinline^make_stage^, except
that is does not produce outputs. that is does not produce outputs.
\clearpage Initiating Streams
------------------
\subsection{Initiating Streams}
\cppexample[128-132]{streaming/integer_stream} .. literalinclude:: /examples/streaming/integer_stream.cpp
:language: C++
:lines: 128-132
In our example, we always have a source \lstinline^int_source^ and a sink In our example, we always have a source ``int_source`` and a sink
\lstinline^int_sink^ with an optional stage \lstinline^int_selector^. Sending ``int_sink`` with an optional stage ``int_selector``. Sending
\lstinline^open_atom^ to the source initiates the stream and the source will ``open_atom`` to the source initiates the stream and the source will
respond with a stream handshake. respond with a stream handshake.
Using the actor composition in CAF (\lstinline^snk * src^ reads \emph{sink Using the actor composition in CAF (``snk * src`` reads *sink
after source}) allows us to redirect the stream handshake we send in after source*) allows us to redirect the stream handshake we send in
\lstinline^caf_main^ to the sink (or to the stage and then from the stage to ``caf_main`` to the sink (or to the stage and then from the stage to
the sink). the sink).
.. _testing:
Testing
=======
CAF comes with built-in support for writing unit tests in a domain-specific
language (DSL). The API looks similar to well-known testing frameworks such as
Boost.Test and Catch but adds CAF-specific macros for testing messaging between
actors.
Our design leverages four main concepts:
* **Checks** represent single boolean expressions.
* **Tests** contain one or more checks.
* **Fixtures** equip tests with a fixed data environment.
* **Suites** group tests together.
The following code illustrates a very basic test case that captures the four
main concepts described above.
.. code-block:: C++
// Adds all tests in this compilation unit to the suite "math".
#define CAF_SUITE math
// Pulls in all the necessary macros.
#include "caf/test/dsl.hpp"
namespace {
struct fixture {};
} // namespace
// Makes all members of `fixture` available to tests in the scope.
CAF_TEST_FIXTURE_SCOPE(math_tests, fixture)
// Implements our first test.
CAF_TEST(divide) {
CAF_CHECK(1 / 1 == 0); // fails
CAF_CHECK(2 / 2 == 1); // passes
CAF_REQUIRE(3 + 3 == 5); // fails and aborts test execution
CAF_CHECK(4 - 4 == 0); // unreachable due to previous requirement error
}
CAF_TEST_FIXTURE_SCOPE_END()
The code above highlights the two basic macros ``CAF_CHECK`` and
``CAF_REQUIRE``. The former reports failed checks, but allows the test
to continue on error. The latter stops test execution if the boolean expression
evaluates to false.
The third macro worth mentioning is ``CAF_FAIL``. It unconditionally
stops test execution with an error message. This is particularly useful for
stopping program execution after receiving unexpected messages, as we will see
later.
Testing Actors
--------------
The following example illustrates how to add an actor system as well as a
scoped actor to fixtures. This allows spawning of and interacting with actors
in a similar way regular programs would. Except that we are using macros such
as ``CAF_CHECK`` and provide tests rather than implementing a
``caf_main``.
.. code-block:: C++
namespace {
struct fixture {
caf::actor_system_config cfg;
caf::actor_system sys;
caf::scoped_actor self;
fixture() : sys(cfg), self(sys) {
// nop
}
};
caf::behavior adder() {
return {
[=](int x, int y) {
return x + y;
}
};
}
} // namespace
CAF_TEST_FIXTURE_SCOPE(actor_tests, fixture)
CAF_TEST(simple actor test) {
// Our Actor-Under-Test.
auto aut = self->spawn(adder);
self->request(aut, caf::infinite, 3, 4).receive(
[=](int r) {
CAF_CHECK(r == 7);
},
[&](caf::error& err) {
// Must not happen, stop test.
CAF_FAIL(sys.render(err));
});
}
CAF_TEST_FIXTURE_SCOPE_END()
The example above works, but suffers from several issues:
* Significant amount of boilerplate code.
* Using a scoped actor as illustrated above can only test one actor at a time. However, messages between other actors are invisible to us.
* CAF runs actors in a thread pool by default. The resulting nondeterminism makes triggering reliable ordering of messages near impossible. Further, forcing timeouts to test error handling code is even harder.
Deterministic Testing
---------------------
CAF provides a scheduler implementation specifically tailored for writing unit
tests called ``test_coordinator``. It does not start any threads and
instead gives unit tests full control over message dispatching and timeout
management.
To reduce boilerplate code, CAF also provides a fixture template called
``test_coordinator_fixture`` that comes with ready-to-use actor system
(``sys``) and testing scheduler (``sched``). The optional
template parameter allows unit tests to plugin custom actor system
configuration classes.
Using this fixture unlocks three additional macros:
* ``expect`` checks for a single message. The macro verifies the content types of the message and invokes the necessary member functions on the test coordinator. Optionally, the macro checks the receiver of the message and its content. If the expected message does not exist, the test aborts.
* ``allow`` is similar to ``expect``, but it does not abort the test if the expected message is missing. This macro returns ``true`` if the allowed message was delivered, ``false`` otherwise.
* ``disallow`` aborts the test if a particular message was delivered to an actor.
The following example implements two actors, ``ping`` and
``pong``, that exchange a configurable amount of messages. The test
*three pings* then checks the contents of each message with
``expect`` and verifies that no additional messages exist using
``disallow``.
.. literalinclude:: /examples/testing/ping_pong.cpp
:language: C++
:lines: 12-60
.. _type-inspection:
Type Inspection (Serialization and String Conversion)
=====================================================
CAF is designed with distributed systems in mind. Hence, all message types must
be serializable and need a platform-neutral, unique name that is configured at
startup (see :ref:`add-custom-message-type`). Using a message type that is not
serializable causes a compiler error (see :ref:`unsafe-message-type`). CAF
serializes individual elements of a message by using the inspection API. This
API allows users to provide code for serialization as well as string conversion
with a single free function. The signature for a class ``my_class`` is always as
follows:
.. code-block:: C++
template <class Inspector>
typename Inspector::result_type inspect(Inspector& f, my_class& x) {
return f(...);
}
The function ``inspect`` passes meta information and data fields to the
variadic call operator of the inspector. The following example illustrates an
implementation for ``inspect`` for a simple POD struct.
.. literalinclude:: /examples/custom_type/custom_types_1.cpp
:language: C++
:lines: 23-33
The inspector recursively inspects all data fields and has builtin support for
(1) ``std::tuple``, (2) ``std::pair``, (3) C arrays, (4) any
container type with ``x.size()``, ``x.empty()``,
``x.begin()`` and ``x.end()``.
We consciously made the inspect API as generic as possible to allow for
extensibility. This allows users to use CAF's types in other contexts, to
implement parsers, etc.
Inspector Concept
-----------------
The following concept class shows the requirements for inspectors. The
placeholder ``T`` represents any user-defined type. For example,
``error`` when performing I/O operations or some integer type when
implementing a hash function.
.. code-block:: C++
Inspector {
using result_type = T;
if (inspector only requires read access to the state of T)
static constexpr bool reads_state = true;
else
static constexpr bool writes_state = true;
template <class... Ts>
result_type operator()(Ts&&...);
}
A saving ``Inspector`` is required to handle constant lvalue and rvalue
references. A loading ``Inspector`` must only accept mutable lvalue
references to data fields, but still allow for constant lvalue references and
rvalue references to annotations.
Annotations
-----------
Annotations allow users to fine-tune the behavior of inspectors by providing
addition meta information about a type. All annotations live in the namespace
``caf::meta`` and derive from ``caf::meta::annotation``. An
inspector can query whether a type ``T`` is an annotation with
``caf::meta::is_annotation<T>::value``. Annotations are passed to the
call operator of the inspector along with data fields. The following list shows
all annotations supported by CAF:
* ``type_name(n)``: Display type name as ``n`` in human-friendly output (position before data fields).
* ``hex_formatted()``: Format the following data field in hex format.
* ``omittable()``: Omit the following data field in human-friendly output.
* ``omittable_if_empty()``: Omit the following data field if it is empty in human-friendly output.
* ``omittable_if_none()``: Omit the following data field if it equals ``none`` in human-friendly output.
* ``save_callback(f)``: Call ``f`` when serializing (position after data fields).
* ``load_callback(f)``: Call ``f`` after deserializing all data fields (position after data fields).
Backwards and Third-party Compatibility
---------------------------------------
CAF evaluates common free function other than ``inspect`` in order to
simplify users to integrate CAF into existing code bases.
Serializers and deserializers call user-defined ``serialize``
functions. Both types support ``operator&`` as well as
``operator()`` for individual data fields. A ``serialize``
function has priority over ``inspect``.
When converting a user-defined type to a string, CAF calls user-defined
``to_string`` functions and prefers those over ``inspect``.
.. _unsafe-message-type:
Whitelisting Unsafe Message Types
---------------------------------
Message types that are not serializable cause compile time errors when used in
actor communication. When using CAF for concurrency only, this errors can be
suppressed by whitelisting types with
``CAF_ALLOW_UNSAFE_MESSAGE_TYPE``. The macro is defined as follows.
Splitting Save and Load Operations
----------------------------------
If loading and storing cannot be implemented in a single function, users can
query whether the inspector is loading or storing. For example, consider the
following class ``foo`` with getter and setter functions and no public
access to its members.
.. literalinclude:: /examples/custom_type/custom_types_3.cpp
:language: C++
:lines: 20-49
Since there is no access to the data fields ``a_`` and ``b_``
(and assuming no changes to ``foo`` are possible), we need to split our
implementation of ``inspect`` as shown below.
.. literalinclude:: /examples/custom_type/custom_types_3.cpp
:language: C++
:lines: 76-103
The purpose of the scope guard in the example above is to write the content of
the temporaries back to ``foo`` at scope exit automatically. Storing
the result of ``f(...)`` in a temporary first and then writing the
changes to ``foo`` is not possible, because ``f(...)`` can
return ``void``.
Using ``aout`` -- A Concurrency-safe Wrapper for ``cout``
=========================================================
When using ``cout`` from multiple actors, output often appears
interleaved. Moreover, using ``cout`` from multiple actors -- and thus
from multiple threads -- in parallel should be avoided regardless, since the
standard does not guarantee a thread-safe implementation.
By replacing ``std::cout`` with ``caf::aout``, actors can achieve a
concurrency-safe text output. The header ``caf/all.hpp`` also defines overloads
for ``std::endl`` and ``std::flush`` for ``aout``, but does not support the full
range of ostream operations (yet). Each write operation to ``aout`` sends a
message to a "hidden" actor. This actor only prints lines, unless output is
forced using ``flush``. The example below illustrates printing of lines of text
from multiple actors (in random order).
.. literalinclude:: /examples/aout.cpp
:language: C++
.. _utility:
Utility
=======
CAF includes a few utility classes that are likely to be part of C++
eventually (or already are in newer versions of the standard). However, until
these classes are part of the standard library on all supported compilers, we
unfortunately have to maintain our own implementations.
.. _optional:
Class ``optional``
------------------
Represents a value that may or may not exist.
+-----------------------------+---------------------------------------------+
| **Constructors** | |
+-----------------------------+---------------------------------------------+
| ``(T value)`` | Constructs an object with a value. |
+-----------------------------+---------------------------------------------+
| ``(none_t = none)`` | Constructs an object without a value. |
+-----------------------------+---------------------------------------------+
| | |
+-----------------------------+---------------------------------------------+
| **Observers** | |
+-----------------------------+---------------------------------------------+
| ``explicit operator bool()``| Checks whether the object contains a value. |
+-----------------------------+---------------------------------------------+
| ``T* operator->()`` | Accesses the contained value. |
+-----------------------------+---------------------------------------------+
| ``T& operator*()`` | Accesses the contained value. |
+-----------------------------+---------------------------------------------+
Class ``expected``
------------------
Represents the result of a computation that *should* return a value. If no value
could be produced, the ``expected<T>`` contains an ``error`` (see :ref:`error`).
+-----------------------------+---------------------------------------------+
| **Constructors** | |
+-----------------------------+---------------------------------------------+
| ``(T value)`` | Constructs an object with a value. |
+-----------------------------+---------------------------------------------+
| ``(error err)`` | Constructs an object with an error. |
+-----------------------------+---------------------------------------------+
| | |
+-----------------------------+---------------------------------------------+
| **Observers** | |
+-----------------------------+---------------------------------------------+
| ``explicit operator bool()``| Checks whether the object contains a value. |
+-----------------------------+---------------------------------------------+
| ``T* operator->()`` | Accesses the contained value. |
+-----------------------------+---------------------------------------------+
| ``T& operator*()`` | Accesses the contained value. |
+-----------------------------+---------------------------------------------+
| ``error& error()`` | Accesses the contained error. |
+-----------------------------+---------------------------------------------+
Constant ``unit``
-----------------
The constant ``unit`` of type ``unit_t`` is the equivalent of
``void`` and can be used to initialize ``optional<void>`` and
``expected<void>``.
Constant ``none``
-----------------
The constant ``none`` of type ``none_t`` can be used to
initialize an ``optional<T>`` to represent "nothing".
...@@ -20,6 +20,34 @@ ...@@ -20,6 +20,34 @@
# import sys # import sys
# sys.path.insert(0, os.path.abspath('.')) # sys.path.insert(0, os.path.abspath('.'))
import os, sys, git, re
# Fetch the CAF version.
import re
with open("../libcaf_core/caf/config.hpp") as f:
match = re.search('^#define CAF_VERSION ([0-9]+)$', f.read(), re.MULTILINE)
if match == None:
raise RuntimeError("unable to locate CAF_VERSION string in config.hpp")
raw_version = int(match.group(1))
major = int(raw_version / 10000)
minor = int(raw_version / 100) % 100
patch = raw_version % 100
version = '{}.{}.{}'.format(major, minor, patch)
# We're building a stable release if the last commit message is
# "Change version to <version>".
repo = git.Repo(os.path.abspath('..'))
is_stable = repo.head.commit.message.startswith("Change version to " + version)
# Generate the full version, including alpha/beta/rc tags. For stable releases,
# this is always the same as the CAF version.
if is_stable:
release = version
else:
last_commit_full = str(repo.head.commit)
last_commit = last_commit_full[:7]
release = version + "+exp.sha." + last_commit
# -- General configuration ------------------------------------------------ # -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here. # If your documentation needs a minimal Sphinx version, state it here.
...@@ -47,21 +75,18 @@ source_suffix = '.rst' ...@@ -47,21 +75,18 @@ source_suffix = '.rst'
# source_encoding = 'utf-8-sig' # source_encoding = 'utf-8-sig'
# The master toctree document. # The master toctree document.
master_doc = 'index' master_doc = 'manual'
# General information about the project. # General information about the project.
project = u'CAF' project = u'CAF'
copyright = u'2016, Dominik Charousset' copyright = u'2020, Dominik Charousset'
author = u'Dominik Charousset' author = u'Dominik Charousset'
# The version info for the project you're documenting, acts as replacement for # Make variables available to .rst.
# |version| and |release|, also used in various other places throughout the rst_epilog = """
# built documents. .. |version| replace:: {}
# .. |release| replace:: {}
# The short X.Y version. """.format(version, release)
version = u'@CAF_VERSION@'
# The full version, including alpha/beta/rc tags.
release = u'@CAF_RELEASE@'
# The language for content autogenerated by Sphinx. Refer to documentation # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # for a list of supported languages.
...@@ -157,7 +182,7 @@ html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] ...@@ -157,7 +182,7 @@ html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
# Add any paths that contain custom static files (such as style sheets) here, # Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files, # relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css". # so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static'] # html_static_path = ['_static']
# Add any extra paths that contain custom files (such as robots.txt or # Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied # .htaccess) here, relative to this directory. These files are copied
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment