~thomas-voss/dbus-cpp/refactor-core-dbus-object

« back to all changes in this revision

Viewing changes to src/core/dbus/bus.cpp

  • Committer: thomas-voss
  • Date: 2014-04-01 20:53:57 UTC
  • Revision ID: thomas.voss@canonical.com-20140401205357-1n4xf4fue3myva8f
And the major refactorings are in.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
namespace
32
32
{
33
 
struct VTable
34
 
{
35
 
    static void unregister_object_path(DBusConnection*, void* data)
36
 
    {
37
 
        delete static_cast<VTable*>(data);
38
 
    }
39
 
 
40
 
    static DBusHandlerResult on_new_message(
41
 
            DBusConnection*,
42
 
            DBusMessage* message,
43
 
            void* data)
44
 
    {
45
 
        auto thiz = static_cast<VTable*>(data);
46
 
        auto instance = thiz->object.lock();
47
 
        if (instance && instance->on_new_message(
48
 
                    core::dbus::Message::from_raw_message(
49
 
                        message)))
50
 
            return DBUS_HANDLER_RESULT_HANDLED;
51
 
 
52
 
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
53
 
    }
54
 
 
55
 
    std::weak_ptr<core::dbus::Object> object;
56
 
};
57
 
 
58
33
struct FunctionTable
59
34
{
60
35
    static void unregister_object_path(DBusConnection*, void* data)
380
355
    }
381
356
}
382
357
 
 
358
void Bus::register_fallback_object_function_table(
 
359
        const types::ObjectPath& path,
 
360
        const Bus::ObjectFunctionTable& table)
 
361
{
 
362
    auto vtable = new DBusObjectPathVTable
 
363
    {
 
364
            FunctionTable::unregister_object_path,
 
365
            FunctionTable::on_new_message,
 
366
            nullptr,
 
367
            nullptr,
 
368
            nullptr,
 
369
            nullptr
 
370
    };
 
371
 
 
372
    Error e;
 
373
    auto result = dbus_connection_try_register_fallback(
 
374
                d->connection.get(),
 
375
                path.as_string().c_str(),
 
376
                vtable,
 
377
                new FunctionTable{table},
 
378
                std::addressof(e.raw()));
 
379
 
 
380
    if (!result || e)
 
381
    {
 
382
        delete vtable;
 
383
        throw std::runtime_error(e.print());
 
384
    }
 
385
}
 
386
 
383
387
void Bus::unregister_object_function_table(
384
388
        const types::ObjectPath& path)
385
389
{
388
392
                path.as_string().c_str());
389
393
}
390
394
 
391
 
void Bus::register_object_for_path(
392
 
        const types::ObjectPath& path,
393
 
        const std::shared_ptr<Object>& object)
394
 
{
395
 
    auto vtable = new DBusObjectPathVTable
396
 
    {
397
 
            VTable::unregister_object_path,
398
 
            VTable::on_new_message,
399
 
            nullptr,
400
 
            nullptr,
401
 
            nullptr,
402
 
            nullptr
403
 
    };
404
 
 
405
 
    Error e;
406
 
    auto result = dbus_connection_try_register_object_path(
407
 
                d->connection.get(),
408
 
                path.as_string().c_str(),
409
 
                vtable,
410
 
                new VTable{object},
411
 
                std::addressof(e.raw()));
412
 
 
413
 
    if (!result || e)
414
 
    {
415
 
        delete vtable;
416
 
        throw std::runtime_error(e.print());
417
 
    }
418
 
}
419
 
 
420
 
void Bus::register_fallback_object_for_path(
421
 
        const types::ObjectPath& path,
422
 
        const std::shared_ptr<Object>& object)
423
 
{
424
 
    auto vtable = new DBusObjectPathVTable
425
 
    {
426
 
            VTable::unregister_object_path,
427
 
            VTable::on_new_message,
428
 
            nullptr,
429
 
            nullptr,
430
 
            nullptr,
431
 
            nullptr
432
 
    };
433
 
 
434
 
    Error e;
435
 
    auto result = dbus_connection_try_register_fallback(
436
 
                d->connection.get(),
437
 
                path.as_string().c_str(),
438
 
                vtable,
439
 
                new VTable{object},
440
 
                std::addressof(e.raw()));
441
 
 
442
 
    if (!result || e)
443
 
    {
444
 
        delete vtable;
445
 
        throw std::runtime_error(e.print());
446
 
    }
447
 
}
448
 
 
449
 
void Bus::unregister_object_path(
450
 
        const types::ObjectPath& path)
451
 
{
452
 
    dbus_connection_unregister_object_path(
453
 
                d->connection.get(),
454
 
                path.as_string().c_str());
455
 
}
456
 
 
457
395
Bus::SignalRouter& Bus::access_signal_router()
458
396
{
459
397
    return d->signal_router;