blob: c8fe0e0d050ebf76de703d66c66785d36ce77870 [file] [log] [blame]
Kim Paulhamus6efcf4952017-09-14 22:46:271// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_WEBAUTH_CBOR_CBOR_VALUES_H_
6#define CONTENT_BROWSER_WEBAUTH_CBOR_CBOR_VALUES_H_
7
8#include <stdint.h>
9#include <string>
10#include <vector>
11
12#include "base/containers/flat_map.h"
13#include "base/macros.h"
14#include "base/memory/manual_constructor.h"
15#include "base/strings/string_piece_forward.h"
16#include "content/common/content_export.h"
17
18namespace content {
19
20// A class for Concise Binary Object Representation (CBOR) values.
21// This does not support:
22// * Negative integers.
23// * Floating-point numbers.
24// * Indefinite-length encodings.
25class CONTENT_EXPORT CBORValue {
26 public:
27 using BinaryValue = std::vector<uint8_t>;
28 using ArrayValue = std::vector<CBORValue>;
29 using MapValue = base::flat_map<std::string, CBORValue>;
30
31 enum class Type {
32 NONE,
33 UNSIGNED,
34 BYTESTRING,
35 STRING,
36 ARRAY,
37 MAP,
38 };
39
40 CBORValue(CBORValue&& that) noexcept;
41 CBORValue() noexcept; // A NONE value.
42
43 explicit CBORValue(Type type);
44 explicit CBORValue(uint64_t in_unsigned);
45
46 explicit CBORValue(const BinaryValue& in_bytes);
47 explicit CBORValue(BinaryValue&& in_bytes) noexcept;
48
49 explicit CBORValue(const char* in_string);
50 explicit CBORValue(std::string&& in_string) noexcept;
51 explicit CBORValue(base::StringPiece in_string);
52
53 explicit CBORValue(const ArrayValue& in_array);
54 explicit CBORValue(ArrayValue&& in_array) noexcept;
55
56 explicit CBORValue(const MapValue& in_map);
57 explicit CBORValue(MapValue&& in_map) noexcept;
58
59 CBORValue& operator=(CBORValue&& that) noexcept;
60
61 ~CBORValue();
62
63 // CBORValue's copy constructor and copy assignment operator are deleted.
64 // Use this to obtain a deep copy explicitly.
65 CBORValue Clone() const;
66
67 // Returns the type of the value stored by the current Value object.
68 Type type() const { return type_; }
69
70 // Returns true if the current object represents a given type.
71 bool is_type(Type type) const { return type == type_; }
72 bool is_none() const { return type() == Type::NONE; }
73 bool is_unsigned() const { return type() == Type::UNSIGNED; }
74 bool is_bytestring() const { return type() == Type::BYTESTRING; }
75 bool is_string() const { return type() == Type::STRING; }
76 bool is_array() const { return type() == Type::ARRAY; }
77 bool is_map() const { return type() == Type::MAP; }
78
79 // These will all fatally assert if the type doesn't match.
80 uint64_t GetUnsigned() const;
81 const BinaryValue& GetBytestring() const;
82 const std::string& GetString() const;
83 const ArrayValue& GetArray() const;
84 const MapValue& GetMap() const;
85
86 private:
87 Type type_;
88
89 union {
90 uint64_t unsigned_value_;
91 base::ManualConstructor<BinaryValue> bytestring_value_;
92 base::ManualConstructor<std::string> string_value_;
93 base::ManualConstructor<ArrayValue> array_value_;
94 base::ManualConstructor<MapValue> map_value_;
95 };
96
97 void InternalMoveConstructFrom(CBORValue&& that);
98 void InternalCleanup();
99
100 DISALLOW_COPY_AND_ASSIGN(CBORValue);
101};
102} // namespace content
103
104#endif // CONTENT_BROWSER_WEBAUTH_CBOR_CBOR_VALUES_H_