//
// © 2018-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

#import "IoTResourceAttributes.h"
#import "IoTProperties.h"
#import "IoTRepresentationUtils.h"
#import "IoTWrappedRepresentation.h"
#import "IoTGlobals.h"

#include <OCApi.h>
#include <OCPlatform.h>

#define GETTER_SYNCHRONIZED(type,property) \
- (type)property \
{ \
    dispatch_assert_queue_not(self.syncQueue); \
    __block type ret; \
    dispatch_sync(self.syncQueue, ^{ \
        ret = self->_##property; \
    }); \
    return ret; \
}

#define SETTER_SYNCHRONIZED(type,method,property,assigner) \
- (void)method:(type)property \
{ \
    dispatch_assert_queue_not(self.syncQueue); \
    makeWeak(self) \
    dispatch_barrier_async(self.syncQueue, ^{ \
        makeStrongAndReturnIfNil(self) \
        self->_##property = assigner; \
        self->_ ##property ## Changed = YES; \
    }); \
}

NSArray *stringArrayFromVector (const OC::OCRepresentation rep, const char * key)
{
    std::vector<std::string> vec =  rep[key];
    NSMutableArray *strings = [[NSMutableArray alloc] init];
    for (auto cppStr: vec) {
        [strings addObject:[NSString stringWithUTF8String:cppStr.c_str()]];
    }
    return [strings copy];
}

std::vector<std::string> vectorFromStringArray(NSArray<NSString *> *strings)
{
    std::vector<std::string> vec;
    for (NSString *str in strings) {
        vec.emplace_back(std::string([str UTF8String]));
    }
    return vec;
}

NSArray *intArrayFromVector (const OC::OCRepresentation rep, const char * key)
{
    std::vector<int> vec =  rep[key];
    NSMutableArray *ints = [[NSMutableArray alloc] init];
    for (auto cppInt: vec) {
        [ints addObject:[NSNumber numberWithInt:cppInt]];
    }
    return [ints copy];
}

std::vector<int> vectorFromIntArray(NSArray<NSNumber *> *ints)
{
    std::vector<int> vec;
    for (NSNumber *intNumber in ints) {
        vec.emplace_back([intNumber intValue]);
    }
    return vec;
}

{% for schema_data in schema_objects %}
{% include "resource_attribute_class.mm.jinja2" %}

{% endfor %}
