properties.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 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_PROPERTIES_H
30 #define CXXTOOLS_PROPERTIES_H
31 
34 #include <iostream>
35 
36 namespace cxxtools
37 {
55  template <typename ObjectType>
57  {
58  const ObjectType& _constObject;
59  bool _outputSize;
60 
61  public:
64  explicit PropertiesOObject(const ObjectType& object, bool outputSize = true)
65  : _constObject(object),
66  _outputSize(outputSize)
67  { }
68 
70  { _outputSize = sw; return *this; }
71 
72  bool outputSize() const
73  { return _outputSize; }
74 
75  const ObjectType& object() const
76  { return _constObject; }
77  };
78 
80  template <typename CharType, typename ObjectType>
81  std::basic_ostream<CharType>& operator<< (std::basic_ostream<CharType>& out, const PropertiesOObject<ObjectType>& object)
82  {
83  try
84  {
85  PropertiesSerializer serializer(out);
86  serializer.outputSize(object.outputSize());
87  serializer.serialize(object.object())
88  .finish();
89  }
90  catch (const std::exception&)
91  {
92  out.setstate(std::ios::failbit);
93  throw;
94  }
95  return out;
96  }
97 
102  template <typename ObjectType>
103  class PropertiesIOObject : public PropertiesOObject<ObjectType>
104  {
105  ObjectType& _object;
106  std::string _name;
107  bool _envSubst;
108  bool _trim;
109 
110  public:
112  explicit PropertiesIOObject(ObjectType& object, const std::string& name = std::string())
113  : PropertiesOObject<ObjectType>(object),
114  _object(object),
115  _name(name),
116  _envSubst(false),
117  _trim(false)
118  { }
119 
120  ObjectType& object()
121  { return _object; }
122 
123  const std::string& name() const
124  { return _name; }
125 
129  PropertiesIOObject& envSubst(bool sw) { _envSubst = sw; return *this; }
130 
132  bool envSubst() const
133  { return _envSubst; }
134 
139  { _trim = sw; return *this; }
140 
142  bool trim() const
143  { return _trim; }
144 
145  };
146 
148  template <typename CharType, typename ObjectType>
149  std::basic_istream<CharType>& operator>> (std::basic_istream<CharType>& in, PropertiesIOObject<ObjectType> object)
150  {
151  PropertiesDeserializer deserializer;
152  deserializer.envSubst(object.envSubst());
153  deserializer.trim(object.trim());
154  deserializer.read(in);
155  if (object.name().empty())
156  deserializer.deserialize(object.object());
157  else
158  deserializer.deserialize(object.object(), object.name());
159  return in;
160  }
161 
162  template <typename ObjectType>
163  PropertiesIOObject<ObjectType> Properties(ObjectType& object, const std::string& name = std::string())
164  {
165  return PropertiesIOObject<ObjectType>(object, name);
166  }
167 
168 }
169 
170 #endif // CXXTOOLS_PROPERTIES_H