Calculator
Extensible stack-based calculator primarily in library form
VariableSet.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2013, Komodo Does Inc
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7 - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 - Neither the name of the Komodo Does Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 
14 #include <memory>
15 #include <map>
16 #include <string>
17 
18 #include "VariableSet.h"
19 
20 namespace Calculator {
21 
23  public:
24  typedef std::shared_ptr<VariableSetPimpl> Ptr;
25  typedef std::map<std::string, std::shared_ptr<StackItem> > MapType;
26 
27  /** Map of name to value */
29  };
30 
32  : pimpl(VariableSetPimpl::Ptr(new VariableSetPimpl()))
33  {
34  }
35 
37  pimpl->map.clear();
38  }
39 
40  std::shared_ptr<StackItem> VariableSet::get(const std::string& name) const {
41  VariableSetPimpl::MapType::const_iterator found = pimpl->map.find(name);
42  if(pimpl->map.end() == found) {
43  return std::shared_ptr<StackItem>();
44  } else {
45  return found->second;
46  }
47  }
48 
49  void VariableSet::set(const std::string& name, std::shared_ptr<StackItem> value) {
50  pimpl->map[name] = value;
51  }
52 
53 } // namespace Calculator
void clear()
Clear all set variables.
Definition: VariableSet.cpp:36
VariableSet()
Construct with an empty variable set.
Definition: VariableSet.cpp:31
void set(const std::string &name, std::shared_ptr< StackItem > value)
Set the value of name, overriding existing values.
Definition: VariableSet.cpp:49
std::shared_ptr< StackItem > get(const std::string &name) const
Get the value of name, if any.
Definition: VariableSet.cpp:40
std::map< std::string, std::shared_ptr< StackItem > > MapType
Definition: VariableSet.cpp:25
MapType map
Map of name to value.
Definition: VariableSet.cpp:28
std::shared_ptr< VariableSetPimpl > Ptr
Definition: VariableSet.cpp:24
Container of Calculator resources.