souffle  2.0.2-371-g6315b36
Global.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2018 The Souffle Developers. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file Global.h
12  *
13  * Defines a configuration environment
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include <iostream>
20 #include <map>
21 #include <string>
22 #include <vector>
23 
24 namespace souffle {
25 
26 /* A simple table class, to be used as a base for others to extend from. */
27 template <typename K, typename V>
28 class BaseTable {
29 public:
30  /* Empty constructor. */
31  BaseTable() : _default(V()), _data(std::map<K, V>()) {}
32  /* Copy constructor. */
33  BaseTable(const BaseTable& other) {
34  data(other.data());
35  }
36  /* Assignment operator. */
37  BaseTable& operator=(const BaseTable& other) {
38  data(other.data());
39  return *this;
40  }
41  /* Set the raw data. */
42  const std::map<K, V>& data() const {
43  return _data;
44  }
45  /* Get the raw data. */
46  void data(const std::map<K, V>& otherData) {
47  _data = otherData;
48  }
49  /* Get a value by its key, if not found return a reference to an object of the value class called with an
50  * empty constructor. */
51  const V& get(const K& key) const {
52  return (has(key)) ? _data.at(key) : _default;
53  }
54  /* Get a value by its key, if not found return the specified value. */
55  const V& get(const K& key, const V& value) const {
56  return (has(key)) ? _data.at(key) : value;
57  }
58  /* Check the table has the specified key. */
59  bool has(const K& key) const {
60  return _data.find(key) != _data.end();
61  }
62  /* Check the table has the specified key and the specified value for that key. */
63  bool has(const K& key, const V& value) const {
64  return has(key) && _data.at(key) == value;
65  }
66  /* Set the entry in the table for the specified key to an object of the value class called with an empty
67  * constructor. */
68  void set(const K& key) {
69  _data[key] = _default;
70  }
71  /* Set the entry in the table for the specified key to the specified value. */
72  void set(const K& key, const V& value) {
73  _data[key] = value;
74  }
75  /* Erase the entry in the table for the specified key. */
76  void unset(const K& key) {
77  _data.erase(key);
78  }
79  /* Print the raw backing data to the specified stream. */
80  void print(std::ostream& os) {
81  os << _data << std::endl;
82  }
83 
84 private:
85  /* Default object made by empty constructor to return by reference. */
86  const V _default;
87  /* The raw data backing this table. */
88  std::map<K, V> _data;
89 };
90 
91 /* Struct to represent an option given to the main function by command line arguments. */
92 struct MainOption {
93  std::string longName; /* The long name for this option, e.g. 'option' for '--option'. */
94  char shortName; /* The short name for this option where a non-character option means none will be
95  displayed, e.g. 'o' for '-o'. */
96  std::string argument; /* The argument this option, e.g. if longName is 'option', shortName is 'o', and
97  argument is 'ARG', then we have '-o=ARG' and '--option=ARG'. */
98  std::string byDefault; /* The default value for this option, used if no this option is not specified as a
99  command line argument. */
100  bool takesMany; /* Whether this option takes many arguments, false for 'it takes only one' true for 'it
101  takes one or more'. */
102  std::string description; /* The description of what this option does, used in the help text produced
103  from the options. */
104 };
105 
106 /* The MainConfig class, used to handle the global configuration and the help text. */
107 class MainConfig : public BaseTable<std::string, std::string> {
108 public:
109  /* Empty constructor, does nothing. */
110  MainConfig() : BaseTable<std::string, std::string>() {}
111  /* The argument processing method, this takes the arguments provided to main, a header, a footer, and a
112  list of options.
113  From these, we construct the help text and the global configuration. See Global.cpp for details. */
114  void processArgs(int argc, char** argv, const std::string& header, const std::string& footer,
115  const std::vector<MainOption> mainOptions);
116  /* Obtain the help text as a string. Note that 'processArgs' must be called before this is used. */
117  const std::string& help() const {
118  return _help;
119  }
120 
121 private:
122  /* The help text, printed if there is an error in the command line arguments. */
123  std::string _help;
124 };
125 
126 /* The global class. Currently used to provide a singleton instance of the global config. This class may be
127  * used to isolate all globals. */
128 class Global {
129 public:
130  /* Deleted copy constructor. */
131  Global(const Global&) = delete;
132  /* Deleted assignment operator. */
133  Global& operator=(const Global&) = delete;
134  /* Obtain the global configuration. */
135  static MainConfig& config() {
136  static MainConfig _config;
137  return _config;
138  }
139 
140 private:
141  /* Private empty constructor, there is only one global instance. */
142  Global() = default;
143 };
144 } // namespace souffle
souffle::BaseTable::has
bool has(const K &key) const
Definition: Global.h:71
souffle::MainOption::takesMany
bool takesMany
Definition: Global.h:106
souffle::MainConfig::MainConfig
MainConfig()
Definition: Global.h:116
souffle::MainConfig::help
const std::string & help() const
Definition: Global.h:123
souffle::BaseTable::get
const V & get(const K &key) const
Definition: Global.h:63
souffle::map
auto map(const std::vector< A > &xs, F &&f)
Applies a function to each element of a vector and returns the results.
Definition: ContainerUtil.h:158
souffle::MainConfig::_help
std::string _help
Definition: Global.h:129
souffle::Global::Global
Global()=default
souffle::MainOption::description
std::string description
Definition: Global.h:108
souffle::BaseTable::operator=
BaseTable & operator=(const BaseTable &other)
Definition: Global.h:49
souffle::Global::operator=
Global & operator=(const Global &)=delete
souffle::BaseTable
Definition: Global.h:34
souffle::MainOption::longName
std::string longName
Definition: Global.h:99
souffle::BaseTable::print
void print(std::ostream &os)
Definition: Global.h:92
souffle::Global
Definition: Global.h:134
souffle::Global::config
static MainConfig & config()
Definition: Global.h:141
souffle::BaseTable::unset
void unset(const K &key)
Definition: Global.h:88
std
Definition: Brie.h:3053
souffle::MainConfig::processArgs
void processArgs(int argc, char **argv, const std::string &header, const std::string &footer, const std::vector< MainOption > mainOptions)
Definition: Global.cpp:35
souffle::MainOption
Definition: Global.h:98
souffle
Definition: AggregateOp.h:25
souffle::BaseTable::BaseTable
BaseTable()
Definition: Global.h:43
souffle::MainOption::shortName
char shortName
Definition: Global.h:100
souffle::BaseTable::data
const std::map< K, V > & data() const
Definition: Global.h:54
souffle::MainOption::argument
std::string argument
Definition: Global.h:102
souffle::MainOption::byDefault
std::string byDefault
Definition: Global.h:104
souffle::MainConfig
Definition: Global.h:113
souffle::BaseTable::set
void set(const K &key)
Definition: Global.h:80
souffle::BaseTable::_default
const V _default
Definition: Global.h:98
souffle::BaseTable::_data
std::map< K, V > _data
Definition: Global.h:100