Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
BasedOnStyle: Google
AccessModifierOffset: -2
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
# Empty is required in AllowShortFunctionsOnASingleLine over Inline because Inline contradicts with AUTOSAR rule A7-1-7
# Such rule is no longer existing in MISRA C++:2023, once we are fully migrated to MISRA C++:2023, switching to Inline
# could be reconsidered
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: false
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
BreakBeforeBraces: Custom
ColumnLimit: 120
DerivePointerAlignment: false
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^(<|")(assert|complex|ctype|errno|fenv|float|inttypes|iso646|limits|locale|math|setjmp|signal|stdalign|stdargh|stdatomic|stdbool|stddef|stdint|stdio|stdlib|stdnoreturn|string|tgmath|threads|time|uchar|wchar|wctype)\.h(>|")$'
Priority: 2
- Regex: '^(<|")(cstdlib|csignal|csetjmp|cstdarg|typeinfo|typeindex|type_traits|bitset|functional|utility|ctime|chrono|cstddef|initializer_list|tuple|any|optional|variant|new|memory|scoped_allocator|memory_resource|climits|cfloat|cstdint|cinttypes|limits|exception|stdexcept|cassert|system_error|cerrno|cctype|cwctype|cstring|cwchar|cuchar|string|string_view|array|vector|deque|list|forward_list|set|map|unordered_set|unordered_map|stack|queue|algorithm|execution|teratorslibrary|iterator|cmath|complex|valarray|random|numeric|ratio|cfenv|iosfwd|ios|istream|ostream|iostream|fstream|sstream|strstream|iomanip|streambuf|cstdio|locale|clocale|codecvt|regex|atomic|thread|mutex|shared_mutex|future|condition_variable|filesystem|ciso646|ccomplex|ctgmath|cstdalign|cstdbool)(>|")$'
Priority: 3
- Regex: '^(<|").*(>|")$'
Priority: 1
IndentWidth: 4
InsertNewlineAtEOF: true
KeepEmptyLinesAtTheStartOfBlocks: true
QualifierAlignment: Left
CommentPragmas: '^.*A2Lfactory:'
---
# Make sure language specific settings are below the generic settings to be compatible to all languages.
Language: Cpp
Standard: c++17
9 changes: 3 additions & 6 deletions src/cpp/src/internal/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
namespace score::mw::per::kvs {

/*********************** Error Implementation *********************/
std::string_view MyErrorDomain::MessageFor(score::result::ErrorCode const& code) const noexcept
{
std::string_view MyErrorDomain::MessageFor(score::result::ErrorCode const& code) const noexcept {
std::string_view msg;
switch (static_cast<ErrorCode>(code))
{
switch (static_cast<ErrorCode>(code)) {
case ErrorCode::UnmappedError:
msg = "Error that was not yet mapped";
break;
Expand Down Expand Up @@ -91,8 +89,7 @@ std::string_view MyErrorDomain::MessageFor(score::result::ErrorCode const& code)
return msg;
}

score::result::Error MakeError(ErrorCode code, std::string_view user_message) noexcept
{
score::result::Error MakeError(ErrorCode code, std::string_view user_message) noexcept {
return {static_cast<score::result::ErrorCode>(code), my_error_domain, user_message};
}

Expand Down
5 changes: 2 additions & 3 deletions src/cpp/src/internal/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ enum class ErrorCode : score::result::ErrorCode {
InvalidValueType,
};

class MyErrorDomain final : public score::result::ErrorDomain
{
public:
class MyErrorDomain final : public score::result::ErrorDomain {
public:
std::string_view MessageFor(score::result::ErrorCode const& code) const noexcept override;
};

Expand Down
109 changes: 44 additions & 65 deletions src/cpp/src/internal/kvs_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,41 @@ uint32_t calculate_hash_adler32(const std::string& data) {
}

/*Parse Adler32 checksum Byte-Array to uint32 */
uint32_t parse_hash_adler32(std::istream& in)
{
std::array<uint8_t,4> buf{};
uint32_t parse_hash_adler32(std::istream& in) {
std::array<uint8_t, 4> buf{};
in.read(reinterpret_cast<char*>(buf.data()), buf.size());

uint32_t value = (uint32_t(buf[0]) << 24)
| (uint32_t(buf[1]) << 16)
| (uint32_t(buf[2]) << 8)
| uint32_t(buf[3]);
uint32_t value = (uint32_t(buf[0]) << 24) | (uint32_t(buf[1]) << 16) | (uint32_t(buf[2]) << 8) |
uint32_t(buf[3]);
return value;
}

/* Split uint32 checksum in bytes for writing*/
std::array<uint8_t,4> get_hash_bytes_adler32(uint32_t hash)
{
std::array<uint8_t, 4> value = {
uint8_t((hash >> 24) & 0xFF),
uint8_t((hash >> 16) & 0xFF),
uint8_t((hash >> 8) & 0xFF),
uint8_t((hash ) & 0xFF)
};
std::array<uint8_t, 4> get_hash_bytes_adler32(uint32_t hash) {
std::array<uint8_t, 4> value = {uint8_t((hash >> 24) & 0xFF), uint8_t((hash >> 16) & 0xFF),
uint8_t((hash >> 8) & 0xFF), uint8_t((hash) & 0xFF)};
return value;
}

/***** Wrapper Functions for Hash *****/
/* Wrapper Functions should isolate the Hash Algorithm, so that the algorithm can be easier replaced*/
/* Wrapper Functions should isolate the Hash Algorithm, so that the algorithm can be easier
* replaced*/

/* Wrapper Function to get checksum in bytes*/
std::array<uint8_t,4> get_hash_bytes(const std::string& data)
{
std::array<uint8_t, 4> get_hash_bytes(const std::string& data) {
uint32_t hash = calculate_hash_adler32(data);
std::array<uint8_t, 4> value = get_hash_bytes_adler32(hash);
return value;
}

/* Wrapper Function to check, if Hash is valid*/
bool check_hash(const std::string& data_calculate, std::istream& data_parse){
bool check_hash(const std::string& data_calculate, std::istream& data_parse) {
bool result;
uint32_t calculated_hash = calculate_hash_adler32(data_calculate);
uint32_t parsed_hash = parse_hash_adler32(data_parse);
if(calculated_hash == parsed_hash){
if (calculated_hash == parsed_hash) {
result = true;
}else{
} else {
result = false;
}

Expand All @@ -91,82 +83,66 @@ bool check_hash(const std::string& data_calculate, std::istream& data_parse){
/*********************** Standalone Helper Functions *********************/

/* Helper Function for Any -> KVSValue conversion */
score::Result<KvsValue> any_to_kvsvalue(const score::json::Any& any){
score::Result<KvsValue> any_to_kvsvalue(const score::json::Any& any) {
score::Result<KvsValue> result = score::MakeUnexpected(ErrorCode::UnmappedError);
if (auto o = any.As<score::json::Object>(); o.has_value()) {
const auto& objAny = o.value().get();
auto type = objAny.find("t");
auto type = objAny.find("t");
auto value = objAny.find("v");
if (type != objAny.end() && value != objAny.end()) {
if (auto typeStr = type->second.As<std::string>(); typeStr.has_value()) {
const std::string_view typeStrV = typeStr.value().get();
const score::json::Any& valueAny = value->second;

if (typeStrV == "i32") {
if (auto n = valueAny.As<int32_t>(); n.has_value()){
if (auto n = valueAny.As<int32_t>(); n.has_value()) {
result = KvsValue(static_cast<int32_t>(n.value()));
}
else {
} else {
result = score::MakeUnexpected(ErrorCode::InvalidValueType);
}
}
else if (typeStrV == "u32") {
} else if (typeStrV == "u32") {
if (auto n = valueAny.As<uint32_t>(); n.has_value()) {
result = KvsValue(static_cast<uint32_t>(n.value()));
}
else {
} else {
result = score::MakeUnexpected(ErrorCode::InvalidValueType);
}
}
else if (typeStrV == "i64") {
} else if (typeStrV == "i64") {
if (auto n = valueAny.As<int64_t>(); n.has_value()) {
result = KvsValue(static_cast<int64_t>(n.value()));
}
else {
} else {
result = score::MakeUnexpected(ErrorCode::InvalidValueType);
}
}
else if (typeStrV == "u64") {
} else if (typeStrV == "u64") {
if (auto n = valueAny.As<uint64_t>(); n.has_value()) {
result = KvsValue(static_cast<uint64_t>(n.value()));
}
else {
} else {
result = score::MakeUnexpected(ErrorCode::InvalidValueType);
}
}
else if (typeStrV == "f64") {
} else if (typeStrV == "f64") {
if (auto n = valueAny.As<double>(); n.has_value()) {
result = KvsValue(n.value());
}
else {
} else {
result = score::MakeUnexpected(ErrorCode::InvalidValueType);
}
}
else if (typeStrV == "bool") {
} else if (typeStrV == "bool") {
if (auto b = valueAny.As<bool>(); b.has_value()) {
result = KvsValue(b.value());
}
else {
} else {
result = score::MakeUnexpected(ErrorCode::InvalidValueType);
}
}
else if (typeStrV == "str") {
} else if (typeStrV == "str") {
if (auto s = valueAny.As<std::string>(); s.has_value()) {
result = KvsValue(s.value().get());
}
else {
} else {
result = score::MakeUnexpected(ErrorCode::InvalidValueType);
}
}
else if (typeStrV == "null") {
} else if (typeStrV == "null") {
if (valueAny.As<score::json::Null>().has_value()) {
result = KvsValue(nullptr);
}
else {
} else {
result = score::MakeUnexpected(ErrorCode::InvalidValueType);
}
}
else if (typeStrV == "arr") {
} else if (typeStrV == "arr") {
if (auto l = valueAny.As<score::json::List>(); l.has_value()) {
KvsValue::Array arr;
bool error = false;
Expand All @@ -179,14 +155,13 @@ score::Result<KvsValue> any_to_kvsvalue(const score::json::Any& any){
}
arr.emplace_back(std::make_shared<KvsValue>(std::move(conv.value())));
}
if (!error){
if (!error) {
result = KvsValue(std::move(arr));
}
} else {
result = score::MakeUnexpected(ErrorCode::InvalidValueType);
}
}
else if (typeStrV == "obj") {
} else if (typeStrV == "obj") {
if (auto obj = valueAny.As<score::json::Object>(); obj.has_value()) {
KvsValue::Object map;
bool error = false;
Expand All @@ -197,7 +172,8 @@ score::Result<KvsValue> any_to_kvsvalue(const score::json::Any& any){
result = score::MakeUnexpected(ErrorCode::InvalidValueType);
break;
}
map.emplace(key.GetAsStringView().to_string(), std::make_shared<KvsValue>(std::move(conv.value())));
map.emplace(key.GetAsStringView().to_string(),
std::make_shared<KvsValue>(std::move(conv.value())));
}
if (!error) {
result = KvsValue(std::move(map));
Expand Down Expand Up @@ -229,22 +205,26 @@ score::Result<score::json::Any> kvsvalue_to_any(const KvsValue& kv) {
switch (kv.getType()) {
case KvsValue::Type::i32: {
obj.emplace("t", score::json::Any(std::string("i32")));
obj.emplace("v", score::json::Any(static_cast<int32_t>(std::get<int32_t>(kv.getValue()))));
obj.emplace("v",
score::json::Any(static_cast<int32_t>(std::get<int32_t>(kv.getValue()))));
break;
}
case KvsValue::Type::u32: {
obj.emplace("t", score::json::Any(std::string("u32")));
obj.emplace("v", score::json::Any(static_cast<uint32_t>(std::get<uint32_t>(kv.getValue()))));
obj.emplace("v",
score::json::Any(static_cast<uint32_t>(std::get<uint32_t>(kv.getValue()))));
break;
}
case KvsValue::Type::i64: {
obj.emplace("t", score::json::Any(std::string("i64")));
obj.emplace("v", score::json::Any(static_cast<int64_t>(std::get<int64_t>(kv.getValue()))));
obj.emplace("v",
score::json::Any(static_cast<int64_t>(std::get<int64_t>(kv.getValue()))));
break;
}
case KvsValue::Type::u64: {
obj.emplace("t", score::json::Any(std::string("u64")));
obj.emplace("v", score::json::Any(static_cast<uint64_t>(std::get<uint64_t>(kv.getValue()))));
obj.emplace("v",
score::json::Any(static_cast<uint64_t>(std::get<uint64_t>(kv.getValue()))));
break;
}
case KvsValue::Type::f64: {
Expand Down Expand Up @@ -315,5 +295,4 @@ score::Result<score::json::Any> kvsvalue_to_any(const KvsValue& kv) {
return result;
}


} /* namespace score::mw::per::kvs */
6 changes: 3 additions & 3 deletions src/cpp/src/internal/kvs_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ namespace score::mw::per::kvs {

uint32_t parse_hash_adler32(std::istream& in);
uint32_t calculate_hash_adler32(const std::string& data);
std::array<uint8_t,4> get_hash_bytes_adler32(uint32_t hash);
std::array<uint8_t,4> get_hash_bytes(const std::string& data);
std::array<uint8_t, 4> get_hash_bytes_adler32(uint32_t hash);
std::array<uint8_t, 4> get_hash_bytes(const std::string& data);
bool check_hash(const std::string& data_calculate, std::istream& data_parse);
score::Result<KvsValue> any_to_kvsvalue(const score::json::Any& any);
score::Result<score::json::Any> kvsvalue_to_any(const KvsValue& kv);

} /* namespace score::mw::per::kvs */

#endif // SCORE_LIB_KVS_INTERNAL_KVS_HELPER_HPP
#endif // SCORE_LIB_KVS_INTERNAL_KVS_HELPER_HPP
Loading
Loading