flag.h
Go to the documentation of this file.
00001 //
00002 // Copyright 2019 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 
00016 #ifndef ABSL_FLAGS_INTERNAL_FLAG_H_
00017 #define ABSL_FLAGS_INTERNAL_FLAG_H_
00018 
00019 #include "absl/flags/internal/commandlineflag.h"
00020 #include "absl/flags/internal/registry.h"
00021 
00022 namespace absl {
00023 namespace flags_internal {
00024 
00025 // This is "unspecified" implementation of absl::Flag<T> type.
00026 template <typename T>
00027 class Flag {
00028  public:
00029   constexpr Flag(const char* name, const flags_internal::HelpGenFunc help_gen,
00030                  const char* filename,
00031                  const flags_internal::FlagMarshallingOpFn marshalling_op,
00032                  const flags_internal::InitialValGenFunc initial_value_gen)
00033       : internal(name, flags_internal::HelpText::FromFunctionPointer(help_gen),
00034                  filename, &flags_internal::FlagOps<T>, marshalling_op,
00035                  initial_value_gen,
00036                  /*retired_arg=*/false, /*def_arg=*/nullptr,
00037                  /*cur_arg=*/nullptr) {}
00038 
00039   // Not copyable/assignable.
00040   Flag(const Flag<T>&) = delete;
00041   Flag<T>& operator=(const Flag<T>&) = delete;
00042 
00043   absl::string_view Name() const { return internal.Name(); }
00044   std::string Help() const { return internal.Help(); }
00045   std::string Filename() const { return internal.Filename(); }
00046 
00047   absl::flags_internal::CommandLineFlag internal;
00048 
00049   void SetCallback(const flags_internal::FlagCallback mutation_callback) {
00050     internal.SetCallback(mutation_callback);
00051   }
00052 
00053  private:
00054   // TODO(rogeeff): add these validations once UnparseFlag invocation is fixed
00055   // for built-in types and when we cleanup existing code from operating on
00056   // forward declared types.
00057   //  auto IsCopyConstructible(const T& v) -> decltype(T(v));
00058   //  auto HasAbslParseFlag(absl::string_view in, T* dst, std::string* err)
00059   //      -> decltype(AbslParseFlag(in, dst, GlobalStringADLGuard(err)));
00060   //  auto HasAbslUnparseFlag(const T& v) -> decltype(AbslUnparseFlag(v));
00061 };
00062 
00063 // This class facilitates Flag object registration and tail expression-based
00064 // flag definition, for example:
00065 // ABSL_FLAG(int, foo, 42, "Foo help").OnUpdate(NotifyFooWatcher);
00066 template <typename T, bool do_register>
00067 class FlagRegistrar {
00068  public:
00069   explicit FlagRegistrar(Flag<T>* flag) : flag_(flag) {
00070     if (do_register) flags_internal::RegisterCommandLineFlag(&flag_->internal);
00071   }
00072 
00073   FlagRegistrar& OnUpdate(flags_internal::FlagCallback cb) && {
00074     flag_->SetCallback(cb);
00075     return *this;
00076   }
00077 
00078   // Make the registrar "die" gracefully as a bool on a line where registration
00079   // happens. Registrar objects are intended to live only as temporary.
00080   operator bool() const { return true; }  // NOLINT
00081 
00082  private:
00083   Flag<T>* flag_;  // Flag being registered (not owned).
00084 };
00085 
00086 // This struct and corresponding overload to MakeDefaultValue are used to
00087 // facilitate usage of {} as default value in ABSL_FLAG macro.
00088 struct EmptyBraces {};
00089 
00090 template <typename T>
00091 T* MakeFromDefaultValue(T t) {
00092   return new T(std::move(t));
00093 }
00094 
00095 template <typename T>
00096 T* MakeFromDefaultValue(EmptyBraces) {
00097   return new T;
00098 }
00099 
00100 }  // namespace flags_internal
00101 }  // namespace absl
00102 
00103 #endif  // ABSL_FLAGS_INTERNAL_FLAG_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14