Skip to content

Commit

Permalink
Merge pull request #4 from kevinbackhouse/clang-format
Browse files Browse the repository at this point in the history
clang-format
  • Loading branch information
kevinbackhouse authored Nov 16, 2024
2 parents 0ad5dac + a5a0554 commit a397516
Show file tree
Hide file tree
Showing 20 changed files with 1,484 additions and 2,242 deletions.
964 changes: 421 additions & 543 deletions include/DBusParse/dbus.hpp

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion include/DBusParse/dbus_auth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with DBusParse. If not, see <https://www.gnu.org/licenses/>.


#pragma once

#include <sys/types.h>
Expand Down
10 changes: 4 additions & 6 deletions include/DBusParse/dbus_print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with DBusParse. If not, see <https://www.gnu.org/licenses/>.


#pragma once

#include "dbus.hpp"
Expand All @@ -31,12 +30,11 @@ class PrinterFD final : public Printer {

const size_t tabsize_;

void printBytes(const char* buf, size_t bufsize);
void printBytes(const char *buf, size_t bufsize);

public:
PrinterFD(int fd, size_t base, size_t tabsize) :
fd_(fd), base_(base), tabsize_(tabsize)
{}
PrinterFD(int fd, size_t base, size_t tabsize)
: fd_(fd), base_(base), tabsize_(tabsize) {}

void printChar(char c) override;
void printUint8(uint8_t x) override;
Expand All @@ -48,7 +46,7 @@ class PrinterFD final : public Printer {
void printUint64(uint64_t x) override;
void printInt64(int64_t x) override;
void printDouble(double x) override;
void printString(const std::string& str) override;
void printString(const std::string &str) override;

// Print a newline character, followed by `tabsize_ * indent`
// space chracters.
Expand Down
18 changes: 6 additions & 12 deletions include/DBusParse/dbus_random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
// You should have received a copy of the GNU General Public License
// along with DBusParse. If not, see <https://www.gnu.org/licenses/>.


#pragma once

#include <random>
#include "dbus.hpp"
#include <random>

// Parameters for generating random DBus types and objects.
class DBusRandom {
Expand Down Expand Up @@ -77,15 +76,10 @@ class DBusRandomMersenne : public DBusRandom {
};

// Generate a random DBusType.
const DBusType& randomType(
DBusRandom& r,
DBusTypeStorage& typeStorage, // Type allocator
const size_t maxdepth
);
const DBusType &randomType(DBusRandom &r,
DBusTypeStorage &typeStorage, // Type allocator
const size_t maxdepth);

// Generate a random DBusObject.
std::unique_ptr<DBusObject> randomObject(
DBusRandom& r,
const DBusType& t,
const size_t maxdepth
);
std::unique_ptr<DBusObject> randomObject(DBusRandom &r, const DBusType &t,
const size_t maxdepth);
76 changes: 34 additions & 42 deletions include/DBusParse/dbus_serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
// You should have received a copy of the GNU General Public License
// along with DBusParse. If not, see <https://www.gnu.org/licenses/>.


#pragma once

#include <string.h>
#include "dbus.hpp"
#include <string.h>

// Round pos up to a multiple of alignment.
inline size_t alignup(size_t pos, size_t alignment) {
Expand All @@ -37,7 +36,9 @@ class SerializerDryRunBase : public Serializer {
SerializerDryRunBase() : pos_(0) {}

virtual void writeByte(char) override { pos_ += sizeof(char); }
virtual void writeBytes(const char*, size_t bufsize) override { pos_ += bufsize; }
virtual void writeBytes(const char *, size_t bufsize) override {
pos_ += bufsize;
}
virtual void writeUint16(uint16_t) override { pos_ += sizeof(uint16_t); }
virtual void writeUint32(uint32_t) override { pos_ += sizeof(uint32_t); }
virtual void writeUint64(uint64_t) override { pos_ += sizeof(uint64_t); }
Expand All @@ -58,84 +59,78 @@ class SerializerDryRun final : public SerializerDryRunBase {

size_t getArrayCount() const { return arrayCount_; }

virtual void recordArraySize(
const std::function<uint32_t(uint32_t)>& f
) override;
virtual void
recordArraySize(const std::function<uint32_t(uint32_t)> &f) override;
};

class SerializerInitArraySizes final : public SerializerDryRunBase {
std::vector<uint32_t>& arraySizes_; // Not owned
std::vector<uint32_t> &arraySizes_; // Not owned

public:
SerializerInitArraySizes(std::vector<uint32_t>& arraySizes) :
arraySizes_(arraySizes)
{}
SerializerInitArraySizes(std::vector<uint32_t> &arraySizes)
: arraySizes_(arraySizes) {}

virtual void recordArraySize(
const std::function<uint32_t(uint32_t)>& f
) override;
virtual void
recordArraySize(const std::function<uint32_t(uint32_t)> &f) override;
};

class SerializeToBufferBase : public Serializer {
size_t arrayCount_;
const std::vector<uint32_t>& arraySizes_; // Not owned
const std::vector<uint32_t> &arraySizes_; // Not owned

public:
SerializeToBufferBase(const std::vector<uint32_t>& arraySizes) :
arrayCount_(0), arraySizes_(arraySizes)
{}
SerializeToBufferBase(const std::vector<uint32_t> &arraySizes)
: arrayCount_(0), arraySizes_(arraySizes) {}

virtual void recordArraySize(
const std::function<uint32_t(uint32_t)>& f
) override;
virtual void
recordArraySize(const std::function<uint32_t(uint32_t)> &f) override;
};

template <Endianness endianness>
class SerializeToBuffer final : public SerializeToBufferBase {
size_t pos_;
char* buf_; // Not owned by this class
char *buf_; // Not owned by this class

public:
SerializeToBuffer(const std::vector<uint32_t>& arraySizes, char* buf) :
SerializeToBufferBase(arraySizes), pos_(0), buf_(buf)
{}
SerializeToBuffer(const std::vector<uint32_t> &arraySizes, char *buf)
: SerializeToBufferBase(arraySizes), pos_(0), buf_(buf) {}

virtual void writeByte(char c) override {
buf_[pos_] = c;
pos_ += sizeof(char);
}

virtual void writeBytes(const char* buf, size_t bufsize) override {
virtual void writeBytes(const char *buf, size_t bufsize) override {
memcpy(&buf_[pos_], buf, bufsize);
pos_ += bufsize;
}

virtual void writeUint16(uint16_t x) override {
static_assert(endianness == LittleEndian || endianness == BigEndian);
if (endianness == LittleEndian) {
*(uint16_t*)&buf_[pos_] = htole16(x);
*(uint16_t *)&buf_[pos_] = htole16(x);
} else {
*(uint16_t*)&buf_[pos_] = htobe16(x);
*(uint16_t *)&buf_[pos_] = htobe16(x);
}
pos_ += sizeof(uint16_t);
}

virtual void writeUint32(uint32_t x) override {
static_assert(endianness == LittleEndian || endianness == BigEndian);
if (endianness == LittleEndian) {
*(uint32_t*)&buf_[pos_] = htole32(x);
*(uint32_t *)&buf_[pos_] = htole32(x);
} else {
*(uint32_t*)&buf_[pos_] = htobe32(x);
*(uint32_t *)&buf_[pos_] = htobe32(x);
}
pos_ += sizeof(uint32_t);
}

virtual void writeUint64(uint64_t x) override {
static_assert(endianness == LittleEndian || endianness == BigEndian);
if (endianness == LittleEndian) {
*(uint64_t*)&buf_[pos_] = htole64(x);
*(uint64_t *)&buf_[pos_] = htole64(x);
} else {
*(uint64_t*)&buf_[pos_] = htobe64(x);
*(uint64_t *)&buf_[pos_] = htobe64(x);
}
pos_ += sizeof(uint64_t);
}
Expand Down Expand Up @@ -170,18 +165,15 @@ class SerializeToBuffer final : public SerializeToBufferBase {
// they're ok.)
template <Endianness endianness>
class SerializeToString final : public SerializeToBufferBase {
std::string& str_; // Not owned
std::string &str_; // Not owned

public:
SerializeToString(const std::vector<uint32_t>& arraySizes, std::string& str) :
SerializeToBufferBase(arraySizes), str_(str)
{}
SerializeToString(const std::vector<uint32_t> &arraySizes, std::string &str)
: SerializeToBufferBase(arraySizes), str_(str) {}

virtual void writeByte(char c) override {
str_.push_back(c);
}
virtual void writeByte(char c) override { str_.push_back(c); }

virtual void writeBytes(const char* buf, size_t bufsize) override {
virtual void writeBytes(const char *buf, size_t bufsize) override {
str_.append(buf, bufsize);
}

Expand All @@ -192,7 +184,7 @@ class SerializeToString final : public SerializeToBufferBase {
} else {
x = htobe16(x);
}
writeBytes((const char*)&x, sizeof(x));
writeBytes((const char *)&x, sizeof(x));
}

virtual void writeUint32(uint32_t x) override {
Expand All @@ -202,7 +194,7 @@ class SerializeToString final : public SerializeToBufferBase {
} else {
x = htobe32(x);
}
writeBytes((const char*)&x, sizeof(x));
writeBytes((const char *)&x, sizeof(x));
}

virtual void writeUint64(uint64_t x) override {
Expand All @@ -212,7 +204,7 @@ class SerializeToString final : public SerializeToBufferBase {
} else {
x = htobe64(x);
}
writeBytes((const char*)&x, sizeof(x));
writeBytes((const char *)&x, sizeof(x));
}

virtual void writeDouble(double d) override {
Expand Down
98 changes: 34 additions & 64 deletions include/DBusParse/dbus_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,88 +15,58 @@
// You should have received a copy of the GNU General Public License
// along with DBusParse. If not, see <https://www.gnu.org/licenses/>.


#pragma once

#include <memory>
#include "dbus.hpp"
#include <memory>

void send_dbus_message_with_fds(
const int fd, const DBusMessage& message,
const size_t nfds, const int* fds
);
void send_dbus_message_with_fds(const int fd, const DBusMessage &message,
const size_t nfds, const int *fds);

void send_dbus_message(const int fd, const DBusMessage& message);
void send_dbus_message(const int fd, const DBusMessage &message);

void print_dbus_object(const int fd, const DBusObject& obj);
void print_dbus_object(const int fd, const DBusObject &obj);

void print_dbus_message(const int fd, const DBusMessage& message);
void print_dbus_message(const int fd, const DBusMessage &message);

std::unique_ptr<DBusMessage> receive_dbus_message(const int fd);

std::unique_ptr<DBusMessage> mk_dbus_method_call_msg(
const uint32_t serialNumber,
std::unique_ptr<DBusMessageBody>&& body,
std::string&& path,
std::string&& interface,
std::string&& destination,
std::string&& member,
const size_t nfds,
const MessageFlags flags
);
const uint32_t serialNumber, std::unique_ptr<DBusMessageBody> &&body,
std::string &&path, std::string &&interface, std::string &&destination,
std::string &&member, const size_t nfds, const MessageFlags flags);

std::unique_ptr<DBusMessage> mk_dbus_method_reply_msg(
const uint32_t serialNumber,
const uint32_t replySerialNumber, // serial number that we are replying to
std::unique_ptr<DBusMessageBody>&& body,
std::string&& destination
);
const uint32_t serialNumber,
const uint32_t replySerialNumber, // serial number that we are replying to
std::unique_ptr<DBusMessageBody> &&body, std::string &&destination);

std::unique_ptr<DBusMessage> mk_dbus_method_error_reply_msg(
const uint32_t serialNumber,
const uint32_t replySerialNumber, // serial number that we are replying to
std::string&& destination,
std::string&& errmsg
);

void dbus_method_call_with_fds(
const int fd,
const uint32_t serialNumber,
std::unique_ptr<DBusMessageBody>&& body,
std::string&& path,
std::string&& interface,
std::string&& destination,
std::string&& member,
const size_t nfds,
const int* fds,
const MessageFlags flags = MSGFLAGS_EMPTY
);

void dbus_method_call(
const int fd,
const uint32_t serialNumber,
std::unique_ptr<DBusMessageBody>&& body,
std::string&& path,
std::string&& interface,
std::string&& destination,
std::string&& member,
const MessageFlags flags = MSGFLAGS_EMPTY
);
const uint32_t serialNumber,
const uint32_t replySerialNumber, // serial number that we are replying to
std::string &&destination, std::string &&errmsg);

void dbus_method_call_with_fds(const int fd, const uint32_t serialNumber,
std::unique_ptr<DBusMessageBody> &&body,
std::string &&path, std::string &&interface,
std::string &&destination, std::string &&member,
const size_t nfds, const int *fds,
const MessageFlags flags = MSGFLAGS_EMPTY);

void dbus_method_call(const int fd, const uint32_t serialNumber,
std::unique_ptr<DBusMessageBody> &&body,
std::string &&path, std::string &&interface,
std::string &&destination, std::string &&member,
const MessageFlags flags = MSGFLAGS_EMPTY);

void dbus_method_reply(
const int fd,
const uint32_t serialNumber,
const uint32_t replySerialNumber, // serial number that we are replying to
std::unique_ptr<DBusMessageBody>&& body,
std::string&& destination
);
const int fd, const uint32_t serialNumber,
const uint32_t replySerialNumber, // serial number that we are replying to
std::unique_ptr<DBusMessageBody> &&body, std::string &&destination);

void dbus_method_error_reply(
const int fd,
const uint32_t serialNumber,
const uint32_t replySerialNumber, // serial number that we are replying to
std::string&& destination,
std::string&& errmsg
);
const int fd, const uint32_t serialNumber,
const uint32_t replySerialNumber, // serial number that we are replying to
std::string &&destination, std::string &&errmsg);

void dbus_send_hello(const int fd);
6 changes: 1 addition & 5 deletions include/DBusParseUtils/endianness.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with DBusParse. If not, see <https://www.gnu.org/licenses/>.


#pragma once

enum Endianness {
LittleEndian,
BigEndian
};
enum Endianness { LittleEndian, BigEndian };
Loading

0 comments on commit a397516

Please sign in to comment.