Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Key.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file utility/keys/Key.hh
11 /// @brief Hidden index key interface class
12 /// @author Stuart G. Mentzer (Stuart_Mentzer@objexx.com)
13 ///
14 /// @note
15 /// @li Interface provides common base class for covariant return in hierarchies
16 /// @li Derived classes specify the friend class(es) that can access the index
17 /// @li Index can be ignored if not used as key/index into a container
18 /// @li Can safely derive from concrete Key types as long as fields aren't added
19 /// @li Can derive privately from an Key to share the index set of another Key type
20 /// without allowing convertibility (for type safety)
21 
22 
23 #ifndef INCLUDED_utility_keys_Key_hh
24 #define INCLUDED_utility_keys_Key_hh
25 
26 
27 // Unit headers
28 #include <utility/keys/Key.fwd.hh>
29 
30 // Package headers
34 
35 // C++ headers
36 #include <cstddef>
37 #include <string>
38 
39 
40 namespace utility {
41 namespace keys {
42 
43 
44 /// @brief Hidden index key interface class
45 class Key
46 {
47 
48 
49 public: // Types
50 
51 
52  // STL/boost style
53  typedef std::size_t index_type;
54  typedef std::size_t size_type;
55 
56  // Project style
57  typedef std::size_t Index;
58  typedef std::size_t Size;
59 
60 
61 private: // Friends
62 
63 
64  template< typename O, typename S, typename C > friend class AutoKey;
65  template< typename O, typename S, typename C > friend class UserKey;
66  template< typename T, typename U > friend class KeyLess;
67  template< typename T, typename U > friend class PointerKeyLess;
68 
69 
70 protected: // Creation
71 
72 
73  /// @brief Default constructor
74  inline
75  Key()
76  {}
77 
78 
79  /// @brief Copy constructor
80  inline
81  Key( Key const & )
82  {}
83 
84 
85 public: // Creation
86 
87 
88  /// @brief Clone this
89  virtual
90  Key *
91  clone() const = 0;
92 
93 
94  /// @brief Destructor
95  inline
96  virtual
97  ~Key()
98  {}
99 
100 
101 public: // Assignment
102 
103 
104  /// @brief Copy assignment
105  inline
106  Key &
107  operator =( Key const & key )
108  {
109  if ( this != &key ) {
110  assign_Key( key );
111  }
112  return *this;
113  }
114 
115 
116 protected: // Assignment
117 
118 
119  /// @brief Key assignment
120  virtual
121  void
122  assign_Key( Key const & key ) = 0;
123 
124 
125 public: // Properties
126 
127 
128  /// @brief ID
129  virtual
130  std::string const &
131  id() const = 0;
132 
133 
134  /// @brief ID
135  virtual
136  std::string &
137  id() = 0;
138 
139 
140  /// @brief ID assignment
141  virtual
142  Key &
143  id( std::string const & id_a ) = 0;
144 
145 
146  /// @brief Identifier
147  virtual
148  std::string const &
149  identifier() const = 0;
150 
151 
152  /// @brief Identifier
153  virtual
154  std::string &
155  identifier() = 0;
156 
157 
158  /// @brief Identifier assignment
159  virtual
160  Key &
161  identifier( std::string const & identifier_a ) = 0;
162 
163 
164  /// @brief Code
165  virtual
166  std::string const &
167  code() const = 0;
168 
169 
170  /// @brief Code
171  virtual
172  std::string &
173  code() = 0;
174 
175 
176  /// @brief Code assignment
177  virtual
178  Key &
179  code( std::string const & code_a ) = 0;
180 
181 
182  /// @brief Index
183  /// @note Only for use as an optimization: DO NOT WRITE CODE DEPENDING ON THE SPECIFIC INDEX VALUE!
184  virtual
185  Index
186  private_index() const = 0;
187 
188 
189 public: // Comparison
190 
191 
192  /// @brief Key == Key
193  /// @note TYpe and index-based polymorphic equality
194  friend
195  inline
196  bool
197  operator ==( Key const & a, Key const & b )
198  {
199  return a.equals( b );
200  }
201 
202 
203  /// @brief Key != Key
204  /// @note TYpe and index-based polymorphic equality
205  friend
206  inline
207  bool
208  operator !=( Key const & a, Key const & b )
209  {
210  return ! a.equals( b );
211  }
212 
213 
214  /// @brief Key < Key
215  /// @note Index-based ordering
216  /// @note Needed for use as key in associative containers
217  friend
218  inline
219  bool
220  operator <( Key const & a, Key const & b )
221  {
222  return a.less_than( b );
223  }
224 
225 
226  /// @brief Key <= Key
227  /// @note Index-based ordering
228  /// @note Needed for use as key in associative containers
229  friend
230  inline
231  bool
232  operator <=( Key const & a, Key const & b )
233  {
234  return ( ( a.less_than( b ) ) || ( a.equals( b ) ) );
235  }
236 
237 
238  /// @brief Key >= Key
239  /// @note Index-based ordering
240  /// @note Needed for use as key in associative containers
241  friend
242  inline
243  bool
244  operator >=( Key const & a, Key const & b )
245  {
246  return ( ( b.less_than( a ) ) || ( a.equals( b ) ) );
247  }
248 
249 
250  /// @brief Key > Key
251  /// @note Index-based ordering
252  /// @note Needed for use as key in associative containers
253  friend
254  inline
255  bool
256  operator >( Key const & a, Key const & b )
257  {
258  return b.less_than( a );
259  }
260 
261 
262  /// @brief Are Keys of Comparable Types?
263  friend
264  inline
265  bool
266  comparable( Key const & a, Key const & b )
267  {
268  return a.comparable( b );
269  }
270 
271 
272 protected: // Conversion
273 
274 
275  /// @brief Index value conversion
276  /// @note A pure virtual version of this slows down key lookup operations
277  /// because it prevents inlining for derived key types
278  inline
279  operator Index() const
280  {
281  return index();
282  }
283 
284 
285 protected: // Properties
286 
287 
288  /// @brief Index
289  virtual
290  Index
291  index() const = 0;
292 
293 
294  /// @brief Index
295  virtual
296  Index &
297  index() = 0;
298 
299 
300  /// @brief Index assignment
301  virtual
302  Key &
303  index( Index const index_a ) = 0;
304 
305 
306 protected: // Comparison
307 
308 
309  /// @brief Equal to a Key?
310  virtual
311  bool
312  equals( Key const & key ) const = 0;
313 
314 
315  /// @brief Less than a Key?
316  virtual
317  bool
318  less_than( Key const & key ) const = 0;
319 
320 
321  /// @brief Comparable to a Key?
322  virtual
323  bool
324  comparable( Key const & key ) const = 0;
325 
326 
327 }; // Key
328 
329 
330 // Friend function namespace declarations
331 
332 
333 /// @brief Key == Key
334 bool
335 operator ==( Key const & a, Key const & b );
336 
337 
338 /// @brief Key != Key
339 bool
340 operator !=( Key const & a, Key const & b );
341 
342 
343 /// @brief Key < Key
344 bool
345 operator <( Key const & a, Key const & b );
346 
347 
348 /// @brief Key <= Key
349 bool
350 operator <=( Key const & a, Key const & b );
351 
352 
353 /// @brief Key >= Key
354 bool
355 operator >=( Key const & a, Key const & b );
356 
357 
358 /// @brief Key > Key
359 bool
360 operator >( Key const & a, Key const & b );
361 
362 
363 /// @brief Are Keys of Comparable Types?
364 bool
365 comparable( Key const & a, Key const & b );
366 
367 
368 } // namespace keys
369 } // namespace utility
370 
371 
372 #endif // INCLUDED_utility_keys_Key_HH
bool operator!=(AutoKey< O, S, C > const &a, AutoKey< O, S, C > const &b)
AutoKey != AutoKey.
Definition: AutoKey.hh:337
friend bool operator>=(Key const &a, Key const &b)
Key >= Key.
Definition: Key.hh:244
bool operator>=(AutoKey< O, S, C > const &a, AutoKey< O, S, C > const &b)
AutoKey >= AutoKey.
Definition: AutoKey.hh:371
Automatic hidden index key abstract base class.
Definition: AutoKey.fwd.hh:30
User-created hidden index key abstract base class.
Definition: UserKey.fwd.hh:29
virtual Key * clone() const =0
Clone this.
virtual bool equals(Key const &key) const =0
Equal to a Key?
std::size_t Size
Definition: Key.hh:58
bool operator<=(AutoKey< O, S, C > const &a, AutoKey< O, S, C > const &b)
AutoKey <= AutoKey.
Definition: AutoKey.hh:360
bool operator<(AutoKey< O, S, C > const &a, AutoKey< O, S, C > const &b)
AutoKey < AutoKey.
Definition: AutoKey.hh:349
std::size_t index_type
Definition: Key.hh:53
virtual std::string const & code() const =0
Code.
friend bool operator==(Key const &a, Key const &b)
Key == Key.
Definition: Key.hh:197
virtual Index index() const =0
Index.
virtual std::string const & id() const =0
ID.
bool operator==(AutoKey< O, S, C > const &a, AutoKey< O, S, C > const &b)
AutoKey == AutoKey.
Definition: AutoKey.hh:326
friend bool operator!=(Key const &a, Key const &b)
Key != Key.
Definition: Key.hh:208
bool operator>(AutoKey< O, S, C > const &a, AutoKey< O, S, C > const &b)
AutoKey > AutoKey.
Definition: AutoKey.hh:382
utility::keys::lookup::key< KeyType > const key
utility::keys::KeyLess forward declarations
utility::keys::UserKey forward declarations
utility::keys::Key forward declarations
Key member comparison functor template.
Definition: KeyLess.fwd.hh:24
std::size_t size_type
Definition: Key.hh:54
bool comparable(Key const &a, Key const &b)
Are Keys of Comparable Types?
Definition: Key.hh:266
Key member comparison functor template for pointers.
Definition: KeyLess.fwd.hh:25
virtual void assign_Key(Key const &key)=0
Key assignment.
virtual std::string const & identifier() const =0
Identifier.
virtual ~Key()
Destructor.
Definition: Key.hh:97
std::size_t Index
Definition: Key.hh:57
virtual bool less_than(Key const &key) const =0
Less than a Key?
Key()
Default constructor.
Definition: Key.hh:75
friend bool comparable(Key const &a, Key const &b)
Are Keys of Comparable Types?
Definition: Key.hh:266
Key & operator=(Key const &key)
Copy assignment.
Definition: Key.hh:107
Key(Key const &)
Copy constructor.
Definition: Key.hh:81
Hidden index key interface class.
Definition: Key.hh:45
friend bool operator>(Key const &a, Key const &b)
Key > Key.
Definition: Key.hh:256
virtual Index private_index() const =0
Index.
friend bool operator<(Key const &a, Key const &b)
Key < Key.
Definition: Key.hh:220
utility::keys::AutoKey forward declarations
friend bool operator<=(Key const &a, Key const &b)
Key <= Key.
Definition: Key.hh:232