OTest2
A C++ testing framework
testmarkfactory.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Ondrej Starek
3  *
4  * This file is part of OTest2.
5  *
6  * OTest2 is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License,
9  * or (at your option) any later version.
10  *
11  * OTest2 is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with OTest2. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <testmarkfactory.h>
21 
22 #include <assert.h>
23 #include <unordered_map>
24 
25 #include <exctestmarkin.h>
26 #include <testmarkbool.h>
27 #include <testmarkfloat.h>
28 #include <testmarkint.h>
29 #include <testmarklist.h>
30 #include <testmarkmap.h>
31 #include <testmarknull.h>
32 #include <testmarkstring.h>
33 #include <utils.h>
34 
35 namespace OTest2 {
36 
37 struct TestMarkFactory::Impl {
38  std::unordered_map<std::string, std::function<TestMarkPtr()>> ctors;
39 
40  /* -- avoid copying */
41  Impl(
42  const Impl&) = delete;
43  Impl& operator = (
44  const Impl&) = delete;
45 
46  Impl();
47  ~Impl();
48 };
49 
50 TestMarkFactory::Impl::Impl() {
51 
52 }
53 
54 TestMarkFactory::Impl::~Impl() {
55 
56 }
57 
58 void TestMarkFactory::doRegisterRecord(
59  const std::string& typemark_,
60  std::function<TestMarkPtr()> ctor_) {
61  auto result_(pimpl->ctors.insert({typemark_, ctor_}));
62  assert(result_.second);
63 }
64 
66  pimpl(new Impl()) {
67 
68  /* -- registration of standard testmark objects */
69  registerMark<TestMarkNull>();
70  registerMark<TestMarkBool>();
71  registerMark<TestMarkInt>();
72  registerMark<TestMarkFloat>();
73  registerMark<TestMarkString>();
74  registerMark<TestMarkList>();
75  registerMark<TestMarkMap>();
76 }
77 
79  odelete(pimpl);
80 }
81 
83  const std::string& typemark_) {
84  auto iter_(pimpl->ctors.find(typemark_));
85  if(iter_ == pimpl->ctors.end())
86  throw ExcTestMarkIn("unknown type mark '" + typemark_ + "'");
87  return (*iter_).second();
88 }
89 
90 } /* -- namespace OTest2 */
testmarkmap.h
testmarkstring.h
testmarkint.h
OTest2::TestMarkFactory::operator=
TestMarkFactory & operator=(const TestMarkFactory &)=delete
OTest2::ExcTestMarkIn
An exception thrown from the testmark deserializer.
Definition: exctestmarkin.h:33
exctestmarkin.h
utils.h
OTest2::TestMarkPtr
std::shared_ptr< TestMark > TestMarkPtr
Definition: testmarkptr.h:26
OTest2
Definition: assertbean.h:25
testmarklist.h
testmarknull.h
testmarkfactory.h
OTest2::TestMarkFactory::createMark
TestMarkPtr createMark(const std::string &typemark_)
Create new empty testmark.
Definition: testmarkfactory.cpp:82
OTest2::TestMarkFactory::TestMarkFactory
TestMarkFactory()
Ctor.
Definition: testmarkfactory.cpp:65
testmarkbool.h
testmarkfloat.h
OTest2::odelete
void odelete(T_ *&object_)
Delete a pointer and set it invalid.
Definition: utils.h:34
OTest2::TestMarkFactory::~TestMarkFactory
~TestMarkFactory()
Dtor.
Definition: testmarkfactory.cpp:78