[C++-sig] Boost Python - Implementing a Cloning function
Jay Riley
super24bitsound at hotmail.com
Fri Jul 1 01:52:46 CEST 2011
I'm trying to implement a Cloning function for one of my classes I want to extend to Python. Extending the classes in C++ is easy as I can just override the Clone function, but it's definitely not working like that for Python. Here's the code, with unimportant detail ommitted.
template<class T>
class Cloneable
{
public:
typedef boost::shared_ptr<T> ClonePtr;
virtual ClonePtr Clone() = 0;
};
class Action : public Cloneable<Action>
{
public:
virtual ClonePtr Clone() override;
}
Action::ClonePtr Action::Clone()
{
return ClonePtr(new Action(*this));
}
class Attack : public Action
{
public:
virtual ClonePtr Clone() override;
}
Attack::ClonePtr Attack::Clone()
{
return Attack::ClonePtr(new Attack(*this));
}
struct AttackWrapper : Game::Battles::Actions::Attack
{
Cloneable<Action>::ClonePtr AttackWrapper::Clone()
{
return call_method<Cloneable<Action>::ClonePtr>(self, "Clone");
}
Cloneable<Action>::ClonePtr AttackWrapper::CloneDefault()
{
return this->Action::Clone();
}
}
I expose it like follows:
class_<Attack, boost::shared_ptr<Attack>, std::auto_ptr<AttackWrapper>, bases<Action> >("Attack")
.def(init<>())
.def(init<Attack>())
.def("Clone", &Attack::Clone, &AttackWrapper::CloneDefault)
;
In my python file:
class ScriptedAttack(Attack):
def __init__(self, Type, ID, Name, Flags, Power, MPCost = 0, SPCost = 0, Accuracy = 0.9, CritChance = 0.1, DefineOwnUse = False, EleWeights = None, StatusEffectChances = None):
#initialization logic
def Clone(self):
#return ScriptedAttack(self)
#what to put here?
Fire = ScriptedAttack(ActionType.MagicAction, PrimaryEngine.GetUID(), "Fire", AttackFlags.Projectile | AttackFlags.Elemental, 32, 14, 0, 1.0, 0.1, False, {Elements.Fire: 1.0})
Fire2 = Fire.Clone()
ActLibrary.AddAttack(Fire)
ActLibrary.AddAttack(Fire2)
I'm really not sure how to do this - I'm stuck on what I need to put under def Clone. I know I can't have multiple inits, and I can't define a copy constructor. I know about the copy function in python, but the Action class (base of Attack) has a custom copy constructor I need to be called.
Can someone help me out with how to do this?
Thanks in advance
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20110630/643d67a5/attachment.html>
More information about the Cplusplus-sig
mailing list