MongoDB C++ Driver取值模版

项目中遇到了使用mongodb存储软件执行记录的需求,在调用mongodb的c++ driver进行数据查询时,从mongodb中取值的方法如下:

try
{
	auto cursor = collection.find({});
	for(row : cursor)
	{
		struct record;
		record.ident = 	QString::fromStdString(bsoncxx::string::to_string(row["ident"].get_utf8().value));
		record.startTime = QDateTime::fromMSecsSinceEpoch(row["start_time"].get_int64().value);
		record.endTime = QDateTime::fromMSecsSinceEpoch(row["end_time"].get_int64().value);
		record.value = row["value"].get_double().value;
	}
	return true;
}
catch (std::exception e)
{
	return false;
}

但是,当mongodb的字段修改后,比如value字段没有了,那么row[“value”]将会抛出异常,但是这种情况下,我希望的是字段不存在则写入一个默认值。
不过如果每一条取值语句都用try…catch包裹起来太过繁杂,于是添加了以下取值模版。

#pragma once

#include <bsoncxx/document/view.hpp>

#include <type_traits>

//bool
template <class T>
typename std::enable_if<std::is_same<bool, T>::value, T>::type bson_get(bsoncxx::document::view view
	, const std::string& key
	, const T& default = T())
{
	try
	{
		return view[key].get_bool().value;
	}
	catch (std::exception e)
	{
		return default;
	}
}

//int, int32
template <class T>
typename std::enable_if<std::is_same<int, T>::value, T>::type bson_get(bsoncxx::document::view view
	, const std::string& key
	, const T& default = T())
{
	try
	{
		return view[key].get_int32().value;
	}
	catch (std::exception e)
	{
		return default;
	}
}

//int64_t
template <class T>
typename std::enable_if<std::is_same<int64_t, T>::value, T>::type bson_get(bsoncxx::document::view view
	, const std::string& key
	, const T& default = T())
{
	try
	{
		return view[key].get_int64().value;
	}
	catch (std::exception e)
	{
		return default;
	}
}

//double
template <class T>
typename std::enable_if<std::is_same<double, T>::value, T>::type bson_get(bsoncxx::document::view view
	, const std::string& key
	, const T& default = T())
{
	try
	{
		return view[key].get_double().value;
	}
	catch (std::exception e)
	{
		return default;
	}
}

//std::string
template <class T>
typename std::enable_if<std::is_same<std::string, T>::value, T>::type bson_get(bsoncxx::document::view view
	, const std::string& key
	, const T& default = T())
{
	try
	{
		return bsoncxx::string::to_string(view[key].get_utf8().value);
	}
	catch (std::exception e)
	{
		return default;
	}
}

//QString
template <class T>
typename std::enable_if<std::is_same<QString, T>::value, T>::type bson_get(bsoncxx::document::view view
	, const std::string& key
	, const T& default = T())
{
	try
	{
		auto&& string = bsoncxx::string::to_string(view[key].get_utf8().value);
		return QString::fromUtf8(string.c_str(), string.size());
	}
	catch (std::exception e)
	{
		return default;
	}
}

//QDateTime
template <class T>
typename std::enable_if<std::is_same<QDateTime, T>::value, T>::type bson_get(bsoncxx::document::view view
	, const std::string& key
	, const T& default = T())
{
	try
	{
		return QDateTime::fromMSecsSinceEpoch(view[key].get_int64());
	}
	catch (std::exception e)
	{
		return default;
	}
}

这样之前的取值方式就可以修改为:

try
{
	auto cursor = collection.find({});
	for(row : cursor)
	{
		struct record;
		record.ident = bson_get<QString>(row, "ident");
		record.startTime = bson_get<QDateTime>(row, "start_time");
		record.endTime = bson_get<QDateTime>(row, "end_time");
		record.value = bson_get<double>(row, "value", 0.0);
	}
	return true;
}
catch (std::exception e)
{
	return false;
}

这样在value字段不存在时,record就会获得一个默认值0.0

使用C++20更加简洁:

#include <bsoncxx/document/view.hpp>

#include <type_traits>
#include <concepts>

#include <QString>
#include <QDateTime>

template <typename T>
concept Ret = std::same_as<T, int>
	|| std::same_as<T, bool>
	|| std::same_as<T, int64_t>
	|| std::same_as<T, std::string>
	|| std::same_as<T, double>
	|| std::same_as<T, QString>
	|| std::same_as<T, QDateTime>;

template <Ret T>
auto bson_get(bsoncxx::document::view view
	, const std::string& key
	, const T& default = T())
{
	try
	{
		if constexpr (std::same_as<T, bool>)
			return view[key].get_bool().value;
		else if constexpr (std::same_as<T, QString>)
			return QString::fromUtf8(view[key].get_utf8().value.data());
		else if constexpr (std::same_as<T, int>)
			return view[key].get_int32().value;
		else if constexpr (std::same_as<T, int64_t>)
			return view[key].get_int64().value;
		else if constexpr (std::same_as<T, QDateTime>)
			return QDateTime::fromMSecsSinceEpoch(view[key].get_int64().value);
		else if constexpr (std::same_as<T, double>)
			return view[key].get_double().value;
		else if constexpr (std::same_as<T, std::string>)
			return std::string(view[key].get_utf8().value);
		else
			static_assert(false,"get value failed");
	}
	catch (std::exception e)
	{
		return default;
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值