Alchemy  1.0
A framework to robustly process network messages and structured data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
storage_policy.h
Go to the documentation of this file.
1 /// @file Hg/storage_policy.h
2 ///
3 /// Manages allocation and access behaviors to memory buffers.
4 ///
5 /// The storage policy is an object that manages the aspects of allocation,
6 /// read and write access to a particular memory buffer. Custom storage policies
7 /// can control access to a buffer through its specialization of this policy.
8 ///
9 /// Here is a list of *potential* implementations of the StoragePolicy:
10 ///
11 /// - Fixed Array Memory Policy
12 /// - Hardware Memory Map Policy
13 /// - Read-Only Memory Policy
14 ///
15 /// The MIT License(MIT)
16 ///
17 /// @copyright 2014 Paul M Watt
18 ///
19 /// Permission is hereby granted, free of charge, to any person obtaining a copy
20 /// of this software and associated documentation files(the "Software"), to deal
21 /// in the Software without restriction, including without limitation the rights
22 /// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
23 /// copies of the Software, and to permit persons to whom the Software is
24 /// furnished to do so, subject to the following conditions :
25 ///
26 /// The above copyright notice and this permission notice shall be included in
27 /// all copies or substantial portions of the Software.
28 ///
29 /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
32 /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34 /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35 /// THE SOFTWARE.
36 ///
37 // ****************************************************************************
38 #ifndef STORAGE_POLICY_H_INCLUDED
39 #define STORAGE_POLICY_H_INCLUDED
40 // Includes *******************************************************************
41 #include <memory>
42 
43 namespace Hg
44 {
45 
46 // Forward Declarations *******************************************************
47 #if defined(_WIN32) && defined(_HAS_TR1)
48 using std::tr1::shared_ptr;
49 #else
50 using std::shared_ptr;
51 #endif
52 
53 // ****************************************************************************
54 /// Storage Policy implementation for contiguous memory buffers.
55 ///
56 /// @tparam IsBufferedT Indicates if the StoragePolicy desires data to
57 /// be buffered, or accessed on demand.
58 ///
59 /// Note: The policy itself is not responsible, and does not buffer the data.
60 /// It is an indication to users of the policy, for how the best way to
61 /// access the storage memory.
62 ///
63 /// For example, storage policies developed to interface directly with
64 /// hardware mapped memory access would indicate not to be buffered.
65 /// However, a network packet buffer, which can vary in size between
66 /// calls would indicate buffering is preferred.
67 ///
68 template <bool IsBufferedT>
70  : std::integral_constant<bool, IsBufferedT>
71 {
72  typedef unsigned char data_type;
73  typedef data_type* pointer;
74  typedef const data_type* const_pointer;
75  typedef std::shared_ptr<data_type> s_pointer;
76  typedef std::weak_ptr<data_type> w_pointer;
77 
78  // **************************************************************************
79  /// Allocates a new storage buffer for this policy.
80  ///
81  /// @param size The number of bytes requested for the allocation.
82  ///
83  /// @return A shared_ptr that contains the allocated container object.
84  /// If the allocation failed, the return value will be invalid.
85  ///
86  static
87  s_pointer allocate(size_t size)
88  {
89  s_pointer spBuffer(new(std::nothrow) data_type[size]);
90  return spBuffer;
91  }
92 
93  // **************************************************************************
94  /// Releases memory buffers allocated through this policy.
95  ///
96  /// @param size The number of bytes requested for the allocation.
97  ///
98  static
99  void deallocate(s_pointer &spBuffer)
100  {
101  // No real action for this storage_policy.
102  // Clear the pointer anyway.
103  spBuffer.reset();
104  }
105 
106  // **************************************************************************
107  /// Performs a storage read operation for this policy.
108  ///
109  /// @param sp_buffer A shared pointer to a buffer previously
110  /// allocated by this storage policies Allocate
111  /// member function.
112  /// @param p_storage The source memory buffer to read from.
113  /// @param size The number of elements read from the buffer.
114  /// @param offset A starting offset from the beginning of the
115  /// specified read/write buffer.
116  ///
117  /// @return This function will always return true.
118  /// Other policy implementations may need to indicate failure.
119  ///
120  static
121  bool read ( const_pointer pBuffer,
122  void* pStorage,
123  size_t size,
124  std::ptrdiff_t offset)
125  {
126  ::memcpy( pStorage,
127  pBuffer + offset,
128  size);
129  return true;
130  }
131 
132  // **************************************************************************
133  /// Performs a storage write operation for this policy.
134  ///
135  /// @param sp_buffer A shared pointer to a buffer previously
136  /// allocated by this storage policies Allocate
137  /// member function.
138  /// @param p_storage The source memory buffer to supply the data
139  /// written into sp_buffer;
140  /// @param size The number of elements write into the buffer.
141  /// @param offset A starting offset from the beginning of the
142  /// specified read/write buffer.
143  ///
144  /// @return This function will always return true.
145  /// Other policy implementations may need to indicate failure.
146  ///
147  static
148  bool write( pointer pBuffer,
149  const void* pStorage,
150  size_t size,
151  std::ptrdiff_t offset)
152  {
153  ::memcpy( pBuffer + offset,
154  pStorage,
155  size);
156  return true;
157  }
158 };
159 
160 typedef StoragePolicy<false> DirectMemStoragePolicy;
161 typedef StoragePolicy<true> BufferedStoragePolicy;
162 
163 } // namespace Hg
164 
165 #endif