Wonder if I should restart this blog….
I have been wondering if I should restart this blog but I have no idea of what I should blog about……lets see how it goes..
What is a box called in French? well not Le Box
I have been wondering if I should restart this blog but I have no idea of what I should blog about……lets see how it goes..
Lua is basically a c lib that provide a runtime vm where you dynamic can load and run small lua programs.
Lua is used a lot as scripting language in games both for intern “game” scripting and as a API for users to extend the game as in World of Warcraft.
A wrapper class around some of the basic lua methods could look like this:
class LuaRef : public RefBase {
public:
LuaRef() : _ls(lua_open()), _status_lastrun(0) {
luaL_openlibs(_ls);
}
~LuaRef() {
lua_close(_ls);
}
//inline operator lua_State*() { return _ls; }
int RunLuaProgram() {
_status_lastrun = lua_pcall(_ls, 0, LUA_MULTRET, 0);
return _status_lastrun;
}
int LoadLuaProgram(const char * path) {
_status_lastrun = luaL_loadfile(_ls, path);
return _status_lastrun;
}
void ShowErrors() {
if ( _status_lastrun!=0 ) {
std::cerr << "-- " << lua_tostring(_ls, -1) << std::endl;
lua_pop(_ls, 1); // remove error message
}
}
private:
lua_State *_ls;
int _status_lastrun;
};
The code is run with
Ref
ll->LoadLuaProgram("hello.lua");
ll->ShowErrors();
ll->RunLuaProgram();
ll->ShowErrors();
where hello.lua looks like:
-- hello.lua
-- the first program in every language
io.write("Hello world, from ",_VERSION,"!\n")
if that is not easy to use, I dont know whats easy. You should now learn to program lua and get some examples working. I will make a post with smart lua programs soon.
Last night with nothing on the TV, I spent some times on wrapping up the intrusive_ptr from boost C++ lib.
Why not just use the lib as it is. For me it was a rater ugly way that you have to call get() on all the object to get the pointee of the intrusive_ptr object.
My RefBase class looks like this:
class RefBase {
public:
RefBase() : count(0) {}
virtual ~RefBase() {}
void add_ref() {
++count;
}
int release_ref() {
return --count;
}
int get_count() const { return count;}
friend void intrusive_ptr_release(const RefBase *t);
friend void intrusive_ptr_add_ref(const RefBase *t);
private:
mutable unsigned int count;
RefBase(const RefBase& rhs);
RefBase& operator=(const RefBase& rhs);
};
inline void intrusive_ptr_add_ref(RefBase *pointee) {
pointee->add_ref();
std::cout << "add " << pointee->get_count() << std::endl;
}
inline void intrusive_ptr_release(RefBase *pointee) {
int ref_count = pointee->release_ref();
std::cout << "dec " << pointee->get_count()<< std::endl;
if (ref_count == 0) {
delete pointee;
}
}
This is the base class that you want to use as you base class for classes that you want to be reference counted.
The next class is a wrapper for intrusive_ptr
template
class Ref {
public:
Ref() : _pointer(NULL) {}
Ref(T *pointer) {
intrusive_ptr_add_ref(pointer);
if (get_ptr() != NULL) {
intrusive_ptr_release(get_ptr());
}
_pointer = pointer;
}
Ref(const Ref& ref) {
intrusive_ptr_add_ref(ref);
if (get_ptr() != NULL) {
intrusive_ptr_release(get_ptr());
}
_pointer = ref._pointer;
}
~Ref() {
intrusive_ptr_release(_pointer.get());
}
Ref& operator=(const Ref& rhs) {
return *this;
}
Ref& operator=(T *pointer) {
return *this;
}
T *operator->() const {
return get_ptr();
}
operator T*() const { return _pointer.get(); }
T *get_ptr() const { return _pointer.get(); }
bool operator==(T *rhs) { return get_ptr() == rhs.get(); }
private:
boost::intrusive_ptr
};
With the basic class and wrapper classe done I only have to show you how to use it.
class MyClass : public RefBase {
public:
MyClass() {
cout << "MyClass created " << endl;
}
virtual ~MyClass() {
cout << "MyClass ending " << endl;
}
void someFun() {
cout << this << endl;
}
};
void MyFun(MyClass *tt){
tt->someFun();
}
int main()
{
Ref
Ref
Ref
Ref
swap(kk, hh2);
return 0;
}
Please note that some things have been left out (threading, error handling, etc) to keep the posting "short".
I wish you a mary christmas for you and your family. Take care out there in the big world.
Remember to toast for absent friends and family!
Here tonight after the kids was in bed and I sat down to do some work, I stared to wonder where the world is going in 2009. Will we have more war, pollution, hunger or will we for once be nice to one another an all just get along? Some agrue that without wars, conflicts, etc the human race will not evolve. I’m not sure I follow the logic in that. Is it only in conflict that we evolve.? By looking at the history that could be a gram of truth in it BUT my argument will be. Have we ever tired to have a very long period without conflicts. Some bad things can be said about EU but also some good. EU have provided the longest peace period in central Europe for a very long time and a lot of good things have come out of that. Science, Art, etc. We just have to push it a bit further and really let the science and art flow freely and embrace it.
I have been looking for a way lib that enable reflection in C++. I found this http://www.extreme.indiana.edu/reflcpp/ but have not been playing around with so much. Do anyone what know of other lib out there that enable reflection for c++.
The real smart would then want to know what I want to use reflection for in C++. Well I have been working on a NoMansLan tuple space and it would help a lot to keep the code base down if I could use reflection to create objects and datatypes from a string. I will get rid of sending binary objects over the wire and just send basic char * for data members etc. It will make my tuple space programming platform independent.
I was wondering if there is a place where time go when they fly fast by me? I do want to have some of that time back. For a weekend as now I have 3-4 things I would like to do. But some how I only manage to get 1-2 things done.
In the last 2-3 weeks I have been deadly tried when the kids have been put to bed. I only manage to watch some TV and then get to bed. The alarm clock rings at 0600 and we are off to a new day. I do look forward to some R&R days between Christmas and new year.
I have added reCAPTACHA for comments. I hope that I this way can stop the high amount of spam that I get.
A short test to see if the widescreen changes to the theme works with flash movies
Powered by WordPress