21#ifndef G_NET_CLIENT_PTR_H
22#define G_NET_CLIENT_PTR_H
40 template <
typename T>
class ClientPtr ;
45 namespace ClientPtrImp
48 bool hasConnected( T * p ,
49 typename std::enable_if<std::is_convertible<T*,Client*>::value>::type * =
nullptr )
51 return p ?
static_cast<Client*
>(p)->hasConnected() : false ;
54 bool hasConnected( T * ,
55 typename std::enable_if<!std::is_convertible<T*,Client*>::value>::type * =
nullptr )
87 void eventSlot(
const std::string & ,
const std::string & ,
const std::string & ) ;
146 G_EXCEPTION( InvalidState ,
tx(
"invalid state of network client holder") )
172 const T *
get() const noexcept ;
178 const T * operator->() const ;
197 T * set(
std::nullptr_t ) noexcept ;
198 T * release() noexcept ;
199 void connectSignals( T & ) ;
200 void disconnectSignals( T & ) ;
204 bool m_has_connected {
false} ;
212 connectSignals( *m_p ) ;
217 template <
typename T>
229 G_WARNING(
"GNet::ClientPtr::onException: unhandled exception: " << e.what() ) ;
234 std::string reason = ( done || m_p->finished() ) ? std::string() : std::string(e.what()) ;
237 m_p->doOnDelete( e.what() , done ) ;
238 deleteSignal().emit( reason ) ;
241 deletedSignal().emit( reason ) ;
250 if( ClientPtrImp::hasConnected(m_p) ) m_has_connected = true ;
251 disconnectSignals( *m_p ) ;
255 connectSignals( *p ) ;
257 std::swap( p , m_p ) ;
266 if( ClientPtrImp::hasConnected(m_p) ) m_has_connected = true ;
267 disconnectSignals( *m_p ) ;
277 return set(
nullptr ) ;
289 delete set( p.release() ) ;
295 delete set(
nullptr ) ;
313 return m_p != nullptr ;
319 return m_has_connected ;
326 throw InvalidState() ;
334 throw InvalidState() ;
The non-template part of GNet::ClientPtr.
G::Slot::Signal< const std::string & > & deletedSignal() noexcept
A signal that is triggered after deleteSignal() once the client has been deleted and the ClientPtr is...
G::Slot::Signal< const std::string & > & deleteSignal() noexcept
A signal that is triggered as the client is deleted following an exception handled by this class.
ClientPtrBase()
Default constructor.
void eventSlot(const std::string &, const std::string &, const std::string &)
Emits an eventSignal().
G::Slot::Signal< const std::string &, const std::string &, const std::string & > & eventSignal() noexcept
A signal that is linked to the contained client's eventSignal().
A smart pointer class for GNet::Client or similar.
bool busy() const
Returns true if the pointer is not nullptr.
T * operator->()
Returns the pointer. Throws if deleted.
ClientPtr(T *p=nullptr)
Constructor. Takes ownership of the new-ed client.
T * get() noexcept
Returns the pointer, or nullptr if deleted.
bool hasConnected() const noexcept
Returns true if any Client owned by this smart pointer has ever successfully connected.
~ClientPtr() override
Destructor.
void reset() noexcept
Resets the pointer.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket/future event...
A mixin base class that identifies the source of an exception when delivered to GNet::ExceptionHandle...
A class that calls an exit function at the end of its scope.
Slot< Args... > slot(TSink &sink, void(TSink::*method)(Args...))
A factory function for Slot objects.
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().