Use thread-safe init for keyboard layouts
This commit is contained in:
parent
44f6167a38
commit
f591a4668c
|
@ -56,7 +56,6 @@ class BopomofoCharacterMap {
|
|||
|
||||
protected:
|
||||
BopomofoCharacterMap();
|
||||
static BopomofoCharacterMap* c_map;
|
||||
};
|
||||
|
||||
const BPMF BPMF::FromHanyuPinyin(const std::string& str) {
|
||||
|
@ -938,12 +937,11 @@ const std::string BPMF::composedString() const {
|
|||
return result;
|
||||
}
|
||||
|
||||
BopomofoCharacterMap* BopomofoCharacterMap::c_map = 0;
|
||||
|
||||
|
||||
const BopomofoCharacterMap& BopomofoCharacterMap::SharedInstance() {
|
||||
if (!c_map) c_map = new BopomofoCharacterMap();
|
||||
|
||||
return *c_map;
|
||||
static BopomofoCharacterMap* map = new BopomofoCharacterMap();
|
||||
return *map;
|
||||
}
|
||||
|
||||
BopomofoCharacterMap::BopomofoCharacterMap() {
|
||||
|
@ -995,28 +993,6 @@ BopomofoCharacterMap::BopomofoCharacterMap() {
|
|||
componentToCharacter[(*iter).second] = (*iter).first;
|
||||
}
|
||||
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::c_StandardLayout = 0;
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::c_ETenLayout = 0;
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::c_HsuLayout = 0;
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::c_ETen26Layout = 0;
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::c_IBMLayout = 0;
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::c_HanyuPinyinLayout = 0;
|
||||
|
||||
void BopomofoKeyboardLayout::FinalizeLayouts() {
|
||||
#define FL(x) \
|
||||
if (x) { \
|
||||
delete x; \
|
||||
} \
|
||||
x = 0
|
||||
FL(c_StandardLayout);
|
||||
FL(c_ETenLayout);
|
||||
FL(c_HsuLayout);
|
||||
FL(c_ETen26Layout);
|
||||
FL(c_IBMLayout);
|
||||
FL(c_HanyuPinyinLayout);
|
||||
#undef FL
|
||||
}
|
||||
|
||||
#define ASSIGNKEY1(m, vec, k, val) \
|
||||
m[k] = (vec.clear(), vec.push_back((BPMF::Component)val), vec)
|
||||
#define ASSIGNKEY2(m, vec, k, val1, val2) \
|
||||
|
@ -1027,8 +1003,7 @@ void BopomofoKeyboardLayout::FinalizeLayouts() {
|
|||
vec.push_back((BPMF::Component)val2), \
|
||||
vec.push_back((BPMF::Component)val3), vec)
|
||||
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::StandardLayout() {
|
||||
if (!c_StandardLayout) {
|
||||
static BopomofoKeyboardLayout* CreateStandardLayout() {
|
||||
std::vector<BPMF::Component> vec;
|
||||
BopomofoKeyToComponentMap ktcm;
|
||||
|
||||
|
@ -1074,13 +1049,10 @@ const BopomofoKeyboardLayout* BopomofoKeyboardLayout::StandardLayout() {
|
|||
ASSIGNKEY1(ktcm, vec, '6', BPMF::Tone2);
|
||||
ASSIGNKEY1(ktcm, vec, '7', BPMF::Tone5);
|
||||
|
||||
c_StandardLayout = new BopomofoKeyboardLayout(ktcm, "Standard");
|
||||
return new BopomofoKeyboardLayout(ktcm, "Standard");
|
||||
}
|
||||
|
||||
return c_StandardLayout;
|
||||
}
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::IBMLayout() {
|
||||
if (!c_IBMLayout) {
|
||||
static BopomofoKeyboardLayout* CreateIBMLayout() {
|
||||
std::vector<BPMF::Component> vec;
|
||||
BopomofoKeyToComponentMap ktcm;
|
||||
|
||||
|
@ -1126,14 +1098,10 @@ const BopomofoKeyboardLayout* BopomofoKeyboardLayout::IBMLayout() {
|
|||
ASSIGNKEY1(ktcm, vec, '.', BPMF::Tone4);
|
||||
ASSIGNKEY1(ktcm, vec, '/', BPMF::Tone5);
|
||||
|
||||
c_IBMLayout = new BopomofoKeyboardLayout(ktcm, "IBM");
|
||||
return new BopomofoKeyboardLayout(ktcm, "IBM");
|
||||
}
|
||||
|
||||
return c_IBMLayout;
|
||||
}
|
||||
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::ETenLayout() {
|
||||
if (!c_ETenLayout) {
|
||||
static BopomofoKeyboardLayout* CreateETenLayout() {
|
||||
std::vector<BPMF::Component> vec;
|
||||
BopomofoKeyToComponentMap ktcm;
|
||||
|
||||
|
@ -1179,14 +1147,10 @@ const BopomofoKeyboardLayout* BopomofoKeyboardLayout::ETenLayout() {
|
|||
ASSIGNKEY1(ktcm, vec, '4', BPMF::Tone4);
|
||||
ASSIGNKEY1(ktcm, vec, '1', BPMF::Tone5);
|
||||
|
||||
c_ETenLayout = new BopomofoKeyboardLayout(ktcm, "ETen");
|
||||
return new BopomofoKeyboardLayout(ktcm, "ETen");
|
||||
}
|
||||
|
||||
return c_ETenLayout;
|
||||
}
|
||||
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::HsuLayout() {
|
||||
if (!c_HsuLayout) {
|
||||
static BopomofoKeyboardLayout* CreateHsuLayout() {
|
||||
std::vector<BPMF::Component> vec;
|
||||
BopomofoKeyToComponentMap ktcm;
|
||||
|
||||
|
@ -1216,13 +1180,10 @@ const BopomofoKeyboardLayout* BopomofoKeyboardLayout::HsuLayout() {
|
|||
ASSIGNKEY1(ktcm, vec, 'w', BPMF::AO);
|
||||
ASSIGNKEY1(ktcm, vec, 'o', BPMF::OU);
|
||||
|
||||
c_HsuLayout = new BopomofoKeyboardLayout(ktcm, "Hsu");
|
||||
return new BopomofoKeyboardLayout(ktcm, "Hsu");
|
||||
}
|
||||
|
||||
return c_HsuLayout;
|
||||
}
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::ETen26Layout() {
|
||||
if (!c_ETen26Layout) {
|
||||
static BopomofoKeyboardLayout* CreateETen26Layout() {
|
||||
std::vector<BPMF::Component> vec;
|
||||
BopomofoKeyToComponentMap ktcm;
|
||||
|
||||
|
@ -1252,18 +1213,42 @@ const BopomofoKeyboardLayout* BopomofoKeyboardLayout::ETen26Layout() {
|
|||
ASSIGNKEY1(ktcm, vec, 'r', BPMF::ER);
|
||||
ASSIGNKEY1(ktcm, vec, 'i', BPMF::AI);
|
||||
ASSIGNKEY1(ktcm, vec, 'z', BPMF::AO);
|
||||
|
||||
c_ETen26Layout = new BopomofoKeyboardLayout(ktcm, "ETen26");
|
||||
return new BopomofoKeyboardLayout(ktcm, "ETen26");
|
||||
}
|
||||
|
||||
return c_ETen26Layout;
|
||||
}
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::HanyuPinyinLayout() {
|
||||
if (!c_HanyuPinyinLayout) {
|
||||
static BopomofoKeyboardLayout* CreateHanyuPinyinLayout() {
|
||||
BopomofoKeyToComponentMap ktcm;
|
||||
c_HanyuPinyinLayout = new BopomofoKeyboardLayout(ktcm, "HanyuPinyin");
|
||||
return new BopomofoKeyboardLayout(ktcm, "HanyuPinyin");
|
||||
}
|
||||
return c_HanyuPinyinLayout;
|
||||
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::StandardLayout() {
|
||||
static BopomofoKeyboardLayout* layout = CreateStandardLayout();
|
||||
return layout;
|
||||
}
|
||||
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::ETenLayout() {
|
||||
static BopomofoKeyboardLayout* layout = CreateETenLayout();
|
||||
return layout;
|
||||
}
|
||||
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::HsuLayout() {
|
||||
static BopomofoKeyboardLayout* layout = CreateHsuLayout();
|
||||
return layout;
|
||||
}
|
||||
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::ETen26Layout() {
|
||||
static BopomofoKeyboardLayout* layout = CreateETen26Layout();
|
||||
return layout;
|
||||
}
|
||||
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::IBMLayout() {
|
||||
static BopomofoKeyboardLayout* layout = CreateIBMLayout();
|
||||
return layout;
|
||||
}
|
||||
|
||||
const BopomofoKeyboardLayout* BopomofoKeyboardLayout::HanyuPinyinLayout() {
|
||||
static BopomofoKeyboardLayout* layout = CreateHanyuPinyinLayout();
|
||||
return layout;
|
||||
}
|
||||
|
||||
} // namespace Mandarin
|
||||
|
|
|
@ -212,7 +212,6 @@ typedef std::map<BPMF::Component, char> BopomofoComponentToKeyMap;
|
|||
|
||||
class BopomofoKeyboardLayout {
|
||||
public:
|
||||
static void FinalizeLayouts();
|
||||
static const BopomofoKeyboardLayout* StandardLayout();
|
||||
static const BopomofoKeyboardLayout* ETenLayout();
|
||||
static const BopomofoKeyboardLayout* HsuLayout();
|
||||
|
@ -400,16 +399,6 @@ class BopomofoKeyboardLayout {
|
|||
std::string m_name;
|
||||
BopomofoKeyToComponentMap m_keyToComponent;
|
||||
BopomofoComponentToKeyMap m_componentToKey;
|
||||
|
||||
static const BopomofoKeyboardLayout* c_StandardLayout;
|
||||
static const BopomofoKeyboardLayout* c_ETenLayout;
|
||||
static const BopomofoKeyboardLayout* c_HsuLayout;
|
||||
static const BopomofoKeyboardLayout* c_ETen26Layout;
|
||||
static const BopomofoKeyboardLayout* c_IBMLayout;
|
||||
|
||||
// this is essentially an empty layout, but we use pointer semantic to tell
|
||||
// the differences--and pass on the responsibility to BopomofoReadingBuffer
|
||||
static const BopomofoKeyboardLayout* c_HanyuPinyinLayout;
|
||||
};
|
||||
|
||||
class BopomofoReadingBuffer {
|
||||
|
|
Loading…
Reference in New Issue