VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
utils.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
10 #ifndef _UGCS_VSM_UTILS_H_
11 #define _UGCS_VSM_UTILS_H_
12 
13 #include <ugcs/vsm/debug.h>
14 #include <ugcs/vsm/exception.h>
15 
16 #include <regex>
17 #include <type_traits>
18 #include <memory>
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 ugcs {
54 namespace vsm {
55 
56 namespace internal {
57 
61 template <class T, class Shared_base = void>
63 public:
65  typedef Shared_base Shared_base_type;
66 
68  static std::shared_ptr<T>
69  Get(T *this_ptr)
70  {
71  return std::dynamic_pointer_cast<T>(this_ptr->shared_from_this());
72  }
73 };
74 
76 template <class T>
77 class Shared_getter<T, void> {
78 public:
80  static std::shared_ptr<T>
81  Get(T *this_ptr __UNUSED)
82  {
83  /* Should not be called. Missing shared base class argument for
84  * DEFINE_COMMON_CLASS if fired.
85  */
86  ASSERT(false);
87  return std::shared_ptr<T>();
88  }
89 };
90 
94 template <class T>
95 class Shared_getter<T, T> {
96 public:
98  static std::shared_ptr<T>
99  Get(T *this_ptr)
100  {
101  return this_ptr->shared_from_this();
102  }
103 };
104 
105 } /* namespace internal */
106 
112 extern std::regex_constants::syntax_option_type platform_independent_filename_regex_matching_flag;
113 
116 uint32_t Get_application_instance_id();
117 
120 uint64_t
122 
123 //
124 std::string&
125 Ltrim(std::string & str);
126 
127 std::string&
128 Rtrim(std::string & str);
129 
130 std::string&
131 Trim(std::string & str);
132 
133 } /* namespace vsm */
134 } /* namespace ugcs */
135 
136 #endif /* _UGCS_VSM_UTILS_H_ */
Helper class for working with classes which are derived from std::enable_shared_from_this.
Definition: utils.h:62
Debugging and troubleshooting helpers.
Shared_base Shared_base_type
The type of base class.
Definition: utils.h:65
static std::shared_ptr< T > Get(T *this_ptr)
Get shared pointer to derived class.
Definition: utils.h:69
#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
std::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:81
static std::shared_ptr< T > Get(T *this_ptr)
Get shared pointer to itself.
Definition: utils.h:99