/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef NMM_BUFFERMANAGER_HPP
#define NMM_BUFFERMANAGER_HPP NMM_BUFFERMANAGER_HPP

#include "nmm/nmm_prefix.hpp"

#include "nmm/utils/thread/ThreadMutex.hpp"

#include "visibility.hpp"

namespace NMM {
  
  class Buffer;
  
  /** Superclass for managing buffers.
   *
   * This is a "fully abstract" class (an "interface") for
   * managing buffers. This is the superclass for all
   * classes which allocate, dispense, release and reuse
   * buffers.
   */
  class DLLEXPORT BufferManager {
  public:

    /** Constructor.
     */
    BufferManager();
    
    /** Requests a buffer.
     *
     * Request an unused buffer of a certain minimal size. 
     * If the buffer manager is able to comply with the request
     * (by reusing a buffer that is large enough, or by allocating
     * a new one), such a buffer is returned. Otherwise, null
     * is returned. This call does not block until an adequate
     * Buffer is available.
     *
     * The reference count is already increased to one inside
     * this function. A Node should NOT increase the reference
     * count of the returned Buffer unless it is really passing
     * it on multiple times!
     *
     * @param size the minimal size of the buffer.
     * @return a pointer to a Buffer object, or null if no buffer is available.
     */
    virtual Buffer* requestBuffer(const size_t size) const = 0;

    /** Release a buffer. 
     *
     * This is called from the Buffer class when
     * the corresponding buffer's reference count has dropped to 0.
     * @param buffer The buffer to be released.
     */
    virtual void releaseBuffer (Buffer* buffer) const = 0;

    /** Returns pointer to this BufferManager object.
     *
     * @return pointer to this BufferManager object
     */
    virtual BufferManager* request();

    /** Decrease reference count and release BufferManager, if it drops to 0.
     */
    virtual void release();

  protected:
    /** The main destructor, which is empty.
     *
     * The destructor is protected, because a BufferManager deletes
     * itself if the refernce counter is 0.
     */
    virtual ~BufferManager() {};

    /** Erase this BufferManager object.
     *
     * This method is protected, since it is called by release.
     */
    virtual void erase() const = 0;
    
  private:
    int ref_count;       ///< reference counter
    ThreadMutex mutex;   ///< thread mutex to protect reference count
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif
