PageRenderTime 37ms CodeModel.GetById 26ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/TeXmacs-1.0.7.11-src/src/Kernel/Abstractions/blackbox.hpp

#
C++ Header | 71 lines | 52 code | 10 blank | 9 comment | 13 complexity | 24c8694ccaed26f7e75810a4affae840 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception
 1
 2/******************************************************************************
 3* MODULE     : blackbox.hpp
 4* DESCRIPTION: For hiding the implementation of a type
 5* COPYRIGHT  : (C) 2005  Joris van der Hoeven
 6*******************************************************************************
 7* This software falls under the GNU general public license version 3 or later.
 8* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
 9* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10******************************************************************************/
11
12#ifndef BLACKBOX_H
13#define BLACKBOX_H
14#include "basic.hpp"
15
16class blackbox_rep: public abstract_struct {
17public:
18  inline blackbox_rep () {}
19  inline virtual ~blackbox_rep () {}
20  virtual int get_type () = 0;
21  virtual bool equal (blackbox_rep* ptr) = 0;
22  virtual tm_ostream& display (tm_ostream& out) = 0;
23};
24
25class blackbox {
26public:
27ABSTRACT_NULL(blackbox);
28};
29ABSTRACT_NULL_CODE(blackbox);
30
31template<class T>
32class whitebox_rep: public blackbox_rep {
33public:
34  T data;
35public:
36  inline whitebox_rep (const T& data2): data (data2) {}
37  inline ~whitebox_rep () {}
38  inline int get_type () { return type_helper<T>::id; }
39  inline bool equal (blackbox_rep* ptr) {
40    return ptr != NULL && ptr->get_type () == type_helper<T>::id &&
41           ((whitebox_rep<T>*) ptr)->data == data; }
42  inline tm_ostream& display (tm_ostream& out) { return out << data; }
43};
44
45inline bool operator == (blackbox bb1, blackbox bb2) {
46  if (is_nil (bb1)) return is_nil (bb2);
47  else return bb1->equal (bb2.rep); }
48inline bool operator != (blackbox bb1, blackbox bb2) {
49  if (is_nil (bb1)) return !is_nil (bb2);
50  else return !bb1->equal (bb2.rep); }
51inline tm_ostream& operator << (tm_ostream& out, blackbox bb) {
52  if (is_nil (bb)) return out << "nil";
53  else return bb->display (out); }
54
55inline int
56type_box (blackbox bb) {
57  return is_nil (bb)? 0: bb->get_type ();
58}
59
60template<class T> blackbox
61close_box (const T& data) {
62  return tm_new<whitebox_rep<T> > (data);
63}
64
65template<class T> T
66open_box (blackbox bb) {
67  ASSERT (type_box (bb) == type_helper<T>::id, "type mismatch");
68  return ((whitebox_rep<T>*) bb.rep) -> data;
69}
70
71#endif // BLACKBOX_H