コード例 #1
0
ファイル: PropertyManager.cpp プロジェクト: Daivuk/OakNut
bool onut::PropertyManager::loadPropertiesFromJson(const Json::Value& json)
{
    for (auto &member : json.getMemberNames())
    {
        auto propertyIT = m_properties.find(member);
        if (propertyIT == m_properties.end()) continue;

        auto &propertyLink = propertyIT->second;
        auto &jsonElement = json[member];

        switch (propertyLink.type)
        {
            case ePropertyType::P_BOOL:
                if (jsonElement.isBool())
                {
                    *(bool*)propertyLink.pProperty = jsonElement.asBool();
                }
                break;
            case ePropertyType::P_INT:
                if (jsonElement.isInt())
                {
                    *(int*)propertyLink.pProperty = jsonElement.asInt();
                }
                break;
            case ePropertyType::P_FLOAT:
                if (jsonElement.isDouble())
                {
                    *(float*)propertyLink.pProperty = static_cast<float>(jsonElement.asDouble());
                }
                break;
            case ePropertyType::P_STRING:
                if (jsonElement.isString())
                {
                    *(std::string*)propertyLink.pProperty = jsonElement.asCString();
                }
                break;
            case ePropertyType::P_POINT:
                if (jsonElement.isArray() &&
                    jsonElement.size() == 2)
                {
                    auto &x = jsonElement[0];
                    auto &y = jsonElement[1];
                    if (x.isInt() && y.isInt())
                    {
                        ((glm::tvec2<int>*)propertyLink.pProperty)->x = x.asInt();
                        ((glm::tvec2<int>*)propertyLink.pProperty)->y = y.asInt();
                    }
                }
                break;
            case ePropertyType::P_VEC2:
                if (jsonElement.isArray() &&
                    jsonElement.size() == 2)
                {
                    auto &x = jsonElement[0];
                    auto &y = jsonElement[1];
                    if (x.isDouble() && y.isDouble())
                    {
                        ((glm::vec2*)propertyLink.pProperty)->x = static_cast<float>(x.asDouble());
                        ((glm::vec2*)propertyLink.pProperty)->y = static_cast<float>(y.asDouble());
                    }
                }
                break;
            case ePropertyType::P_VEC3:
                if (jsonElement.isArray() &&
                    jsonElement.size() == 3)
                {
                    auto &x = jsonElement[0];
                    auto &y = jsonElement[1];
                    auto &z = jsonElement[2];
                    if (x.isDouble() && y.isDouble() && z.isDouble())
                    {
                        ((glm::vec3*)propertyLink.pProperty)->x = static_cast<float>(x.asDouble());
                        ((glm::vec3*)propertyLink.pProperty)->y = static_cast<float>(y.asDouble());
                        ((glm::vec3*)propertyLink.pProperty)->z = static_cast<float>(z.asDouble());
                    }
                }
                break;
            case ePropertyType::P_VEC4:
                if (jsonElement.isArray() &&
                    jsonElement.size() == 4)
                {
                    auto &x = jsonElement[0];
                    auto &y = jsonElement[1];
                    auto &z = jsonElement[2];
                    auto &w = jsonElement[3];
                    if (x.isDouble() && y.isDouble() && z.isDouble() && w.isDouble())
                    {
                        ((glm::vec4*)propertyLink.pProperty)->x = static_cast<float>(x.asDouble());
                        ((glm::vec4*)propertyLink.pProperty)->y = static_cast<float>(y.asDouble());
                        ((glm::vec4*)propertyLink.pProperty)->z = static_cast<float>(z.asDouble());
                        ((glm::vec4*)propertyLink.pProperty)->w = static_cast<float>(w.asDouble());
                    }
                }
                break;
            case ePropertyType::P_ENTITY_ARRAY:
                if (jsonElement.isArray())
                {
                    auto pMyEntity = dynamic_cast<Entity*>(this);
                    auto pEntities = static_cast<ObjectVector<Entity>*>(propertyLink.pProperty);
                    for (auto &jsonEntity : jsonElement)
                    {
                        if (jsonEntity.isObject())
                        {
                            if (pMyEntity)
                            {
                                auto pEntity = new onut::Entity();
                                pEntity->retain();
                                pEntity->loadPropertiesFromJson(jsonEntity);
                                if (pEntities == &pMyEntity->getChildren())
                                {
                                    pMyEntity->add(pEntity);
                                }
                                else
                                {
                                    pEntities->push_back(pEntity);
                                }
                                pEntity->release();
                            }
                            else
                            {
                                assert(false); // TODO
                            }
                        }
                    }
                }
                break;
            case ePropertyType::P_COMPONENT_ARRAY:
                if (jsonElement.isArray())
                {
                    auto pComponentManager = dynamic_cast<ComponentManager*>(this);
                    auto pComponents = static_cast<ObjectVector<Component>*>(propertyLink.pProperty);
                    for (auto &jsonComponent : jsonElement)
                    {
                        if (jsonComponent.isArray() &&
                            jsonComponent.size() == 2 &&
                            jsonComponent[0].isString() &&
                            jsonComponent[1].isObject())
                        {
                            auto szComponentClassName = jsonComponent[0].asCString();
                            auto pComponentObject = ObjectLibrary::createObject(szComponentClassName);
                            if (!pComponentObject) continue;
                            auto pComponent = dynamic_cast<onut::Component*>(pComponentObject);
                            if (!pComponent) continue;
                            pComponent->loadPropertiesFromJson(jsonComponent[1]);
                            pComponent->retain();
                            if (pComponentManager && pComponents == &pComponentManager->getComponents())
                            {
                                pComponentManager->addComponent(pComponent);
                                pComponent->release();
                            }
                            else
                            {
                                pComponents->push_back(pComponent);
                            }
                        }
                    }
                }
                break;
            case ePropertyType::P_MATERIAL:
                if (jsonElement.isString())
                {
                    auto ppMaterial = static_cast<onut::Material**>(propertyLink.pProperty);
                    auto pContentManager = Game::getGame()->getComponent<ContentManager>();
                    if (ppMaterial && pContentManager)
                    {
                        auto filename = "assets/" + jsonElement.asString();
                        auto pMaterial = pContentManager->getResource<onut::Material>(filename);
                        if (!pMaterial)
                        {
                            pMaterial = new onut::Material();
                            if (!pMaterial->loadPropertiesFromFile(filename))
                            {
                                pMaterial->release();
                                break;
                            }
                            pContentManager->addResource(filename, pMaterial);
                        }
                        pMaterial->retain();
                        *ppMaterial = pMaterial;
                    }
                }
                break;
            case ePropertyType::P_MESH:
                if (jsonElement.isString())
                {
                    auto ppMesh = static_cast<onut::Mesh**>(propertyLink.pProperty);
                    auto pContentManager = Game::getGame()->getComponent<ContentManager>();
                    if (ppMesh && pContentManager)
                    {
                        auto filename = "assets/" + jsonElement.asString();
                        auto pMesh = pContentManager->getResource<onut::Mesh>(filename);
                        if (!pMesh)
                        {
                            pMesh = onut::Mesh::create();
                            if (!pMesh->load(filename))
                            {
                                pMesh->release();
                                break;
                            }
                            pContentManager->addResource(filename, pMesh);
                        }
                        pMesh->retain();
                        *ppMesh = pMesh;
                    }
                }
                break;
            case ePropertyType::P_TEXTURE:
                if (jsonElement.isString())
                {
                    auto ppTexture = static_cast<onut::Texture**>(propertyLink.pProperty);
                    auto pContentManager = Game::getGame()->getComponent<ContentManager>();
                    if (ppTexture && pContentManager)
                    {
                        auto filename = "assets/" + jsonElement.asString();
                        auto pTexture = pContentManager->getResource<onut::Texture>(filename);
                        if (!pTexture)
                        {
                            pTexture = onut::Texture::create();
                            if (!pTexture->load(filename))
                            {
                                pTexture->release();
                                break;
                            }
                            pContentManager->addResource(filename, pTexture);
                        }
                        pTexture->retain();
                        *ppTexture = pTexture;
                    }
                }
                break;
            default:
                assert(false);
        }
    }

    return true;
}
コード例 #2
0
ファイル: main.c プロジェクト: cvrahimis/OSSimulator
int main(int argc, const char * argv[]) {
    int rc;
    pthread_t clockThread;
    pthread_t cpuThread;
    
    circularlistnode *doneQ;
    doneQ = (circularlistnode *)malloc(sizeof(circularlistnode));
    doneQ -> next = doneQ;
    doneQ -> prev = doneQ;
    
    circularlistnode *start;
    start = (circularlistnode *)malloc(sizeof(circularlistnode));
    start -> next = start;
    start -> prev = start;
    
    circularlistnode *waitQ;
    waitQ = (circularlistnode *)malloc(sizeof(circularlistnode));
    waitQ -> next = waitQ;
    waitQ -> prev = waitQ;
    
    sharedRes *sharedResource = (sharedRes *) malloc(sizeof(sharedRes));
    sharedResource->properties = loadPropertiesFromFile(argv[1]);
    
    priorityscheduler *scheduler = (priorityscheduler *)malloc(sizeof(priorityscheduler));
    pr_init_scheduler(scheduler, sharedResource->properties->maxPriority);
    
    //int size = (int)ceil(log2(NUM_OF_MEM));
    pagePointer freeMemTable[((int)ceil(log2(sharedResource->properties->numMemoryBlocks))) + 1];
    
    for (int i = 0; i < ((int)ceil(log2(sharedResource->properties->numMemoryBlocks))) + 1; i++) {
        page *start = (page *) malloc(sizeof(page));
        start->next = start;
        start->prev = start;
        freeMemTable[i] = start;
    }
    
    page *firstPage = (page *)malloc(sizeof(page));
    firstPage->startAddress = 0;
    firstPage->size = sharedResource->properties->numMemoryBlocks;

    freeMemTable[((int)log2(sharedResource->properties->numMemoryBlocks))]->next = firstPage;
    freeMemTable[((int)log2(sharedResource->properties->numMemoryBlocks))]->prev = firstPage;
    firstPage->next = freeMemTable[((int)log2(sharedResource->properties->numMemoryBlocks))];
    firstPage->prev = freeMemTable[((int)log2(sharedResource->properties->numMemoryBlocks))];
    
    sharedResource->scheduler = scheduler;
    sharedResource->time = 0;
    sharedResource->doneQ = doneQ;
    sharedResource->doneQSize = 0;
    sharedResource->nextPid = 0;
    sharedResource->waitQ = waitQ;
    if (sharedResource->properties == NULL)
    {
        printf("Unable to read system properties. Cannot run simulation.\n");
        return 1;
    }
    sharedResource->numOfRandGenProcs = sharedResource->properties->maxRandGenProcs;
    sharedResource->freeMemoryArray = freeMemTable;
    
    //NOTE Ready to start generating the number of pages a process needs to execute and figure out hw to give it to them.
    
    pthread_mutex_init(&lock, NULL);

    // Seed the random function.
    srand((unsigned int)time(NULL));

    rc = pthread_create(&clockThread, NULL, cpuClock, (void *)sharedResource);
    if(rc)
    {
        printf("ERROR. Return code from thread %d\n", rc);
        exit(-1);
    }

    rc = pthread_create(&cpuThread, NULL, cpu, (void *)sharedResource);
    if(rc)
    {
        printf("ERROR. Return code from thread %d\n", rc);
        exit(-1);
    }

    rc = pthread_join(clockThread, NULL);
    if(rc)
    {
        printf("Error, return code from thread_join() is %d\n", rc);
        exit(-1);
    }
    rc = pthread_join(cpuThread, NULL);
    if(rc)
    {
        printf("Error, return code from thread_join() is %d\n", rc);
        exit(-1);
    }
    printf("main completed join with thread clock thread \n");
    printf("main completed join with thread readyQ thread \n");
    printf("main completed join with thread cpu thread \n");
    
    printData(sharedResource->doneQ, sharedResource->doneQ->next);
    
    pthread_mutex_destroy(&lock);
    return 0;
}