1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
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 |
|
---|
35 | namespace flair {
|
---|
36 | namespace core {
|
---|
37 |
|
---|
38 | Time GetTime(void) {
|
---|
39 | #ifdef __XENO__
|
---|
40 | return rt_timer_read();
|
---|
41 | #else
|
---|
42 | struct timeval t;
|
---|
43 | gettimeofday(&t, NULL);
|
---|
44 | return (Time)((Time)(t.tv_sec) * 1000000 + (Time)(t.tv_usec)) * 1000;
|
---|
45 |
|
---|
46 | #endif
|
---|
47 | }
|
---|
48 |
|
---|
49 | void Printf(const char *format, ...) {
|
---|
50 | va_list args;
|
---|
51 | va_start(args, format);
|
---|
52 | #ifdef __XENO__
|
---|
53 | if (rt_task_self() != NULL) {
|
---|
54 | rt_vfprintf(stderr, format, args);
|
---|
55 | } else
|
---|
56 | #endif
|
---|
57 | {
|
---|
58 | vfprintf(stderr, format, args);
|
---|
59 | }
|
---|
60 |
|
---|
61 | va_end(args);
|
---|
62 | }
|
---|
63 |
|
---|
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);
|
---|
68 | }
|
---|
69 |
|
---|
70 | Object::~Object() {
|
---|
71 | if (pimpl_->parent != NULL)
|
---|
72 | pimpl_->parent->pimpl_->RemoveChild(this);
|
---|
73 | delete pimpl_;
|
---|
74 | }
|
---|
75 |
|
---|
76 | string Object::ObjectName(void) const { return pimpl_->name; }
|
---|
77 |
|
---|
78 | string Object::ObjectType(void) const { return pimpl_->type; }
|
---|
79 |
|
---|
80 | const Object *Object::Parent(void) const { return pimpl_->parent; }
|
---|
81 |
|
---|
82 | vector<const Object *> *Object::TypeChilds(void) const {
|
---|
83 | return &(pimpl_->type_childs);
|
---|
84 | }
|
---|
85 |
|
---|
86 | vector<const Object *> *Object::Childs(void) const { return &(pimpl_->childs); }
|
---|
87 |
|
---|
88 | void Object::ColorPrintf(color_t color, const char *function, int line,
|
---|
89 | const char *format, va_list *args) const {
|
---|
90 | #ifdef __XENO__
|
---|
91 | if (rt_task_self() != NULL) {
|
---|
92 | rt_fprintf(stderr, "\033[%dm", color);
|
---|
93 | if (line) {
|
---|
94 | rt_fprintf(stderr, "%s - line %d, %s: ", function, line,
|
---|
95 | pimpl_->name.c_str());
|
---|
96 | } else {
|
---|
97 | rt_fprintf(stderr, "%s, %s: ", function, pimpl_->name.c_str());
|
---|
98 | }
|
---|
99 | rt_vfprintf(stderr, format, *args);
|
---|
100 | rt_fprintf(stderr, "\033[%dm", color_t::Auto);
|
---|
101 | } else
|
---|
102 | #endif
|
---|
103 | {
|
---|
104 | fprintf(stderr, "\033[%dm", color);
|
---|
105 | if (line) {
|
---|
106 | fprintf(stderr, "%s - line %d, %s: ", function, line,
|
---|
107 | pimpl_->name.c_str());
|
---|
108 | } else {
|
---|
109 | fprintf(stderr, "%s, %s: ", function, pimpl_->name.c_str());
|
---|
110 | }
|
---|
111 | vfprintf(stderr, format, *args);
|
---|
112 | fprintf(stderr, "\033[%dm", color_t::Auto);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | void Object::Information(const char *function, int line, const char *format,
|
---|
117 | ...) const {
|
---|
118 | va_list args;
|
---|
119 | va_start(args, format);
|
---|
120 | ColorPrintf(color_t::Green, function, line, format, &args);
|
---|
121 | va_end(args);
|
---|
122 | }
|
---|
123 |
|
---|
124 | void Object::Warning(const char *function, const char *format, ...) const {
|
---|
125 | va_list args;
|
---|
126 | va_start(args, format);
|
---|
127 | ColorPrintf(color_t::Orange, function, 0, format, &args);
|
---|
128 | va_end(args);
|
---|
129 | }
|
---|
130 |
|
---|
131 | void Object::Error(const char *function, const char *format, ...) const {
|
---|
132 | va_list args;
|
---|
133 | va_start(args, format);
|
---|
134 | ColorPrintf(color_t::Red, function, 0, format, &args);
|
---|
135 | va_end(args);
|
---|
136 |
|
---|
137 | pimpl_->error_occured = true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool Object::ErrorOccured(bool recursive) const {
|
---|
141 | return pimpl_->ErrorOccured(recursive);
|
---|
142 | }
|
---|
143 |
|
---|
144 | } // end namespace core
|
---|
145 | } // end namespace flair
|
---|