Skip to content

Commit

Permalink
UPBGE: Replace STR_String by std::string.
Browse files Browse the repository at this point in the history
Using std::string instead of STR_String is benefic for the following reasons:
- it requests less works to use and already existing standart class
- std::string can be compiler side optimized
- std::string supports more functions than STR_String

All places using STR_String are replaced by std::string, but old places using
const char * or char * are now trying to use std::string. In some cases is
not possible for example the blender file and path utilities.

The major refactor to support std::string is for python attributes class
KX_PYATTRIBUTE_DEF. Previously we know that the attributes list ends when
the name of an attribute was to NULL. The issue with std::string is that
is not a pointer and NULL could not be anymore used. To solve this issue
a default attribut with an empty name is added: KX_PYATTRIBUTE_NULL. Its
name length is checked to know if it's the last attribut.

Even if std::string has more function than STR_String, this class lack of
format and upper functions. To compensate this lack the boost functions
are used.

As STR_String is still used in blender by the GHOST part, a constructor
doing the conversion from std::string to STR_String is written.
  • Loading branch information
panzergame committed Dec 19, 2016
1 parent d63c4d1 commit 38cc2eb
Show file tree
Hide file tree
Showing 230 changed files with 1,093 additions and 1,119 deletions.
2 changes: 2 additions & 0 deletions intern/string/STR_String.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#endif

#include <vector>
#include <string> // Compatibility
#include <limits.h>

#include <cstring>
Expand Down Expand Up @@ -81,6 +82,7 @@ class STR_String
STR_String(const STR_String &str);
STR_String(const STR_String & str, int len);
STR_String(const char *src1, int src1_len, const char *src2, int src2_len);
STR_String(const std::string& s); // Compatibility
explicit STR_String(int val);
explicit STR_String(dword val);
explicit STR_String(float val);
Expand Down
10 changes: 9 additions & 1 deletion intern/string/intern/STR_String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,15 @@ STR_String::STR_String(const char *src1, int len1, const char *src2, int len2) :
this->m_data[len1 + len2] = 0;
}


STR_String::STR_String(const std::string& s) :
m_data(new char[s.size() + 8]),
m_len(s.size()),
m_max(s.size() + 8)
{
assertd(this->m_data != NULL);
memcpy(this->m_data, s.c_str(), s.size());
this->m_data[s.size()] = 0;
}

//
// Create a string with an integer value
Expand Down
8 changes: 4 additions & 4 deletions source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c

char* startscenename = startscene->id.name + 2;
char pathname[FILE_MAXDIR+FILE_MAXFILE];
STR_String exitstring = "";
std::string exitstring = "";
BlendFileData *bfd = NULL;

BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));

KX_SetOrigPath(STR_String(blenderdata->name));
KX_SetOrigPath(std::string(blenderdata->name));

#ifdef WITH_PYTHON

Expand Down Expand Up @@ -121,8 +121,8 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
// base the actuator filename with respect
// to the original file working directory

if (exitstring != "") {
BLI_strncpy(basedpath, exitstring.ReadPtr(), sizeof(basedpath));
if (!exitstring.empty()) {
BLI_strncpy(basedpath, exitstring.c_str(), sizeof(basedpath));
}

// load relative to the last loaded file, this used to be relative
Expand Down
6 changes: 3 additions & 3 deletions source/gameengine/BlenderRoutines/KX_BlenderCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void KX_BlenderCanvas::SetMousePosition(int x, int y)
WM_cursor_warp(m_win, winX + x + 1, winY + (winH - y - 1));
}

void KX_BlenderCanvas::MakeScreenShot(const char *filename)
void KX_BlenderCanvas::MakeScreenShot(const std::string& filename)
{
unsigned int *pixeldata;
bScreen *screen = m_win->screen;
Expand Down Expand Up @@ -286,8 +286,8 @@ void KX_BlenderCanvas::MakeScreenShot(const char *filename)

// create file path
char path[FILE_MAX];
BLI_strncpy(path, filename, FILE_MAX);
BLI_path_abs(path, KX_GetMainPath().ReadPtr());
BLI_strncpy(path, filename.c_str(), FILE_MAX);
BLI_path_abs(path, KX_GetMainPath().c_str());

/* save_screenshot() frees dumprect and im_format */
save_screenshot(path, width, height, pixeldata, im_format);
Expand Down
2 changes: 1 addition & 1 deletion source/gameengine/BlenderRoutines/KX_BlenderCanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class KX_BlenderCanvas : public RAS_ICanvas
virtual void SetMouseState(RAS_MouseState mousestate);
virtual void SetMousePosition(int x, int y);

virtual void MakeScreenShot(const char *filename);
virtual void MakeScreenShot(const std::string& filename);

virtual void BeginDraw();
virtual void EndDraw();
Expand Down
10 changes: 5 additions & 5 deletions source/gameengine/Converter/BL_ActionActuator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "BL_Action.h"
#include "BL_ActionManager.h"
#include "KX_GameObject.h"
#include "STR_HashedString.h"
#include <string>
#include "MEM_guardedalloc.h"
#include "DNA_nla_types.h"
#include "DNA_action_types.h"
Expand All @@ -61,8 +61,8 @@ extern "C" {
}

BL_ActionActuator::BL_ActionActuator(SCA_IObject *gameobj,
const STR_String& propname,
const STR_String& framepropname,
const std::string& propname,
const std::string& framepropname,
float starttime,
float endtime,
struct bAction *action,
Expand Down Expand Up @@ -560,7 +560,7 @@ PyAttributeDef BL_ActionActuator::Attributes[] = {
KX_PYATTRIBUTE_RW_FUNCTION("useContinue", BL_ActionActuator, pyattr_get_use_continue, pyattr_set_use_continue),
KX_PYATTRIBUTE_FLOAT_RW_CHECK("blendTime", 0, MAXFRAMEF, BL_ActionActuator, m_blendframe, CheckBlendTime),
KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",0,100,false,BL_ActionActuator,m_playtype,CheckType),
{ NULL } //Sentinel
KX_PYATTRIBUTE_NULL //Sentinel
};

PyObject *BL_ActionActuator::pyattr_get_action(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
Expand All @@ -580,7 +580,7 @@ int BL_ActionActuator::pyattr_set_action(void *self_v, const KX_PYATTRIBUTE_DEF
}

bAction *action= NULL;
STR_String val = _PyUnicode_AsString(value);
std::string val = _PyUnicode_AsString(value);

if (val != "")
{
Expand Down
8 changes: 4 additions & 4 deletions source/gameengine/Converter/BL_ActionActuator.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class BL_ActionActuator : public SCA_IActuator
public:
Py_Header
BL_ActionActuator(SCA_IObject* gameobj,
const STR_String& propname,
const STR_String& framepropname,
const std::string& propname,
const std::string& framepropname,
float starttime,
float endtime,
struct bAction *action,
Expand Down Expand Up @@ -136,8 +136,8 @@ class BL_ActionActuator : public SCA_IActuator
short m_layer;
short m_ipo_flags;
struct bAction *m_action;
STR_String m_propname;
STR_String m_framepropname;
std::string m_propname;
std::string m_framepropname;
};

enum {
Expand Down
6 changes: 3 additions & 3 deletions source/gameengine/Converter/BL_ArmatureActuator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ PyAttributeDef BL_ArmatureActuator::Attributes[] = {
KX_PYATTRIBUTE_FLOAT_RW("weight",0.0f,1.0f,BL_ArmatureActuator,m_weight),
KX_PYATTRIBUTE_FLOAT_RW("influence",0.0f,1.0f,BL_ArmatureActuator,m_influence),
KX_PYATTRIBUTE_INT_RW("type",0,ACT_ARM_MAXTYPE,false,BL_ArmatureActuator,m_type),
{ NULL } //Sentinel
KX_PYATTRIBUTE_NULL //Sentinel
};

PyObject *BL_ArmatureActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
{
BL_ArmatureActuator* actuator = static_cast<BL_ArmatureActuator*>(self);
KX_GameObject *target = (!strcmp(attrdef->m_name, "target")) ? actuator->m_gametarget : actuator->m_gamesubtarget;
KX_GameObject *target = (attrdef->m_name == "target") ? actuator->m_gametarget : actuator->m_gamesubtarget;
if (!target)
Py_RETURN_NONE;
else
Expand All @@ -240,7 +240,7 @@ PyObject *BL_ArmatureActuator::pyattr_get_object(void *self, const struct KX_PYA
int BL_ArmatureActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
{
BL_ArmatureActuator* actuator = static_cast<BL_ArmatureActuator*>(self);
KX_GameObject* &target = (!strcmp(attrdef->m_name, "target")) ? actuator->m_gametarget : actuator->m_gamesubtarget;
KX_GameObject* &target = (attrdef->m_name == "target") ? actuator->m_gametarget : actuator->m_gamesubtarget;
KX_GameObject *gameobj;

if (!ConvertPythonToGameObject(actuator->GetLogicManager(), value, &gameobj, true, "actuator.object = value: BL_ArmatureActuator"))
Expand Down
4 changes: 2 additions & 2 deletions source/gameengine/Converter/BL_ArmatureActuator.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class BL_ArmatureActuator : public SCA_IActuator
BL_ArmatureConstraint* m_constraint;
KX_GameObject* m_gametarget;
KX_GameObject* m_gamesubtarget;
STR_String m_posechannel;
STR_String m_constraintname;
std::string m_posechannel;
std::string m_constraintname;
float m_weight;
float m_influence;
int m_type;
Expand Down
9 changes: 4 additions & 5 deletions source/gameengine/Converter/BL_ArmatureChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ PyAttributeDef BL_ArmatureChannel::Attributes[] = {
// Keep these attributes in order of BCA_ defines!!! used by py_attr_getattr and py_attr_setattr
KX_PYATTRIBUTE_RO_FUNCTION("bone",BL_ArmatureChannel,py_attr_getattr),
KX_PYATTRIBUTE_RO_FUNCTION("parent",BL_ArmatureChannel,py_attr_getattr),

{ NULL } //Sentinel
KX_PYATTRIBUTE_NULL //Sentinel
};

/* attributes directly taken from bPoseChannel */
Expand Down Expand Up @@ -144,7 +143,7 @@ PyAttributeDef BL_ArmatureChannel::AttributesPtr[] = {
KX_PYATTRIBUTE_FLOAT_RW("ik_rot_weight",0,1.0f,bPoseChannel,ikrotweight),
KX_PYATTRIBUTE_FLOAT_RW("ik_lin_weight",0,1.0f,bPoseChannel,iklinweight),
KX_PYATTRIBUTE_RW_FUNCTION("joint_rotation",BL_ArmatureChannel,py_attr_get_joint_rotation,py_attr_set_joint_rotation),
{ NULL } //Sentinel
KX_PYATTRIBUTE_NULL //Sentinel
};

PyObject *BL_ArmatureChannel::py_attr_getattr(void *self_v, const struct KX_PYATTRIBUTE_DEF *attrdef)
Expand Down Expand Up @@ -417,7 +416,7 @@ PyMethodDef BL_ArmatureBone::Methods[] = {

/* no attributes on C++ class since it is never instantiated */
PyAttributeDef BL_ArmatureBone::Attributes[] = {
{ NULL } //Sentinel
KX_PYATTRIBUTE_NULL //Sentinel
};

// attributes that work on proxy ptr (points to a Bone structure)
Expand All @@ -437,7 +436,7 @@ PyAttributeDef BL_ArmatureBone::AttributesPtr[] = {
KX_PYATTRIBUTE_FLOAT_MATRIX_RO("bone_mat",Bone,bone_mat,3),
KX_PYATTRIBUTE_RO_FUNCTION("parent",BL_ArmatureBone,py_bone_get_parent),
KX_PYATTRIBUTE_RO_FUNCTION("children",BL_ArmatureBone,py_bone_get_children),
{ NULL } //Sentinel
KX_PYATTRIBUTE_NULL //Sentinel
};

PyObject *BL_ArmatureBone::py_bone_get_parent(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
Expand Down
11 changes: 5 additions & 6 deletions source/gameengine/Converter/BL_ArmatureConstraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ PyTypeObject BL_ArmatureConstraint::Type = {

PyObject *BL_ArmatureConstraint::py_repr(void)
{
return PyUnicode_FromString(m_name);
return PyUnicode_FromStdString(m_name);
}

#endif // WITH_PYTHON
Expand Down Expand Up @@ -96,7 +96,7 @@ BL_ArmatureConstraint::BL_ArmatureConstraint(
m_target->RegisterObject(m_armature);
if (m_subtarget)
m_subtarget->RegisterObject(m_armature);
BLI_snprintf(m_name, sizeof(m_name), "%s:%s", m_posechannel->name, m_constraint->name);
m_name = std::string(m_posechannel->name) + ":" + std::string(m_constraint->name);
}

BL_ArmatureConstraint::~BL_ArmatureConstraint()
Expand Down Expand Up @@ -211,9 +211,9 @@ void BL_ArmatureConstraint::RestoreTarget()
}
}

bool BL_ArmatureConstraint::Match(const char* posechannel, const char* constraint)
bool BL_ArmatureConstraint::Match(const std::string& posechannel, const std::string& constraint)
{
return (!strcmp(m_posechannel->name, posechannel) && !strcmp(m_constraint->name, constraint));
return ((m_posechannel->name == posechannel) && (m_constraint->name == constraint));
}

void BL_ArmatureConstraint::SetTarget(KX_GameObject* target)
Expand Down Expand Up @@ -282,8 +282,7 @@ PyAttributeDef BL_ArmatureConstraint::Attributes[] = {
KX_PYATTRIBUTE_RO_FUNCTION("ik_flag",BL_ArmatureConstraint,py_attr_getattr),
KX_PYATTRIBUTE_RW_FUNCTION("ik_dist",BL_ArmatureConstraint,py_attr_getattr,py_attr_setattr),
KX_PYATTRIBUTE_RW_FUNCTION("ik_mode",BL_ArmatureConstraint,py_attr_getattr,py_attr_setattr),

{ NULL } //Sentinel
KX_PYATTRIBUTE_NULL //Sentinel
};


Expand Down
6 changes: 3 additions & 3 deletions source/gameengine/Converter/BL_ArmatureConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class BL_ArmatureConstraint : public PyObjectPlus, public SG_QList
struct bConstraint* m_constraint;
struct bPoseChannel* m_posechannel;
class BL_ArmatureObject* m_armature;
char m_name[64];
std::string m_name;
KX_GameObject* m_target;
KX_GameObject* m_subtarget;
struct Object* m_blendtarget;
Expand All @@ -85,8 +85,8 @@ class BL_ArmatureConstraint : public PyObjectPlus, public SG_QList
void UpdateTarget();
void RestoreTarget();

bool Match(const char* posechannel, const char* constraint);
const char* GetName() { return m_name; }
bool Match(const std::string& posechannel, const std::string& constraint);
const std::string& GetName() { return m_name; }

void SetConstraintFlag(int flag)
{
Expand Down
16 changes: 8 additions & 8 deletions source/gameengine/Converter/BL_ArmatureObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void BL_ArmatureObject::LoadConstraints(KX_BlenderSceneConverter* converter)
GetActionManager();
}

BL_ArmatureConstraint* BL_ArmatureObject::GetConstraint(const char* posechannel, const char* constraintname)
BL_ArmatureConstraint* BL_ArmatureObject::GetConstraint(const std::string& posechannel, const std::string& constraintname)
{
SG_DList::iterator<BL_ArmatureConstraint> cit(m_controlledConstraints);
for (cit.begin(); !cit.end(); ++cit) {
Expand All @@ -354,13 +354,13 @@ BL_ArmatureConstraint* BL_ArmatureObject::GetConstraint(const char* posechannel,
return NULL;
}

BL_ArmatureConstraint* BL_ArmatureObject::GetConstraint(const char* posechannelconstraint)
BL_ArmatureConstraint* BL_ArmatureObject::GetConstraint(const std::string& posechannelconstraint)
{
// performance: use hash string instead of plain string compare
SG_DList::iterator<BL_ArmatureConstraint> cit(m_controlledConstraints);
for (cit.begin(); !cit.end(); ++cit) {
BL_ArmatureConstraint* constraint = *cit;
if (!strcmp(constraint->GetName(), posechannelconstraint))
if (constraint->GetName() == posechannelconstraint)
return constraint;
}
return NULL;
Expand Down Expand Up @@ -402,14 +402,14 @@ BL_ArmatureChannel* BL_ArmatureObject::GetChannel(bPoseChannel* pchan)
return NULL;
}

BL_ArmatureChannel* BL_ArmatureObject::GetChannel(const char* str)
BL_ArmatureChannel* BL_ArmatureObject::GetChannel(const std::string& str)
{
LoadChannels();
SG_DList::iterator<BL_ArmatureChannel> cit(m_poseChannels);
for (cit.begin(); !cit.end(); ++cit)
{
BL_ArmatureChannel* channel = *cit;
if (!strcmp(channel->m_posechannel->name, str))
if (channel->m_posechannel->name == str)
return channel;
}
return NULL;
Expand Down Expand Up @@ -641,7 +641,7 @@ PyAttributeDef BL_ArmatureObject::Attributes[] = {

KX_PYATTRIBUTE_RO_FUNCTION("constraints", BL_ArmatureObject, pyattr_get_constraints),
KX_PYATTRIBUTE_RO_FUNCTION("channels", BL_ArmatureObject, pyattr_get_channels),
{NULL} //Sentinel
KX_PYATTRIBUTE_NULL //Sentinel
};

static int bl_armature_object_get_constraints_size_cb(void *self_v)
Expand All @@ -654,7 +654,7 @@ static PyObject *bl_armature_object_get_constraints_item_cb(void *self_v, int in
return ((BL_ArmatureObject *)self_v)->GetConstraint(index)->GetProxy();
}

static const char *bl_armature_object_get_constraints_item_name_cb(void *self_v, int index)
static const std::string bl_armature_object_get_constraints_item_name_cb(void *self_v, int index)
{
return ((BL_ArmatureObject *)self_v)->GetConstraint(index)->GetName();
}
Expand All @@ -680,7 +680,7 @@ static PyObject *bl_armature_object_get_channels_item_cb(void *self_v, int index
return ((BL_ArmatureObject *)self_v)->GetChannel(index)->GetProxy();
}

static const char *bl_armature_object_get_channels_item_name_cb(void *self_v, int index)
static const std::string bl_armature_object_get_channels_item_name_cb(void *self_v, int index)
{
return ((BL_ArmatureObject *)self_v)->GetChannel(index)->GetName();
}
Expand Down
6 changes: 3 additions & 3 deletions source/gameengine/Converter/BL_ArmatureObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ class BL_ArmatureObject : public KX_GameObject
// for constraint python API
void LoadConstraints(KX_BlenderSceneConverter* converter);
size_t GetConstraintNumber() const { return m_constraintNumber; }
BL_ArmatureConstraint* GetConstraint(const char* posechannel, const char* constraint);
BL_ArmatureConstraint* GetConstraint(const char* posechannelconstraint);
BL_ArmatureConstraint* GetConstraint(const std::string& posechannel, const std::string& constraint);
BL_ArmatureConstraint* GetConstraint(const std::string& posechannelconstraint);
BL_ArmatureConstraint* GetConstraint(int index);
// for pose channel python API
void LoadChannels();
size_t GetChannelNumber() const { return m_channelNumber; }
BL_ArmatureChannel* GetChannel(bPoseChannel* channel);
BL_ArmatureChannel* GetChannel(const char* channel);
BL_ArmatureChannel* GetChannel(const std::string& channel);
BL_ArmatureChannel* GetChannel(int index);

/// Retrieve the pose matrix for the specified bone.
Expand Down
6 changes: 3 additions & 3 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, KX_Scene* scene,
// Without checking names, we get some reuse we don't want that can cause
// problems with material LoDs.
if (blenderobj && ((meshobj = converter->FindGameMesh(mesh/*, ob->lay*/)) != NULL)) {
const STR_String bge_name = meshobj->GetName();
const STR_String blender_name = ((ID *)blenderobj->data)->name + 2;
const std::string bge_name = meshobj->GetName();
const std::string blender_name = ((ID *)blenderobj->data)->name + 2;
if (bge_name == blender_name) {
return meshobj;
}
Expand Down Expand Up @@ -1198,7 +1198,7 @@ static void blenderSceneSetBackground(Scene *blenderscene)
}
}

static KX_GameObject* getGameOb(STR_String busc,CListValue* sumolist)
static KX_GameObject* getGameOb(std::string busc,CListValue* sumolist)
{
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
KX_GameObject *gameobje = *it;
Expand Down
2 changes: 1 addition & 1 deletion source/gameengine/Converter/BL_BlenderDataConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#ifndef __BL_BLENDERDATACONVERSION_H__
#define __BL_BLENDERDATACONVERSION_H__

#include "STR_String.h"
#include <string>
#include "EXP_Python.h"
#include "KX_PhysicsEngineEnums.h"
#include "SCA_IInputDevice.h"
Expand Down
Loading

1 comment on commit 38cc2eb

@lordloki
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Please sign in to comment.