Botan  1.11.15
src/lib/filters/basefilt.h
Go to the documentation of this file.
00001 /*
00002 * Basic Filters
00003 * (C) 1999-2007 Jack Lloyd
00004 * (C) 2013 Joel Low
00005 *
00006 * Botan is released under the Simplified BSD License (see license.txt)
00007 */
00008 
00009 #ifndef BOTAN_BASEFILT_H__
00010 #define BOTAN_BASEFILT_H__
00011 
00012 #include <botan/filter.h>
00013 #include <thread>
00014 
00015 namespace Botan {
00016 
00017 /**
00018 * BitBucket is a filter which simply discards all inputs
00019 */
00020 struct BOTAN_DLL BitBucket : public Filter
00021    {
00022    void write(const byte[], size_t) {}
00023 
00024    std::string name() const { return "BitBucket"; }
00025    };
00026 
00027 /**
00028 * This class represents Filter chains. A Filter chain is an ordered
00029 * concatenation of Filters, the input to a Chain sequentially passes
00030 * through all the Filters contained in the Chain.
00031 */
00032 
00033 class BOTAN_DLL Chain : public Fanout_Filter
00034    {
00035    public:
00036       void write(const byte input[], size_t length) { send(input, length); }
00037 
00038       std::string name() const;
00039 
00040       /**
00041       * Construct a chain of up to four filters. The filters are set
00042       * up in the same order as the arguments.
00043       */
00044       Chain(Filter* = nullptr, Filter* = nullptr,
00045             Filter* = nullptr, Filter* = nullptr);
00046 
00047       /**
00048       * Construct a chain from range of filters
00049       * @param filter_arr the list of filters
00050       * @param length how many filters
00051       */
00052       Chain(Filter* filter_arr[], size_t length);
00053    };
00054 
00055 /**
00056 * This class represents a fork filter, whose purpose is to fork the
00057 * flow of data. It causes an input message to result in n messages at
00058 * the end of the filter, where n is the number of forks.
00059 */
00060 class BOTAN_DLL Fork : public Fanout_Filter
00061    {
00062    public:
00063       void write(const byte input[], size_t length) { send(input, length); }
00064       void set_port(size_t n) { Fanout_Filter::set_port(n); }
00065 
00066       std::string name() const;
00067 
00068       /**
00069       * Construct a Fork filter with up to four forks.
00070       */
00071       Fork(Filter*, Filter*, Filter* = nullptr, Filter* = nullptr);
00072 
00073       /**
00074       * Construct a Fork from range of filters
00075       * @param filter_arr the list of filters
00076       * @param length how many filters
00077       */
00078       Fork(Filter* filter_arr[], size_t length);
00079    };
00080 
00081 /**
00082 * This class is a threaded version of the Fork filter. While this uses
00083 * threads, the class itself is NOT thread-safe. This is meant as a drop-
00084 * in replacement for Fork where performance gains are possible.
00085 */
00086 class BOTAN_DLL Threaded_Fork : public Fork
00087    {
00088    public:
00089       std::string name() const;
00090 
00091       /**
00092       * Construct a Threaded_Fork filter with up to four forks.
00093       */
00094       Threaded_Fork(Filter*, Filter*, Filter* = nullptr, Filter* = nullptr);
00095 
00096       /**
00097       * Construct a Threaded_Fork from range of filters
00098       * @param filter_arr the list of filters
00099       * @param length how many filters
00100       */
00101       Threaded_Fork(Filter* filter_arr[], size_t length);
00102 
00103       ~Threaded_Fork();
00104 
00105    protected:
00106       void set_next(Filter* f[], size_t n);
00107       void send(const byte in[], size_t length);
00108 
00109    private:
00110       void thread_delegate_work(const byte input[], size_t length);
00111       void thread_entry(Filter* filter);
00112 
00113       std::vector<std::shared_ptr<std::thread>> m_threads;
00114       std::unique_ptr<struct Threaded_Fork_Data> m_thread_data;
00115    };
00116 
00117 }
00118 
00119 #endif