allegro_util/
lib.rs

1// Copyright (c) 2014 by SiegeLord
2//
3// All rights reserved. Distributed under ZLib. For full terms see the file LICENSE.
4
5use libc::c_char;
6use std::ffi::CStr;
7
8#[allow(non_camel_case_types)]
9pub type c_bool = u8;
10
11pub trait Flag
12{
13	fn zero() -> Self;
14}
15
16pub unsafe fn from_c_str(c_str: *const c_char) -> String
17{
18	unsafe { String::from_utf8_lossy(CStr::from_ptr(c_str as *const _).to_bytes()).into_owned() }
19}
20
21#[macro_export]
22macro_rules! if_ok {
23	($e: expr) => {
24		if ($e).is_err()
25		{
26			return Err(());
27		}
28	};
29}
30
31#[macro_export]
32macro_rules! opaque {
33	($f: ident) => {
34		/* Mimicking c_void */
35		#[allow(missing_copy_implementations)]
36		pub enum $f {}
37	};
38}
39
40#[macro_export]
41macro_rules! derive_copy_clone {
42	($t: ty) => {
43		impl Copy for $t {}
44		impl Clone for $t
45		{
46			fn clone(&self) -> Self
47			{
48				*self
49			}
50		}
51	};
52}
53
54#[macro_export]
55macro_rules! flag_type
56{
57	($f: ident { $($n: ident = $v: expr),*}) =>
58	{
59		#[derive(Copy, Clone, Debug)]
60		pub struct $f
61		{
62			bits: u32
63		}
64
65		impl $f
66		{
67			#[inline]
68			pub fn get(&self) -> u32
69			{
70				self.bits
71			}
72		}
73
74		impl Flag for $f
75		{
76			fn zero() -> $f
77			{
78				$f{bits: 0}
79			}
80		}
81
82		impl ::std::ops::BitOr for $f
83		{
84			type Output = $f;
85			fn bitor(self, e: $f) -> $f
86			{
87				$f{bits: self.bits | e.bits}
88			}
89		}
90
91		impl ::std::ops::BitAnd for $f
92		{
93			type Output = bool;
94			fn bitand(self, e: $f) -> bool
95			{
96				self.bits & e.bits != 0
97			}
98		}
99
100		$(
101			pub const $n: $f = $f{bits: $v};
102		)+
103	}
104}
105
106#[macro_export]
107macro_rules! flags
108{
109	($f: ident { $($n: ident = $v: expr),*}) =>
110	{
111		$(
112			pub const $n: $f = $f{bits: $v};
113		)+
114	}
115}
116
117#[macro_export]
118macro_rules! cast_to_c {
119	($p:ident, f32) => {
120		$p as c_float
121	};
122	($p:ident, Color) => {
123		*$p
124	};
125}