1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #ifndef anna_comm_ReceiverFactory_hpp
10 #define anna_comm_ReceiverFactory_hpp
12 #include <anna/core/RuntimeException.hpp>
13 #include <anna/core/mt/Mutex.hpp>
14 #include <anna/config/defines.hpp>
27 Interface for receivers factories.
29 Is recommended (performance issues) to use #Recycler<T> as in the following example:
35 class MyReceiverFactory : public ReceiverFactory {
37 MyReceiverFactory () : ReceiverFactory ("MyReceiverFactory") {;}
40 anna::Recycler<MyReceiver> a_receivers;
42 Receiver* do_create () throw () { return a_receivers.create (); }
43 void do_release (Receiver* receiver) throw () { a_receivers.release (static_cast <MyReceiver*> (receiver)); }
48 In most of cases, is recommended to use an comm::ReceiverFactoryImpl instance with the receiver type used in our application.
51 \see ReceiverFactoryImpl
53 class ReceiverFactory : public Mutex {
56 Returns the logical name for this receiver factory.
57 @return logical name for this receiver factory.
59 const std::string& getName() const throw() { return a_name; }
62 Creates a receiver instance for this factory. Reuse resources when needed.
64 \return The new receiver instance.
65 \warning All the receivers obtained shall be released by mean #release.
67 Receiver* create() throw(RuntimeException);
70 Release the receiver instance provided.
72 \return The receiver instance to be released.
73 \warning The parameter should have been obtained by mean #create.
75 void release(Receiver* receiver) throw();
78 Returns a string with relevant information for this instance.
79 @return string with relevant information for this instance.
81 std::string asString() const
83 std::string msg("anna::comm::ReceiverFactory { Name: ");
89 Returns a XML document with relevant information for this instance.
90 \param parent XML node from which created data will depend on.
91 @return a XML document with relevant information for this instance.
93 xml::Node* asXML(xml::Node* parent) const throw();
98 \param name Logical name for this receivers factory.
100 ReceiverFactory(const char* name);
103 Creates the receiver instance if no other reusable instance is found. This method is
104 called from a critical section activated for this instance.
106 \return The new receiver instance.
107 \warning All the receivers obtained shall be released by mean #release.
109 virtual Receiver* do_create() throw() = 0;
112 Release the receiver provided. This method is called from a critical section activated
115 \param The receiver instance to be released.
117 virtual void do_release(Receiver* receiver) throw() = 0;
120 const std::string a_name;
122 ReceiverFactory(const ReceiverFactory&);
124 WHEN_SINGLETHREAD(Receiver* a_receiver;)