Skip to content

Commit

Permalink
mpl
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrono Law committed Feb 26, 2016
1 parent 8d7d32d commit 5fbac1f
Show file tree
Hide file tree
Showing 10 changed files with 912 additions and 0 deletions.
149 changes: 149 additions & 0 deletions mpl/algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright (c) 2016
// Author: Chrono Law
#include <std.hpp>
//using namespace std;

#include <boost/mpl/for_each.hpp>
#include <boost/mpl/tag.hpp>
#include <boost/type_index.hpp>

#include <boost/core/ignore_unused.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/string.hpp>
#include <boost/mpl/copy.hpp>

using namespace boost;
using namespace boost::mpl;

///////////////////////////////////////

#include <boost/mpl/back_inserter.hpp>
#include <boost/mpl/front_inserter.hpp>
#include <boost/mpl/copy.hpp>

void case1()
{
typedef mpl::vector<char, int , long> vec;

typedef copy<vec,
mpl::back_inserter<vec>>::type vec2;
assert((size<vec2>::value == 6));

typedef mpl::string<> str1;
typedef copy<
mpl::string<'time'>,
mpl::front_inserter<str1>>::type str2;
std::cout << c_str<str2>::value << std::endl;

}

///////////////////////////////////////

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/count.hpp>
#include <boost/mpl/contains.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/size.hpp>

void case2()
{
typedef range_c<int, 0, 10> rc;
assert((size<rc>::value == 10));

#define INTV(n) integral_c<rc::value_type, n>

assert((deref<
find<rc, INTV(0)>::type
>::type::value == 0));

assert((contains<rc, INTV(9)>::value));
assert((!contains<rc, INTV(10)>::value));

assert((count<rc, INTV(5)>::value == 1));
assert((count<rc, INTV(-5)>::value == 0));

typedef vector_c<int, 0,1,2,3,4,5,6,7,8,9> vec;
assert((equal<rc, vec>::value));
assert((!is_same<rc, vec>::value));

}

///////////////////////////////////////
#include <boost/mpl/reverse.hpp>
#include <boost/mpl/replace.hpp>
#include <boost/mpl/remove.hpp>

void case3()
{
typedef mpl::vector<char, short, int, long> vec;
typedef replace<vec,
int, char>::type vec2;

assert((count<vec2, char>::value == 2));
assert((is_same<deref<
advance<begin<vec2>::type, int_<2>>::type>::type,
char>::value));

typedef mpl::remove<vec2, short>::type vec3;
assert((!contains<vec3, short>::value));

assert((equal<
reverse<vec3>::type,
mpl::vector<long, char, char>
>::value));

}

///////////////////////////////////////

struct mpl_func1
{
template<typename T>
void operator()(T t)
{ std::cout << typeindex::type_id<T>().pretty_name() << std::endl; }
//{ std::cout << typeid(t).name() << std::endl; }
};

struct mpl_func2
{
template<typename T>
void operator()(T t)
{
if (is_same<typename tag<T>::type, integral_c_tag>::value)
{ std::cout << t << ',';}
}
};

void case4()
{
typedef range_c<int, 0, 5> rc;

typedef mpl::vector<> vec;

typedef mpl::copy<rc,
mpl::back_inserter<vec>>::type vec2;

typedef push_front<vec2, float>::type vec3;

mpl::for_each<vec3>(mpl_func1());

std::cout << std::endl;

mpl::for_each<vec3>(mpl_func2());
}


///////////////////////////////////////

int main()
{
std::cout << "hello container" << std::endl;

case1();
case2();
case3();
case4();
}
94 changes: 94 additions & 0 deletions mpl/assert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) 2016
// Author: Chrono Law
#include <std.hpp>
//using namespace std;

#include <boost/core/ignore_unused.hpp>
#include <boost/type_traits.hpp>

#include <boost/mpl/vector.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/equal_to.hpp>

using namespace boost;
using namespace boost::mpl;

///////////////////////////////////////

#include <boost/mpl/assert.hpp>

void case1()
{
typedef mpl::vector<int, char> vec;

BOOST_MPL_ASSERT((is_same<int,
front<vec>::type> ));

//BOOST_MPL_ASSERT((equal_to<
// size<vec>::type, int_<3>>));

}

///////////////////////////////////////
template<typename T>
struct my_operation
{
BOOST_MPL_ASSERT_NOT((is_integral<T>));
BOOST_MPL_ASSERT((
is_integral<typename T::value_type>));

typedef typename next<T>::type type;
};

void case2()
{
typedef my_operation<int_<3>>::type t;
//typedef my_operation<int>::type error;

boost::ignore_unused<t>();

BOOST_MPL_ASSERT_RELATION(int_<5>::value, >, 0);
//BOOST_MPL_ASSERT_RELATION(sizeof(int), >, sizeof(long));
}


///////////////////////////////////////

template<typename T>
struct my_operation2
{
BOOST_MPL_ASSERT_MSG(!is_pod<T>::value,
IS_POD_ERROR, (T));
};

void case3()
{
//BOOST_MPL_ASSERT_MSG(1>2, DEMO_MESSAGE, ());
//my_operation2<int>::type;
}

///////////////////////////////////////
#include <boost/mpl/print.hpp>
#include <boost/mpl/transform.hpp>

void case4()
{
typedef mpl::print<int>::type x;
typedef mpl::vector<char, int_<5>, float> vec;
typedef transform<vec, print<_>>::type none;

boost::ignore_unused<x, none>();
}

///////////////////////////////////////

int main()
{
std::cout << "hello debug" << std::endl;

case1();
case2();
case3();
//case4();
}
133 changes: 133 additions & 0 deletions mpl/container.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright (c) 2016
// Author: Chrono Law
#include <std.hpp>
//using namespace std;

#include <boost/core/ignore_unused.hpp>
#include <boost/mpl/int.hpp>

#include <boost/mpl/vector.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/back.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/mpl/clear.hpp>

using namespace boost;
using namespace boost::mpl;

///////////////////////////////////////

void case1()
{
typedef mpl::vector<char, int , long,
int_<8>, std::string> vec;

assert((empty<vec>::value == false));
assert((size<vec>::value == 5));

assert((is_same<front<vec>::type, char>::value));
assert((is_same<back<vec>::type, std::string>::value));
assert((is_same<at_c<vec, 1>::type, int>::value));

typedef push_front<vec, float>::type vec2;
assert((size<vec2>::value == 6));
assert((is_same<front<vec2>::type, float>::value));

typedef clear<vec2>::type vec3;
assert((empty<vec3>::value));

}

///////////////////////////////////////
#define BOOST_MPL_LIMIT_STRING_SIZE 24
#include <boost/mpl/string.hpp>

void case2()
{
typedef mpl::string<'a','b','c'> abc;
typedef mpl::string<'Hell','o !!'> hello;
//typedef mpl::string<'Hello !!'> hello2;
//typedef mpl::string<"Hello !!"> hello3;

boost::ignore_unused<abc>();

assert((empty<hello>::value == false));
assert((size<hello>::value == 8));

assert((front<hello>::type::value == 'H'));
assert((back<hello>::type::value == '!'));

typedef push_back<hello, char_<'?'>>::type hello2;
assert((c_str<hello2>::value ==
std::string("Hello !!?") ));

}

///////////////////////////////////////
#include <boost/mpl/map.hpp>
#include <boost/mpl/count.hpp>

void case3()
{
typedef mpl::map<
mpl::pair<int, std::vector<int>>,
mpl::pair<char,std::string>,
mpl::pair<int_<3>, float[3]>
> m;
assert((empty<m>::value == false));
assert((size<m>::value == 3));

typedef front<m>::type p1;
assert((is_same<first<p1>::type, int>::value));
assert((is_same<p1::second,std::vector<int>>::value));

assert((has_key<m, char>::value));
assert((count<m, int_<0>>::value == 0));

typedef at<m, char>::type mdata;
assert((is_same<mdata, std::string>::value));

}

///////////////////////////////////////
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/advance.hpp>
#include <boost/mpl/distance.hpp>

void case4()
{
typedef mpl::vector<char, int , long,
int_<8>, std::string> vec;

typedef begin<vec>::type i1;
assert((is_same<deref<i1>::type, char>::value));

typedef next<i1>::type i2;
assert((is_same<deref<i2>::type, int>::value));
assert((is_same<i1, prior<i2>::type>::value));

typedef advance<i2, int_<4>>::type i3;
assert((distance<i1, i3>::type::value == 5));
assert((distance<i3, i1>::type::value == -5));
assert((is_same<i3, mpl::end<vec>::type>::value));

typedef insert<vec, i2, float>::type vec2;

boost::ignore_unused<vec2>();
}
///////////////////////////////////////

int main()
{
std::cout << "hello container" << std::endl;

case1();
case2();
case3();
case4();
}
Loading

0 comments on commit 5fbac1f

Please sign in to comment.