【问题标题】:boost::mpl: How to generate pre-generated header files for lists with more than 50 entries?boost::mpl: 如何为超过 50 个条目的列表生成预生成的头文件?
【发布时间】:2013-10-21 11:44:30
【问题描述】:

我想编写一个状态机,它的反应列表包含超过 50 个条目。

我发现 here 一些 python 脚本可以为超过 50 个条目的列表生成头文件。

但我无法生成单个头文件。有人可以向我解释如何使用这些脚本吗?我也没有在提升文档方面找到任何帮助。

任何帮助将不胜感激。

【问题讨论】:

  • 你需要多少?
  • 我认为现在是 60 或 70。

标签: c++ boost boost-mpl


【解决方案1】:

重要更新:

this answer 这个问题描述了一个更改进的脚本,它只需要一个工作 python 环境。

原答案

在 Boost 邮件列表中查看我的 answer 至您的 question

以下 bash 代码将为 51 到 100 个条目生成 MPL 容器。如果您不关心其他 MPL 容器类型而只想生成 MPL 列表,请相应地调整 CONTAINERS 的值。

#! /bin/bash

# The name of this script.
SCRIPT_NAME=$(basename $0)
# The usage message for this script.
USAGE="Usage:  ${SCRIPT_NAME} <path-to-boost-source> [<new-value-for BOOST_MPL_LIMIT_VECTOR_SIZE>]"

# Script-arguments:
# The (absolute) path to the directory containing the Boost-source.
SOURCE=$(readlink -e "$1")
# The new limit for Boost.MPL's vector.
NEW_BOOST_MPL_LIMIT_VECTOR_SIZE=$2

# Check validity of arguments.
if [[ -z "${SOURCE}" ]] || ! [[ -d "${SOURCE}" ]]; then
  echo "Missing path to Boost directory."
  echo ${USAGE}
  exit
fi
REGEX_INTEGER='^[0-9]+$'
if [[ -n "${NEW_BOOST_MPL_LIMIT_VECTOR_SIZE}" ]] && ! [[ ${NEW_BOOST_MPL_LIMIT_VECTOR_SIZE} =~ ${REGEX_INTEGER} ]]; then
  echo "New size for BOOST_MPL_LIMIT_VECTOR_SIZE must be an unsigned integer!"
  echo ${USAGE}
  exit
fi


# The Boost.MPL-containers for which more preprocessed
# header-files shall be generated.
CONTAINERS="vector list set map"
# The same containers without Boost.MPL-map.
CONTAINERS_WITHOUT_MAP=$(echo ${CONTAINERS} | sed 's/map//g')

# Change into include-directory of Boost.MPL.
pushd ${SOURCE}/boost/mpl  >/dev/null 2>&1

# Possibly change the limit for Boost.MPL's vector.
if [[ -n "${NEW_BOOST_MPL_LIMIT_VECTOR_SIZE}" ]]; then
  sed -i "s/\(define\s\+BOOST_MPL_LIMIT_VECTOR_SIZE\s\+\)[0-9]\+/\1${NEW_BOOST_MPL_LIMIT_VECTOR_SIZE}/" ./limits/vector.hpp
fi

# Create hpp-files for each MPL-container with 60 to 100 elements
# which will be used during generation.
for container in ${CONTAINERS}; do
  for i in {6..10}; do
    sed -e "s/50/${i}0/g" \
        -e "s/41/$((i-1))1/g" \
        -e "s/40/$((i-1))0/g" \
        ./${container}/${container}50.hpp \
      > ./${container}/${container}${i}0.hpp;
  done
done
for container in ${CONTAINERS_WITHOUT_MAP}; do
  for i in {6..10}; do
    sed -e "s/50/${i}0/g" \
        -e "s/41/$((i-1))1/g" \
        -e "s/40/$((i-1))0/g" \
        ./${container}/${container}50_c.hpp \
      > ./${container}/${container}${i}0_c.hpp;
  done
done

# Leave include-directory of Boost.MPL.
popd  >/dev/null 2>&1
# Change into source-directory of Boost.MPL.
pushd ${SOURCE}/libs/mpl/preprocessed  >/dev/null 2>&1

# Create cpp-files for each MPL-container with 60 to 100 elements
# which will be used during generation.
for container in ${CONTAINERS}; do
  for i in {6..10}; do
    sed -e "s/50/${i}0/g" \
        -e "s/41/$((i-1))1/g" \
        -e "s/40/$((i-1))0/g" \
        ./${container}/${container}50.cpp \
      > ./${container}/${container}${i}0.cpp;
  done
done
for container in ${CONTAINERS_WITHOUT_MAP}; do
  for i in {6..10}; do
    sed -e "s/50/${i}0/g" \
        -e "s/41/$((i-1))1/g" \
        -e "s/40/$((i-1))0/g" \
        ./${container}/${container}50_c.cpp \
      > ./${container}/${container}${i}0_c.cpp;
  done
done

# Now generate MPL-preprocessed files using the python-scripts.
python preprocess_vector.py all ${SOURCE}
python preprocess_list.py   all ${SOURCE}
python preprocess_set.py    all ${SOURCE}
python preprocess_map.py    all ${SOURCE}
python preprocess.py        all ${SOURCE}

# Leave source-directory of Boost.MPL.
popd  >/dev/null 2>&1

对于包含超过 100 个条目的更大容器,请调整 for 循环中的范围。

希望对您有所帮助。
德尼兹

更新:

我意识到我的原始脚本版本只生成了 MPL 容器标头的编号版本。但是,它没有更新未编号的版本。

我通过在脚本末尾附近添加python preprocess.py all ${SOURCE} 来修复它。 但是,如果在生成过程中相应的宏设置(例如,BOOST_MPL_LIMIT_VECTOR_SIZE 的更高值)与原始值不同,这只会生成未编号的 MPL 容器标头的修改版本。

可以在${SOURCE}/libs/mpl/preprocessed/include 的每个子目录中的文件user.hpp 中进行这些修改。

例如,通过添加

#define BOOST_MPL_LIMIT_VECTOR_SIZE 30

对于${SOURCE}/libs/mpl/preprocessed/include/gcc/user.hppboost::mpl::vector(包括在boost/mpl/vector.hpp)现在能够容纳 30 个元素,而不是默认的 20 个。

注意:

但是,仅针对生成调整 BOOST_MPL_LIMIT_VECTOR_SIZE 之类的值并保持其在 ${SOURCE}/boost/mpl/limits/vector.hpp 中定义的值不变,这可能不是一个好主意。
可能,这甚至无法编译,因为无法找到底层的 boost::mpl::vector21boost::mpl::vectorN(因为没有自动包含在内)。 最好的解决方案可能是调整${SOURCE}/boost/mpl/limits/vector.hpp 中的值。如果给出第二个(可选)参数,上述 bash 脚本可以自动执行此操作。

【讨论】:

    【解决方案2】:

    最终更新

    Boost 1.59.0 开始,下面解释的脚本现在包含在每个 Boost 版本中。

    它们位于 /libs/mpl/preprocessed/

    该目录中的 README.txt 应该向您解释如何使用它。 (或者继续阅读。)

    原答案

    因为我意识到并不是每个人手头都有一个工作的 bash shell(是的,我是说你是 Windows 人 ;-))但每个人都需要一个工作的 python 环境,我将我提到的above 的助手bash-script 转换为python:

    boost_mpl_preprocess.py

    # Copyright Deniz Bahadir 2015
    #
    # Distributed under the Boost Software License, Version 1.0. 
    # (See accompanying file LICENSE_1_0.txt or copy at 
    # http://www.boost.org/LICENSE_1_0.txt)
    #
    # See http://www.boost.org/libs/mpl for documentation.
    # See https://stackoverflow.com/a/20660264/3115457 for further information.
    # See https://stackoverflow.com/a/29627158/3115457 for further information.
    
    import argparse
    import sys
    import os
    import os.path
    import re
    import fileinput
    import shutil
    
    
    def create_more_container_files(sourceDir, suffix, maxElements, containers, containers2):
        """Creates additional files for the individual MPL-containers."""
    
        # Create files for each MPL-container with 20 to 'maxElements' elements
        # which will be used during generation.
        for container in containers:
            for i in range(20, maxElements, 10):
                # Create copy of "template"-file.
                newFile = os.path.join( sourceDir, container, container + str(i+10) + suffix )
                shutil.copyfile( os.path.join( sourceDir, container, container + "20" + suffix ), newFile ) 
                # Adjust copy of "template"-file accordingly.
                for line in fileinput.input( newFile, inplace=1, mode="rU" ):
                    line = re.sub(r'20', re.escape(str(i+10)), line.rstrip())
                    line = re.sub(r'11', re.escape(str(i + 1)), line.rstrip())
                    line = re.sub(r'10(?![0-9])', re.escape(str(i)), line.rstrip())
                    print(line)
        for container in containers2:
            for i in range(20, maxElements, 10):
                # Create copy of "template"-file.
                newFile = os.path.join( sourceDir, container, container + str(i+10) + "_c" + suffix )
                shutil.copyfile( os.path.join( sourceDir, container, container + "20_c" + suffix ), newFile ) 
                # Adjust copy of "template"-file accordingly.
                for line in fileinput.input( newFile, inplace=1, mode="rU" ):
                    line = re.sub(r'20', re.escape(str(i+10)), line.rstrip())
                    line = re.sub(r'11', re.escape(str(i + 1)), line.rstrip())
                    line = re.sub(r'10(?![0-9])', re.escape(str(i)), line.rstrip())
                    print(line)
    
    
    def create_input_for_numbered_sequences(headerDir, sourceDir, containers, maxElements):
        """Creates additional source- and header-files for the numbered sequence MPL-containers."""
        # Create additional container-list without "map".
        containersWithoutMap = containers[:]
        try:
            containersWithoutMap.remove('map')
        except ValueError:
            # We can safely ignore if "map" is not contained in 'containers'!
            pass
        # Create header/source-files.
        create_more_container_files(headerDir, ".hpp", maxElements, containers, containersWithoutMap)
        create_more_container_files(sourceDir, ".cpp", maxElements, containers, containersWithoutMap)
    
    
    def adjust_container_limits_for_variadic_sequences(headerDir, containers, maxElements):
        """Adjusts the limits of variadic sequence MPL-containers."""
        for container in containers:
            headerFile = os.path.join( headerDir, "limits", container + ".hpp" )
            regexMatch   = r'(define\s+BOOST_MPL_LIMIT_' + container.upper() + r'_SIZE\s+)[0-9]+'
            regexReplace = r'\g<1>' + re.escape( str(maxElements) )
            for line in fileinput.input( headerFile, inplace=1, mode="rU" ):
                line = re.sub(regexMatch, regexReplace, line.rstrip())
                print(line)
    
    
    def to_positive_multiple_of_10(string):
        """Converts a string into its encoded positive integer (greater zero) or throws an exception."""
        try:
            value = int(string)
        except ValueError:
            msg = '"%r" is not a positive multiple of 10 (greater zero).' % string
            raise argparse.ArgumentTypeError(msg)
        if value <= 0 or value % 10 != 0:
            msg = '"%r" is not a positive multiple of 10 (greater zero).' % string
            raise argparse.ArgumentTypeError(msg)
        return value
    
    
    def to_existing_absolute_path(string):
        """Converts a path into its absolute path and verifies that it exists or throws an exception."""
        value = os.path.abspath(string)
        if not os.path.exists( value ) or not os.path.isdir( value ):
            msg = '"%r" is not a valid path to a directory.' % string
            raise argparse.ArgumentTypeError(msg)
        return value
    
    
    def main():
        """The main function."""
    
        # Prepare and run cmdline-parser.
        cmdlineParser = argparse.ArgumentParser(description="A generator-script for pre-processed Boost.MPL headers.")
        cmdlineParser.add_argument("-v", "--verbose", dest='verbose', action='store_true',
                                   help="Be a little bit more verbose.")
        cmdlineParser.add_argument("-s", "--sequence-type", dest='seqType', choices=['variadic', 'numbered', 'both'],
                                   default='both',
                                   help="Only update pre-processed headers for the selected sequence types, "
                                        "either 'numbered' sequences, 'variadic' sequences or 'both' sequence "
                                        "types. (Default=both)")
        cmdlineParser.add_argument("--no-vector", dest='want_vector', action='store_false',
                                   help="Do not update pre-processed headers for Boost.MPL Vector.")
        cmdlineParser.add_argument("--no-list", dest='want_list', action='store_false',
                                   help="Do not update pre-processed headers for Boost.MPL List.")
        cmdlineParser.add_argument("--no-set", dest='want_set', action='store_false',
                                   help="Do not update pre-processed headers for Boost.MPL Set.")
        cmdlineParser.add_argument("--no-map", dest='want_map', action='store_false',
                                   help="Do not update pre-processed headers for Boost.MPL Map.")
        cmdlineParser.add_argument("--num-elements", dest='numElements', metavar="<num-elements>",
                                   type=to_positive_multiple_of_10, default=100,
                                   help="The maximal number of elements per container sequence. (Default=100)")
        cmdlineParser.add_argument(dest='sourceDir', metavar="<source-dir>",
                                   type=to_existing_absolute_path,
                                   help="The source-directory of Boost.")
        args = cmdlineParser.parse_args()
    
        # Some verbose debug output.
        if args.verbose:
            print "Arguments extracted from command-line:"
            print "  verbose          = ", args.verbose
            print "  source directory = ", args.sourceDir
            print "  num elements     = ", args.numElements
            print "  sequence type    = ", args.seqType
            print "  want: vector     = ", args.want_vector
            print "  want: list       = ", args.want_list
            print "  want: set        = ", args.want_set
            print "  want: map        = ", args.want_map
    
        # The directories for header- and source files of Boost.MPL.    
        # NOTE: Assuming 'args.sourceDir' is the source-directory of the entire boost project.
        headerDir = os.path.join( args.sourceDir, "boost", "mpl" )
        sourceDir = os.path.join( args.sourceDir, "libs", "mpl", "preprocessed" )
        # Check that the header/source-directories exist.
        if not os.path.exists( headerDir ) or not os.path.exists( sourceDir ):
            # Maybe 'args.sourceDir' is not the source-directory of the entire boost project
            # but instead of the Boost.MPL git-directory, only?
            headerDir = os.path.join( args.sourceDir, "include", "boost", "mpl" )
            sourceDir = os.path.join( args.sourceDir, "preprocessed" )
            if not os.path.exists( headerDir ) or not os.path.exists( sourceDir ):
                cmdlineParser.print_usage()
                print "error: Cannot find Boost.MPL header/source files in given Boost source-directory!"
                sys.exit(0)
    
        # Some verbose debug output.
        if args.verbose:
            print "Chosen header-directory: ", headerDir
            print "Chosen source-directory: ", sourceDir
    
        # Create list of containers for which files shall be pre-processed.
        containers = []
        if args.want_vector:
            containers.append('vector')
        if args.want_list:
            containers.append('list')
        if args.want_set:
            containers.append('set')
        if args.want_map:
            containers.append('map')
        if containers == []:
            print "Nothing to do. (Why did you prevent generating pre-processed headers for all Boost.MPL container types?"
            sys.exit(0)
    
        # Some verbose debug output.
        if args.verbose:
            print "Containers for which to pre-process headers: ", containers
    
        # Create (additional) input files for generating pre-processed headers of numbered sequence MPL containers.
        if args.seqType == "both" or args.seqType == "numbered":
            create_input_for_numbered_sequences(headerDir, sourceDir, containers, args.numElements)
        # Modify settings for generating pre-processed headers of variadic sequence MPL containers.
        if args.seqType == "both" or args.seqType == "variadic":
            adjust_container_limits_for_variadic_sequences(headerDir, containers, args.numElements)
    
        # Generate MPL-preprocessed files.
        os.chdir( sourceDir )
        if args.seqType == "both" or args.seqType == "numbered":
            if args.want_vector:
                if args.verbose:
                    print "Pre-process headers for Boost.MPL numbered vectors."
                os.system( "python " + os.path.join( sourceDir, "preprocess_vector.py" ) + " all " + args.sourceDir )
            if args.want_list:
                if args.verbose:
                    print "Pre-process headers for Boost.MPL numbered lists."
                os.system( "python " + os.path.join( sourceDir, "preprocess_list.py" ) + " all " + args.sourceDir )
            if args.want_set:
                if args.verbose:
                    print "Pre-process headers for Boost.MPL numbered sets."
                os.system( "python " + os.path.join( sourceDir, "preprocess_set.py" ) + " all " + args.sourceDir )
            if args.want_map:
                if args.verbose:
                    print "Pre-process headers for Boost.MPL numbered maps."
                os.system( "python " + os.path.join( sourceDir, "preprocess_map.py" ) + " all " + args.sourceDir )
        if args.seqType == "both" or args.seqType == "variadic":
            if args.verbose:
                print "Pre-process headers for Boost.MPL variadic containers."
            os.system( "python " + os.path.join( sourceDir, "preprocess.py" ) + " all " + args.sourceDir )
    
    
    if __name__ == '__main__':
        main()
    

    运行它
    python boost_mpl_preprocess.py --help
    

    提供一些使用信息。

    但您也可以只使用单个命令行参数运行它,该参数确定 Boost 源目录的路径(例如 python boost_mpl_preprocess.py ./boost-source)并使用默认行为。

    这导致为所有四种 Boost.MPL 容器类型(vectorlistsetmap)创建预处理的标头及其 numbered and variadic 形式。

    注意:

    在以 variadic 形式预处理 Boost.MPL 容器时,作为副作用,它们关联的 limit-macros BOOST_MPL_LIMIT_container_SIZE 的值也将被修改。它获取调用 boost_mpl_preprocess.py 时确定的元素数量。

    例如,以下调用仅预处理 Boost.MPL vectorvariadic 形式,并将 ./boost-source/boost/mpl/limits/vector.hpp 中的宏 BOOST_MPL_LIMIT_VECTOR_SIZE 设置为 70

    $ python boost_mpl_preprocess.py --no-list --no-set --no-map \
        --sequence-type=variadic --num-elements=70 ./boost-source
    

    更新

    由于 Boost 1.56 预处理 Boost.MPL 容器,即使使用上述脚本也不再起作用,因为 header-cmets 的变化,原来的 python-scripts 依赖。

    以下脚本可用于解决此问题。它只是将缺少的信息添加到 header-cmets 中。

    fix_boost_mpl_preprocess.py

    # Copyright Deniz Bahadir 2015
    #
    # Distributed under the Boost Software License, Version 1.0. 
    # (See accompanying file LICENSE_1_0.txt or copy at 
    # http://www.boost.org/LICENSE_1_0.txt)
    #
    # See http://www.boost.org/libs/mpl for documentation.
    # See https://stackoverflow.com/a/29627158/3115457 for further information.
    
    import argparse
    import sys
    import os.path
    import re
    import fileinput
    import datetime
    import glob
    
    
    def fix_header_comment(filename, timestamp):
        """Fixes the header-comment of the given file."""
        # Fix input file.
        name = os.path.basename( filename )
        for line in fileinput.input( filename, inplace=1, mode="rU" ):
            # If header-comment already contains anything for '$Id$', remove it.
            line = re.sub(r'\$Id:[^$]+\$', r'$Id$', line.rstrip())
            # Replace '$Id$' by a string containing the file's name (and a timestamp)!
            line = re.sub(re.escape(r'$Id$'), r'$Id: ' + name + r' ' + timestamp.isoformat() + r' $', line.rstrip())
            print(line)
    
    
    def fix_input_files_for_variadic_seq(sourceDir, timestamp):
        """Fixes files used as input when pre-processing MPL-containers in their variadic form."""
        files = glob.glob( os.path.join( sourceDir, "src", "*" ) )
        for currentFile in sorted( files ):
            fix_header_comment( currentFile, timestamp )
    
    
    def fix_input_files_for_numbered_seq(sourceDir, suffix, timestamp, containers):
        """Fixes files used as input when pre-processing MPL-containers in their numbered form."""
        # Fix input files for each MPL-container type.
        for container in containers:
            files = glob.glob( os.path.join( sourceDir, container, container + '*' + suffix ) )
            for currentFile in sorted( files ):
                fix_header_comment( currentFile, timestamp )
    
    
    def fix_input_files(headerDir, sourceDir, containers=['vector', 'list', 'set', 'map'], verbose='false'):
        """Fixes source- and header-files used as input when pre-processing MPL-containers."""
        # The new modification time.
        timestamp = datetime.datetime.now();
        # Fix the input files.
        if verbose:
            print "Fix input files for pre-processing Boost.MPL variadic containers."
        fix_input_files_for_variadic_seq(sourceDir, timestamp)
        if verbose:
            print "Fix input files for pre-processing Boost.MPL numbered containers."
        fix_input_files_for_numbered_seq(headerDir, ".hpp", timestamp, containers)
        fix_input_files_for_numbered_seq(sourceDir, ".cpp", timestamp, containers)
    
    
    def to_existing_absolute_path(string):
        """Converts a path into its absolute path and verifies that it exists or throws an exception."""
        value = os.path.abspath(string)
        if not os.path.exists( value ) or not os.path.isdir( value ):
            msg = '"%r" is not a valid path to a directory.' % string
            raise argparse.ArgumentTypeError(msg)
        return value
    
    
    def main():
        """The main function."""
    
        # Prepare and run cmdline-parser.
        cmdlineParser = argparse.ArgumentParser(
                        description="Fixes the input files used for pre-processing of Boost.MPL headers.")
        cmdlineParser.add_argument("-v", "--verbose", dest='verbose', action='store_true',
                                   help="Be a little bit more verbose.")
        cmdlineParser.add_argument(dest='sourceDir', metavar="<source-dir>",
                                   type=to_existing_absolute_path,
                                   help="The source-directory of Boost.")
        args = cmdlineParser.parse_args()
    
        # Some verbose debug output.
        if args.verbose:
            print "Arguments extracted from command-line:"
            print "  verbose           = ", args.verbose
            print "  source directory  = ", args.sourceDir
    
        # The directories for header- and source files of Boost.MPL.    
        # NOTE: Assuming 'args.sourceDir' is the source-directory of the entire boost project.
        headerDir = os.path.join( args.sourceDir, "boost", "mpl" )
        sourceDir = os.path.join( args.sourceDir, "libs", "mpl", "preprocessed" )
        # Check that the header/source-directories exist.
        if not os.path.exists( headerDir ) or not os.path.exists( sourceDir ):
            # Maybe 'args.sourceDir' is not the source-directory of the entire boost project
            # but instead of the Boost.MPL git-directory, only?
            headerDir = os.path.join( args.sourceDir, "include", "boost", "mpl" )
            sourceDir = os.path.join( args.sourceDir, "preprocessed" )
            if not os.path.exists( headerDir ) or not os.path.exists( sourceDir ):
                cmdlineParser.print_usage()
                print "error: Cannot find Boost.MPL header/source files in given Boost source-directory!"
                sys.exit(0)
    
        # Some verbose debug output.
        if args.verbose:
            print "Chosen header-directory: ", headerDir
            print "Chosen source-directory: ", sourceDir
    
        # Fix input file for generating pre-processed headers.
        fix_input_files(headerDir, sourceDir, verbose = args.verbose)
    
    
    if __name__ == '__main__':
        main()
    

    像这样运行它:

    $ python fix-boost_mpl_preprocess.py ./boost-source
    

    【讨论】:

      【解决方案3】:

      我不确定您的问题到底是什么。您使用哪种状态机... 但也许这个链接有帮助: http://lists.boost.org/Archives/boost/2004/08/70900.php

      【讨论】:

      • 我的问题是 boost 为列表、向量等提供了最多 50 个条目的预生成头文件。我知道这个邮件列表条目,但我想自己生成预生成的头文件包含 60 个或更多条目的列表。
      猜你喜欢
      • 2012-02-23
      • 2011-08-26
      • 1970-01-01
      • 2022-10-16
      • 2023-03-29
      • 2012-01-04
      • 2016-07-02
      • 2019-04-10
      • 1970-01-01
      相关资源
      最近更新 更多