Last active
August 29, 2015 14:04
-
-
Save 2sin18/6268d788231e4ca586cb to your computer and use it in GitHub Desktop.
Utilities to use templates in C++ with less pain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////////////////////// | |
/* | |
Copyright (c) 2014 Sam Yuen | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or altertantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
//////////////////////////////////////////////////////////////////////////////// | |
= `cplus.hh` | |
This file consists of utilities make C++ simpler. | |
//////////////////////////////////////////////////////////////////////////////// | |
*/ | |
#ifndef HACTAR_CPLUS_HH | |
#define HACTAR_CPLUS_HH | |
#include <stdlib.h> | |
#include <stdint.h> | |
// Put it into private section of a class to disable implicit casts. | |
#define CPLUS_BAD_CAST(T) \ | |
T(const T&) | |
// Put it into private section of a class to disable assigns. | |
#define CPLUS_BAD_ASSIGN(T) \ | |
T& operator=(const T&) | |
// It could be used to check if a type is complete at compile time | |
#define CPLUS_ASSERT_COMPLETE_TYPE(T) \ | |
typedef char _##T##_COMPLETE[sizeof(T) ? 1 : -1]; \ | |
enum { _ASSERT_##T##_COMPLETE = \ | |
sizeof(_##T##_COMPLETE) } | |
// It could be used to check if a type extends another type at compile time. | |
#define CPLUS_ASSERT_TYPE_EXTENDS(T, BASE) \ | |
static int _##T##_EXTENDS_##BASE(BASE*); \ | |
static void _##T##_EXTENDS_##BASE(...); \ | |
enum { _ASSERT_##T##_EXTENDS_##BASE = \ | |
sizeof(_##T##_EXTENDS_##BASE((T*) NULL)) } | |
// It could be used to check if a static item exists at compile time. | |
// Take an example, `CPLUS_ASSERT_EXISTS(foo, &T::foo, void (T::*) ())` checks | |
// if `T` has a method called `foo` with specified type; | |
// Another example, `CPLUS_ASSERT_EXISTS(bar, &T::bar, long*)` checks if `T` | |
// has a static member `bar` with `long` type. | |
#define CPLUS_ASSERT_EXISTS(ID, STATIC_PTR, PTR_TYPE) \ | |
template<PTR_TYPE> class _##ID##_HOLDER {}; \ | |
static int _##ID##_EXISTS(_##ID##_HOLDER<STATIC_PTR>*); \ | |
static void _##ID##_EXISTS(...); \ | |
enum { _ASSERT_##ID##_EXISTS = sizeof(_##ID##_EXISTS(NULL)) } | |
#endif | |
//////////////////////////////////////////////////////////////////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment