Skip to content

Commit 668b9fb

Browse files
committed
UPBGE: Implement implicit cast in CListValue::iterator.
Previously CListValue::iterator was simply std::vector<CValue *>::iterator, it requested to cast the CValue pointer to an other type in the loop code. Generally an extra line was dedicated to this operation. To avoid this line or at least avoir explicit cast, the iterator is now a cast owning a std::vector::iterator. This class is declared with a template argulment which is the class name to cast to when deferencing the iterator for example: CListValue::iterator<KX_GameObject> it = list->GetBegin() The class redefines the ++ operator, the * operator for deferencing and the != operator, to be used in a loop. By this way the developer doesn't have to bother of the cast if he set properly the iterator. Also this class gives less control to the original iterator, it's safer.
1 parent 40bb11c commit 668b9fb

File tree

8 files changed

+74
-42
lines changed

8 files changed

+74
-42
lines changed

source/gameengine/Converter/BL_BlenderDataConversion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,8 +1745,8 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
17451745
}
17461746

17471747
// Look at every material texture and ask to create realtime cube map.
1748-
for (CListValue::iterator it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
1749-
KX_GameObject *gameobj = (KX_GameObject*)*it;
1748+
for (CListValue::iterator<KX_GameObject> it = sumolist->GetBegin(), end = sumolist->GetEnd(); it != end; ++it) {
1749+
KX_GameObject *gameobj = *it;
17501750

17511751
for (unsigned short i = 0, meshcount = gameobj->GetMeshCount(); i < meshcount; ++i) {
17521752
RAS_MeshObject *mesh = gameobj->GetMesh(i);

source/gameengine/Expressions/EXP_ListValue.h

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,33 @@ class CListValue : public CPropValue
3939
virtual CValue* GetReplica();
4040

4141
public:
42-
typedef std::vector<CValue *>::iterator iterator;
42+
typedef std::vector<CValue *>::iterator baseIterator;
43+
44+
template <class T>
45+
class iterator
46+
{
47+
private:
48+
baseIterator m_it;
49+
50+
public:
51+
iterator(baseIterator it)
52+
:m_it(it)
53+
{
54+
}
55+
56+
inline void operator++()
57+
{
58+
++m_it;
59+
}
60+
61+
inline T *operator*()
62+
{
63+
return (T *)*m_it;
64+
}
65+
66+
template <class U>
67+
friend bool operator!=(const iterator<U>& it1, const iterator<U>& it2);
68+
};
4369

4470
void MergeList(CListValue* otherlist);
4571
bool RemoveValue(CValue* val);
@@ -59,8 +85,8 @@ class CListValue : public CPropValue
5985
CValue *GetFront();
6086
CValue *GetBack();
6187
int GetCount() { return m_pValueArray.size(); }
62-
iterator GetBegin();
63-
iterator GetEnd();
88+
baseIterator GetBegin();
89+
baseIterator GetEnd();
6490
virtual const STR_String & GetText();
6591

6692
bool CheckEqual(CValue* first,CValue* second);
@@ -89,5 +115,11 @@ class CListValue : public CPropValue
89115
bool m_bReleaseContents;
90116
};
91117

118+
template <class T>
119+
inline bool operator!=(const CListValue::iterator<T>& it1, const CListValue::iterator<T>& it2)
120+
{
121+
return it1.m_it != it2.m_it;
122+
}
123+
92124
#endif /* __EXP_LISTVALUE_H__ */
93125

source/gameengine/Expressions/intern/ListValue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ CValue *CListValue::GetBack()
107107
return m_pValueArray.back();
108108
}
109109

110-
CListValue::iterator CListValue::GetBegin()
110+
CListValue::baseIterator CListValue::GetBegin()
111111
{
112112
return m_pValueArray.begin();
113113
}
114114

115-
CListValue::iterator CListValue::GetEnd()
115+
CListValue::baseIterator CListValue::GetEnd()
116116
{
117117
return m_pValueArray.end();
118118
}

source/gameengine/Ketsji/KX_GameObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ void KX_GameObject::UpdateComponents()
17001700
return;
17011701
}
17021702

1703-
for (CListValue::iterator it = m_components->GetBegin(), end = m_components->GetEnd(); it != end; ++it) {
1703+
for (CListValue::baseIterator it = m_components->GetBegin(), end = m_components->GetEnd(); it != end; ++it) {
17041704
PyObject *pycomp = (*it)->GetProxy();
17051705
if (!PyObject_CallMethod(pycomp, "update", "")) {
17061706
PyErr_Print();

source/gameengine/Ketsji/KX_KetsjiEngine.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ bool KX_KetsjiEngine::NextFrame()
403403
#endif
404404

405405
// for each scene, call the proceed functions
406-
for (CListValue::iterator sceit = m_scenes->GetBegin(); sceit != m_scenes->GetEnd(); ++sceit) {
407-
KX_Scene *scene = (KX_Scene *)*sceit;
406+
for (CListValue::iterator<KX_Scene> sceit = m_scenes->GetBegin(), sceend = m_scenes->GetEnd(); sceit != sceend; ++sceit) {
407+
KX_Scene *scene = *sceit;
408408

409409
/* Suspension holds the physics and logic processing for an
410410
* entire scene. Objects can be suspended individually, and
@@ -499,8 +499,8 @@ bool KX_KetsjiEngine::NextFrame()
499499

500500
void KX_KetsjiEngine::UpdateSuspendedScenes()
501501
{
502-
for (CListValue::iterator sceneit = m_scenes->GetBegin(); sceneit != m_scenes->GetEnd(); ++sceneit) {
503-
KX_Scene *scene = (KX_Scene *)*sceneit;
502+
for (CListValue::iterator<KX_Scene> sceneit = m_scenes->GetBegin(), sceneend = m_scenes->GetEnd(); sceneit != sceneend; ++sceneit) {
503+
KX_Scene *scene = *sceneit;
504504
if (scene->IsSuspended()) {
505505
if (scene->getSuspendedTime() == 0.0f) {
506506
scene->setSuspendedTime(m_clockTime);
@@ -534,8 +534,8 @@ void KX_KetsjiEngine::Render()
534534

535535
BeginFrame();
536536

537-
for (CListValue::iterator sceit = m_scenes->GetBegin(), sceend = m_scenes->GetEnd(); sceit != sceend; ++sceit) {
538-
KX_Scene *scene = (KX_Scene *)*sceit;
537+
for (CListValue::iterator<KX_Scene> sceit = m_scenes->GetBegin(), sceend = m_scenes->GetEnd(); sceit != sceend; ++sceit) {
538+
KX_Scene *scene = *sceit;
539539
// shadow buffers
540540
RenderShadowBuffers(scene);
541541
// cubemaps
@@ -586,8 +586,8 @@ void KX_KetsjiEngine::Render()
586586
unsigned short pass = 0;
587587

588588
// for each scene, call the proceed functions
589-
for (CListValue::iterator sceit = m_scenes->GetBegin(), sceend = m_scenes->GetEnd(); sceit != sceend; ++sceit) {
590-
KX_Scene *scene = (KX_Scene *)*sceit;
589+
for (CListValue::iterator<KX_Scene> sceit = m_scenes->GetBegin(), sceend = m_scenes->GetEnd(); sceit != sceend; ++sceit) {
590+
KX_Scene *scene = *sceit;
591591
KX_Camera *activecam = scene->GetActiveCamera();
592592
CListValue *cameras = scene->GetCameraList();
593593

@@ -620,8 +620,8 @@ void KX_KetsjiEngine::Render()
620620
}
621621

622622
// Draw the scene once for each camera with an enabled viewport
623-
for (CListValue::iterator it = cameras->GetBegin(), end = cameras->GetEnd(); it != end; ++it) {
624-
KX_Camera *cam = (KX_Camera*)(*it);
623+
for (CListValue::iterator<KX_Camera> it = cameras->GetBegin(), end = cameras->GetEnd(); it != end; ++it) {
624+
KX_Camera *cam = *it;
625625
if (cam->GetViewport()) {
626626
// do the rendering
627627
RenderFrame(scene, cam, pass++);
@@ -809,8 +809,8 @@ void KX_KetsjiEngine::UpdateAnimations(KX_Scene *scene)
809809
// Sanity/debug print to make sure we're actually going at the fps we want (should be close to anim_timestep)
810810
// CM_Debug("Anim fps: " << 1.0/(m_frameTime - m_previousAnimTime));
811811
m_previousAnimTime = m_frameTime;
812-
for (CListValue::iterator sceneit = m_scenes->GetBegin(); sceneit != m_scenes->GetEnd(); ++sceneit)
813-
((KX_Scene *)*sceneit)->UpdateAnimations(m_frameTime);
812+
for (CListValue::iterator<KX_Scene> sceneit = m_scenes->GetBegin(), sceneend = m_scenes->GetEnd(); sceneit != sceneend; ++sceneit)
813+
(*sceneit)->UpdateAnimations(m_frameTime);
814814
}
815815
}
816816
else
@@ -1245,8 +1245,8 @@ void KX_KetsjiEngine::RenderDebugProperties()
12451245
unsigned propsAct = 0;
12461246
unsigned propsMax = (m_canvas->GetHeight() - ycoord) / const_ysize;
12471247

1248-
for (CListValue::iterator sceit = m_scenes->GetBegin(); sceit != m_scenes->GetEnd(); ++sceit) {
1249-
KX_Scene *scene = (KX_Scene *)*sceit;
1248+
for (CListValue::iterator<KX_Scene> sceit = m_scenes->GetBegin(), sceend = m_scenes->GetEnd(); sceit != sceend; ++sceit) {
1249+
KX_Scene *scene = *sceit;
12501250
/* the 'normal' debug props */
12511251
std::vector<SCA_DebugProp *>& debugproplist = scene->GetDebugProperties();
12521252

@@ -1753,8 +1753,8 @@ void KX_KetsjiEngine::Resize()
17531753
const RAS_FrameSettings &framesettings = firstscene->GetFramingType();
17541754

17551755
if (framesettings.FrameType() == RAS_FrameSettings::e_frame_extend) {
1756-
for (CListValue::iterator sceit = m_scenes->GetBegin(); sceit != m_scenes->GetEnd(); ++sceit) {
1757-
KX_Scene *scene = (KX_Scene *)*sceit;
1756+
for (CListValue::iterator<KX_Scene> sceit = m_scenes->GetBegin(), sceend = m_scenes->GetEnd(); sceit != sceend; ++sceit) {
1757+
KX_Scene *scene = *sceit;
17581758
KX_Camera *cam = scene->GetActiveCamera();
17591759
cam->InvalidateProjectionMatrix();
17601760
}

source/gameengine/Ketsji/KX_MouseFocusSensor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus()
377377

378378
CListValue *cameras = m_kxscene->GetCameraList();
379379

380-
for (CListValue::iterator it = cameras->GetBegin(), end = cameras->GetEnd(); it != end; ++it) {
381-
KX_Camera *cam = (KX_Camera*)(*it);
380+
for (CListValue::iterator<KX_Camera> it = cameras->GetBegin(), end = cameras->GetEnd(); it != end; ++it) {
381+
KX_Camera *cam = *it;
382382
if ((cam != activecam) && cam->GetViewport())
383383
if (ParentObjectHasFocusCamera(cam))
384384
return true;

source/gameengine/Ketsji/KX_PythonInit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,8 @@ static PyObject *gPySetGLSLMaterialSetting(PyObject *,
10821082
if (KX_GetActiveEngine()) {
10831083
CListValue *scenes = KX_GetActiveEngine()->CurrentScenes();
10841084

1085-
for (CListValue::iterator it = scenes->GetBegin(); it != scenes->GetEnd(); ++it) {
1086-
KX_Scene *scene = (KX_Scene *)*it;
1085+
for (CListValue::iterator<KX_Scene> it = scenes->GetBegin(), end = scenes->GetEnd(); it != end; ++it) {
1086+
KX_Scene *scene = *it;
10871087
// temporarily store the glsl settings in the scene for the GLSL materials
10881088
scene->GetBlenderScene()->gm.flag = gs->glslflag;
10891089
if (scene->GetBucketManager()) {

source/gameengine/Ketsji/KX_Scene.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,8 @@ SCA_IObject* KX_Scene::AddReplicaObject(class CValue* originalobject,
977977
replica->InitComponents();
978978
// Initialize components recursively.
979979
CListValue *childrecursive = replica->GetChildrenRecursive();
980-
for (CListValue::iterator it = childrecursive->GetBegin(), end = childrecursive->GetEnd(); it != end; ++it) {
981-
KX_GameObject *gameobj = (KX_GameObject *)*it;
980+
for (CListValue::iterator<KX_GameObject> it = childrecursive->GetBegin(), end = childrecursive->GetEnd(); it != end; ++it) {
981+
KX_GameObject *gameobj = *it;
982982
gameobj->InitComponents();
983983
}
984984
childrecursive->Release();
@@ -1417,8 +1417,8 @@ void KX_Scene::CalculateVisibleMeshes(RAS_IRasterizer* rasty,KX_Camera* cam, int
14171417
m_boundingBoxManager->Update(false);
14181418

14191419
// Update the object boudning volume box if the object had a deformer.
1420-
for (CListValue::iterator it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
1421-
KX_GameObject *gameobj = static_cast<KX_GameObject*>(*it);
1420+
for (CListValue::iterator<KX_GameObject> it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
1421+
KX_GameObject *gameobj = *it;
14221422
if (gameobj->GetDeformer()) {
14231423
/** Update all the deformer, not only per material.
14241424
* One of the side effect is to clear some flags about AABB calculation.
@@ -1438,8 +1438,8 @@ void KX_Scene::CalculateVisibleMeshes(RAS_IRasterizer* rasty,KX_Camera* cam, int
14381438
* since DBVT culling will only set it to false.
14391439
* This is similar to what RAS_BucketManager does for RAS_MeshSlot culling.
14401440
*/
1441-
for (CListValue::iterator it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
1442-
KX_GameObject *gameobj = static_cast<KX_GameObject*>(*it);
1441+
for (CListValue::iterator<KX_GameObject> it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
1442+
KX_GameObject *gameobj = *it;
14431443
gameobj->SetCulled(true);
14441444
}
14451445

@@ -1473,8 +1473,8 @@ void KX_Scene::DrawDebug(RAS_IRasterizer *rasty)
14731473
{
14741474
const bool showBoundingBox = KX_GetActiveEngine()->GetShowBoundingBox();
14751475
if (showBoundingBox) {
1476-
for (CListValue::iterator it = m_objectlist->GetBegin(); it != m_objectlist->GetEnd(); ++it) {
1477-
KX_GameObject *gameobj = (KX_GameObject *)*it;
1476+
for (CListValue::iterator<KX_GameObject> it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
1477+
KX_GameObject *gameobj = *it;
14781478

14791479
if (!gameobj->GetCulled() && gameobj->GetMeshCount() != 0) {
14801480
const MT_Vector3& scale = gameobj->NodeGetWorldScaling();
@@ -1500,8 +1500,8 @@ void KX_Scene::DrawDebug(RAS_IRasterizer *rasty)
15001500
}
15011501
}
15021502
// The side effect of a armature is that it was added in the animated object list.
1503-
for (CListValue::iterator it = m_animatedlist->GetBegin(), end = m_animatedlist->GetEnd(); it != end; ++it) {
1504-
KX_GameObject *gameobj = (KX_GameObject *)*it;
1503+
for (CListValue::iterator<KX_GameObject> it = m_animatedlist->GetBegin(), end = m_animatedlist->GetEnd(); it != end; ++it) {
1504+
KX_GameObject *gameobj = *it;
15051505
if (gameobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE) {
15061506
BL_ArmatureObject *armature = (BL_ArmatureObject *)gameobj;
15071507
if (armature->GetDrawDebug() || KX_GetActiveEngine()->GetShowArmatures()) {
@@ -1704,10 +1704,10 @@ RAS_MaterialBucket* KX_Scene::FindBucket(class RAS_IPolyMaterial* polymat, bool
17041704
void KX_Scene::RenderBuckets(const MT_Transform & cameratransform,
17051705
class RAS_IRasterizer* rasty)
17061706
{
1707-
for (CListValue::iterator it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
1707+
for (CListValue::iterator<KX_GameObject> it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
17081708
/* This function update all mesh slot info (e.g culling, color, matrix) from the game object.
17091709
* It's done just before the render to be sure of the object color and visibility. */
1710-
((KX_GameObject *)*it)->UpdateBuckets();
1710+
(*it)->UpdateBuckets();
17111711
}
17121712

17131713
m_bucketmanager->Renderbuckets(cameratransform,rasty);
@@ -1727,8 +1727,8 @@ void KX_Scene::UpdateObjectLods()
17271727
const MT_Vector3& cam_pos = m_active_camera->NodeGetWorldPosition();
17281728
const float lodfactor = m_active_camera->GetLodDistanceFactor();
17291729

1730-
for (CListValue::iterator it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
1731-
KX_GameObject *gameobj = (KX_GameObject *)*it;
1730+
for (CListValue::iterator<KX_GameObject> it = m_objectlist->GetBegin(), end = m_objectlist->GetEnd(); it != end; ++it) {
1731+
KX_GameObject *gameobj = *it;
17321732
if (!gameobj->GetCulled()) {
17331733
gameobj->UpdateLod(cam_pos, lodfactor);
17341734
}

0 commit comments

Comments
 (0)