OTest2
A C++ testing framework
userdata.h
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 #ifndef OTest2__INCLUDE_OTEST2_USERDATA_H_
21 #define OTest2__INCLUDE_OTEST2_USERDATA_H_
22 
23 #include <assert.h>
24 #include <memory>
25 #include <string>
26 #include <typeinfo>
27 #include <type_traits>
28 #include <utility>
29 
30 #include <otest2/exc.h>
31 #include <otest2/typetraits.h>
32 #include <otest2/utils.h>
33 
34 namespace OTest2 {
35 
40  private:
41  std::string name;
42 
43  public:
49  explicit UserDataMissingException(
50  const std::string& name_);
51 
56  UserDataMissingException&& exc_) noexcept;
57 
62 
63  /* -- avoid copying */
65  const UserDataMissingException&) = delete;
67  const UserDataMissingException&) = delete;
68 
69  /* -- exception interface */
70  virtual std::string reason() const override;
71 };
72 
78  private:
79  std::string name;
80  std::string actual_type;
81  std::string expected_type;
82 
83  public:
92  const std::string& name_,
93  const std::string& actual_,
94  const std::string& expected_);
95 
100  UserDataWrongTypeException&& exc_) noexcept;
101 
106 
107  /* -- avoid copying */
109  const UserDataWrongTypeException&) = delete;
111  const UserDataWrongTypeException&) = delete;
112 
113  /* -- exception interface */
114  virtual std::string reason() const override;
115 };
116 
120 class UserData {
121  private:
122  struct Impl;
123  Impl* pimpl;
124 
125  class Datum {
126  public:
127  Datum();
128  virtual ~Datum();
129 
130  /* -- avoid copying */
131  Datum(
132  const Datum&) = delete;
133  Datum& operator = (
134  const Datum&) = delete;
135  };
136 
137  template<typename Type_>
138  class TypedDatum : public Datum {
139  private:
140  Type_* datum;
141 
142  public:
143  explicit TypedDatum(
144  Type_* datum_) :
145  datum(datum_) {
146  assert(datum != nullptr);
147 
148  }
149 
150  virtual ~TypedDatum() = default;
151 
152  /* -- avoid copying */
153  TypedDatum(
154  const TypedDatum&) = delete;
155  TypedDatum& operator = (
156  const TypedDatum&) = delete;
157 
158  typename std::add_lvalue_reference<Type_>::type getValue() const noexcept {
159  return *datum;
160  }
161  };
162 
163  void doSetDatum(
164  const std::string& name_,
165  std::unique_ptr<Datum>&& datum_);
166  Datum* doGetDatum(
167  const std::string& name_) const;
168 
169 
170  public:
171  /* -- avoid copying */
172  UserData(
173  const UserData&) = delete;
175  const UserData&) = delete;
176 
180  UserData();
181 
185  ~UserData();
186 
194  template<typename Type_>
195  void setDatum(
196  const std::string& name_,
197  Type_* datum_) {
198  doSetDatum(name_, ::OTest2::make_unique<TypedDatum<Type_> >(datum_));
199  }
200 
211  template<typename Type_>
213  const std::string& name_) const {
214  Datum* d_(doGetDatum(name_));
215 
216  /* -- exact type */
217  auto* td1_(dynamic_cast<TypedDatum<typename std::remove_reference<Type_>::type>*>(d_));
218  if(td1_ != nullptr)
219  return td1_->getValue();
220 
221  /* -- remove const and volatile modifier */
222  auto* td2_(dynamic_cast<TypedDatum<
223  typename std::remove_cv<
224  typename std::remove_reference<Type_>::type>::type>*>(d_));
225  if(td2_ != nullptr)
226  return td2_->getValue();
227 
228  /* -- invalid type request */
230  name_,
231  typeid(*d_).name(),
232  typeid(TypedDatum<typename std::remove_reference<Type_>::type>).name());
233  }
234 };
235 
236 } /* -- namespace OTest2 */
237 
238 #endif /* -- OTest2__INCLUDE_OTEST2_USERDATA_H_ */
OTest2::UserDataWrongTypeException::~UserDataWrongTypeException
~UserDataWrongTypeException()
Dtor.
Definition: userdata.cpp:69
OTest2::UserDataMissingException::UserDataMissingException
UserDataMissingException(const std::string &name_)
Ctor.
Definition: userdata.cpp:29
OTest2::UserData::~UserData
~UserData()
Dtor.
Definition: userdata.cpp:113
typetraits.h
OTest2::UserDataWrongTypeException::UserDataWrongTypeException
UserDataWrongTypeException(const std::string &name_, const std::string &actual_, const std::string &expected_)
Ctor.
Definition: userdata.cpp:50
OTest2::make_unique
std::unique_ptr< Type_ > make_unique(Args_ &&... args_)
Replacement of the unique_ptr function missing in C++11.
Definition: utils.h:44
OTest2::UserData
An object keeping user data passed from user's custom main function.
Definition: userdata.h:120
OTest2::UserDataMissingException::operator=
UserDataMissingException & operator=(const UserDataMissingException &)=delete
utils.h
OTest2::UserDataWrongTypeException::operator=
UserDataWrongTypeException & operator=(const UserDataWrongTypeException &)=delete
exc.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::UserData::getDatum
std::add_lvalue_reference< Type_ >::type getDatum(const std::string &name_) const
Get user datum of specified type.
Definition: userdata.h:212
type
TokenType type
Definition: runnerfiltertags.cpp:502
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::UserData::UserData
UserData()
Ctor.
Definition: userdata.cpp:108
OTest2::UserData::setDatum
void setDatum(const std::string &name_, Type_ *datum_)
Set a user datum.
Definition: userdata.h:195