/usr/include/docopt/docopt_value.h is in libdocopt-dev 0.6.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | //
// value.h
// docopt
//
// Created by Jared Grubb on 2013-10-14.
// Copyright (c) 2013 Jared Grubb. All rights reserved.
//
#ifndef docopt__value_h_
#define docopt__value_h_
#include <string>
#include <vector>
#include <functional> // std::hash
#include <iosfwd>
namespace docopt {
/// A generic type to hold the various types that can be produced by docopt.
///
/// This type can be one of: {bool, long, string, vector<string>}, or empty.
struct value {
/// An empty value
value() {}
value(std::string);
value(std::vector<std::string>);
explicit value(bool);
explicit value(long);
explicit value(int v) : value(static_cast<long>(v)) {}
~value();
value(value const&);
value(value&&) noexcept;
value& operator=(value const&);
value& operator=(value&&) noexcept;
// Test if this object has any contents at all
explicit operator bool() const { return kind != Kind::Empty; }
// Test the type contained by this value object
bool isBool() const { return kind==Kind::Bool; }
bool isString() const { return kind==Kind::String; }
bool isLong() const { return kind==Kind::Long; }
bool isStringList() const { return kind==Kind::StringList; }
// Throws std::invalid_argument if the type does not match
bool asBool() const;
long asLong() const;
std::string const& asString() const;
std::vector<std::string> const& asStringList() const;
size_t hash() const noexcept;
// equality is based on hash-equality
friend bool operator==(value const&, value const&);
friend bool operator!=(value const&, value const&);
private:
enum class Kind {
Empty,
Bool,
Long,
String,
StringList
};
union Variant {
Variant() {}
~Variant() { /* do nothing; will be destroyed by ~value */ }
bool boolValue;
long longValue;
std::string strValue;
std::vector<std::string> strList;
};
static const char* kindAsString(Kind kind) {
switch (kind) {
case Kind::Empty: return "empty";
case Kind::Bool: return "bool";
case Kind::Long: return "long";
case Kind::String: return "string";
case Kind::StringList: return "string-list";
}
return "unknown";
}
void throwIfNotKind(Kind expected) const {
if (kind == expected)
return;
std::string error = "Illegal cast to ";
error += kindAsString(expected);
error += "; type is actually ";
error += kindAsString(kind);
throw std::runtime_error(std::move(error));
}
private:
Kind kind = Kind::Empty;
Variant variant {};
};
/// Write out the contents to the ostream
std::ostream& operator<<(std::ostream&, value const&);
}
namespace std {
template <>
struct hash<docopt::value> {
size_t operator()(docopt::value const& val) const noexcept {
return val.hash();
}
};
}
namespace docopt {
inline
value::value(bool v)
: kind(Kind::Bool)
{
variant.boolValue = v;
}
inline
value::value(long v)
: kind(Kind::Long)
{
variant.longValue = v;
}
inline
value::value(std::string v)
: kind(Kind::String)
{
new (&variant.strValue) std::string(std::move(v));
}
inline
value::value(std::vector<std::string> v)
: kind(Kind::StringList)
{
new (&variant.strList) std::vector<std::string>(std::move(v));
}
inline
value::value(value const& other)
: kind(other.kind)
{
switch (kind) {
case Kind::String:
new (&variant.strValue) std::string(other.variant.strValue);
break;
case Kind::StringList:
new (&variant.strList) std::vector<std::string>(other.variant.strList);
break;
case Kind::Bool:
variant.boolValue = other.variant.boolValue;
break;
case Kind::Long:
variant.longValue = other.variant.longValue;
break;
case Kind::Empty:
default:
break;
}
}
inline
value::value(value&& other) noexcept
: kind(other.kind)
{
switch (kind) {
case Kind::String:
new (&variant.strValue) std::string(std::move(other.variant.strValue));
break;
case Kind::StringList:
new (&variant.strList) std::vector<std::string>(std::move(other.variant.strList));
break;
case Kind::Bool:
variant.boolValue = other.variant.boolValue;
break;
case Kind::Long:
variant.longValue = other.variant.longValue;
break;
case Kind::Empty:
default:
break;
}
}
inline
value::~value()
{
switch (kind) {
case Kind::String:
variant.strValue.~basic_string();
break;
case Kind::StringList:
variant.strList.~vector();
break;
case Kind::Empty:
case Kind::Bool:
case Kind::Long:
default:
// trivial dtor
break;
}
}
inline
value& value::operator=(value const& other) {
// make a copy and move from it; way easier.
return *this = value{other};
}
inline
value& value::operator=(value&& other) noexcept {
// move of all the types involved is noexcept, so we dont have to worry about
// these two statements throwing, which gives us a consistency guarantee.
this->~value();
new (this) value(std::move(other));
return *this;
}
template <class T>
void hash_combine(std::size_t& seed, const T& v);
inline
size_t value::hash() const noexcept
{
switch (kind) {
case Kind::String:
return std::hash<std::string>()(variant.strValue);
case Kind::StringList: {
size_t seed = std::hash<size_t>()(variant.strList.size());
for(auto const& str : variant.strList) {
hash_combine(seed, str);
}
return seed;
}
case Kind::Bool:
return std::hash<bool>()(variant.boolValue);
case Kind::Long:
return std::hash<long>()(variant.longValue);
case Kind::Empty:
default:
return std::hash<void*>()(nullptr);
}
}
inline
bool value::asBool() const
{
throwIfNotKind(Kind::Bool);
return variant.boolValue;
}
inline
long value::asLong() const
{
// Attempt to convert a string to a long
if (kind == Kind::String) {
const std::string& str = variant.strValue;
std::size_t pos;
const long ret = stol(str, &pos); // Throws if it can't convert
if (pos != str.length()) {
// The string ended in non-digits.
throw std::runtime_error( str + " contains non-numeric characters.");
}
return ret;
}
throwIfNotKind(Kind::Long);
return variant.longValue;
}
inline
std::string const& value::asString() const
{
throwIfNotKind(Kind::String);
return variant.strValue;
}
inline
std::vector<std::string> const& value::asStringList() const
{
throwIfNotKind(Kind::StringList);
return variant.strList;
}
inline
bool operator==(value const& v1, value const& v2)
{
if (v1.kind != v2.kind)
return false;
switch (v1.kind) {
case value::Kind::String:
return v1.variant.strValue==v2.variant.strValue;
case value::Kind::StringList:
return v1.variant.strList==v2.variant.strList;
case value::Kind::Bool:
return v1.variant.boolValue==v2.variant.boolValue;
case value::Kind::Long:
return v1.variant.longValue==v2.variant.longValue;
case value::Kind::Empty:
default:
return true;
}
}
inline
bool operator!=(value const& v1, value const& v2)
{
return !(v1 == v2);
}
}
#endif /* defined(docopt__value_h_) */
|