Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Map_Test.h
blobc896c52e71aab8a81244f0dc37ff9a3230be84c5
1 /* -*- C++ -*- */
3 //=============================================================================
4 /**
5 * @file Map_Test.h
7 * This file has the class definitions needed for template generation in
8 * Map_Test.cpp. They have to be in a separate file so AIX xlC can
9 * find them at auto-instantiate time.
11 * @author Irfan Pyarali <irfan@cs.wustl.edu>
13 //=============================================================================
16 #ifndef ACE_TESTS_MAP_TEST_H
17 #define ACE_TESTS_MAP_TEST_H
19 #include "ace/OS_NS_string.h"
21 #if !defined (ACE_LACKS_PRAGMA_ONCE)
22 # pragma once
23 #endif /* ACE_LACKS_PRAGMA_ONCE */
25 #include "ace/Active_Map_Manager.h"
26 #include "ace/Containers.h"
28 // Key data type.
29 typedef ACE_Array<char> KEY;
31 /**
32 * @class Key_Generator
34 * @brief Defines a key generator.
36 * This class is used in adapters of maps that do not produce keys.
38 class Key_Generator
40 public:
42 Key_Generator (void)
43 : counter_ (0)
47 int operator() (KEY &key)
49 // Keep original information in the key intact.
50 size_t original_size = key.size ();
52 // Size of this counter key.
53 const size_t counter_key_size = sizeof this->counter_;
55 // Resize to accommodate both the original data and the new key.
56 key.size (counter_key_size + original_size);
58 // Add new key data.
59 ACE_OS::memcpy (&key[original_size],
60 &++this->counter_,
61 counter_key_size);
63 // Success.
64 return 0;
67 private:
68 u_long counter_;
71 class Hash_Key
73 public:
74 u_long operator () (const KEY &key) const
76 // Recover system generated part of key.
77 u_long value;
78 size_t counter_key_size = sizeof (u_long);
80 // Copy system key from user key.
81 ACE_OS::memcpy (&value,
82 &key[key.size () - counter_key_size],
83 sizeof value);
85 // Return the system key value as the hash value.
86 return value;
90 class Key_Adapter
92 public:
94 int encode (const KEY &original_key,
95 const ACE_Active_Map_Manager_Key &active_key,
96 KEY &modified_key)
98 // Keep original information in the key intact.
99 modified_key = original_key;
100 size_t original_size = modified_key.size ();
102 // Size of active key.
103 size_t active_key_size = active_key.size ();
105 // Resize to accommodate both the original data and the new active key.
106 modified_key.size (active_key_size + original_size);
108 // Copy active key data into user key.
109 active_key.encode (&modified_key[original_size]);
111 // Success.
112 return 0;
115 int decode (const KEY &modified_key,
116 ACE_Active_Map_Manager_Key &active_key)
118 // Read the active key data from the back of the key.
119 size_t active_key_size = active_key.size ();
120 size_t original_size = modified_key.size () - active_key_size;
122 // Read off value of index and generation.
123 active_key.decode (&modified_key[original_size]);
125 // Success.
126 return 0;
129 int decode (const KEY &modified_key,
130 KEY &original_key)
132 // Read the original user key data from the front of the
133 // modified key.
134 size_t active_key_size = ACE_Active_Map_Manager_Key::size ();
136 // Copy all the data.
137 original_key = modified_key;
139 // Resize to ignore active key data.
140 original_key.size (original_key.size () - active_key_size);
142 // Success.
143 return 0;
147 #endif /* ACE_TESTS_MAP_TEST_H */