File: DisplayBase.hh

package info (click to toggle)
cadabra2 2.4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,732 kB
  • sloc: ansic: 133,450; cpp: 92,064; python: 1,530; javascript: 203; sh: 184; xml: 182; objc: 53; makefile: 51
file content (56 lines) | stat: -rw-r--r-- 1,569 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once

#include <string>
#include <sstream>
#include "Props.hh"
#include "Storage.hh"

namespace cadabra {

	/// \ingroup display
	///
	/// Base class for all display classes.  A key difficulty with
	/// printing is to figure out when to print additional brackets for
	/// objects which would otherwise not render correctly. For example, a
	/// sum inside a product need brackets, but it does not need brackets
	/// when it is given as an argument to a function, because then the
	/// brackets are already there from the function call.

	class DisplayBase {
		public:
			DisplayBase(const Kernel&, const Ex&);

			void output(std::ostream&);
			void output(std::ostream&, Ex::iterator);

			virtual void dispatch(std::ostream&, Ex::iterator)=0;

		protected:
			/// Determine if a node needs extra brackets around it. Uses context from the
			/// parent node if necessary. Has to be implemented in a derived class, because
			/// the answer depends on the printing method (e.g. `(a+b)/c` needs brackets
			/// when printed like this, but does not need brackets when printed as
			/// `\frac{a+b}{c}`).

			virtual bool needs_brackets(Ex::iterator it)=0;

			const Ex&     tree;
			const Kernel& kernel;

		};

	template <typename DisplayType> std::string ex_to_string(const Kernel& kernel, const Ex& ex)
	{
		std::ostringstream ss;
		DisplayType dt(kernel, ex);
		dt.output(ss);
		return ss.str();
	}

	template <typename DisplayType> std::string ex_to_string(const Kernel& kernel, Ex::iterator it)
	{
		return ex_to_string<DisplayType>(kernel, Ex(it));
	}


	}