Jump to content

  • Log In with Google      Sign In   
  • Create Account


ApochPiQ

Member Since 17 Jul 2002
Offline Last Active Yesterday, 11:15 AM

Posts I've Made

In Topic: What's the problem here !?

15 August 2014 - 04:10 PM

You're still not asking a question.

The program is doing exactly what you told it to do.

In Topic: What's the problem here !?

15 August 2014 - 03:48 PM

I dunno... what is the problem? Please ask an actual question :-)

In Topic: Passing function template pointer

14 August 2014 - 04:24 PM

This is a really simple mockup of how you could proceed:

#include "stdafx.h"
#include <iostream>
#include <string>

enum Type {
    TYPE_INT,
    TYPE_STRING,
    TYPE_FLOAT,
};


void Printer (int value) {
    std::cout << "INT: " << value << std::endl;
}

void Printer (const std::string & value) {
    std::cout << "STR: " << value.c_str() << std::endl;
}

void Printer (float value) {
    std::cout << "FLT: " << value << std::endl;
}


template <typename T>
struct PrinterWrapper {
    void operator () (void * valuePtr) {
        Printer(*reinterpret_cast<const T *>(valuePtr));
    }
};


template <template <typename FuncParamT> class FuncT>
void InvokeByType (Type t, void * valuePtr) {
    switch (t) {
    case TYPE_INT:
        FuncT<int>()(valuePtr);
        break;

    case TYPE_STRING:
        FuncT<std::string>()(valuePtr);
        break;

    case TYPE_FLOAT:
        FuncT<float>()(valuePtr);
        break;
    }
}


int main () {
    int intValue = 42;
    std::string strValue = "Test";
    float fltValue = 3.1415f;

    InvokeByType<PrinterWrapper>(TYPE_INT, &intValue);
    InvokeByType<PrinterWrapper>(TYPE_STRING, &strValue);
    InvokeByType<PrinterWrapper>(TYPE_FLOAT, &fltValue);
}
Obviously the void* is kinda lame, but you can easily replace that with passing through your XML node and using appropriate parser functions to go from the node attributes to the target types.

I *think* that solves your situation :-)

In Topic: Pi decimals as a random number generator

13 August 2014 - 08:22 PM

You could, but it's not particularly efficient. If I want to generate a thousand random numbers, I have to expand the Taylor series a painfully long way, which gets into numerical precision problems and other nastiness. There exist much more effective ways to get pseudo-random numbers.

In Topic: Passing function template pointer

13 August 2014 - 04:40 PM

Think of it this way: your function template as posted will generate overloads of the function appendAttribute, each with the same function signature but a different body. To wit, the function signature is:

void foo(ICustomComponentAttributes::AttributeVector&, const xml::Node&)
Now, part two of the equation:

template <typename FType>
void InvokeFunction (FType func) {
    func(42);
}
Calling InvokeFunction(foo); for any function foo (well, as long as it accepts something convertible from int) will call through to foo with the parameter 42.

Since the appendAttribute function signature is known, you can do the same trick. Consider this minimal program:

#include <iostream>

template <typename T>
void Invoked (int param) {
    std::cout << param << std::endl;
}

template <typename FType>
void Invoker (FType func) {
    func(42);
}

int main () {
    Invoker(Invoked<int>);
}
I'm not entirely clear on what you want your attribute append code to look like, but those are the building blocks; hopefully that gets you to a solution, but if not, if you could clarify your intent a bit I'd be happy to try and piece together something.

PARTNERS