<commit_before>/* <x0/CustomDataMgr.h>
 *
 * This file is part of the x0 web server project and is released under AGPL-3.
 * http://www.xzero.io/
 *
 * (c) 2009-2014 <NAME> <<EMAIL>>
 */

#ifndef sw_x0_CustomDataMgr_h
#define sw_x0_CustomDataMgr_h (1)

#include <x0/Types.h>
#include <unordered_map>
#include <memory>
#include <cassert>

namespace x0 {

struct X0_API CustomData
{
	CustomData(const CustomData&) = delete;
	CustomData& operator=(const CustomData&) = delete;
	CustomData() = default;
	virtual ~CustomData() {}
};

#define CUSTOMDATA_API_INLINE                                            \
private:                                                                 \
	std::unordered_map<const void *, std::unique_ptr<x0::CustomData>> customData_; \
public:                                                                  \
	void clearCustomData()                                               \
	{                                                                    \
		customData_.clear();                                             \
	}                                                                    \
                                                                         \
	void clearCustomData(void *key)                                      \
	{                                                                    \
		auto i = customData_.find(key);                                  \
		if (i != customData_.end()) {                                    \
			customData_.erase(i);                                        \
		}                                                                \
	}                                                                    \
                                                                         \
	x0::CustomData* customData(const void* key) const                    \
	{                                                                    \
		auto i = customData_.find(key);                                  \
		return i != customData_.end() ? i->second.get() : nullptr;       \
	}                                                                    \
                                                                         \
	template<typename T>                                                 \
	T* customData(const void* key) const                                 \
	{                                                                    \
		auto i = customData_.find(key);                                  \
		return i != customData_.end()                                    \
			? static_cast<T*>(i->second.get())                           \
			: nullptr;                                                   \
	}                                                                    \
                                                                         \
	template<typename T, typename... Args>                               \
	T* setCustomData(const void *key, Args&&... args)                    \
	{                                                                    \
		auto i = customData_.find(key);                                  \
		if (i != customData_.end())                                      \
			return static_cast<T*>(i->second.get());                     \
                                                                         \
		T* value = new T(args...);                                       \<commit_msg>[base] CustomDataMgr: adds overload to setCustomData() to allow assigning pre-allocated values<commit_after>/* <x0/CustomDataMgr.h>
 *
 * This file is part of the x0 web server project and is released under AGPL-3.
 * http://www.xzero.io/
 *
 * (c) 2009-2014 <NAME> <<EMAIL>>
 */

#ifndef sw_x0_CustomDataMgr_h
#define sw_x0_CustomDataMgr_h (1)

#include <x0/Types.h>
#include <unordered_map>
#include <memory>
#include <cassert>

namespace x0 {

struct X0_API CustomData
{
	CustomData(const CustomData&) = delete;
	CustomData& operator=(const CustomData&) = delete;
	CustomData() = default;
	virtual ~CustomData() {}
};

#define CUSTOMDATA_API_INLINE                                            \
private:                                                                 \
	std::unordered_map<const void *, std::unique_ptr<x0::CustomData>> customData_; \
public:                                                                  \
	void clearCustomData()                                               \
	{                                                                    \
		customData_.clear();                                             \
	}                                                                    \
                                                                         \
	void clearCustomData(void *key)                                      \
	{                                                                    \
		auto i = customData_.find(key);                                  \
		if (i != customData_.end()) {                                    \
			customData_.erase(i);                                        \
		}                                                                \
	}                                                                    \
                                                                         \
	x0::CustomData* customData(const void* key) const                    \
	{                                                                    \
		auto i = customData_.find(key);                                  \
		return i != customData_.end() ? i->second.get() : nullptr;       \
	}                                                                    \
                                                                         \
	template<typename T>                                                 \
	T* customData(const void* key) const                                 \
	{                                                                    \
		auto i = customData_.find(key);                                  \
		return i != customData_.end()                                    \
			? static_cast<T*>(i->second.get())                           \
			: nullptr;                                                   \
	}                                                                    \
                                                                         \
    x0::CustomData* setCustomData(const void* key,                       \
                              std::unique_ptr<x0::CustomData>&& value)   \
    {                                                                    \
        customData_[key] = std::move(value);                             \
        return value.get();                                              \
    }                                                                    \
                                                                         \
	template<typename T, typename... Args>                               \
	T* setCustomData(const void *key, Args&&... args)                    \
	{                                                                    \
		auto i = customData_.find(key);                                  \
		if (i != customData_.end())                                      \
			return static_cast<T*>(i->second.get());                     \
                                                                         \
		T* value = new T(args...);                                       \
{'fraction_non_alphanumeric': 0.0781081081081081, 'fraction_numerical': 0.005405405405405406, 'mean_word_length': 0.5159770585825482, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 121, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5771596', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1298, 'n_words': 264}