paludis  Version 2.6.0
destringify.hh
Go to the documentation of this file.
1 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
2 
3 /*
4  * Copyright (c) 2006 Stephen Bennett
5  *
6  * This file is part of the Paludis package manager. Paludis is free software;
7  * you can redistribute it and/or modify it under the terms of the GNU General
8  * Public License version 2, as published by the Free Software Foundation.
9  *
10  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  * Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #ifndef PALUDIS_GUARD_PALUDIS_DESTRINGIFY_HH
21 #define PALUDIS_GUARD_PALUDIS_DESTRINGIFY_HH 1
22 
24 #include <sstream>
25 #include <string>
26 
27 /** \file
28  * Destringify functions.
29  *
30  * \ingroup g_strings
31  *
32  * \section Examples
33  *
34  * - None at this time.
35  */
36 
37 namespace paludis
38 {
39  /**
40  * Default exception type thrown by destringify when it fails.
41  *
42  * \ingroup g_strings
43  * \ingroup g_exceptions
44  * \nosubgrouping
45  */
47  public Exception
48  {
49  public:
50  ///\name Basic operations
51  ///\{
52 
53  DestringifyError(const std::string & str) noexcept;
54 
55  ///\}
56  };
57 
58  /**
59  * For internal use by destringify.
60  *
61  * \ingroup g_strings
62  */
63  namespace destringify_internals
64  {
65  /**
66  * Basic destringifier.
67  *
68  * \ingroup g_strings
69  */
70  template <typename Type_, typename Exception_>
72  {
73  /**
74  * Do the destringification.
75  */
76  static Type_ do_destringify(const std::string & s)
77  {
78  std::istringstream ss(s);
79  Type_ t;
80  ss >> t;
81  if (! ss.eof() || ss.fail())
82  throw Exception_(s);
83  return t;
84  }
85  };
86 
87  /**
88  * Specialised destringify for std::string.
89  *
90  * \ingroup g_strings
91  */
92  template <typename Exception_>
93  struct Destringifier<std::string, Exception_>
94  {
95  /**
96  * Do the destringification.
97  */
98  static std::string do_destringify(const std::string & s)
99  {
100  return s;
101  }
102  };
103 
104  /**
105  * Specialised destringify for bool.
106  *
107  * \ingroup g_strings
108  */
109  template <typename Exception_>
110  struct Destringifier<bool, Exception_>
111  {
112  /**
113  * Do the destringification.
114  */
115  static bool do_destringify(const std::string & s)
116  {
117  /* Don't use boolalpha on std::istringstream here, since it's way too difficult
118  * to test for errors and eof. See gcc bug 37958. */
119  if (s == "true")
120  return true;
121  else if (s == "false")
122  return false;
123  else
125  }
126  };
127 
128  /**
129  * Specialised destringify for char.
130  *
131  * \ingroup g_strings
132  */
133  template <typename Exception_>
134  struct Destringifier<char, Exception_>
135  {
136  /**
137  * Do the destringification.
138  */
139  static char do_destringify(const std::string & s)
140  {
141  if (s.length() == 1)
142  return s[0];
143  else
144  throw Exception_(s);
145  }
146  };
147  }
148 
149  /**
150  * Extract a value of some type from a string.
151  *
152  * \ingroup g_strings
153  */
154  template <typename Type_, typename Exception_ = DestringifyError>
155  Type_ destringify(const std::string & s)
156  {
157  if (s == "")
158  throw Exception_("");
159 
161  }
162 }
163 
164 #endif
Definition: about_metadata-fwd.hh:23
static bool do_destringify(const std::string &s)
Definition: destringify.hh:115
STL namespace.
static Type_ do_destringify(const std::string &s)
Definition: destringify.hh:76
Definition: exception.hh:74
static std::string do_destringify(const std::string &s)
Definition: destringify.hh:98
Type_ destringify(const std::string &s)
Definition: destringify.hh:155
static char do_destringify(const std::string &s)
Definition: destringify.hh:139
Definition: destringify.hh:46
#define PALUDIS_VISIBLE
Definition: attributes.hh:59