summaryrefslogtreecommitdiff
path: root/src/mongo/util/options_parser/value.cpp
blob: 2222355f1ac9410508f4cb8fd854b534ea128787 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Copyright 2013 10gen Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mongo/util/options_parser/value.h"

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

namespace mongo {
namespace optionenvironment {

    // Value implementation

    // Value access functions

    Status Value::get(StringVector_t* val) const {
        if (_type != StringVector) {
            StringBuilder sb;
            sb << "Attempting to get Value as type: StringVector, but Value is of type: "
               << typeToString();
            return Status(ErrorCodes::TypeMismatch, sb.str());
        }
        *val = _stringVectorVal;
        return Status::OK();
    }
    Status Value::get(StringMap_t* val) const {
        if (_type != StringMap) {
            StringBuilder sb;
            sb << "Attempting to get Value as type: StringMap, but Value is of type: "
               << typeToString();
            return Status(ErrorCodes::TypeMismatch, sb.str());
        }
        *val = _stringMapVal;
        return Status::OK();
    }
    Status Value::get(bool* val) const {
        if (_type != Bool) {
            StringBuilder sb;
            sb << "Attempting to get Value as type: Bool, but Value is of type: "
               << typeToString();
            return Status(ErrorCodes::TypeMismatch, sb.str());
        }
        *val = _boolVal;
        return Status::OK();
    }
    Status Value::get(double* val) const {
        if (_type != Double) {
            StringBuilder sb;
            sb << "Attempting to get Value as type: Double, but Value is of type: "
               << typeToString();
            return Status(ErrorCodes::TypeMismatch, sb.str());
        }
        *val = _doubleVal;
        return Status::OK();
    }
    Status Value::get(int* val) const {
        if (_type != Int) {
            StringBuilder sb;
            sb << "Attempting to get Value as type: Int, but Value is of type: "
               << typeToString();
            return Status(ErrorCodes::TypeMismatch, sb.str());
        }
        *val = _intVal;
        return Status::OK();
    }
    Status Value::get(long* val) const {
        if (_type == Long) {
            *val = _longVal;
            return Status::OK();
        }
        else if (_type == Int) {
            *val = _intVal;
            return Status::OK();
        }
        StringBuilder sb;
        sb << "Value of type: " << typeToString()
           << " is not convertible to type: Long";
        return Status(ErrorCodes::TypeMismatch, sb.str());
    }
    Status Value::get(std::string* val) const {
        if (_type != String) {
            StringBuilder sb;
            sb << "Attempting to get Value as type: string, but Value is of type: "
               << typeToString();
            return Status(ErrorCodes::TypeMismatch, sb.str());
        }
        *val = _stringVal;
        return Status::OK();
    }
    Status Value::get(unsigned long long* val) const {
        if (_type == UnsignedLongLong) {
            *val = _unsignedLongLongVal;
            return Status::OK();
        }
        else if (_type == Unsigned) {
            *val = _unsignedVal;
            return Status::OK();
        }
        StringBuilder sb;
        sb << "Value of type: " << typeToString()
           << " is not convertible to type: UnsignedLongLong";
        return Status(ErrorCodes::TypeMismatch, sb.str());
    }
    Status Value::get(unsigned* val) const {
        if (_type != Unsigned) {
            StringBuilder sb;
            sb << "Attempting to get Value as type: Unsigned, but Value is of type: "
               << typeToString();
            return Status(ErrorCodes::TypeMismatch, sb.str());
        }
        *val = _unsignedVal;
        return Status::OK();
    }

    // Value utility functions

    std::string Value::typeToString() const {
        switch (_type) {
            case StringVector: return "StringVector";
            case StringMap: return "StringMap";
            case Bool: return "Bool";
            case Double: return "Double";
            case Int: return "Int";
            case Long: return "Long";
            case String: return "String";
            case UnsignedLongLong: return "UnsignedLongLong";
            case Unsigned: return "Unsigned";
            case None: return "None";
            default: return "Unknown";
        }
    }
    bool Value::isEmpty() const {
        return _type == None;
    }
    bool Value::equal(const Value& otherVal) const {
        if (_type != otherVal._type) {
            return false;
        }
        switch (_type) {
            case StringVector: return _stringVectorVal == otherVal._stringVectorVal;
            case StringMap: return _stringMapVal == otherVal._stringMapVal;
            case Bool: return _boolVal == otherVal._boolVal;
            case Double: return _doubleVal == otherVal._doubleVal;
            case Int: return _intVal == otherVal._intVal;
            case Long: return _longVal == otherVal._longVal;
            case String: return _stringVal == otherVal._stringVal;
            case UnsignedLongLong: return _unsignedLongLongVal == otherVal._unsignedLongLongVal;
            case Unsigned: return _unsignedVal == otherVal._unsignedVal;
            case None: return true;
            default: return false; /* Undefined */
        }
    }

    // Dump the value as a string.  This function is used only for debugging purposes.
    std::string Value::toString() const {
        StringBuilder sb;
        switch (_type) {
            case StringVector:
                if (!_stringVectorVal.empty())
                {
                    // Convert all but the last element to avoid a trailing ","
                    for (StringVector_t::const_iterator iterator = _stringVectorVal.begin();
                         iterator != _stringVectorVal.end() - 1; iterator++) {
                        sb << *iterator << ",";
                    }

                    // Now add the last element with no delimiter
                    sb << _stringVectorVal.back();
                }
                break;
            case StringMap:
                if (!_stringMapVal.empty())
                {
                    // Convert all but the last element to avoid a trailing ","
                    if (_stringMapVal.begin() != _stringMapVal.end()) {
                        StringMap_t::const_iterator iterator;
                        StringMap_t::const_iterator it_last;
                        for (iterator = _stringMapVal.begin(), it_last = --_stringMapVal.end();
                             iterator != it_last; ++iterator) {
                            sb << iterator->first << ":" << iterator->second << ",";
                        }
                    }

                    // Now add the last element with no delimiter
                    sb << _stringMapVal.end()->first << ":" << _stringMapVal.end()->second;
                }
                break;
            case Bool: sb << _boolVal; break;
            case Double: sb << _doubleVal; break;
            case Int: sb << _intVal; break;
            case Long: sb << _longVal; break;
            case String: sb << _stringVal; break;
            case UnsignedLongLong: sb << _unsignedLongLongVal; break;
            case Unsigned: sb << _unsignedVal; break;
            case None: sb << "(not set)"; break;
            default: sb << "(undefined)"; break;
        }
        return sb.str();
    }
    const std::type_info& Value::type() const {
        switch (_type) {
            case StringVector: return typeid(StringVector_t);
            case StringMap: return typeid(StringMap_t);
            case Bool: return typeid(bool);
            case Double: return typeid(double);
            case Int: return typeid(int);
            case Long: return typeid(long);
            case String: return typeid(std::string);
            case UnsignedLongLong: return typeid(unsigned long long);
            case Unsigned: return typeid(unsigned);
            case None: return typeid(void);
            default: return typeid(void);
        }
    }

} // namespace optionenvironment
} // namespace mongo