分析模式-计量的C++实现——完美版本

以下是http://www.csdn.net/develop/Article/14/14487.shtm的一个改进版本,本来是作为评论附在上一篇文章后面的,但是作为评论发表的话,会丢失很多文字,很奇怪的说。本文是我和singleracer讨论的结果,个人认为几乎接近完美,希望能对大家有帮助,大家可以自己加入其它的单位体系和单位。

计量的模板类:

#ifndef _QUANTITY_H_
#define _QUANTITY_H_

#ifdef _DEBUG
#define ASSERT_SAME_TYPE( T1, T2) do {T1 v=T2();v; }while(false)
#else
#define ASSERT_SAME_TYPE( T1, T2)
#endif

#include "singleton.h"

template<class UnitType>
struct UnitTraits {
 typedef UnitType::Validator Validator;
};

template<class ValueType>
class ValueTraits {
public:
 static bool equal(const ValueType & l, const ValueType & r)
 {return (l == r);}
};

template<>
class ValueTraits<double> {
public:
 static bool equal(const double & l, const double & r)
 {
  static double precision = 0.000000001;
  return (l - r < precision);
 }
};

template<>
class ValueTraits<float> {
public:
 static bool equal(const float & l, const float & r)
 {
  static float precision = 0.000000001f;
  return (l - r < precision);
 }
};

template<typename ValueType, class UnitValidator, class _Traits = ValueTraits<ValueType> >
class Quantity
{
public:
 typedef UnitValidator Validator;

 template<typename UnitType>Quantity(const ValueType & amount, const UnitType & )
  : m_amount(amount), m_unit(Singleton< unit_wrap<UnitType, ValueType> >::Instance())
 {
  ASSERT_SAME_TYPE(UnitTraits<UnitType>::Validator, Validator);
 }
 ~Quantity()
 {
 }
 Quantity(const Quantity & other)
  : m_amount(o

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
paho.mqtt.embedded-c是一个C语言实现的MQTT客户端库,它提供了MQTT协议的完整实现,可以运行在嵌入式系统中。如果你需要在C++项目中使用MQTT客户端,可以使用paho.mqtt.embedded-c库的C++封装,也就是paho.mqtt.cpp库。 paho.mqtt.cpp库是一个基于paho.mqtt.embedded-c库的C++封装,它提供了更加易用的C++ API,使得在C++项目中使用MQTT更加方便。 下面是一个使用paho.mqtt.cpp库实现MQTT客户端的示例代码: ```c++ #include <cstdlib> #include <iostream> #include <cstring> #include <chrono> #include <thread> #include "mqtt/async_client.h" const std::string SERVER_ADDRESS("tcp://localhost:1883"); const std::string CLIENT_ID("paho_cpp_async_subcribe"); const std::string TOPIC("hello"); class callback : public virtual mqtt::callback { public: virtual void connection_lost(const std::string& cause) override { std::cout << "Connection lost: " << cause << std::endl; } virtual void message_arrived(mqtt::const_message_ptr msg) override { std::cout << "Message arrived" << std::endl; std::cout << "Topic: " << msg->get_topic() << std::endl; std::cout << "Payload: " << msg->to_string() << std::endl; } virtual void delivery_complete(mqtt::delivery_token_ptr token) override { std::cout << "Delivery complete" << std::endl; } }; int main(int argc, char* argv[]) { mqtt::async_client client(SERVER_ADDRESS, CLIENT_ID); callback cb; client.set_callback(cb); mqtt::connect_options conn_opts; conn_opts.set_keep_alive_interval(20); conn_opts.set_clean_session(true); std::cout << "Connecting to the MQTT server..." << std::flush; try { mqtt::token_ptr conntok = client.connect(conn_opts); conntok->wait(); std::cout << "OK" << std::endl; } catch (const mqtt::exception& exc) { std::cerr << "\nERROR: Unable to connect to MQTT server: " << exc.what() << std::endl; return 1; } mqtt::token_ptr subtok = client.subscribe(TOPIC, 0); subtok->wait(); std::cout << "Subscribed to topic: " << TOPIC << std::endl; while (true) { std::this_thread::sleep_for(std::chrono::seconds(1)); } client.unsubscribe(TOPIC)->wait(); client.disconnect()->wait(); return 0; } ``` 在这个示例代码中,我们使用了paho.mqtt.cpp库来连接到MQTT服务器,订阅一个主题,然后等待消息的到来。当消息到来时,我们会打印出来消息的主题和内容。 如果你需要在C++项目中使用MQTT客户端,paho.mqtt.cpp库会是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

noho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值