[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 4 | // %flair:license}
|
---|
| 5 | // created: 2012/05/07
|
---|
| 6 | // filename: Object.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: Base class for all Framework's classes
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include <stdio.h>
|
---|
| 19 |
|
---|
| 20 | #include "Object.h"
|
---|
| 21 | #include "Object_impl.h"
|
---|
| 22 | #include "FrameworkManager.h"
|
---|
| 23 |
|
---|
| 24 | #ifdef __XENO__
|
---|
| 25 | #include <native/task.h>
|
---|
| 26 | #include <native/timer.h>
|
---|
| 27 | #include "rtdk.h"
|
---|
| 28 | #else
|
---|
| 29 | #include <sys/time.h>
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | using std::string;
|
---|
| 33 | using std::vector;
|
---|
| 34 |
|
---|
[15] | 35 | namespace flair {
|
---|
| 36 | namespace core {
|
---|
[2] | 37 |
|
---|
[15] | 38 | Time GetTime(void) {
|
---|
[2] | 39 | #ifdef __XENO__
|
---|
[15] | 40 | return rt_timer_read();
|
---|
[2] | 41 | #else
|
---|
[15] | 42 | struct timeval t;
|
---|
| 43 | gettimeofday(&t, NULL);
|
---|
| 44 | return (Time)((Time)(t.tv_sec) * 1000000 + (Time)(t.tv_usec)) * 1000;
|
---|
[2] | 45 |
|
---|
| 46 | #endif
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[15] | 49 | void Printf(const char *format, ...) {
|
---|
| 50 | va_list args;
|
---|
| 51 | va_start(args, format);
|
---|
[2] | 52 | #ifdef __XENO__
|
---|
[15] | 53 | if (rt_task_self() != NULL) {
|
---|
| 54 | rt_vfprintf(stderr, format, args);
|
---|
| 55 | } else
|
---|
[2] | 56 | #endif
|
---|
[15] | 57 | {
|
---|
| 58 | vfprintf(stderr, format, args);
|
---|
| 59 | }
|
---|
[2] | 60 |
|
---|
[15] | 61 | va_end(args);
|
---|
[2] | 62 | }
|
---|
| 63 |
|
---|
[15] | 64 | Object::Object(const Object *parent, string name, string type) {
|
---|
| 65 | pimpl_ = new Object_impl(this, parent, name, type);
|
---|
| 66 | if (parent != NULL)
|
---|
| 67 | parent->pimpl_->AddChild(this);
|
---|
[2] | 68 | }
|
---|
| 69 |
|
---|
[15] | 70 | Object::~Object() {
|
---|
| 71 | if (pimpl_->parent != NULL)
|
---|
| 72 | pimpl_->parent->pimpl_->RemoveChild(this);
|
---|
| 73 | delete pimpl_;
|
---|
[2] | 74 | }
|
---|
| 75 |
|
---|
[15] | 76 | string Object::ObjectName(void) const { return pimpl_->name; }
|
---|
[2] | 77 |
|
---|
[15] | 78 | string Object::ObjectType(void) const { return pimpl_->type; }
|
---|
[2] | 79 |
|
---|
[15] | 80 | const Object *Object::Parent(void) const { return pimpl_->parent; }
|
---|
[2] | 81 |
|
---|
[15] | 82 | vector<const Object *> *Object::TypeChilds(void) const {
|
---|
| 83 | return &(pimpl_->type_childs);
|
---|
[2] | 84 | }
|
---|
| 85 |
|
---|
[15] | 86 | vector<const Object *> *Object::Childs(void) const { return &(pimpl_->childs); }
|
---|
[2] | 87 |
|
---|
[15] | 88 | void Object::ColorPrintf(color_t color, const char *function, int line,
|
---|
| 89 | const char *format, va_list *args) const {
|
---|
[2] | 90 | #ifdef __XENO__
|
---|
[15] | 91 | if (rt_task_self() != NULL) {
|
---|
| 92 | rt_fprintf(stderr, "\033[%dm", color);
|
---|
[294] | 93 | if(function) {
|
---|
| 94 | if (line) {
|
---|
| 95 | rt_fprintf(stderr, "%s - line %d, %s: ", function, line,
|
---|
| 96 | pimpl_->name.c_str());
|
---|
| 97 | } else {
|
---|
| 98 | rt_fprintf(stderr, "%s, %s: ", function, pimpl_->name.c_str());
|
---|
| 99 | }
|
---|
[2] | 100 | }
|
---|
[15] | 101 | rt_vfprintf(stderr, format, *args);
|
---|
| 102 | rt_fprintf(stderr, "\033[%dm", color_t::Auto);
|
---|
| 103 | } else
|
---|
[2] | 104 | #endif
|
---|
[15] | 105 | {
|
---|
| 106 | fprintf(stderr, "\033[%dm", color);
|
---|
[294] | 107 | if(function) {
|
---|
| 108 | if (line) {
|
---|
| 109 | fprintf(stderr, "%s - line %d, %s: ", function, line,
|
---|
| 110 | pimpl_->name.c_str());
|
---|
| 111 | } else {
|
---|
| 112 | fprintf(stderr, "%s, %s: ", function, pimpl_->name.c_str());
|
---|
| 113 | }
|
---|
[2] | 114 | }
|
---|
[15] | 115 | vfprintf(stderr, format, *args);
|
---|
| 116 | fprintf(stderr, "\033[%dm", color_t::Auto);
|
---|
| 117 | }
|
---|
[2] | 118 | }
|
---|
| 119 |
|
---|
[15] | 120 | void Object::Information(const char *function, int line, const char *format,
|
---|
| 121 | ...) const {
|
---|
| 122 | va_list args;
|
---|
| 123 | va_start(args, format);
|
---|
| 124 | ColorPrintf(color_t::Green, function, line, format, &args);
|
---|
| 125 | va_end(args);
|
---|
[2] | 126 | }
|
---|
| 127 |
|
---|
| 128 | void Object::Warning(const char *function, const char *format, ...) const {
|
---|
[15] | 129 | va_list args;
|
---|
| 130 | va_start(args, format);
|
---|
| 131 | ColorPrintf(color_t::Orange, function, 0, format, &args);
|
---|
| 132 | va_end(args);
|
---|
[2] | 133 | }
|
---|
| 134 |
|
---|
| 135 | void Object::Error(const char *function, const char *format, ...) const {
|
---|
[15] | 136 | va_list args;
|
---|
| 137 | va_start(args, format);
|
---|
| 138 | ColorPrintf(color_t::Red, function, 0, format, &args);
|
---|
| 139 | va_end(args);
|
---|
[2] | 140 |
|
---|
[15] | 141 | pimpl_->error_occured = true;
|
---|
[2] | 142 | }
|
---|
| 143 |
|
---|
[15] | 144 | bool Object::ErrorOccured(bool recursive) const {
|
---|
| 145 | return pimpl_->ErrorOccured(recursive);
|
---|
[2] | 146 | }
|
---|
| 147 |
|
---|
| 148 | } // end namespace core
|
---|
| 149 | } // end namespace flair
|
---|