summaryrefslogtreecommitdiff
path: root/src/mongo/util/options_parser/value.h
blob: 0528deb76d3019b3eb1cf97785d770b66d2ed635 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#include <map>
#include <vector>

#include "mongo/base/status.h"
#include "mongo/bson/util/builder.h"

namespace mongo {
namespace optionenvironment {

class Constraint;
class KeyConstraint;

/**
 * Helper typedefs for the more complex C++ types supported by this Value class
 */
typedef std::map<std::string, std::string> StringMap_t;
typedef std::vector<std::string> StringVector_t;

typedef std::string Key;

/** A simple container interface for storing various C++ values.
 *
 *  Usage:
 *
 *  Value intVal(2);
 *  Value stringVal("string");
 *
 *  int intContents = 1;
 *  Status ret = stringVal.get(&intContents);
 *  // ret != Status::OK()
 *  // intContents is still 1
 *
 *  ret = intVal.get(&intContents);
 *  // ret == Status::OK()
 *  // intContents is now 2
 */
class Value {
public:
    // Constructors

    explicit Value() : _type(None) {}
    explicit Value(StringVector_t val) : _stringVectorVal(val), _type(StringVector) {}
    explicit Value(StringMap_t val) : _stringMapVal(val), _type(StringMap) {}
    explicit Value(bool val) : _boolVal(val), _type(Bool) {}
    explicit Value(double val) : _doubleVal(val), _type(Double) {}
    explicit Value(int val) : _intVal(val), _type(Int) {}
    explicit Value(long val) : _longVal(val), _type(Long) {}
    explicit Value(std::string val) : _stringVal(val), _type(String) {}
    explicit Value(const char* val) : _stringVal(val), _type(String) {}
    explicit Value(unsigned long long val) : _unsignedLongLongVal(val), _type(UnsignedLongLong) {}
    explicit Value(unsigned val) : _unsignedVal(val), _type(Unsigned) {}

    // Access interface

    Status get(StringVector_t* val) const;
    Status get(StringMap_t* val) const;
    Status get(bool* val) const;
    Status get(double* val) const;
    Status get(int* val) const;
    Status get(long* val) const;
    Status get(std::string* val) const;
    Status get(unsigned long long* val) const;
    Status get(unsigned* val) const;

    // Utility functions

    /**
     *  Return the value's type as a string
     */
    std::string typeToString() const;

    /**
     *  Return true if the value was created with the no argument constructor
     */
    bool isEmpty() const;

    /**
     *  Return true if the other Value equals this value, both in type and in contents
     *
     *  Two empty values are equal
     */
    bool equal(const Value&) const;

    /**
     *  Return the std::string representation of this Value.  This function is used only for
     *  debugging purposes and does not output data in an easily parseable format.
     */
    std::string toString() const;

    /**
     *  The functions below are the legacy interface to be consistent with boost::any during the
     *  transition period
     */

    /**
     *  Returns the contents of this Value as type T.  Throws AssertionException if the type
     *  does not match
     */
    template <typename T>
    T as() const;

    /**
     *  Return the type_info for this value
     */
    const std::type_info& type() const;

private:
    StringVector_t _stringVectorVal;
    StringMap_t _stringMapVal;
    std::string _stringVal;
    union {
        bool _boolVal;
        double _doubleVal;
        int _intVal;
        long _longVal;
        unsigned long long _unsignedLongLongVal;
        unsigned _unsignedVal;
    };

    // Types currently supported by Value
    enum Type {
        StringVector,      // std::vector<std::string>
        StringMap,         // std::map<std::string, std::string>
        Bool,              // bool
        Double,            // double
        Int,               // int
        Long,              // long
        String,            // std::string
        UnsignedLongLong,  // unsigned long long
        Unsigned,          // unsigned
        None,              // (not set)
    };

    Type _type;
};

template <typename T>
T Value::as() const {
    T valueType;

    Status ret = get(&valueType);
    if (!ret.isOK()) {
        StringBuilder message;
        message << "failed to extract typed value from Value container: " << ret.toString();
        uasserted(17114, message.str());
    }

    return valueType;
}

}  // namespace optionenvironment
}  // namespace mongo