summaryrefslogtreecommitdiff
path: root/security/nss/gtests/nss_bogo_shim/config.h
blob: 3764783bcbcbb1998716877caabe946ebce89daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* -*- 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 <cassert>

#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <string>
#include <typeinfo>

// Abstract base class for a given config flag.
class ConfigEntryBase {
 public:
  ConfigEntryBase(const std::string& name, const std::string& type)
      : name_(name), type_(type) {}

  virtual ~ConfigEntryBase() {}

  const std::string& type() const { return type_; }
  virtual bool Parse(std::queue<const char*>* args) = 0;

 protected:
  bool ParseInternal(std::queue<const char*>* args, std::string* out);
  bool ParseInternal(std::queue<const char*>* args, int* out);
  bool ParseInternal(std::queue<const char*>* args, bool* out);

  const std::string name_;
  const std::string type_;
};

// Template specializations for the concrete flag types.
template <typename T>
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<const char*>* 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 <typename T>
  void AddEntry(const std::string& name, T init) {
    entries_[name] = std::unique_ptr<ConfigEntryBase>(
      new ConfigEntry<T>(name, init));
  }

  Status ParseArgs(int argc, char** argv);

  template <typename T>
  T get(const std::string& key) const {
    auto e = entry(key);
    assert(e->type() == typeid(T).name());
    return static_cast<const ConfigEntry<T>*>(e)->get();
  }

 private:
  static std::string XformFlag(const std::string& arg);

  std::map<std::string, std::unique_ptr<ConfigEntryBase>> 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_