Limi
generics.h
1 /*
2  * Copyright 2016, IST Austria
3  *
4  * This file is part of Limi.
5  *
6  * Limi is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Limi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with Limi. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIMI_DATASTRUCTURES_GENERICS_H
21 #define LIMI_DATASTRUCTURES_GENERICS_H
22 
23 #include <ostream>
24 #include <functional>
25 #include <sstream>
26 
27 
28 typedef std::function<void(std::ostream&)> printable;
29 
30 namespace std {
31 inline std::ostream& operator<<(std::ostream& os, const printable& p) {
32  p(os);
33  return os;
34 }
35 
36 inline std::stringstream& operator<<(std::stringstream& os, const printable& p) {
37  p(os);
38  return os;
39 }
40 }
41 
42 namespace Limi {
43 
52 template< class Key >
53 struct independence;
54 
55 
65 template< class Key >
67  inline bool operator()(const Key& a, const Key& b) const {
68  return false;
69  }
70 };
71 
80 template< class Key >
81 struct printer_base {
82  virtual ~printer_base() {}
83  inline printable operator()(const Key& item) const {
84  return [&](std::ostream& o) {print(item, o);};
85  }
86 
92  virtual void print(const Key& item, std::ostream& out) const = 0;
93 };
94 
103 template< class Key >
104 struct printer : public printer_base<Key> {
105  virtual void print(const Key& item, std::ostream& out) const override {
106  out << item;
107  }
108 };
109 
110 }
111 
112 #endif // LIMI_DATASTRUCTURES_GENERICS_H
The printer base is what custom printers need to inherit from.
Definition: generics.h:81
The main namespace of the library.
Definition: antichain_algo.h:40
This class simply states that no symbols are independent.
Definition: generics.h:66
Definition: generics.h:30
The template for the independence relation.
Definition: generics.h:53
virtual void print(const Key &item, std::ostream &out) const override
Prints an item to the out stream.
Definition: generics.h:105
The default implementation of Limi::printer_base.
Definition: generics.h:104
virtual void print(const Key &item, std::ostream &out) const =0
Prints an item to the out stream.