source: finroc_core/tAnnotatable.h @ 476:bc9f23700bec

14.08
Last change on this file since 476:bc9f23700bec was 476:bc9f23700bec, checked in by Max Reichardt <mreichardt@…>, 10 months ago

Replaces static assertion that causes compiler error in gcc 9.3 with a plain assertion

File size: 5.7 KB
Line 
1//
2// You received this file as part of Finroc
3// A framework for intelligent robot control
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    core/tAnnotatable.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2012-10-26
27 *
28 * \brief   Contains tAnnotatable
29 *
30 * \b tAnnotatable
31 *
32 * Abstract base class for classes that can be annotated with Finroc annotations
33 * (classes derived from tAnnotation)
34 *
35 */
36//----------------------------------------------------------------------
37#ifndef __core__tAnnotatable_h__
38#define __core__tAnnotatable_h__
39
40//----------------------------------------------------------------------
41// External includes (system with <>, local with "")
42//----------------------------------------------------------------------
43
44//----------------------------------------------------------------------
45// Internal includes with ""
46//----------------------------------------------------------------------
47#include "core/internal/tAnnotatableImplementation.h"
48
49//----------------------------------------------------------------------
50// Namespace declaration
51//----------------------------------------------------------------------
52namespace finroc
53{
54namespace core
55{
56
57//----------------------------------------------------------------------
58// Forward declarations / typedefs / enums
59//----------------------------------------------------------------------
60class tAnnotatableBase {};
61
62//----------------------------------------------------------------------
63// Class declaration
64//----------------------------------------------------------------------
65//! Annotatable class
66/*!
67 * Abstract base class for classes that can be annotated with Finroc annotations
68 * (classes derived from tAnnotation).
69 *
70 * The C++ data type is used to lookup annotations.
71 * => max. one annotation of a specific C++ data type may be added.
72 */
73class tAnnotatable : public internal::tAnnotatableImplementation
74{
75
76//----------------------------------------------------------------------
77// Public methods and typedefs
78//----------------------------------------------------------------------
79public:
80
81  tAnnotatable()
82  {}
83
84  /*!
85   * Attach annotation to this object
86   *
87   * \tparam TAnnotation Annotation type to add annotation with (used for lookup later; may also be base class)
88   * \param ann Annotation to add. It will be automatically deleted when this tAnnotatable object is.
89   */
90  template <typename TAnnotation>
91  void AddAnnotation(TAnnotation& ann)
92  {
93    assert(static_cast<void*>(&ann) == &static_cast<tAnnotation&>(ann) && "tAnnotation must be first parent class when using multiple inheritance");
94    internal::tAnnotatableImplementation::AddAnnotation(ann, typeid(TAnnotation).name());
95  }
96
97  /*!
98   * Create annotation of specified type and attach it to this object.
99   *
100   * \tparam TAnnotation Annotation type to create and attach (type is used for lookup later)
101   * \param constructor_arguments Constructor arguments that are forwarded to TAnnotation constructor
102   * \return Annotation that was created. It will be automatically deleted when this tAnnotatable object is.
103   */
104  template <typename TAnnotation, typename... TArguments>
105  TAnnotation& EmplaceAnnotation(TArguments && ... constructor_arguments)
106  {
107    TAnnotation* created_annotation = new TAnnotation(constructor_arguments...);
108    AddAnnotation(*created_annotation);
109    return *created_annotation;
110  }
111
112  /*!
113   * Obtain annotation of specified type attached to this object
114   *
115   * \tparam TAnnotation Annotation type (with which annotation was added)
116   * \return Annotation. Null if this object has no annotation of specified type attached.
117   */
118  template <typename TAnnotation>
119  TAnnotation* GetAnnotation() const
120  {
121    return static_cast<TAnnotation*>(internal::tAnnotatableImplementation::GetAnnotation(typeid(TAnnotation).name()));
122  }
123
124  /*!
125   * Obtain annotation of specified type attached to this object
126   *
127   * \param rtti_name Annotation type name as obtained from C++ RTTI (typeid(...).name())
128   * \return Annotation. Null if this object has no annotation of specified type attached.
129   */
130  tAnnotation* GetAnnotation(const char* rtti_name) const
131  {
132    return internal::tAnnotatableImplementation::GetAnnotation(rtti_name);
133  }
134
135//----------------------------------------------------------------------
136// Protected methods
137//----------------------------------------------------------------------
138protected:
139
140  /*!
141   * Notify annotations that object is to be deleted
142   */
143  void NotifyAnnotationsDelete()
144  {
145    internal::tAnnotatableImplementation::NotifyAnnotationsDelete();
146  }
147
148  /*!
149   * Notify annotations that object has been initialized
150   */
151  void NotifyAnnotationsInitialized()
152  {
153    internal::tAnnotatableImplementation::NotifyAnnotationsInitialized();
154  }
155
156};
157
158//----------------------------------------------------------------------
159// End of namespace declaration
160//----------------------------------------------------------------------
161}
162}
163
164
165#endif
Note: See TracBrowser for help on using the repository browser.