1
0
Fork 0

Remove use of unary_function, binary_function

Preparing for switch to C++17
This commit is contained in:
James Turner 2020-06-22 10:18:00 +01:00
parent 425a2f4fe3
commit 3883b19556
6 changed files with 42 additions and 90 deletions

View file

@ -42,35 +42,22 @@ namespace
{ {
struct GuiFont struct GuiFont
{ {
const char* name; const std::string name;
puFont *font; puFont *font;
struct Predicate
: public std::unary_function<const GuiFont, bool>
{
Predicate(const std::string& name_) : name(name_) {}
bool operator() (const GuiFont& f1) const
{
return (name == f1.name);
}
const std::string name;
};
}; };
const GuiFont guifonts[] = { const std::initializer_list<GuiFont> guiFonts = {
{ "default", &PUFONT_HELVETICA_12 }, {"default", &PUFONT_HELVETICA_12},
{ "FIXED_8x13", &PUFONT_8_BY_13 }, {"FIXED_8x13", &PUFONT_8_BY_13},
{ "FIXED_9x15", &PUFONT_9_BY_15 }, {"FIXED_9x15", &PUFONT_9_BY_15},
{ "TIMES_10", &PUFONT_TIMES_ROMAN_10 }, {"TIMES_10", &PUFONT_TIMES_ROMAN_10},
{ "TIMES_24", &PUFONT_TIMES_ROMAN_24 }, {"TIMES_24", &PUFONT_TIMES_ROMAN_24},
{ "HELVETICA_10", &PUFONT_HELVETICA_10 }, {"HELVETICA_10", &PUFONT_HELVETICA_10},
{ "HELVETICA_12", &FONT_HELVETICA_12 }, {"HELVETICA_12", &FONT_HELVETICA_12},
{ "HELVETICA_14", &FONT_HELVETICA_14 }, {"HELVETICA_14", &FONT_HELVETICA_14},
{ "HELVETICA_18", &PUFONT_HELVETICA_18 }, {"HELVETICA_18", &PUFONT_HELVETICA_18},
{ "SANS_12B", &FONT_SANS_12B }, {"SANS_12B", &FONT_SANS_12B},
{ nullptr, nullptr }
}; };
const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])-1];
} }
FGFontCache* FGFontCache::instance() FGFontCache* FGFontCache::instance()
@ -104,19 +91,18 @@ FGFontCache::~FGFontCache()
} }
} }
inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1, bool FGFontCache::FntParams::operator<(const FntParams& other) const
const FntParams& f2) const
{ {
int comp = f1.name.compare(f2.name); int comp = name.compare(other.name);
if (comp < 0) if (comp < 0)
return true; return true;
else if (comp > 0) else if (comp > 0)
return false; return false;
if (f1.size < f2.size) if (size < other.size)
return true; return true;
else if (f1.size > f2.size) else if (size > other.size)
return false; return false;
return f1.slant < f2.slant; return slant < other.slant;
} }
FGFontCache::FontCacheEntry* FGFontCache::FontCacheEntry*
@ -133,9 +119,12 @@ FGFontCache::getfnt(const std::string& fontName, float size, float slant)
if (texi != _texFonts.end()) { if (texi != _texFonts.end()) {
texfont = texi->second; texfont = texi->second;
} else { } else {
const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd, auto guifont = std::find_if(guiFonts.begin(), guiFonts.end(),
GuiFont::Predicate(fontName)); [&fontName](const GuiFont& gf) {
if (guifont != guifontsEnd) { return gf.name == fontName;
});
if (guifont != guiFonts.end()) {
pufont = guifont->font; pufont = guifont->font;
} }
} }
@ -149,7 +138,7 @@ FGFontCache::getfnt(const std::string& fontName, float size, float slant)
f->pufont = new puFont; f->pufont = new puFont;
f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant); f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
} else { } else {
f->pufont = guifonts[0].font; f->pufont = guiFonts.begin()->font;
} }
// insert into the cache // insert into the cache

View file

@ -48,11 +48,8 @@ private:
: name(name_), size(size_), slant(slant_) : name(name_), size(size_), slant(slant_)
{ {
} }
};
struct FntParamsLess bool operator<(const FntParams& other) const;
: public std::binary_function<const FntParams, const FntParams, bool>
{
bool operator() (const FntParams& f1, const FntParams& f2) const;
}; };
struct FontCacheEntry { struct FontCacheEntry {
@ -71,7 +68,7 @@ private:
SGPath _path; SGPath _path;
typedef std::map<const std::string, fntTexFont*> TexFontMap; typedef std::map<const std::string, fntTexFont*> TexFontMap;
typedef std::map<const FntParams, FontCacheEntry*, FntParamsLess> PuFontMap; typedef std::map<const FntParams, FontCacheEntry*> PuFontMap;
TexFontMap _texFonts; TexFontMap _texFonts;
PuFontMap _cache; PuFontMap _cache;

View file

@ -284,7 +284,11 @@ public:
simgear::requestConsole(); // ensure console is shown on Windows simgear::requestConsole(); // ensure console is shown on Windows
std::sort(_aircraft.begin(), _aircraft.end(), ciLessLibC()); std::sort(_aircraft.begin(), _aircraft.end(),
[](const std::string& lhs, const std::string& rhs) {
return strcasecmp(lhs.c_str(), rhs.c_str()) < 0 ? 1 : 0;
});
cout << "Available aircraft:" << endl; cout << "Available aircraft:" << endl;
for ( unsigned int i = 0; i < _aircraft.size(); i++ ) { for ( unsigned int i = 0; i < _aircraft.size(); i++ ) {
cout << _aircraft[i] << endl; cout << _aircraft[i] << endl;
@ -353,16 +357,6 @@ private:
return 0; return 0;
} }
// recommended in Meyers, Effective STL when internationalization and embedded
// NULLs aren't an issue. Much faster than the STL or Boost lex versions.
struct ciLessLibC : public std::binary_function<string, string, bool>
{
bool operator()(const std::string &lhs, const std::string &rhs) const
{
return strcasecmp(lhs.c_str(), rhs.c_str()) < 0 ? 1 : 0;
}
};
int _minStatus; int _minStatus;
string_list _aircraft; string_list _aircraft;
}; };

View file

@ -173,11 +173,11 @@ double testNan(double val)
return val; return val;
} }
} // namespace
struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void> void FGModelMgr::update(double dt)
{ {
void operator()(FGModelMgr::Instance* instance) const std::for_each(_instances.begin(), _instances.end(), [](FGModelMgr::Instance* instance) {
{
SGModelPlacement* model = instance->model; SGModelPlacement* model = instance->model;
double roll, pitch, heading; double roll, pitch, heading;
roll = pitch = heading = 0.0; roll = pitch = heading = 0.0;
@ -217,14 +217,7 @@ struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
model->setHeadingDeg(heading); model->setHeadingDeg(heading);
instance->model->update(); instance->model->update();
} });
};
}
void
FGModelMgr::update (double dt)
{
std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
} }
void void

View file

@ -1192,11 +1192,13 @@ void CameraGroup::resized()
const CameraInfo* CameraGroup::getGUICamera() const const CameraInfo* CameraGroup::getGUICamera() const
{ {
ConstCameraIterator result auto result = std::find_if(camerasBegin(), camerasEnd(),
= std::find_if(camerasBegin(), camerasEnd(), [](const osg::ref_ptr<CameraInfo>& cam) {
FlagTester<CameraInfo>(GUI)); return cam->flags & GUI;
});
if (result == camerasEnd()) { if (result == camerasEnd()) {
return NULL; return nullptr;
} }
return *result; return *result;

View file

@ -130,28 +130,5 @@ protected:
}; };
/**
* Class for testing if flags are set in an object with a flags member.
*/
template<typename T>
class FlagTester : public std::unary_function<osg::ref_ptr<T>, bool>
{
public:
/** Initialize with flags to test for.
* @param flags logical or of flags to test.
*/
FlagTester(unsigned flags_) : flags(flags_) {}
/** test operator
* @param obj An object with a flags member
* @return true if flags member of obj contains any of the flags
* (bitwise and with flags is nonzero).
*/
bool operator() (const osg::ref_ptr<T>& obj)
{
return (obj->flags & flags) != 0;
}
unsigned flags;
};
} }
#endif #endif