Skip to content

Commit 6eaa8de

Browse files
committed
improved detects.cc & factory.cc
1 parent 34bf5e9 commit 6eaa8de

File tree

2 files changed

+204
-206
lines changed

2 files changed

+204
-206
lines changed

tests/detects.cc

+170-165
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,18 @@
88
// Created by Hedzr Yeh on 2021/10/13.
99
//
1010

11-
#include "undo_cxx.hh"
11+
#include "undo_cxx/detail/undo-if.hh"
1212

1313
#include <iomanip>
1414
#include <iostream>
15-
#include <math.h>
1615
#include <string>
1716

18-
#include <functional>
19-
#include <memory>
20-
#include <random>
21-
2217
#include <deque>
2318
#include <list>
24-
#include <optional>
2519
#include <queue>
2620
#include <stack>
21+
#include <type_traits>
22+
#include <utility>
2723
#include <vector>
2824

2925
namespace md {
@@ -76,7 +72,7 @@ namespace md {
7672

7773
struct foo {
7874
int const &bar(int &&) {
79-
static int vv_{0};
75+
static int const vv_{0};
8076
return vv_;
8177
}
8278
};
@@ -95,176 +91,185 @@ namespace md {
9591
// };
9692

9793
struct with_string {
98-
std::string to_string() const { return ""; }
94+
[[nodiscard]] std::string to_string() const { return ""; }
9995
};
10096

10197
struct wrong_string {
102-
const char *to_string() const { return ""; }
98+
[[nodiscard]] const char *to_string() const { return ""; }
10399
};
104100

105101
} // namespace md
106102

107-
void test_detect_2() {
108-
using namespace md;
109-
using namespace undo_cxx::traits;
110-
std::cout << '\n'
111-
<< has_string<int>::value << '\n'
112-
<< has_string<with_string>::value << '\n'
113-
<< has_string<wrong_string>::value << '\n';
103+
namespace {
104+
105+
static void test_detect_2() {
106+
using namespace md;
107+
using namespace undo_cxx::traits;
108+
std::cout << '\n'
109+
<< has_string<int>::value << '\n'
110+
<< has_string<with_string>::value << '\n'
111+
<< has_string<wrong_string>::value << '\n';
112+
113+
// static_assert(has_subscript_v<std::vector<int>, int &, std::size_t>);
114+
// static_assert(!has_subscript_v<std::vector<int>, int &, int>);
115+
116+
// using a = subscript_t<std::vector<int>, int &, size_t>;
117+
// using b = subscript_t<std::vector<int>, int &, int>;
118+
}
119+
120+
static void test_detect_1() {
121+
struct aa {
122+
void get() {}
123+
};
124+
struct bb {};
125+
struct cc {
126+
int get() { return 1; }
127+
};
128+
struct dd {
129+
std::string get(int, float, bool) { return "1"; }
130+
};
131+
struct ee {
132+
std::string get() { return "1"; }
133+
};
134+
135+
std::cout << std::boolalpha;
136+
std::cout << md::has_get<aa>::value << '\n';
137+
std::cout << md::has_get<bb>::value << '\n';
138+
std::cout << md::has_get<cc>::value << '\n';
139+
std::cout << md::has_get<dd>::value << '\n';
140+
std::cout << md::has_get<ee>::value << '\n';
141+
}
142+
143+
} // namespace
144+
145+
namespace dp::undo::bugs {
146+
template<typename T, class Container = std::stack<T>>
147+
class M {
148+
public:
149+
void test_emplace() {
150+
if constexpr (undo_cxx::traits::has_emplace_v<Container>) {
151+
std::cout << "M: emplace() exists." << '\n';
152+
} else {
153+
std::cout << "M: emplace() not exists." << '\n';
154+
}
155+
}
156+
void test_emplace_back() {
157+
if constexpr (undo_cxx::traits::has_emplace_back_v<Container>) {
158+
std::cout << "M: emplace_back() exists." << '\n';
159+
} else {
160+
std::cout << "M: emplace_back() not exists." << '\n';
161+
}
162+
}
163+
void test_push_back() {
164+
if constexpr (undo_cxx::traits::has_push_back_v<Container>) {
165+
std::cout << "M: push_back() exists." << '\n';
166+
} else {
167+
std::cout << "M: push_back() not exists." << '\n';
168+
}
169+
}
170+
void test_pop_back() {
171+
if constexpr (undo_cxx::traits::has_pop_back_v<Container>) {
172+
std::cout << "M: pop_back() exists." << '\n';
173+
} else {
174+
std::cout << "M: pop_back() not exists." << '\n';
175+
}
176+
}
177+
void test_begin() {
178+
using TX = std::list<std::string>;
179+
static_assert(undo_cxx::traits::has_begin_v<TX>);
180+
181+
if constexpr (undo_cxx::traits::has_begin_v<Container>) {
182+
std::cout << "M: begin() exists." << '\n';
183+
} else {
184+
std::cout << "M: begin() not exists." << '\n';
185+
}
186+
}
187+
void add(T &&t) {
188+
if constexpr (undo_cxx::traits::has_emplace_variadic_v<Container>) {
189+
_coll.emplace(t);
190+
std::cout << "M: emplace(...) invoked with " << std::quoted(t) << '\n';
191+
} else {
192+
std::cout << "M: emplace(...) not exists." << '\n';
193+
}
194+
}
195+
void add(T const &t) {
196+
if constexpr (undo_cxx::traits::has_push_v<Container>) {
197+
_coll.push(t);
198+
std::cout << "M: push() invoked with " << std::quoted(t) << '\n';
199+
} else {
200+
std::cout << "M: push() not exists." << '\n';
201+
}
202+
}
203+
void pop() {
204+
if constexpr (undo_cxx::traits::has_pop_v<Container>) {
205+
_coll.pop();
206+
std::cout << "M: pop() invoked." << '\n';
207+
} else {
208+
std::cout << "M: pop() not exists." << '\n';
209+
}
210+
}
211+
[[nodiscard]] T const &top() const {
212+
// if constexpr (undo_cxx::traits::has_top_func<Container>::value) {
213+
if constexpr (undo_cxx::traits::has_top_v<Container>) {
214+
auto &vv = _coll.top();
215+
std::cout << "M: top() invoked." << '\n';
216+
return vv;
217+
} else {
218+
std::cout << "M: top() not exists." << '\n';
219+
static T vv{"<<nothing>>"};
220+
return vv;
221+
}
222+
}
114223

115-
// static_assert(has_subscript_v<std::vector<int>, int &, std::size_t>);
116-
// static_assert(!has_subscript_v<std::vector<int>, int &, int>);
224+
private:
225+
Container _coll;
226+
};
227+
} // namespace dp::undo::bugs
117228

118-
// using a = subscript_t<std::vector<int>, int &, size_t>;
119-
// using b = subscript_t<std::vector<int>, int &, int>;
120-
}
229+
namespace {
121230

122-
void test_detect_1() {
123-
struct aa {
124-
void get() {}
125-
};
126-
struct bb {};
127-
struct cc {
128-
int get() { return 1; }
129-
};
130-
struct dd {
131-
std::string get(int, float, bool) { return "1"; }
132-
};
133-
struct ee {
134-
std::string get() { return "1"; }
135-
};
231+
template<typename T, class Container>
232+
static void tua_v1(dp::undo::bugs::M<T, Container> &m1) {
233+
static std::string const s1("data1"), s2("data2");
136234

137-
std::cout << std::boolalpha;
138-
std::cout << md::has_get<aa>::value << '\n';
139-
std::cout << md::has_get<bb>::value << '\n';
140-
std::cout << md::has_get<cc>::value << '\n';
141-
std::cout << md::has_get<dd>::value << '\n';
142-
std::cout << md::has_get<ee>::value << '\n';
143-
}
144-
145-
namespace dp { namespace undo { namespace bugs {
146-
template<typename T, class Container = std::stack<T>>
147-
class M {
148-
public:
149-
void test_emplace() {
150-
if constexpr (undo_cxx::traits::has_emplace_v<Container>) {
151-
std::cout << "M: emplace() exists." << '\n';
152-
} else {
153-
std::cout << "M: emplace() not exists." << '\n';
154-
}
155-
}
156-
void test_emplace_back() {
157-
if constexpr (undo_cxx::traits::has_emplace_back_v<Container>) {
158-
std::cout << "M: emplace_back() exists." << '\n';
159-
} else {
160-
std::cout << "M: emplace_back() not exists." << '\n';
161-
}
162-
}
163-
void test_push_back() {
164-
if constexpr (undo_cxx::traits::has_push_back_v<Container>) {
165-
std::cout << "M: push_back() exists." << '\n';
166-
} else {
167-
std::cout << "M: push_back() not exists." << '\n';
168-
}
169-
}
170-
void test_pop_back() {
171-
if constexpr (undo_cxx::traits::has_pop_back_v<Container>) {
172-
std::cout << "M: pop_back() exists." << '\n';
173-
} else {
174-
std::cout << "M: pop_back() not exists." << '\n';
175-
}
176-
}
177-
void test_begin() {
178-
using TX = std::list<std::string>;
179-
static_assert(undo_cxx::traits::has_begin_v<TX>);
180-
181-
if constexpr (undo_cxx::traits::has_begin_v<Container>) {
182-
std::cout << "M: begin() exists." << '\n';
183-
} else {
184-
std::cout << "M: begin() not exists." << '\n';
185-
}
186-
}
187-
void add(T &&t) {
188-
if constexpr (undo_cxx::traits::has_emplace_variadic_v<Container>) {
189-
_coll.emplace(t);
190-
std::cout << "M: emplace(...) invoked with " << std::quoted(t) << '\n';
191-
} else {
192-
std::cout << "M: emplace(...) not exists." << '\n';
193-
}
194-
}
195-
void add(T const &t) {
196-
if constexpr (undo_cxx::traits::has_push_v<Container>) {
197-
_coll.push(t);
198-
std::cout << "M: push() invoked with " << std::quoted(t) << '\n';
199-
} else {
200-
std::cout << "M: push() not exists." << '\n';
201-
}
202-
}
203-
void pop() {
204-
if constexpr (undo_cxx::traits::has_pop_v<Container>) {
205-
_coll.pop();
206-
std::cout << "M: pop() invoked." << '\n';
207-
} else {
208-
std::cout << "M: pop() not exists." << '\n';
209-
}
210-
}
211-
T const &top() const {
212-
// if constexpr (undo_cxx::traits::has_top_func<Container>::value) {
213-
if constexpr (undo_cxx::traits::has_top_v<Container>) {
214-
auto &vv = _coll.top();
215-
std::cout << "M: top() invoked." << '\n';
216-
return vv;
217-
} else {
218-
std::cout << "M: top() not exists." << '\n';
219-
static T vv{"<<nothing>>"};
220-
return vv;
221-
}
222-
}
223-
224-
private:
225-
Container _coll;
226-
};
227-
}}} // namespace dp::undo::bugs
228-
template<typename T, class Container>
229-
void tua_v1(dp::undo::bugs::M<T, Container> &m1) {
230-
static std::string s1("data1"), s2("data2");
231-
232-
m1.add(s1);
233-
m1.add(std::move(s2));
234-
m1.pop();
235-
std::cout << m1.top() << '\n';
236-
m1.test_emplace();
237-
m1.test_emplace_back();
238-
m1.test_push_back();
239-
m1.test_pop_back();
240-
std::cout << '\n';
241-
}
242-
void test_detect_has() {
243-
using namespace dp::undo::bugs;
244-
245-
std::cout << "------ test for std::stack" << '\n';
246-
M<std::string> m1;
247-
tua_v1(m1);
248-
249-
std::cout << "------ test for std::list" << '\n';
250-
M<std::string, std::list<std::string>> m2;
251-
tua_v1(m2);
252-
253-
std::cout << "------ test for std::vector" << '\n';
254-
M<std::string, std::vector<std::string>> m3;
255-
tua_v1(m3);
256-
257-
std::cout << "------ test for std::queue" << '\n';
258-
M<std::string, std::queue<std::string>> m4;
259-
tua_v1(m4);
260-
261-
std::cout << "------ test for std::deque" << '\n';
262-
M<std::string, std::deque<std::string>> m5;
263-
tua_v1(m5);
264-
}
235+
m1.add(s1);
236+
m1.add(std::move(s2));
237+
m1.pop();
238+
std::cout << m1.top() << '\n';
239+
m1.test_emplace();
240+
m1.test_emplace_back();
241+
m1.test_push_back();
242+
m1.test_pop_back();
243+
std::cout << '\n';
244+
}
265245

266-
int main() {
246+
static void test_detect_has() {
247+
using namespace dp::undo::bugs;
248+
249+
std::cout << "------ test for std::stack" << '\n';
250+
M<std::string> m1;
251+
tua_v1(m1);
252+
253+
std::cout << "------ test for std::list" << '\n';
254+
M<std::string, std::list<std::string>> m2;
255+
tua_v1(m2);
267256

257+
std::cout << "------ test for std::vector" << '\n';
258+
M<std::string, std::vector<std::string>> m3;
259+
tua_v1(m3);
260+
261+
std::cout << "------ test for std::queue" << '\n';
262+
M<std::string, std::queue<std::string>> m4;
263+
tua_v1(m4);
264+
265+
std::cout << "------ test for std::deque" << '\n';
266+
M<std::string, std::deque<std::string>> m5;
267+
tua_v1(m5);
268+
}
269+
270+
} // namespace
271+
272+
int main() {
268273
test_detect_has();
269274

270275
test_detect_1();

0 commit comments

Comments
 (0)