Lolly 1.4.28
Loading...
Searching...
No Matches
Classes | Functions
blackbox.hpp File Reference
#include "basic.hpp"
Include dependency graph for blackbox.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  blackbox_rep
 A template class representing an opaque pointer. More...
 
class  blackbox
 
class  whitebox_rep< T >
 

Functions

bool is_nil (blackbox x)
 
bool operator== (blackbox bb1, blackbox bb2)
 Equality operator for blackbox instances.
 
bool operator!= (blackbox bb1, blackbox bb2)
 Inequality operator for blackbox instances.
 
tm_ostreamoperator<< (tm_ostream &out, blackbox bb)
 Output stream operator for blackbox instances.
 
int type_box (blackbox bb)
 Get the type of the blackbox instance.
 
template<class T >
blackbox close_box (const T &data)
 Create a blackbox instance with the given data.
 
template<class T >
open_box (blackbox bb)
 Open the blackbox instance and retrieve the stored data.
 

Detailed Description

defines a templated blackbox class representing an opaque pointer

Author
Joris van der Hoeven
Date
2005

Definition in file blackbox.hpp.

Function Documentation

◆ is_nil()

bool is_nil ( blackbox x)
inline

Definition at line 29 of file blackbox.hpp.

30: public blackbox_rep {
31public:
32 T data; /**< The data stored in the whitebox. */
33
34public:
35 /**
36 * @brief Constructor.
37 * @param data2 The data to be stored in the whitebox.
38 */
39 inline whitebox_rep (const T& data2) : data (data2) {}
40 inline ~whitebox_rep () {}
41
42 /**
43 * @brief Get the type of the whitebox representation.
44 * @return The type of the whitebox representation.
45 */
46 inline int get_type () { return type_helper<T>::id; }
47
48 /**
49 * @brief Check if the whitebox representation is equal to another
50 * blackbox_rep pointer.
51 * @param ptr A pointer to another blackbox_rep.
52 * @return True if the whitebox representation is equal to the other
53 * blackbox_rep, false otherwise.
54 */
55 inline bool equal (blackbox_rep* ptr) {
56 return ptr != NULL && ptr->get_type () == type_helper<T>::id &&
57 ((whitebox_rep<T>*) ptr)->data == data;
58 }
59
60 /**
61 * @brief Display the whitebox representation.
62 * @param out The output stream to display the whitebox representation.
63 * @return The output stream after displaying the whitebox representation.
64 */
65 inline tm_ostream& display (tm_ostream& out) { return out << data; }
66};
67
68/**
69 * @brief Equality operator for blackbox instances.
70 * @param bb1 The first blackbox instance.
71 * @param bb2 The second blackbox instance.
72 * @return True if the blackbox instances are equal, false otherwise.
73 */
74inline bool
76 if (is_nil (bb1)) return is_nil (bb2);
77 else return bb1->equal (bb2.rep);
78}
79
80/**
81 * @brief Inequality operator for blackbox instances.
82 * @param bb1 The first blackbox instance.
83 * @param bb2 The second blackbox instance.
84 * @return True if the blackbox instances are not equal, false otherwise.
85 */
86inline bool
88 if (is_nil (bb1)) return !is_nil (bb2);
89 else return !bb1->equal (bb2.rep);
90}
91
92/**
93 * @brief Output stream operator for blackbox instances.
94 * @param out The output stream.
95 * @param bb The blackbox instance.
96 * @return The output stream after displaying the blackbox instance.
97 */
98inline tm_ostream&
100 if (is_nil (bb)) return out << "nil";
101 else return bb->display (out);
102}
103
104/**
105 * @brief Get the type of the blackbox instance.
106 * @param bb The blackbox instance.
107 * @return The type of the blackbox instance.
108 */
109inline int
111 return is_nil (bb) ? 0 : bb->get_type ();
112}
113
114/**
115 * @brief Create a blackbox instance with the given data.
116 * @tparam T The type of data to be stored in the whitebox.
117 * @param data The data to be stored in the whitebox.
118 * @return The blackbox instance.
119 */
120template <class T>
122close_box (const T& data) {
123 return tm_new<whitebox_rep<T>> (data);
124}
125
126/**
127 * @brief Open the blackbox instance and retrieve the stored data.
128 * @tparam T The type of data stored in the whitebox.
129 * @param bb The blackbox instance.
130 * @return The stored data.
131 */
132template <class T>
133T
135 ASSERT (type_box (bb) == type_helper<T>::id, "type mismatch");
136 return ((whitebox_rep<T>*) bb.rep)->data;
137}
138
139#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
The list class represents a linked list.
Definition list.hpp:48
Helper struct for type identification and initialization.
Definition basic.hpp:303

◆ operator==()

bool operator== ( blackbox bb1,
blackbox bb2 )
inline

Equality operator for blackbox instances.

Parameters
bb1The first blackbox instance.
bb2The second blackbox instance.
Returns
True if the blackbox instances are equal, false otherwise.

Definition at line 76 of file blackbox.hpp.

76 {
77 if (is_nil (bb1)) return is_nil (bb2);
78 else return bb1->equal (bb2.rep);
79}

◆ operator!=()

bool operator!= ( blackbox bb1,
blackbox bb2 )
inline

Inequality operator for blackbox instances.

Parameters
bb1The first blackbox instance.
bb2The second blackbox instance.
Returns
True if the blackbox instances are not equal, false otherwise.

Definition at line 88 of file blackbox.hpp.

88 {
89 if (is_nil (bb1)) return !is_nil (bb2);
90 else return !bb1->equal (bb2.rep);
91}

◆ operator<<()

tm_ostream & operator<< ( tm_ostream & out,
blackbox bb )
inline

Output stream operator for blackbox instances.

Parameters
outThe output stream.
bbThe blackbox instance.
Returns
The output stream after displaying the blackbox instance.

Definition at line 99 of file blackbox.hpp.

100 {
101 if (is_nil (bb)) return out << "nil";
102 else return bb->display (out);
103}

◆ type_box()

int type_box ( blackbox bb)
inline

Get the type of the blackbox instance.

Parameters
bbThe blackbox instance.
Returns
The type of the blackbox instance.

Definition at line 111 of file blackbox.hpp.

111 {
112 return is_nil (bb) ? 0 : bb->get_type ();
113}

◆ close_box()

template<class T >
blackbox close_box ( const T & data)

Create a blackbox instance with the given data.

Template Parameters
TThe type of data to be stored in the whitebox.
Parameters
dataThe data to be stored in the whitebox.
Returns
The blackbox instance.

Definition at line 123 of file blackbox.hpp.

123 {
124 return tm_new<whitebox_rep<T>> (data);
125}

◆ open_box()

template<class T >
T open_box ( blackbox bb)

Open the blackbox instance and retrieve the stored data.

Template Parameters
TThe type of data stored in the whitebox.
Parameters
bbThe blackbox instance.
Returns
The stored data.

Definition at line 135 of file blackbox.hpp.

135 {
136 ASSERT (type_box (bb) == type_helper<T>::id, "type mismatch");
137 return ((whitebox_rep<T>*) bb.rep)->data;
138}