<commit_before>/*
 * para.hpp
 *
 *  Created on: May 18, 2013
 *      Author: lenx
 */

#ifndef PROTON_PARA_HPP_
#define PROTON_PARA_HPP_

#include <proton/base.hpp>
#include <proton/ref.hpp>

namespace proton{


template<typename objT>
class para_ {
private:
	constexpr static long id_shift=sizeof(long)/2;
	constexpr static long offset_mask =long((unsigned long)(-1)>>id_shift);

	long _id_and_offset; // lowest half is offset, highest half is id
	objT* _p;

	long compute_id_offset(long id, long offset)
	{
		// [ATTENTION!] offset should be less than 65536 on 32bit!
		return (id<<id_shift)+(offset & offset_mask);
	}

	para_(const long* id, objT* p)
		:_id_and_offset(compute_id_offset(*id,(char*)p-(char*)id)),
		 _p(p)
	{}

public:

	template<A,Tr>
	para_(const ref_<objT,A,Tr>& r):para_(r.__rp()->id(), r.__p())
	{}

	bool __validate()
	{
		long offset=_id_and_offset & offset_mask;
		long* id=(long*)((char*)p-offset);
		return ((*id) & offset_mask) == (_id_and_offset >> id_shift);
	}

	~para_()
	{
		PROTON_THROW_IF(!__validate(), "para_ validation failed. please use ref_ instead.")
	}

    /** operator-> points to the object refered.
     */
    objT* operator->()
    {
#ifdef PROTON_DEBUG_PARA
		PROTON_THROW_IF(!__validate(), "para_ validation failed. please use ref_ instead.")
#endif
        return _p;
    }

    /** operator-> points to the object refered.
     */
    const objT* operator->()const
    {
#ifdef PROTON_DEBUG_PARA
		PROTON_THROW_IF(!__validate(), "para_ validation failed. please use ref_ instead.")
#endif
        return _p;
    }
};

} // ns proton


#endif /* PROTON_PARA_HPP_ */<commit_msg>weak based on new wrefc_t<commit_after>/*
 * weak.hpp
 *
 *  Created on: May 18, 2013
 *      Author: lenx
 */

#ifndef PROTON_WEAK_HPP_
#define PROTON_WEAK_HPP_

#include <proton/base.hpp>
#include <proton/ref.hpp>

namespace proton{


template<typename refT>
class weak_ {
public:
	typedef typename refT::refc_t refc_t;
	typedef typename refT::obj_t obj_t;
	typedef typename refT::alloc_t alloc_t;

private:

	refc_t* _w;
	obj_t* _p;

public:

	weak_(const refT& r):_w(r._rp), _p(r._p)
	{
		if(_w)
			_w->weak_enter();
	}

	~weak_()
	{
		if(_w){
			if(!_w->weak_release()){
				if(!_w->count()){
	            	alloc_t::confiscate(_w);
				}
			}
			_w=NULL;
		}
	}

	refT operator*()const
	{
		if(_w && _w->count()){
			// [FIXME] TOCTTOU might happen here in multi-threading
			return refT(alloc_inner, _w, _p);
		}
		else
			return none;
	}

	bool operator<(const weak_& w)const
	{
		return _w < w._w;
	}

	bool operator==(const weak_& w)const
	{
		return _w == w._w;
	}

	//[TODO] unordered_map & unordered_set support
};

template<typename refT>
weak_<refT> weak(const refT& t)
{
	return weak_<refT>(t);
}

} // ns proton


#endif /* PROTON_WEAK_HPP_ */
{'fraction_non_alphanumeric': 0.14838709677419354, 'fraction_numerical': 0.007526881720430108, 'mean_word_length': 3.560457516339869, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7113987', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1102, 'n_words': 250}