MongoDB  2.7.0
environment.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 <boost/shared_ptr.hpp>
19 #include <map>
20 #include <vector>
21 
22 #include "mongo/base/status.h"
23 #include "mongo/db/jsobj.h"
24 #include "mongo/util/options_parser/value.h"
25 
26 namespace mongo {
27 namespace optionenvironment {
28 
29  class Constraint;
30  class KeyConstraint;
31 
32  typedef std::string Key;
33 
98  class Environment {
99  public:
100  Environment() : valid(false) { }
101  ~Environment() { }
102 
112  Status addKeyConstraint(KeyConstraint* keyConstraint);
113  Status addConstraint(Constraint* constraint);
114 
119  Status set(const Key& key, const Value& value);
120 
125  Status remove(const Key& key);
126 
131  Status setDefault(const Key& key, const Value& value);
132 
137  Status get(const Key& key, Value* value) const;
138 
142  template <typename T>
143  Status get(const Key& key, T* value_contents) const;
144 
149  Status validate(bool setValid=true);
150 
154  Status setAll(const Environment& other);
155 
163  bool count(const Key& key) const;
164 
169  Value operator[](const Key& key) const;
170 
201  BSONObj toBSON() const;
202 
203  /* Debugging */
204  void dump() const;
205 
206  protected:
207  std::vector<Constraint*> constraints;
208  std::vector<KeyConstraint*> keyConstraints;
209  std::map <Key, Value> values;
210  std::map <Key, Value> default_values;
211  bool valid;
212  };
213 
214  template <typename T>
215  Status Environment::get(const Key& get_key, T* get_value) const {
216  Value value;
217  Status ret = get(get_key, &value);
218  if (!ret.isOK()) {
219  return ret;
220  }
221  ret = value.get(get_value);
222  if (!ret.isOK()) {
223  StringBuilder sb;
224  sb << "Error getting value for key: \"" << get_key << "\": " << ret.toString();
225  return Status(ErrorCodes::NoSuchKey, sb.str());
226  }
227  return Status::OK();
228  }
229 
230 } // namespace optionenvironment
231 } // namespace mongo
BSONObj toBSON() const
Gets the BSON representation of this Environment.
Definition: environment.cpp:362
bool count(const Key &key) const
The functions below are the legacy interface to be consistent with boost::program_options::variables_...
Definition: environment.cpp:191
A Constraint validates an Environment.
Definition: constraints.h:32
Status set(const Key &key, const Value &value)
Add the Value to this Environment with the given Key.
Definition: environment.cpp:58
Status validate(bool setValid=true)
Runs all registered Constraints and returns the result.
Definition: environment.cpp:156
BSON classes.
A simple container interface for storing various C++ values.
Definition: options_parser/value.h:54
Status addKeyConstraint(KeyConstraint *keyConstraint)
These functions are to add Constraints and KeyConstraints which will be run against this environment ...
Definition: environment.cpp:31
Status get(const Key &key, Value *value) const
Populate the given Value with the Value stored for the given Key.
Definition: environment.cpp:42
stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 ...
Definition: builder.h:61
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition: bsonobj.h:77
Status setDefault(const Key &key, const Value &value)
Add a default Value to this Environment with the given Key.
Definition: environment.cpp:107
A KeyConstraint is a Constraint on a specific Key.
Definition: constraints.h:46
Status setAll(const Environment &other)
Sets all variables in the given Environment in this Environment.
Definition: environment.cpp:128
An Environment is a map of values that can be validated according to a set of registered constraints...
Definition: environment.h:98
Value operator[](const Key &key) const
Definition: environment.cpp:202