Skip to content

Commit

Permalink
lol
Browse files Browse the repository at this point in the history
  • Loading branch information
wardru committed Dec 10, 2023
1 parent 71d596a commit 1d55fd8
Show file tree
Hide file tree
Showing 33 changed files with 284 additions and 565 deletions.
5 changes: 3 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ TabWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortFunctionsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
AllowShortBlocksOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 80
ColumnLimit: 120
AccessModifierOffset: -8
NamespaceIndentation: None
PointerAlignment: Right
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
FixNamespaceComments: false
AlignAfterOpenBracket: Align
PackConstructorInitializers: Never
46 changes: 31 additions & 15 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,45 @@ jobs:
runs-on: ubuntu-latest
container:
image: ghcr.io/panduza/pzacxx-build-img:latest

strategy:
matrix:
os: [linux]
compiler: [gcc]
build_type: [Debug]
shared: ["True", "False"]
exclude:
- os: linux
compiler: mingw
- os: win
compiler: gcc
- os: win
compiler: clang
steps:
- name: Checkout
uses: actions/checkout@v4

- if: ${{ env.ACT }}
name: Hack container for local development
run:
pacman -S --noconfirm nodejs
run: pacman -S --noconfirm nodejs

- name: Cache Conan
id: cache-conan
uses: actions/cache@v3
- name: "cache"
uses: actions/cache@v2
with:
path: ~/.conan2
key: conan-cache
path: ~/.conan2/p
key: conan-cache-${{ matrix.os }}-${{ matrix.compiler }}-${{ matrix.build_type }}-${{ matrix.shared }}

- name:
if: steps.cache-conan.outputs.cache-hit != 'true'
run:
conan install -pr:b ./conan_profiles/linux_gcc -pr:h ./conan_profiles/linux_gcc --build=missing -s build_type=Debug -o shared=False .

- name: "Install"
run:
conan install -pr:b ./conan_profiles/linux_gcc -pr:h ./conan_profiles/${{ matrix.os }}_${{ matrix.compiler }} --build=missing -s build_type=${{ matrix.build_type }} -o shared=${{ matrix.shared }} .

- name: Install dependencies and build (Debug, Static)
- name: "Build"
run: |
echo "Building Debug, Static"
if [ "${{ matrix.shared }}" = "True" ]; then
lib_type="shared"
else
lib_type="static"
fi
ls -l build
echo "build folder: ./build/${{ matrix.os }}-${{ matrix.compiler }}-$lib_type"
cmake --preset ${{ matrix.os }}-${{ matrix.compiler }}-$lib_type
cmake --build ./build/${{ matrix.os }}-${{ matrix.compiler }}-$lib_type --config ${{ matrix.build_type }}
15 changes: 5 additions & 10 deletions examples/bps.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,19 @@ int main()
if (cli->register_devices() < 0)
return -1;

auto vm = cli->get_interface<pza::itf::voltmeter>(
"default", "Panduza_FakeBps", "channel", 0, "vm");
auto vm = cli->get_interface<pza::itf::voltmeter>("default", "Panduza_FakeBps", "channel", 0, "vm");
if (!vm) {
spdlog::error("voltmeter not found");
return -1;
}

auto ctrl = cli->get_interface<pza::itf::bps_chan_ctrl>(
"default", "Panduza_FakeBps", "channel", 0, "ctrl");
auto ctrl = cli->get_interface<pza::itf::bps_chan_ctrl>("default", "Panduza_FakeBps", "channel", 0, "ctrl");
if (!ctrl) {
spdlog::error("ctrl not found");
return -1;
}

auto am = cli->get_interface<pza::itf::ammeter>(
"default", "Panduza_FakeBps", "channel", 0, "am");
auto am = cli->get_interface<pza::itf::ammeter>("default", "Panduza_FakeBps", "channel", 0, "am");
if (!am) {
spdlog::error("ammeter not found");
return -1;
Expand All @@ -44,11 +41,9 @@ int main()
ctrl->set_voltage(3.3);
ctrl->set_current(0.1);

vm->register_measure_callback(
[&]() { spdlog::info("voltage: {}", vm->get_measure()); });
vm->register_measure_callback([&]() { spdlog::info("voltage: {}", vm->get_measure()); });

am->register_measure_callback(
[&]() { spdlog::info("current: {}", am->get_measure()); });
am->register_measure_callback([&]() { spdlog::info("current: {}", am->get_measure()); });

while (true)
;
Expand Down
47 changes: 18 additions & 29 deletions include/pza/core/client.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public:
using u_ptr = std::unique_ptr<client>;
using w_ptr = std::weak_ptr<client>;

explicit client(const std::string &addr, int port,
std::optional<std::string> id = std::nullopt);
explicit client(const std::string &addr, int port, std::optional<std::string> id = std::nullopt);
client(const client &) = delete;
client &operator=(const client &) = delete;
client(client &&) = delete;
Expand All @@ -37,50 +36,40 @@ public:
void set_connection_timeout(unsigned int timeout_ms);
[[nodiscard]] unsigned int get_connection_timeout() const;

device::s_ptr
register_device(const std::string &group, const std::string &name,
unsigned int timeout_ms = device_timeout_default);
device::s_ptr register_device(const std::string &group, const std::string &name,
unsigned int timeout_ms = device_timeout_default);
int register_devices(unsigned int timeout_ms = devices_timeout_default);

[[nodiscard]] device::s_ptr get_device(const std::string &group,
const std::string &name) const;
[[nodiscard]] device::s_ptr get_device(const std::string &group, const std::string &name) const;
[[nodiscard]] std::vector<device::s_ptr> get_devices() const;
[[nodiscard]] std::vector<device::s_ptr>
get_devices_in_group(const std::string &group) const;
[[nodiscard]] std::vector<device::s_ptr> get_devices_in_group(const std::string &group) const;
[[nodiscard]] std::set<std::string> get_groups() const;

[[nodiscard]] itf_base::s_ptr
get_interface(const std::string &group, const std::string &name,
const std::string &interface_group, unsigned int idx,
const std::string &interface_name) const;
[[nodiscard]] itf_base::s_ptr
get_interface(const std::string &group, const std::string &name,
const std::string &interface_name) const;
[[nodiscard]] itf_base::s_ptr get_interface(const std::string &group, const std::string &name,
const std::string &interface_group, unsigned int idx,
const std::string &interface_name) const;
[[nodiscard]] itf_base::s_ptr get_interface(const std::string &group, const std::string &name,
const std::string &interface_name) const;

template <typename T>
[[nodiscard]] std::shared_ptr<T>
get_interface(const std::string &group, const std::string &name,
const std::string &interface_group, unsigned int idx,
const std::string &interface_name) const
[[nodiscard]] std::shared_ptr<T> get_interface(const std::string &group, const std::string &name,
const std::string &interface_group, unsigned int idx,
const std::string &interface_name) const
{
return std::dynamic_pointer_cast<T>(get_interface(
group, name, interface_group, idx, interface_name));
return std::dynamic_pointer_cast<T>(get_interface(group, name, interface_group, idx, interface_name));
}

template <typename T>
[[nodiscard]] std::shared_ptr<T>
get_interface(const std::string &group, const std::string &name,
const std::string &interface_name) const
[[nodiscard]] std::shared_ptr<T> get_interface(const std::string &group, const std::string &name,
const std::string &interface_name) const
{
return std::dynamic_pointer_cast<T>(
get_interface(group, name, interface_name));
return std::dynamic_pointer_cast<T>(get_interface(group, name, interface_name));
}

private:
static constexpr unsigned int platforms_timeout_default = 500;
static constexpr unsigned int device_timeout_default = 500;
static constexpr unsigned int devices_timeout_default =
device_timeout_default * 5;
static constexpr unsigned int devices_timeout_default = device_timeout_default * 5;

std::unique_ptr<client_impl> _impl;
};
Expand Down
22 changes: 8 additions & 14 deletions include/pza/core/device.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public:
using u_ptr = std::unique_ptr<device>;
using w_ptr = std::weak_ptr<device>;

explicit device(mqtt_service &mqtt, struct device_info &info);
explicit device(mqtt_service *mqtt, struct device_info &info);
device(const device &) = delete;
device &operator=(const device &) = delete;
device(device &&) = delete;
Expand All @@ -35,32 +35,26 @@ public:

[[nodiscard]] unsigned int get_number_of_interfaces() const;

[[nodiscard]] itf_base::s_ptr
get_interface(const std::string &name) const;
[[nodiscard]] itf_base::s_ptr get_interface(const std::string &name) const;

template <typename T>
std::shared_ptr<T> get_interface(const std::string &name) const
template <typename T> std::shared_ptr<T> get_interface(const std::string &name) const
{
return std::dynamic_pointer_cast<T>(get_interface(name));
}

[[nodiscard]] itf_base::s_ptr
get_interface(const std::string &interface_group, unsigned int idx,
const std::string &name) const;
[[nodiscard]] itf_base::s_ptr get_interface(const std::string &interface_group, unsigned int idx,
const std::string &name) const;

template <typename T>
std::shared_ptr<T> get_interface(const std::string &interface_group,
unsigned int idx,
std::shared_ptr<T> get_interface(const std::string &interface_group, unsigned int idx,
const std::string &name) const
{
return std::dynamic_pointer_cast<T>(
get_interface(interface_group, idx, name));
return std::dynamic_pointer_cast<T>(get_interface(interface_group, idx, name));
}

[[nodiscard]] std::vector<std::string> get_interface_names() const;

[[nodiscard]] unsigned int
get_interface_group_count(const std::string &group) const;
[[nodiscard]] unsigned int get_interface_group_count(const std::string &group) const;

private:
std::unique_ptr<device_impl> _impl;
Expand Down
2 changes: 1 addition & 1 deletion include/pza/core/interface.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public:
[[nodiscard]] const std::string &get_type() const;

protected:
explicit itf_base(mqtt_service &mqtt, itf_info &info);
explicit itf_base(mqtt_service *mqtt, itf_info &info);

std::unique_ptr<itf_impl> _impl;
};
Expand Down
2 changes: 1 addition & 1 deletion include/pza/interfaces/ammeter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public:
using u_ptr = std::unique_ptr<ammeter>;
using w_ptr = std::weak_ptr<ammeter>;

explicit ammeter(mqtt_service &mqtt, itf_info &info);
explicit ammeter(mqtt_service *mqtt, itf_info &info);
ammeter(const ammeter &) = delete;
ammeter(ammeter &&) = delete;
ammeter &operator=(const ammeter &) = delete;
Expand Down
2 changes: 1 addition & 1 deletion include/pza/interfaces/bps_chan_ctrl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public:
using u_ptr = std::unique_ptr<bps_chan_ctrl>;
using w_ptr = std::weak_ptr<bps_chan_ctrl>;

explicit bps_chan_ctrl(mqtt_service &mqtt, itf_info &info);
explicit bps_chan_ctrl(mqtt_service *mqtt, itf_info &info);
bps_chan_ctrl(const bps_chan_ctrl &) = delete;
bps_chan_ctrl(bps_chan_ctrl &&) = delete;
bps_chan_ctrl &operator=(const bps_chan_ctrl &) = delete;
Expand Down
2 changes: 1 addition & 1 deletion include/pza/interfaces/device.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public:
using u_ptr = std::unique_ptr<device>;
using w_ptr = std::weak_ptr<device>;

explicit device(mqtt_service &mqtt, itf_info &info);
explicit device(mqtt_service *mqtt, itf_info &info);
device(const device &) = delete;
device(device &&) = delete;
device &operator=(const device &) = delete;
Expand Down
2 changes: 1 addition & 1 deletion include/pza/interfaces/meter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public:
using u_ptr = std::unique_ptr<meter>;
using w_ptr = std::weak_ptr<meter>;

explicit meter(mqtt_service &mqtt, itf_info &info);
explicit meter(mqtt_service *mqtt, itf_info &info);
meter(const meter &) = delete;
meter(meter &&) = delete;
meter &operator=(const meter &) = delete;
Expand Down
2 changes: 1 addition & 1 deletion include/pza/interfaces/platform.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public:
using u_ptr = std::unique_ptr<platform>;
using w_ptr = std::weak_ptr<platform>;

explicit platform(mqtt_service &mqtt, itf_info &info);
explicit platform(mqtt_service *mqtt, itf_info &info);
platform(const platform &) = delete;
platform(platform &&) = delete;
platform &operator=(const platform &) = delete;
Expand Down
2 changes: 1 addition & 1 deletion include/pza/interfaces/voltmeter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public:
using u_ptr = std::unique_ptr<voltmeter>;
using w_ptr = std::weak_ptr<voltmeter>;

explicit voltmeter(mqtt_service &mqtt, itf_info &info);
explicit voltmeter(mqtt_service *mqtt, itf_info &info);
voltmeter(const voltmeter &) = delete;
voltmeter(voltmeter &&) = delete;
voltmeter &operator=(const voltmeter &) = delete;
Expand Down
14 changes: 5 additions & 9 deletions source/core/attribute.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "attribute.hxx"

attribute::attribute(std::string name) : _name(std::move(name))
attribute::attribute(std::string name)
: _name(std::move(name))
{
}

Expand Down Expand Up @@ -30,8 +31,7 @@ void attribute::on_message(mqtt::const_message_ptr msg)
} else if (std::holds_alternative<bool>(type)) {
_set_field<bool>(json, name);
} else {
spdlog::error(
"attribute::on_message: unknown field type");
spdlog::error("attribute::on_message: unknown field type");
}
}

Expand All @@ -42,15 +42,11 @@ void attribute::on_message(mqtt::const_message_ptr msg)
}
}

void attribute::register_callback(const std::function<void(void)> &cb)
{
_callbacks.push_back(cb);
}
void attribute::register_callback(const std::function<void(void)> &cb) { _callbacks.push_back(cb); }

void attribute::remove_callback(const std::function<void(void)> &cb)
{
_callbacks.remove_if([&](const std::function<void(void)> &f) {
return f.target_type() == cb.target_type() &&
f.target<void(void)>() == cb.target<void(void)>();
return f.target_type() == cb.target_type() && f.target<void(void)>() == cb.target<void(void)>();
});
}
Loading

0 comments on commit 1d55fd8

Please sign in to comment.