├── .gitignore ├── LICENSE ├── README.md ├── any ├── any.hpp ├── readme.md └── test.cpp ├── functinal_helper ├── bind.hpp ├── func_traits.hpp ├── readme.md └── test.cpp └── optional ├── optional.hpp ├── optional_test.cpp └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | #VS project files 2 | *.v12.suo 3 | *.vcxproj 4 | *.sdf 5 | *.vcxproj.filters 6 | *.sln 7 | *.vcxproj.user 8 | Debug 9 | Release -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 yufeng Z 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ATMPL 2 | 3 | **all library components write in c++11 without any other dependency.** 4 | 5 | at present, this ATMPL--Another Template Meta Programming Library has a little supported 6 | 7 | features as follow: 8 | 9 | ### functional_helper 10 | include function traits and a enhanced bind template class. 11 | 12 | ### Any 13 | 14 | Safe, generic container for single values of different value types. **similar to [boost.Any](http://www.boost.org/doc/libs/1_60_0/doc/html/any.html)** but c++11 version. 15 | 16 | ### Optional 17 | 18 | A value-semantic, type-safe wrapper for representing 'optional' (or 'nullable') objects of a given type. An optional object may or may not contain a value of the underlying type. [boost.Optional](http://www.boost.org/doc/libs/1_61_0/libs/optional/doc/html/index.html) C++11 version 19 | 20 | **all test on VS2015/GCC 5.3.0/Clang 3.8.0** 21 | 22 | -------------------------------------------------------------------------------- /any/any.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by bhzyf on 2016/5/2. 3 | // 4 | 5 | #ifndef CLIONSCIENCE_ANY_HPP 6 | #define CLIONSCIENCE_ANY_HPP 7 | #include 8 | #include 9 | 10 | namespace ATMPL 11 | { 12 | class any 13 | { 14 | private: 15 | class placeholder 16 | { 17 | public: // structors 18 | 19 | virtual ~placeholder() 20 | { 21 | } 22 | 23 | public: // queries 24 | 25 | virtual placeholder *clone() const = 0; 26 | 27 | }; 28 | 29 | template 30 | class holder : public placeholder 31 | { 32 | public: 33 | holder(const _ValueType &v) 34 | : _hold(v) 35 | { 36 | } 37 | 38 | placeholder *clone() const override 39 | { 40 | return new holder(_hold); 41 | } 42 | 43 | public: 44 | _ValueType _hold; 45 | }; 46 | 47 | public: 48 | any() : _holder(nullptr) 49 | { 50 | } 51 | 52 | template 53 | any(const _ValueType &v) 54 | : _holder(new holder::type>::type>(v)) 55 | { 56 | } 57 | 58 | // Perfect forwarding of _ValueType 59 | template 60 | any(_ValueType &&v 61 | , typename std::enable_if::type>::value, _ValueType>::type* = nullptr 62 | , typename std::enable_if::value>::type* = nullptr) 63 | :_holder(new holder< typename std::decay<_ValueType>::type >(std::forward<_ValueType>(v))) 64 | { 65 | } 66 | 67 | //copy constructor 68 | any(const any &other) 69 | : _holder(other._holder != nullptr ? other._holder->clone() : nullptr) 70 | { 71 | } 72 | 73 | //move constructor 74 | any(any && other) 75 | :_holder(other._holder) 76 | { 77 | other._holder = nullptr; 78 | } 79 | 80 | //assignment constructor 81 | any & operator=(const any& other_any) 82 | { 83 | _holder = other_any._holder->clone(); 84 | return *this; 85 | } 86 | 87 | //move assignment constructor 88 | any & operator=(any&& other_any) 89 | { 90 | _holder = other_any._holder; 91 | other_any._holder = nullptr; 92 | return *this; 93 | } 94 | 95 | ~any() 96 | { 97 | delete _holder; 98 | _holder = nullptr; 99 | } 100 | 101 | template 102 | static _ValueType cast(any &operand) 103 | { 104 | typedef typename std::remove_reference<_ValueType>::type nonref; 105 | nonref * op = cast(&operand); 106 | typedef typename std::conditional::value, _ValueType, 107 | typename std::add_lvalue_reference<_ValueType>::type>::type ref_type; 108 | return static_cast(*op); 109 | } 110 | 111 | private: 112 | template 113 | static _ValueType *cast(any *operand) 114 | { 115 | auto _holder = dynamic_cast *>(operand->_holder); 116 | if (_holder == nullptr) 117 | throw std::bad_cast(); 118 | return &_holder->_hold; 119 | } 120 | 121 | template 122 | static const _ValueType *cast(const any *operand) 123 | { 124 | return cast<_ValueType>(const_cast(operand)); 125 | } 126 | 127 | 128 | public: 129 | placeholder *_holder; 130 | }; 131 | 132 | 133 | } 134 | #endif //CLIONSCIENCE_ANY_HPP 135 | -------------------------------------------------------------------------------- /any/readme.md: -------------------------------------------------------------------------------- 1 | examples: 2 | 3 | ```c++ 4 | #include 5 | #include 6 | #include "any.hpp" 7 | #include 8 | using namespace ATMPL; 9 | 10 | template 11 | void pod_test(any &v) 12 | { 13 | std::cout << "any cast to " << typeid(_T).name() << " : " << any::cast<_T>(v) << std::endl; 14 | } 15 | 16 | any ret() 17 | { 18 | int i = 1; 19 | return any(i); 20 | } 21 | 22 | int main() 23 | { 24 | 25 | 26 | try 27 | { 28 | any v(short(1)); 29 | pod_test(v); 30 | 31 | any v_int(int(2)); 32 | pod_test(v_int); 33 | 34 | any v_long(long(3)); 35 | pod_test(v_long); 36 | 37 | any v_longlong((long long)(4)); 38 | pod_test(v_longlong); 39 | 40 | any v_char(char('5')); 41 | pod_test(v_char); 42 | 43 | char str[] = "hello world"; 44 | any v_cptr(str); 45 | pod_test(v_cptr); 46 | 47 | any v_string(std::string("HI! string any cast")); 48 | pod_test(v_string); 49 | 50 | any any_0; 51 | any any_1(ret()); 52 | pod_test(any_1); 53 | 54 | any any_2(any_1); 55 | pod_test(any_2); 56 | 57 | any any_3 = ret(); 58 | pod_test(any_3); 59 | 60 | any any_4 = any_1; 61 | pod_test(any_4); 62 | 63 | any any_5 = any(int(5)); 64 | pod_test(any_5); 65 | 66 | std::vector any_vec; 67 | any_vec.push_back('A'); 68 | any_vec.push_back(short(1)); 69 | any_vec.push_back(int(2)); 70 | any_vec.push_back(str); 71 | any_vec.push_back(std::string("hello any,world")); 72 | pod_test(any_vec[0]); 73 | pod_test(any_vec[1]); 74 | pod_test(any_vec[2]); 75 | pod_test(any_vec[3]); 76 | pod_test(any_vec[4]); 77 | 78 | printf("test passed\n"); 79 | } 80 | catch (std::bad_cast&e) 81 | { 82 | std::cout << e.what()<<" occured when use any\n"; 83 | } 84 | 85 | 86 | } 87 | ``` -------------------------------------------------------------------------------- /any/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "any.hpp" 4 | #include 5 | using namespace ATMPL; 6 | 7 | template 8 | void pod_test(any &v) 9 | { 10 | std::cout << "any cast to " << typeid(_T).name() << " : " << any::cast<_T>(v) << std::endl; 11 | } 12 | 13 | any ret() 14 | { 15 | int i = 1; 16 | return any(i); 17 | } 18 | 19 | int main() 20 | { 21 | 22 | 23 | try 24 | { 25 | any v(short(1)); 26 | pod_test(v); 27 | 28 | any v_int(int(2)); 29 | pod_test(v_int); 30 | 31 | any v_long(long(3)); 32 | pod_test(v_long); 33 | 34 | any v_longlong((long long)(4)); 35 | pod_test(v_longlong); 36 | 37 | any v_char(char('5')); 38 | pod_test(v_char); 39 | 40 | char str[] = "hello world"; 41 | any v_cptr(str); 42 | pod_test(v_cptr); 43 | 44 | any v_string(std::string("HI! string any cast")); 45 | pod_test(v_string); 46 | 47 | any any_0; 48 | any any_1(ret()); 49 | pod_test(any_1); 50 | 51 | any any_2(any_1); 52 | pod_test(any_2); 53 | 54 | any any_3 = ret(); 55 | pod_test(any_3); 56 | 57 | any any_4 = any_1; 58 | pod_test(any_4); 59 | 60 | any any_5 = any(int(5)); 61 | pod_test(any_5); 62 | 63 | std::vector any_vec; 64 | any_vec.push_back('A'); 65 | any_vec.push_back(short(1)); 66 | any_vec.push_back(int(2)); 67 | any_vec.push_back(str); 68 | any_vec.push_back(std::string("hello any,world")); 69 | pod_test(any_vec[0]); 70 | pod_test(any_vec[1]); 71 | pod_test(any_vec[2]); 72 | pod_test(any_vec[3]); 73 | pod_test(any_vec[4]); 74 | 75 | printf("test passed\n"); 76 | } 77 | catch (std::bad_cast&e) 78 | { 79 | std::cout << e.what()<<" occured when use any\n"; 80 | } 81 | 82 | 83 | } -------------------------------------------------------------------------------- /functinal_helper/bind.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "func_traits.hpp" 5 | namespace ATMPL 6 | { 7 | 8 | 9 | namespace functional_helper 10 | { 11 | template 12 | struct bind_holder; 13 | 14 | template 15 | struct is_bind_holder 16 | :public std::false_type 17 | {}; 18 | template 19 | struct is_bind_holder> 20 | :public std::true_type 21 | {}; 22 | 23 | template 24 | struct is_bind_holder> 25 | :public std::true_type 26 | {}; 27 | 28 | template 29 | struct is_bind_holder> 30 | :public std::true_type 31 | {}; 32 | 33 | template 34 | struct is_bind_holder> 35 | :public std::true_type 36 | {}; 37 | 38 | 39 | 40 | template 41 | struct bind_holder_stripper_helper; 42 | 43 | template 44 | struct bind_holder_stripper_helper 45 | : public func_traits 46 | {}; 47 | 48 | template 49 | struct bind_holder_stripper_helper 50 | :public bind_holder_stripper_helper::value> 51 | {}; 52 | 53 | template 54 | struct bind_holder_stripper 55 | :public bind_holder_stripper_helper::value> 56 | {}; 57 | 58 | template 59 | struct bind_holder 60 | { 61 | private: 62 | BindExpr _bind_expr; 63 | public: 64 | 65 | typedef F bind_type; 66 | typedef bind_holder_stripper underlying_type; 67 | typedef std::integral_constant bind_arity; 68 | explicit bind_holder(BindExpr&& _functor) 69 | :_bind_expr(std::move(std::forward(_functor))) 70 | { 71 | } 72 | 73 | template 74 | struct bind_arg 75 | { 76 | static_assert(N < bind_arity::value, "error: invalid parameter index."); 77 | using type = typename std::tuple_element>::type; 78 | }; 79 | 80 | template 81 | auto operator()(_Args&&...params) 82 | ->decltype(this->_bind_expr(std::forward<_Args>(params)...)) 83 | { 84 | return this->_bind_expr(std::forward<_Args>(params)...); 85 | } 86 | }; 87 | 88 | template 89 | struct remove_cvr 90 | { 91 | using type = typename std::remove_cv::type>::type; 92 | }; 93 | 94 | template 95 | auto bind(F&& f, Args&&... args) 96 | ->decltype((bind_holder(f), std::forward(args)...)), 97 | typename remove_cvr::type, Args...> 98 | (std::bind(std::forward(f), std::forward(args)...)))) 99 | { 100 | return (bind_holder(f), std::forward(args)...)), 101 | typename remove_cvr::type, Args...> 102 | (std::bind(std::forward(f), std::forward(args)...))); 103 | } 104 | } 105 | 106 | } -------------------------------------------------------------------------------- /functinal_helper/func_traits.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace ATMPL 6 | { 7 | 8 | namespace functional_helper 9 | { 10 | template 11 | struct func_traits; 12 | 13 | //function 14 | template 15 | struct func_traits<_Ret(_Args...)> 16 | { 17 | typedef _Ret ret; 18 | typedef std::integral_constant arity; 19 | template 20 | struct arg 21 | { 22 | static_assert(N < arity::value, "invalid parameter index"); 23 | using type = typename std::tuple_element>::type; 24 | }; 25 | }; 26 | 27 | //function pointer 28 | template 29 | struct func_traits<_Ret(*)(_Args...)> :public func_traits<_Ret(_Args...)> 30 | { 31 | }; 32 | 33 | //member function pointer 34 | template 35 | struct func_traits<_Ret(_Class::*)(_Args...)> :public func_traits<_Ret(_Class&, _Args...)> 36 | { 37 | }; 38 | 39 | template 40 | struct func_traits<_Ret(_Class::*)(_Args...) const> :public func_traits<_Ret(_Class&, _Args...)> 41 | { 42 | }; 43 | 44 | 45 | //functor and lambda 46 | template 47 | struct func_traits : public func_traits 48 | { 49 | }; 50 | 51 | } 52 | 53 | 54 | } -------------------------------------------------------------------------------- /functinal_helper/readme.md: -------------------------------------------------------------------------------- 1 | ### functional_helper 2 | 3 | include function traits and a enhanced bind template class. 4 | 5 | ##### function_traits 6 | 7 | it provide a template class named function_traits