Thea
NamedObject.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2009
12 //
13 //============================================================================
14 
15 #ifndef __Thea_NamedObject_hpp__
16 #define __Thea_NamedObject_hpp__
17 
18 #include "Common.hpp"
19 
20 namespace Thea {
21 
23 class THEA_API INamedObject
24 {
25  public:
27  virtual ~INamedObject() = 0;
28 
30  virtual char const * THEA_ICALL getName() const = 0;
31 
37  virtual int8 THEA_ICALL setName(char const * s) = 0;
38 
39 }; // class INamedObject
40 
42 
58 class THEA_API NamedObject : public virtual INamedObject
59 {
60  public:
61  THEA_DECL_SMART_POINTERS(NamedObject)
62 
63 
65 
67  NamedObject(std::string const & name_) : name(name_) {}
68 
70  NamedObject(NamedObject const & src): name(src.name) {}
71 
74 
76  NamedObject & operator=(NamedObject const & src) { name = src.name; return *this; }
77 
78  char const * THEA_ICALL getName() const { return name.c_str(); }
79 
86  int8 THEA_ICALL setName(char const * s) { name = s; return true; }
87 
94  virtual int8 setName(std::string const & s) { name = s; return true; }
95 
96  protected:
98  std::string const & getNameStr() const { return name; }
99 
100  private:
101  std::string name;
102 
103 }; // class NamedObject
104 
106 inline std::ostream &
107 operator<<(std::ostream & os, NamedObject const & obj)
108 {
109  std::string cls = getTypeName(obj);
110 
111  // Remove template parameters
112  size_t last = cls.find('<');
113 
114  // Remove namespace qualifiers
115  size_t first = cls.rfind(':', last);
116 
117  if (last == std::string::npos) last = cls.length();
118  first = (first == std::string::npos ? 0 : first + 1);
119 
120  return os << cls.substr(first, last - first) << " '" << obj.getName() << '\'';
121 }
122 
123 } // namespace Thea
124 
125 THEA_DECL_EXTERN_SMART_POINTERS(Thea::NamedObject)
126 
127 #endif
NamedObject(NamedObject const &src)
Copy constructor.
Definition: NamedObject.hpp:70
std::string getTypeName()
Get the name of a type, ignoring reference and cv-qualifiers.
Definition: Common.hpp:107
char const * getName() const
Get the name of the object.
Definition: NamedObject.hpp:78
Root namespace for the Thea library.
NamedObject(std::string const &name_)
Initializing constructor.
Definition: NamedObject.hpp:67
An object wrapping a name string.
Definition: NamedObject.hpp:58
Interface for an object that has a name string.
Definition: NamedObject.hpp:23
virtual ~INamedObject()=0
Destructor.
Definition: NamedObject.hpp:41
std::string const & getNameStr() const
Access the name string directly, for efficiency.
Definition: NamedObject.hpp:98
int8 setName(char const *s)
Set the name of the object from a C-style string.
Definition: NamedObject.hpp:86
NamedObject & operator=(NamedObject const &src)
Assignment operator.
Definition: NamedObject.hpp:76
virtual int8 setName(std::string const &s)
Set the name of the object from a std::string.
Definition: NamedObject.hpp:94
std::ostream & operator<<(std::ostream &os, AffineTransformN< N, T > const &tr)
Pipe a textual representation of an affine transform to a std::ostream.
~NamedObject()
Destructor.
Definition: NamedObject.hpp:73
#define THEA_ICALL
Calling convention for member functions of abstract interface classes (for talking to shared librarie...
Definition: Platform.hpp:115