paludis  Version 2.6.0
digest_registry.hh
1 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
2 
3 /*
4  * Copyright (c) 2011, 2012 David Leverton
5  *
6  * This file is part of the Paludis package manager. Paludis is free software;
7  * you can redistribute it and/or modify it under the terms of the GNU General
8  * Public License version 2, as published by the Free Software Foundation.
9  *
10  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  * Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_DIGEST_REGISTRY_HH
21 #define PALUDIS_GUARD_PALUDIS_UTIL_DIGEST_REGISTRY_HH 1
22 
23 #include <iosfwd>
24 #include <string>
26 #include <paludis/util/pimp.hh>
28 #include <paludis/util/wrapped_forward_iterator-fwd.hh>
29 #include <functional>
30 #include <utility>
31 
32 namespace paludis
33 {
34  class DigestRegistry;
35 
36  extern template class Pimp<DigestRegistry>;
37  extern template class PALUDIS_VISIBLE Singleton<DigestRegistry>;
38 
40  public Singleton<DigestRegistry>
41  {
42  friend class Singleton<DigestRegistry>;
43 
44  public:
45  typedef std::function<std::string (std::istream &)> Function;
46 
47  Function get(const std::string & algo) const;
48 
49  struct AlgorithmsConstIteratorTag;
51 
52  AlgorithmsConstIterator begin_algorithms() const PALUDIS_ATTRIBUTE((warn_unused_result));
53  AlgorithmsConstIterator end_algorithms() const PALUDIS_ATTRIBUTE((warn_unused_result));
54 
55  template <typename T_>
57  {
58  public:
59  Registration(const std::string & algo)
60  {
61  get_instance()->register_function(algo, do_digest<T_>);
62  }
63  };
64 
65  private:
67  ~DigestRegistry();
68 
70 
71  void register_function(const std::string & algo, const Function & func);
72 
73  template <typename T_>
74  static std::string
75  do_digest(std::istream & stream)
76  {
77  T_ digest(stream);
78  return digest.hexsum();
79  }
80  };
81 }
82 
83 #endif
Definition: pimp.hh:51
Definition: about_metadata-fwd.hh:23
Definition: wrapped_forward_iterator-fwd.hh:26
Definition: digest_registry.hh:56
Definition: digest_registry.hh:39
#define PALUDIS_ATTRIBUTE(x)
Definition: attributes.hh:53
#define PALUDIS_VISIBLE
Definition: attributes.hh:59