bin.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_BIN_H
30 #define CXXTOOLS_BIN_H
31 
34 #include <iostream>
35 
36 namespace cxxtools
37 {
38  namespace bin
39  {
58  template <typename ObjectType>
59  class BinOObject
60  {
61  const ObjectType& _constObject;
62 
63  public:
65  explicit BinOObject(const ObjectType& object)
66  : _constObject(object)
67  { }
68 
69  const ObjectType& object() const
70  { return _constObject; }
71  };
72 
74  template <typename CharType, typename ObjectType>
75  std::basic_ostream<CharType>& operator<< (std::basic_ostream<CharType>& out, const BinOObject<ObjectType>& object)
76  {
77  try
78  {
79  Serializer serializer(out);
80  serializer.serialize(object.object())
81  .finish();
82  }
83  catch (const std::exception&)
84  {
85  out.setstate(std::ios::failbit);
86  throw;
87  }
88 
89  return out;
90  }
91 
95  template <typename ObjectType>
96  BinOObject<ObjectType> Bin(const ObjectType& object)
97  {
98  return BinOObject<ObjectType>(object);
99  }
100 
101  template <typename ObjectType>
102  class BinIOObject : public BinOObject<ObjectType>
103  {
104  ObjectType& _object;
105 
106  public:
107  explicit BinIOObject(ObjectType& object)
108  : BinOObject<ObjectType>(object),
109  _object(object)
110  { }
111 
112  ObjectType& object()
113  { return _object; }
114  };
115 
117  template <typename CharType, typename ObjectType>
118  std::basic_istream<CharType>& operator>> (std::basic_istream<CharType>& in, BinIOObject<ObjectType> object)
119  {
120  try
121  {
122  Deserializer deserializer(in);
123  deserializer.deserialize(object.object());
124  }
125  catch (const std::exception&)
126  {
127  in.setstate(std::ios::failbit);
128  throw;
129  }
130  return in;
131  }
132 
133  template <typename ObjectType>
134  BinIOObject<ObjectType> Bin(ObjectType& object)
135  {
136  return BinIOObject<ObjectType>(object);
137  }
138 
139  }
140 }
141 
142 #endif // CXXTOOLS_BIN_H
143