Skip to content

Commit

Permalink
fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
lidavidm committed Mar 16, 2024
1 parent 9cf9909 commit 478f9b0
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 138 deletions.
2 changes: 1 addition & 1 deletion c/driver/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

include(FetchContent)

add_library(adbc_driver_framework STATIC catalog.cc objects.cc)
add_library(adbc_driver_framework STATIC base.cc catalog.cc objects.cc)
adbc_configure_target(adbc_driver_framework)
set_target_properties(adbc_driver_framework PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(adbc_driver_framework
Expand Down
160 changes: 160 additions & 0 deletions c/driver/framework/base.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "driver/framework/base.h"

namespace adbc::driver {
Result<bool> Option::AsBool() const {
return std::visit(
[&](auto&& value) -> Result<bool> {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
if (value == ADBC_OPTION_VALUE_ENABLED) {
return true;
} else if (value == ADBC_OPTION_VALUE_DISABLED) {
return false;
}
}
return status::InvalidArgument("Invalid boolean value {}", *this);
},
value_);
}

Result<int64_t> Option::AsInt() const {
return std::visit(
[&](auto&& value) -> Result<int64_t> {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, int64_t>) {
return value;
} else if constexpr (std::is_same_v<T, std::string>) {
int64_t parsed = 0;
auto begin = value.data();
auto end = value.data() + value.size();
auto result = std::from_chars(begin, end, parsed);
if (result.ec != std::errc()) {
return status::InvalidArgument("Invalid integer value '{}': not an integer",
value);
} else if (result.ptr != end) {
return status::InvalidArgument("Invalid integer value '{}': trailing data",
value);
}
return parsed;
}
return status::InvalidArgument("Invalid integer value {}", *this);
},
value_);
}

Result<std::string_view> Option::AsString() const {
return std::visit(
[&](auto&& value) -> Result<std::string_view> {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
return value;
}
return status::InvalidArgument("Invalid string value {}", *this);
},
value_);
}

AdbcStatusCode Option::CGet(char* out, size_t* length, AdbcError* error) const {
if (!out || !length) {
return status::InvalidArgument("Must provide both out and length to GetOption")
.ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
size_t value_size_with_terminator = value.size() + 1;
if (*length >= value_size_with_terminator) {
std::memcpy(out, value.data(), value.size());
out[value.size()] = 0;
}
*length = value_size_with_terminator;
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not a string").ToAdbc(error);
}
},
value_);
}

AdbcStatusCode Option::CGet(uint8_t* out, size_t* length, AdbcError* error) const {
if (!out || !length) {
return status::InvalidArgument("Must provide both out and length to GetOption")
.ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string> ||
std::is_same_v<T, std::vector<uint8_t>>) {
if (*length >= value.size()) {
std::memcpy(out, value.data(), value.size());
}
*length = value.size();
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not a bytestring").ToAdbc(error);
}
},
value_);
}

AdbcStatusCode Option::CGet(int64_t* out, AdbcError* error) const {
if (!out) {
return status::InvalidArgument("Must provide out to GetOption").ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, int64_t>) {
*out = value;
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not an integer").ToAdbc(error);
}
},
value_);
}

AdbcStatusCode Option::CGet(double* out, AdbcError* error) const {
if (!out) {
return status::InvalidArgument("Must provide out to GetOption").ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, double> || std::is_same_v<T, int64_t>) {
*out = value;
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not a double").ToAdbc(error);
}
},
value_);
}
} // namespace adbc::driver
144 changes: 7 additions & 137 deletions c/driver/framework/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,153 +83,23 @@ class Option {
bool has_value() const { return !std::holds_alternative<Unset>(value_); }

/// \brief Try to parse a string value as a boolean.
Result<bool> AsBool() const {
return std::visit(
[&](auto&& value) -> Result<bool> {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
if (value == ADBC_OPTION_VALUE_ENABLED) {
return true;
} else if (value == ADBC_OPTION_VALUE_DISABLED) {
return false;
}
}
return status::InvalidArgument("Invalid boolean value {}", *this);
},
value_);
}
Result<bool> AsBool() const;

/// \brief Try to parse a string or integer value as an integer.
Result<int64_t> AsInt() const {
return std::visit(
[&](auto&& value) -> Result<int64_t> {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, int64_t>) {
return value;
} else if constexpr (std::is_same_v<T, std::string>) {
int64_t parsed = 0;
auto begin = value.data();
auto end = value.data() + value.size();
auto result = std::from_chars(begin, end, parsed);
if (result.ec != std::errc()) {
return status::InvalidArgument("Invalid integer value '{}': not an integer",
value);
} else if (result.ptr != end) {
return status::InvalidArgument("Invalid integer value '{}': trailing data",
value);
}
return parsed;
}
return status::InvalidArgument("Invalid integer value {}", *this);
},
value_);
}
Result<int64_t> AsInt() const;

/// \brief Get the value if it is a string.
Result<std::string_view> AsString() const {
return std::visit(
[&](auto&& value) -> Result<std::string_view> {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
return value;
}
return status::InvalidArgument("Invalid string value {}", *this);
},
value_);
}
Result<std::string_view> AsString() const;

private:
Value value_;

// Methods used by trampolines to export option values in C below
friend class ObjectBase;

AdbcStatusCode CGet(char* out, size_t* length, AdbcError* error) const {
if (!out || !length) {
return status::InvalidArgument("Must provide both out and length to GetOption")
.ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
size_t value_size_with_terminator = value.size() + 1;
if (*length >= value_size_with_terminator) {
std::memcpy(out, value.data(), value.size());
out[value.size()] = 0;
}
*length = value_size_with_terminator;
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not a string").ToAdbc(error);
}
},
value_);
}

AdbcStatusCode CGet(uint8_t* out, size_t* length, AdbcError* error) const {
if (!out || !length) {
return status::InvalidArgument("Must provide both out and length to GetOption")
.ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string> ||
std::is_same_v<T, std::vector<uint8_t>>) {
if (*length >= value.size()) {
std::memcpy(out, value.data(), value.size());
}
*length = value.size();
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not a bytestring").ToAdbc(error);
}
},
value_);
}

AdbcStatusCode CGet(int64_t* out, AdbcError* error) const {
if (!out) {
return status::InvalidArgument("Must provide out to GetOption").ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, int64_t>) {
*out = value;
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not an integer").ToAdbc(error);
}
},
value_);
}

AdbcStatusCode CGet(double* out, AdbcError* error) const {
if (!out) {
return status::InvalidArgument("Must provide out to GetOption").ToAdbc(error);
}
return std::visit(
[&](auto&& value) -> AdbcStatusCode {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, double> || std::is_same_v<T, int64_t>) {
*out = value;
return ADBC_STATUS_OK;
} else if constexpr (std::is_same_v<T, Unset>) {
return status::NotFound("Unknown option").ToAdbc(error);
} else {
return status::NotFound("Option value is not a double").ToAdbc(error);
}
},
value_);
}
AdbcStatusCode CGet(char* out, size_t* length, AdbcError* error) const;
AdbcStatusCode CGet(uint8_t* out, size_t* length, AdbcError* error) const;
AdbcStatusCode CGet(int64_t* out, AdbcError* error) const;
AdbcStatusCode CGet(double* out, AdbcError* error) const;
};

/// \brief Base class for private_data of AdbcDatabase, AdbcConnection, and
Expand Down
1 change: 1 addition & 0 deletions c/driver/framework/objects.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

#include "driver/framework/objects.h"

#include <string_view>

#include "driver/framework/catalog.h"
Expand Down
1 change: 1 addition & 0 deletions ci/scripts/cpp_clang_tidy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ build_subproject() {
-DCMAKE_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}"

run-clang-tidy \
-extra-arg=-Wno-unknown-warning-option \
-j $(nproc) \
-p "${build_dir}" \
-fix \
Expand Down

0 comments on commit 478f9b0

Please sign in to comment.