Skip to content

Commit

Permalink
[C++] Run clang-format on all files (#2928)
Browse files Browse the repository at this point in the history
There is a .clang-format file in the repository, indicating that files
should be formatted with this standard, but they have drifted out of
conformance over time, which makes it easy to accidentally introduce
unrelated differences when submitting changes. This formats the entire
c++ directory.

Ideally, format compliance should be checked when a pull request is
made, I'm open to creating a PR which adds this workflow if desired.
  • Loading branch information
Gerrit0 committed May 31, 2024
1 parent 076706f commit 46edd89
Show file tree
Hide file tree
Showing 32 changed files with 295 additions and 361 deletions.
10 changes: 5 additions & 5 deletions lang/c++/api/CustomAttributes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
#ifndef avro_CustomAttributes_hh__
#define avro_CustomAttributes_hh__

#include "Config.hh"
#include <boost/optional.hpp>
#include <iostream>
#include <map>
#include <string>
#include "Config.hh"

namespace avro {

// CustomAttributes class stores avro custom attributes.
// Each attribute is represented by a unique name and value.
// User is supposed to create CustomAttributes object and then add it to Schema.
class AVRO_DECL CustomAttributes {
public:
public:
// Retrieves the custom attribute json entity for that attributeName, returns an
// null if the attribute doesn't exist.
boost::optional<std::string> getAttribute(const std::string &name) const;
Expand All @@ -45,12 +45,12 @@ class AVRO_DECL CustomAttributes {
}

// Prints the attribute value for the specific attribute.
void printJson(std::ostream& os, const std::string &name) const;
void printJson(std::ostream &os, const std::string &name) const;

private:
private:
std::map<std::string, std::string> attributes_;
};

} // namespace avro
} // namespace avro

#endif
8 changes: 4 additions & 4 deletions lang/c++/api/Exception.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#define avro_Exception_hh__

#include "Config.hh"
#include <stdexcept>
#include <fmt/core.h>
#include <stdexcept>

namespace avro {

Expand All @@ -32,9 +32,9 @@ class AVRO_DECL Exception : public virtual std::runtime_error {
public:
explicit Exception(const std::string &msg) : std::runtime_error(msg) {}

template <typename... Args>
Exception(fmt::format_string<Args...> fmt, Args&&... args)
: std::runtime_error(fmt::format(fmt, std::forward<Args>(args)...) ) {}
template<typename... Args>
Exception(fmt::format_string<Args...> fmt, Args &&...args)
: std::runtime_error(fmt::format(fmt, std::forward<Args>(args)...)) {}
};

} // namespace avro
Expand Down
8 changes: 5 additions & 3 deletions lang/c++/api/GenericDatum.hh
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,13 @@ inline Type GenericDatum::type() const {
inline LogicalType GenericDatum::logicalType() const {
return (type_ == AVRO_UNION) ?
#if __cplusplus >= 201703L
std::any_cast<GenericUnion>(&value_)->datum().logicalType() :
std::any_cast<GenericUnion>(&value_)->datum().logicalType()
:
#else
boost::any_cast<GenericUnion>(&value_)->datum().logicalType() :
boost::any_cast<GenericUnion>(&value_)->datum().logicalType()
:
#endif
logicalType_;
logicalType_;
}

template<typename T>
Expand Down
17 changes: 9 additions & 8 deletions lang/c++/api/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public:
Name();
explicit Name(const std::string &name);
Name(std::string simpleName, std::string ns);
Name(const Name& other);
Name& operator=(const Name& other);
Name(Name&& other);
Name& operator=(Name&& other);
Name(const Name &other);
Name &operator=(const Name &other);
Name(Name &&other);
Name &operator=(Name &&other);
~Name();

std::string fullname() const;
Expand Down Expand Up @@ -162,7 +162,7 @@ public:
}
virtual size_t fixedSize() const = 0;

void addCustomAttributesForField(const CustomAttributes& customAttributes) {
void addCustomAttributesForField(const CustomAttributes &customAttributes) {
checkLock();
doAddCustomAttribute(customAttributes);
}
Expand Down Expand Up @@ -199,7 +199,7 @@ protected:
virtual void doAddLeaf(const NodePtr &newLeaf) = 0;
virtual void doAddName(const std::string &name) = 0;
virtual void doSetFixedSize(size_t size) = 0;
virtual void doAddCustomAttribute(const CustomAttributes& customAttributes) = 0;
virtual void doAddCustomAttribute(const CustomAttributes &customAttributes) = 0;

private:
const Type type_;
Expand All @@ -216,8 +216,9 @@ inline std::ostream &operator<<(std::ostream &os, const avro::Node &n) {
}
} // namespace std

template <> struct fmt::formatter<avro::Name> : fmt::formatter<std::string> {
template <typename FormatContext>
template<>
struct fmt::formatter<avro::Name> : fmt::formatter<std::string> {
template<typename FormatContext>
auto format(const avro::Name &n, FormatContext &ctx) {
return fmt::formatter<std::string>::format(n.fullname(), ctx);
}
Expand Down
9 changes: 3 additions & 6 deletions lang/c++/api/NodeImpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
#include <sstream>
#include <utility>

#include "CustomAttributes.hh"
#include "Node.hh"
#include "NodeConcepts.hh"
#include "CustomAttributes.hh"

namespace avro {

Expand Down Expand Up @@ -161,7 +161,7 @@ protected:
void setLeafToSymbolic(size_t index, const NodePtr &node) override;

void doAddCustomAttribute(const CustomAttributes &customAttributes) override {
customAttributes_.add(customAttributes);
customAttributes_.add(customAttributes);
}

SchemaResolution furtherResolution(const Node &reader) const {
Expand Down Expand Up @@ -325,10 +325,7 @@ public:
void printJson(std::ostream &os, size_t depth) const override;

bool isValid() const override {
return ((nameAttribute_.size() == 1) &&
(leafAttributes_.size() == leafNameAttributes_.size()) &&
(customAttributes_.size() == 0 ||
customAttributes_.size() == leafAttributes_.size()));
return ((nameAttribute_.size() == 1) && (leafAttributes_.size() == leafNameAttributes_.size()) && (customAttributes_.size() == 0 || customAttributes_.size() == leafAttributes_.size()));
}

const GenericDatum &defaultValueAt(size_t index) override {
Expand Down
2 changes: 1 addition & 1 deletion lang/c++/api/Reader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public:
union {
double d;
uint64_t i;
} v = { 0 };
} v = {0};
reader_.read(v.i);
val = v.d;
}
Expand Down
2 changes: 1 addition & 1 deletion lang/c++/api/Schema.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#define avro_Schema_hh__

#include "Config.hh"
#include "NodeImpl.hh"
#include "CustomAttributes.hh"
#include "NodeImpl.hh"
#include <string>

/// \file
Expand Down
7 changes: 4 additions & 3 deletions lang/c++/api/Types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef avro_Types_hh__
#define avro_Types_hh__

#include <iostream>
#include <fmt/format.h>
#include <iostream>

#include "Config.hh"

Expand Down Expand Up @@ -110,8 +110,9 @@ std::ostream &operator<<(std::ostream &os, const Null &null);

} // namespace avro

template <> struct fmt::formatter<avro::Type> : fmt::formatter<std::string> {
template <typename FormatContext>
template<>
struct fmt::formatter<avro::Type> : fmt::formatter<std::string> {
template<typename FormatContext>
auto format(avro::Type t, FormatContext &ctx) {
return fmt::formatter<std::string>::format(avro::toString(t), ctx);
}
Expand Down
17 changes: 8 additions & 9 deletions lang/c++/examples/cpx.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,32 @@
* limitations under the License.
*/


#ifndef CPX_HH_1278398428__H_
#define CPX_HH_1278398428__H_


#include "avro/Specific.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
#include "avro/Encoder.hh"
#include "avro/Specific.hh"

namespace c {
struct cpx {
double re;
double im;
};

}
} // namespace c
namespace avro {
template<> struct codec_traits<c::cpx> {
static void encode(Encoder& e, const c::cpx& v) {
template<>
struct codec_traits<c::cpx> {
static void encode(Encoder &e, const c::cpx &v) {
avro::encode(e, v.re);
avro::encode(e, v.im);
}
static void decode(Decoder& d, c::cpx& v) {
static void decode(Decoder &d, c::cpx &v) {
avro::decode(d, v.re);
avro::decode(d, v.im);
}
};

}
} // namespace avro
#endif
14 changes: 6 additions & 8 deletions lang/c++/examples/custom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,28 @@

#include <complex>

#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
#include "avro/Encoder.hh"
#include "avro/Specific.hh"

namespace avro {
template<typename T>
struct codec_traits<std::complex<T> > {
static void encode(Encoder& e, const std::complex<T>& c) {
struct codec_traits<std::complex<T>> {
static void encode(Encoder &e, const std::complex<T> &c) {
avro::encode(e, std::real(c));
avro::encode(e, std::imag(c));
}

static void decode(Decoder& d, std::complex<T>& c) {
static void decode(Decoder &d, std::complex<T> &c) {
T re, im;
avro::decode(d, re);
avro::decode(d, im);
c = std::complex<T>(re, im);
}
};

}
int
main()
{
} // namespace avro
int main() {
std::unique_ptr<avro::OutputStream> out = avro::memoryOutputStream();
avro::EncoderPtr e = avro::binaryEncoder();
e->init(*out);
Expand Down
17 changes: 6 additions & 11 deletions lang/c++/examples/datafile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,21 @@

#include <fstream>

#include "cpx.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
#include "avro/ValidSchema.hh"
#include "avro/Compiler.hh"
#include "avro/DataFile.hh"
#include "avro/Decoder.hh"
#include "avro/Encoder.hh"
#include "avro/ValidSchema.hh"
#include "cpx.hh"


avro::ValidSchema loadSchema(const char* filename)
{
avro::ValidSchema loadSchema(const char *filename) {
std::ifstream ifs(filename);
avro::ValidSchema result;
avro::compileJsonSchema(ifs, result);
return result;
}

int
main()
{
int main() {
avro::ValidSchema cpxSchema = loadSchema("cpx.json");

{
Expand All @@ -59,4 +55,3 @@ main()
}
return 0;
}

10 changes: 3 additions & 7 deletions lang/c++/examples/generated.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
* limitations under the License.
*/

#include "cpx.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
#include "avro/Encoder.hh"
#include "cpx.hh"


int
main()
{
int main() {
std::unique_ptr<avro::OutputStream> out = avro::memoryOutputStream();
avro::EncoderPtr e = avro::binaryEncoder();
e->init(*out);
Expand All @@ -41,4 +38,3 @@ main()
std::cout << '(' << c2.re << ", " << c2.im << ')' << std::endl;
return 0;
}

16 changes: 7 additions & 9 deletions lang/c++/examples/generic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@
* limitations under the License.
*/

#include <fstream>
#include <complex>
#include <fstream>

#include "cpx.hh"

#include "avro/Compiler.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
#include "avro/Specific.hh"
#include "avro/Encoder.hh"
#include "avro/Generic.hh"
#include "avro/Specific.hh"

int
main()
{
int main() {
std::ifstream ifs("cpx.json");

avro::ValidSchema cpxSchema;
Expand All @@ -51,14 +49,14 @@ main()
avro::decode(*d, datum);
std::cout << "Type: " << datum.type() << std::endl;
if (datum.type() == avro::AVRO_RECORD) {
const avro::GenericRecord& r = datum.value<avro::GenericRecord>();
const avro::GenericRecord &r = datum.value<avro::GenericRecord>();
std::cout << "Field-count: " << r.fieldCount() << std::endl;
if (r.fieldCount() == 2) {
const avro::GenericDatum& f0 = r.fieldAt(0);
const avro::GenericDatum &f0 = r.fieldAt(0);
if (f0.type() == avro::AVRO_DOUBLE) {
std::cout << "Real: " << f0.value<double>() << std::endl;
}
const avro::GenericDatum& f1 = r.fieldAt(1);
const avro::GenericDatum &f1 = r.fieldAt(1);
if (f1.type() == avro::AVRO_DOUBLE) {
std::cout << "Imaginary: " << f1.value<double>() << std::endl;
}
Expand Down
Loading

0 comments on commit 46edd89

Please sign in to comment.