21#ifndef G_NET_CLIENT_PTR_H
22#define G_NET_CLIENT_PTR_H
38 template <
typename T>
class ClientPtr ;
43 namespace ClientPtrImp
46 typename std::conditional<std::is_base_of<GNet::ExceptionSource,T>::value,bool,
void>::type =
true>
47 std::string exceptionSourceId( T * p )
49 return p ?
static_cast<ExceptionSource*
>(p)->exceptionSourceId() :
std::string() ;
52 typename std::conditional<std::is_base_of<GNet::ExceptionSource,T>::value,void,
bool>::type =
false>
53 std::string exceptionSourceId( T * )
58 typename std::conditional<std::is_base_of<GNet::Client,T>::value,bool,
void>::type =
true>
59 bool hasConnected( T * p )
61 return p ?
static_cast<Client*
>(p)->hasConnected() : false ;
64 typename std::conditional<std::is_base_of<GNet::Client,T>::value,void,
bool>::type =
false>
65 bool hasConnected( T * )
97 void eventSlot(
const std::string & ,
const std::string & ,
const std::string & ) ;
161 G_EXCEPTION( InvalidState ,
tx(
"invalid state of network client holder") ) ;
176 void reset( std::unique_ptr<T> p ) ;
187 const T *
get() const noexcept ;
193 const T * operator->() const ;
203 std::
string exceptionSourceId() const override ;
213 T * set(
std::nullptr_t ) noexcept ;
214 T * release() noexcept ;
215 void connectSignals( T & ) ;
216 void disconnectSignals( T & ) ;
220 bool m_has_connected {
false} ;
228 connectSignals( *m_p ) ;
233 template <
typename T>
245 G_WARNING(
"GNet::ClientPtr::onException: unhandled exception: " << e.what() ) ;
250 std::string reason = ( done || m_p->finished() ) ? std::string() : std::string(e.what()) ;
253 m_p->doOnDelete( e.what() , done ) ;
254 deleteSignal().emit( reason ) ;
257 deletedSignal().emit( reason ) ;
266 if( ClientPtrImp::hasConnected(m_p) ) m_has_connected = true ;
267 disconnectSignals( *m_p ) ;
271 connectSignals( *p ) ;
273 std::swap( p , m_p ) ;
282 if( ClientPtrImp::hasConnected(m_p) ) m_has_connected = true ;
283 disconnectSignals( *m_p ) ;
293 return set(
nullptr ) ;
305 delete set( p.release() ) ;
311 delete set(
nullptr ) ;
329 return m_p != nullptr ;
335 return m_has_connected ;
342 throw InvalidState() ;
350 throw InvalidState() ;
357 return GNet::ClientPtrImp::exceptionSourceId<T>( m_p ) ;
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(T *p)
Resets the pointer.
void reset(std::unique_ptr< T > p)
Resets the pointer.
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)
A briefer alternative to G::gettext_noop().