context.cpp
Go to the documentation of this file.
00001 // License: Apache 2.0. See LICENSE file in root directory.
00002 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
00003 
00004 #include <mutex>
00005 #include <array>
00006 #include <string>
00007 
00008 #include "r200.h"
00009 #include "f200.h"
00010 #include "sr300.h"
00011 #include "zr300.h"
00012 #include "uvc.h"
00013 #include "context.h"
00014 
00015 
00016 #define constexpr_support 1
00017 
00018 #ifdef _MSC_VER
00019 #if (_MSC_VER <= 1800) // constexpr is not supported in MSVC2013
00020 #undef constexpr_support
00021 #define constexpr_support 0
00022 #endif
00023 #endif
00024 
00025 #if (constexpr_support == 1)
00026 template<unsigned... Is> struct seq{};
00027 template<unsigned N, unsigned... Is>
00028 struct gen_seq : gen_seq<N-1, N-1, Is...>{};
00029 template<unsigned... Is>
00030 struct gen_seq<0, Is...> : seq<Is...>{};
00031 
00032 template<unsigned N1, unsigned... I1, unsigned N2, unsigned... I2>
00033 constexpr std::array<char const, N1+N2-1> concat(char const (&a1)[N1], char const (&a2)[N2], seq<I1...>, seq<I2...>){
00034   return {{ a1[I1]..., a2[I2]... }};
00035 }
00036 
00037 template<unsigned N1, unsigned N2>
00038 constexpr std::array<char const, N1+N2-1> concat(char const (&a1)[N1], char const (&a2)[N2]){
00039   return concat(a1, a2, gen_seq<N1-1>{}, gen_seq<N2>{});
00040 }
00041 
00042 constexpr auto rs_api_version = concat("VERSION: ",RS_API_VERSION_STR);
00043 
00044 #else    // manual version tracking is required
00045 static const std::string rs_api_version("VERSION: 1.11.1");
00046 
00047 #endif
00048 
00049 bool is_compatible(std::shared_ptr<rs_device> device)
00050 {
00051     return device->supports(RS_CAPABILITIES_ENUMERATION);
00052 }
00053 
00054 rs_context_base::rs_context_base()
00055 {
00056     context = rsimpl::uvc::create_context();
00057 
00058     for(auto device : query_devices(context))
00059     {
00060         LOG_INFO("UVC device detected with VID = 0x" << std::hex << get_vendor_id(*device) << " PID = 0x" << get_product_id(*device));
00061 
00062         if (get_vendor_id(*device) != VID_INTEL_CAMERA)
00063             continue;
00064 
00065         std::shared_ptr<rs_device> rs_dev;
00066 
00067         switch(get_product_id(*device))
00068         {
00069             case R200_PRODUCT_ID:  rs_dev = rsimpl::make_r200_device(device); break;
00070             case LR200_PRODUCT_ID: rs_dev = rsimpl::make_lr200_device(device); break;
00071             case ZR300_PRODUCT_ID: rs_dev = rsimpl::make_zr300_device(device); break;
00072             case F200_PRODUCT_ID:  rs_dev = rsimpl::make_f200_device(device); break;
00073             case SR300_PRODUCT_ID: rs_dev = rsimpl::make_sr300_device(device); break;
00074         }
00075 
00076         if (rs_dev && is_compatible(rs_dev))
00077         {
00078             devices.push_back(rs_dev);
00079         }
00080         else
00081         {
00082             LOG_ERROR("Device is not supported by librealsense!");
00083         }
00084     }
00085 }
00086 
00087 // Enforce singleton semantics on rs_context
00088 rs_context* rs_context_base::instance = nullptr;
00089 int rs_context_base::ref_count = 0;
00090 std::mutex rs_context_base::instance_lock;
00091 std::string rs_context_base::api_version = std::string(rs_api_version.begin(),rs_api_version.end());
00092 
00093 rs_context* rs_context_base::acquire_instance()
00094 {
00095     std::lock_guard<std::mutex> lock(instance_lock);
00096     if (ref_count++ == 0)
00097     {
00098         instance = new rs_context_base();
00099     }
00100     return instance;
00101 }
00102 
00103 void rs_context_base::release_instance()
00104 {
00105     std::lock_guard<std::mutex> lock(instance_lock);
00106     if (--ref_count == 0)
00107     {
00108         delete instance;
00109     }
00110 }
00111 
00112 rs_context_base::~rs_context_base()
00113 {
00114     assert(ref_count == 0);
00115 }
00116 
00117 size_t rs_context_base::get_device_count() const
00118 {
00119     return devices.size();
00120 }
00121 
00122 rs_device* rs_context_base::get_device(int index) const
00123 {
00124     return devices[index].get();
00125 }


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Tue Jun 25 2019 19:54:38