http://www.developer.com/ws/brew/article.php/3355691/Discovering-C-Idioms-in-BREW.htm
The following is a non-exhaustive list of recurring idioms in BREW, considered mainly from a C++ perspective. This is by no means a new enterprise, but the existing information is mostly scattered in articles, the BREW Forum[1], or knowledge bases[2][3]. The initial title was "Design Patterns in BREW," but the term "idiom" was finally preferred, largely because idioms are defined as low-level patterns specific to one language and this is exactly what this article wants to be about. Two of the idioms presented in this installment, "Stack Starvation" and "EverLoad," are fairly common and their implementation is (almost) trivial. The third one, "Static Is Dynamic," needs more attention and raises some interesting problems. Problem: Context: Forces: Solution: Example: Resulting Context: Related Patterns: Smart Pointers, Pooled Allocation, Variable Allocation, EverLoad Problem: Context: Forces: Solution: Example: Cppapp is the application class in subsequent examples. The reference returned by shared() behaves fundamentally like a static value. Because there is only one instance of CPPApp, there will be only one instance of sharedValue and implicitly one value for the refernce returned by shared(). There are problems with this approach, as we will see later, but the overall the solution is satisfactory. Let's see how this applies to Singleton, a pattern based on an internally kept static instance[5][6]. Usually, we express a Singleton like this: A Singleton should not be directly instantiated (that's why the private constructor) and obviously getInstance() returns the same instance. Let's see how this translates in BREW. Singleton has a unique instance, kept by CPPApp (b_). To access it, Singleton is a friend of CPPApp. On the other hand, the creation of b_ has to be done by CPPApp (in OnAppInitData()) and has to be inhibited at compile time in other contexts (private constructor). This implies that CPPApp has to be a friend of Singleton. An example of using Singleton is presented in test1(). It was mentioned before that simply using CPPApp::sharedValue might create some discomfort for the user. There are some problems with this design: A more complex design might alleviate these issues: Static defines the manipulation pattern and StaticBase is a simple policy class for manipulating types. There is a container of static variables in CPPAppp, Every static variable is uniquely identified by type and an unique ID, a simple int in this case. Let's take, for example, a simple class having two static variables: This is how the two variables have to be defined to be unique: Now, CPPApp has an automatic way of keeping track of static variables (of course, the array will be replaced by a container in real life). They are strongly typed and user classes now own them: Resulting Context: Problem: Forces: Solution: Example: Related Patterns: Variable Allocation The next article will continue this discussion, presenting other idioms. [1] http://brewforums.qualcomm.com [2] http://brew.qualcomm.com/brew/en/developer/resources/ds/faq.html [3] http://brew.qualcomm.com/brew/en/developer/resources/ds/okb.html [4]
[5] GOF. Design Patterns: Elements of Reusable Object-Oriented Software, Addison Wesley. 1994 [6] John M. Vlissides. Pattern Hatching: Design Patterns Applied, Addison Wesley. 1998 [7] For a complete discussion of the new/delete operator, see Scott Meyers. More Effective C++, Addison Wesley. 1996 [8] http://fusshuhn.ourfamily.com/cppincomp.html Download the accompanying code file here (77 Kb).
Discovering C++ Idioms in BREW
May 19, 2004
Stack Starvation
struct STR{...};
bool f(/*const STR s - it copies STR on stack*/ const STR& s )
{
//char a[300]; avoid, expensive
char* a = new char[300];
//while (f(s)) {}; recursion
delete[] a; //don't forget to deallocate - error prone
}
Static Is Dynamic
class CPPApp : public AEEApplet
{
//other members omitted for brevity
//... ...
public:
static CPPApp* getInstance()
{
return (CPPApp*)GETAPPINSTANCE();
}
private:
CPPApp( const CPPApp&);
const CPPApp& operator=( const CPPApp&);
private:
int sharedValue;
friend Singleton;
Singleton * b_;
};
boolean CPPApp::OnAppInitData()
{
b_ = new Singleton (2);
//...
return TRUE;
}
void CPPApp::OnAppfreeData()
{
delete b_;
//....
}
struct Singleton
{
static Singleton * getInstance()
{
CPPApp* c = CPPApp::getInstance();
return c->b_;
}
int getValue()
{
return i_;
}
private:
friend CPPApp;
Singleton (int i) : i_(i)
{}
private:
int i_;
};
struct Test
{
void test1()
{
Singleton * b = Singleton::getInstance();
b->getValue();
Singleton * bb = Singleton::getInstance();
bb->getValue();
// Singleton * bbb = new Singleton (11); //doesn't compile
}
static int& shared()
{
return CPPApp::getInstance->sharedValue;
}
};
struct Singleton
{
static Singleton* getInstance()
{
if (!instance_ )
instance_ = new Singleton;
return instance_;
}
private:
static Singleton* instance_;
private:
Singleton(){}
};
Singleton* Singleton::instance_ = 0;
class CPPApp : public AEEApplet
{
//....
private:
friend struct Static;
void* statics[4]; //simple container
};
struct Static
{
template <class T>
static typename T::STATIC_TYPE staticValue()
{
CPPApp* c = CPPApp::getInstance();
return *static_cast<typename T::STATIC_TYPE_PTR>
(c->statics[T::getUID()]);
}
template <class T>
static void staticValue(const typename T::STATIC_TYPE t)
{
CPPApp* c = CPPApp::getInstance();
c->statics[T::getUID()] = &t
}
};
template <class TP>
struct StaticBase
{
typedef TP* STATIC_TYPE_PTR;
typedef TP& STATIC_TYPE;
};
struct A : public Static
{
StaticA a;
StaticB b;
};
struct StaticA : public StaticBase<int>
{
static size_t getUID()
{
return 0;
}
};
struct StaticB : public StaticBase<int>
{
static size_t getUID()
{
return 1;
}
};
void test2()
{
Writer writer;
int val = 777;
A::staticValue<StaticA>(val);
StaticA::STATIC_TYPE i = A::staticValue<StaticA>();
writer.WriteLine(i);
i = 2345;
writer.WriteLine(A::staticValue<StaticA>());
val = 888;
A::staticValue<StaticB>(val);
writer.WriteLine(A::staticValue<StaticB>());
A::staticValue<StaticB>() = 4567;
writer.WriteLine(A::staticValue<StaticB>());
}
EverLoad
//global scope
void* operator new(size_t sz);
void operator delete(void *p);
void* operator new[](size_t sz);
void operator delete[](void *p);
class A
{
public:
static void* operator new(size_t sz);
static void operator delete(void *p);
//....
}
What's Next?
References
http://www.developer.com/ws/brew/article.php/3323091
http://www.developer.com/ws/brew/article.php/3315011Download