Coder Social home page Coder Social logo

Comments (5)

shierei avatar shierei commented on June 11, 2024 1

simple.cpp.txt

Attached please see a modified test/exampels/simple.cpp. The key is to give the name of metaclass that is not the same as the metaclass's C++ type name. For example, in this example I gave the name of the Person metaclass "People". After you re-make simple.cpp and run it, you should see the following exception raised.

test/examples/simple.cpp:112: FAILED:
due to unexpected exception with message:
the metaclass Person couldn't be found

from ponder.

billyquith avatar billyquith commented on June 11, 2024

Do you have an example of this failing please? The unit tests seem to work: https://github.com/billyquith/ponder/blob/master/test/ponder/class.cpp

from ponder.

billyquith avatar billyquith commented on June 11, 2024

simple.cpp.txt:

#include "test.hpp"

//! [eg_simple]

#include <ponder/classbuilder.hpp>
#include <ponder/uses/runtime.hpp>
#include <iostream>

//! [eg_simple_class]
class Person
{
public:
     // constructor
    Person(const std::string& name)
        : m_name(name)
    {}

    // accessors for private members
    std::string name() const { return m_name; }
    void setName(const std::string& name) { m_name = name; }

    // public members
    float height;
    unsigned int shoeSize;

    // member function
    bool hasBigFeet() const { return shoeSize > 10; } // U.K.!

private:
    std::string m_name;
};
//! [eg_simple_class]

//! [eg_simple_declare]
PONDER_TYPE(Person)     // declare the type to Ponder

static void declare()   // declare the class members to Ponder
{
    ponder::Class::declare<Person>("People")
        .constructor<std::string>()
        .property("name", &Person::name, &Person::setName)
        .property("height", &Person::height)
        .property("shoeSize", &Person::shoeSize)
        .function("hasBigFeet", &Person::hasBigFeet)
        ;
}
//! [eg_simple_declare]

//! [eg_simple_use]
// An example of how you might use Ponder:
static void use()
{
    //! [eg_simple_metaclass]
    // retrieve the metaclass (containing the member data)
  const ponder::Class& metaclass = ponder::classByName("People");
    //! [eg_simple_metaclass]

    //! [eg_simple_create]
    // construct a new person
    ponder::UserObject person = ponder::runtime::create(metaclass, "Bozo");
    //! [eg_simple_create]

    // set attributes
    person.set("height", 1.62f);
    person.set("shoeSize", 28);

    // retrieve a function we would like to call
    const auto& func = metaclass.function("hasBigFeet");
    
    // call the function and get the result
    const bool bigFeet = ponder::runtime::call(func, person).to<bool>();

    // nasty
    //! [eg_simple_destroy]
    ponder::runtime::destroy(person);
    //! [eg_simple_destroy]
}
//! [eg_simple_use]

//! [eg_simple]

from ponder.

billyquith avatar billyquith commented on June 11, 2024

Ok, yes this needs some work.

from ponder.

billyquith avatar billyquith commented on June 11, 2024
    return UserObject(&classByObject(object), new Holder(RefTraits::getPointer(object)));

from UserObject UserObject::makeRef(T& object) is the problem. classByName() isn't the problem, it is makeRef() trying to get the metaclass details via the object type, which is different. Probably best to look up everything by type (id) and map the names instead. This could mean substantial changes to the registration/declaration.

from ponder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.