Lolly 1.4.28
Loading...
Searching...
No Matches
blackbox.hpp
Go to the documentation of this file.
1
2/** \file blackbox.hpp
3 * \copyright GPLv3
4 * \details defines a templated blackbox class representing an opaque pointer
5 * \author Joris van der Hoeven
6 * \date 2005
7 */
8
9#ifndef BLACKBOX_H
10#define BLACKBOX_H
11#include "basic.hpp"
12
13/**
14 * @brief A template class representing an opaque pointer.
15 */
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:
28};
30
31template <class T> class whitebox_rep : public blackbox_rep {
32public:
33 T data; /**< The data stored in the whitebox. */
34
35public:
36 /**
37 * @brief Constructor.
38 * @param data2 The data to be stored in the whitebox.
39 */
40 inline whitebox_rep (const T& data2) : data (data2) {}
41 inline ~whitebox_rep () {}
42
43 /**
44 * @brief Get the type of the whitebox representation.
45 * @return The type of the whitebox representation.
46 */
47 inline int get_type () { return type_helper<T>::id; }
48
49 /**
50 * @brief Check if the whitebox representation is equal to another
51 * blackbox_rep pointer.
52 * @param ptr A pointer to another blackbox_rep.
53 * @return True if the whitebox representation is equal to the other
54 * blackbox_rep, false otherwise.
55 */
56 inline bool equal (blackbox_rep* ptr) {
57 return ptr != NULL && ptr->get_type () == type_helper<T>::id &&
58 ((whitebox_rep<T>*) ptr)->data == data;
59 }
60
61 /**
62 * @brief Display the whitebox representation.
63 * @param out The output stream to display the whitebox representation.
64 * @return The output stream after displaying the whitebox representation.
65 */
66 inline tm_ostream& display (tm_ostream& out) { return out << data; }
67};
68
69/**
70 * @brief Equality operator for blackbox instances.
71 * @param bb1 The first blackbox instance.
72 * @param bb2 The second blackbox instance.
73 * @return True if the blackbox instances are equal, false otherwise.
74 */
75inline bool
77 if (is_nil (bb1)) return is_nil (bb2);
78 else return bb1->equal (bb2.rep);
79}
80
81/**
82 * @brief Inequality operator for blackbox instances.
83 * @param bb1 The first blackbox instance.
84 * @param bb2 The second blackbox instance.
85 * @return True if the blackbox instances are not equal, false otherwise.
86 */
87inline bool
89 if (is_nil (bb1)) return !is_nil (bb2);
90 else return !bb1->equal (bb2.rep);
91}
92
93/**
94 * @brief Output stream operator for blackbox instances.
95 * @param out The output stream.
96 * @param bb The blackbox instance.
97 * @return The output stream after displaying the blackbox instance.
98 */
101 if (is_nil (bb)) return out << "nil";
102 else return bb->display (out);
103}
104
105/**
106 * @brief Get the type of the blackbox instance.
107 * @param bb The blackbox instance.
108 * @return The type of the blackbox instance.
109 */
110inline int
112 return is_nil (bb) ? 0 : bb->get_type ();
113}
114
115/**
116 * @brief Create a blackbox instance with the given data.
117 * @tparam T The type of data to be stored in the whitebox.
118 * @param data The data to be stored in the whitebox.
119 * @return The blackbox instance.
120 */
121template <class T>
123close_box (const T& data) {
124 return tm_new<whitebox_rep<T>> (data);
125}
126
127/**
128 * @brief Open the blackbox instance and retrieve the stored data.
129 * @tparam T The type of data stored in the whitebox.
130 * @param bb The blackbox instance.
131 * @return The stored data.
132 */
133template <class T>
134T
136 ASSERT (type_box (bb) == type_helper<T>::id, "type mismatch");
137 return ((whitebox_rep<T>*) bb.rep)->data;
138}
139
140#endif // BLACKBOX_H
#define ASSERT(cond, msg)
Macro used to assert that a condition is true, and throw an exception with an error message if the co...
Definition basic.hpp:85
tm_ostream & operator<<(tm_ostream &out, blackbox bb)
Output stream operator for blackbox instances.
Definition blackbox.hpp:100
bool operator==(blackbox bb1, blackbox bb2)
Equality operator for blackbox instances.
Definition blackbox.hpp:76
bool operator!=(blackbox bb1, blackbox bb2)
Inequality operator for blackbox instances.
Definition blackbox.hpp:88
T open_box(blackbox bb)
Open the blackbox instance and retrieve the stored data.
Definition blackbox.hpp:135
bool is_nil(blackbox x)
Definition blackbox.hpp:29
blackbox close_box(const T &data)
Create a blackbox instance with the given data.
Definition blackbox.hpp:123
int type_box(blackbox bb)
Get the type of the blackbox instance.
Definition blackbox.hpp:111
A template class representing an opaque pointer.
Definition blackbox.hpp:16
virtual tm_ostream & display(tm_ostream &out)=0
virtual int get_type()=0
virtual bool equal(blackbox_rep *ptr)=0
virtual ~blackbox_rep()
Definition blackbox.hpp:19
#define ABSTRACT_NULL_CODE(PTR)
Macro for abstract null indirect structure code definition.
Definition classdef.hpp:474
#define ABSTRACT_NULL(PTR)
Macro for abstract null indirect structure definition.
Definition classdef.hpp:466
The list class represents a linked list.
Definition list.hpp:48
bool equal(blackbox_rep *ptr)
Check if the whitebox representation is equal to another blackbox_rep pointer.
Definition blackbox.hpp:56
int get_type()
Get the type of the whitebox representation.
Definition blackbox.hpp:47
whitebox_rep(const T &data2)
Constructor.
Definition blackbox.hpp:40
tm_ostream & display(tm_ostream &out)
Display the whitebox representation.
Definition blackbox.hpp:66
Structure representing an abstract object with a reference count.
Definition classdef.hpp:49
Helper struct for type identification and initialization.
Definition basic.hpp:303