libt2n: (gerd) command handling, unit-test still bailing out
[libt2n] / src / container.hxx
1 /***************************************************************************
2  *   Copyright (C) 2006 by Gerd v. Egidy                                   *
3  *   gve@intra2net.com                                                     *
4  *                                                                         *
5  *   This library is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Lesser General Public License version   *
7  *   2.1 as published by the Free Software Foundation.                     *
8  *                                                                         *
9  *   This library is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU Lesser General Public License for more details.                   *
13  *                                                                         *
14  *   You should have received a copy of the GNU Lesser General Public      *
15  *   License along with this program; if not, write to the                 *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 #ifndef __LIBT2N_CONTAINER
20 #define __LIBT2N_CONTAINER
21
22 #include "command.hxx"
23 #include "t2n_exception.hxx"
24
25 #include <iostream>
26
27 namespace libt2n
28 {
29
30 /** @brief contains the result (return value or exception) of a executed command
31 */
32 class result_container
33 {
34     private:
35         enum result_type_t { regular, exception } result_type;
36
37         result *res;
38         t2n_exception *ex;
39
40         friend class boost::serialization::access;
41         // When the class Archive corresponds to an output archive, the
42         // & operator is defined similar to <<.  Likewise, when the class Archive
43         // is a type of input archive the & operator is defined similar to >>.
44         template<class Archive>
45         void serialize(Archive & ar, const unsigned int version)
46         {
47             ar & BOOST_SERIALIZATION_NVP(result_type);
48             ar & BOOST_SERIALIZATION_NVP(res);
49             ar & BOOST_SERIALIZATION_NVP(ex);
50         }
51
52     public:
53         result_container()
54             { res=0; ex=0; }
55
56         result_container(result *_res)
57             { set_result(_res); }
58         result_container(t2n_exception *_ex)
59             { set_exception(_ex); }
60
61         void set_result(result *_res)
62             { res=_res; ex=0; result_type=regular; }
63         void set_exception(t2n_exception *_ex)
64             { res=0; ex=_ex; result_type=exception; }
65
66         /** @brief returns the result or throw the carried exception.
67                    ATTENTION: the result object is deleted in the destructor
68         */
69         result* get_result(void)
70         {
71             if (result_type==exception)
72                 ex->do_throw();
73             return res;
74         }
75
76         /// deletes the carried result or exception objects
77         ~result_container()
78         {
79             if (res)
80                 delete res;
81             if (ex)
82                 delete ex;
83         }
84 };
85
86 /** @brief contains a command
87 */
88 class command_container
89 {
90     private:
91         command *cmd;
92
93         friend class boost::serialization::access;
94         template<class Archive>
95         void serialize(Archive & ar, const unsigned int version)
96         {
97             std::cerr << "ser: command_container" << std::endl;
98             ar & BOOST_SERIALIZATION_NVP(cmd);
99         }
100
101     public:
102         command_container()
103             { cmd=0; }
104         command_container(command *_cmd)
105             { cmd=_cmd; }
106
107         /// return the contained command
108         command* get_command()
109             { return cmd; }
110
111         ~command_container()
112         {
113             if (cmd)
114                 delete cmd;
115         }
116 };
117
118 } // namespace libt2n
119
120 BOOST_CLASS_TRACKING(libt2n::result_container, boost::serialization::track_never)
121 BOOST_CLASS_TRACKING(libt2n::command_container, boost::serialization::track_never)
122
123 #endif
124