libt2n: (gerd) basic command handling (still some todos)
[libt2n] / src / container.hxx
CommitLineData
7087e187
GE
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
25namespace libt2n
26{
27
28/** @brief contains the result (return value or exception) of a executed command
29*/
30class result_container
31{
32 private:
33 enum result_type_t { regular, exception } result_type;
34
35 result *res;
36 t2n_exception *ex;
37
38 friend class boost::serialization::access;
39 // When the class Archive corresponds to an output archive, the
40 // & operator is defined similar to <<. Likewise, when the class Archive
41 // is a type of input archive the & operator is defined similar to >>.
42 template<class Archive>
43 void serialize(Archive & ar, const unsigned int version)
44 {
45 ar & BOOST_SERIALIZATION_NVP(result_type);
46 ar & BOOST_SERIALIZATION_NVP(res);
47 ar & BOOST_SERIALIZATION_NVP(ex);
48 }
49
50 public:
51 result_container()
52 { res=0; ex=0; }
53
54 result_container(result *_res)
55 { set_result(_res); }
56 result_container(t2n_exception *_ex)
57 { set_exception(_ex); }
58
59 void set_result(result *_res)
60 { res=_res; ex=0; result_type=regular; }
61 void set_exception(t2n_exception *_ex)
62 { res=0; ex=_ex; result_type=exception; }
63
64 /** @brief returns the result or throw the carried exception.
65 ATTENTION: the result object is deleted in the destructor
66 */
67 result* get_result(void)
68 {
69 if (result_type==exception)
70 ex->do_throw();
71 return res;
72 }
73
74 /// deletes the carried result or exception objects
75 ~result_container()
76 {
77 if (res)
78 delete res;
79 if (ex)
80 delete ex;
81 }
82};
83
84/** @brief contains a command
85*/
86class command_container
87{
88 private:
89 command *cmd;
90
91 friend class boost::serialization::access;
92 template<class Archive>
93 void serialize(Archive & ar, const unsigned int version)
94 {
95 ar & BOOST_SERIALIZATION_NVP(cmd);
96 }
97
98 public:
99 command_container()
100 { cmd=0; }
101 command_container(command *_cmd)
102 { cmd=_cmd; }
103
104 /// return the contained command
105 command* get_command()
106 { return cmd; }
107
108 ~command_container()
109 {
110 if (cmd)
111 delete cmd;
112 }
113};
114
115} // namespace libt2n
116
117BOOST_CLASS_TRACKING(libt2n::result_container, boost::serialization::track_never)
118BOOST_CLASS_TRACKING(libt2n::command_container, boost::serialization::track_never)
119
120#endif
121