[C++] POCO learning summary (13): types and byte order, error handling and debugging, and obtaining system information

[C++] Guo Laoer’s blog post: C++ directory

1. Type

POCO defines types for fixed-size integers
Header file: #include “Poco/Types.h”

  • Poco::Int8
  • Bit::Int16,
  • Poco::Int32,
  • Bit::Int64
  • Poco::UInt8
  • Poco::UInt16
  • Poco::UInt32
  • Poco::UInt64
  • Poco::IntPtr, Poco::UIntPtr: an integer with the same size as the pointer (32 or 64 bits)

2. Long type and pointer type

POCO has two macros to determine the size of long types and pointer types.
If the pointer is 64 bits, there is a macro definition: POCO_PTR_IS_64_BIT, otherwise none
If the long is 64 bits, there is a macro definition: POCO_LONG_IS_64_BIT, otherwise none a>

3. Poco::ByteOrder byte order

3.1 Native byte order

POCO uses macros to determine the byte order of the current host:

  • POCO_ARCH_LITTLE_ENDIAN: If the machine is little endian
  • POCO_ARCH_BIG_ENDIAN: If the machine is big endian

3.2 Byte order conversion

The class Poco::ByteOrder provides static methods for byte order conversion.
All functions available for: Int16, UInt16, Int32, UInt32, Int64, UInt64

  • IntXX flipBytes(IntXX value): Change the byte order from big endian to little endian and vice versa
  • IntXX toBigEndian(IntXX value): Convert host byte order to big endian
  • IntXX toLittleEndian(IntXX value): Convert host byte order to little endian
  • IntXX fromBigEndian(IntXX value): Convert from big endian to host byte order
  • IntXX fromLittleEndian(IntXX value): Convert little endian byte order to host byte order
  • IntXX tonnetwork (IntXX value): Convert host byte order to network byte order
  • IntXX fromNetwork(IntXX value): Convert network byte order to host byte order
  • Network byte order is big endian

3.3 Example

vi ByteOrderTest.cpp

#include "Poco/ByteOrder.h"
#include <iostream>
using Poco::ByteOrder;
using Poco::UInt16;
int main(int argc, char** argv)
{
    
    
	#ifdef POCO_ARCH_LITTLE_ENDIAN
	std::cout << "little endian" << std::endl;
	#else
	std::cout << "big endian" << std::endl;
	#endif
	UInt16 port = 80;
	UInt16 networkPort = ByteOrder::toNetwork(port);
	return 0;
}

Compile:

g++ ByteOrderTest.cpp -I ~/git/poco/install/include -L ~/git/poco/install/lib -lPocoFoundationd

Output: (most computers are little endian)

little endian

4. Poco::Any any type

4.1 Description

Instances of Poco::Any can hold values ​​of any built-in or user-defined type.
Poco: Any supports value semantics.
The value can be extracted in a type-safe manner.
The type of the value must be known in order to extract it.
Poco::AnyCast() and Poco::RefAnyCast() function templates are used to extract values.

4.2 Example

I saw any.cpp

#include "Poco/Any.h"
#include "Poco/Exception.h"
#include <iostream>

using Poco::Any;
using Poco::AnyCast;
using Poco::RefAnyCast;

int main(int argc, char** argv)
{
    
    
	Any any(42);
	int i = AnyCast<int>(any); // okay
	int& ri = RefAnyCast<int>(any); // okay
	std::cout << "i = " << i << "; ri = " << ri << std::endl;

	try
	{
    
    
		short s = AnyCast<short>(any); // throws BadCastException
	}
	catch (Poco::BadCastException&)
	{
    
    
		std::cout << "BadCastException" << std::endl;
	}
	return 0;
}

Compile:

g++ any.cpp -I ~/git/poco/install/include -L ~/git/poco/install/lib -lPocoFoundationd

Output:

i = 42; ri = 42
BadCastException

5. Poco::DynamicAny dynamic any type

5.1 Description

Header file: #include "Poco/DynamicAny.h"
Instances of Poco::DynamicAny can save dynamic values ​​of any type.
Poco::DynamicAny supports value semantics.
The value can be extracted in a type-safe manner.
Supports safe implicit and explicit conversions (checked ranges) of various types (standard types, std::string).

Note: (data loss of numeric values ​​is prohibited)

  • value < 0 is never converted to unsigned type
  • value requires x bits and will never be converted to a smaller bit range (eg: value = 2000, requires 16 bits, conversion to 8 bits is prohibited)
  • Allows loss of precision from int to float and back
  • Allow string truncation (string to a single character)

5.2 Example

I saw dany.cpp

#include "Poco/DynamicAny.h"
#include "Poco/Exception.h"
#include <iostream>
using Poco::DynamicAny;

int main(int argc, char** argv)
{
    
    
	DynamicAny any(42);
	int i = any;
	std::string s(any.convert<std::string>());
	std::cout << "i = " << i << "; s = " << s << std::endl;
	any.convert(s); // or without the need to cast
	const int& ri(any.extract<int>());
	short ss = any;
	std::cout << "ri = " << ri << "; ss = " << ss << std::endl;

	try
	{
    
    
		short s = any.extract<short>(); // throws BadCastException
	}
	catch (Poco::BadCastException&)
	{
    
    
		std::cout << "BadCastException" << std::endl;
	}
	return 0;
}

Compile:

g++ dany.cpp -I ~/git/poco/install/include -L ~/git/poco/install/lib -lPocoFoundationd

Output:

i = 42; s = 42
ri = 42; ss = 42
BadCastException

6. Abnormality

6.1 Description

All POCO exceptions are subclasses of POCO::Exception, which is derived from std::Exception.
Header file: #include “Poco/Exception.h”
For example:

  • Poco::LogicException: logic error
  • Poco::RuntimeException: running error
  • Poco::ApplicationException: Application level error

Each Poco::Exception has a name, message (describing the cause of the exception), and optional nested exceptions.

6.2 Commonly used functions

  • const char* name() const: Returns the name of the exception
  • std::string& message() const: returns the message text passed by the constructor
  • std::string displayText() const: returns the name and message text, separated by ":"
  • const Exception* nested() const: Returns a pointer to a nested exception, or 0 if there is none
  • Exception* clone() const: returns an exact copy of the exception
  • void rethrow() const: Rethrow the exception

6.3 Custom exceptions

POCO_DECLARE_EXCEPTION: Declare exception class
POCO_IMPLEMENT_EXCEPTION: Implement exception class

// MyException.h
#include “Poco/Exception.h”
POCO_DECLARE_EXCEPTION(MyLib_API, MyException, Poco::Exception)

// MyException.cpp
#include “MyException.h”
POCO_IMPLEMENT_EXCEPTION(MyException, Poco::Exception, “Something really bad happened…”)

7, affirmation

Macros checked at runtime

  • poco_assert(cond): If cond≠true, throw AssertionViolationException
  • poco_assert_dbg(cond): similar to poco_assert, but only used in debug builds
  • poco_check_ptr(ptr): If ptr is empty, throw NullPointerException
  • poco_bugcheck(), poco_bugcheck_msg(string): throw BugcheckException

8. Debugging

1) In a debug build, you can use the macros poco_debugger() and poco_debugger_msg(message) to force breakpoints (if the code is running under the control of a debugger).
2) poco_assert, poco_check_ptr and poco_bugcheck are enabled in both debug and release versions. In debug builds, if the debugger is available, the breakpoint is triggered before the exception is thrown.
3) poco_assert_dbg and poco_debugger are only enabled in debug versions.
4) Use bool Debugger::isAvailable() to check whether it is running under the debugger.
5) Use void Debugger::enter() to force a breakpoint.
6) Use void Debugger::message() to write messages to the debugger log or standard output.

9. Obtain system information when compiling

9.1 POCO_OS platform related macros

Header file: Poco/Platform.h
The POCO_OS macro can be used to determine the operating system. It will have one of the following values:
POCO_OS_AIX
POCO_OS_LINUX
POCO_OS_SOLARIS
POCO_OS_CYGWIN a>
POCO_OS_MAC_OS_X
POCO_OS_TRU64
POCO_OS_FREE_BSD
POCO_OS_NET_BSD
POCO_OS_VMS POCO_OS_QNX POCO_OS_WINDOWS_NT POCO_OS_IRIX POCO_OS_VXWORKS POCO_OS_OPEN_BSD
POCO_OS_HPUX




Windows platform: POCO_OS_FAMILY_WINDOWS
Unix platform: POCO_OS_FAMILY_UNIX

9.2 POCO_ARCH hardware-related macros

The POCO_ARCH macro can be used to determine the hardware architecture. It will have one of the following values:
POCO_ARCH_ALPHA
POCO_ARCH_IA64: Intel 64-bit
POCO_ARCH_AMD64
POCO_ARCH_MIPS
POCO_ARCH_ARM
POCO_ARCH_POWER
POCO_ARCH_HPPA
POCO_ARCH_PPC
POCO_ARCH_IA32: Intel 32-bit
POCO_ARCH_SPARC

9.3 Big and small endian

POCO_ARCH_LITTLE_ENDIAN: The architecture is little endian
POCO_ARCH_BIG_ENDIAN: The architecture is big endian

10. System information during runtime

Static function of Poco::Environment, used to determine system and environment information at runtime.
Header file: #include “Poco/Environment.h”

  • std::string get(const Std::string& name): Returns the value of the environment variable. If the variable is not defined, Poco::NotFoundException is thrown.
  • bool has(const std::string& name): Check whether the environment variable is defined.
  • void set(const std::string& name, const std::string& value): Set the value of the environment variable.
  • std:: string osName (): Returns the name of the operating system (uname).
  • std::string osVersion(): Returns the version of the operating system (uname -r).
  • std::string osArchitecture(): Returns a string describing the hardware architecture (uname -m).
  • std::string nodeName(): Returns the computer name (uname -n). Similar to Poco::DNS::hostName()
  • std::string nodeId(): Returns the Ethernet address of the first Ethernet adapter found on the system in the format xx:xx:xx:xx:xx (or all zeros if there is no Ethernet adapter).
#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;
	if (Environment::has("HOME"))
		std::cout << "Home: " << Environment::get("HOME") << std::endl;
	Environment::set("POCO", "foo");
	return 0;
}

Compile:

g++ e.cpp -I ~/git/poco/install/include -L ~/git/poco/install/lib -lPocoFoundationd

Output:

OS Name: Linux
OS Version: 6.2.0-37-generic
OS Arch: x86_64
Node Name: laoer-VirtualBox
Node ID: 08:00:27:ad:b1:7a
Home: /home/laoer

Guess you like

Origin blog.csdn.net/u010168781/article/details/134958957