Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7129715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:16:19+00:00 2026-05-28T11:16:19+00:00

I am attempting to write a interpreter that will turn a string like: vector(random(0,

  • 0

I am attempting to write a interpreter that will turn a string like:

vector(random(0, 1), 2, 3)

into a bound function using boost::bind like:

bind(&vector, bind(&random, 0, 1), 2, 3)

The intended use is for a particle system so I want the ability to be able to pass a particle to these which is achieved by adding a lambda like:

bind(&vector, bind(&random, 0, 1, _1), 2, 3, _1)

These bound functions are then defined as:
typedef boost::function<boost::any(Particle*)> bound_particle_func; so I can form a sort of list of function calls passing each particle to this list to manipulate their behaviour and thus create an effect.

I can easily make the interpreter handle something like vector(1, 2, 3) but when it comes to nested functions things are getting messy and I feel like I’m bodging things.

Because the parser currently handles nested functions in a recursive manner I can’t directly bind the lambda value to the embedded function.

so instead of ending up with
bind(&vector, bind(&random, 0, 1, _1), 2, 3, _1)
I actually end up with
bind(&vector, bound random function, 2, 3, _1) which doesn’t pass the bound function the particle.

I don’t know how to better deal with nested functions.

The actual code I have at the moment (which isn’t compilable) is:

typedef std::vector<Token>::iterator token_it;    
typedef boost::function<boost::any(Particle*)> bound_particle_func;
Vector3 vector(float x, float y, float z, Particle* p = 0);
bound_particle_func parse(token_it& it);

bound_particle_func ParseVector(std::vector<Token>& tokens) {
        const static int arg_count = 3;
        std::vector<boost::variant<float, bound_particle_func>> args(arg_count);
        int type[] = { 0, 0, 0 };

        for(token_it it = tokens.begin(); it != tokens.end(); ++it) {
                Token& t = *it;

                if(t.type == Type::FLOAT) {
                        args.push_back(t.float_value);
                } else if(t.type == Type::FUNCTION) {
                        args.push_back(parse(it));
                        type[args.size() - 1] = 1;
                } else {
                        throw std::runtime_error("Type error: expected float");
                }

                if(args.size() > arg_count) {
                        throw std::runtime_error("Too many arguments for function `vector`");
                }
        }

        return boost::bind(&vector, (type[0] == 0 ? boost::get<float>(args[0]) : boost::get<bound_particle_func>(args[0])),
                                    (type[1] == 0 ? boost::get<float>(args[1]) : boost::get<bound_particle_func>(args[1])),
                                    (type[2] == 0 ? boost::get<float>(args[2]) : boost::get<bound_particle_func>(args[2])),
                                    boost::lambda::_1);
}

The bound_particle_func parse(token_it& it); function just passes the relevant tokens to the appropriate function like the one above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:16:20+00:00Added an answer on May 28, 2026 at 11:16 am

    Do not use boost::function or boost::bind. You cannot pass boost::function to something that needs an int.

    Since you only need one type of deferred argument, and you need to pass it to all your functions, it is much easier to roll your own function objects.


    An alternative that does use boost::function and boost::bind would be to use something like that:

    Vector3 f_vector(bound_particle_func x, bound_particle_func y,  
                       bound_particle_func z, Particle* p = 0)
    {
       return vector (boost::get<float>(x(p)), boost::get<float>(y(p)), 
                        boost::get<float>(z(p)), p);
    }
    

    and likewise for your other functions. Numbers are represented by bound functions that just return their bound float argument and ignore their Particle* argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to write a CompiledQuery using Linq-to-Entities that will replace a stored procedure
I'm attempting to write a function in assembly that will detect if a longer
I am attempting to write a one-line Perl script that will toggle a line
I am attempting to write an ASP.net web service that will be utilized by
I am attempting to write a dynamic dispatcher for a function that's templated on
I'm attempting to write a query that will return The most recent AccountDate with
I'm attempting to write a query that will return any customer that has multiple
I'm attempting to write a Twitter Powershell script that will use community created interfaces
When attempting to write/read cookies that have brackets in the name, it seems like
I am attempting to write a firefox addon that will analyze the displayed page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.