Sunday, February 26, 2017

boost mpl if demonstration

Consider the following code adapted from Polukhin:

When run:

detail::pre_inc_functor::operator ()
x = 6
detail::plus_assignable_functor::operator ()
cls::operator +=
y = 90
detail::post_inc_functor::operator ()
post::operator ++
z = 141
post::operator ++
alpha = 141
z = 142

Saturday, February 25, 2017

boost mpl manipulating a vector

Consider the following code copied from Polukhin:


When run, this code simply prints "Hello World! 78"

Thursday, February 23, 2017

Luabind tostring demonstration

Consider the following code:

When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/tostring$ cat test.lua
package.loadlib('./testclass.so', 'init')()
a = testclass(101)
print(tostring(a))
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/tostring$ cat commands.bash
#!/bin/bash
g++ testclass.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o testclass.so testclass.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/tostring$ ./commands.bash
101
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/tostring$

Luabind operator other() demonstration

Consider the following code:


When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator_other$ cat test.lua
package.loadlib('./testclass.so', 'init')()
a = testclass(5)
b = testclass(9)
c = a + b
a:print()
b:print()
c:print()
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator_other$ cat commands.bash
#!/bin/bash
g++ testclass.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o testclass.so testclass.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator_other$ ./commands.bash
5
9
14
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator_other$

Luabind operator demonstration

Consider the following code:


When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator$ cat test.lua
package.loadlib('./testclass.so', 'init')()
a = testclass(5)
a:print()
c = a + 6
c:print()
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator$ cat commands.bash 
#!/bin/bash
g++ testclass.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o testclass.so testclass.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator$ ./commands.bash 
5
11
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/operator$ 

Luabind: enums demonstration

Consider the following code:

When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/enums$ cat test.lua 
package.loadlib('./testclass.so', 'init')()
print(A.first)
print(A.second)
print(A.third)
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/enums$ cat ./commands.bash 
#!/bin/bash
g++ testclass.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o testclass.so testclass.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/enums$ ./commands.bash 
1
2
3
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/enums$ 

You actually don't have to define the enum in the C++ struct A. Try removing it and the test.lua will still work.

Saturday, February 18, 2017

boost mpl vector demonstration

Consider the following code copied from Polukhin's book:


When run, this code prints nothing. All checking is done at compile time.

boost enable_if_c example

Consider the following code adapted from Polukhin's book:


When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/cpp/boost/polukhin/ch04/enable_if_c$ g++ -I~/dkuyu/bin/boost_1_60_0 c.cpp && ./a.out
double data_processor<T, typename boost::enable_if_c<boost::is_integral<T>::value>::type>::process(const T&, const T&, const T&) [with T = int; typename boost::enable_if_c<boost::is_integral<T>::value>::type = void]
double data_processor<T, typename boost::enable_if_c<boost::is_integral<T>::value>::type>::process(const T&, const T&, const T&) [with T = short int; typename boost::enable_if_c<boost::is_integral<T>::value>::type = void]
double data_processor<T, typename boost::enable_if_c<boost::is_float<T>::value>::type>::process(const T&, const T&, const T&) [with T = double; typename boost::enable_if_c<boost::is_float<T>::value>::type = void]
double data_processor<T, typename boost::enable_if_c<boost::is_float<T>::value>::type>::process(const T&, const T&, const T&) [with T = float; typename boost::enable_if_c<boost::is_float<T>::value>::type = void]
double data_processor<T, enable>::process(const T&, const T&, const T&) [with T = const char*; enable = void]

Thursday, February 9, 2017

Luabind: properties demonstration

Consider the following code:

When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/properties$ cat commands.bash 
#!/bin/bash
g++ testclass.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o testclass.so testclass.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/properties$ ./commands.bash 
7
89
5
lua: property 'b' is read only
stack traceback:
[C]: in function '__newindex'
test.lua:7: in main chunk
[C]: in ?
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/properties$ 

Luabind: Binding an overloaded class function

Consider the following code:


When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/overloaded_function$ cat test.lua 
package.loadlib('./overloaded_function.so', 'init')()
a = overloaded_function()
a:print_string(6)
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/overloaded_function$ cat commands.bash 
#!/bin/bash
g++ overloaded_function.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o overloaded_function.so overloaded_function.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/overloaded_function$ ./commands.bash 
print_string: 6

Wednesday, February 8, 2017

Consider the following code which registers a free function (plus()) as a member function of class A:

When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/register_free$ cat test.lua
package.loadlib('./register_free.so', 'init')()
x = A(3)
print(x:plus(5))
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/register_free$ cat commands.bash
#!/bin/bash
g++ register_free.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o register_free.so register_free.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/register_free$ ./commands.bash
8
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/register_free$

Luabind class demonstration

Consider this code that exposes a C++ class to Lua:

When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/testclass$ cat test.lua 
package.loadlib('./testclass.so', 'init')()
a = testclass('a string')
a:print_string()
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/testclass$ cat commands.bash 
#!/bin/bash
g++ testclass.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o testclass.so testclass.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/testclass$ ./commands.bash 
a string

Saturday, February 4, 2017

Implementing a type trait

I copied/adapted this code from Polukhin's Boost book:

When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/cpp/polukhin/ch04/implement_type_trait$ g++ -std=c++11 -I/home/kuyu/dkuyu/bin/boost_1_60_0 1.cpp && ./a.out
int=false
vector<int>=true
vector<char*>=true



Friday, February 3, 2017

How to make a Windows folder always available in Ubuntu in Oracle Virtualbox

First share your Windows folder to your Ubuntu virtual machine in Oracle Virtualbox:



In your Ubuntu machine, add a similar line to your /etc/fstab file:

dkuyu   /home/kuyu/dkuyu    vboxsf  uid=kuyu,gid=kuyu,rw,dmode=700,fmode=700,_netdev      0 0

(Be sure to create the Linux folder afterwards. You can do this via mkdir command. In this example, make sure that /home/kuyu/dkuyu exists. If not, create it.)

Reboot your Ubuntu virtual machine.

After reboot, the Windows folder should be available in the Ubuntu folder /home/kuyu/dkuyu (for this example).

Luabind for sin math function

First we register the cmath sin() function as a function named sin() in Lua:

We write a simple script to test the Lua sin() function:

kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/sin$ cat test.lua 
package.loadlib('./c.so', 'init')()
print(sin(1.57))

We compile and test it as follows:

kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/sin$ cat commands.bash 
#!/bin/bash
g++ c.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o c.so c.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/sin$ ./commands.bash 
0.99999970197678

(1.57 is very close to pi/2 and the sin of pi/2 is 1.)

Thursday, February 2, 2017

Making luabind work in ubuntu

To be able to run Luabind in Ubuntu, you need to install libluabind-dev and lua:
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ sudo apt-get install libluabind-dev
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ sudo apt-get install lua

Consider this hellobind.cpp code:
Then consider this test.lua code:

kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ cat test.lua
package.loadlib('./hellobind.so', 'init')()
greet()

To run the test.lua code:

kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ cat commands.bash 
#!/bin/bash
g++ hellobind.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o hellobind.so hellobind.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ ./commands.bash 
hello world!

Sometimes, loading the C++ library in Lua can be problematic. To see what the problems are when loading the library, you can execute this lua file (execute "lua test1.lua"):

kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ cat test1.lua
local initfunction, errormessage = package.loadlib('/home/kuyu/dkuyu/Dropbox/practice/lua/luabind/hellobind.so','init')
if errormessage then
    print('Error loading hello_world:', errormessage)
end