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