POCO C++库学习和分析 平台与环境

摘要:
POCOC++库学习和分析——在编写程序时,平台和环境有时需要收集一些系统信息来绑定软件和硬件或生成唯一的注册代码信息。Poco提供了一个非常简单的类环境来实现这个函数。
POCO C++库学习和分析 -- 平台与环境

在写程序的时候,有时候需要收集一些系统信息,用作软硬件的绑定或生成唯一的注册码信息等。Poco中提供了一个很简单的类Environment来实现这个功能。这个类的定义如下:

class Foundation_API Environment
	/// This class provides access to environment variables
	/// and some general system information.
{
public:
	typedef UInt8 NodeId[6]; /// Ethernet address.
	
	static std::string get(const std::string& name);
		/// Returns the value of the environment variable
		/// with the given name. Throws a NotFoundException
		/// if the variable does not exist.
		
	static std::string get(const std::string& name, const std::string& defaultValue);
		/// Returns the value of the environment variable
		/// with the given name. If the environment variable
		/// is undefined, returns defaultValue instead.
		
	static bool has(const std::string& name);
		/// Returns true iff an environment variable
		/// with the given name is defined.
		
	static void set(const std::string& name, const std::string& value);
		/// Sets the environment variable with the given name
		/// to the given value.

	static std::string osName();
		/// Returns the operating system name.
		
	static std::string osVersion();
		/// Returns the operating system version.
		
	static std::string osArchitecture();
		/// Returns the operating system architecture.
		
	static std::string nodeName();
		/// Returns the node (or host) name.
		
	static void nodeId(NodeId& id);
		/// Returns the Ethernet address of the first Ethernet
		/// adapter found on the system.
		///
		/// Throws a SystemException if no Ethernet adapter is available.
		
	static std::string nodeId();
		/// Returns the Ethernet address (format "xx:xx:xx:xx:xx:xx")
		/// of the first Ethernet adapter found on the system.
		///
		/// Throws a SystemException if no Ethernet adapter is available.
		
	static unsigned processorCount();
		/// Returns the number of processors installed in the system.
		///
		/// If the number of processors cannot be determined, returns 1.
		
	static Poco::UInt32 libraryVersion();
		/// Returns the POCO C++ Libraries version as a hexadecimal
		/// number in format 0xAABBCCDD, where
		///    - AA is the major version number,
		///    - BB is the minor version number,
		///    - CC is the revision number, and
		///    - DD is the patch level number.
		///
		/// Some patch level ranges have special meanings:
		///    - Dx mark development releases,
		///    - Ax mark alpha releases, and
		///    - Bx mark beta releases.
};

从定义中我们可以看到,它的功能包括:

1. 获取系统第一块网卡的信息

2. 获取、设置指定名称的环境变量值

3. 获取操作系统名称、版本、结构

4. 获取处理器数量

下面是其的一个使用例子:

#include "stdafx.h"
#include "Poco/Environment.h"
#include <iostream>
using Poco::Environment;


int main(int argc, char** argv)
{
	std::cout
		<< "OS Name: " << Environment::osName() << std::endl
		<< "OS Version: " << Environment::osVersion() << std::endl
		<< "OS Arch: " << Environment::osArchitecture() << std::endl
		<< "Node Name: " << Environment::nodeName() << std::endl
		<< "Node ID: " << Environment::nodeId() << std::endl
		<< "Processor Count: " << Environment::processorCount() << std::endl
		<< "Library Version: " << Environment::libraryVersion() << std::endl;

	if (Environment::has("TEMP"))
		std::cout << "TEMP: " << Environment::get("TEMP") << std::endl;
	Environment::set("POCO", "foo");

	return 0;

}

Environment的内部的实现上很简单,依赖于EnvironmentImpl类,每中操作系统实现了自己的EnvironmentImpl类,从而实现了对不同操作系统统一接口。

(版权所有,转载时请注明作者和出处 http://blog.csdn.net/arau_sh/article/details/8698406

免责声明:文章转载自《POCO C++库学习和分析 平台与环境》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇matlab中的曲线拟合与插值安卓自动化单元测试——登录模块测试下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

WPF知识点全攻略13- 绘图

行&lt;线条X1=“10”Y1=“100”X2=“260”Y2=“100“Stroke=“黑色”StrokeDashArray=“5”StrokeThickness=“2”&gt;线冲程&gt;矩形&lt;矩形边距=“5”笔划=“黑色”高度=“100”宽度=“100“&gt;&lt;&书信电报,...

Cesium深入浅出之视频投影【转】

通常,我们使用矩形,因为视频形状是方形的。据怀疑,视频标签隐藏了这段关系。如果再次显示,视频将再次移动。此处使用VideoSynchronizer。它可以使视频元素与铯的模拟时钟同步。让我们看看它的构造函数:name type description optionsObject option子属性:name type默认值description用于驱动视频的...

SecureCRT优化调整、永久设置、保护眼睛和配色方案

您可以根据个人喜好调整字体大小。我已经习惯了4号字体。到目前为止,SecureCRT优化已经完成。...

转:(WIN)S04-CH01 PCIE XDMA开发环境搭建以及环路测试

XDMAIP使用部分教程分LINUX篇和WINDOWS篇两个部分。通过实战,面向应用,提供给大家XILINXFPGAPCIE应用解决方案。本教程以MK7160FA作为样机测试。这是一款高性价比的FPGA开发板。而M_AXI_LITE挂载的BRAM是需要进行BAR空间操作,所以地址设置为0x80000000...

「雕爷学编程」Arduino动手做(26)——4X4矩阵键盘模块

37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。108种传感器模块系列实验实验二十六:4X4矩阵键盘模块矩阵键盘是单片机外部设备中所使用的排布类似于矩阵的键盘组。工作原理矩阵键盘又称为行列式键盘,它是用4条I/O线作为行线,4条I/O线作为列线组成的键盘。矩阵键盘所需库文件在ArduinoIDE1.8.0...

如何给LINUX红帽子版本配置IP(转)

如果需要绑定另一个IP地址,只需在文件名中添加一个,并在文件中的DEVICE中添加eth0:x。LINUX可以支持多达255个IP别名,多个网卡绑定到一个IP地址,并且可以使用多个网卡创建具有相同IP地址的虚拟网卡。事实上,这种技术已经存在于Sun和Cisco中,它们分别被称为中继和以太网信道技术。在Linux中,这种技术被称为绑定。...