From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- security/nss/gtests/nss_bogo_shim/config.h | 94 ++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 security/nss/gtests/nss_bogo_shim/config.h (limited to 'security/nss/gtests/nss_bogo_shim/config.h') diff --git a/security/nss/gtests/nss_bogo_shim/config.h b/security/nss/gtests/nss_bogo_shim/config.h new file mode 100644 index 0000000000..0e7fb5ed58 --- /dev/null +++ b/security/nss/gtests/nss_bogo_shim/config.h @@ -0,0 +1,94 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Generic command line flags system for NSS BoGo shim. This class +// could actually in principle handle other programs. The flags are +// defined in the consumer code. + +#ifndef config_h_ +#define config_h_ + +#include + +#include +#include +#include +#include +#include +#include + +// Abstract base class for a given config flag. +class ConfigEntryBase { + public: + ConfigEntryBase(const std::string& nm, const std::string& typ) + : name_(nm), type_(typ) {} + + virtual ~ConfigEntryBase() {} + + const std::string& type() const { return type_; } + virtual bool Parse(std::queue& args) = 0; + + protected: + bool ParseInternal(std::queue& args, std::vector& out); + bool ParseInternal(std::queue& args, std::string& out); + bool ParseInternal(std::queue& args, int& out); + bool ParseInternal(std::queue& args, bool& out); + + const std::string name_; + const std::string type_; +}; + +// Template specializations for the concrete flag types. +template +class ConfigEntry : public ConfigEntryBase { + public: + ConfigEntry(const std::string& name, T init) + : ConfigEntryBase(name, typeid(T).name()), value_(init) {} + T get() const { return value_; } + + bool Parse(std::queue& args) { + return ParseInternal(args, value_); + } + + private: + T value_; +}; + +// The overall configuration (I.e., the total set of flags). +class Config { + public: + enum Status { kOK, kUnknownFlag, kMalformedArgument, kMissingValue }; + + Config() : entries_() {} + + template + void AddEntry(const std::string& name, T init) { + entries_[name] = + std::unique_ptr(new ConfigEntry(name, init)); + } + + Status ParseArgs(int argc, char** argv); + + template + T get(const std::string& key) const { + auto e = entry(key); + assert(e->type() == typeid(T).name()); + return static_cast*>(e)->get(); + } + + private: + static std::string XformFlag(const std::string& arg); + + std::map> entries_; + + const ConfigEntryBase* entry(const std::string& key) const { + auto e = entries_.find(key); + if (e == entries_.end()) return nullptr; + return e->second.get(); + } +}; + +#endif // config_h_ -- cgit v1.2.3