Poco::Tuple

Poco::TuplePoco::NamedTuple を紹介します。

(私は使ったことが無いのですが)boost の tuple とほとんど同じように思えます。

TupleTest.cpp

・TestGetSet() で、単純な get/set を確認。
・TestOperatorLT() では、std::set に3つの tuple を insert して、最初の要素の昇順になること
 (つまり operator < が機能すること)を確認。 ・TestNamedTuple() では Poco::NamedTuple の要素を名前で引けることを確認。

#include <Poco/Format.h>
#include <Poco/Runnable.h>
#include <Poco/Thread.h>
#include <Poco/Tuple.h>
#include <Poco/NamedTuple.h>
 
#include <string>
#include <set>
 
#include "ScopedLogMessage.h"
#include "PrepareConsoleLogger.h"
 
void TestGetSet(ScopedLogMessage& msg)
{
	msg.Message(" --- Test get/set ---");
 
	typedef Poco::Tuple<short, long, double> MyTupleType;
 
	MyTupleType aTuple(1, 2, 3.0);
	msg.Message(Poco::format("  aTuple: (%+hd, %+ld, %+4.2f), length=%i"
				,	aTuple.get<0>()
				,	aTuple.get<1>()
				,	aTuple.get<2>()
				,	static_cast<int>(aTuple.length) ));
 
	aTuple.set<0>(-1 * aTuple.get<0>());
	aTuple.set<1>(-1 * aTuple.get<1>());
	aTuple.set<2>(-1 * aTuple.get<2>());
 
	msg.Message(Poco::format("  aTuple: (%+hd, %+ld, %+4.2f), length=%i"
				,	aTuple.get<0>()
				,	aTuple.get<1>()
				,	aTuple.get<2>()
				,	static_cast<int>(aTuple.length) ));
}
 
void TestOperatorLT(ScopedLogMessage& msg)
{
	msg.Message(" --- Test operator less than ---");
 
	typedef Poco::Tuple<std::string, int, long, float> MyTupleType;
	typedef std::set<MyTupleType> MyTupleSet;
 
	MyTupleType aTuple1("1", 1, 111, 1.11f);
	MyTupleType aTuple2("2", 2, 222, 2.22f);
	MyTupleType aTuple3("3", 3, 333, 3.33f);
 
	MyTupleSet testSet;
	testSet.insert(aTuple3);
	testSet.insert(aTuple2);
	testSet.insert(aTuple1);
 
	MyTupleSet::iterator itr = testSet.begin();
	MyTupleSet::iterator itrEnd = testSet.end();
	int count = 0;
	while(itr != itrEnd)
	{
		msg.Message(Poco::format("  testSet #%i: (\"%s\", %i, %ld, %4.2hf), length=%i"
					,	count++
					,	itr->get<0>()
					,	itr->get<1>()
					,	itr->get<2>()
					,	itr->get<3>()
					,	static_cast<int>(itr->length) ));
		++itr;
	}
}
 
void TestNamedTuple(ScopedLogMessage& msg)
{
	msg.Message(" --- Test NamedTuple ---");
 
	typedef Poco::NamedTuple<std::string, int, long, float> MyTupleType;
 
	MyTupleType aTuple("1", 1, 111, 1.11f);
 
	msg.Message("  names for aTuple elements:");
	for(std::size_t i=0; i<aTuple.names()->size(); ++i)
	{
		msg.Message(Poco::format("   %z: %s", i, aTuple.names()->at(i)));
	}
	msg.Message("  get elements by name:");
	msg.Message(Poco::format("   aTuple: (\"%s\", %i, %ld, %4.2hf), length=%i"
				,	aTuple[aTuple.names()->at(0)].convert<std::string>()
				,	aTuple[aTuple.names()->at(1)].convert<int>()
				,	aTuple[aTuple.names()->at(2)].convert<long>()
				,	aTuple[aTuple.names()->at(3)].convert<float>()
				,	static_cast<int>(aTuple.length) ));
}
 
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);
 
	ScopedLogMessage msg("TupleTest ", "start", "end");
 
	TestGetSet(msg);
	TestOperatorLT(msg);
	TestNamedTuple(msg);
 
	return 0;
}

Results of execution

[0] TupleTest start
[0]  --- Test get/set ---
[0]   aTuple: (+1, +2, +3.00), length=3
[0]   aTuple: (-1, -2, -3.00), length=3
[0]  --- Test operator less than ---
[0]   testSet #0: ("1", 1, 111, 1.11), length=4
[0]   testSet #1: ("2", 2, 222, 2.22), length=4
[0]   testSet #2: ("3", 3, 333, 3.33), length=4
[0]  --- Test NamedTuple ---
[0]   names for aTuple elements:
[0]    0: A
[0]    1: B
[0]    2: C
[0]    3: D
[0]   get elements by name:
[0]    aTuple: ("1", 1, 111, 1.11), length=4
[0] TupleTest end

Downloads

ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2013.05.31 updated)
・2010年8月2日からのダウンロード数:750

Subversion

・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。

Powered by POCO Copyright © 2010 Round Square Inc. All rights reserved.


Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>