OTest2
A C++ testing framework
userdata.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 #include <userdata.h>
20 
21 #include <unordered_map>
22 #include <memory>
23 #include <utility>
24 
25 #include <utils.h>
26 
27 namespace OTest2 {
28 
30  const std::string& name_) :
31  name(name_) {
32 
33 }
34 
36  UserDataMissingException&& exc_) noexcept :
37  Exception(std::move(exc_)),
38  name(std::move(exc_.name)) {
39 
40 }
41 
43 
44 }
45 
46 std::string UserDataMissingException::reason() const {
47  return "missing user datum '" + name + "'";
48 }
49 
51  const std::string& name_,
52  const std::string& actual_,
53  const std::string& expected_) :
54  name(name_),
55  actual_type(actual_),
56  expected_type(expected_) {
57 
58 }
59 
61  UserDataWrongTypeException&& exc_) noexcept :
62  Exception(std::move(exc_)),
63  name(std::move(exc_.name)),
64  actual_type(std::move(exc_.actual_type)),
65  expected_type(std::move(exc_.expected_type)) {
66 
67 }
68 
70 
71 }
72 
74  return "requested type of the user datum '" + name + "' is '"
75  + expected_type + "' but the type '" + actual_type + "' is set";
76 }
77 
78 UserData::Datum::Datum() {
79 
80 }
81 
82 UserData::Datum::~Datum() {
83 
84 }
85 
86 struct UserData::Impl {
87  typedef std::unordered_map<std::string, std::unique_ptr<UserData::Datum>> Data;
88  Data data;
89 
90  /* -- avoid copying */
91  Impl(
92  const Impl&) = delete;
93  Impl& operator = (
94  const Impl&) = delete;
95 
96  Impl();
97  ~Impl();
98 };
99 
100 UserData::Impl::Impl() {
101 
102 }
103 
104 UserData::Impl::~Impl() {
105 
106 }
107 
109  pimpl(new Impl) {
110 
111 }
112 
114  odelete(pimpl);
115 }
116 
117 void UserData::doSetDatum(
118  const std::string& name_,
119  std::unique_ptr<Datum>&& datum_) {
120  pimpl->data.erase(name_);
121  pimpl->data.emplace(name_, std::move(datum_));
122 }
123 
124 UserData::Datum* UserData::doGetDatum(
125  const std::string& name_) const {
126  auto iter_(pimpl->data.find(name_));
127  if(iter_ == pimpl->data.end())
128  throw UserDataMissingException(name_);
129  return (*iter_).second.get();
130 }
131 
132 } /* -- namespace OTest2 */
OTest2::UserDataWrongTypeException::~UserDataWrongTypeException
~UserDataWrongTypeException()
Dtor.
Definition: userdata.cpp:69
OTest2::UserDataMissingException::UserDataMissingException
UserDataMissingException(const std::string &name_)
Ctor.
Definition: userdata.cpp:29
userdata.h
OTest2::UserData::~UserData
~UserData()
Dtor.
Definition: userdata.cpp:113
OTest2::UserDataWrongTypeException::UserDataWrongTypeException
UserDataWrongTypeException(const std::string &name_, const std::string &actual_, const std::string &expected_)
Ctor.
Definition: userdata.cpp:50
utils.h
OTest2::UserDataWrongTypeException
An exception thrown if the expected type of a user datum is different than the actual one.
Definition: userdata.h:77
OTest2::UserDataMissingException
An exception thrown if user datum is missing.
Definition: userdata.h:39
OTest2::UserDataMissingException::~UserDataMissingException
~UserDataMissingException()
Dtor.
Definition: userdata.cpp:42
OTest2
Definition: assertbean.h:25
OTest2::UserData::operator=
UserData & operator=(const UserData &)=delete
OTest2::Exception
Generic OTest2 exception.
Definition: exc.h:31
OTest2::UserDataMissingException::reason
virtual std::string reason() const override
Get exception reason.
Definition: userdata.cpp:46
OTest2::UserDataWrongTypeException::reason
virtual std::string reason() const override
Get exception reason.
Definition: userdata.cpp:73
OTest2::odelete
void odelete(T_ *&object_)
Delete a pointer and set it invalid.
Definition: utils.h:34
OTest2::UserData::UserData
UserData()
Ctor.
Definition: userdata.cpp:108