Sha256: 0ed4150337697d089e7e0e0a7e8812771bc9ae04217fa331228116873ab2c8b5

Contents?: true

Size: 1.97 KB

Versions: 24

Compression:

Stored size: 1.97 KB

Contents

// -*- c++ -*-
#pragma once
#ifndef __XOT_UTIL_H__
#define __XOT_UTIL_H__


#include <stdint.h>
#include <math.h>
#include <assert.h>
#include <xot/defs.h>
#include <xot/time.h>


namespace Xot
{


	int bit2byte (int bits);

	int byte2bit (int bytes);


	void seed (uint value = (uint) (time() * 1000));

	double random (double max_ = 1);

	double random (double min_, double max_);


	template <typename T = uint>
	inline constexpr T
	bit (int nth, T base = 0x1)
	{
		return (T) (base << nth);
	}

	template <typename T>
	inline constexpr T
	clip (T minval, T maxval, T value)
	{
		return value > maxval ? maxval : (value < minval ? minval : value);
	}


	inline constexpr double
	deg2rad (double degree)
	{
		return degree / 180.0 * M_PI;
	}

	inline constexpr double
	rad2deg (double radian)
	{
		return radian / M_PI * 180.0;
	}


	template <typename T>
	inline void
	add_flag (T* pvalue, uint flag)
	{
		assert(pvalue);

		*pvalue |= flag;
	}

	template <typename T>
	inline void
	remove_flag (T* pvalue, uint flag)
	{
		assert(pvalue);

		*pvalue &= ~flag;
	}

	template <typename T>
	inline bool
	has_flag (T value, uint flag)
	{
		if (flag == 0) return false;
		return (value & flag) == flag;
	}

	template <typename T>
	inline bool
	check_and_remove_flag (T* pvalue, uint flag)
	{
		assert(pvalue);

		bool has = has_flag(*pvalue, flag);
		remove_flag(pvalue, flag);
		return has;
	}


	static const uintptr_t POINTER_FLAG = 0x1;

	template <typename T>
	inline T*
	set_pointer_flag (T* pointer, bool flag = true)
	{
		uintptr_t intval = *(uintptr_t*) &pointer;
		if (flag)
			intval |= POINTER_FLAG;
		else
			intval &= ~POINTER_FLAG;

		return *(T**) &intval;
	}

	template <typename T>
	inline const T*
	set_pointer_flag (const T* pointer, bool flag = true)
	{
		return set_pointer_flag(const_cast<T*>(pointer), flag);
	}

	template <typename T>
	inline bool
	get_pointer_flag (const T* pointer)
	{
		const uintptr_t& intval = *(uintptr_t*) &pointer;
		return intval & POINTER_FLAG;
	}


}// Xot


#endif//EOH

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
xot-0.1.38 include/xot/util.h
xot-0.1.37 include/xot/util.h
xot-0.1.36 include/xot/util.h
xot-0.1.35 include/xot/util.h
xot-0.1.34 include/xot/util.h
xot-0.1.33 include/xot/util.h
xot-0.1.32 include/xot/util.h
xot-0.1.31 include/xot/util.h
xot-0.1.30 include/xot/util.h
xot-0.1.29 include/xot/util.h
xot-0.1.28 include/xot/util.h
xot-0.1.27 include/xot/util.h
xot-0.1.26 include/xot/util.h
xot-0.1.25 include/xot/util.h
xot-0.1.24 include/xot/util.h
xot-0.1.23 include/xot/util.h
xot-0.1.22 include/xot/util.h
xot-0.1.21 include/xot/util.h
xot-0.1.20 include/xot/util.h
xot-0.1.19 include/xot/util.h