MongoDB  2.7.0
options_parser/value.h
1 /* Copyright 2013 10gen Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <map>
19 #include <vector>
20 
21 #include "mongo/base/status.h"
22 #include "mongo/bson/util/builder.h"
23 
24 namespace mongo {
25 namespace optionenvironment {
26 
27  class Constraint;
28  class KeyConstraint;
29 
33  typedef std::map<std::string, std::string> StringMap_t;
34  typedef std::vector<std::string> StringVector_t;
35 
36  typedef std::string Key;
37 
54  class Value {
55  public:
56 
57  // Constructors
58 
59  explicit Value() : _type(None) { }
60  explicit Value(StringVector_t val) : _stringVectorVal(val), _type(StringVector) {}
61  explicit Value(StringMap_t val) : _stringMapVal(val), _type(StringMap) {}
62  explicit Value(bool val) : _boolVal(val), _type(Bool) { }
63  explicit Value(double val) : _doubleVal(val), _type(Double) { }
64  explicit Value(int val) : _intVal(val), _type(Int) { }
65  explicit Value(long val) : _longVal(val), _type(Long) { }
66  explicit Value(std::string val) : _stringVal(val), _type(String) { }
67  explicit Value(unsigned long long val) : _unsignedLongLongVal(val),
68  _type(UnsignedLongLong) { }
69  explicit Value(unsigned val) : _unsignedVal(val), _type(Unsigned) { }
70 
71  // Access interface
72 
73  Status get(StringVector_t* val) const;
74  Status get(StringMap_t* val) const;
75  Status get(bool* val) const;
76  Status get(double* val) const;
77  Status get(int* val) const;
78  Status get(long* val) const;
79  Status get(string* val) const;
80  Status get(unsigned long long* val) const;
81  Status get(unsigned* val) const;
82 
83  // Utility functions
84 
88  std::string typeToString() const;
89 
93  bool isEmpty() const;
94 
100  bool equal(const Value&) const;
101 
106  std::string toString() const;
107 
117  template <typename T>
118  T as() const;
119 
123  const std::type_info& type() const;
124 
125  private:
126  StringVector_t _stringVectorVal;
127  StringMap_t _stringMapVal;
128  std::string _stringVal;
129  union {
130  bool _boolVal;
131  double _doubleVal;
132  int _intVal;
133  long _longVal;
134  unsigned long long _unsignedLongLongVal;
135  unsigned _unsignedVal;
136  };
137 
138  // Types currently supported by Value
139  enum Type {
140  StringVector, // std::vector<std::string>
141  StringMap, // std::map<std::string, std::string>
142  Bool, // bool
143  Double, // double
144  Int, // int
145  Long, // long
146  String, // std::string
147  UnsignedLongLong, // unsigned long long
148  Unsigned, // unsigned
149  None, // (not set)
150  };
151 
152  Type _type;
153  };
154 
155  template <typename T>
156  T Value::as() const {
157  T valueType;
158 
159  Status ret = get(&valueType);
160  if (!ret.isOK()) {
161  StringBuilder message;
162  message << "failed to extract typed value from Value container: " << ret.toString();
163  throw MsgAssertionException(17114, message.str());
164  }
165 
166  return valueType;
167  }
168 
169 } // namespace optionenvironment
170 } // namespace mongo
std::string toString() const
Return the string representation of this Value.
Definition: value.cpp:166
std::string typeToString() const
Return the value's type as a string.
Definition: value.cpp:128
bool equal(const Value &) const
Return true if the other Value equals this value, both in type and in contents.
Definition: value.cpp:146
T as() const
The functions below are the legacy interface to be consistent with boost::any during the transition p...
Definition: options_parser/value.h:156
A simple container interface for storing various C++ values.
Definition: options_parser/value.h:54
bool isEmpty() const
Return true if the value was created with the no argument constructor.
Definition: value.cpp:143
Definition: string_map.h:46
stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 ...
Definition: builder.h:61
const std::type_info & type() const
Return the type_info for this value.
Definition: value.cpp:211
Definition: assert_util.h:157