Skip to content

Commit 90a4b30

Browse files
committed
update example2 description
1 parent 7786759 commit 90a4b30

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

example2 - decoupled pointer.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ==========================================================================
2-
// Implementation: easily changable pointer for code decoupling
2+
// Implementation: easily changable dynamic allocation policy.
33
// Just retype the pointer type in line 93.
44
// ==========================================================================
55

@@ -13,9 +13,13 @@ decltype( auto ) get_raw_pointer( Ptr&& ptr )
1313
else return ptr.operator->();
1414
}
1515

16+
// ======================================================================
1617
// a wrapper class of a pointer, for safe compilation.
17-
// if the wrapped class doesn't have required method, it does nothing.
18-
// you can get the original pointer type by using unwrapped ( pointer_impl< Ty, Ptr >::unwrapped ).
18+
// if the wrapped class doesn't have the required method,
19+
// try to behave like the expected behavior from the name.
20+
// if it fails, it eventually does nothing. ( or static assertion fails. )
21+
// you can get the original pointer type by using unwrapped ( pointer< Ty >::unwrapped ).
22+
// ======================================================================
1923
template < typename Ty, typename Ptr >
2024
class pointer_impl
2125
{
@@ -75,26 +79,27 @@ class pointer_impl
7579
const Ty& operator*() const noexcept { return *impl; }
7680
Ty* operator->() noexcept { return get_raw_pointer( impl ); }
7781
const Ty* operator->() const noexcept { return get_raw_pointer( impl ); }
78-
operator bool() const noexcept { return static_cast< bool >( get_raw_pointer( impl ) ); }
82+
operator bool() const noexcept { return static_cast<bool>( get_raw_pointer( impl ) ); }
7983

8084
// special member functions
8185
pointer_impl( Ty* impl = nullptr ) : impl{ impl } {}
8286

8387
private:
8488
Ptr impl;
8589
};
90+
// pointer_impl end =====================================================
8691

87-
// ======================================================
88-
// ******************************************************
92+
// ======================================================================
93+
// **********************************************************************
8994
// Decoupling by using keyword.
9095
// Memory allocation policy has the only dependancy on this code.
91-
// ======================================================
96+
// ======================================================================
9297
template < typename Ty >
9398
using pointer = pointer_impl< Ty, std::unique_ptr< Ty > >; // can change to diffrent pointer
9499
// if you add more pointer class,
95100
// you can also change to the pointer class.
96-
// ******************************************************
97-
// ======================================================
101+
// **********************************************************************
102+
// ======================================================================
98103

99104
template < typename Ty, typename ... Args >
100105
auto make( Args&& ... args )
@@ -142,11 +147,11 @@ int main()
142147
std::cout << "get_deleter() call =====================\n";
143148
std::cout << typeid( a.get_deleter() ).name() << '\n';
144149
std::cout << "========================================\n\n\n";
145-
150+
146151
std::cout << "print value ============================\n";
147152
std::cout << "value: " << *a << '\n';
148153
std::cout << "========================================\n\n\n";
149-
154+
150155
std::cout << "must call constructor and destructor ===\n";
151156
a.reset( new INT{ 5 } );
152157
std::cout << "========================================\n\n\n";

0 commit comments

Comments
 (0)