MCPcopy Create free account
hub / github.com/docopt/docopt.cpp / value

Class value

docopt_value.h:31–105  ·  view source on GitHub ↗

A generic type to hold the various types that can be produced by docopt. This type can be one of: {bool, long, string, vector }, or empty.

Source from the content-addressed store, hash-verified

29 ///
30 /// This type can be one of: {bool, long, string, vector<string>}, or empty.
31 struct value {
32 /// An empty value
33 value() {}
34
35 value(std::string);
36 value(std::vector<std::string>);
37
38 explicit value(bool);
39 explicit value(long);
40 explicit value(int v) : value(static_cast<long>(v)) {}
41
42 ~value();
43 value(value const&);
44 value(value&&) noexcept;
45 value& operator=(value const&);
46 value& operator=(value&&) noexcept;
47
48 Kind kind() const { return kind_; }
49
50 // Test if this object has any contents at all
51 explicit operator bool() const { return kind_ != Kind::Empty; }
52
53 // Test the type contained by this value object
54 bool isBool() const { return kind_==Kind::Bool; }
55 bool isString() const { return kind_==Kind::String; }
56 bool isLong() const { return kind_==Kind::Long; }
57 bool isStringList() const { return kind_==Kind::StringList; }
58
59 // Throws std::invalid_argument if the type does not match
60 bool asBool() const;
61 long asLong() const;
62 std::string const& asString() const;
63 std::vector<std::string> const& asStringList() const;
64
65 size_t hash() const noexcept;
66
67 friend bool operator==(value const&, value const&);
68 friend bool operator!=(value const&, value const&);
69
70 private:
71 union Variant {
72 Variant() {}
73 ~Variant() { /* do nothing; will be destroyed by ~value */ }
74
75 bool boolValue;
76 long longValue;
77 std::string strValue;
78 std::vector<std::string> strList;
79 };
80
81 static const char* kindAsString(Kind kind) {
82 switch (kind) {
83 case Kind::Empty: return "empty";
84 case Kind::Bool: return "bool";
85 case Kind::Long: return "long";
86 case Kind::String: return "string";
87 case Kind::StringList: return "string-list";
88 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected