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