Skip to content

Commit dbf36ca

Browse files
authored
Add CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC() macro (#98)
* rename dangerous_export_object.hpp -> export_object_generic.hpp * CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT -> CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC * rename func name * rename macro name * is_dangerously_exportable_object<> -> is_exportable_object_generic<> * improve README.md * add CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT() for compatiblity
1 parent 92ffc89 commit dbf36ca

File tree

8 files changed

+60
-55
lines changed

8 files changed

+60
-55
lines changed

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ If you want to print a user-defined type, you can enable the library to print it
144144
[See Full Example Code](./readme/user-defined-class2.cpp)
145145

146146
```cpp
147-
CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(i, str());
147+
CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(i, str());
148148
```
149149
150150
![user-defined-class2.png](./readme/user-defined-class2.png)
@@ -322,7 +322,7 @@ The style of indents of the Container, Set and Map categories (See [Supported ty
322322
* Member functions to be displayed must be const.
323323
* Compile errors in this macro, such as ambiguous function calls, are never reported due to SFINAE.
324324
*/
325-
#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(members...)
325+
#define CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(members...)
326326

327327
/**
328328
* Make export_var() support enum T.
@@ -553,21 +553,19 @@ cpp_dump(my_enum_A);
553553
554554
![user-defined-enum.png](./readme/user-defined-enum.png)
555555
556-
#### 2. Use CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT() macro
556+
#### 2. Use CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC() macro
557557
558558
This macro enables `cpp_dump()` to print any type with specified members.
559559
This macro doesn't require the user type to be accessible from the top level (or even the type name).
560560
561-
However, if you do not use this macro carefully, it might cause ambiguous function call errors.
562-
Moreover, the errors are never reported due to SFINAE, and the user type will remain unsupported.
563-
564-
If you use this macro only once, it won't cause ambiguous function call errors.
561+
If you use this macro two or more times, you need to be careful of ambiguous function call errors.
562+
If such an error occurs, it won't be reported due to SFINAE.
565563
[See Full Example Code](./readme/user-defined-class2.cpp)
566564
567565
```cpp
568566
// At top level
569-
// CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(members...)
570-
CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(i, str());
567+
// CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(members...)
568+
CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(i, str());
571569
572570
// Anywhere
573571
struct class_A {
@@ -930,7 +928,7 @@ namespace cp = cpp_dump;
930928
#define CPP_DUMP_SET_OPTION(...)
931929
#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)
932930
#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)
933-
#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)
931+
#define CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(...)
934932
#endif
935933
936934
#include <bits/stdc++.h>
@@ -987,7 +985,7 @@ cpp_dump() prints variables recursively, so they can dump nested variables of an
987985
| User-defined | `CPP_DUMP_DEFINE_EXPORT_OBJECT(T, members...);` is at top level and the member functions to be displayed is const. | |
988986
| Enum | `CPP_DUMP_DEFINE_EXPORT_ENUM(T, members...);` is at top level. | |
989987
| Ostream | All of the above are not satisfied, `std::is_function_v<T> == false && std::is_member_pointer_v<T> == false`, and the function `std::ostream& operator<<(std::ostream&, const T &)` is defined. **The string representation of T must not be an empty string** (This makes manipulators unsupported). | |
990-
| User-defined2 | All of the above are not satisfied, T has all members specified by just one `CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(members...);` at top level, and the member functions to be displayed is const. | |
988+
| User-defined2 | All of the above are not satisfied, T has all members specified by just one `CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(members...);` at top level, and the member functions to be displayed is const. | |
991989
| Asterisk | All of the above are not satisfied, `cpp_dump::enable_asterisk == true` and the function `TypeExceptT operator*(const T &)` or the const member function `TypeExceptT T::operator*() const` is defined. | Iterators |
992990

993991
### Display example

hpp/export_var/dangerous_export_object.hpp renamed to hpp/export_var/export_object_generic.hpp

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,44 @@
1515
#include "../type_check.hpp"
1616
#include "./export_object_common.hpp"
1717

18-
#define _p_CPP_DUMP_EXPAND_FOR_DANGEROUS_EXPORT_OBJECT(member) value.member
19-
#define _p_CPP_DUMP_EXPAND_FOR_DANGEROUS_EXPORT_OBJECT2(member) append_output(#member, value.member)
18+
#define _p_CPP_DUMP_EXPAND_FOR_EXPORT_OBJECT_GENERIC(member) value.member
19+
#define _p_CPP_DUMP_EXPAND_FOR_EXPORT_OBJECT_GENERIC2(member) append_output(#member, value.member)
2020

2121
/**
2222
* Make export_var() support every type that has the specified members.
2323
* Member functions to be displayed must be const.
2424
* Compile errors in this macro, such as ambiguous function calls, are never reported due to SFINAE.
2525
*/
26-
#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...) \
27-
namespace cpp_dump { \
28-
\
29-
namespace _detail { \
30-
\
31-
template <typename T> \
32-
inline auto dangerous_export_object( \
33-
const T &value, \
34-
const std::string &indent, \
35-
std::size_t last_line_length, \
36-
std::size_t current_depth, \
37-
bool fail_on_newline, \
38-
const export_command &command \
39-
) -> decltype(_p_CPP_DUMP_EXPAND_VA(_p_CPP_DUMP_EXPAND_FOR_DANGEROUS_EXPORT_OBJECT, __VA_ARGS__), std::string()) { \
40-
std::string class_name = es::class_name(get_typename<T>()); \
41-
\
42-
_p_CPP_DUMP_DEFINE_EXPORT_OBJECT_COMMON1; \
43-
\
44-
_p_CPP_DUMP_EXPAND_VA(_p_CPP_DUMP_EXPAND_FOR_DANGEROUS_EXPORT_OBJECT2, __VA_ARGS__); \
45-
\
46-
_p_CPP_DUMP_DEFINE_EXPORT_OBJECT_COMMON2; \
47-
} \
48-
\
49-
} /* namespace _detail */ \
50-
\
26+
#define CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(...) \
27+
namespace cpp_dump { \
28+
\
29+
namespace _detail { \
30+
\
31+
template <typename T> \
32+
inline auto export_object_generic( \
33+
const T &value, \
34+
const std::string &indent, \
35+
std::size_t last_line_length, \
36+
std::size_t current_depth, \
37+
bool fail_on_newline, \
38+
const export_command &command \
39+
) -> decltype(_p_CPP_DUMP_EXPAND_VA(_p_CPP_DUMP_EXPAND_FOR_EXPORT_OBJECT_GENERIC, __VA_ARGS__), std::string()) { \
40+
std::string class_name = es::class_name(get_typename<T>()); \
41+
\
42+
_p_CPP_DUMP_DEFINE_EXPORT_OBJECT_COMMON1; \
43+
\
44+
_p_CPP_DUMP_EXPAND_VA(_p_CPP_DUMP_EXPAND_FOR_EXPORT_OBJECT_GENERIC2, __VA_ARGS__); \
45+
\
46+
_p_CPP_DUMP_DEFINE_EXPORT_OBJECT_COMMON2; \
47+
} \
48+
\
49+
} /* namespace _detail */ \
50+
\
5151
} // namespace cpp_dump
52+
53+
/**
54+
* This is deprecated.
55+
* Use CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC() instead.
56+
*/
57+
#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...) \
58+
CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(__VA_ARGS__)

hpp/export_var/export_var.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
#include "../export_command/export_command.hpp"
1313
#include "../type_check.hpp"
14-
#include "./dangerous_export_object.hpp"
1514
#include "./export_arithmetic.hpp"
1615
#include "./export_asterisk.hpp"
1716
#include "./export_container.hpp"
1817
#include "./export_enum.hpp"
1918
#include "./export_exception.hpp"
2019
#include "./export_map.hpp"
2120
#include "./export_object.hpp"
21+
#include "./export_object_generic.hpp"
2222
#include "./export_ostream.hpp"
2323
#include "./export_other/export_other.hpp"
2424
#include "./export_pointer.hpp"
@@ -79,8 +79,8 @@ std::string export_var(
7979
return export_other(value, indent, last_line_length, current_depth, fail_on_newline, command);
8080
} else if constexpr (is_ostream<T>) {
8181
return export_ostream(value, indent, last_line_length, current_depth, fail_on_newline, command);
82-
} else if constexpr (is_dangerously_exportable_object<T>) {
83-
return dangerous_export_object(
82+
} else if constexpr (is_exportable_object_generic<T>) {
83+
return export_object_generic(
8484
value, indent, last_line_length, current_depth, fail_on_newline, command
8585
);
8686
} else {

hpp/type_check.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,19 +249,19 @@ inline constexpr bool is_ostream = decltype(_is_ostream<_remove_cvref<T>>(0))::v
249249
struct export_command;
250250

251251
template <typename T>
252-
auto _is_dangerously_exportable_object(int) -> std::enable_if_t<
252+
auto _is_exportable_object_generic(int) -> std::enable_if_t<
253253
!_is_exportable_partial<T> && !is_ostream<T>,
254-
decltype(dangerous_export_object(std::declval<T>(), "", 0, 0, false, std::declval<export_command>()), std::true_type())>;
254+
decltype(export_object_generic(std::declval<T>(), "", 0, 0, false, std::declval<export_command>()), std::true_type())>;
255255
template <typename>
256-
std::false_type _is_dangerously_exportable_object(long);
256+
std::false_type _is_exportable_object_generic(long);
257257

258258
template <typename T>
259-
inline constexpr bool is_dangerously_exportable_object =
260-
decltype(_is_dangerously_exportable_object<_remove_cvref<T>>(0))::value;
259+
inline constexpr bool is_exportable_object_generic =
260+
decltype(_is_exportable_object_generic<_remove_cvref<T>>(0))::value;
261261

262262
template <typename T>
263263
auto _is_asterisk(int) -> std::enable_if_t<
264-
!_is_exportable_partial<T> && !is_ostream<T> && !is_dangerously_exportable_object<T>
264+
!_is_exportable_partial<T> && !is_ostream<T> && !is_exportable_object_generic<T>
265265
&& !std::is_same_v<_remove_cvref<decltype(*std::declval<const T>())>, T>,
266266
std::true_type>;
267267
template <typename>
@@ -271,8 +271,8 @@ template <typename T>
271271
inline constexpr bool is_asterisk = decltype(_is_asterisk<_remove_cvref<T>>(0))::value;
272272

273273
template <typename T>
274-
inline constexpr bool is_exportable = _is_exportable_partial<T> || is_ostream<T>
275-
|| is_dangerously_exportable_object<T> || is_asterisk<T>;
274+
inline constexpr bool is_exportable =
275+
_is_exportable_partial<T> || is_ostream<T> || is_exportable_object_generic<T> || is_asterisk<T>;
276276

277277
template <typename T>
278278
inline constexpr bool is_iterable_like = is_container<T> || is_map<T> || is_set<T> || is_tuple<T>;
@@ -291,7 +291,7 @@ const char* _get_typename() {
291291
#endif
292292
}
293293

294-
// Currently, used only by export_exception() and CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT()
294+
// Currently, used only by export_exception() and CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC()
295295
template <typename T>
296296
std::string get_typename() {
297297
#if defined(__GNUC__) && !defined(__clang__)

readme/user-defined-class2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "../dump.hpp"
44

5-
CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(i, str());
5+
CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(i, str());
66

77
int main() {
88
std::clog << std::endl;

test/dump_indent_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ struct self_reference_class {
3939
};
4040

4141
CPP_DUMP_DEFINE_EXPORT_OBJECT(class_a, int_a, str);
42-
CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(int_b, str);
43-
CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(str_member, pointer, ref);
42+
CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(int_b, str);
43+
CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(str_member, pointer, ref);
4444

4545
#define PRINT(x) (x), (clog << #x ";" << endl)
4646

test/dump_variable_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using namespace std;
1212
namespace cp = cpp_dump;
1313

14-
CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(member_var, member_func());
14+
CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(member_var, member_func());
1515

1616
namespace ns {
1717

test/odr_test.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct class_b {
1616

1717
CPP_DUMP_DEFINE_EXPORT_ENUM(enum_a, enum_a::s, enum_a::k);
1818
CPP_DUMP_DEFINE_EXPORT_OBJECT(class_a, int_a, a_str());
19-
CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(int_b, b_str());
19+
CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(int_b, b_str());
2020
CPP_DUMP_SET_OPTION_IN_GLOBAL(max_line_width, 100);
2121
CPP_DUMP_SET_OPTION_IN_GLOBAL(max_depth, 5);
2222
CPP_DUMP_SET_OPTION_IN_GLOBAL(max_iteration_count, 20);

0 commit comments

Comments
 (0)