• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
noodlyappendage

Can I create a wrapper around std::find_if that passes in a comparator?

5 posts in this topic

I want to be able to search a private vector, without exposing the vector to the caller, using a custom comparator function that is passed in as a parameter. This seems like it should be possible, since I'm basically just creating a wrapper around a function that already exists, but I'm having trouble. Could this work or is my approach flawed?

 

What I'm trying to do is something like this:

class Container
{
public:
    int findObject(Comparator compare);

private:
    std::vector<Object*> objectList_;
};

int Container::findObject(Comparator compare)
{
    std::vector<int>::iterator itr;
    itr = std::find_if(objectList_.begin(); objectList_.end(); compare)
    {
        // ...
    }	
}

I think what I'm not getting is what the Comparator parameter type should be. If I type it as a class, then I don't think I can pass in any arbitrary comparitor I want. It would have to be the same one all the time right?

Edited by noodlyappendage
0

Share this post


Link to post
Share on other sites

As you noticed, you don't actually know what type to expect, because there could in fact be a multitude of types that satisfy what you need.  You could be specific and require a std::function<bool(Object*)>.  (This method essentially performs type erasure, so that your findObject function doesn't need to care what the actual type is.)  But that comes with a runtime cost and a somewhat less flexible interface.

 

Assuming there isn't some other difficulty that would prevent this, the typical solution to this type of problem is to use templates.  You'll accept any type whatsoever, as long as it satisfies your needs.  In this case, you need what std::find_if needs:  it should be callable, taking an Object pointer as its only parameter, and returning a bool.  Try this out:

class Container
{
public:
    template <typename TComparator>
    Object* findObject(TComparator compare);

private:
    std::vector<Object*> objectList_;
};

template <typename TComparator>
Object* Container::findObject(TComparator compare)
{
    auto itr = std::find_if(objectList_.begin(), objectList_.end(), compare);
    return (itr != objectList_.end()) ? *itr : nullptr;
}

Note that this would all need to be inside your header file, because templates cannot be compiled independently of the calling code, since the types aren't known until a piece of code makes the call.  Therefore the calling code actually needs full access to the source code in order to compile it.  However, once you get an iterator by calling std::find_if, if you still had a substantial amount of code to execute before returning a value, you could then call into another private member function that is defined in a .cpp file like normal, and thus keep the template bloat within your header file to a minimum.

 

(I used C++11 keywords auto and nullptr in the above code.  Depending on your compiler, you might need to switch back to fully specifying the type instead of using auto, and then returning NULL or 0 instead of nullptr.)

Edited by Andy Gainey
2

Share this post


Link to post
Share on other sites

You could be specific and require a std::function. (This method essentially performs type erasure, so that your findObject function doesn't need to care what the actual type is.) But that comes with a runtime cost and a somewhat less flexible interface.

 

How so?  Do you have performance numbers? How is it less flexible to use a general-purpose standard way vs. a bespoke custom way?

Edited by Bregma
0

Share this post


Link to post
Share on other sites

As you noticed, you don't actually know what type to expect, because there could in fact be a multitude of types that satisfy what you need.  You could be specific and require a std::function<bool(Object*)>.  (This method essentially performs type erasure, so that your findObject function doesn't need to care what the actual type is.)  But that comes with a runtime cost and a somewhat less flexible interface.

 

Assuming there isn't some other difficulty that would prevent this, the typical solution to this type of problem is to use templates.  You'll accept any type whatsoever, as long as it satisfies your needs.  In this case, you need what std::find_if needs:  it should be callable, taking an Object pointer as its only parameter, and returning a bool.  Try this out:

class Container
{
public:
    template <typename TComparator>
    Object* findObject(TComparator compare);

private:
    std::vector<Object*> objectList_;
};

template <typename TComparator>
Object* Container::findObject(TComparator compare)
{
    auto itr = std::find_if(objectList_.begin(), objectList_.end(), compare);
    return (itr != objectList_.end()) ? *itr : nullptr;
}

Note that this would all need to be inside your header file, because templates cannot be compiled independently of the calling code, since the types aren't known until a piece of code makes the call.  Therefore the calling code actually needs full access to the source code in order to compile it.  However, once you get an iterator by calling std::find_if, if you still had a substantial amount of code to execute before returning a value, you could then call into another private member function that is defined in a .cpp file like normal, and thus keep the template bloat within your header file to a minimum.

 

(I used C++11 keywords auto and nullptr in the above code.  Depending on your compiler, you might need to switch back to fully specifying the type instead of using auto, and then returning NULL or 0 instead of nullptr.)

 

Thank you! This works exactly how I need it to!

0

Share this post


Link to post
Share on other sites

You could be specific and require a std::function. (This method essentially performs type erasure, so that your findObject function doesn't need to care what the actual type is.) But that comes with a runtime cost and a somewhat less flexible interface.

 
How so?  Do you have performance numbers? How is it less flexible to use a general-purpose standard way vs. a bespoke custom way?


The type erasure requires wrapping the function behind some virtual function calls (or a function pointer at the least), which some common compilers today still will not optimize across even on maximum settings. Moving the body of your find function to the .cpp file will also require you to use LTO/WHOPR in order to inline the calls at all (but still requires devirtualization to run well). If your functor is large enough, it'll also fail to fit in a std::function's buffer and hence require allocation/deallocation to store. The flexibility claims are not necessarily valid; I can't think of anything you can give the template you can't give a std::function.

Overall, std::function is a fantastic tool to use when you need its features. The "bespoke custom way" is also quite usable and definitely not completely superseded by std::function.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0