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