Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Collection.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "data/cassandra/impl/ManagedObject.hpp"
23
24#include <cassandra.h>
25#include <xrpl/basics/base_uint.h>
26
27#include <cstdint>
28#include <stdexcept>
29#include <string>
30#include <string_view>
31#include <vector>
32
33namespace data::cassandra::impl {
34
35class Collection : public ManagedObject<CassCollection> {
36 static constexpr auto kDELETER = [](CassCollection* ptr) { cass_collection_free(ptr); };
37
38 static void
39 throwErrorIfNeeded(CassError const rc, std::string_view const label)
40 {
41 if (rc == CASS_OK)
42 return;
43 auto const tag = '[' + std::string{label} + ']';
44 throw std::logic_error(tag + ": " + cass_error_desc(rc));
45 }
46
47public:
48 /* implicit */ Collection(CassCollection* ptr);
49
50 template <typename Type>
51 explicit Collection(std::vector<Type> const& value)
52 : ManagedObject{cass_collection_new(CASS_COLLECTION_TYPE_LIST, value.size()), kDELETER}
53 {
54 bind(value);
55 }
56
57 template <typename Type>
58 void
59 bind(std::vector<Type> const& values) const
60 {
61 for (auto const& value : values)
62 append(value);
63 }
64
65 void
66 append(bool const value) const
67 {
68 auto const rc = cass_collection_append_bool(*this, value ? cass_true : cass_false);
69 throwErrorIfNeeded(rc, "Bind bool");
70 }
71
72 void
73 append(int64_t const value) const
74 {
75 auto const rc = cass_collection_append_int64(*this, value);
76 throwErrorIfNeeded(rc, "Bind int64");
77 }
78
79 void
80 append(ripple::uint256 const& value) const
81 {
82 auto const rc = cass_collection_append_bytes(
83 *this,
84 static_cast<cass_byte_t const*>(static_cast<unsigned char const*>(value.data())),
85 ripple::uint256::size()
86 );
87 throwErrorIfNeeded(rc, "Bind ripple::uint256");
88 }
89};
90} // namespace data::cassandra::impl
Definition Collection.hpp:35
Definition ManagedObject.hpp:28