source: finroc_core/tAnnotatable.h @ 362:2d1e2fdc4ac9

Last change on this file since 362:2d1e2fdc4ac9 was 362:2d1e2fdc4ac9, checked in by Tobias Föhst <foehst@…>, 7 years ago

Added and updated license information

File size: 5.8 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#ifndef __clang__ // clang does not like this assert: static_assert expression is not an integral constant expression
94    static_assert(static_cast<void*>(&ann) == &static_cast<tAnnotation&>(ann), "tAnnotation must be first parent class when using multiple inheritance");
95#endif
96    internal::tAnnotatableImplementation::AddAnnotation(ann, typeid(TAnnotation).name());
97  }
98
99  /*!
100   * Create annotation of specified type and attach it to this object.
101   *
102   * \tparam TAnnotation Annotation type to create and attach (type is used for lookup later)
103   * \param constructor_arguments Constructor arguments that are forwarded to TAnnotation constructor
104   * \return Annotation that was created. It will be automatically deleted when this tAnnotatable object is.
105   */
106  template <typename TAnnotation, typename... TArguments>
107  TAnnotation& EmplaceAnnotation(TArguments && ... constructor_arguments)
108  {
109    TAnnotation* created_annotation = new TAnnotation(constructor_arguments...);
110    AddAnnotation(*created_annotation);
111    return *created_annotation;
112  }
113
114  /*!
115   * Obtain annotation of specified type attached to this object
116   *
117   * \tparam TAnnotation Annotation type (with which annotation was added)
118   * \return Annotation. Null if this object has no annotation of specified type attached.
119   */
120  template <typename TAnnotation>
121  TAnnotation* GetAnnotation() const
122  {
123    return static_cast<TAnnotation*>(internal::tAnnotatableImplementation::GetAnnotation(typeid(TAnnotation).name()));
124  }
125
126  /*!
127   * Obtain annotation of specified type attached to this object
128   *
129   * \param rtti_name Annotation type name as obtained from C++ RTTI (typeid(...).name())
130   * \return Annotation. Null if this object has no annotation of specified type attached.
131   */
132  tAnnotation* GetAnnotation(const char* rtti_name) const
133  {
134    return internal::tAnnotatableImplementation::GetAnnotation(rtti_name);
135  }
136
137//----------------------------------------------------------------------
138// Protected methods
139//----------------------------------------------------------------------
140protected:
141
142  /*!
143   * Notify annotations that object is to be deleted
144   */
145  void NotifyAnnotationsDelete()
146  {
147    internal::tAnnotatableImplementation::NotifyAnnotationsDelete();
148  }
149
150  /*!
151   * Notify annotations that object has been initialized
152   */
153  void NotifyAnnotationsInitialized()
154  {
155    internal::tAnnotatableImplementation::NotifyAnnotationsInitialized();
156  }
157
158};
159
160//----------------------------------------------------------------------
161// End of namespace declaration
162//----------------------------------------------------------------------
163}
164}
165
166
167#endif
Note: See TracBrowser for help on using the repository browser.