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

View file

@ -48,11 +48,8 @@ private:
: name(name_), size(size_), slant(slant_)
{
}
};
struct FntParamsLess
: public std::binary_function<const FntParams, const FntParams, bool>
{
bool operator() (const FntParams& f1, const FntParams& f2) const;
bool operator<(const FntParams& other) const;
};
struct FontCacheEntry {
@ -71,7 +68,7 @@ private:
SGPath _path;
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;
PuFontMap _cache;

View file

@ -284,7 +284,11 @@ public:
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;
for ( unsigned int i = 0; i < _aircraft.size(); i++ ) {
cout << _aircraft[i] << endl;
@ -353,16 +357,6 @@ private:
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;
string_list _aircraft;
};

View file

@ -173,11 +173,11 @@ double testNan(double 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;
double roll, pitch, heading;
roll = pitch = heading = 0.0;
@ -217,14 +217,7 @@ struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
model->setHeadingDeg(heading);
instance->model->update();
}
};
}
void
FGModelMgr::update (double dt)
{
std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
});
}
void

View file

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