userdata.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 
30 #pragma once
31 
32 #include "../api_core.h"
33 #include <memory>
34 
35 namespace clan
36 {
39 
41 {
42 public:
43  virtual ~UserDataBase() { }
44 };
45 
46 template<typename T>
47 class UserData : public UserDataBase
48 {
49 public:
50  UserData(const std::shared_ptr<T> &data) : data(data) { }
51  std::shared_ptr<T> data;
52 };
53 
55 class CL_API_CORE UserDataOwner
56 {
57 public:
59  : user_data(0)
60  {
61  }
62 
64  {
65  if (user_data)
66  {
67  user_data->~UserDataBase();
68  user_data = 0;
69  }
70  }
71 
72  template<typename T>
73  void set_data(const std::shared_ptr<T> &data)
74  {
75  if (user_data)
76  {
77  user_data->~UserDataBase();
78  user_data = 0;
79  }
80 
81  static_assert(sizeof(UserData<T>) <= 32, "userdata_storage is too small!");
82  user_data = new(userdata_storage) UserData<T>(data);
83  }
84 
85  template<typename T>
86  std::shared_ptr<T> get_data()
87  {
88  UserData<T> *d = dynamic_cast<UserData<T> *>(user_data);
89  if (d)
90  {
91  return d->data;
92  }
93  else
94  {
95  return std::shared_ptr<T>();
96  }
97  }
98 
99 private:
100  UserDataOwner(const UserDataOwner &that); // do not implement
101  UserDataOwner &operator=(const UserDataOwner &that); // do not implement
102 
103  UserDataBase *user_data;
104  char userdata_storage[32];
105 };
106 
107 }
std::shared_ptr< T > get_data()
Definition: userdata.h:86
std::shared_ptr< T > data
Definition: userdata.h:51
void set_data(const std::shared_ptr< T > &data)
Definition: userdata.h:73
Definition: userdata.h:47
virtual ~UserDataBase()
Definition: userdata.h:43
UserData(const std::shared_ptr< T > &data)
Definition: userdata.h:50
Helper class to store any shared_ptr as user data on an object.
Definition: userdata.h:55
~UserDataOwner()
Definition: userdata.h:63
Definition: userdata.h:40
UserDataOwner()
Definition: userdata.h:58