Calculator
Extensible stack-based calculator primarily in library form
StackOperatorFactory.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 <vector>
16 #include <sstream>
17 #include <string>
18 
19 #include "StackOperatorFactory.h"
20 #include "StackOperator.h"
21 
22 namespace Calculator {
23 
25  public:
26  typedef std::shared_ptr<StackOperatorFactoryPimpl> Ptr;
27 
28  /** The creators available in this factory.*/
29  std::vector<StackOperatorCreator::Ptr> creators;
30  };
31 
34  {
35  }
36 
38  : pimpl(StackOperatorFactoryPimpl::Ptr(new StackOperatorFactoryPimpl(*(rhs.pimpl))))
39  {
40  }
41 
43  : pimpl(rhs.pimpl)
44  {
45  rhs.pimpl.reset();
46  }
47 
49  pimpl.reset(new StackOperatorFactoryPimpl(*(rhs.pimpl)));
50  return *this;
51  }
52 
54  pimpl = rhs.pimpl;
55  rhs.pimpl.reset();
56  return *this;
57  }
58 
59  void StackOperatorFactory::addCreator(std::shared_ptr<StackOperatorCreator> creator) {
60  pimpl->creators.push_back(creator);
61  }
62 
63  std::string StackOperatorFactory::getHelp() const {
64  std::ostringstream os;
65 
66  for(auto iter = pimpl->creators.begin(); pimpl->creators.end() != iter; ++iter) {
67  os << (*iter)->getHelp() << std::endl;
68  }
69 
70  return os.str();
71  }
72 
73  StackOperator::Ptr StackOperatorFactory::create(const std::string& str) const {
74  for(auto iter = pimpl->creators.begin(); pimpl->creators.end() != iter; ++iter) {
75  StackOperator::Ptr created = (*iter)->create(str);
76  if(created) {
77  return created;
78  }
79  }
80 
81  return StackOperator::Ptr();
82  }
83 
84 } // namespace Calculator
Factory to create StackOperators and help from StackOperatorCreators.
std::shared_ptr< StackOperator > create(const std::string &str) const
Try to create a StackOperator for the given str.
StackOperatorFactory & operator=(const StackOperatorFactory &rhs)
void addCreator(std::shared_ptr< StackOperatorCreator > creator)
Add creator as available after all previous creators.
std::vector< StackOperatorCreator::Ptr > creators
The creators available in this factory.
std::shared_ptr< StackOperatorFactoryPimpl > Ptr
std::shared_ptr< StackOperator > Ptr
Definition: StackOperator.h:31
Container of Calculator resources.