示例#1
0
void WinConfigurationTest::testConfiguration()
{
    WinRegistryKey regKey("HKEY_CURRENT_USER\\Software\\Applied Informatics\\Test");
    if (regKey.exists()) regKey.deleteKey();
    assert (!regKey.exists());

    AutoPtr<WinRegistryConfiguration> pReg = new WinRegistryConfiguration("HKEY_CURRENT_USER\\Software\\Applied Informatics\\Test");
    pReg->setString("name1", "value1");
    assert (pReg->getString("name1") == "value1");
    pReg->setInt("name1", 1); // overwrite should also change type
    assert (pReg->getInt("name1") == 1);
    pReg->setString("name2", "value2");
    assert (pReg->getString("name2") == "value2");
#if defined(POCO_HAVE_INT64)
    pReg->setUInt64("name2", std::numeric_limits<UInt64>::max()); // overwrite should also change type
    assert (pReg->getUInt64("name2") == std::numeric_limits<UInt64>::max());
    pReg->setInt64("name2", std::numeric_limits<Int64>::min());
    assert (pReg->getInt64("name2") == std::numeric_limits<Int64>::min());
#endif
    assert (pReg->hasProperty("name1"));
    assert (pReg->hasProperty("name2"));

    std::string dfl = pReg->getString("nonexistent", "default");
    assert (dfl == "default");

    AutoPtr<Poco::Util::AbstractConfiguration> pView = pReg->createView("config");
    dfl = pView->getString("sub.foo", "default");
    assert (dfl == "default");

    pView->setString("sub.foo", "bar");
    assert (pView->getString("sub.foo", "default") == "bar");

    std::string value;
    assert (pReg->convertToRegFormat("A.B.C", value) == "A\\B");
    assert (value == "C");

    Poco::Util::AbstractConfiguration::Keys keys;
    pReg->keys(keys);
    assert (keys.size() == 3);
    assert (std::find(keys.begin(), keys.end(), "name1") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "name2") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "config") != keys.end());

    pReg->keys("config", keys);
    assert (keys.size() == 1);
    assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());

    AutoPtr<WinRegistryConfiguration> pRootReg = new WinRegistryConfiguration("");

    assert (pRootReg->getInt("HKEY_CURRENT_USER.Software.Applied Informatics.Test.name1") == 1);

    pRootReg->keys(keys);
    assert (keys.size() == 6);
    assert (std::find(keys.begin(), keys.end(), "HKEY_CLASSES_ROOT") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "HKEY_CURRENT_CONFIG") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "HKEY_CURRENT_USER") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "HKEY_LOCAL_MACHINE") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "HKEY_PERFORMANCE_DATA") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "HKEY_USERS") != keys.end());

    pRootReg->keys("HKEY_CURRENT_USER.Software.Applied Informatics.Test", keys);
    assert (keys.size() == 3);
    assert (std::find(keys.begin(), keys.end(), "name1") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "name2") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "config") != keys.end());
}
示例#2
0
DictionaryPtr DictionaryFactory::create(const std::string & name, Poco::Util::AbstractConfiguration & config,
	const std::string & config_prefix, Context & context) const
{
	Poco::Util::AbstractConfiguration::Keys keys;
	const auto & layout_prefix = config_prefix + ".layout";
	config.keys(layout_prefix, keys);
	if (keys.size() != 1)
		throw Exception{name + ": element dictionary.layout should have exactly one child element",
			ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG};

	const DictionaryStructure dict_struct{config, config_prefix + ".structure"};

	auto source_ptr = DictionarySourceFactory::instance().create(
		name, config, config_prefix + ".source", dict_struct, context);

	const DictionaryLifetime dict_lifetime{config, config_prefix + ".lifetime"};

	const bool require_nonempty = config.getBool(config_prefix + ".require_nonempty", false);

	const auto & layout_type = keys.front();

	if ("range_hashed" == layout_type)
	{
		if (dict_struct.key)
			throw Exception{"'key' is not supported for dictionary of layout 'range_hashed'",
				ErrorCodes::UNSUPPORTED_METHOD};

		if (!dict_struct.range_min || !dict_struct.range_max)
			throw Exception{name + ": dictionary of layout 'range_hashed' requires .structure.range_min and .structure.range_max",
				ErrorCodes::BAD_ARGUMENTS};

		return std::make_unique<RangeHashedDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, require_nonempty);
	}
	else if ("complex_key_hashed" == layout_type)
	{
		if (!dict_struct.key)
			throw Exception{"'key' is required for dictionary of layout 'complex_key_hashed'",
				ErrorCodes::BAD_ARGUMENTS};

		return std::make_unique<ComplexKeyHashedDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, require_nonempty);
	}
	else if ("complex_key_cache" == layout_type)
	{
		if (!dict_struct.key)
			throw Exception{"'key' is required for dictionary of layout 'complex_key_hashed'",
				ErrorCodes::BAD_ARGUMENTS};

		const auto size = config.getInt(layout_prefix + ".complex_key_cache.size_in_cells");
		if (size == 0)
			throw Exception{name + ": dictionary of layout 'cache' cannot have 0 cells",
				ErrorCodes::TOO_SMALL_BUFFER_SIZE};

		if (require_nonempty)
			throw Exception{name + ": dictionary of layout 'cache' cannot have 'require_nonempty' attribute set",
				ErrorCodes::BAD_ARGUMENTS};

		return std::make_unique<ComplexKeyCacheDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, size);
	}
	else
	{
		if (dict_struct.key)
			throw Exception{"'key' is not supported for dictionary of layout '" + layout_type + "'",
				ErrorCodes::UNSUPPORTED_METHOD};

		if (dict_struct.range_min || dict_struct.range_max)
			throw Exception{name + ": elements .structure.range_min and .structure.range_max should be defined only "
					"for a dictionary of layout 'range_hashed'",
				ErrorCodes::BAD_ARGUMENTS};

		if ("flat" == layout_type)
		{
			return std::make_unique<FlatDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, require_nonempty);
		}
		else if ("hashed" == layout_type)
		{
			return std::make_unique<HashedDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, require_nonempty);
		}
		else if ("cache" == layout_type)
		{
			const auto size = config.getInt(layout_prefix + ".cache.size_in_cells");
			if (size == 0)
				throw Exception{name + ": dictionary of layout 'cache' cannot have 0 cells",
					ErrorCodes::TOO_SMALL_BUFFER_SIZE};

			if (require_nonempty)
				throw Exception{name + ": dictionary of layout 'cache' cannot have 'require_nonempty' attribute set",
					ErrorCodes::BAD_ARGUMENTS};

			return std::make_unique<CacheDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, size);
		}
	}

	throw Exception{name + ": unknown dictionary layout type: " + layout_type,
		ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG};
};