KISS

Keep It Simple Stupid

LinkedListT in Bada

| comments

I worked on a few projects for Bada OS 2.0 half a year or more ago, and I’d wanted to share some of my solutions since then. Finally, the blog is up and running, so I do that until it’s too late.

One of the weirdest classes in Bada is LinkedListT<>. It’s very easy to get a number of memory leaks unless you know a few subtleties. Whereas LinkedList has Remove, RemoveAll, and other remove methods with bool deallocate = false parameter, LinkedListT’s remove methods don’t have it. Therefore, you can’t easily clear such a list.

Then, destructing a LinkedListT doesn’t automatically deallocate any object in the container. You have to remove all of them manually (remember, there is no RemoveAll method that deallocates all the objects).

And last, to get an item from the container, you have to declare a variable on the stack, and use GetAt() to “fill” the object. Doing this a lot makes this typing rather boring.

To solve all of the mentioned issues, I created a child class with additional behavior. Here it is:

LinkedListT with additional behavior
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * FixedLinkedListT.h
 */

#ifndef FIXEDLINKEDLISTT_H_
#define FIXEDLINKEDLISTT_H_

#include <FBase.h>

// A class that extends Osp::Base::Collection::LinkedListT and implements
// the missing methods from Osp::Base::Collection::LinkedList:
//   void RemoveAll(bool deallocate);
//   Type GetItemAt(int index);
// Use when Type is a pointer only

template <class Type>
class FixedLinkedListT :
  public Osp::Base::Collection::LinkedListT<Type>
{
public:
  bool removeAllOnDeallocating;

  FixedLinkedListT() :
      removeAllOnDeallocating(true)
  {
  }

  virtual ~FixedLinkedListT()
  {
      if (removeAllOnDeallocating)
      {
          this->RemoveAll(true);
      }
  }

  void RemoveAll(bool deallocate)
  {
      for (int i = this->GetCount() - 1; i >= 0; --i)
      {
          Type object;
          this->GetAt(i, object);
          delete object;

          this->RemoveAt(i);
      }
  }

  Type GetItemAt(int index)
  {
      Type object;
      this->GetAt(index, object);
      return object;
  }
};

#endif /* FIXEDLINKEDLISTT_H_ */

NB! The class works when Type is a pointer only!

Comments