VSM C++ SDK
Vehicle Specific Modules SDK
utils.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
10 #ifndef _UTILS_H_
11 #define _UTILS_H_
12 
13 #include <memory>
14 #include <ugcs/vsm/debug.h>
15 #include <ugcs/vsm/exception.h>
16 
17 #include <type_traits>
18 #include <ugcs/vsm/regex.h>
19 
25 #define DEFINE_COMMON_CLASS(__class_name, ...) \
26  public: \
27  \
28  \
29  typedef std::shared_ptr<__class_name> Ptr; \
30  \
31  \
32  typedef std::weak_ptr<__class_name> Weak_ptr; \
33  \
34  \
35  template <typename... Args> \
36  static Ptr \
37  Create(Args &&... args) \
38  { \
39  return std::make_shared<__class_name>(std::forward<Args>(args)...); \
40  } \
41  \
42  private: \
43  typedef ugcs::vsm::internal::Shared_getter<__class_name, ## __VA_ARGS__> Shared_getter; \
44  friend Shared_getter; \
45  \
46  \
47  Ptr \
48  Shared_from_this() \
49  { \
50  return Shared_getter::Get(this); \
51  }
52 
53 namespace std {
56 template<typename T, typename ...Args>
57 std::unique_ptr<T> make_unique( Args&& ...args )
58 {
59  return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
60 }
61 } /* namespace std */
62 
63 namespace ugcs {
64 namespace vsm {
65 
66 namespace internal {
67 
71 template <class T, class Shared_base = void>
73 public:
75  typedef Shared_base Shared_base_type;
76 
78  static std::shared_ptr<T>
79  Get(T *this_ptr)
80  {
81  return std::dynamic_pointer_cast<T>(this_ptr->shared_from_this());
82  }
83 };
84 
86 template <class T>
87 class Shared_getter<T, void> {
88 public:
90  static std::shared_ptr<T>
91  Get(T *this_ptr __UNUSED)
92  {
93  /* Should not be called. Missing shared base class argument for
94  * DEFINE_COMMON_CLASS if fired.
95  */
96  ASSERT(false);
97  return std::shared_ptr<T>();
98  }
99 };
100 
104 template <class T>
105 class Shared_getter<T, T> {
106 public:
108  static std::shared_ptr<T>
109  Get(T *this_ptr)
110  {
111  return this_ptr->shared_from_this();
112  }
113 };
114 
115 } /* namespace internal */
116 
122 extern regex::regex_constants::syntax_option_type platform_independent_filename_regex_matching_flag;
123 
126 uint32_t Get_application_instance_id();
127 
130 uint64_t
132 
133 } /* namespace vsm */
134 } /* namespace ugcs */
135 
136 #endif /* _UTILS_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Helper class for working with classes which are derived from std::enable_shared_from_this.
Definition: utils.h:72
std::unique_ptr< T > make_unique(Args &&...args)
Method for creating non sharable objects, until this method is added to C++14.
Definition: utils.h:57
Debugging and troubleshooting helpers.
Shared_base Shared_base_type
The type of base class.
Definition: utils.h:75
STL namespace.
static std::shared_ptr< T > Get(T *this_ptr)
Get shared pointer to derived class.
Definition: utils.h:79
#define ASSERT(x)
No action in release.
Definition: debug.h:68
VSM exceptions definition.
#define __UNUSED
Use with unused arguments if you like to declare that it is not (yet) used a the function.
Definition: defs.h:30
regex::regex_constants::syntax_option_type platform_independent_filename_regex_matching_flag
This flag is used to support case insensitive file systems in regular expression matching.
uint64_t Get_random_seed()
Return a random number on each call.
uint32_t Get_application_instance_id()
Return instance id which is randomly generated on the first call.
static std::shared_ptr< T > Get(T *this_ptr __attribute__((unused)))
Get which leads to runtime failure.
Definition: utils.h:91
static std::shared_ptr< T > Get(T *this_ptr)
Get shared pointer to itself.
Definition: utils.h:109