Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace more of the public headers #6279

Merged
merged 9 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions include/ccf/byte_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace std
{
size_t operator()(const llvm_vecsmall::SmallVector<T, N>& v) const
{
static constexpr siphash::SipKey k{
static constexpr ccf::siphash::SipKey k{
0x7720796f726c694b, 0x2165726568207361};
return siphash::siphash<2, 4>(v.data(), v.size(), k);
return ccf::siphash::siphash<2, 4>(v.data(), v.size(), k);
}
};
}
Expand Down
6 changes: 3 additions & 3 deletions include/ccf/ds/contiguous_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <numeric>
#include <vector>

namespace ds
namespace ccf::ds
{
// Dense representation of an ordered set of values, assuming it contains
// some contiguous ranges of adjacent values. Stores a sequence of ranges,
Expand Down Expand Up @@ -500,7 +500,7 @@ namespace ds

FMT_BEGIN_NAMESPACE
template <typename T>
struct formatter<ds::ContiguousSet<T>>
struct formatter<ccf::ds::ContiguousSet<T>>
{
template <typename ParseContext>
constexpr auto parse(ParseContext& ctx)
Expand All @@ -509,7 +509,7 @@ struct formatter<ds::ContiguousSet<T>>
}

template <typename FormatContext>
auto format(const ds::ContiguousSet<T>& v, FormatContext& ctx) const
auto format(const ccf::ds::ContiguousSet<T>& v, FormatContext& ctx) const
{
std::vector<std::string> ranges;
for (const auto& [from, additional] : v.get_ranges())
Expand Down
16 changes: 8 additions & 8 deletions include/ccf/ds/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <string_view>
#include <vector>

namespace ds::hashutils
namespace ccf::ds::hashutils
{
template <typename T>
inline void hash_combine(size_t& n, const T& v, std::hash<T>& h)
Expand Down Expand Up @@ -42,9 +42,9 @@ namespace std
{
// For cryptographically secure hashing, use SipHash directly with a
// secret key. For std::hash, we use this fixed key
static constexpr siphash::SipKey k{
static constexpr ccf::siphash::SipKey k{
0x7720796f726c694b, 0x2165726568207361};
return siphash::siphash<2, 4>(v, k);
return ccf::siphash::siphash<2, 4>(v, k);
}
};

Expand All @@ -53,7 +53,7 @@ namespace std
{
size_t operator()(const std::vector<T>& v) const
{
return ds::hashutils::hash_container(v);
return ccf::ds::hashutils::hash_container(v);
}
};

Expand All @@ -62,7 +62,7 @@ namespace std
{
size_t operator()(const std::array<T, N>& v) const
{
return ds::hashutils::hash_container(v);
return ccf::ds::hashutils::hash_container(v);
}
};

Expand All @@ -74,18 +74,18 @@ namespace std
size_t n = 0x444e414c544f4353;

std::hash<A> h_a{};
ds::hashutils::hash_combine(n, v.first, h_a);
ccf::ds::hashutils::hash_combine(n, v.first, h_a);

std::hash<B> h_b{};
ds::hashutils::hash_combine(n, v.second, h_b);
ccf::ds::hashutils::hash_combine(n, v.second, h_b);

return n;
}
};

}

namespace ds
namespace ccf::ds
{
/// Simple, fast constexpr hash function (NOT cryptographically sound)
namespace
Expand Down
2 changes: 1 addition & 1 deletion include/ccf/ds/hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <string>
#include <vector>

namespace ds
namespace ccf::ds
{
static uint8_t hex_char_to_int(char c)
{
Expand Down
77 changes: 40 additions & 37 deletions include/ccf/ds/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,38 @@
#include <fmt/format.h>
#include <sstream>

/** Represents a field within a JSON object. Tuples of these can be used in
* schema generation.
*/
template <typename T>
struct JsonField
namespace ccf
{
using Target = T;
char const* name;
};
/** Represents a field within a JSON object. Tuples of these can be used in
* schema generation.
*/
template <typename T>
struct JsonField
{
using Target = T;
char const* name;
};

class JsonParseError : public std::invalid_argument
{
public:
std::vector<std::string> pointer_elements = {};
class JsonParseError : public std::invalid_argument
{
public:
std::vector<std::string> pointer_elements = {};

using std::invalid_argument::invalid_argument;
using std::invalid_argument::invalid_argument;

std::string pointer() const
{
return fmt::format(
"#/{}",
fmt::join(pointer_elements.crbegin(), pointer_elements.crend(), "/"));
}
std::string pointer() const
{
return fmt::format(
"#/{}",
fmt::join(pointer_elements.crbegin(), pointer_elements.crend(), "/"));
}

std::string describe() const
{
return fmt::format("At {}: {}", pointer(), what());
}
};
std::string describe() const
{
return fmt::format("At {}: {}", pointer(), what());
}
};
}

namespace std
{
Expand Down Expand Up @@ -89,7 +92,7 @@ namespace std
}
catch (const std::exception& e)
{
throw JsonParseError(fmt::format(
throw ccf::JsonParseError(fmt::format(
"Vector of bytes object \"{}\" is not valid base64", j.dump()));
}
}
Expand All @@ -101,7 +104,7 @@ namespace std

if (!j.is_array())
{
throw JsonParseError(
throw ccf::JsonParseError(
fmt::format("Vector object \"{}\" is not an array", j.dump()));
}

Expand All @@ -111,7 +114,7 @@ namespace std
{
t.push_back(j.at(i).template get<T>());
}
catch (JsonParseError& jpe)
catch (ccf::JsonParseError& jpe)
{
jpe.pointer_elements.push_back(std::to_string(i));
throw;
Expand Down Expand Up @@ -398,14 +401,14 @@ namespace std
const auto it = j.find(JSON_FIELD); \
if (it == j.end()) \
{ \
throw JsonParseError( \
throw ccf::JsonParseError( \
"Missing required field '" JSON_FIELD "' in object: " + j.dump()); \
} \
try \
{ \
t.C_FIELD = it->get<decltype(TYPE::C_FIELD)>(); \
} \
catch (JsonParseError & jpe) \
catch (ccf::JsonParseError & jpe) \
{ \
jpe.pointer_elements.push_back(JSON_FIELD); \
throw; \
Expand Down Expand Up @@ -438,7 +441,7 @@ namespace std
#define FILL_SCHEMA_REQUIRED_WITH_RENAMES_FOR_JSON_NEXT( \
TYPE, C_FIELD, JSON_FIELD) \
j["properties"][JSON_FIELD] = \
::ds::json::schema_element<decltype(TYPE::C_FIELD)>(); \
ccf::ds::json::schema_element<decltype(TYPE::C_FIELD)>(); \
j["required"].push_back(JSON_FIELD);
#define FILL_SCHEMA_REQUIRED_WITH_RENAMES_FOR_JSON_FINAL( \
TYPE, C_FIELD, JSON_FIELD) \
Expand All @@ -452,7 +455,7 @@ namespace std
#define FILL_SCHEMA_OPTIONAL_WITH_RENAMES_FOR_JSON_NEXT( \
TYPE, C_FIELD, JSON_FIELD) \
j["properties"][JSON_FIELD] = \
::ds::json::schema_element<decltype(TYPE::C_FIELD)>();
ccf::ds::json::schema_element<decltype(TYPE::C_FIELD)>();
#define FILL_SCHEMA_OPTIONAL_WITH_RENAMES_FOR_JSON_FINAL( \
TYPE, C_FIELD, JSON_FIELD) \
FILL_SCHEMA_OPTIONAL_WITH_RENAMES_FOR_JSON_NEXT(TYPE, C_FIELD, JSON_FIELD)
Expand Down Expand Up @@ -494,9 +497,9 @@ namespace std
TYPE, FIELD, #FIELD)

#define JSON_FIELD_FOR_JSON_NEXT(TYPE, FIELD) \
JsonField<decltype(TYPE::FIELD)>{#FIELD},
ccf::JsonField<decltype(TYPE::FIELD)>{#FIELD},
#define JSON_FIELD_FOR_JSON_FINAL(TYPE, FIELD) \
JsonField<decltype(TYPE::FIELD)> \
ccf::JsonField<decltype(TYPE::FIELD)> \
{ \
# FIELD \
}
Expand Down Expand Up @@ -723,7 +726,7 @@ namespace std
{ \
if (!j.is_object()) \
{ \
throw JsonParseError("Expected object, found: " + j.dump()); \
throw ccf::JsonParseError("Expected object, found: " + j.dump()); \
} \
_FOR_JSON_COUNT_NN(__VA_ARGS__)(POP1)(READ_REQUIRED, TYPE, ##__VA_ARGS__) \
} \
Expand Down Expand Up @@ -758,7 +761,7 @@ namespace std
{ \
if (!j.is_object()) \
{ \
throw JsonParseError("Expected object, found: " + j.dump()); \
throw ccf::JsonParseError("Expected object, found: " + j.dump()); \
} \
_FOR_JSON_COUNT_NN(__VA_ARGS__) \
(POP2)(READ_REQUIRED_WITH_RENAMES, TYPE, ##__VA_ARGS__) \
Expand Down Expand Up @@ -843,7 +846,7 @@ namespace std
}); \
if (it == std::end(m)) \
{ \
throw JsonParseError(fmt::format( \
throw ccf::JsonParseError(fmt::format( \
"Value {} in enum " #TYPE " has no specified JSON conversion", \
(size_t)e)); \
} \
Expand All @@ -862,7 +865,7 @@ namespace std
}); \
if (it == std::end(m)) \
{ \
throw JsonParseError( \
throw ccf::JsonParseError( \
fmt::format("{} is not convertible to " #TYPE, j.dump())); \
} \
e = it->first; \
Expand Down
2 changes: 1 addition & 1 deletion include/ccf/ds/json_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <nlohmann/json.hpp>
#include <set>

namespace ds
namespace ccf::ds
{
namespace json
{
Expand Down
4 changes: 2 additions & 2 deletions include/ccf/ds/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "ccf/ds/enum_formatter.h"
#include "ccf/ds/logger_level.h"
#include "ccf/ds/thread_ids.h"
#include "ccf/threading/thread_ids.h"

#define FMT_HEADER_ONLY
#include <fmt/chrono.h>
Expand Down Expand Up @@ -72,7 +72,7 @@ namespace logger
else
{
#ifdef INSIDE_ENCLAVE
thread_id = threading::get_current_thread_id();
thread_id = ccf::threading::get_current_thread_id();
#else
thread_id = 100;
#endif
Expand Down
17 changes: 9 additions & 8 deletions include/ccf/ds/openapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <set>
#include <string_view>

namespace ds
namespace ccf::ds
{
/**
* This namespace contains helper functions, structs, and templates for
Expand Down Expand Up @@ -296,7 +296,7 @@ namespace ds
}

return add_schema_to_components(
document, ds::json::schema_name<T>(), schema);
document, ccf::ds::json::schema_name<T>(), schema);
}
else if constexpr (
nonstd::is_specialization<T, std::map>::value ||
Expand Down Expand Up @@ -328,7 +328,7 @@ namespace ds
schema["items"] = items;
}
return add_schema_to_components(
document, ds::json::schema_name<T>(), schema);
document, ccf::ds::json::schema_name<T>(), schema);
}
else if constexpr (nonstd::is_specialization<T, std::pair>::value)
{
Expand All @@ -338,20 +338,21 @@ namespace ds
items.push_back(add_schema_component<typename T::second_type>());
schema["items"] = items;
return add_schema_to_components(
document, ds::json::schema_name<T>(), schema);
document, ccf::ds::json::schema_name<T>(), schema);
}
else if constexpr (
std::is_same<T, std::string>::value || std::is_arithmetic_v<T> ||
std::is_same<T, nlohmann::json>::value ||
std::is_same<T, ds::json::JsonSchema>::value)
std::is_same<T, ccf::ds::json::JsonSchema>::value)
{
ds::json::fill_schema<T>(schema);
ccf::ds::json::fill_schema<T>(schema);
return add_schema_to_components(
document, ds::json::schema_name<T>(), schema);
document, ccf::ds::json::schema_name<T>(), schema);
}
else
{
const auto name = sanitise_components_key(ds::json::schema_name<T>());
const auto name =
sanitise_components_key(ccf::ds::json::schema_name<T>());

auto& components = access::get_object(document, "components");
auto& schemas = access::get_object(components, "schemas");
Expand Down
2 changes: 1 addition & 1 deletion include/ccf/ds/siphash.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <vector>

// C++ port of reference implementation
namespace siphash
namespace ccf::siphash
{
using SipState = uint64_t[4];
using SipKey = uint64_t[2];
Expand Down
6 changes: 3 additions & 3 deletions include/ccf/ds/unit_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <nlohmann/json.hpp>
#include <string>

namespace ds
namespace ccf::ds
{
// Inspired by CLI11's AsNumberWithUnit
class UnitStringConverter
Expand Down Expand Up @@ -212,7 +212,7 @@ namespace ds

FMT_BEGIN_NAMESPACE
template <>
struct formatter<ds::SizeString>
struct formatter<ccf::ds::SizeString>
{
template <typename ParseContext>
constexpr auto parse(ParseContext& ctx)
Expand All @@ -221,7 +221,7 @@ struct formatter<ds::SizeString>
}

template <typename FormatContext>
auto format(const ds::SizeString& v, FormatContext& ctx) const
auto format(const ccf::ds::SizeString& v, FormatContext& ctx) const
{
std::stringstream ss;
ss << v.str;
Expand Down
Loading