Alchemy  1.0
A framework to robustly process network messages and structured data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
static_storage_policy.h
Go to the documentation of this file.
1 /// @file Hg/static_storage_policy.h
2 ///
3 /// Manages allocation and access behaviors to fixed-size memory buffers.
4 ///
5 /// The MIT License(MIT)
6 ///
7 /// @copyright 2014 Paul M Watt
8 ///
9 // ****************************************************************************
10 #ifndef STATIC_STORAGE_POLICY_H_INCLUDED
11 #define STATIC_STORAGE_POLICY_H_INCLUDED
12 // Includes *******************************************************************
13 #include <Hg/storage_policy.h>
14 
15 namespace Hg
16 {
17 
18 // ****************************************************************************
19 /// Storage Policy implementation for fixed-size memory buffers.
20 ///
21 /// @tparam IsBufferedT Indicates if the StoragePolicy desires data to
22 /// be buffered, or accessed on demand.
23 ///
24 /// Note: The policy itself is not responsible, and does not buffer the data.
25 /// It is an indication to users of the policy, for how the best way to
26 /// access the storage memory.
27 ///
28 /// For example, storage policies developed to interface directly with
29 /// hardware mapped memory access would indicate not to be buffered.
30 /// However, a network packet buffer, which can vary in size between
31 /// calls would indicate buffering is preferred.
32 ///
33 template <bool IsBufferedT>
35  : std::integral_constant<bool, IsBufferedT>
36 {
37  typedef unsigned char data_type;
38  typedef data_type* pointer;
39  typedef const data_type* const_pointer;
40  typedef std::shared_ptr<data_type> s_pointer;
41  typedef std::weak_ptr<data_type> w_pointer;
42 
43  // **************************************************************************
44  /// Not Implemented.
45  /// This is a fixed-size static storage policy;
46  ///
47  /// @return 0.
48  ///
49  static
50  s_pointer allocate(size_t size)
51  {
52  return 0;
53  }
54 
55  // **************************************************************************
56  /// Not Implemented.
57  /// This is a fixed-size static storage policy.
58  ///
59  static
60  void deallocate(s_pointer &spBuffer)
61  { }
62 
63  // **************************************************************************
64  /// Performs a storage read operation for this policy.
65  ///
66  /// @param sp_buffer A shared pointer to a buffer previously
67  /// allocated by this storage policies Allocate
68  /// member function.
69  /// @param p_storage The source memory buffer to read from.
70  /// @param size The number of elements read from the buffer.
71  /// @param offset A starting offset from the beginning of the
72  /// specified read/write buffer.
73  ///
74  /// @return This function will always return true.
75  /// Other policy implementations may need to indicate failure.
76  ///
77  static
78  bool read ( const_pointer pBuffer,
79  void* pStorage,
80  size_t size,
81  std::ptrdiff_t offset)
82  {
83  ::memcpy( pStorage,
84  pBuffer + offset,
85  size);
86  return true;
87  }
88 
89  // **************************************************************************
90  /// Performs a storage write operation for this policy.
91  ///
92  /// @param sp_buffer A shared pointer to a buffer previously
93  /// allocated by this storage policies Allocate
94  /// member function.
95  /// @param p_storage The source memory buffer to supply the data
96  /// written into sp_buffer;
97  /// @param size The number of elements write into the buffer.
98  /// @param offset A starting offset from the beginning of the
99  /// specified read/write buffer.
100  ///
101  /// @return This function will always return true.
102  /// Other policy implementations may need to indicate failure.
103  ///
104  static
105  bool write( pointer pBuffer,
106  const void* pStorage,
107  size_t size,
108  std::ptrdiff_t offset)
109  {
110  ::memcpy( pBuffer + offset,
111  pStorage,
112  size);
113  return true;
114  }
115 };
116 
117 typedef StaticStoragePolicy<true> BufferedStaticStoragePolicy;
118 
119 } // namespace Hg
120 
121 #endif