Commit 28b2e498 authored by Mirko Bonadei's avatar Mirko Bonadei Committed by Commit Bot

Roll abseil_revision cd86d0d20a..aa468ad755.

Change Log:
https://chromium.googlesource.com/external/github.com/abseil/abseil-cpp/+log/cd86d0d20a..aa468ad755
Full diff:
https://chromium.googlesource.com/external/github.com/abseil/abseil-cpp/+/cd86d0d20a..aa468ad755

Bug: None
Change-Id: I60b77aab9d6bf82fb8f914ff67b07ae3edbdf51b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1599604
Commit-Queue: Mirko Bonadei <mbonadei@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#658528}
parent 020d874d
......@@ -4,7 +4,7 @@ URL: https://github.com/abseil/abseil-cpp
License: Apache 2.0
License File: LICENSE
Version: 0
Revision: cd86d0d20ab167c33b23d3875db68d1d4bad3a3b
Revision: aa468ad75539619b47979911297efbb629c52e44
Security Critical: yes
Description:
......
......@@ -20,6 +20,7 @@ add_subdirectory(base)
add_subdirectory(algorithm)
add_subdirectory(container)
add_subdirectory(debugging)
add_subdirectory(flags)
add_subdirectory(hash)
add_subdirectory(memory)
add_subdirectory(meta)
......
......@@ -969,7 +969,7 @@ class raw_hash_set {
// This overload kicks in when the argument is an rvalue of init_type. Its
// purpose is to handle brace-init-list arguments.
//
// flat_hash_set<std::string, int> s;
// flat_hash_map<std::string, int> s;
// s.insert({"abc", 42});
std::pair<iterator, bool> insert(init_type&& value) {
return emplace(std::move(value));
......
......@@ -201,6 +201,7 @@ list(APPEND ABSL_MSVC_FLAGS
"/wd4180"
"/wd4244"
"/wd4267"
"/wd4503"
"/wd4800"
)
......
......@@ -202,6 +202,7 @@ ABSL_MSVC_FLAGS = [
"/wd4180",
"/wd4244",
"/wd4267",
"/wd4503",
"/wd4800",
]
......
......@@ -163,8 +163,8 @@ COPT_VARS = {
"ABSL_LLVM_TEST_FLAGS":
LLVM_TEST_DISABLE_WARNINGS_FLAGS,
"ABSL_LLVM_EXCEPTIONS_FLAGS": ["-fexceptions"],
"ABSL_CLANG_CL_FLAGS": (
MSVC_BIG_WARNING_FLAGS + LLVM_DISABLE_WARNINGS_FLAGS + MSVC_DEFINES),
"ABSL_CLANG_CL_FLAGS":
(MSVC_BIG_WARNING_FLAGS + LLVM_DISABLE_WARNINGS_FLAGS + MSVC_DEFINES),
"ABSL_CLANG_CL_TEST_FLAGS":
LLVM_TEST_DISABLE_WARNINGS_FLAGS,
"ABSL_CLANG_CL_EXCEPTIONS_FLAGS":
......@@ -179,6 +179,8 @@ COPT_VARS = {
"/wd4244",
# conversion from 'size_t' to 'type', possible loss of data
"/wd4267",
# The decorated name was longer than the compiler limit
"/wd4503",
# forcing value to bool 'true' or 'false' (performance warning)
"/wd4800",
],
......
......@@ -70,8 +70,9 @@ class StarlarkStyle(object):
return "GENERATED_copts.bzl"
# Copt file generation
def copt_list(name, arg_list, style):
"""Copt file generation."""
make_line = lambda s: " \"" + s + "\"" + style.separator()
external_str_list = [make_line(s) for s in arg_list]
......
#
# Copyright 2019 The Abseil Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
load(
"//absl:copts/configure_copts.bzl",
"ABSL_DEFAULT_COPTS",
"ABSL_DEFAULT_LINKOPTS",
"ABSL_TEST_COPTS",
)
package(default_visibility = ["//visibility:public"])
licenses(["notice"]) # Apache 2.0
cc_library(
name = "internal",
srcs = [
"internal/program_name.cc",
],
hdrs = [
"internal/path_util.h",
"internal/program_name.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
visibility = [
"//absl/flags:__pkg__",
],
deps = [
"//absl/strings",
"//absl/synchronization",
],
)
cc_library(
name = "config",
srcs = [
"usage_config.cc",
],
hdrs = [
"config.h",
"usage_config.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":internal",
"//absl/base:core_headers",
"//absl/strings",
"//absl/synchronization",
],
)
cc_library(
name = "marshalling",
srcs = [
"marshalling.cc",
],
hdrs = [
"marshalling.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
"//absl/base:core_headers",
"//absl/strings",
"//absl/strings:str_format",
],
)
cc_library(
name = "handle",
srcs = [
"internal/commandlineflag.cc",
],
hdrs = [
"internal/commandlineflag.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
visibility = [
"//absl/flags:__pkg__",
],
deps = [
":config",
":marshalling",
"//absl/base",
"//absl/base:core_headers",
"//absl/strings",
"//absl/synchronization",
"//absl/types:optional",
],
)
cc_library(
name = "registry",
srcs = [
"internal/registry.cc",
"internal/type_erased.cc",
],
hdrs = [
"internal/registry.h",
"internal/type_erased.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
visibility = [
"//absl/flags:__pkg__",
],
deps = [
":config",
":handle",
"//absl/base",
"//absl/base:core_headers",
"//absl/base:dynamic_annotations",
"//absl/strings",
"//absl/synchronization",
],
)
cc_library(
name = "flag",
srcs = [
"flag.cc",
],
hdrs = [
"declare.h",
"flag.h",
"internal/flag.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":config",
":handle",
":marshalling",
":registry",
"//absl/base",
"//absl/base:core_headers",
"//absl/strings",
],
)
cc_library(
name = "usage",
srcs = [
"internal/usage.cc",
],
hdrs = [
"internal/usage.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
visibility = [
"//absl/flags:__pkg__",
],
deps = [
":config",
":flag",
":handle",
":internal",
"//absl/strings",
"//absl/synchronization",
],
)
cc_library(
name = "parse",
srcs = ["parse.cc"],
hdrs = [
"internal/parse.h",
"parse.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":config",
":flag",
":handle",
":internal",
":registry",
":usage",
"//absl/strings",
"//absl/synchronization",
],
)
############################################################################
# Unit tests in alpahabetical order.
cc_test(
name = "commandlineflag_test",
size = "small",
srcs = [
"internal/commandlineflag_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":flag",
":handle",
":registry",
"//absl/memory",
"//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "flag_test",
size = "small",
srcs = [
"flag_test.cc",
"flag_test_defs.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":flag",
"//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "marshalling_test",
size = "small",
srcs = [
"marshalling_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":marshalling",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "path_util_test",
size = "small",
srcs = [
"internal/path_util_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":internal",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "parse_test",
size = "small",
srcs = [
"parse_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":flag",
":parse",
"//absl/base",
"//absl/base:scoped_set_env",
"//absl/strings",
"//absl/types:span",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "program_name_test",
size = "small",
srcs = [
"internal/program_name_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":internal",
"//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "type_erased_test",
size = "small",
srcs = [
"internal/type_erased_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":flag",
":registry",
"//absl/memory",
"//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "usage_config_test",
size = "small",
srcs = [
"usage_config_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":config",
":internal",
"//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "usage_test",
size = "small",
srcs = [
"internal/usage_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":config",
":flag",
":internal",
":parse",
":usage",
"//absl/memory",
"//absl/strings",
"@com_google_googletest//:gtest",
],
)
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build_overrides/build.gni")
# Usage of Abseil in Chromium is guarded by an explicit opt-in list, before
# adding projects to this list please reach out to cxx@chromium.org and CC:
# - https://cs.chromium.org/chromium/src/third_party/abseil-cpp/OWNERS
#
# More information can be found at:
# https://docs.google.com/document/d/1DgS1-A3rzboTLjpf4m1sqkJgWjnY_Ru2dokk1X1vBDU
if (build_with_chromium) {
visibility = [
"//libassistant/*",
"//third_party/webrtc/*",
"//third_party/abseil-cpp/*",
"//third_party/googletest:gtest",
]
} else {
visibility = [ "*" ]
}
# Build targets in this module are marked as "testonly" because it is not clear
# how ABSL_FLAG will interact with //base/command_line.h.
# If this is a problem, feel free to remove "testonly" and use "assert_no_deps"
# on the main Chrome binary.
source_set("internal") {
testonly = true
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [
"//build/config/compiler:no_chromium_code",
"//third_party/abseil-cpp:absl_default_cflags_cc",
]
public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
sources = [
"internal/program_name.cc",
]
public = [
"internal/path_util.h",
"internal/program_name.h",
]
deps = [
"../strings",
"../synchronization",
]
visibility = []
visibility += [ ":*" ]
}
source_set("config") {
testonly = true
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [
"//build/config/compiler:no_chromium_code",
"//third_party/abseil-cpp:absl_default_cflags_cc",
]
public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
sources = [
"usage_config.cc",
]
public = [
"config.h",
"usage_config.h",
]
deps = [
":internal",
"../base:core_headers",
"../strings",
"../synchronization",
]
}
source_set("marshalling") {
testonly = true
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [
"//build/config/compiler:no_chromium_code",
"//third_party/abseil-cpp:absl_default_cflags_cc",
]
public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
sources = [
"marshalling.cc",
]
public = [
"marshalling.h",
]
deps = [
"../base:core_headers",
"../strings",
"../strings:str_format",
]
}
source_set("handle") {
testonly = true
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [
"//build/config/compiler:no_chromium_code",
"//third_party/abseil-cpp:absl_default_cflags_cc",
]
public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
sources = [
"internal/commandlineflag.cc",
]
public = [
"internal/commandlineflag.h",
]
deps = [
":config",
":marshalling",
"../base",
"../base:core_headers",
"../strings",
"../synchronization",
"../types:optional",
]
visibility = []
visibility += [ ":*" ]
}
source_set("registry") {
testonly = true
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [
"//build/config/compiler:no_chromium_code",
"//third_party/abseil-cpp:absl_default_cflags_cc",
]
public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
sources = [
"internal/registry.cc",
"internal/type_erased.cc",
]
public = [
"internal/registry.h",
"internal/type_erased.h",
]
deps = [
":config",
":handle",
"../base",
"../base:core_headers",
"../base:dynamic_annotations",
"../strings",
"../synchronization",
]
visibility = []
visibility += [ ":*" ]
}
source_set("flag") {
testonly = true
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [
"//build/config/compiler:no_chromium_code",
"//third_party/abseil-cpp:absl_default_cflags_cc",
]
public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
sources = [
"flag.cc",
]
public = [
"declare.h",
"flag.h",
"internal/flag.h",
]
deps = [
":config",
":handle",
":marshalling",
":registry",
"../base",
"../base:core_headers",
"../strings",
]
}
source_set("usage") {
testonly = true
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [
"//build/config/compiler:no_chromium_code",
"//third_party/abseil-cpp:absl_default_cflags_cc",
]
public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
sources = [
"internal/usage.cc",
]
public = [
"internal/usage.h",
]
deps = [
":config",
":flag",
":handle",
":internal",
"../strings",
"../synchronization",
]
visibility = []
visibility += [ ":*" ]
}
source_set("parse") {
testonly = true
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [
"//build/config/compiler:no_chromium_code",
"//third_party/abseil-cpp:absl_default_cflags_cc",
]
public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
sources = [
"parse.cc",
]
public = [
"internal/parse.h",
"parse.h",
]
deps = [
":config",
":flag",
":handle",
":internal",
":registry",
":usage",
"../strings",
"../synchronization",
]
}
#
# Copyright 2019 The Abseil Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Internal-only target, do not depend on directly.
absl_cc_library(
NAME
flags_internal
SRCS
"internal/program_name.cc"
HDRS
"internal/path_util.h"
"internal/program_name.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::strings
absl::synchronization
PUBLIC
)
absl_cc_library(
NAME
flags_config
SRCS
"usage_config.cc"
HDRS
"config.h"
"usage_config.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::flags_internal
absl::core_headers
absl::strings
absl::synchronization
)
absl_cc_library(
NAME
flags_marshalling
SRCS
"marshalling.cc"
HDRS
"marshalling.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::core_headers
absl::strings
absl::str_format
)
# Internal-only target, do not depend on directly.
absl_cc_library(
NAME
flags_handle
SRCS
"internal/commandlineflag.cc"
HDRS
"internal/commandlineflag.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::flags_config
absl::flags_marshalling
absl::base
absl::core_headers
absl::strings
absl::synchronization
absl::optional
)
# Internal-only target, do not depend on directly.
absl_cc_library(
NAME
flags_registry
SRCS
"internal/registry.cc"
"internal/type_erased.cc"
HDRS
"internal/registry.h"
"internal/type_erased.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::flags_config
absl::flags_handle
absl::base
absl::core_headers
absl::dynamic_annotations
absl::strings
absl::synchronization
)
absl_cc_library(
NAME
flags
SRCS
"flag.cc"
HDRS
"declare.h"
"flag.h"
"internal/flag.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::flags_config
absl::flags_handle
absl::flags_marshalling
absl::flags_registry
absl::base
absl::core_headers
absl::strings
)
# Internal-only target, do not depend on directly.
absl_cc_library(
NAME
flags_usage
SRCS
"internal/usage.cc"
HDRS
"internal/usage.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::flags_config
absl::flags
absl::flags_handle
absl::flags_internal
absl::strings
absl::synchronization
)
absl_cc_library(
NAME
flags_parse
SRCS
"parse.cc"
HDRS
"internal/parse.h"
"parse.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::flags_config
absl::flags
absl::flags_handle
absl::flags_internal
absl::flags_registry
absl::flags_usage
absl::strings
absl::synchronization
)
############################################################################
# Unit tests in alpahabetical order.
absl_cc_test(
NAME
flags_commandlineflag_test
SRCS
"internal/commandlineflag_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags
absl::flags_handle
absl::flags_registry
absl::memory
absl::strings
gtest_main
)
absl_cc_test(
NAME
flags_flag_test
SRCS
"flag_test.cc"
"flag_test_defs.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags
absl::strings
gtest_main
)
absl_cc_test(
NAME
flags_marshalling_test
SRCS
"marshalling_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags_marshalling
gtest_main
)
absl_cc_test(
NAME
flags_parse_test
SRCS
"parse_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags
absl::base
absl::flags_parse
absl::scoped_set_env
absl::span
absl::strings
gmock_main
)
absl_cc_test(
NAME
flags_path_util_test
SRCS
"internal/path_util_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags_internal
gtest_main
)
absl_cc_test(
NAME
flags_program_name_test
SRCS
"internal/program_name_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags_internal
absl::strings
gtest_main
)
absl_cc_test(
NAME
flags_type_erased_test
SRCS
"internal/type_erased_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags
absl::flags_registry
absl::memory
absl::strings
gtest_main
)
absl_cc_test(
NAME
flags_usage_config_test
SRCS
"usage_config_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags_config
absl::flags_internal
absl::strings
gtest_main
)
absl_cc_test(
NAME
flags_usage_test
SRCS
"internal/usage_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::flags_config
absl::flags
absl::flags_internal
absl::flags_parse
absl::flags_usage
absl::memory
absl::strings
gtest
)
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_FLAGS_CONFIG_H_
#define ABSL_FLAGS_CONFIG_H_
// Determine if we should strip string literals from the Flag objects.
#if !defined(ABSL_FLAGS_STRIP_NAMES)
// Non-mobile linux platforms don't strip string literals.
#if (defined(__linux__) || defined(__Fuchsia__)) && !defined(__ANDROID__)
#define ABSL_FLAGS_STRIP_NAMES 0
// So do Macs (not iOS or embedded Apple platforms).
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#if !TARGET_OS_IPHONE && !TARGET_OS_EMBEDDED
#define ABSL_FLAGS_STRIP_NAMES 0
#endif
// And Windows.
#elif defined(_WIN32)
#define ABSL_FLAGS_STRIP_NAMES 0
// And Myriad.
#elif defined(__myriad2__)
#define ABSL_FLAGS_STRIP_NAMES 0
#endif
#endif // !defined(ABSL_FLAGS_STRIP_NAMES)
#if ABSL_FLAGS_STRIP_NAMES
#if !defined(ABSL_FLAGS_STRIP_HELP)
#define ABSL_FLAGS_STRIP_HELP 1
#endif
#else
#if !defined(ABSL_FLAGS_STRIP_HELP)
#define ABSL_FLAGS_STRIP_HELP 0
#endif
#endif
#endif // ABSL_FLAGS_CONFIG_H_
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: declare.h
// -----------------------------------------------------------------------------
//
// This file defines the ABSL_DECLARE_FLAG macro, allowing you to declare an
// `absl::Flag` for use within a translation unit. You should place this
// declaration within the header file associated with the .cc file that defines
// and owns the `Flag`.
#ifndef ABSL_FLAGS_DECLARE_H_
#define ABSL_FLAGS_DECLARE_H_
#include "absl/strings/string_view.h"
namespace absl {
namespace flags_internal {
// absl::Flag<T> represents a flag of type 'T' created by ABSL_FLAG.
template <typename T>
class Flag;
} // namespace flags_internal
// Flag
//
// Forward declaration of the `absl::Flag` type for use in defining the macro.
template <typename T>
using Flag = flags_internal::Flag<T>;
} // namespace absl
// ABSL_DECLARE_FLAG()
//
// This macro is a convenience for declaring use of an `absl::Flag` within a
// translation unit. This macro should be used within a header file to
// declare usage of the flag within any .cc file including that header file.
//
// The ABSL_DECLARE_FLAG(type, name) macro expands to:
//
// extern absl::Flag<type> FLAGS_name;
#define ABSL_DECLARE_FLAG(type, name) extern ::absl::Flag<type> FLAGS_##name
#endif // ABSL_FLAGS_DECLARE_H_
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/flag.h"
#include <cstring>
namespace absl {
// We want to validate the type mismatch between type definition and
// declaration. The lock-free implementation does not allow us to do it,
// so in debug builds we always use the slower implementation, which always
// validates the type.
#ifndef NDEBUG
#define ABSL_FLAGS_ATOMIC_GET(T) \
T GetFlag(const absl::Flag<T>& flag) { \
T result; \
flag.internal.Read(&result, &flags_internal::FlagOps<T>); \
return result; \
}
#else
#define ABSL_FLAGS_ATOMIC_GET(T) \
T GetFlag(const absl::Flag<T>& flag) { \
const int64_t r = flag.internal.atomic.load(std::memory_order_acquire); \
if (r != flags_internal::CommandLineFlag::kAtomicInit) { \
T t; \
memcpy(&t, &r, sizeof(T)); \
return t; \
} \
T result; \
flag.internal.Read(&result, &flags_internal::FlagOps<T>); \
return result; \
}
#endif
ABSL_FLAGS_INTERNAL_FOR_EACH_LOCK_FREE(ABSL_FLAGS_ATOMIC_GET)
#undef ABSL_FLAGS_ATOMIC_GET
} // namespace absl
This diff is collapsed.
This diff is collapsed.
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file is used to test the mismatch of the flag type between definition
// and declaration. These are definitions. flag_test.cc contains declarations.
#include <string>
#include "absl/flags/flag.h"
ABSL_FLAG(int, mistyped_int_flag, 0, "");
ABSL_FLAG(std::string, mistyped_string_flag, "", "");
This diff is collapsed.
This diff is collapsed.
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/internal/commandlineflag.h"
#include "gtest/gtest.h"
#include "absl/flags/flag.h"
#include "absl/flags/internal/registry.h"
#include "absl/memory/memory.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
ABSL_FLAG(int, int_flag, 201, "int_flag help");
ABSL_FLAG(std::string, string_flag, "dflt",
absl::StrCat("string_flag", " help"));
ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
namespace {
namespace flags = absl::flags_internal;
class CommandLineFlagTest : public testing::Test {
protected:
void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
void TearDown() override { flag_saver_.reset(); }
private:
std::unique_ptr<flags::FlagSaver> flag_saver_;
};
TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
auto* flag_01 = flags::FindCommandLineFlag("int_flag");
ASSERT_TRUE(flag_01);
EXPECT_EQ(flag_01->Name(), "int_flag");
EXPECT_EQ(flag_01->Help(), "int_flag help");
EXPECT_EQ(flag_01->Typename(), "");
EXPECT_TRUE(!flag_01->IsRetired());
EXPECT_TRUE(flag_01->IsOfType<int>());
EXPECT_TRUE(absl::EndsWith(
flag_01->Filename(),
"absl/flags/internal/commandlineflag_test.cc"));
auto* flag_02 = flags::FindCommandLineFlag("string_flag");
ASSERT_TRUE(flag_02);
EXPECT_EQ(flag_02->Name(), "string_flag");
EXPECT_EQ(flag_02->Help(), "string_flag help");
EXPECT_EQ(flag_02->Typename(), "");
EXPECT_TRUE(!flag_02->IsRetired());
EXPECT_TRUE(flag_02->IsOfType<std::string>());
EXPECT_TRUE(absl::EndsWith(
flag_02->Filename(),
"absl/flags/internal/commandlineflag_test.cc"));
auto* flag_03 = flags::FindRetiredFlag("bool_retired_flag");
ASSERT_TRUE(flag_03);
EXPECT_EQ(flag_03->Name(), "bool_retired_flag");
EXPECT_EQ(flag_03->Help(), "");
EXPECT_EQ(flag_03->Typename(), "");
EXPECT_TRUE(flag_03->IsRetired());
EXPECT_TRUE(flag_03->IsOfType<bool>());
EXPECT_EQ(flag_03->Filename(), "RETIRED");
}
// --------------------------------------------------------------------
TEST_F(CommandLineFlagTest, TestValueAccessMethods) {
absl::SetFlag(&FLAGS_int_flag, 301);
auto* flag_01 = flags::FindCommandLineFlag("int_flag");
ASSERT_TRUE(flag_01);
EXPECT_EQ(flag_01->CurrentValue(), "301");
EXPECT_EQ(flag_01->DefaultValue(), "201");
absl::SetFlag(&FLAGS_string_flag, "new_str_value");
auto* flag_02 = flags::FindCommandLineFlag("string_flag");
ASSERT_TRUE(flag_02);
EXPECT_EQ(flag_02->CurrentValue(), "new_str_value");
EXPECT_EQ(flag_02->DefaultValue(), "dflt");
}
// --------------------------------------------------------------------
TEST_F(CommandLineFlagTest, TestSetFromStringCurrentValue) {
std::string err;
auto* flag_01 = flags::FindCommandLineFlag("int_flag");
EXPECT_FALSE(flag_01->on_command_line);
EXPECT_TRUE(flag_01->SetFromString("11", flags::SET_FLAGS_VALUE,
flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
EXPECT_FALSE(flag_01->on_command_line);
EXPECT_TRUE(flag_01->SetFromString("-123", flags::SET_FLAGS_VALUE,
flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
EXPECT_FALSE(flag_01->on_command_line);
EXPECT_TRUE(!flag_01->SetFromString("xyz", flags::SET_FLAGS_VALUE,
flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
EXPECT_EQ(err, "Illegal value 'xyz' specified for flag 'int_flag'");
EXPECT_FALSE(flag_01->on_command_line);
EXPECT_TRUE(!flag_01->SetFromString("A1", flags::SET_FLAGS_VALUE,
flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'");
EXPECT_FALSE(flag_01->on_command_line);
EXPECT_TRUE(flag_01->SetFromString("0x10", flags::SET_FLAGS_VALUE,
flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16);
EXPECT_FALSE(flag_01->on_command_line);
EXPECT_TRUE(flag_01->SetFromString("011", flags::SET_FLAGS_VALUE,
flags::kCommandLine, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
EXPECT_TRUE(flag_01->on_command_line);
EXPECT_TRUE(!flag_01->SetFromString("", flags::SET_FLAGS_VALUE,
flags::kProgrammaticChange, &err));
EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'");
auto* flag_02 = flags::FindCommandLineFlag("string_flag");
EXPECT_TRUE(flag_02->SetFromString("xyz", flags::SET_FLAGS_VALUE,
flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz");
EXPECT_TRUE(flag_02->SetFromString("", flags::SET_FLAGS_VALUE,
flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "");
}
// --------------------------------------------------------------------
TEST_F(CommandLineFlagTest, TestSetFromStringDefaultValue) {
std::string err;
auto* flag_01 = flags::FindCommandLineFlag("int_flag");
EXPECT_TRUE(flag_01->SetFromString("111", flags::SET_FLAGS_DEFAULT,
flags::kProgrammaticChange, &err));
EXPECT_EQ(flag_01->DefaultValue(), "111");
auto* flag_02 = flags::FindCommandLineFlag("string_flag");
EXPECT_TRUE(flag_02->SetFromString("abc", flags::SET_FLAGS_DEFAULT,
flags::kProgrammaticChange, &err));
EXPECT_EQ(flag_02->DefaultValue(), "abc");
}
// --------------------------------------------------------------------
TEST_F(CommandLineFlagTest, TestSetFromStringIfDefault) {
std::string err;
auto* flag_01 = flags::FindCommandLineFlag("int_flag");
EXPECT_TRUE(flag_01->SetFromString("22", flags::SET_FLAG_IF_DEFAULT,
flags::kProgrammaticChange, &err))
<< err;
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
EXPECT_TRUE(flag_01->SetFromString("33", flags::SET_FLAG_IF_DEFAULT,
flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
// EXPECT_EQ(err, "ERROR: int_flag is already set to 22");
// Reset back to default value
EXPECT_TRUE(flag_01->SetFromString("201", flags::SET_FLAGS_VALUE,
flags::kProgrammaticChange, &err));
EXPECT_TRUE(flag_01->SetFromString("33", flags::SET_FLAG_IF_DEFAULT,
flags::kProgrammaticChange, &err));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 201);
// EXPECT_EQ(err, "ERROR: int_flag is already set to 201");
}
} // namespace
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_FLAGS_INTERNAL_FLAG_H_
#define ABSL_FLAGS_INTERNAL_FLAG_H_
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/registry.h"
namespace absl {
namespace flags_internal {
// This is "unspecified" implementation of absl::Flag<T> type.
template <typename T>
class Flag {
public:
constexpr Flag(const char* name, const flags_internal::HelpGenFunc help_gen,
const char* filename,
const flags_internal::FlagMarshallingOpFn marshalling_op,
const flags_internal::InitialValGenFunc initial_value_gen)
: internal(name, flags_internal::HelpText::FromFunctionPointer(help_gen),
filename, &flags_internal::FlagOps<T>, marshalling_op,
initial_value_gen,
/*retired_arg=*/false, /*def_arg=*/nullptr,
/*cur_arg=*/nullptr) {}
// Not copyable/assignable.
Flag(const Flag<T>&) = delete;
Flag<T>& operator=(const Flag<T>&) = delete;
absl::string_view Name() const { return internal.Name(); }
std::string Help() const { return internal.Help(); }
std::string Filename() const { return internal.Filename(); }
absl::flags_internal::CommandLineFlag internal;
void SetCallback(const flags_internal::FlagCallback mutation_callback) {
internal.SetCallback(mutation_callback);
}
private:
// TODO(rogeeff): add these validations once UnparseFlag invocation is fixed
// for built-in types and when we cleanup existing code from operating on
// forward declared types.
// auto IsCopyConstructible(const T& v) -> decltype(T(v));
// auto HasAbslParseFlag(absl::string_view in, T* dst, std::string* err)
// -> decltype(AbslParseFlag(in, dst, GlobalStringADLGuard(err)));
// auto HasAbslUnparseFlag(const T& v) -> decltype(AbslUnparseFlag(v));
};
// This class facilitates Flag object registration and tail expression-based
// flag definition, for example:
// ABSL_FLAG(int, foo, 42, "Foo help").OnUpdate(NotifyFooWatcher);
template <typename T, bool do_register>
class FlagRegistrar {
public:
explicit FlagRegistrar(Flag<T>* flag) : flag_(flag) {
if (do_register) flags_internal::RegisterCommandLineFlag(&flag_->internal);
}
FlagRegistrar& OnUpdate(flags_internal::FlagCallback cb) && {
flag_->SetCallback(cb);
return *this;
}
// Make the registrar "die" gracefully as a bool on a line where registration
// happens. Registrar objects are intended to live only as temporary.
operator bool() const { return true; } // NOLINT
private:
Flag<T>* flag_; // Flag being registered (not owned).
};
// This struct and corresponding overload to MakeDefaultValue are used to
// facilitate usage of {} as default value in ABSL_FLAG macro.
struct EmptyBraces {};
template <typename T>
T* MakeFromDefaultValue(T t) {
return new T(std::move(t));
}
template <typename T>
T* MakeFromDefaultValue(EmptyBraces) {
return new T;
}
} // namespace flags_internal
} // namespace absl
#endif // ABSL_FLAGS_INTERNAL_FLAG_H_
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_FLAGS_INTERNAL_PARSE_H_
#define ABSL_FLAGS_INTERNAL_PARSE_H_
#include <string>
#include <vector>
#include "absl/flags/declare.h"
ABSL_DECLARE_FLAG(std::vector<std::string>, flagfile);
ABSL_DECLARE_FLAG(std::vector<std::string>, fromenv);
ABSL_DECLARE_FLAG(std::vector<std::string>, tryfromenv);
ABSL_DECLARE_FLAG(std::vector<std::string>, undefok);
namespace absl {
namespace flags_internal {
enum class ArgvListAction { kRemoveParsedArgs, kKeepParsedArgs };
enum class UsageFlagsAction { kHandleUsage, kIgnoreUsage };
enum class OnUndefinedFlag {
kIgnoreUndefined,
kReportUndefined,
kAbortIfUndefined
};
std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
ArgvListAction arg_list_act,
UsageFlagsAction usage_flag_act,
OnUndefinedFlag on_undef_flag);
} // namespace flags_internal
} // namespace absl
#endif // ABSL_FLAGS_INTERNAL_PARSE_H_
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
#define ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
namespace absl {
namespace flags_internal {
// A portable interface that returns the basename of the filename passed as an
// argument. It is similar to basename(3)
// <https://linux.die.net/man/3/basename>.
// For example:
// flags_internal::Basename("a/b/prog/file.cc")
// returns "file.cc"
// flags_internal::Basename("file.cc")
// returns "file.cc"
inline absl::string_view Basename(absl::string_view filename) {
auto last_slash_pos = filename.find_last_of("/\\");
return last_slash_pos == absl::string_view::npos
? filename
: filename.substr(last_slash_pos + 1);
}
// A portable interface that returns the directory name of the filename
// passed as an argument, including the trailing slash.
// Returns the empty string if a slash is not found in the input file name.
// For example:
// flags_internal::Package("a/b/prog/file.cc")
// returns "a/b/prog/"
// flags_internal::Package("file.cc")
// returns ""
inline absl::string_view Package(absl::string_view filename) {
auto last_slash_pos = filename.find_last_of("/\\");
return last_slash_pos == absl::string_view::npos
? absl::string_view()
: filename.substr(0, last_slash_pos + 1);
}
} // namespace flags_internal
} // namespace absl
#endif // ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/internal/path_util.h"
#include "gtest/gtest.h"
namespace {
namespace flags = absl::flags_internal;
TEST(FlagsPathUtilTest, TestBasename) {
EXPECT_EQ(flags::Basename(""), "");
EXPECT_EQ(flags::Basename("a.cc"), "a.cc");
EXPECT_EQ(flags::Basename("dir/a.cc"), "a.cc");
EXPECT_EQ(flags::Basename("dir1/dir2/a.cc"), "a.cc");
EXPECT_EQ(flags::Basename("../dir1/dir2/a.cc"), "a.cc");
EXPECT_EQ(flags::Basename("/dir1/dir2/a.cc"), "a.cc");
EXPECT_EQ(flags::Basename("/dir1/dir2/../dir3/a.cc"), "a.cc");
}
// --------------------------------------------------------------------
TEST(FlagsPathUtilTest, TestPackage) {
EXPECT_EQ(flags::Package(""), "");
EXPECT_EQ(flags::Package("a.cc"), "");
EXPECT_EQ(flags::Package("dir/a.cc"), "dir/");
EXPECT_EQ(flags::Package("dir1/dir2/a.cc"), "dir1/dir2/");
EXPECT_EQ(flags::Package("../dir1/dir2/a.cc"), "../dir1/dir2/");
EXPECT_EQ(flags::Package("/dir1/dir2/a.cc"), "/dir1/dir2/");
EXPECT_EQ(flags::Package("/dir1/dir2/../dir3/a.cc"), "/dir1/dir2/../dir3/");
}
} // namespace
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/internal/program_name.h"
#include <string>
#include "absl/flags/internal/path_util.h"
#include "absl/synchronization/mutex.h"
namespace absl {
namespace flags_internal {
ABSL_CONST_INIT static absl::Mutex program_name_guard(absl::kConstInit);
ABSL_CONST_INIT static std::string* program_name
ABSL_GUARDED_BY(program_name_guard) = nullptr;
std::string ProgramInvocationName() {
absl::MutexLock l(&program_name_guard);
return program_name ? *program_name : "UNKNOWN";
}
std::string ShortProgramInvocationName() {
absl::MutexLock l(&program_name_guard);
return program_name ? std::string(flags_internal::Basename(*program_name))
: "UNKNOWN";
}
void SetProgramInvocationName(absl::string_view prog_name_str) {
absl::MutexLock l(&program_name_guard);
if (!program_name)
program_name = new std::string(prog_name_str);
else
program_name->assign(prog_name_str.data(), prog_name_str.size());
}
} // namespace flags_internal
} // namespace absl
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_FLAGS_INTERNAL_PROGRAM_NAME_H_
#define ABSL_FLAGS_INTERNAL_PROGRAM_NAME_H_
#include <string>
#include "absl/strings/string_view.h"
// --------------------------------------------------------------------
// Program name
namespace absl {
namespace flags_internal {
// Returns program invocation name or "UNKNOWN" if `SetProgramInvocationName()`
// is never called. At the moment this is always set to argv[0] as part of
// library initialization.
std::string ProgramInvocationName();
// Returns base name for program invocation name. For example, if
// ProgramInvocationName() == "a/b/mybinary"
// then
// ShortProgramInvocationName() == "mybinary"
std::string ShortProgramInvocationName();
// Sets program invocation name to a new value. Should only be called once
// during program initialization, before any threads are spawned.
void SetProgramInvocationName(absl::string_view prog_name_str);
} // namespace flags_internal
} // namespace absl
#endif // ABSL_FLAGS_INTERNAL_PROGRAM_NAME_H_
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/internal/program_name.h"
#include "gtest/gtest.h"
#include "absl/strings/match.h"
namespace {
namespace flags = absl::flags_internal;
TEST(FlagsPathUtilTest, TestInitialProgamName) {
flags::SetProgramInvocationName("absl/flags/program_name_test");
std::string program_name = flags::ProgramInvocationName();
for (char& c : program_name)
if (c == '\\') c = '/';
#if !defined(__wasm__) && !defined(__asmjs__)
const std::string expect_name = "absl/flags/program_name_test";
const std::string expect_basename = "program_name_test";
#else
// For targets that generate javascript or webassembly the invocation name
// has the special value below.
const std::string expect_name = "this.program";
const std::string expect_basename = "this.program";
#endif
EXPECT_TRUE(absl::EndsWith(program_name, expect_name)) << program_name;
EXPECT_EQ(flags::ShortProgramInvocationName(), expect_basename);
}
TEST(FlagsPathUtilTest, TestProgamNameInterfaces) {
flags::SetProgramInvocationName("a/my_test");
EXPECT_EQ(flags::ProgramInvocationName(), "a/my_test");
EXPECT_EQ(flags::ShortProgramInvocationName(), "my_test");
absl::string_view not_null_terminated("absl/aaa/bbb");
not_null_terminated = not_null_terminated.substr(1, 10);
flags::SetProgramInvocationName(not_null_terminated);
EXPECT_EQ(flags::ProgramInvocationName(), "bsl/aaa/bb");
EXPECT_EQ(flags::ShortProgramInvocationName(), "bb");
}
} // namespace
This diff is collapsed.
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_FLAGS_INTERNAL_REGISTRY_H_
#define ABSL_FLAGS_INTERNAL_REGISTRY_H_
#include <functional>
#include <map>
#include <string>
#include "absl/base/macros.h"
#include "absl/flags/internal/commandlineflag.h"
// --------------------------------------------------------------------
// Global flags registry API.
namespace absl {
namespace flags_internal {
// CommandLineFlagInfo holds all information for a flag.
struct CommandLineFlagInfo {
std::string name; // the name of the flag
std::string type; // DO NOT use. Use flag->IsOfType<T>() instead.
std::string description; // the "help text" associated with the flag
std::string current_value; // the current value, as a std::string
std::string default_value; // the default value, as a std::string
std::string filename; // 'cleaned' version of filename holding the flag
bool has_validator_fn; // true if RegisterFlagValidator called on this flag
bool is_default; // true if the flag has the default value and
// has not been set explicitly from the cmdline
// or via SetCommandLineOption.
// nullptr for ABSL_FLAG. A pointer to the flag's current value
// otherwise. E.g., for DEFINE_int32(foo, ...), flag_ptr will be
// &FLAGS_foo.
const void* flag_ptr;
};
//-----------------------------------------------------------------------------
void FillCommandLineFlagInfo(CommandLineFlag* flag,
CommandLineFlagInfo* result);
//-----------------------------------------------------------------------------
CommandLineFlag* FindCommandLineFlag(absl::string_view name);
CommandLineFlag* FindCommandLineV1Flag(const void* flag_ptr);
CommandLineFlag* FindRetiredFlag(absl::string_view name);
// Executes specified visitor for each non-retired flag in the registry.
// Requires the caller hold the registry lock.
void ForEachFlagUnlocked(std::function<void(CommandLineFlag*)> visitor);
// Executes specified visitor for each non-retired flag in the registry. While
// callback are executed, the registry is locked and can't be changed.
void ForEachFlag(std::function<void(CommandLineFlag*)> visitor);
//-----------------------------------------------------------------------------
// Store the list of all flags in *OUTPUT, sorted by file.
void GetAllFlags(std::vector<CommandLineFlagInfo>* OUTPUT);
//-----------------------------------------------------------------------------
bool RegisterCommandLineFlag(CommandLineFlag*, const void* ptr = nullptr);
//-----------------------------------------------------------------------------
// Retired registrations:
//
// Retired flag registrations are treated specially. A 'retired' flag is
// provided only for compatibility with automated invocations that still
// name it. A 'retired' flag:
// - is not bound to a C++ FLAGS_ reference.
// - has a type and a value, but that value is intentionally inaccessible.
// - does not appear in --help messages.
// - is fully supported by _all_ flag parsing routines.
// - consumes args normally, and complains about type mismatches in its
// argument.
// - emits a complaint but does not die (e.g. LOG(ERROR)) if it is
// accessed by name through the flags API for parsing or otherwise.
//
// The registrations for a flag happen in an unspecified order as the
// initializers for the namespace-scope objects of a program are run.
// Any number of weak registrations for a flag can weakly define the flag.
// One non-weak registration will upgrade the flag from weak to non-weak.
// Further weak registrations of a non-weak flag are ignored.
//
// This mechanism is designed to support moving dead flags into a
// 'graveyard' library. An example migration:
//
// 0: Remove references to this FLAGS_flagname in the C++ codebase.
// 1: Register as 'retired' in old_lib.
// 2: Make old_lib depend on graveyard.
// 3: Add a redundant 'retired' registration to graveyard.
// 4: Remove the old_lib 'retired' registration.
// 5: Eventually delete the graveyard registration entirely.
//
// Returns bool to enable use in namespace-scope initializers.
// For example:
//
// static const bool dummy = base::RetiredFlag<int32_t>("myflag");
//
// Or to declare several at once:
//
// static bool dummies[] = {
// base::RetiredFlag<std::string>("some_string_flag"),
// base::RetiredFlag<double>("some_double_flag"),
// base::RetiredFlag<int32_t>("some_int32_flag")
// };
// Retire flag with name "name" and type indicated by ops.
bool Retire(FlagOpFn ops, FlagMarshallingOpFn marshalling_ops,
const char* name);
// Registered a retired flag with name 'flag_name' and type 'T'.
template <typename T>
inline bool RetiredFlag(const char* flag_name) {
return flags_internal::Retire(flags_internal::FlagOps<T>,
flags_internal::FlagMarshallingOps<T>,
flag_name);
}
// If the flag is retired, returns true and indicates in |*type_is_bool|
// whether the type of the retired flag is a bool.
// Only to be called by code that needs to explicitly ignore retired flags.
bool IsRetiredFlag(absl::string_view name, bool* type_is_bool);
//-----------------------------------------------------------------------------
// Saves the states (value, default value, whether the user has set
// the flag, registered validators, etc) of all flags, and restores
// them when the FlagSaver is destroyed.
//
// This class is thread-safe. However, its destructor writes to
// exactly the set of flags that have changed value during its
// lifetime, so concurrent _direct_ access to those flags
// (i.e. FLAGS_foo instead of {Get,Set}CommandLineOption()) is unsafe.
class FlagSaver {
public:
FlagSaver();
~FlagSaver();
FlagSaver(const FlagSaver&) = delete;
void operator=(const FlagSaver&) = delete;
// Prevents saver from restoring the saved state of flags.
void Ignore();
private:
class FlagSaverImpl* impl_; // we use pimpl here to keep API steady
};
} // namespace flags_internal
} // namespace absl
#endif // ABSL_FLAGS_INTERNAL_REGISTRY_H_
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/internal/type_erased.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/flags/config.h"
#include "absl/flags/usage_config.h"
#include "absl/strings/str_cat.h"
namespace absl {
namespace flags_internal {
bool GetCommandLineOption(absl::string_view name, std::string* value) {
if (name.empty()) return false;
assert(value);
CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
if (flag == nullptr || flag->IsRetired()) {
return false;
}
absl::MutexLock l(InitFlagIfNecessary(flag));
*value = flag->CurrentValue();
return true;
}
bool GetCommandLineFlagInfo(absl::string_view name,
CommandLineFlagInfo* OUTPUT) {
if (name.empty()) return false;
CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
if (flag == nullptr || flag->IsRetired()) {
return false;
}
assert(OUTPUT);
FillCommandLineFlagInfo(flag, OUTPUT);
return true;
}
CommandLineFlagInfo GetCommandLineFlagInfoOrDie(absl::string_view name) {
CommandLineFlagInfo info;
if (!GetCommandLineFlagInfo(name, &info)) {
ABSL_INTERNAL_LOG(FATAL, absl::StrCat("Flag '", name, "' does not exist"));
}
return info;
}
// --------------------------------------------------------------------
bool SetCommandLineOption(absl::string_view name, absl::string_view value) {
return SetCommandLineOptionWithMode(name, value,
flags_internal::SET_FLAGS_VALUE);
}
bool SetCommandLineOptionWithMode(absl::string_view name,
absl::string_view value,
FlagSettingMode set_mode) {
CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
if (!flag || flag->IsRetired()) return false;
std::string error;
if (!flag->SetFromString(value, set_mode, kProgrammaticChange, &error)) {
// Errors here are all of the form: the provided name was a recognized
// flag, but the value was invalid (bad type, or validation failed).
flags_internal::ReportUsageError(error, false);
return false;
}
return true;
}
// --------------------------------------------------------------------
bool IsValidFlagValue(absl::string_view name, absl::string_view value) {
CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
if (flag == nullptr) {
return false;
}
if (flag->IsRetired()) {
return true;
}
// No need to lock the flag since we are not mutating it.
void* obj = Clone(flag->op, flag->def);
std::string ignored_error;
const bool result =
flags_internal::Parse(flag->marshalling_op, value, obj, &ignored_error) &&
Validate(flag, obj);
Delete(flag->op, obj);
return result;
}
// --------------------------------------------------------------------
bool SpecifiedOnCommandLine(absl::string_view name) {
CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
if (flag != nullptr && !flag->IsRetired()) {
absl::MutexLock l(InitFlagIfNecessary(flag));
return flag->IsSpecifiedOnCommandLine();
}
return false;
}
} // namespace flags_internal
} // namespace absl
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_
#define ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_
#include <string>
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/registry.h"
// --------------------------------------------------------------------
// Registry interfaces operating on type erased handles.
namespace absl {
namespace flags_internal {
// If a flag named "name" exists, store its current value in *OUTPUT
// and return true. Else return false without changing *OUTPUT.
// Thread-safe.
bool GetCommandLineOption(absl::string_view name, std::string* value);
// If a flag named "name" exists, store its information in *OUTPUT
// and return true. Else return false without changing *OUTPUT.
// Thread-safe.
bool GetCommandLineFlagInfo(absl::string_view name,
CommandLineFlagInfo* OUTPUT);
// Returns the CommandLineFlagInfo of the flagname. exit() with an
// error code if name not found.
// Thread-safe.
CommandLineFlagInfo GetCommandLineFlagInfoOrDie(absl::string_view name);
// Set the value of the flag named "name" to value. If successful,
// returns true. If not successful (e.g., the flag was not found or
// the value is not a valid value), returns false.
// Thread-safe.
bool SetCommandLineOption(absl::string_view name, absl::string_view value);
bool SetCommandLineOptionWithMode(absl::string_view name,
absl::string_view value,
FlagSettingMode set_mode);
//-----------------------------------------------------------------------------
// Returns true iff all of the following conditions are true:
// (a) "name" names a registered flag
// (b) "value" can be parsed succesfully according to the type of the flag
// (c) parsed value passes any validator associated with the flag
bool IsValidFlagValue(absl::string_view name, absl::string_view value);
//-----------------------------------------------------------------------------
// Returns true iff a flag named "name" was specified on the command line
// (either directly, or via one of --flagfile or --fromenv or --tryfromenv).
//
// Any non-command-line modification of the flag does not affect the
// result of this function. So for example, if a flag was passed on
// the command line but then reset via SET_FLAGS_DEFAULT, this
// function will still return true.
bool SpecifiedOnCommandLine(absl::string_view name);
//-----------------------------------------------------------------------------
// If a flag with specified "name" exists and has type T, store
// its current value in *dst and return true. Else return false
// without touching *dst. T must obey all of the requirements for
// types passed to DEFINE_FLAG.
template <typename T>
inline bool GetByName(absl::string_view name, T* dst) {
CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
if (!flag) return false;
if (auto val = flag->Get<T>()) {
*dst = *val;
return true;
}
return false;
}
} // namespace flags_internal
} // namespace absl
#endif // ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/internal/type_erased.h"
#include <cmath>
#include "gtest/gtest.h"
#include "absl/flags/flag.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
ABSL_FLAG(int, int_flag, 1, "int_flag help");
ABSL_FLAG(std::string, string_flag, "dflt", "string_flag help");
ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
namespace {
namespace flags = absl::flags_internal;
class TypeErasedTest : public testing::Test {
protected:
void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
void TearDown() override { flag_saver_.reset(); }
private:
std::unique_ptr<flags::FlagSaver> flag_saver_;
};
// --------------------------------------------------------------------
TEST_F(TypeErasedTest, TestGetCommandLineOption) {
std::string value;
EXPECT_TRUE(flags::GetCommandLineOption("int_flag", &value));
EXPECT_EQ(value, "1");
EXPECT_TRUE(flags::GetCommandLineOption("string_flag", &value));
EXPECT_EQ(value, "dflt");
EXPECT_FALSE(flags::GetCommandLineOption("bool_retired_flag", &value));
EXPECT_FALSE(flags::GetCommandLineOption("unknown_flag", &value));
}
// --------------------------------------------------------------------
TEST_F(TypeErasedTest, TestSetCommandLineOption) {
EXPECT_TRUE(flags::SetCommandLineOption("int_flag", "101"));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
EXPECT_TRUE(flags::SetCommandLineOption("string_flag", "asdfgh"));
EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
EXPECT_FALSE(flags::SetCommandLineOption("bool_retired_flag", "true"));
EXPECT_FALSE(flags::SetCommandLineOption("unknown_flag", "true"));
}
// --------------------------------------------------------------------
TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAGS_VALUE) {
EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
flags::SET_FLAGS_VALUE));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
flags::SET_FLAGS_VALUE));
EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
flags::SET_FLAGS_VALUE));
EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
flags::SET_FLAGS_VALUE));
}
// --------------------------------------------------------------------
TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAG_IF_DEFAULT) {
EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
flags::SET_FLAG_IF_DEFAULT));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
// This semantic is broken. We return true instead of false. Value is not
// updated.
EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "202",
flags::SET_FLAG_IF_DEFAULT));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
flags::SET_FLAG_IF_DEFAULT));
EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
flags::SET_FLAG_IF_DEFAULT));
EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
flags::SET_FLAG_IF_DEFAULT));
}
// --------------------------------------------------------------------
TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAGS_DEFAULT) {
EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
flags::SET_FLAGS_DEFAULT));
EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
flags::SET_FLAGS_DEFAULT));
EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
flags::SET_FLAGS_DEFAULT));
EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
flags::SET_FLAGS_DEFAULT));
// This should be successfull, since flag is still is not set
EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "202",
flags::SET_FLAG_IF_DEFAULT));
EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 202);
}
// --------------------------------------------------------------------
TEST_F(TypeErasedTest, TestIsValidFlagValue) {
EXPECT_TRUE(flags::IsValidFlagValue("int_flag", "57"));
EXPECT_TRUE(flags::IsValidFlagValue("int_flag", "-101"));
EXPECT_FALSE(flags::IsValidFlagValue("int_flag", "1.1"));
EXPECT_TRUE(flags::IsValidFlagValue("string_flag", "#%^#%^$%DGHDG$W%adsf"));
EXPECT_TRUE(flags::IsValidFlagValue("bool_retired_flag", "true"));
}
} // namespace
This diff is collapsed.
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_FLAGS_INTERNAL_USAGE_H_
#define ABSL_FLAGS_INTERNAL_USAGE_H_
#include <iosfwd>
#include <string>
#include "absl/flags/declare.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/strings/string_view.h"
// --------------------------------------------------------------------
// Usage reporting interfaces
namespace absl {
namespace flags_internal {
// Sets the "usage" message to be used by help reporting routines.
// For example:
// absl::SetProgramUsageMessage(
// absl::StrCat("This program does nothing. Sample usage:\n", argv[0],
// " <uselessarg1> <uselessarg2>"));
// Do not include commandline flags in the usage: we do that for you!
// Note: Calling SetProgramUsageMessage twice will trigger a call to std::exit.
void SetProgramUsageMessage(absl::string_view new_usage_message);
// Returns the usage message set by SetProgramUsageMessage().
absl::string_view ProgramUsageMessage();
// --------------------------------------------------------------------
// The format to report the help messages in.
enum class HelpFormat {
kHumanReadable,
};
// Outputs the help message describing specific flag.
void FlagHelp(std::ostream& out, const flags_internal::CommandLineFlag& flag,
HelpFormat format = HelpFormat::kHumanReadable);
// Produces the help messages for all flags matching the filter. A flag matches
// the filter if it is defined in a file with a filename which includes
// filter string as a substring. You can use '/' and '.' to restrict the
// matching to a specific file names. For example:
// FlagsHelp(out, "/path/to/file.");
// restricts help to only flags which resides in files named like:
// .../path/to/file.<ext>
// for any extension 'ext'. If the filter is empty this function produces help
// messages for all flags.
void FlagsHelp(std::ostream& out, absl::string_view filter = {},
HelpFormat format = HelpFormat::kHumanReadable);
// --------------------------------------------------------------------
// If any of the 'usage' related command line flags (listed on the bottom of
// this file) has been set this routine produces corresponding help message in
// the specified output stream and returns:
// 0 - if "version" or "only_check_flags" flags were set and handled.
// 1 - if some other 'usage' related flag was set and handled.
// -1 - if no usage flags were set on a commmand line.
// Non negative return values are expected to be used as an exit code for a
// binary.
int HandleUsageFlags(std::ostream& out);
} // namespace flags_internal
} // namespace absl
ABSL_DECLARE_FLAG(bool, help);
ABSL_DECLARE_FLAG(bool, helpfull);
ABSL_DECLARE_FLAG(bool, helpshort);
ABSL_DECLARE_FLAG(bool, helppackage);
ABSL_DECLARE_FLAG(bool, version);
ABSL_DECLARE_FLAG(bool, only_check_args);
ABSL_DECLARE_FLAG(std::string, helpon);
ABSL_DECLARE_FLAG(std::string, helpmatch);
#endif // ABSL_FLAGS_INTERNAL_USAGE_H_
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
//
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: parse.h
// -----------------------------------------------------------------------------
//
// This file defines the main parsing function for Abseil flags:
// `absl::ParseCommandLine()`.
#ifndef ABSL_FLAGS_PARSE_H_
#define ABSL_FLAGS_PARSE_H_
#include <string>
#include <vector>
#include "absl/flags/internal/parse.h"
namespace absl {
// ParseCommandLine()
//
// Parses the set of command-line arguments passed in the `argc` (argument
// count) and `argv[]` (argument vector) parameters from `main()`, assigning
// values to any defined Abseil flags. (Any arguments passed after the
// flag-terminating delimiter (`--`) are treated as positional arguments and
// ignored.)
//
// Any command-line flags (and arguments to those flags) are parsed into Abseil
// Flag values, if those flags are defined. Any undefined flags will either
// return an error, or be ignored if that flag is designated using `undefok` to
// indicate "undefined is OK."
//
// Any command-line positional arguments not part of any command-line flag (or
// arguments to a flag) are returned in a vector, with the program invocation
// name at position 0 of that vector. (Note that this includes positional
// arguments after the flag-terminating delimiter `--`.)
//
// After all flags and flag arguments are parsed, this function looks for any
// built-in usage flags (e.g. `--help`), and if any were specified, it reports
// help messages and then exits the program.
std::vector<char*> ParseCommandLine(int argc, char* argv[]);
} // namespace absl
#endif // ABSL_FLAGS_PARSE_H_
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -35,10 +35,10 @@ cc_library(
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":city",
"//absl/base:core_headers",
"//absl/base:endian",
"//absl/container:fixed_array",
"//absl/hash:city",
"//absl/meta:type_traits",
"//absl/numeric:int128",
"//absl/strings",
......
......@@ -42,23 +42,28 @@
//
// Example:
//
// // Suppose we have a class `Circle` for which we want to add hashing
// // Suppose we have a class `Circle` for which we want to add hashing:
// class Circle {
// public:
// ...
// private:
// std::pair<int, int> center_;
// int radius_;
// };
//
// // To add hashing support to `Circle`, we simply need to add an ordinary
// // function `AbslHashValue()`, and return the combined hash state of the
// // existing hash state and the class state:
// public:
// ...
// private:
// std::pair<int, int> center_;
// int radius_;
// };
//
// // To add hashing support to `Circle`, we simply need to add a free
// // (non-member) function `AbslHashValue()`, and return the combined hash
// // state of the existing hash state and the class state. You can add such a
// // free function using a friend declaration within the body of the class:
// class Circle {
// public:
// ...
// template <typename H>
// friend H AbslHashValue(H h, const Circle& c) {
// return H::combine(std::move(h), c.center_, c.radius_);
// }
// ...
// };
//
// For more information, see Adding Type Support to `absl::Hash` below.
//
......@@ -239,7 +244,7 @@ using Hash = absl::hash_internal::Hash<T>;
// }
// private:
// virtual void HashValue(absl::HashState state) const = 0;
// };
// };
//
// class Impl : Interface {
// private:
......
......@@ -56,6 +56,7 @@
#include <cstdint>
#include <string>
#include <type_traits>
#include <vector>
#include "absl/base/port.h"
#include "absl/strings/numbers.h"
......@@ -268,6 +269,17 @@ class AlphaNum {
AlphaNum(T e) // NOLINT(runtime/explicit)
: AlphaNum(static_cast<typename std::underlying_type<T>::type>(e)) {}
// vector<bool>::reference and const_reference require special help to
// convert to `AlphaNum` because it requires two user defined conversions.
template <
typename T,
typename std::enable_if<
std::is_class<T>::value &&
(std::is_same<T, std::vector<bool>::reference>::value ||
std::is_same<T, std::vector<bool>::const_reference>::value)>::type* =
nullptr>
AlphaNum(T e) : AlphaNum(static_cast<bool>(e)) {} // NOLINT(runtime/explicit)
private:
absl::string_view piece_;
char digits_[numbers_internal::kFastToBufferSize];
......
......@@ -18,6 +18,7 @@
#include <cstdint>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "absl/strings/substitute.h"
......@@ -395,6 +396,18 @@ TEST(StrAppend, Basics) {
"No limit thanks to C++11's variadic templates");
}
TEST(StrCat, VectorBoolReferenceTypes) {
std::vector<bool> v;
v.push_back(true);
v.push_back(false);
std::vector<bool> const& cv = v;
// Test that vector<bool>::reference and vector<bool>::const_reference
// are handled as if the were really bool types and not the proxy types
// they really are.
std::string result = absl::StrCat(v[0], v[1], cv[0], cv[1]); // NOLINT
EXPECT_EQ(result, "1010");
}
#ifdef GTEST_HAS_DEATH_TEST
TEST(StrAppend, Death) {
std::string s = "self";
......
......@@ -507,7 +507,7 @@ class optional : private optional_internal::optional_data<T>,
template <typename U>
T value_or(U&& v) && { // NOLINT(build/c++11)
static_assert(std::is_move_constructible<value_type>::value,
"optional<T>::value_or: T must by copy constructible");
"optional<T>::value_or: T must by move constructible");
static_assert(std::is_convertible<U&&, value_type>::value,
"optional<T>::value_or: U must be convertible to T");
return static_cast<bool>(*this) ? std::move(**this)
......
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